| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
open Names |
| 13 |
open Context |
| 14 |
open CErrors |
| 15 |
open Util |
| 16 |
open Glob_term |
| 17 |
open Constrexpr |
| 18 |
open Libnames |
| 19 |
open Typeclasses |
| 20 |
open Pp |
| 21 |
open Libobject |
| 22 |
open Nameops |
| 23 |
open Context.Rel.Declaration |
| 24 |
|
| 25 |
module RelDecl = Context.Rel.Declaration |
| 26 |
|
| 27 |
|
| 28 |
let generalizable_table = Summary.ref Id.Pred.empty ~name:"generalizable-ident" |
| 29 |
|
| 30 |
let declare_generalizable_ident table {CAst.loc;v=id} = |
| 31 |
if not (Id.equal id (root_of_id id)) then |
| 32 |
user_err ?loc |
| 33 |
((Id.print id ++ str |
| 34 |
" is not declarable as generalizable identifier: it must have no trailing digits, quote, or _.")); |
| 35 |
if Id.Pred.mem id table then |
| 36 |
user_err ?loc |
| 37 |
((Id.print id++str" is already declared as a generalizable identifier.")) |
| 38 |
else Id.Pred.add id table |
| 39 |
|
| 40 |
let add_generalizable gen table = |
| 41 |
match gen with |
| 42 |
| None -> Id.Pred.empty |
| 43 |
| Some [] -> Id.Pred.full |
| 44 |
| Some l -> List.fold_left (fun table lid -> declare_generalizable_ident table lid) |
| 45 |
table l |
| 46 |
|
| 47 |
let cache_generalizable_type (local,cmd) = |
| 48 |
generalizable_table := add_generalizable cmd !generalizable_table |
| 49 |
|
| 50 |
let load_generalizable_type _ (local,cmd) = |
| 51 |
generalizable_table := add_generalizable cmd !generalizable_table |
| 52 |
|
| 53 |
let in_generalizable : bool * lident list option -> obj = |
| 54 |
declare_object {(default_object "GENERALIZED-IDENT") with |
| 55 |
load_function = load_generalizable_type; |
| 56 |
cache_function = cache_generalizable_type; |
| 57 |
classify_function = (fun (local, _) -> if local then Dispose else Keep) |
| 58 |
} |
| 59 |
|
| 60 |
let declare_generalizable ~local gen = |
| 61 |
Lib.add_leaf (in_generalizable (local, gen)) |
| 62 |
|
| 63 |
let find_generalizable_ident id = Id.Pred.mem (root_of_id id) !generalizable_table |
| 64 |
|
| 65 |
let is_global id = |
| 66 |
try ignore (Nametab.locate_extended (qualid_of_ident id)); true |
| 67 |
with Not_found -> false |
| 68 |
|
| 69 |
let is_named id env = |
| 70 |
try ignore (Environ.lookup_named id env); true |
| 71 |
with Not_found -> false |
| 72 |
|
| 73 |
let is_freevar ids env x = |
| 74 |
not (Id.Set.mem x ids || is_named x env || is_global x) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
let ungeneralizable loc id = |
| 80 |
user_err ?loc |
| 81 |
(str "Unbound and ungeneralizable variable " ++ Id.print id ++ str ".") |
| 82 |
|
| 83 |
let free_vars_of_constr_expr c ?(bound=Id.Set.empty) l = |
| 84 |
let found loc id bdvars l = |
| 85 |
if Id.List.mem id l then l |
| 86 |
else if is_freevar bdvars (Global.env ()) id |
| 87 |
then |
| 88 |
if find_generalizable_ident id then id :: l |
| 89 |
else ungeneralizable loc id |
| 90 |
else l |
| 91 |
in |
| 92 |
let rec aux bdvars l c = match CAst.(c.v) with |
| 93 |
| CRef (qid,_) when qualid_is_ident qid -> |
| 94 |
found c.CAst.loc (qualid_basename qid) bdvars l |
| 95 |
| CNotation (_,(InConstrEntry,"{ _ : _ | _ }"), ({ CAst.v = CRef (qid,_) } :: _, [], [], [])) when |
| 96 |
qualid_is_ident qid && not (Id.Set.mem (qualid_basename qid) bdvars) -> |
| 97 |
Constrexpr_ops.fold_constr_expr_with_binders (fun a l -> Id.Set.add a l) aux (Id.Set.add (qualid_basename qid) bdvars) l c |
| 98 |
| _ -> Constrexpr_ops.fold_constr_expr_with_binders (fun a l -> Id.Set.add a l) aux bdvars l c |
| 99 |
in aux bound l c |
| 100 |
|
| 101 |
let generalizable_vars_of_glob_constr ?(bound=Id.Set.empty) ?(allowed=Id.Set.empty) = |
| 102 |
let rec vars bound vs c = match DAst.get c with |
| 103 |
| GVar id -> |
| 104 |
let loc = c.CAst.loc in |
| 105 |
if is_freevar bound (Global.env ()) id then |
| 106 |
if List.exists (fun {CAst.v} -> Id.equal v id) vs then vs |
| 107 |
else CAst.(make ?loc id) :: vs |
| 108 |
else vs |
| 109 |
| _ -> Glob_ops.fold_glob_constr_with_binders Id.Set.add vars bound vs c |
| 110 |
in fun rt -> |
| 111 |
let vars = List.rev (vars bound [] rt) in |
| 112 |
List.iter (fun {CAst.loc;v=id} -> |
| 113 |
if not (Id.Set.mem id allowed || find_generalizable_ident id) then |
| 114 |
ungeneralizable loc id) vars; |
| 115 |
vars |
| 116 |
|
| 117 |
let make_fresh ids env x = |
| 118 |
Namegen.next_ident_away_from x (fun x -> not (is_freevar ids env x)) |
| 119 |
|
| 120 |
let next_name_away_from na avoid = |
| 121 |
match na with |
| 122 |
| Anonymous -> make_fresh avoid (Global.env ()) (Id.of_string "anon") |
| 123 |
| Name id -> make_fresh avoid (Global.env ()) id |
| 124 |
|
| 125 |
let rec is_class_arg env c = |
| 126 |
let open Constr in |
| 127 |
match kind c with |
| 128 |
| Prod (_,_,c) |
| 129 |
| Cast (c,_,_) |
| 130 |
| LetIn (_,_,_,c) -> is_class_arg env c |
| 131 |
| _ -> |
| 132 |
let c, _ = decompose_app c in |
| 133 |
match destRef c with |
| 134 |
| exception DestKO -> false |
| 135 |
| r, _ -> is_class env r |
| 136 |
|
| 137 |
let combine_params env avoid applied needed = |
| 138 |
let named, applied = |
| 139 |
List.partition |
| 140 |
(function |
| 141 |
(t, Some {CAst.loc;v=ExplByName id}) -> |
| 142 |
let is_id decl = Name.equal (Name id) (RelDecl.get_name decl) in |
| 143 |
if not (List.exists is_id needed) then |
| 144 |
user_err ?loc (str "Wrong argument name: " ++ Id.print id); |
| 145 |
true |
| 146 |
| _ -> false) applied |
| 147 |
in |
| 148 |
let named = List.map |
| 149 |
(fun x -> match x with (t, Some {CAst.loc;v=ExplByName id}) -> id, t | _ -> assert false) |
| 150 |
named |
| 151 |
in |
| 152 |
let rec aux ids avoid app need = |
| 153 |
match need with |
| 154 |
| [] -> begin match app with |
| 155 |
| [] -> List.rev ids, avoid |
| 156 |
| (x, _) :: _ -> user_err ?loc:(Constrexpr_ops.constr_loc x) (str "Typeclass does not expect more arguments") |
| 157 |
end |
| 158 |
|
| 159 |
| LocalDef _ :: need -> aux ids avoid app need |
| 160 |
|
| 161 |
|
| 162 |
| LocalAssum ({binder_name=Name id}, _) :: need when Id.List.mem_assoc id named -> |
| 163 |
aux (Id.List.assoc id named :: ids) avoid app need |
| 164 |
|
| 165 |
| decl :: need -> |
| 166 |
begin match app, is_class_arg env (get_type decl) with |
| 167 |
| (x, _) :: app, false -> aux (x :: ids) avoid app need |
| 168 |
|
| 169 |
| [], false | _, true -> |
| 170 |
let id' = next_name_away_from (RelDecl.get_name decl) avoid in |
| 171 |
let t' = CAst.make @@ CRef (qualid_of_ident id',None) in |
| 172 |
aux (t' :: ids) (Id.Set.add id' avoid) app need |
| 173 |
end |
| 174 |
in |
| 175 |
aux [] avoid applied needed |
| 176 |
|
| 177 |
let destClassAppExpl cl = |
| 178 |
let open CAst in |
| 179 |
let loc = cl.loc in |
| 180 |
match cl.v with |
| 181 |
| CApp ({ v = CRef (ref, inst) }, l) -> CAst.make ?loc (ref, l, inst) |
| 182 |
| CRef (ref, inst) -> CAst.make ?loc:cl.loc (ref, [], inst) |
| 183 |
| _ -> raise Not_found |
| 184 |
|
| 185 |
let implicit_application env ty = |
| 186 |
let is_class = |
| 187 |
try |
| 188 |
let ({CAst.v=(qid, _, _)} as clapp) = destClassAppExpl ty in |
| 189 |
if Libnames.idset_mem_qualid qid env then None |
| 190 |
else |
| 191 |
let gr = Nametab.locate qid in |
| 192 |
Option.map (fun cl -> cl, clapp) (Typeclasses.class_info (Global.env ()) gr) |
| 193 |
with Not_found -> None |
| 194 |
in |
| 195 |
match is_class with |
| 196 |
| None -> ty, env |
| 197 |
| Some (c, {CAst.loc;v=(id, par, inst)}) -> |
| 198 |
let avoid = Id.Set.union env (Id.Set.of_list (free_vars_of_constr_expr ty ~bound:env [])) in |
| 199 |
let args, avoid = combine_params (Global.env ()) avoid par (List.rev c.cl_context) in |
| 200 |
CAst.make ?loc @@ CAppExpl ((id, inst), args), avoid |
| 201 |
|
| 202 |
let warn_ignoring_implicit_status = |
| 203 |
CWarnings.create ~name:"ignoring-implicit-status" ~category:CWarnings.CoreCategories.implicits |
| 204 |
(fun na -> |
| 205 |
strbrk "Ignoring implicit status of product binder " ++ |
| 206 |
Name.print na ++ strbrk " and following binders") |
| 207 |
|
| 208 |
let implicits_of_glob_constr ?(with_products=true) l = |
| 209 |
let add_impl ?loc na bk l = match bk with |
| 210 |
| NonMaxImplicit -> CAst.make ?loc (Some (na,false)) :: l |
| 211 |
| MaxImplicit -> CAst.make ?loc (Some (na,true)) :: l |
| 212 |
| Explicit -> CAst.make ?loc None :: l |
| 213 |
in |
| 214 |
let rec aux c = |
| 215 |
match DAst.get c with |
| 216 |
| GProd (na, _, bk, t, b) -> |
| 217 |
if with_products then add_impl na bk (aux b) |
| 218 |
else |
| 219 |
let () = match bk with |
| 220 |
| NonMaxImplicit |
| 221 |
| MaxImplicit -> warn_ignoring_implicit_status na ?loc:c.CAst.loc |
| 222 |
| Explicit -> () |
| 223 |
in [] |
| 224 |
| GLambda (na, _, bk, t, b) -> add_impl ?loc:t.CAst.loc na bk (aux b) |
| 225 |
| GLetIn (na, _, b, t, c) -> aux c |
| 226 |
| GRec (fix_kind, nas, args, tys, bds) -> |
| 227 |
let nb = match fix_kind with |GFix (_, n) -> n | GCoFix n -> n in |
| 228 |
List.fold_right (fun (na,_,bk,t,_) l -> |
| 229 |
match t with |
| 230 |
| Some _ -> l |
| 231 |
| _ -> add_impl ?loc:c.CAst.loc na bk l) |
| 232 |
args.(nb) (aux bds.(nb)) |
| 233 |
| _ -> [] |
| 234 |
in aux l |