| 1 |
Require Import Ltac2.Ltac2. |
| 2 |
|
| 3 |
(** Ltac2 is typed à la ML. *) |
| 4 |
|
| 5 |
Ltac2 test0 n := Int.add n 1. |
| 6 |
|
| 7 |
Print Ltac2 test0. |
| 8 |
|
| 9 |
Ltac2 test1 () := test0 0. |
| 10 |
|
| 11 |
Print Ltac2 test1. |
| 12 |
|
| 13 |
Fail Ltac2 test2 () := test0 true. |
| 14 |
|
| 15 |
Fail Ltac2 test2 () := test0 0 0. |
| 16 |
|
| 17 |
Ltac2 test3 f x := x, (f x, x). |
| 18 |
|
| 19 |
Print Ltac2 test3. |
| 20 |
|
| 21 |
(** Polymorphism *) |
| 22 |
|
| 23 |
Ltac2 rec list_length l := |
| 24 |
match l with |
| 25 |
| [] => 0 |
| 26 |
| x :: l => Int.add 1 (list_length l) |
| 27 |
end. |
| 28 |
|
| 29 |
Print Ltac2 list_length. |
| 30 |
|
| 31 |
(** Pattern-matching *) |
| 32 |
|
| 33 |
Ltac2 ifb b f g := match b with |
| 34 |
| true => f () |
| 35 |
| false => g () |
| 36 |
end. |
| 37 |
|
| 38 |
Print Ltac2 ifb. |
| 39 |
|
| 40 |
Ltac2 if_not_found e f g := match e with |
| 41 |
| Not_found => f () |
| 42 |
| _ => g () |
| 43 |
end. |
| 44 |
|
| 45 |
Fail Ltac2 ifb' b f g := match b with |
| 46 |
| true => f () |
| 47 |
end. |
| 48 |
|
| 49 |
Fail Ltac2 if_not_found' e f g := match e with |
| 50 |
| Not_found => f () |
| 51 |
end. |
| 52 |
|
| 53 |
(** Reimplementing 'do'. Return value of the function useless. *) |
| 54 |
|
| 55 |
Ltac2 rec do n tac := match Int.equal n 0 with |
| 56 |
| true => () |
| 57 |
| false => tac (); do (Int.sub n 1) tac |
| 58 |
end. |
| 59 |
|
| 60 |
Print Ltac2 do. |
| 61 |
|
| 62 |
(** Non-function pure values are OK. *) |
| 63 |
|
| 64 |
Ltac2 tuple0 := ([1; 2], true, (fun () => "yay")). |
| 65 |
|
| 66 |
Print Ltac2 tuple0. |
| 67 |
|
| 68 |
(** Impure values are not. *) |
| 69 |
|
| 70 |
Fail Ltac2 not_a_value := { contents := 0 }. |
| 71 |
Fail Ltac2 not_a_value := "nope". |
| 72 |
Fail Ltac2 not_a_value := list_length []. |
| 73 |
|
| 74 |
(** Check type ascription on letrec *) |
| 75 |
|
| 76 |
Ltac2 rec loop : 'a -> 'b := fun x => loop x. |
| 77 |
Fail Ltac2 rec loop2 : bool -> bool := fun x => if x then loop2 false else 0. |
| 78 |
|
| 79 |
Fail Ltac2 rec not_a_fun := (). |