| 1 |
Require Import Ltac2.Ltac2 Ltac2.Notations. |
| 2 |
|
| 3 |
Ltac2 mutable foo () := constructor. |
| 4 |
|
| 5 |
Goal True. |
| 6 |
Proof. |
| 7 |
foo (). |
| 8 |
Qed. |
| 9 |
|
| 10 |
Ltac2 Set foo := fun _ => fail. |
| 11 |
|
| 12 |
Goal True. |
| 13 |
Proof. |
| 14 |
Fail foo (). |
| 15 |
constructor. |
| 16 |
Qed. |
| 17 |
|
| 18 |
|
| 19 |
(** Bindings are dynamic *) |
| 20 |
|
| 21 |
Ltac2 Type rec nat := [O | S (nat)]. |
| 22 |
|
| 23 |
Ltac2 rec nat_eq n m := |
| 24 |
match n with |
| 25 |
| O => match m with | O => true | S _ => false end |
| 26 |
| S n => match m with | O => false | S m => nat_eq n m end |
| 27 |
end. |
| 28 |
|
| 29 |
Ltac2 assert_eq n m := |
| 30 |
match nat_eq n m with |
| 31 |
| true => () |
| 32 |
| false => Control.throw Assertion_failure end. |
| 33 |
|
| 34 |
Ltac2 mutable x := O. |
| 35 |
Ltac2 y () := x. |
| 36 |
Ltac2 Eval (assert_eq (y()) O). |
| 37 |
Ltac2 Set x := (S O). |
| 38 |
Ltac2 Eval (assert_eq (y()) (S O)). |
| 39 |
|
| 40 |
Ltac2 mutable quw := fun (n : nat) => O. |
| 41 |
Ltac2 Set quw := fun n => |
| 42 |
match n with |
| 43 |
| O => O |
| 44 |
| S n => S (S (quw n)) |
| 45 |
end. |
| 46 |
|
| 47 |
Ltac2 Eval (quw (S (S O))). |
| 48 |
|
| 49 |
(** Not the right type *) |
| 50 |
Fail Ltac2 Set foo := 0. |
| 51 |
|
| 52 |
Ltac2 bar () := (). |
| 53 |
|
| 54 |
(** Cannot redefine non-mutable tactics *) |
| 55 |
Fail Ltac2 Set bar := fun _ => (). |
| 56 |
|
| 57 |
(** Subtype check *) |
| 58 |
|
| 59 |
Ltac2 rec h x := h x. |
| 60 |
|
| 61 |
Ltac2 mutable f x := h x. |
| 62 |
Fail Ltac2 Set f := fun x => x. |
| 63 |
|
| 64 |
Ltac2 mutable g x := x. |
| 65 |
Ltac2 Set g := h. |
| 66 |
|
| 67 |
(** Rebinding with old values *) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
Ltac2 mutable qux n := S n. |
| 72 |
|
| 73 |
Ltac2 Set qux as self := fun n => self (self n). |
| 74 |
|
| 75 |
Ltac2 Eval assert_eq (qux O) (S (S O)). |
| 76 |
|
| 77 |
Ltac2 mutable quz := O. |
| 78 |
|
| 79 |
Ltac2 Set quz as self := S self. |
| 80 |
|
| 81 |
Ltac2 Eval (assert_eq quz (S O)). |
| 82 |
|
| 83 |
Ltac2 rec addn n := |
| 84 |
match n with |
| 85 |
| O => fun m => m |
| 86 |
| S n => fun m => S (addn n m) |
| 87 |
|
| 88 |
end. |
| 89 |
Ltac2 mutable rec quy n := |
| 90 |
match n with |
| 91 |
| O => S O |
| 92 |
| S n => S (quy n) |
| 93 |
end. |
| 94 |
|
| 95 |
Ltac2 Set quy as self := fun n => |
| 96 |
match n with |
| 97 |
| O => O |
| 98 |
| S n => addn (self n) (quy n) |
| 99 |
end. |
| 100 |
Ltac2 Eval (assert_eq (quy (S (S O))) (S (S (S O)))). |
| 101 |
Ltac2 Eval (assert_eq (quy (S (S (S O)))) (S (S (S (S (S (S O))))))). |
| 102 |
|
| 103 |
(******************************* |
| 104 |
|
| 105 |
module interactions |
| 106 |
|
| 107 |
********************************) |
| 108 |
|
| 109 |
Ltac2 assert_eq_int x y := Control.assert_true (Int.equal x y). |
| 110 |
|
| 111 |
Module Orig. |
| 112 |
Ltac2 mutable x () := 0. |
| 113 |
End Orig. |
| 114 |
|
| 115 |
Ltac2 Eval assert_eq_int (Orig.x()) 0. |
| 116 |
|
| 117 |
Module Change1. |
| 118 |
Ltac2 Set Orig.x as y := fun () => Int.add (y()) 1. |
| 119 |
Ltac2 Eval assert_eq_int (Orig.x()) 1. |
| 120 |
End Change1. |
| 121 |
|
| 122 |
(* mutation is export not global by default *) |
| 123 |
Ltac2 Eval assert_eq_int (Orig.x()) 0. |
| 124 |
|
| 125 |
Module Change2. |
| 126 |
Ltac2 Set Orig.x as y := fun () => Int.add 5 (Int.mul (y()) 2). |
| 127 |
End Change2. |
| 128 |
|
| 129 |
Module Test1. |
| 130 |
Import Change1. |
| 131 |
|
| 132 |
Ltac2 Eval assert_eq_int (Orig.x()) 1. |
| 133 |
|
| 134 |
Import Change2. |
| 135 |
|
| 136 |
Ltac2 Eval assert_eq_int (Orig.x()) 7. |
| 137 |
End Test1. |
| 138 |
|
| 139 |
Module Test2. |
| 140 |
Import Change2. |
| 141 |
|
| 142 |
Ltac2 Eval assert_eq_int (Orig.x()) 5. |
| 143 |
|
| 144 |
Import Change1. |
| 145 |
|
| 146 |
Ltac2 Eval assert_eq_int (Orig.x()) 6. |
| 147 |
|
| 148 |
(* repeating imports with "as" causes accumulation (!) *) |
| 149 |
Import Change1. |
| 150 |
|
| 151 |
Ltac2 Eval assert_eq_int (Orig.x()) 7. |
| 152 |
|
| 153 |
Import Change1 Change1. |
| 154 |
|
| 155 |
(* Import doesn't deduplicate / assume idempotence (may change in the future?) *) |
| 156 |
Ltac2 Eval assert_eq_int (Orig.x()) 9. |
| 157 |
|
| 158 |
Import Orig. |
| 159 |
|
| 160 |
(* Importing the original definition does not act as a redefinition to the original value *) |
| 161 |
Ltac2 Eval assert_eq_int (Orig.x()) 9. |
| 162 |
End Test2. |
| 163 |
|
| 164 |
Module E1. Export Change1. End E1. |
| 165 |
Module E2. Export Change1. End E2. |
| 166 |
Module E3. |
| 167 |
Export E1. |
| 168 |
Export E2. |
| 169 |
|
| 170 |
Ltac2 Eval assert_eq_int (Orig.x()) 2. |
| 171 |
End E3. |
| 172 |
|
| 173 |
Module Test3. |
| 174 |
Import E3. |
| 175 |
|
| 176 |
(* Import/Export does assume idempotence (!) Change1 only got imported once. *) |
| 177 |
Ltac2 Eval assert_eq_int (Orig.x()) 1. |
| 178 |
End Test3. |
| 179 |
|
| 180 |
Module Depth. |
| 181 |
Ltac2 mutable x () := 0. |
| 182 |
Module Outer. |
| 183 |
Module Inner. |
| 184 |
Ltac2 Set x := fun () => 1. |
| 185 |
End Inner. |
| 186 |
End Outer. |
| 187 |
|
| 188 |
Ltac2 Eval assert_eq_int (x()) 0. |
| 189 |
|
| 190 |
Import Outer. |
| 191 |
Ltac2 Eval assert_eq_int (x()) 0. |
| 192 |
|
| 193 |
Import Inner. |
| 194 |
Ltac2 Eval assert_eq_int (x()) 1. |
| 195 |
End Depth. |
| 196 |
|
| 197 |
Module Locality. |
| 198 |
Ltac2 mutable x () := 0. |
| 199 |
|
| 200 |
Module Inner. |
| 201 |
#[local] Ltac2 Set x := fun () => Control.throw Assertion_failure. |
| 202 |
End Inner. |
| 203 |
|
| 204 |
Ltac2 Eval x(). |
| 205 |
|
| 206 |
Module Inner2. |
| 207 |
#[export] Ltac2 Set x := fun () => Control.throw Assertion_failure. |
| 208 |
End Inner2. |
| 209 |
|
| 210 |
Ltac2 Eval x(). |
| 211 |
Section testS. |
| 212 |
Import Inner2. |
| 213 |
Fail Ltac2 Eval x(). |
| 214 |
End testS. |
| 215 |
|
| 216 |
Ltac2 Eval x(). |
| 217 |
Module Inner3. |
| 218 |
#[global] Ltac2 Set x := fun () => Control.throw Assertion_failure. |
| 219 |
End Inner3. |
| 220 |
Fail Ltac2 Eval x(). |
| 221 |
|
| 222 |
End Locality. |