-
Notifications
You must be signed in to change notification settings - Fork 19
/
Demo.v
442 lines (342 loc) · 11.6 KB
/
Demo.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
(** * Simple examples on how to use our framework *)
From Coq Require Import String.
From Coq Require Import Basics.
From Coq Require Import List.
From ConCert.Embedding Require Import Ast.
From ConCert.Embedding Require Import Notations.
From ConCert.Embedding Require Import EvalE.
From ConCert.Embedding Require Import PCUICtoTemplate.
From ConCert.Embedding Require Import PCUICTranslate.
From MetaCoq.Utils Require Import monad_utils.
Definition expr_to_tc Σ := compose trans (expr_to_term Σ).
Definition type_to_tc := compose trans type_to_term.
Definition global_to_tc := compose trans_minductive_entry trans_global_dec.
Module TC := Common.BasicAst.
Import ListNotations.
Import MCMonadNotation.
Import BaseTypes.
Import StdLib.
Module MC := MetaCoq.Template.Ast.
Import BasicAst.
(** MetaCoq demo *)
Section MCDemo.
Import bytestring.
Local Open Scope bs.
(* Quote *)
MetaCoq Quote Definition id_nat_syn := (fun x : nat => x).
(* Print id_nat_syn. *)
(* Ast.tLambda (nNamed "x")
(Ast.tInd {| TC.inductive_mind := "nat"; TC.inductive_ind := 0 |}
[]) (Ast.tRel 0) : Ast.term *)
(* Unquote *)
MetaCoq Unquote Definition plus_one :=
(MC.tLambda (aRelevant (nNamed "x"))
(MC.tInd (mkInd (MPfile ["Datatypes"; "Init"; "Coq"], "nat") 0) nil)
(MC.tApp (MC.tConstruct
(mkInd (MPfile ["Datatypes"; "Init"; "Coq"], "nat") 0) 1 nil)
(MC.tRel 0 :: nil))).
(* fun x : nat => S x : nat -> nat *)
End MCDemo.
Definition x := "x".
Definition y := "y".
Definition z := "z".
Definition negb_app_true :=
[|
(\x : Bool =>
case x : Bool return Bool of
| True -> False
| False -> True) True
|].
(* Execute the program using the interpreter *)
Example eval_negb_app_true :
expr_eval_n Σ 3 nil negb_app_true = Ok (vConstr Bool "false" nil).
Proof. reflexivity. Qed.
Example eval_negb_app_true' :
expr_eval_i Σ 3 nil (indexify nil negb_app_true) = Ok (vConstr Bool "false" nil).
Proof. reflexivity. Qed.
(* Make a Coq function from the AST of the program *)
MetaCoq Unquote Definition coq_negb_app_true :=
(expr_to_tc Σ (indexify nil negb_app_true)).
Definition my_negb_syn :=
[| \x : Bool => case x : Bool return Bool of
| True -> False
| False -> True |].
MetaCoq Unquote Definition my_negb :=
(expr_to_tc Σ (indexify nil my_negb_syn)).
Lemma my_negb_coq_negb b :
my_negb b = negb b.
Proof. reflexivity. Qed.
Example eval_my_negb_syn :
expr_eval_n Σ 3 nil my_negb_syn = Ok
(vClos [] "x" cmLam [!Bool!]
[!Bool!]
(eCase (Bool, [])
[!Bool!] [|"x"|]
[({| pName := "true"; pVars := [] |},
[|$ "false" $ Bool|]);
({| pName := "false"; pVars := [] |},
[|$ "true" $ Bool|])])).
Proof. reflexivity. Qed.
Example eval_my_negb_true :
expr_eval_i Σ 4 nil (indexify nil [| {my_negb_syn} True |]) =
Ok (vConstr Bool "false" nil).
Proof. reflexivity. Qed.
MetaCoq Unquote Definition coq_my_negb := (expr_to_tc Σ (indexify nil my_negb_syn)).
Import MCMonadNotation.
Definition is_zero_syn :=
[|
\x : Nat =>
case x : Nat return Bool of
| Zero -> True
| Suc y -> False
|].
MetaCoq Unquote Definition is_zero' := (expr_to_tc Σ (indexify nil is_zero_syn)).
Definition pred_syn :=
[|
\x : Nat =>
case x : Nat return Nat of
| Zero -> x
| Suc y -> y
|].
MetaCoq Unquote Definition pred' := (expr_to_tc Σ (indexify nil pred_syn)).
Definition prog2 := [| Suc (Suc Zero) |].
Example value_eval :
expr_eval_n Σ 3 nil prog2 = Ok (vConstr Nat "Suc"
[vConstr Nat "Suc"
[vConstr Nat "Z" []]]).
Proof. reflexivity. Qed.
Example eval_is_zero_true :
expr_eval_i Σ 4 nil (indexify nil [|{is_zero_syn} Zero |]) =
Ok (vConstr Bool "true" []).
Proof. reflexivity. Qed.
Example eval_is_zero_false :
expr_eval_i Σ 4 nil (indexify nil [|{is_zero_syn} {prog2} |]) =
Ok (vConstr Bool "false" []).
Proof. reflexivity. Qed.
Inductive blah :=
Bar : blah -> blah -> blah
| Baz : blah.
Definition Σ' : global_env :=
[gdInd "blah" 0 [("Bar", [(None,tyInd "blah"); (None,tyInd "blah")]); ("Baz", [])] false;
gdInd Nat 0 [("Z", []); ("Suc", [(None,tyInd Nat)])] false].
Notation "'Bar'" := (eConstr "blah" "Bar") (in custom expr).
Notation "'Baz'" := (eConstr "blah" "Baz") (in custom expr).
Definition prog3 := [| Bar (Bar Baz Baz) Baz |].
Example eval_prog3 :
expr_eval_n Σ' 5 [] prog3 = Ok
(vConstr "blah" "Bar"
[vConstr "blah" "Bar"
[vConstr "blah" "Baz" []; vConstr "blah" "Baz" []];
vConstr "blah" "Baz" []]).
Proof. reflexivity. Qed.
(* Examples of a fixpoint *)
Definition fact := "fact".
Definition factorial_syn :=
[|
fix fact (x : Nat) : Nat :=
case x : Nat return Nat of
| Zero -> 1
| Suc y -> x * (fact y)
|].
MetaCoq Unquote Definition factorial :=
(expr_to_tc Σ (indexify [] factorial_syn)).
Definition plus_syn : expr :=
[| fix "plus" (x : Nat) : Nat -> Nat :=
\y : Nat =>
case x : Nat return Nat of
| Zero -> y
| Suc z -> Suc ("plus" z y) |].
MetaCoq Unquote Definition my_plus := (expr_to_tc Σ (indexify [] plus_syn)).
Lemma my_plus_correct n m :
my_plus n m = n + m.
Proof. induction n; simpl; auto. Qed.
Definition two :=
(vConstr Nat "Suc"
[vConstr Nat "Suc" [vConstr Nat "Z" []]]).
Definition one_plus_one :=
[| {plus_syn} 1 1 |].
Example eval_one_plus_one :
expr_eval_n Σ 10 [] one_plus_one =
Ok (vConstr Nat "Suc" [vConstr Nat "Suc" [vConstr Nat "Z" []]]).
Proof. reflexivity. Qed.
Definition two_arg_fun_syn := [| \x : Nat => \y : Bool => x |].
MetaCoq Unquote Definition two_arg_fun_app :=
(expr_to_tc Σ (indexify [] [| {two_arg_fun_syn} 1 True |])).
Parameter bbb: bool.
MetaCoq Quote Definition two_arg_fun_app_syn' := ((fun (x : nat) (_ : bool) => x) 1 bbb).
Example one_plus_one_two :
expr_eval_n Σ 10 [] one_plus_one = Ok two.
Proof. reflexivity. Qed.
Example one_plus_one_two_i :
expr_eval_i Σ 10 [] (indexify [] one_plus_one) = Ok two.
Proof. reflexivity. Qed.
Definition plus_syn' :=
[| \x : Nat =>
(fix "plus" (y : Nat) : Nat :=
case y : Nat return Nat of
| Zero -> x
| Suc z -> Suc ("plus" z))
|].
MetaCoq Unquote Definition my_plus' :=
(expr_to_tc Σ (indexify [] plus_syn')).
Lemma my_plus'_0 : forall n, my_plus' 0 n = n.
Proof.
induction n; simpl; easy.
Qed.
Lemma my_plus'_Sn : forall n m, my_plus' (S n) m = S (my_plus' n m).
Proof.
induction m; simpl; easy.
Qed.
Lemma my_plus'_comm : forall n m, my_plus' n m = my_plus' m n.
Proof.
induction n; intros m; simpl.
+ rewrite my_plus'_0. reflexivity.
+ rewrite my_plus'_Sn. easy.
Qed.
(* my_plus corresponds to addition of natural numbers defined in the standard library *)
Lemma my_plus'_correct : forall n m, my_plus' n m = n + m.
Proof.
intros n m.
induction m; simpl; easy.
Qed.
Definition id_rec :=
[| (fix "plus" (y : Nat) : Nat :=
case y : Nat return Nat of
| Zero -> 0
| Suc z -> Suc ("plus" z))
|].
Example eval_id_rec :
expr_eval_n Σ 20 [] [| {id_rec} (Suc (Suc (Suc 1))) |] =
Ok (vConstr Nat "Suc"
[vConstr Nat "Suc"
[vConstr Nat "Suc"
[vConstr Nat "Suc"
[vConstr Nat "Z" []]]]]).
Proof. reflexivity. Qed.
Example eval_id_rec' :
expr_eval_i Σ 20 [] (indexify [] [| {id_rec} (Suc (Suc (Suc 1))) |]) =
Ok (vConstr Nat "Suc"
[vConstr Nat "Suc"
[vConstr Nat "Suc"
[vConstr Nat "Suc"
[vConstr Nat "Z" []]]]]).
Proof. reflexivity. Qed.
Example id_rec_named_and_indexed :
let arg := [| Suc (Suc (Suc 1)) |] in
expr_eval_n Σ 20 [] [| {id_rec} {arg} |] =
expr_eval_i Σ 20 [] (indexify [] [| {id_rec} {arg} |]).
Proof. reflexivity. Qed.
Example plus_named_and_indexed :
let two := [| (Suc 1)|] in
let three := [| Suc {two} |] in
expr_eval_n Σ 20 [] [| ({plus_syn} {two}) {three} |] =
expr_eval_i Σ 20 [] (indexify [] [| ({plus_syn} {two}) {three} |]).
Proof. reflexivity. Qed.
Example eval_plus_syn_one :
expr_eval_i Σ 10 [] (indexify [] [| {plus_syn} 1 |]) =
Ok (vClos
[("x",
vConstr Nat "Suc"
[vConstr Nat "Z" []]);
("plus",
vClos [] "x" (cmFix "plus") [!Nat!]
(tyArr [!Nat!]
[!Nat!])
[|\ "y" : Nat =>
{eCase (Nat, [])
[!Nat!] (eRel 1)
[({| pName := "Z"; pVars := [] |}, eRel 0);
({| pName := "Suc"; pVars := ["z"] |},
eApp [|$ "Suc" $ Nat|]
(eApp (eApp (eRel 3) (eRel 0)) (eRel 1)))]}|])] "y"
cmLam [!Nat!] [!Nat!]
(eCase (Nat, [])
[!Nat!] (eRel 1)
[({| pName := "Z"; pVars := [] |}, eRel 0);
({| pName := "Suc"; pVars := ["z"] |},
eApp [|$ "Suc" $ Nat|]
(eApp (eApp (eRel 3) (eRel 0)) (eRel 1)))])).
Proof. reflexivity. Qed.
Example eval_plus_syn :
indexify [] [| {plus_syn}|] =
[|fix "plus" ("x" : Nat)
: Nat -> Nat :=
\ "y" : Nat =>
{eCase (Nat, [])
[!Nat!] (eRel 1)
[({| pName := "Z"; pVars := [] |}, eRel 0);
({| pName := "Suc"; pVars := ["z"] |},
eApp [|$ "Suc" $ Nat|]
(eApp (eApp (eRel 3) (eRel 0)) (eRel 1)))]}|].
Proof. reflexivity. Qed.
Example eval_plus_syn_zero :
expr_eval_n Σ 10 [] [| {plus_syn} 0 |] =
Ok (vClos
[("x", vConstr Nat "Z" []);
("plus",
vClos [] "x" (cmFix "plus") [!Nat!]
(tyArr [!Nat!]
[!Nat!])
[|\ "y" : Nat =>
{eCase (Nat, [])
[!Nat!] [|"x"|]
[({| pName := "Z"; pVars := [] |}, [|"y"|]);
({| pName := "Suc"; pVars := ["z"] |},
eApp [|$ "Suc" $ Nat|]
(eApp (eApp [|"plus"|] [|"z"|]) [|"y"|]))]}|])] "y"
cmLam [!Nat!] [!Nat!]
(eCase (Nat, [])
[!Nat!] [|"x"|]
[({| pName := "Z"; pVars := [] |}, [|"y"|]);
({| pName := "Suc"; pVars := ["z"] |},
eApp [|$ "Suc" $ Nat|]
(eApp (eApp [|"plus"|] [|"z"|]) [|"y"|]))])).
Proof. reflexivity. Qed.
Definition fun_app := [| (\x : Nat => \y : Nat => y + x) Zero |].
Example eval_fun_app :
expr_eval_n Σ' 10 [] fun_app =
Ok (vClos [("x", vConstr Nat "Z" [])] "y" cmLam
[!Nat!] [!Nat!]
(eApp (eApp (eConst "Coq/Init/Nat@add") [|"y"|]) [|"x"|])).
Proof. reflexivity. Qed.
Inductive mybool :=
| mfalse
| mtrue.
Definition stupid_case (b : mybool) : nat :=
match b with
| mtrue => 1
| mfalse => 0
end.
Definition stupid_case' (b : mybool * mybool) : nat :=
match b with
| (mtrue, _) => 1
| (mfalse, _) => 0
end.
Definition is_zero (n : nat) :=
match n with
| S n => mfalse
| O => mtrue
end.
MetaCoq Quote Definition q_stupid_case := Eval compute in stupid_case.
(* Nested patters are transformed into the nested "case" expressions *)
MetaCoq Quote Definition q_stupid_case' := Eval compute in stupid_case'.
Inductive Bazz :=
cBazz : nat -> nat -> nat -> Bazz.
Definition Bazz_match b :=
match b with
cBazz n m k => n
end.
MetaCoq Quote Definition q_Bazz_match := Eval compute in Bazz_match.
(** Inductives *)
Definition Nat_syn :=
[\ data "MyNat" =
"Z" [_]
| "Suc" ["MyNat", _] \].
MetaCoq Unquote Inductive (global_to_tc Nat_syn).
(** Records *)
Import Template.Ast.
Unset Primitive Projections.
Definition State_syn :=
[\ record "State" := "mkState" { "balance" : Nat ; "day" : Nat } \].
MetaCoq Unquote Inductive (global_to_tc State_syn).
(* Print State. *)