Debian Codegraph @

test-suite/ltac2/compat.v

raw
1 Require Import Ltac2.Ltac2.
2
3 Import Ltac2.Notations.
4
5 (** Test calls to Ltac1 from Ltac2 *)
6
7 Ltac2 foo () := ltac1:(discriminate).
8
9 Goal true = false -> False.
10 Proof.
11 foo ().
12 Qed.
13
14 Goal true = false -> false = true.
15 Proof.
16 intros H; ltac1:(match goal with [ H : ?P |- _ ] => rewrite H end); reflexivity.
17 Qed.
18
19 Goal true = false -> false = true.
20 Proof.
21 intros H; ltac1:(rewrite H); reflexivity.
22 Abort.
23
24 (** Variables do not cross the compatibility layer boundary. *)
25 Fail Ltac2 bar nay := ltac1:(discriminate nay).
26
27 Fail Ltac2 pose1 (v : constr) :=
28 ltac1:(pose $v).
29
30 (** Variables explicitly crossing the boundary must satisfy typing properties *)
31 Goal True.
32 Proof.
33 (* Wrong type *)
34 Fail ltac1:(x |- idtac) 0.
35 (* OK, and runtime has access to variable *)
36 ltac1:(x |- idtac x) (Ltac1.of_constr constr:(Type)).
37
38 (* Same for ltac1val *)
39 Fail Ltac1.run (ltac1val:(x |- idtac) 0).
40 Ltac1.run (ltac1val:(x |- idtac x) (Ltac1.of_constr constr:(Type))).
41 Abort.
42
43 (** Check value-returning FFI *)
44
45 (* A dummy CPS wrapper in Ltac1 *)
46 Ltac arg k :=
47 match goal with
48 | [ |- ?P ] => k P
49 end.
50
51 Ltac2 testeval v :=
52 let r := { contents := None } in
53 let k c :=
54 let () := match Ltac1.to_constr c with
55 | None => ()
56 | Some c => r.(contents) := Some c
57 end in
58 (* dummy return value *)
59 ltac1val:(idtac)
60 in
61 let tac := ltac1val:(arg) in
62 let () := Ltac1.apply tac [Ltac1.lambda k] (fun _ => ()) in
63 match r.(contents) with
64 | None => fail
65 | Some c => if Constr.equal v c then () else fail
66 end.
67
68 Goal True.
69 Proof.
70 testeval 'True.
71 Abort.
72
73 Goal nat.
74 Proof.
75 testeval 'nat.
76 Abort.
77
78 (* CPS towers *)
79 Ltac2 testeval2 tac :=
80 let fail _ := Control.zero Not_found in
81 let cast c := match Ltac1.to_constr c with
82 | None => fail ()
83 | Some c => c
84 end in
85 let f x y z :=
86 let x := cast x in
87 let y := cast y in
88 let z := cast z in
89 Ltac1.of_constr constr:($x $y $z)
90 in
91 let f := Ltac1.lambda (fun x => Ltac1.lambda (fun y => Ltac1.lambda (fun z => f x y z))) in
92 Ltac1.apply tac [f] Ltac1.run.
93
94 Goal False -> True.
95 Proof.
96 ltac1:(
97 let ff := ltac2:(tac |- testeval2 tac) in
98 ff ltac:(fun k =>
99 let c := k (fun (n : nat) (i : True) (e : False) => i) O I in
100 exact c)
101 ).
102 Qed.
103
104 (** Test calls to Ltac2 from Ltac1 *)
105
106 Set Default Proof Mode "Classic".
107
108 Ltac foo := ltac2:(foo ()).
109
110 Goal true = false -> False.
111 Proof.
112 ltac2:(foo ()).
113 Qed.
114
115 Goal true = false -> False.
116 Proof.
117 foo.
118 Qed.
119
120 (** Variables do not cross the compatibility layer boundary. *)
121 Fail Ltac bar x := ltac2:(foo x).
122
123 Ltac mytac tac := idtac "wow".
124
125 Goal True.
126 Proof.
127 (** Fails because quotation is evaluated eagerly *)
128 Fail mytac ltac2:(fail).
129 (** One has to thunk thanks to the idtac trick *)
130 let t := idtac; ltac2:(fail) in mytac t.
131 constructor.
132 Qed.