| 1 |
(* This file is giving some examples about how implicit arguments and |
| 2 |
scopes are treated when using abbreviations or notations, in terms |
| 3 |
or patterns, or when using @ and parentheses in terms and patterns. |
| 4 |
|
| 5 |
The convention is: |
| 6 |
|
| 7 |
Constant foo with implicit arguments and scopes used in a term or a pattern: |
| 8 |
|
| 9 |
foo do not deactivate further arguments and scopes |
| 10 |
@foo deactivate further arguments and scopes |
| 11 |
(foo x) deactivate further arguments and scopes |
| 12 |
(@foo x) deactivate further arguments and scopes |
| 13 |
|
| 14 |
Notations binding to foo: |
| 15 |
|
| 16 |
# := foo do not deactivate further arguments and scopes |
| 17 |
# := @foo deactivate further arguments and scopes |
| 18 |
# x := foo x do not deactivate further arguments and scopes |
| 19 |
# x := @foo x do not deactivate further arguments and scopes |
| 20 |
|
| 21 |
Abbreviations binding to foo: |
| 22 |
|
| 23 |
f := foo do not deactivate further arguments and scopes |
| 24 |
f := @foo deactivate further arguments and scopes |
| 25 |
f x := foo x do not deactivate further arguments and scopes |
| 26 |
f x := @foo x do not deactivate further arguments and scopes |
| 27 |
*) |
| 28 |
|
| 29 |
(* One checks that abbreviations and notations in patterns now behave like in terms *) |
| 30 |
|
| 31 |
Inductive prod' A : Type -> Type := |
| 32 |
| pair' (a:A) B (b:B) (c:bool) : prod' A B. |
| 33 |
Arguments pair' [A] a%_bool_scope [B] b%_bool_scope c%_bool_scope. |
| 34 |
Notation "0" := true : bool_scope. |
| 35 |
|
| 36 |
(* 1. Abbreviations do not stop implicit arguments to be inserted and scopes to be used *) |
| 37 |
Notation c1 x := (pair' x). |
| 38 |
Check pair' 0 0 0 : prod' bool bool. |
| 39 |
Check (pair' 0) _ 0%bool 0%bool : prod' bool bool. (* parentheses are blocking implicit and scopes *) |
| 40 |
Check c1 0 0 0 : prod' bool bool. |
| 41 |
Check fun x : prod' bool bool => match x with c1 0 y 0 => 2 | _ => 1 end. |
| 42 |
|
| 43 |
(* 2. Abbreviations do not stop implicit arguments to be inserted and scopes to be used *) |
| 44 |
Notation c2 x := (@pair' _ x). |
| 45 |
Check (@pair' _ 0) _ 0%bool 0%bool : prod' bool bool. (* parentheses are blocking implicit and scopes *) |
| 46 |
Check c2 0 0 0 : prod' bool bool. |
| 47 |
Check fun A (x : prod' bool A) => match x with c2 0 y 0 => 2 | _ => 1 end. |
| 48 |
Check fun A (x : prod' bool A) => match x with (@pair' _ 0) _ y 0%bool => 2 | _ => 1 end. |
| 49 |
|
| 50 |
(* 3. Abbreviations do not stop implicit arguments to be inserted and scopes to be used *) |
| 51 |
Notation c3 x := ((@pair') _ x). |
| 52 |
Check (@pair') _ 0%bool _ 0%bool 0%bool : prod' bool bool. (* @ is blocking implicit and scopes *) |
| 53 |
Check ((@pair') _ 0%bool) _ 0%bool 0%bool : prod' bool bool. (* parentheses and @ are blocking implicit and scopes *) |
| 54 |
Check c3 0 0 0 : prod' bool bool. |
| 55 |
Check fun A (x :prod' bool A) => match x with c3 0 y 0 => 2 | _ => 1 end. |
| 56 |
|
| 57 |
(* 4. Abbreviations do not stop implicit arguments to be inserted and scopes to be used *) |
| 58 |
(* unless an atomic @ is given *) |
| 59 |
Notation c4 := (@pair'). |
| 60 |
Check (@pair') _ 0%bool _ 0%bool 0%bool : prod' bool bool. |
| 61 |
Check c4 _ 0%bool _ 0%bool 0%bool : prod' bool bool. |
| 62 |
Check fun A (x :prod' bool A) => match x with c4 _ 0%bool _ y 0%bool => 2 | _ => 1 end. |
| 63 |
Check fun A (x :prod' bool A) => match x with (@pair') _ 0%bool _ y 0%bool => 2 | _ => 1 end. |
| 64 |
|
| 65 |
(* 5. Non-@id notations inherit implicit arguments to be inserted and scopes to be used *) |
| 66 |
Notation "# x" := (pair' x) (at level 0, x at level 1). |
| 67 |
Check pair' 0 0 0 : prod' bool bool. |
| 68 |
Check # 0 0 0 : prod' bool bool. |
| 69 |
Check fun A (x :prod' bool A) => match x with # 0 y 0 => 2 | _ => 1 end. |
| 70 |
|
| 71 |
(* 6. Non-@id notations inherit implicit arguments to be inserted and scopes to be used *) |
| 72 |
Notation "## x" := ((@pair') _ x) (at level 0, x at level 1). |
| 73 |
Check (@pair') _ 0%bool _ 0%bool 0%bool : prod' bool bool. |
| 74 |
Check ((@pair') _ 0%bool) _ 0%bool 0%bool : prod' bool bool. |
| 75 |
Check ## 0%bool 0 0 : prod' bool bool. |
| 76 |
Check fun A (x :prod' bool A) => match x with ## 0%bool y 0 => 2 | _ => 1 end. |
| 77 |
|
| 78 |
(* 7. Notations stop further implicit arguments to be inserted and scopes to be used *) |
| 79 |
Notation "###" := (@pair') (at level 0). |
| 80 |
Check (@pair') _ 0%bool _ 0%bool 0%bool : prod' bool bool. |
| 81 |
Check ### _ 0%bool _ 0%bool 0%bool : prod' bool bool. |
| 82 |
Check fun A (x :prod' bool A) => match x with ### _ 0%bool _ y 0%bool => 2 | _ => 1 end. |
| 83 |
|
| 84 |
(* 8. Notations w/o @ preserves implicit arguments and scopes *) |
| 85 |
Notation "####" := pair' (at level 0). |
| 86 |
Check #### 0 0 0 : prod' bool bool. |
| 87 |
Check fun A (x :prod' bool A) => match x with #### 0 y 0 => 2 | _ => 1 end. |
| 88 |
|
| 89 |
(* 9. Non-@id notations inherit implicit arguments and scopes *) |
| 90 |
Notation "##### x" := (pair' x) (at level 0, x at level 1). |
| 91 |
Check ##### 0 0 0 : prod' bool bool. |
| 92 |
Check fun A (x :prod' bool A) => match x with ##### 0 y 0 => 2 | _ => 1 end. |
| 93 |
|
| 94 |
(* 10. Check computation of binding variable through other notations *) |
| 95 |
(* it should be detected as binding variable and the scopes not being checked *) |
| 96 |
Notation "'FUNNAT' i => t" := (fun i : nat => i = t) (at level 200). |
| 97 |
Notation "'Funnat' i => t" := (FUNNAT i => t + i%nat) (at level 200). |
| 98 |
|
| 99 |
(* 11. Notations with needed factorization of a recursive pattern *) |
| 100 |
(* See https://github.com/rocq-prover/rocq/issues/6078#issuecomment-342287412 *) |
| 101 |
Module M11. |
| 102 |
Notation "[:: x1 ; .. ; xn & s ]" := (cons x1 .. (cons xn s) ..). |
| 103 |
Notation "[:: x1 ; .. ; xn ]" := (cons x1 .. (cons xn nil) ..). |
| 104 |
Check [:: 1 ; 2 ; 3 ]. |
| 105 |
Check [:: 1 ; 2 ; 3 & nil ]. (* was failing *) |
| 106 |
End M11. |
| 107 |
|
| 108 |
(* 12. Preventively check that a variable which does not occur can be instantiated *) |
| 109 |
(* by any term. In particular, it should not be restricted to a binder *) |
| 110 |
Module M12. |
| 111 |
Notation "N ++ x" := (S x) (only parsing). |
| 112 |
Check 2 ++ 0. |
| 113 |
End M12. |
| 114 |
|
| 115 |
(* 13. Check that internal data about associativity are not used in comparing levels *) |
| 116 |
Module M13. |
| 117 |
Notation "x ;; z" := (x + z) |
| 118 |
(at level 100, z at level 200, only parsing, right associativity). |
| 119 |
Notation "x ;; z" := (x * z) |
| 120 |
(at level 100, z at level 200, only parsing) : foo_scope. |
| 121 |
End M13. |
| 122 |
|
| 123 |
(* 14. Check that a notation with a "ident" binder does not include a pattern *) |
| 124 |
Module M14. |
| 125 |
Notation "'myexists' x , p" := (ex (fun x => p)) |
| 126 |
(at level 200, x ident, p at level 200, right associativity) : type_scope. |
| 127 |
Check myexists I, I = 0. (* Should not be seen as a constructor *) |
| 128 |
End M14. |
| 129 |
|
| 130 |
(* 15. Testing different ways to give the same levels without failing *) |
| 131 |
|
| 132 |
Module M15. |
| 133 |
Local Notation "###### x" := (S x) (right associativity, at level 79, x at next level). |
| 134 |
Fail Local Notation "###### x" := (S x) (right associativity, at level 79, x at level 79). |
| 135 |
Local Notation "###### x" := (S x) (at level 79). |
| 136 |
End M15. |
| 137 |
|
| 138 |
(* 16. Some test about custom entries *) |
| 139 |
Module M16. |
| 140 |
(* Test locality *) |
| 141 |
Local Declare Custom Entry foo. |
| 142 |
Fail Notation "#" := 0 (in custom foo). (* Should be local *) |
| 143 |
Local Notation "#" := 0 (in custom foo). |
| 144 |
|
| 145 |
(* Test import *) |
| 146 |
Module A. |
| 147 |
Declare Custom Entry foo2. |
| 148 |
End A. |
| 149 |
Notation "##" := 0 (in custom foo2). |
| 150 |
Import A. |
| 151 |
Local Notation "####" := 0 (in custom foo2). |
| 152 |
|
| 153 |
(* Test Print Grammar *) |
| 154 |
Print Custom Grammar foo. |
| 155 |
Print Custom Grammar foo2. |
| 156 |
End M16. |
| 157 |
Fail Local Notation "###" := 0 (in custom foo). |
| 158 |
Fail Print Custom Grammar foo. |
| 159 |
Notation "####" := 0 (in custom foo2). |
| 160 |
|
| 161 |
(* Example showing the need for strong evaluation of |
| 162 |
cases_pattern_of_glob_constr (this used to raise Not_found at some |
| 163 |
time) *) |
| 164 |
|
| 165 |
Module M17. |
| 166 |
|
| 167 |
Notation "# x ## t & u" := ((fun x => (x,t)),(fun x => (x,u))) (at level 1, x pattern). |
| 168 |
Check fun y : nat => # (x,z) ## y & y. |
| 169 |
|
| 170 |
End M17. |
| 171 |
|
| 172 |
Module Bug10750. |
| 173 |
|
| 174 |
Notation "#" := 0 (only printing). |
| 175 |
Print Visibility. |
| 176 |
|
| 177 |
End Bug10750. |
| 178 |
|
| 179 |
Module M18. |
| 180 |
|
| 181 |
Module A. |
| 182 |
Module B. |
| 183 |
Infix "+++" := Nat.add (at level 70). |
| 184 |
End B. |
| 185 |
End A. |
| 186 |
Import A. |
| 187 |
(* Check that the notation in module B is not visible *) |
| 188 |
Infix "+++" := Nat.add (at level 80). |
| 189 |
|
| 190 |
End M18. |
| 191 |
|
| 192 |
Module InheritanceArgumentScopes. |
| 193 |
|
| 194 |
Axiom p : forall (A:Type) (b:nat), A = A /\ b = b. |
| 195 |
Check fun A n => p (A * A) (n * n). (* safety check *) |
| 196 |
Notation q := @p. |
| 197 |
Check fun A n => q (A * A) (n * n). (* check that argument scopes are propagated *) |
| 198 |
|
| 199 |
End InheritanceArgumentScopes. |
| 200 |
|
| 201 |
Module InheritanceMaximalImplicitPureNotation. |
| 202 |
|
| 203 |
Definition id {A B:Type} (a:B) := a. |
| 204 |
Notation "#" := (@id nat). |
| 205 |
Check # = (fun a:nat => a). (* # should inherit its maximal implicit argument *) |
| 206 |
|
| 207 |
End InheritanceMaximalImplicitPureNotation. |
| 208 |
|
| 209 |
Module TreeLikeLookAhead. |
| 210 |
|
| 211 |
Notation "6 ^" := true (at level 0, format "6 ^"). |
| 212 |
Notation "6 ?" := false (at level 0, format "6 ?"). |
| 213 |
Check 6. |
| 214 |
|
| 215 |
End TreeLikeLookAhead. |
| 216 |
|
| 217 |
Module FactorizationListSeparators. |
| 218 |
|
| 219 |
Notation "[ a + + .. + + c | d ]" := (cons a .. (cons c d) ..) (a at level 10). |
| 220 |
Notation "[ a + + .. + + c ]" := (cons a .. (cons c nil) ..) (a at level 10). |
| 221 |
Check [0 + + 1 | nil]. |
| 222 |
Check [0 + + 1]. |
| 223 |
|
| 224 |
End FactorizationListSeparators. |
| 225 |
|
| 226 |
Module TestNonExistentCustomOnlyPrinting. |
| 227 |
|
| 228 |
Fail Notation "[ x ]" := (id x) (x custom doesntexist, only printing). |
| 229 |
Fail Notation "# x" := (id x) (in custom doesntexist, only printing). |
| 230 |
|
| 231 |
End TestNonExistentCustomOnlyPrinting. |
| 232 |
|
| 233 |
Module NotationClauseIn. |
| 234 |
|
| 235 |
Notation "1" := unit. |
| 236 |
Check fun x => match x in 1 with tt => 0 end. |
| 237 |
|
| 238 |
End NotationClauseIn. |