| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
open Util |
| 16 |
open Names |
| 17 |
open Globnames |
| 18 |
open Constrexpr |
| 19 |
open Notation_term |
| 20 |
open Glob_term |
| 21 |
|
| 22 |
|
| 23 |
let notation_with_optional_scope_eq inscope1 inscope2 = match (inscope1,inscope2) with |
| 24 |
| LastLonelyNotation, LastLonelyNotation -> true |
| 25 |
| NotationInScope s1, NotationInScope s2 -> String.equal s1 s2 |
| 26 |
| (LastLonelyNotation | NotationInScope _), _ -> false |
| 27 |
|
| 28 |
let notation_with_optional_scope_compare inscope1 inscope2 = match inscope1, inscope2 with |
| 29 |
| LastLonelyNotation, LastLonelyNotation -> 0 |
| 30 |
| LastLonelyNotation, _ -> -1 |
| 31 |
| _, LastLonelyNotation -> 1 |
| 32 |
| NotationInScope s1, NotationInScope s2 -> String.compare s1 s2 |
| 33 |
|
| 34 |
let entry_relative_level_eq t1 t2 = match t1, t2 with |
| 35 |
| LevelLt n1, LevelLt n2 -> Int.equal n1 n2 |
| 36 |
| LevelLe n1, LevelLe n2 -> Int.equal n1 n2 |
| 37 |
| LevelSome, LevelSome -> true |
| 38 |
| (LevelLt _ | LevelLe _ | LevelSome), _ -> false |
| 39 |
|
| 40 |
let notation_entry_eq s1 s2 = match (s1,s2) with |
| 41 |
| InConstrEntry, InConstrEntry -> true |
| 42 |
| InCustomEntry s1, InCustomEntry s2 -> CustomName.equal s1 s2 |
| 43 |
| (InConstrEntry | InCustomEntry _), _ -> false |
| 44 |
|
| 45 |
let notation_entry_compare s1 s2 = match s1, s2 with |
| 46 |
| InConstrEntry, InConstrEntry -> 0 |
| 47 |
| InConstrEntry, _ -> -1 |
| 48 |
| _, InConstrEntry -> 1 |
| 49 |
| InCustomEntry s1, InCustomEntry s2 -> CustomName.compare s1 s2 |
| 50 |
|
| 51 |
let notation_entry_level_eq |
| 52 |
{ notation_entry = e1; notation_level = n1 } |
| 53 |
{ notation_entry = e2; notation_level = n2 } = |
| 54 |
notation_entry_eq e1 e2 && Int.equal n1 n2 |
| 55 |
|
| 56 |
let notation_entry_relative_level_eq |
| 57 |
{ notation_subentry = e1; notation_relative_level = n1; notation_position = s1 } |
| 58 |
{ notation_subentry = e2; notation_relative_level = n2; notation_position = s2 } = |
| 59 |
notation_entry_eq e1 e2 && entry_relative_level_eq n1 n2 && s1 = s2 |
| 60 |
|
| 61 |
let notation_eq (from1,ntn1) (from2,ntn2) = |
| 62 |
notation_entry_eq from1 from2 && String.equal ntn1 ntn2 |
| 63 |
|
| 64 |
let notation_compare (from1,ntn1) (from2,ntn2) = |
| 65 |
let c = notation_entry_compare from1 from2 in |
| 66 |
if c <> 0 then c else String.compare ntn1 ntn2 |
| 67 |
|
| 68 |
let specific_notation_compare (scope1,ntn1) (scope2,ntn2) = |
| 69 |
let c = notation_with_optional_scope_compare scope1 scope2 in |
| 70 |
if c <> 0 then c |
| 71 |
else notation_compare ntn1 ntn2 |
| 72 |
|
| 73 |
let pair_eq f g (x1, y1) (x2, y2) = f x1 x2 && g y1 y2 |
| 74 |
|
| 75 |
let notation_binder_kind_eq k1 k2 = match k1, k2 with |
| 76 |
| AsIdent, AsIdent -> true |
| 77 |
| AsName, AsName -> true |
| 78 |
| AsAnyPattern, AsAnyPattern -> true |
| 79 |
| AsStrictPattern, AsStrictPattern -> true |
| 80 |
| (AsIdent | AsName | AsAnyPattern | AsStrictPattern), _ -> false |
| 81 |
|
| 82 |
let notation_binder_source_eq s1 s2 = match s1, s2 with |
| 83 |
| NtnBinderParsedAsSomeBinderKind bk1, NtnBinderParsedAsSomeBinderKind bk2 -> notation_binder_kind_eq bk1 bk2 |
| 84 |
| NtnBinderParsedAsBinder, NtnBinderParsedAsBinder -> true |
| 85 |
| NtnBinderParsedAsConstr bk1, NtnBinderParsedAsConstr bk2 -> notation_binder_kind_eq bk1 bk2 |
| 86 |
| (NtnBinderParsedAsSomeBinderKind _ | NtnBinderParsedAsBinder | NtnBinderParsedAsConstr _), _ -> false |
| 87 |
|
| 88 |
let ntpe_eq t1 t2 = match t1, t2 with |
| 89 |
| NtnTypeConstr, NtnTypeConstr -> true |
| 90 |
| NtnTypeBinder s1, NtnTypeBinder s2 -> notation_binder_source_eq s1 s2 |
| 91 |
| NtnTypeConstrList, NtnTypeConstrList -> true |
| 92 |
| NtnTypeBinderList s1, NtnTypeBinderList s2 -> notation_binder_source_eq s1 s2 |
| 93 |
| (NtnTypeConstr | NtnTypeBinder _ | NtnTypeConstrList | NtnTypeBinderList _), _ -> false |
| 94 |
|
| 95 |
let var_attributes_eq (_, ((entry1, sc1), binders1, tp1)) (_, ((entry2, sc2), binders2, tp2)) = |
| 96 |
notation_entry_relative_level_eq entry1 entry2 && |
| 97 |
pair_eq (List.equal String.equal) (List.equal String.equal) sc1 sc2 && |
| 98 |
Id.Set.equal binders1 binders2 && |
| 99 |
ntpe_eq tp1 tp2 |
| 100 |
|
| 101 |
let interpretation_eq (vars1, t1 as x1) (vars2, t2 as x2) = |
| 102 |
x1 == x2 || |
| 103 |
List.equal var_attributes_eq vars1 vars2 && |
| 104 |
Notation_ops.eq_notation_constr (List.map fst vars1, List.map fst vars2) t1 t2 |
| 105 |
|
| 106 |
type level = notation_entry_level * entry_relative_level list |
| 107 |
|
| 108 |
let level_eq ({ notation_entry = s1; notation_level = l1}, t1) ({ notation_entry = s2; notation_level = l2}, t2) = |
| 109 |
notation_entry_eq s1 s2 && Int.equal l1 l2 && List.equal entry_relative_level_eq t1 t2 |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
type interp_rule = |
| 114 |
| NotationRule of Constrexpr.specific_notation |
| 115 |
| AbbrevRule of Globnames.abbreviation |
| 116 |
|
| 117 |
let specific_notation_eq (sc1, (e1, s1)) (sc2, (e2, s2)) = |
| 118 |
notation_with_optional_scope_eq sc1 sc2 && |
| 119 |
notation_entry_eq e1 e2 && |
| 120 |
String.equal s1 s2 |
| 121 |
|
| 122 |
let interp_rule_eq r1 r2 = match r1, r2 with |
| 123 |
| NotationRule n1, NotationRule n2 -> specific_notation_eq n1 n2 |
| 124 |
| AbbrevRule kn1, AbbrevRule kn2 -> KerName.equal kn1 kn2 |
| 125 |
| (AbbrevRule _ | NotationRule _), _ -> false |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
type key = |
| 131 |
| RefKey of GlobRef.t |
| 132 |
| Oth |
| 133 |
|
| 134 |
module KeyOrd = struct |
| 135 |
type t = key |
| 136 |
let compare k1 k2 = match k1, k2 with |
| 137 |
| RefKey gr1, RefKey gr2 -> GlobRef.UserOrd.compare gr1 gr2 |
| 138 |
| RefKey _, Oth -> -1 |
| 139 |
| Oth, RefKey _ -> 1 |
| 140 |
| Oth, Oth -> 0 |
| 141 |
let canonize env k = match k with |
| 142 |
| Oth -> Oth |
| 143 |
| RefKey gr -> RefKey (Environ.QGlobRef.canonize env gr) |
| 144 |
end |
| 145 |
|
| 146 |
module KeyMap = Environ.QMap(Map.Make(KeyOrd))(KeyOrd) |
| 147 |
|
| 148 |
type notation_applicative_status = |
| 149 |
| AppBoundedNotation of int |
| 150 |
| AppUnboundedNotation |
| 151 |
| NotAppNotation |
| 152 |
|
| 153 |
let notation_applicative_status_eq s1 s2 = match s1, s2 with |
| 154 |
| AppBoundedNotation n1, AppBoundedNotation n2 -> Int.equal n1 n2 |
| 155 |
| AppUnboundedNotation, AppUnboundedNotation -> true |
| 156 |
| NotAppNotation, NotAppNotation -> true |
| 157 |
| (AppBoundedNotation _ | AppUnboundedNotation | NotAppNotation), _ -> false |
| 158 |
|
| 159 |
type notation_rule = { |
| 160 |
not_rule : interp_rule; |
| 161 |
not_patt : interpretation; |
| 162 |
not_status : notation_applicative_status; |
| 163 |
} |
| 164 |
|
| 165 |
let notation_rule_eq x1 x2 = |
| 166 |
x1 == x2 || |
| 167 |
(interp_rule_eq x1.not_rule x2.not_rule && |
| 168 |
interpretation_eq x1.not_patt x2.not_patt && |
| 169 |
notation_applicative_status_eq x1.not_status x2.not_status) |
| 170 |
|
| 171 |
module NotationSet : |
| 172 |
sig |
| 173 |
|
| 174 |
type t |
| 175 |
val empty : t |
| 176 |
val add : notation_rule -> t -> t |
| 177 |
val remove : notation_rule -> t -> t |
| 178 |
val repr : t -> notation_rule list |
| 179 |
|
| 180 |
end = |
| 181 |
struct |
| 182 |
|
| 183 |
type diff = Add | Sub |
| 184 |
|
| 185 |
type data = { |
| 186 |
ntn_todo : (diff * notation_rule) list; |
| 187 |
ntn_done : notation_rule list; |
| 188 |
} |
| 189 |
|
| 190 |
type t = data ref option |
| 191 |
|
| 192 |
let empty = None |
| 193 |
|
| 194 |
let push k r s = match s with |
| 195 |
| None -> Some (ref { ntn_done = []; ntn_todo = [k, r] }) |
| 196 |
| Some { contents = s } -> |
| 197 |
Some (ref { ntn_done = s.ntn_done; ntn_todo = (k, r) :: s.ntn_todo }) |
| 198 |
|
| 199 |
let add r s = push Add r s |
| 200 |
|
| 201 |
let remove r s = push Sub r s |
| 202 |
|
| 203 |
let force s = |
| 204 |
if List.is_empty s.ntn_todo then None |
| 205 |
else |
| 206 |
let cmp r1 r2 = Notation_ops.strictly_finer_interpretation_than r1.not_patt r2.not_patt in |
| 207 |
|
| 208 |
let fold accu (knd, ntn) = match knd with |
| 209 |
| Add -> |
| 210 |
let finer, rest = List.partition (fun c -> cmp c ntn) accu in |
| 211 |
(finer @ ntn :: rest) |
| 212 |
| Sub -> |
| 213 |
List.remove_first (fun rule -> notation_rule_eq ntn rule) accu |
| 214 |
in |
| 215 |
Some (List.fold_left fold s.ntn_done (List.rev s.ntn_todo)) |
| 216 |
|
| 217 |
let repr s = match s with |
| 218 |
| None -> [] |
| 219 |
| Some r -> |
| 220 |
match force !r with |
| 221 |
| None -> r.contents.ntn_done |
| 222 |
| Some ans -> |
| 223 |
let () = r := { ntn_done = ans; ntn_todo = [] } in |
| 224 |
ans |
| 225 |
|
| 226 |
end |
| 227 |
|
| 228 |
let keymap_add env key interp map = |
| 229 |
let old = try KeyMap.find env key map with Not_found -> NotationSet.empty in |
| 230 |
KeyMap.add env key (NotationSet.add interp old) map |
| 231 |
|
| 232 |
let keymap_remove env key interp map = |
| 233 |
let old = try KeyMap.find env key map with Not_found -> NotationSet.empty in |
| 234 |
KeyMap.add env key (NotationSet.remove interp old) map |
| 235 |
|
| 236 |
let keymap_find env key map = |
| 237 |
try NotationSet.repr (KeyMap.find env key map) |
| 238 |
with Not_found -> [] |
| 239 |
|
| 240 |
|
| 241 |
let notations_key_table = Summary.ref |
| 242 |
~stage:Summary.Stage.Interp |
| 243 |
~name:"notation_uninterpretation" |
| 244 |
(KeyMap.empty : NotationSet.t KeyMap.t) |
| 245 |
|
| 246 |
let glob_constr_keys c = match DAst.get c with |
| 247 |
| GApp (c, _) -> |
| 248 |
begin match DAst.get c with |
| 249 |
| GRef (ref, _) -> [RefKey (canonical_gr ref); Oth] |
| 250 |
| _ -> [Oth] |
| 251 |
end |
| 252 |
| GProj ((cst,_), _, _) -> [RefKey (canonical_gr (GlobRef.ConstRef cst))] |
| 253 |
| GRef (ref,_) -> [RefKey (canonical_gr ref)] |
| 254 |
| _ -> [Oth] |
| 255 |
|
| 256 |
let cases_pattern_key c = match DAst.get c with |
| 257 |
| PatCstr (ref,_,_) -> RefKey (canonical_gr (GlobRef.ConstructRef ref)) |
| 258 |
| _ -> Oth |
| 259 |
|
| 260 |
let notation_constr_key = function |
| 261 |
| NApp (NRef (ref,_),args) -> RefKey(canonical_gr ref), AppBoundedNotation (List.length args) |
| 262 |
| NProj ((cst,_),args,_) -> RefKey(canonical_gr (GlobRef.ConstRef cst)), AppBoundedNotation (List.length args + 1) |
| 263 |
| NList (_,_,NApp (NRef (ref,_),args),_,_) |
| 264 |
| NBinderList (_,_,NApp (NRef (ref,_),args),_,_) -> |
| 265 |
RefKey (canonical_gr ref), AppBoundedNotation (List.length args) |
| 266 |
| NRef (ref,_) -> RefKey(canonical_gr ref), NotAppNotation |
| 267 |
| NApp (NList (_,_,NApp (NRef (ref,_),args),_,_), args') -> |
| 268 |
RefKey (canonical_gr ref), AppBoundedNotation (List.length args + List.length args') |
| 269 |
| NApp (NList (_,_,NApp (_,args),_,_), args') -> |
| 270 |
Oth, AppBoundedNotation (List.length args + List.length args') |
| 271 |
| NApp (NVar _,_) -> Oth, AppUnboundedNotation |
| 272 |
| NApp (_,args) -> Oth, AppBoundedNotation (List.length args) |
| 273 |
| NList (_,_,NApp (NVar x,_),_,_) when x = Notation_ops.ldots_var -> Oth, AppUnboundedNotation |
| 274 |
| _ -> Oth, NotAppNotation |
| 275 |
|
| 276 |
let uninterp_notations env c = |
| 277 |
List.map_append (fun key -> keymap_find env key !notations_key_table) |
| 278 |
(glob_constr_keys c) |
| 279 |
|
| 280 |
let uninterp_cases_pattern_notations env c = |
| 281 |
keymap_find env (cases_pattern_key c) !notations_key_table |
| 282 |
|
| 283 |
let uninterp_ind_pattern_notations env ind = |
| 284 |
keymap_find env (RefKey (canonical_gr (GlobRef.IndRef ind))) !notations_key_table |
| 285 |
|
| 286 |
let remove_uninterpretation env rule (metas,c as pat) = |
| 287 |
let (key,n) = notation_constr_key c in |
| 288 |
notations_key_table := keymap_remove env key { not_rule = rule; not_patt = pat; not_status = n } !notations_key_table |
| 289 |
|
| 290 |
let declare_uninterpretation env rule (metas,c as pat) = |
| 291 |
let (key,n) = notation_constr_key c in |
| 292 |
notations_key_table := keymap_add env key { not_rule = rule; not_patt = pat; not_status = n } !notations_key_table |
| 293 |
|
| 294 |
let freeze () = |
| 295 |
!notations_key_table |
| 296 |
|
| 297 |
let unfreeze fkm = |
| 298 |
notations_key_table := fkm |
| 299 |
|
| 300 |
let with_notation_uninterpretation_protection f x = |
| 301 |
let open Memprof_coq.Resource_bind in |
| 302 |
let& () = Util.protect_state ~freeze ~unfreeze in |
| 303 |
f x |
| 304 |
|
| 305 |
|
| 306 |
type notation_use = |
| 307 |
| OnlyPrinting |
| 308 |
| OnlyParsing |
| 309 |
| ParsingAndPrinting |