Debian Codegraph @

test-suite/ltac2/matching.v

raw
1 Require Import Ltac2.Ltac2 Ltac2.Notations.
2
3 Ltac2 Type exn ::= [ Nope ].
4
5 Ltac2 check_constr x y := match Constr.equal x y with
6 | true => ()
7 | false => Control.throw Nope
8 end.
9
10 Ltac2 check_id id id' := match Ident.equal id id' with
11 | true => ()
12 | false => Control.throw Nope
13 end.
14
15 Goal True -> False.
16 Proof.
17 Fail
18 let b := { contents := true } in
19 let f c :=
20 match b.(contents) with
21 | true => Message.print (Message.of_constr c); b.(contents) := false; fail
22 | false => ()
23 end
24 in
25 (** This fails because the matching is not allowed to backtrack once
26 it commits to a branch*)
27 lazy_match! '(nat -> bool) with context [?a] => f a end.
28 lazy_match! Control.goal () with ?a -> ?b => Message.print (Message.of_constr b) end.
29
30 (** This one works by taking the second match context, i.e. ?a := nat *)
31 let b := { contents := true } in
32 let f c :=
33 match b.(contents) with
34 | true => b.(contents) := false; fail
35 | false => Message.print (Message.of_constr c)
36 end
37 in
38 match! '(nat -> bool) with context [?a] => f a end.
39 Abort.
40
41 Goal forall (i j : unit) (x y : nat) (b : bool), True.
42 Proof.
43 Fail match! goal with
44 | [ h : ?t, h' : ?t |- _ ] => ()
45 end.
46 intros i j x y b.
47 match! goal with
48 | [ h : ?t, h' : ?t |- _ ] =>
49 check_id h @x;
50 check_id h' @y
51 end.
52 match! reverse goal with
53 | [ h : ?t, h' : ?t |- _ ] =>
54 check_id h @j;
55 check_id h' @i
56 end.
57
58 pose (def1 := 0 + 0).
59 pose (def2 := true).
60 match! goal with
61 | [ h := _ |- _ ] => check_id h @def2
62 end.
63 match! reverse goal with
64 | [ h := _ |- _ ] => check_id h @def1
65 end.
66 match! goal with
67 | [ h := _ : nat |- _ ] => check_id h @def1
68 end.
69 match! goal with
70 | [ h : nat |- _ ] => check_id h @def1
71 end.
72 match! reverse goal with
73 | [ h : nat |- _ ] => check_id h @x
74 end.
75 match! goal with
76 | [ h := ?v |- _ ] => check_constr v constr:(true)
77 end.
78 match! goal with
79 | [ h := context c [ 0 ] |- _ ] => check_constr (Pattern.instantiate c constr:(3)) constr:(3 + 0)
80 end.
81 match! goal with
82 | [ h1 : context [ unit ] , h2 := context [ 0 ] , h3 : context [ bool ] |- _ ] => ()
83 end.
84 match! goal with
85 | [ h1 : unit , h2 := context [ 0 ] , h3 : context [ bool ] |- _ ] => ()
86 end.
87 lazy_match! goal with
88 | [ h1 : context c1 [ ?t1 ] , h2 := context c2 [ Nat.add ?v2 ] , h3 : context c3 [ ?t2 ] |- _ ] =>
89 check_constr t1 'bool;
90 check_constr v2 '0;
91 check_constr t2 'bool;
92 check_constr (Pattern.instantiate c1 'Empty_set) 'Empty_set;
93 check_constr (Pattern.instantiate c2 '(Nat.add 1)) '(1 + 0);
94 check_constr (Pattern.instantiate c3 'unit) 'unit
95 end.
96 match! goal with
97 | [ h1 : unit , h2 := context c2 [ Nat.add ?v2 ] , h3 : context c3 [ ?t2 ] |- _ ] =>
98 check_constr v2 '0;
99 check_constr t2 'bool;
100 check_constr (Pattern.instantiate c2 '(Nat.add 1)) '(1 + 0);
101 check_constr (Pattern.instantiate c3 'unit) 'unit
102 end.
103 Abort.
104
105 (* Check #79 *)
106 Goal 2 = 3.
107 Control.plus
108 (fun ()
109 => lazy_match! goal with
110 | [ |- 2 = 3 ] => Control.zero (Tactic_failure None)
111 | [ |- 2 = _ ] => Control.zero (Tactic_failure (Some (Message.of_string "should not be printed")))
112 end)
113 (fun e
114 => match e with
115 | Tactic_failure c
116 => match c with
117 | None => ()
118 | _ => Control.zero e
119 end
120 | e => Control.zero e
121 end).
122 Abort.
123
124 Fail Ltac2 Eval fun x => match x with (x,x) => x end.
125 Ltac2 Eval fun x => match x with (x,y) => x end.
126
127 Module StupidTuple.
128 Ltac2 foo x :=
129 match x with
130 | ((a,b),c) => b
131 end .
132
133 Fail Ltac2 Eval foo (1,2).
134 End StupidTuple.
135
136 Module Empty.
137 Ltac2 Type empty := [].
138 Ltac2 bar (x:empty) := match x with end.
139 End Empty.
140
141 Module DeepType.
142 Ltac2 Type rec mylist := [ Nil | One (unit option) | Cons (unit option, mylist) ].
143
144 Fail Ltac2 test x :=
145 match x with
146 | Nil, _ => ()
147 | _, Nil => ()
148 | One None, _ => ()
149 | _, One None => ()
150 end.
151
152 Fail Ltac2 test x :=
153 match x with
154 | Nil, _ => ()
155 | _, Nil => ()
156 | One None, _ => ()
157 | _, One None => ()
158 | Cons _ _, Cons _ _ => ()
159 end.
160
161 Succeed Ltac2 test x :=
162 match x with
163 | Nil, _ => ()
164 | _, Nil => ()
165 | One None, _ => ()
166 | _, One None => ()
167 | _, _ => ()
168 end.
169
170 Succeed Ltac2 test x :=
171 match x with
172 | Nil, _ => ()
173 | _, Nil => ()
174 | One _, _ => ()
175 | _, One _ => ()
176 | Cons _ _, _ => ()
177 | _, Cons _ _ => ()
178 end.
179 End DeepType.
180
181 Module As.
182 Ltac2 option_flat x :=
183 match x with
184 | Some (Some _ as v) => v
185 | Some None | None => None
186 end.
187
188 (* this checks that we didn't parse the pattern as "Some (Some (_ as v))" *)
189 Ltac2 Eval option_flat (Some (Some 0)).
190 End As.
191
192 Module Record.
193
194 Ltac2 bang x := match x with { contents := v } => v end.
195
196 Ltac2 Type exn ::= [ Regression_Test_Failure ].
197
198 Ltac2 check_eq_int a b :=
199 if Int.equal a b then () else Control.throw Regression_Test_Failure .
200
201 Print Ltac2 bang.
202
203 Ltac2 Eval
204 let r := { contents := 0 } in
205 r.(contents) := 1;
206 check_eq_int (bang r) 1.
207
208 Fail Ltac2 Eval
209 let r := { contents := 0 } in
210 r.(contents) := 1;
211 check_eq_int (bang r) 0.
212
213 Ltac2 Type foo := { a : int; b : int }.
214
215 Ltac2 bar x := match x with { a := a } => a end.
216
217 End Record.
218
219 Module Atom.
220
221 Fail Ltac2 mix_atoms x :=
222 match x with
223 | 0 | "" => false
224 | _ => true
225 end.
226
227 Ltac2 match_int x :=
228 match x with
229 | 0 => false
230 | _ => true
231 end.
232 Fail Ltac2 dummy : (int * int) -> _ := match_int.
233
234 Ltac2 match_str x :=
235 match x with
236 | "" => false
237 | _ => true
238 end.
239 Fail Ltac2 dummy : int -> _ := match_str.
240
241 End Atom.