Debian Codegraph @

test-suite/ltac2/ind.v

raw
1 Require Import Ltac2.Ltac2.
2 Require Import Ltac2.Option.
3
4 Ltac2 Eval
5 let nat := Option.get (Env.get [@Corelib; @Init; @Datatypes; @nat]) in
6 let nat := match nat with
7 | Std.IndRef nat => nat
8 | _ => Control.throw Not_found
9 end in
10 let data := Ind.data nat in
11 (* Check that there is only one inductive in the block *)
12 let ntypes := Ind.nblocks data in
13 let () := if Int.equal ntypes 1 then () else Control.throw Not_found in
14 let nat' := Ind.repr (Ind.get_block data 0) in
15 (* Check it corresponds *)
16 let () := if Ind.equal nat nat' then () else Control.throw Not_found in
17 let () := if Int.equal (Ind.index nat) 0 then () else Control.throw Not_found in
18 (* Check the number of constructors *)
19 let nconstr := Ind.nconstructors data in
20 let () := if Int.equal nconstr 2 then () else Control.throw Not_found in
21 (* Check that we can get the inductive type from a pattern match expression *)
22 let tm := '(match 0 with 0 => 0 | S n => n end) in
23 let ind := match Constr.Unsafe.kind tm with
24 | Constr.Unsafe.Case case _ _ _ _ => Constr.Unsafe.Case.inductive case
25 | _ => Control.throw Not_found end in
26 (if Ind.equal ind nat then () else Control.throw Not_found);
27 (* Create a fresh instance *)
28 let s := Ind.get_constructor data 1 in
29 let s := Env.instantiate (Std.ConstructRef s) in
30 constr:($s 0).
31
32 Ltac2 Eval
33 let acc := Option.get (Env.get [@Corelib; @Init; @Wf; @Acc]) in
34 let acc := match acc with
35 | Std.IndRef acc => acc
36 | _ => Control.throw Not_found
37 end in
38 let data := Ind.data acc in
39 let () :=
40 if Int.equal (Ind.nparams data) 3 (* A, R, x are parameters. *)
41 then () else Control.throw Not_found in
42 let () :=
43 if Int.equal (Ind.nparams_uniform data) 2 (* A, R are recursively uniform parameters. *)
44 then () else Control.throw Not_found in
45 let () :=
46 if Int.equal (Array.get (Ind.constructor_nargs data) 0) 1
47 then () else Control.throw Not_found in
48 ()
49 .
50
51 Inductive ltac2_ind_test (A B: Type) (f : A -> B) (a : A) (b:= f a) : A -> B -> Type
52 := c0 (a0 : A) (b0 := f a0) : ltac2_ind_test A B f a a0 b0.
53
54 Ltac2 Eval
55 let acc := Option.get (Env.get [@ind; @ltac2_ind_test]) in
56 let acc := match acc with
57 | Std.IndRef acc => acc
58 | _ => Control.throw Not_found
59 end in
60 let data := Ind.data acc in
61 let () :=
62 if Int.equal (Ind.nparams data) 4 (* A, B, f, a are parameters. *)
63 then () else Control.throw Not_found in
64 let () :=
65 if Int.equal (Array.get (Ind.constructor_nargs data) 0) 1
66 (* a0 is the only argument of c0. *)
67 then () else Control.throw Not_found in
68 let () :=
69 if Int.equal (Array.get (Ind.constructor_ndecls data) 0) 2
70 (* a0 and b0 can both be bound when pattern matching. *)
71 then () else Control.throw Not_found in
72 ()
73 .