| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
open CErrors |
| 14 |
open Util |
| 15 |
open Pp |
| 16 |
open Names |
| 17 |
open Nameops |
| 18 |
open Notation_term |
| 19 |
open Notation_ops |
| 20 |
|
| 21 |
type key = |
| 22 |
| RefKey of GlobRef.t |
| 23 |
| Oth |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
let canonize_key env k = match k with |
| 28 |
| Oth -> Oth |
| 29 |
| RefKey gr -> |
| 30 |
let gr' = Environ.QGlobRef.canonize env gr in |
| 31 |
if gr' == gr then k else RefKey gr' |
| 32 |
|
| 33 |
let mkRefKey env gr = |
| 34 |
RefKey (Environ.QGlobRef.canonize env gr) |
| 35 |
|
| 36 |
let key_compare k1 k2 = match k1, k2 with |
| 37 |
| RefKey gr1, RefKey gr2 -> GlobRef.UserOrd.compare gr1 gr2 |
| 38 |
| RefKey _, Oth -> -1 |
| 39 |
| Oth, RefKey _ -> 1 |
| 40 |
| Oth, Oth -> 0 |
| 41 |
|
| 42 |
module KeyOrd = struct type t = key let compare = key_compare end |
| 43 |
module KeyMap = Map.Make(KeyOrd) |
| 44 |
|
| 45 |
module ReservedSet : |
| 46 |
sig |
| 47 |
type t |
| 48 |
val empty : t |
| 49 |
val add : (Id.t * notation_constr) -> t -> t |
| 50 |
val find : (Id.t -> notation_constr -> bool) -> t -> Id.t * notation_constr |
| 51 |
end = |
| 52 |
struct |
| 53 |
type t = (Id.t * notation_constr) list |
| 54 |
|
| 55 |
let empty = [] |
| 56 |
|
| 57 |
let rec mem id c = function |
| 58 |
| [] -> false |
| 59 |
| (id', c') :: l -> |
| 60 |
if c == c' && Id.equal id id' then true else mem id c l |
| 61 |
|
| 62 |
let add p l = |
| 63 |
let (id, c) = p in |
| 64 |
if mem id c l then l else p :: l |
| 65 |
|
| 66 |
let rec find f = function |
| 67 |
| [] -> raise Not_found |
| 68 |
| (id, c) as p :: l -> if f id c then p else find f l |
| 69 |
end |
| 70 |
|
| 71 |
|
| 72 |
let keymap_add env key data map = |
| 73 |
let key = canonize_key env key in |
| 74 |
let old = try KeyMap.find key map with Not_found -> ReservedSet.empty in |
| 75 |
KeyMap.add key (ReservedSet.add data old) map |
| 76 |
|
| 77 |
let reserve_table = Summary.ref Id.Map.empty ~name:"reserved-type" |
| 78 |
let reserve_revtable = Summary.ref KeyMap.empty ~name:"reserved-type-rev" |
| 79 |
|
| 80 |
let notation_constr_key env = function |
| 81 |
| NApp (NRef (ref,_),args) -> mkRefKey env ref, Some (List.length args) |
| 82 |
| NList (_,_,NApp (NRef (ref,_),args),_,_) |
| 83 |
| NBinderList (_,_,NApp (NRef (ref,_),args),_,_) -> mkRefKey env ref, Some (List.length args) |
| 84 |
| NRef (ref,_) -> mkRefKey env ref, None |
| 85 |
| _ -> Oth, None |
| 86 |
|
| 87 |
let add_reserved_type env (id,t) = |
| 88 |
let key = fst (notation_constr_key env t) in |
| 89 |
reserve_table := Id.Map.add id t !reserve_table; |
| 90 |
reserve_revtable := keymap_add env key (id, t) !reserve_revtable |
| 91 |
|
| 92 |
let declare_reserved_type_binding env {CAst.loc;v=id} t = |
| 93 |
if not (Id.equal id (root_of_id id)) then |
| 94 |
user_err ?loc |
| 95 |
((Id.print id ++ str |
| 96 |
" is not reservable: it must have no trailing digits, quote, or _.")); |
| 97 |
begin try |
| 98 |
let _ = Id.Map.find id !reserve_table in |
| 99 |
user_err ?loc |
| 100 |
((Id.print id ++ str " is already bound to a type.")) |
| 101 |
with Not_found -> () end; |
| 102 |
add_reserved_type env (id,t) |
| 103 |
|
| 104 |
let declare_reserved_type idl t = |
| 105 |
let env = Global.env () in |
| 106 |
List.iter (fun id -> declare_reserved_type_binding env id t) (List.rev idl) |
| 107 |
|
| 108 |
let find_reserved_type id = Id.Map.find (root_of_id id) !reserve_table |
| 109 |
|
| 110 |
let constr_key env evd c = |
| 111 |
try mkRefKey env (fst @@ EConstr.destRef evd (fst (EConstr.decompose_app evd c))) |
| 112 |
with Constr.DestKO -> Oth |
| 113 |
|
| 114 |
let revert_reserved_type env evd t = |
| 115 |
try |
| 116 |
let reserved = KeyMap.find (constr_key env evd t) !reserve_revtable in |
| 117 |
let t = Detyping.detype Detyping.Now ~flags:(PrintingFlags.Detype.current()) env evd t in |
| 118 |
let factorize_eqns = PrintingFlags.Extern.FactorizeEqns.current() in |
| 119 |
|
| 120 |
|
| 121 |
let filter _ pat = |
| 122 |
try |
| 123 |
let _ : _ * _ * _ * _ = |
| 124 |
match_notation_constr ~print_parentheses:true ~factorize_eqns t ~vars:Id.Set.empty ([], pat) |
| 125 |
in |
| 126 |
true |
| 127 |
with No_match -> false |
| 128 |
in |
| 129 |
let (id, _) = ReservedSet.find filter reserved in |
| 130 |
Name id |
| 131 |
with Not_found | Failure _ -> Anonymous |
| 132 |
|
| 133 |
let () = Namegen.set_reserved_typed_name revert_reserved_type |