-
Notifications
You must be signed in to change notification settings - Fork 0
/
Restr.v
157 lines (132 loc) · 4.14 KB
/
Restr.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
From iVM Require Import Lens DSet.
Unset Suggest Proof Using.
(** ** Restriction lenses *)
Import DSetNotations.
Section restriction_section.
Context {A : Type} {F : A -> Type}.
Definition restr u : Type := forall (a: A), a ∈ u -> F a.
Local Notation S := (forall a, F a).
#[refine] Instance fullLens : Lens S (restr Ω) :=
{
proj f a _ := f a;
update _ g a := g a I;
}.
Proof.
all: unfold restr; try reflexivity.
cbn. intros f g.
extensionality a.
extensionality t.
destruct t. reflexivity.
Defined.
Proposition fullLens_is_all : fullLens ≃ sndMixer.
Proof. easy. Qed.
#[refine] Instance subsetLens {u v} (Huv: u ⊆ v) : Lens (restr v) (restr u) :=
{
proj f a Ha := f a (Huv a Ha);
update f g a Hv := match decide (a ∈ u) with
| left Hu => g a Hu
| _ => f a Hv
end;
}.
Proof.
- abstract (intros f g;
extensionality a;
extensionality Ha;
decided Ha;
reflexivity).
- abstract (intros f;
extensionality a;
extensionality Hv;
destruct (decide _) as [Hu|_];
[ f_equal; apply is_true_unique
| reflexivity ]).
- abstract (intros f g g';
extensionality a;
extensionality Hv;
destruct (decide _);
reflexivity).
Defined.
Instance restrLens u : Lens S (restr u) :=
subsetLens full_terminal ∘ fullLens.
Proposition emptyLens_is_void : restrLens ∅ ≃ fstMixer.
Proof. intros f g. now extensionality a. Qed.
(** By construction *)
Instance full_sublens u : (restrLens u | fullLens).
Proof.
apply sublens_comp'.
Qed.
Global Instance restrLens_proper_sub :
Proper (@subset A ==> @Submixer S) restrLens.
Proof.
intros u v Huv.
intros f g h. extensionality a. cbn.
destruct (decide (a ∈ u)) as [Hu|Hu];
destruct (decide (a ∈ v)) as [Hv|Hv];
try reflexivity.
exfalso.
apply Hv, Huv, Hu.
Qed.
(* TODO: Useful? *)
Instance submixer_subset {u v} (Huv: u ⊆ v) : (restrLens u | restrLens v).
Proof.
(* Does not work in 8.12: [rewrite Huv.]
cf. Coq issue #4175. *)
apply restrLens_proper_sub. exact Huv.
Qed.
Instance separate_independent u v (Huv: u # v) :
Independent (restrLens u) (restrLens v).
Proof.
intros f g h. extensionality a. cbn.
destruct (decide (a ∈ v)) as [Hv|Hv];
destruct (decide (a ∈ u)) as [Hu|Hu];
try reflexivity.
exfalso.
apply (Huv a).
split; assumption.
Qed.
(** ** Point lenses, [restrLens !{a}] simplified *)
Context {H_eqdec: EqDec A}.
#[refine] Instance pointLens' {a u} (Ha: a ∈ u) : Lens (restr u) (F a) :=
{
proj f := f a Ha;
update f x a' Hu := match decide (a = a') with
| left H => rew H in x
| _ => f a' Hu
end;
}.
Proof.
- abstract (intros f x;
decided (@eq_refl _ a);
revert H;
apply EqDec.UIP_K;
reflexivity).
- intros f;
extensionality a';
extensionality Hu;
destruct (decide (a = a')) as [H|H];
[ subst a; cbn; f_equal; apply is_true_unique
| reflexivity ].
- abstract (intros f x x';
extensionality a';
extensionality Hu;
destruct (decide (a = a')) as [H|H];
reflexivity).
Defined.
Instance pointLens a : Lens S (F a) := pointLens' full_spec ∘ fullLens.
Instance pointLens_sublens {a u} (Ha: a ∈ u) : (pointLens a | restrLens u).
Proof.
intros f g h. extensionality a'. cbn.
destruct (decide (a = a')) as [H|H].
- subst a. decided Ha. reflexivity.
- destruct (decide _); reflexivity.
Qed.
Instance pointLens_independent {a a'} (Ha: a <> a') :
Independent (pointLens a) (pointLens a').
Proof.
intros f x x'. cbn.
extensionality a''.
destruct (decide (a' = a'')) as [He'|He'];
destruct (decide (a = a'')) as [He|He];
congruence.
Qed.
End restriction_section.