-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamples.v
342 lines (291 loc) · 7.63 KB
/
Examples.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
From mathcomp Require Import all_ssreflect all_algebra.
From mathcomp.analysis Require Import boolp reals distr.
Require Import Util.
Require Import Rml.
Require Import Rml_semantic.
(** * Helper **)
Fixpoint check_valid (A : Type) (vl : seq (nat * Type)) (fl : seq (nat * Type)) (x : Rml) {struct x} : bool :=
match x with
| Var p true => List.existsb (fun (a : nat * Type) => (p.1 == a.1) && asbool (p.2 = a.2)) fl && asbool (p.2 = A)
| Var p false => List.existsb (fun (a : nat * Type) => (p.1 == a.1) && asbool (p.2 = a.2)) vl && asbool (p.2 = A)
| Const A0 a => asbool (A = A0)
| Let_stm p x1 x2 => check_valid p.2 vl fl x1 && check_valid A (p :: vl) fl x2
| If_stm b m1 m2 => check_valid bool vl fl b && check_valid A vl fl m1 && check_valid A vl fl m2
| App_stm T e1 e2 => check_valid (T -> A) vl fl e1 && check_valid T vl fl e2
| Let_rec T T0 nf nx e1 e2 => check_valid A vl [:: (nx, T), (nf, T -> T0) & fl] e1 && check_valid T vl fl e2 && asbool (T0 = A)
| Random x => check_valid nat vl fl x && asbool ((nat <: Type) = A)
| Flip => asbool ((bool <: Type) = A)
end.
Theorem type_checker :
forall A vl fl x, check_valid A vl fl x = true <-> rml_valid_type A vl fl x.
Proof.
assert (asbool_true : forall (k : Type), `[< k = k >] = true) by (intros ; apply (@asbool_equiv_eqP (k = k) true true) ; constructor ; easy ; easy).
intros.
split.
{ intros.
generalize dependent fl.
generalize dependent vl.
generalize dependent A.
induction x ; intros.
{ destruct b.
{
inversion H.
rewrite H1.
apply andb_prop in H1.
inversion_clear H1.
apply List.existsb_exists in H0.
destruct H0.
inversion_clear H0.
apply andb_prop in H3.
inversion_clear H3.
apply asboolW in H2.
apply asboolW in H4.
apply PeanoNat.Nat.eqb_eq in H0.
subst.
rewrite (surjective_pairing p).
rewrite H0.
rewrite H4.
rewrite <- (surjective_pairing x).
apply (valid_fun_var vl fl x).
assumption.
}
{
inversion H.
apply andb_prop in H1.
inversion_clear H1.
apply List.existsb_exists in H0.
destruct H0.
inversion_clear H0.
apply andb_prop in H3.
inversion_clear H3.
apply asboolW in H2.
apply asboolW in H4.
apply PeanoNat.Nat.eqb_eq in H0.
rewrite (surjective_pairing p).
rewrite H0.
rewrite H4.
rewrite <- (surjective_pairing x).
subst.
rewrite H4.
apply (valid_var vl fl x).
assumption.
}
}
{
inversion H.
apply asboolW in H1.
subst.
constructor.
}
{
inversion H.
apply andb_prop in H1.
inversion_clear H1.
apply IHx1 in H0.
apply IHx2 in H2.
constructor ; assumption.
}
{
inversion H.
apply andb_prop in H1.
inversion_clear H1.
apply andb_prop in H0.
inversion_clear H0.
apply IHx1 in H1.
apply IHx2 in H3.
apply IHx3 in H2.
constructor ; assumption.
}
{
inversion H.
apply andb_prop in H1.
inversion_clear H1.
apply IHx1 in H0.
apply IHx2 in H2.
constructor ; assumption.
}
{
inversion H.
apply andb_prop in H1.
inversion_clear H1.
apply andb_prop in H0.
inversion_clear H0.
apply asboolW in H2.
apply IHx1 in H1.
apply IHx2 in H3.
subst.
constructor ; assumption.
}
{
inversion H.
apply andb_prop in H1.
inversion_clear H1.
apply asboolW in H2.
apply IHx in H0.
subst.
constructor ; assumption.
}
{
inversion H.
apply asboolW in H1.
subst.
constructor.
}
}
{
intros.
generalize dependent fl.
generalize dependent vl.
generalize dependent A.
induction x ; intros.
{
inversion H ; subst.
{
simpl.
apply andb_true_intro.
split.
apply List.existsb_exists.
exists p.
split.
assumption.
apply andb_true_intro.
split.
apply eq_refl.
apply (asbool_true p.2).
apply (asbool_true p.2).
}
{
simpl.
apply andb_true_intro.
split.
apply List.existsb_exists.
exists p.
split.
assumption.
apply andb_true_intro.
split.
apply eq_refl.
apply (asbool_true p.2).
apply (asbool_true p.2).
}
}
{
inversion H ; subst.
simpl.
apply (asbool_true A).
}
{
inversion H ; subst.
simpl.
apply andb_true_intro.
split.
apply IHx1.
assumption.
apply IHx2.
assumption.
}
{
inversion H ; subst.
simpl.
apply andb_true_intro.
split.
apply andb_true_intro.
split.
apply IHx1.
assumption.
apply IHx2.
assumption.
apply IHx3.
assumption.
}
{
inversion H ; subst.
simpl.
apply andb_true_intro.
split.
apply IHx1.
assumption.
apply IHx2.
assumption.
}
{
inversion H ; subst.
simpl.
apply andb_true_intro.
split.
apply andb_true_intro.
split.
apply IHx1 ; assumption.
apply IHx2 ; assumption.
apply (asbool_true T0).
}
{
inversion H ; subst.
simpl.
apply andb_true_intro.
split.
apply IHx ; assumption.
apply (asbool_true nat).
}
{
inversion H ; subst.
simpl.
apply (asbool_true bool).
}
}
Defined.
Fixpoint option_ssem {R : realType} {T : Type} (x : Rml) : option {distr (Choice T) / R}.
destruct (check_valid T nil nil x) eqn : cv.
- apply type_checker in cv.
exact (Some (@ssem R T x cv)).
- exact None.
Qed.
(** * Examples **)
Definition some : Rml :=
Let_rec nat nat 0 1
(Random (Var (1,nat <: Type) true))
(Const 10).
Definition some_valid : rml_valid_type nat nil nil some.
Proof.
assert (check_valid nat nil nil some = true).
native_compute.
destruct boolp.pselect.
reflexivity.
contradiction.
apply type_checker.
assumption.
Qed.
Definition some_valid2 : rml_valid_type nat nil nil some.
constructor.
- constructor.
+ apply (valid_fun_var nil [:: (1,nat <: Type); (0,nat -> nat <: Type)] (1,nat <: Type)).
left.
reflexivity.
+ constructor.
Defined.
Compute (@replace_all_variables_aux_type nat some nil nil (env_nil nil) some_valid).
Compute (@replace_all_variables_aux_type nat some nil nil (env_nil nil) some_valid2).
Check @ssem_aux _ nat (sFix nat 0 1 (sRandom _ (sVar 1)) (sConst 10)) nil (svalid_fix nat [::] nat 0 1 (sRandom _ (sVar 1))
(sConst 10)
(svalid_random nat [:: (1, nat <: Type); (0, nat -> nat <: Type)]
(sVar 1) _
(svalid_fun_var nat [:: (1, nat <: Type); (0, nat -> nat <: Type)] 1
_)) (svalid_const nat [::] 10)).
Compute @ssem_aux _ nat (sConst 10) nil (svalid_const nat nil 10).
Check @ssem.
From xhl Require Import pwhile.pwhile.
Compute @ssem R nat (Const 10) (valid_const nat nil nil 10).
Lemma is10 :
@ssem R nat (Const 10) (valid_const nat nil nil 10) = @dunit R (Choice nat) 10.
Proof.
simpl.
reflexivity.
Qed.
Lemma is_random :
@ssem R nat (Random (Const 10)) (valid_random nil nil (Const 10) (valid_const nat nil nil 10)) = @duni R (Choice nat) (range 10).
Proof.
simpl.
compute.
native_compute.
reflexivity.
Qed.