| 1 |
Require Import Ltac2.Ltac2. |
| 2 |
|
| 3 |
Ltac2 Custom Entry mycustom. |
| 4 |
|
| 5 |
Ltac2 Notation "mycustom:(" x(mycustom) ")" := x. |
| 6 |
|
| 7 |
(* NB this test declares level not in order deliberately *) |
| 8 |
|
| 9 |
(* custom entries get autodeclared as RIGHTA, but we can still do left |
| 10 |
associative notations *) |
| 11 |
Ltac2 Notation x(self) "+" y(next) : mycustom(2) := (x, y). |
| 12 |
|
| 13 |
(* we can also do right associative notations *) |
| 14 |
Ltac2 Notation x(self) "-" y(self) : mycustom(1) := (x, y). |
| 15 |
|
| 16 |
(* put back atomic tactics in mycustom *) |
| 17 |
Ltac2 Notation x(tactic(0)) : mycustom(0) := x. |
| 18 |
|
| 19 |
Ltac2 Eval (mycustom:("" + 1 + true) : (string * int) * bool). |
| 20 |
Ltac2 Eval (mycustom:("" - 1 - true) : string * (int * bool)). |
| 21 |
|
| 22 |
(* - (level 1) binds stronger than + (level 2) *) |
| 23 |
Ltac2 Eval (mycustom:("" + 1 - true) : string * (int * bool)). |
| 24 |
Ltac2 Eval (mycustom:("" - 1 + true) : (string * int) * bool). |
| 25 |
|
| 26 |
|
| 27 |
Ltac2 Custom Entry math. |
| 28 |
|
| 29 |
(* unspecified level means highest possible level *) |
| 30 |
Ltac2 Notation "math:(" x(math) ")" : 0 := x. |
| 31 |
Ltac2 Notation "(" x(math) ")" : math(0) := x. |
| 32 |
|
| 33 |
(* next on the right: these are all left associative *) |
| 34 |
Ltac2 Notation x(self) "<" y(next) : math(70) := (Int.lt x y). |
| 35 |
Ltac2 Notation x(self) "<=" y(next) : math(70) := (Int.le x y). |
| 36 |
Ltac2 Notation x(self) "=" y(next) : math(70) := (Int.equal x y). |
| 37 |
Ltac2 Notation x(self) "+" y(next) : math(50) := (Int.add x y). |
| 38 |
Ltac2 Notation x(self) "-" y(next) : math(50) := (Int.sub x y). |
| 39 |
Ltac2 Notation x(self) "*" y(next) : math(40) := (Int.mul x y). |
| 40 |
Ltac2 Notation x(self) "/" y(next) : math(40) := (Int.div x y). |
| 41 |
|
| 42 |
Ltac2 Notation "-" x(self) : math(35) := (Int.neg x). |
| 43 |
|
| 44 |
(* ltac2_atom and int aren't available as a custom entry (should it |
| 45 |
be?) so we need to use tactic(0) to parse ltac2 references and |
| 46 |
literal ints |
| 47 |
|
| 48 |
Since tactic(0) includes parenthesized tactics we can't insert it |
| 49 |
directly like we did for mycustom, so instead use the ' prefix *) |
| 50 |
Ltac2 Notation "'" x(tactic(0)) : math(0) := x. |
| 51 |
|
| 52 |
Ltac2 Eval Control.assert_true (Int.equal 14 math:('2 + '3 * '4)). |
| 53 |
Ltac2 Eval Control.assert_true (Int.equal 20 math:(('2 + '3) * '4)). |
| 54 |
|
| 55 |
Ltac2 Eval Control.assert_true math:('2 < '2 + '1). |
| 56 |
Ltac2 Eval Control.assert_false math:('2 < '2 - '1). |
| 57 |
|
| 58 |
Ltac2 Eval Control.assert_true math:('3 = '0 - - '3). |
| 59 |
Ltac2 Eval Control.assert_true math:('2 = '3 * '2 / '3). |
| 60 |
Ltac2 Eval Control.assert_true math:('0 = '3 * ('2 / '3)). |
| 61 |
|
| 62 |
Ltac2 Eval Control.assert_true math:(- - '1 = - - - - '1). |
| 63 |
|
| 64 |
(* future work: autodeclare unknown levels *) |
| 65 |
Fail Ltac2 Notation "bad" x(mycustom(3)) : mycustom(2) := x. |
| 66 |
(* Ltac2 Eval mycustom:(bad 0). *) |