| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open Util |
| 13 |
open CAst |
| 14 |
open CErrors |
| 15 |
open Names |
| 16 |
open Libnames |
| 17 |
open Locus |
| 18 |
open Tac2env |
| 19 |
open Tac2print |
| 20 |
open Tac2expr |
| 21 |
open Tac2typing_env |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
let rocq_type n = KerName.make Tac2env.rocq_prefix (Id.of_string n) |
| 26 |
|
| 27 |
let t_int = rocq_type "int" |
| 28 |
let t_string = rocq_type "string" |
| 29 |
let t_constr = rocq_type "constr" |
| 30 |
let t_preterm = rocq_type "preterm" |
| 31 |
let t_pattern = rocq_type "pattern" |
| 32 |
let t_ident = rocq_type "ident" |
| 33 |
let t_bool = rocq_type "bool" |
| 34 |
|
| 35 |
let ltac2_env : Tac2typing_env.t Genintern.Store.field = |
| 36 |
Genintern.Store.field "ltac2_env" |
| 37 |
|
| 38 |
let drop_ltac2_env store = |
| 39 |
Genintern.Store.remove store ltac2_env |
| 40 |
|
| 41 |
let error_nargs_mismatch ?loc kn nargs nfound = |
| 42 |
let cstr = Tac2env.shortest_qualid_of_constructor kn in |
| 43 |
user_err ?loc (str "Constructor " ++ pr_qualid cstr ++ str " expects " ++ |
| 44 |
int nargs ++ str " arguments, but is applied to " ++ int nfound ++ |
| 45 |
str " arguments") |
| 46 |
|
| 47 |
let error_nparams_mismatch ?loc nargs nfound = |
| 48 |
user_err ?loc (str "Type expects " ++ int nargs ++ |
| 49 |
str " arguments, but is applied to " ++ int nfound ++ |
| 50 |
str " arguments") |
| 51 |
|
| 52 |
let rec intern_type env ({loc;v=t} : raw_typexpr) : TVar.t glb_typexpr = match t with |
| 53 |
| CTypVar (Name id) -> GTypVar (get_alias (CAst.make ?loc id) env) |
| 54 |
| CTypVar Anonymous -> GTypVar (fresh_id env) |
| 55 |
| CTypRef (rel, args) -> |
| 56 |
let (kn, nparams) = match rel with |
| 57 |
| RelId qid -> |
| 58 |
begin match (if qualid_is_ident qid then find_rec_var (qualid_basename qid) env else None) with |
| 59 |
| Some (kn, n) -> |
| 60 |
(Other kn, n) |
| 61 |
| None -> |
| 62 |
let kn = |
| 63 |
try Tac2env.locate_type qid |
| 64 |
with Not_found -> |
| 65 |
user_err ?loc (str "Unbound type constructor " ++ pr_qualid qid) |
| 66 |
in |
| 67 |
let (nparams, _) = Tac2env.interp_type kn in |
| 68 |
(Other kn, nparams) |
| 69 |
end |
| 70 |
| AbsKn (Other kn) -> |
| 71 |
let (nparams, _) = Tac2env.interp_type kn in |
| 72 |
(Other kn, nparams) |
| 73 |
| AbsKn (Tuple n) -> |
| 74 |
(Tuple n, n) |
| 75 |
in |
| 76 |
let nargs = List.length args in |
| 77 |
let () = |
| 78 |
if not (Int.equal nparams nargs) then |
| 79 |
let qid = match rel with |
| 80 |
| RelId lid -> lid |
| 81 |
| AbsKn (Other kn) -> shortest_qualid_of_type ?loc kn |
| 82 |
| AbsKn (Tuple _) -> assert false |
| 83 |
in |
| 84 |
user_err ?loc (strbrk "The type constructor " ++ pr_qualid qid ++ |
| 85 |
strbrk " expects " ++ int nparams ++ strbrk " argument(s), but is here \ |
| 86 |
applied to " ++ int nargs ++ strbrk "argument(s)") |
| 87 |
in |
| 88 |
GTypRef (kn, List.map (fun t -> intern_type env t) args) |
| 89 |
| CTypArrow (t1, t2) -> GTypArrow (intern_type env t1, intern_type env t2) |
| 90 |
|
| 91 |
let fresh_type_scheme env (t : type_scheme) : TVar.t glb_typexpr = |
| 92 |
let (n, t) = t in |
| 93 |
let subst = Array.init n (fun _ -> fresh_id env) in |
| 94 |
let substf i = GTypVar subst.(i) in |
| 95 |
subst_type substf t |
| 96 |
|
| 97 |
let fresh_mix_type_scheme env (t : mix_type_scheme) : TVar.t glb_typexpr = |
| 98 |
let (n, t) = t in |
| 99 |
let subst = Array.init n (fun _ -> fresh_id env) in |
| 100 |
let substf = function |
| 101 |
| LVar i -> GTypVar subst.(i) |
| 102 |
| GVar n -> GTypVar n |
| 103 |
in |
| 104 |
subst_type substf t |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
let is_pure_constructor kn = |
| 109 |
match snd (Tac2env.interp_type kn) with |
| 110 |
| GTydAlg _ | GTydOpn -> true |
| 111 |
| GTydRec fields -> |
| 112 |
let is_pure (_, mut, _) = not mut in |
| 113 |
List.for_all is_pure fields |
| 114 |
| GTydDef _ -> assert false |
| 115 |
|
| 116 |
let is_pure_field kn i = |
| 117 |
match snd (Tac2env.interp_type kn) with |
| 118 |
| GTydRec fields -> |
| 119 |
let is_pure (_, mut, _) = not mut in |
| 120 |
is_pure (List.nth fields i) |
| 121 |
| GTydAlg _ | GTydOpn | GTydDef _ -> assert false |
| 122 |
|
| 123 |
type not_value_reason = |
| 124 |
| MutString |
| 125 |
| Application |
| 126 |
| MutDef of ltac_constant |
| 127 |
| MutCtor of type_constant |
| 128 |
| MutProj of type_constant |
| 129 |
| MaybeValButNotSupported |
| 130 |
|
| 131 |
let rec check_value = function |
| 132 |
| GTacAtm (AtmInt _) | GTacVar _ | GTacPrm _ -> None |
| 133 |
| GTacFun (bnd,_) -> assert (not (CList.is_empty bnd)); None |
| 134 |
| GTacAtm (AtmStr _) -> Some MutString |
| 135 |
| GTacApp _ -> Some Application |
| 136 |
| GTacRef kn -> |
| 137 |
if (Tac2env.interp_global kn).gdata_mutable then Some (MutDef kn) |
| 138 |
else None |
| 139 |
| GTacCst (Tuple _, _, el) -> List.find_map check_value el |
| 140 |
| GTacCst (_, _, []) -> None |
| 141 |
| GTacOpn (_, el) -> List.find_map check_value el |
| 142 |
| GTacCst (Other kn, _, el) -> |
| 143 |
if is_pure_constructor kn then List.find_map check_value el |
| 144 |
else Some (MutCtor kn) |
| 145 |
| GTacLet (_, bnd, e) -> |
| 146 |
|
| 147 |
List.find_map (fun (_, e) -> check_value e) ((Anonymous,e)::bnd) |
| 148 |
| GTacPrj (kn,e,i) -> if is_pure_field kn i then check_value e |
| 149 |
else Some (MutProj kn) |
| 150 |
| GTacCse _ | GTacSet _ | GTacExt _ |
| 151 |
| GTacWth _ | GTacFullMatch _ -> Some MaybeValButNotSupported |
| 152 |
|
| 153 |
let is_rec_rhs = function |
| 154 |
| GTacFun _ -> true |
| 155 |
| GTacAtm _ | GTacVar _ | GTacRef _ | GTacApp _ | GTacLet _ | GTacPrj _ |
| 156 |
| GTacSet _ | GTacExt _ | GTacPrm _ | GTacCst _ |
| 157 |
| GTacCse _ | GTacOpn _ | GTacWth _ | GTacFullMatch _-> false |
| 158 |
|
| 159 |
let warn_not_unit = |
| 160 |
CWarnings.create ~name:"not-unit" ~category:CWarnings.CoreCategories.ltac2 |
| 161 |
(fun (env, t) -> |
| 162 |
strbrk "This expression should have type unit but has type " ++ |
| 163 |
pr_glbtype env t ++ str ".") |
| 164 |
|
| 165 |
let check_elt_unit loc env t = |
| 166 |
let maybe_unit = match kind env t with |
| 167 |
| GTypVar _ -> true |
| 168 |
| GTypArrow _ -> false |
| 169 |
| GTypRef (Tuple 0, []) -> true |
| 170 |
| GTypRef _ -> false |
| 171 |
in |
| 172 |
if not maybe_unit then warn_not_unit ?loc (env, t) |
| 173 |
|
| 174 |
let is_empty_type env t = match kind env t with |
| 175 |
| GTypVar _ | GTypArrow _ | GTypRef (Tuple _, _) -> false |
| 176 |
| GTypRef (Other kn, _) -> |
| 177 |
let def = Tac2env.interp_type kn in |
| 178 |
match def with |
| 179 |
| _, GTydAlg { galg_constructors = [] } -> true |
| 180 |
| _ -> false |
| 181 |
|
| 182 |
let check_elt_empty loc env t = match kind env t with |
| 183 |
| GTypVar _ -> |
| 184 |
user_err ?loc (str "Cannot infer an empty type for this expression") |
| 185 |
| GTypArrow _ | GTypRef (Tuple _, _) -> |
| 186 |
user_err ?loc (str "Type" ++ spc () ++ pr_glbtype env t ++ spc () ++ str "is not an empty type") |
| 187 |
| GTypRef (Other kn, _) -> |
| 188 |
let def = Tac2env.interp_type kn in |
| 189 |
match def with |
| 190 |
| _, GTydAlg { galg_constructors = [] } -> kn |
| 191 |
| _ -> |
| 192 |
user_err ?loc (str "Type" ++ spc () ++ pr_glbtype env t ++ spc () ++ str "is not an empty type") |
| 193 |
|
| 194 |
let check_unit ?loc t = |
| 195 |
let env = empty_env () in |
| 196 |
|
| 197 |
let t = fresh_type_scheme env t in |
| 198 |
let maybe_unit = match kind env t with |
| 199 |
| GTypVar _ -> true |
| 200 |
| GTypArrow _ -> false |
| 201 |
| GTypRef (Tuple 0, []) -> true |
| 202 |
| GTypRef _ -> false |
| 203 |
in |
| 204 |
if not maybe_unit then warn_not_unit ?loc (env, t) |
| 205 |
|
| 206 |
let get_constructor env var = match var with |
| 207 |
| RelId qid -> |
| 208 |
let c = try Some (Tac2env.locate_constructor qid) with Not_found -> None in |
| 209 |
begin match c with |
| 210 |
| Some knc -> Other knc |
| 211 |
| None -> |
| 212 |
CErrors.user_err ?loc:qid.CAst.loc (str "Unbound constructor " ++ pr_qualid qid) |
| 213 |
end |
| 214 |
| AbsKn knc -> knc |
| 215 |
|
| 216 |
let get_projection var = match var with |
| 217 |
| RelId qid -> |
| 218 |
let kn = try Tac2env.locate_projection qid with Not_found -> |
| 219 |
user_err ?loc:qid.CAst.loc (pr_qualid qid ++ str " is not a projection") |
| 220 |
in |
| 221 |
Tac2env.interp_projection kn |
| 222 |
| AbsKn kn -> |
| 223 |
Tac2env.interp_projection kn |
| 224 |
|
| 225 |
let intern_atm env = function |
| 226 |
| AtmInt n -> (GTacAtm (AtmInt n), GTypRef (Other t_int, [])) |
| 227 |
| AtmStr s -> (GTacAtm (AtmStr s), GTypRef (Other t_string, [])) |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
let fresh_var avoid = |
| 233 |
let bad id = |
| 234 |
Id.Set.mem id avoid || |
| 235 |
(try ignore (locate_ltac (qualid_of_ident id)); true with Not_found -> false) |
| 236 |
in |
| 237 |
Namegen.next_ident_away_from (Id.of_string "p") bad |
| 238 |
|
| 239 |
let add_name accu = function |
| 240 |
| Name id -> Id.Set.add id accu |
| 241 |
| Anonymous -> accu |
| 242 |
|
| 243 |
let loc_of_relid = function |
| 244 |
| RelId {loc} -> loc |
| 245 |
| AbsKn _ -> None |
| 246 |
|
| 247 |
let is_unit_pattern = function |
| 248 |
| CPatRef (AbsKn (Tuple 0), []) -> true |
| 249 |
| _ -> false |
| 250 |
|
| 251 |
let extract_pattern_type ({loc;v=p} as pat) = match p with |
| 252 |
| CPatCnv (pat, ty) -> pat, Some ty |
| 253 |
| CPatAtm _ | CPatVar _ | CPatRef _ | CPatOr _ | CPatAs _ | CPatRecord _ -> |
| 254 |
if is_unit_pattern p then |
| 255 |
|
| 256 |
let t_unit = CAst.make ?loc @@ CTypRef (AbsKn (Tuple 0), []) in |
| 257 |
pat, Some t_unit |
| 258 |
else pat, None |
| 259 |
|
| 260 |
|
| 261 |
let expand_pattern avoid bnd = |
| 262 |
let fold (avoid, bnd) (pat, t) = |
| 263 |
let na, expand = match pat.v with |
| 264 |
| CPatVar na -> |
| 265 |
|
| 266 |
na, None |
| 267 |
| _ -> |
| 268 |
if is_unit_pattern pat.v then |
| 269 |
Anonymous, None |
| 270 |
else |
| 271 |
let id = fresh_var avoid in |
| 272 |
let qid = RelId (qualid_of_ident ?loc:pat.loc id) in |
| 273 |
Name id, Some qid |
| 274 |
in |
| 275 |
let avoid = ids_of_pattern avoid pat in |
| 276 |
let avoid = add_name avoid na in |
| 277 |
(avoid, (na, pat, expand) :: bnd) |
| 278 |
in |
| 279 |
let (_, bnd) = List.fold_left fold (avoid, []) bnd in |
| 280 |
let fold e (na, pat, expand) = match expand with |
| 281 |
| None -> e |
| 282 |
| Some qid -> |
| 283 |
let loc = loc_of_relid qid in |
| 284 |
CAst.make ?loc @@ CTacCse (CAst.make ?loc @@ CTacRef qid, [pat, e]) |
| 285 |
in |
| 286 |
let expand e = List.fold_left fold e bnd in |
| 287 |
let nas = List.rev_map (fun (na, _, _) -> na) bnd in |
| 288 |
(nas, expand) |
| 289 |
|
| 290 |
let is_alias env qid = match get_variable env qid with |
| 291 |
| ArgArg (TacAlias _) -> true |
| 292 |
| ArgVar _ | (ArgArg (TacConstant _)) -> false |
| 293 |
|
| 294 |
let is_user_name qid = match qid with |
| 295 |
| AbsKn _ -> false |
| 296 |
| RelId _ -> true |
| 297 |
|
| 298 |
let deprecated_ltac2_alias = |
| 299 |
Deprecation.create_warning |
| 300 |
~object_name:"Ltac2 abbreviation" |
| 301 |
~warning_name_if_no_since:"deprecated-ltac2-abbreviation" |
| 302 |
(fun kn -> pr_qualid (Tac2env.shortest_qualid_of_ltac Id.Set.empty (TacAlias kn))) |
| 303 |
|
| 304 |
let deprecated_ltac2_def = |
| 305 |
Deprecation.create_warning |
| 306 |
~object_name:"Ltac2 definition" |
| 307 |
~warning_name_if_no_since:"deprecated-ltac2-definition" |
| 308 |
(fun kn -> pr_qualid (Tac2env.shortest_qualid_of_ltac Id.Set.empty (TacConstant kn))) |
| 309 |
|
| 310 |
let check_deprecated_ltac2 ?loc qid def = |
| 311 |
if is_user_name qid then match def with |
| 312 |
| TacAlias kn -> |
| 313 |
begin match (Tac2env.interp_alias kn).alias_depr with |
| 314 |
| None -> () |
| 315 |
| Some depr -> deprecated_ltac2_alias ?loc (kn, depr) |
| 316 |
end |
| 317 |
| TacConstant kn -> |
| 318 |
begin match (Tac2env.interp_global kn).gdata_deprecation with |
| 319 |
| None -> () |
| 320 |
| Some depr -> deprecated_ltac2_def ?loc (kn, depr) |
| 321 |
end |
| 322 |
|
| 323 |
type ('a,'b) field = |
| 324 |
| PresentField of 'a |
| 325 |
| MissingField of 'b |
| 326 |
|
| 327 |
let intern_record env loc fs = |
| 328 |
let map (proj, e) = |
| 329 |
let loc = match proj with |
| 330 |
| RelId {CAst.loc} -> loc |
| 331 |
| AbsKn _ -> None |
| 332 |
in |
| 333 |
let proj = get_projection proj in |
| 334 |
(loc, proj, e) |
| 335 |
in |
| 336 |
let fs = List.map map fs in |
| 337 |
let kn = match fs with |
| 338 |
| [] -> user_err ?loc (str "Cannot infer the corresponding record type") |
| 339 |
| (_, proj, _) :: _ -> proj.pdata_type |
| 340 |
in |
| 341 |
let params, typdef = match Tac2env.interp_type kn with |
| 342 |
| n, GTydRec def -> n, def |
| 343 |
| _ -> assert false |
| 344 |
in |
| 345 |
let subst = Array.init params (fun _ -> fresh_id env) in |
| 346 |
|
| 347 |
let args = Array.make (List.length typdef) None in |
| 348 |
let iter (loc, pinfo, e) = |
| 349 |
if KerName.equal kn pinfo.pdata_type then |
| 350 |
let index = pinfo.pdata_indx in |
| 351 |
match args.(index) with |
| 352 |
| None -> |
| 353 |
let exp = subst_type (fun i -> GTypVar subst.(i)) pinfo.pdata_ptyp in |
| 354 |
args.(index) <- Some (e, exp) |
| 355 |
| Some _ -> |
| 356 |
let (name, _, _) = List.nth typdef pinfo.pdata_indx in |
| 357 |
user_err ?loc (str "Field " ++ Id.print name ++ str " is defined \ |
| 358 |
several times") |
| 359 |
else |
| 360 |
user_err ?loc (str "Field " ++ str " does not \ |
| 361 |
pertain to record definition " ++ pr_typref pinfo.pdata_type) |
| 362 |
in |
| 363 |
let () = List.iter iter fs in |
| 364 |
let args = Array.mapi (fun i arg -> match arg with |
| 365 |
| None -> |
| 366 |
let field, _, typ = List.nth typdef i in |
| 367 |
let typ' = subst_type (fun i -> GTypVar subst.(i)) typ in |
| 368 |
MissingField (i, field, typ, typ') |
| 369 |
| Some arg -> PresentField arg) |
| 370 |
args |
| 371 |
in |
| 372 |
let tparam = List.init params (fun i -> GTypVar subst.(i)) in |
| 373 |
kn, tparam, args |
| 374 |
|
| 375 |
let ctor_data_for_patterns kn data = { |
| 376 |
ctyp = Some data.cdata_type; |
| 377 |
cnargs = List.length data.cdata_args; |
| 378 |
cindx = match data.cdata_indx with None -> Open kn | Some i -> Closed i; |
| 379 |
} |
| 380 |
|
| 381 |
let ctor_data_of_tuple n = { |
| 382 |
ctyp = None; |
| 383 |
cnargs = n; |
| 384 |
cindx = Closed 0; |
| 385 |
} |
| 386 |
|
| 387 |
type wip_pat_r = |
| 388 |
| PatVar of Name.t |
| 389 |
| PatAtm of atom |
| 390 |
| PatRef of ctor_data_for_patterns * wip_pat list |
| 391 |
| PatOr of wip_pat list |
| 392 |
| PatAs of wip_pat * lident |
| 393 |
and wip_pat = wip_pat_r CAst.t |
| 394 |
|
| 395 |
let catchall = CAst.make (PatVar Anonymous) |
| 396 |
|
| 397 |
let pat_or ?loc = function |
| 398 |
| [] -> assert false |
| 399 |
| [x] -> x |
| 400 |
| pats -> CAst.make ?loc (PatOr pats) |
| 401 |
|
| 402 |
let rec intern_pat_rec env cpat t = |
| 403 |
let loc = cpat.loc in |
| 404 |
match cpat.v with |
| 405 |
| CPatVar x -> begin match x with |
| 406 |
| Anonymous -> Id.Map.empty, CAst.make ?loc (PatVar x) |
| 407 |
| Name id -> |
| 408 |
let patvars = Id.Map.singleton id (loc,t) in |
| 409 |
patvars, CAst.make ?loc (PatVar x) |
| 410 |
end |
| 411 |
| CPatAtm atm -> |
| 412 |
let _, t' = intern_atm env atm in |
| 413 |
let () = unify ?loc env t t' in |
| 414 |
Id.Map.empty, CAst.make ?loc (PatAtm atm) |
| 415 |
| CPatAs (p, x) -> |
| 416 |
let patvars, p = intern_pat_rec env p t in |
| 417 |
let patvars = Id.Map.update x.v (function |
| 418 |
| Some _ -> |
| 419 |
CErrors.user_err ?loc |
| 420 |
Pp.(str "Variable " ++ Id.print x.v ++ |
| 421 |
str " is bound several times in this matching.") |
| 422 |
| None -> Some (x.loc,t)) |
| 423 |
patvars |
| 424 |
in |
| 425 |
patvars, CAst.make ?loc (PatAs (p, x)) |
| 426 |
| CPatRef (ctor,args) -> |
| 427 |
let ctor = get_constructor env ctor in |
| 428 |
let ctor, argts = |
| 429 |
let nargs = List.length args in |
| 430 |
match ctor with |
| 431 |
| Tuple n -> |
| 432 |
assert (Int.equal nargs n); |
| 433 |
let ts = List.init n (fun _ -> GTypVar (fresh_id env)) in |
| 434 |
let () = unify ?loc env t (GTypRef (ctor, ts)) in |
| 435 |
ctor_data_of_tuple n, ts |
| 436 |
| Other kn -> |
| 437 |
let data = interp_constructor kn in |
| 438 |
let nexpectargs = List.length data.cdata_args in |
| 439 |
if not (Int.equal nargs nexpectargs) then error_nargs_mismatch ?loc kn nexpectargs nargs; |
| 440 |
let subst = Array.init data.cdata_prms (fun _ -> fresh_id env) in |
| 441 |
let substf i = GTypVar subst.(i) in |
| 442 |
let types = List.map (fun t -> subst_type substf t) data.cdata_args in |
| 443 |
let targs = List.init data.cdata_prms substf in |
| 444 |
let ans = GTypRef (Other data.cdata_type, targs) in |
| 445 |
let () = unify ?loc env t ans in |
| 446 |
ctor_data_for_patterns kn data, types |
| 447 |
in |
| 448 |
let patvars, args = CList.fold_left2_map (fun patvars arg argt -> |
| 449 |
let argvars, arg = intern_pat_rec env arg argt in |
| 450 |
let patvars = Id.Map.union (fun id _ (loc,_) -> |
| 451 |
CErrors.user_err ?loc |
| 452 |
Pp.(str "Variable " ++ Id.print id ++ |
| 453 |
str " is bound several times in this matching.")) |
| 454 |
patvars argvars |
| 455 |
in |
| 456 |
patvars, arg) |
| 457 |
Id.Map.empty |
| 458 |
args |
| 459 |
argts |
| 460 |
in |
| 461 |
patvars, CAst.make ?loc (PatRef (ctor,args)) |
| 462 |
| CPatRecord pats -> |
| 463 |
let kn, tparam, args = intern_record env loc pats in |
| 464 |
let () = unify ?loc env t (GTypRef (Other kn, tparam)) in |
| 465 |
let args = Array.to_list args in |
| 466 |
let patvars, args = CList.fold_left_map (fun patvars -> function |
| 467 |
| MissingField _ -> patvars, catchall |
| 468 |
| PresentField (arg, argty) -> |
| 469 |
let (argvars,arg) = intern_pat_rec env arg argty in |
| 470 |
let patvars = Id.Map.union (fun id _ (loc,_) -> |
| 471 |
CErrors.user_err ?loc |
| 472 |
Pp.(str "Variable " ++ Id.print id ++ |
| 473 |
str " is bound several times in this matching.")) |
| 474 |
patvars argvars |
| 475 |
in |
| 476 |
patvars, arg) |
| 477 |
Id.Map.empty |
| 478 |
args |
| 479 |
in |
| 480 |
let ctor = { ctyp = Some kn; cnargs = List.length args; cindx = Closed 0 } in |
| 481 |
patvars, CAst.make ?loc (PatRef (ctor, args)) |
| 482 |
| CPatCnv (pat,typ) -> |
| 483 |
let typ = intern_type env typ in |
| 484 |
let () = unify ?loc env t typ in |
| 485 |
intern_pat_rec env pat typ |
| 486 |
| CPatOr [] -> assert false |
| 487 |
| CPatOr (first::rest) -> |
| 488 |
let patvars, first = intern_pat_rec env first t in |
| 489 |
let rest = List.map (fun pat -> |
| 490 |
let patvars', pat = intern_pat_rec env pat t in |
| 491 |
if not (Id.Map.equal (fun (_,t) (loc,t') -> |
| 492 |
unify ?loc env t t'; |
| 493 |
true) |
| 494 |
patvars patvars') |
| 495 |
|
| 496 |
then begin |
| 497 |
let leftdom = Id.Map.domain patvars in |
| 498 |
let rightdom = Id.Map.domain patvars' in |
| 499 |
let only_left = Id.Set.diff leftdom rightdom in |
| 500 |
let only_right = Id.Set.diff rightdom leftdom in |
| 501 |
let pp_side side vars () = if Id.Set.is_empty vars then mt() else Pp.fmt "@ (%s pattern also binds %t)" side (fun () -> Pp.pr_enum Id.print (Id.Set.elements vars)) in |
| 502 |
CErrors.user_err ?loc Pp.(fmt "These patterns do not bind the same variables%t%t." (pp_side "left" only_left) (pp_side "right" only_right)) |
| 503 |
end; |
| 504 |
pat) |
| 505 |
rest |
| 506 |
in |
| 507 |
patvars, CAst.make ?loc (PatOr (first::rest)) |
| 508 |
|
| 509 |
|
| 510 |
let intern_pat env cpat t = |
| 511 |
let patvars, pat = intern_pat_rec env cpat t in |
| 512 |
Id.Map.map (fun (_,v) -> monomorphic v) patvars, pat |
| 513 |
|
| 514 |
let rec glb_of_wip_pat_r = function |
| 515 |
| PatVar x -> GPatVar x |
| 516 |
| PatAtm atm -> GPatAtm atm |
| 517 |
| PatRef (ctor,pats) -> GPatRef (ctor, List.map glb_of_wip_pat pats) |
| 518 |
| PatOr pats -> GPatOr (List.map glb_of_wip_pat pats) |
| 519 |
| PatAs (p,x) -> GPatAs (glb_of_wip_pat p, x.v) |
| 520 |
and glb_of_wip_pat pat = glb_of_wip_pat_r pat.CAst.v |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
let default_matrix = |
| 525 |
let rec default_row = function |
| 526 |
| [] -> assert false |
| 527 |
| {v=PatRef _ | PatAtm _} :: _ -> [] |
| 528 |
| {v=PatVar _} :: rest -> [rest] |
| 529 |
| {v=PatOr pats} :: rest -> List.map_append default_row (List.map (fun x -> x::rest) pats) |
| 530 |
| {v=PatAs (p,_)} :: rest -> default_row (p::rest) |
| 531 |
in |
| 532 |
List.map_append default_row |
| 533 |
|
| 534 |
type generalized_ctor = |
| 535 |
| AtomCtor of atom |
| 536 |
| OtherCtor of ctor_data_for_patterns |
| 537 |
|
| 538 |
let rec root_ctors = function |
| 539 |
| {v=PatVar _} -> [] |
| 540 |
| {v=PatRef (ctor,_)} -> [OtherCtor ctor] |
| 541 |
| {v=PatAtm a} -> [AtomCtor a] |
| 542 |
| {v=PatOr pats} -> List.map_append root_ctors pats |
| 543 |
| {v=PatAs (p,_)} -> root_ctors p |
| 544 |
|
| 545 |
|
| 546 |
type missing_ctors = |
| 547 |
| Unknown |
| 548 |
| Extension of { example : atom option } |
| 549 |
| Known of ctor_data_for_patterns list |
| 550 |
|
| 551 |
type maybe_missing_ctors = |
| 552 |
| Missing of missing_ctors |
| 553 |
| NoMissing of ctor_data_for_patterns list |
| 554 |
|
| 555 |
let make_ctor ctyp tdata is_const n = |
| 556 |
let cnargs = if is_const then 0 else |
| 557 |
let rec find n = function |
| 558 |
| [] -> assert false |
| 559 |
| (_, _, []) :: rem -> |
| 560 |
find n rem |
| 561 |
| (_, _, argtys) :: rem -> |
| 562 |
if Int.equal n 0 then List.length argtys |
| 563 |
else find (pred n) rem |
| 564 |
in |
| 565 |
find n tdata.galg_constructors |
| 566 |
in |
| 567 |
{ |
| 568 |
ctyp; |
| 569 |
cindx = Closed n; |
| 570 |
cnargs; |
| 571 |
} |
| 572 |
|
| 573 |
let make_int_example ints = |
| 574 |
let rec aux i = if Int.Set.mem i ints then aux (i+1) else i |
| 575 |
in aux 0 |
| 576 |
|
| 577 |
let make_string_example strings = |
| 578 |
let rec aux s = if String.Set.mem s strings then aux (s^"*") else s |
| 579 |
in aux "" |
| 580 |
|
| 581 |
let make_atom_example = function |
| 582 |
| AtomCtor (AtmInt i) :: rest -> |
| 583 |
let ints = List.fold_left (fun ints c -> match c with |
| 584 |
| AtomCtor (AtmInt i) -> Int.Set.add i ints |
| 585 |
| _ -> assert false) |
| 586 |
(Int.Set.singleton i) |
| 587 |
rest |
| 588 |
in |
| 589 |
AtmInt (make_int_example ints) |
| 590 |
| AtomCtor (AtmStr s) :: rest -> |
| 591 |
let strings = List.fold_left (fun strings c -> match c with |
| 592 |
| AtomCtor (AtmStr s) -> String.Set.add s strings |
| 593 |
| _ -> assert false) |
| 594 |
(String.Set.singleton s) |
| 595 |
rest |
| 596 |
in |
| 597 |
AtmStr (make_string_example strings) |
| 598 |
| OtherCtor _ :: _ | [] -> assert false |
| 599 |
|
| 600 |
|
| 601 |
let missing_ctors_from env t = function |
| 602 |
| [] -> |
| 603 |
|
| 604 |
if is_empty_type env t then NoMissing [] |
| 605 |
else Missing Unknown |
| 606 |
| AtomCtor _ :: _ as l -> Missing (Extension {example=Some (make_atom_example l)}) |
| 607 |
| OtherCtor {ctyp=None; cnargs} :: _ -> |
| 608 |
NoMissing [ctor_data_of_tuple cnargs] |
| 609 |
| OtherCtor {cindx=Open _} :: _ -> Missing (Extension {example=None}) |
| 610 |
| OtherCtor ({ctyp=Some ctyp} as data) :: _ as ctors -> |
| 611 |
let _, tdata = interp_type ctyp in |
| 612 |
match tdata with |
| 613 |
| GTydOpn | GTydDef _ -> assert false |
| 614 |
| GTydRec _ -> NoMissing [data] |
| 615 |
| GTydAlg tdata -> |
| 616 |
let const = Array.make tdata.galg_nconst false in |
| 617 |
let nonconst = Array.make tdata.galg_nnonconst false in |
| 618 |
let () = List.iter (function |
| 619 |
| OtherCtor {cindx=Closed i; cnargs} -> |
| 620 |
let which = if Int.equal 0 cnargs then const else nonconst in |
| 621 |
which.(i) <- true |
| 622 |
| AtomCtor _ | OtherCtor {cindx=Open _} -> assert false) |
| 623 |
ctors |
| 624 |
in |
| 625 |
let fold is_const i (missing, present) ispresent = |
| 626 |
let ctor = (make_ctor data.ctyp tdata is_const i) in |
| 627 |
if ispresent then missing, ctor :: present |
| 628 |
else ctor :: missing, present |
| 629 |
in |
| 630 |
let acc = CArray.fold_left_i (fold false) ([],[]) nonconst in |
| 631 |
let missing, present = CArray.fold_left_i (fold true) acc const in |
| 632 |
if List.is_empty missing then NoMissing present |
| 633 |
else Missing (Known missing) |
| 634 |
|
| 635 |
let specialized_types env ts ctor = match ts with |
| 636 |
| [] -> assert false |
| 637 |
| t :: rest -> |
| 638 |
let argts = match ctor with |
| 639 |
| AtomCtor _ -> [] |
| 640 |
| OtherCtor {ctyp=None; cnargs=n} -> |
| 641 |
let argts = List.init n (fun _ -> GTypVar (fresh_id env)) in |
| 642 |
let () = unify env t (GTypRef (Tuple n, argts)) in |
| 643 |
argts |
| 644 |
| OtherCtor {cindx=Open kn} -> |
| 645 |
let data = interp_constructor kn in |
| 646 |
let subst = Array.init data.cdata_prms (fun _ -> fresh_id env) in |
| 647 |
let substf i = GTypVar subst.(i) in |
| 648 |
let types = List.map (fun t -> subst_type substf t) data.cdata_args in |
| 649 |
let targs = List.init data.cdata_prms substf in |
| 650 |
let ans = GTypRef (Other data.cdata_type, targs) in |
| 651 |
let () = unify env t ans in |
| 652 |
types |
| 653 |
| OtherCtor {ctyp=Some ctyp; cnargs; cindx=Closed i} -> |
| 654 |
let ntargs, tdata = interp_type ctyp in |
| 655 |
match tdata with |
| 656 |
| GTydOpn | GTydDef _ -> assert false |
| 657 |
| GTydRec tdata -> |
| 658 |
let subst = Array.init ntargs (fun _ -> fresh_id env) in |
| 659 |
let substf i = GTypVar subst.(i) in |
| 660 |
let types = List.map (fun (_,_,t) -> subst_type substf t) tdata in |
| 661 |
let targs = List.init ntargs substf in |
| 662 |
let ans = GTypRef (Other ctyp, targs) in |
| 663 |
let () = unify env t ans in |
| 664 |
types |
| 665 |
| GTydAlg tdata -> |
| 666 |
let ctors = List.filter (fun (_, _,argts) -> |
| 667 |
if cnargs = 0 |
| 668 |
then List.is_empty argts |
| 669 |
else not (List.is_empty argts)) |
| 670 |
tdata.galg_constructors |
| 671 |
in |
| 672 |
let _, _, argts = List.nth ctors i in |
| 673 |
let subst = Array.init ntargs (fun _ -> fresh_id env) in |
| 674 |
let substf i = GTypVar subst.(i) in |
| 675 |
let types = List.map (fun t -> subst_type substf t) argts in |
| 676 |
let targs = List.init ntargs substf in |
| 677 |
let ans = GTypRef (Other ctyp, targs) in |
| 678 |
let () = unify env t ans in |
| 679 |
types |
| 680 |
in |
| 681 |
List.append argts rest |
| 682 |
|
| 683 |
let specialized_multi_matrix (patsP, patsQ, patsR) ctor = |
| 684 |
let same_atom atm atm' = match atm, atm' with |
| 685 |
| AtmInt i, AtmInt j -> Int.equal i j |
| 686 |
| AtmStr i, AtmStr j -> String.equal i j |
| 687 |
| AtmInt _, AtmStr _ | AtmStr _, AtmInt _ -> assert false |
| 688 |
in |
| 689 |
let same_ctor_indx i j = match i, j with |
| 690 |
| Closed i, Closed j -> Int.equal i j |
| 691 |
| Open kn, Open kn' -> KerName.equal kn kn' |
| 692 |
| Closed _, Open _ | Open _, Closed _ -> false |
| 693 |
in |
| 694 |
let same_ctor ctor ctor' = match ctor, ctor' with |
| 695 |
| AtomCtor atm, AtomCtor atm' -> same_atom atm atm' |
| 696 |
| OtherCtor ctor, OtherCtor ctor' -> |
| 697 |
Int.equal ctor.cnargs ctor'.cnargs |
| 698 |
&& same_ctor_indx ctor.cindx ctor'.cindx |
| 699 |
| AtomCtor _, OtherCtor _ | OtherCtor _, AtomCtor _ -> assert false |
| 700 |
in |
| 701 |
let rec special_row rowP rowQ rowR = match rowP with |
| 702 |
| [] -> assert false |
| 703 |
| {v=PatRef (ctor',args)} :: rest -> |
| 704 |
if same_ctor ctor (OtherCtor ctor') then [List.append args rest, rowQ, rowR] |
| 705 |
else [] |
| 706 |
| {v=PatAtm atm} :: rest -> |
| 707 |
if same_ctor ctor (AtomCtor atm) then [rest, rowQ, rowR] |
| 708 |
else [] |
| 709 |
| {v=PatVar _} :: rest -> begin match ctor with |
| 710 |
| OtherCtor ctor -> [List.append (List.make ctor.cnargs catchall) rest, rowQ, rowR] |
| 711 |
| AtomCtor _ -> [rest, rowQ, rowR] |
| 712 |
end |
| 713 |
| {v=PatOr pats} :: rest -> |
| 714 |
List.map_append (fun x -> special_row (x::rest) rowQ rowR) pats |
| 715 |
| {v=PatAs (p,_)} :: rest -> special_row (p::rest) rowQ rowR |
| 716 |
in |
| 717 |
let res = List.flatten (List.map3 special_row patsP patsQ patsR) in |
| 718 |
List.split3 res |
| 719 |
|
| 720 |
let specialized_matrix pats ctor = |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
let dummy = List.make (List.length pats) () in |
| 725 |
let pats, _, _ = specialized_multi_matrix (pats, dummy, dummy) ctor in |
| 726 |
pats |
| 727 |
|
| 728 |
let rec lift_interned_pat pat = CAst.map lift_interned_pat_r pat |
| 729 |
and lift_interned_pat_r = let open PartialPat in function |
| 730 |
| PatVar x -> Var x |
| 731 |
| PatAtm a -> Atom a |
| 732 |
| PatRef (ctor, pats) -> Ref (ctor, List.map lift_interned_pat pats) |
| 733 |
| PatOr pats -> Or (List.map lift_interned_pat pats) |
| 734 |
| PatAs (p,x) -> As (lift_interned_pat p, x.v) |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
let rec missing_matches env ts pats n = |
| 743 |
match n with |
| 744 |
| 0 -> begin match pats with [] -> Some [] | _::_ -> None end |
| 745 |
| _ -> |
| 746 |
let root_ctors = List.map_append root_ctors (List.map List.hd pats) in |
| 747 |
match missing_ctors_from env (List.hd ts) root_ctors with |
| 748 |
| NoMissing ctors -> specialized_missing_matches env ts pats n ctors |
| 749 |
| Missing missing_ctors -> |
| 750 |
match missing_matches env (List.tl ts) (default_matrix pats) (n-1) with |
| 751 |
| None -> None |
| 752 |
| Some missing -> match missing_ctors with |
| 753 |
| Unknown -> Some (lift_interned_pat catchall :: missing) |
| 754 |
| Extension {example} -> Some (CAst.make (PartialPat.Extension {example}) :: missing) |
| 755 |
| Known missing_ctors -> |
| 756 |
let misspats = List.map (fun ctor -> |
| 757 |
CAst.make (PatRef (ctor, List.make ctor.cnargs catchall))) |
| 758 |
missing_ctors |
| 759 |
in |
| 760 |
Some (lift_interned_pat (pat_or misspats) :: missing) |
| 761 |
|
| 762 |
and specialized_missing_matches env ts pats n = function |
| 763 |
| [] -> None |
| 764 |
| ctor :: rest -> |
| 765 |
match missing_matches env |
| 766 |
(specialized_types env ts (OtherCtor ctor)) |
| 767 |
(specialized_matrix pats (OtherCtor ctor)) |
| 768 |
(ctor.cnargs + n - 1) |
| 769 |
with |
| 770 |
| None -> specialized_missing_matches env ts pats n rest |
| 771 |
| Some missing -> |
| 772 |
let args, missing = List.chop ctor.cnargs missing in |
| 773 |
|
| 774 |
Some (CAst.make (PartialPat.Ref (ctor, args)) :: missing) |
| 775 |
|
| 776 |
let check_no_missing_pattern env t pats = |
| 777 |
match missing_matches env [t] (List.map (fun x -> [x]) pats) 1 with |
| 778 |
| None -> () |
| 779 |
| Some missing -> |
| 780 |
let missing = match missing with [x] -> x | _ -> assert false in |
| 781 |
CErrors.user_err Pp.( |
| 782 |
str "Non exhaustive match. Values in this pattern are not matched:" ++ fnl() ++ |
| 783 |
pr_partial_pat missing) |
| 784 |
|
| 785 |
type utility = |
| 786 |
| Useless |
| 787 |
| PartiallyUseless of Loc.t option list |
| 788 |
|
| 789 |
let combine_utilities us = |
| 790 |
let fold (all_useless, useless_locs) = function |
| 791 |
| _, None -> (false, useless_locs) |
| 792 |
| loc, Some Useless -> (all_useless, [loc]::useless_locs) |
| 793 |
| _, Some (PartiallyUseless locs) -> (false, locs::useless_locs) |
| 794 |
in |
| 795 |
let all_useless, useless_locs = List.fold_left fold (true,[]) us in |
| 796 |
if List.is_empty useless_locs then None |
| 797 |
else if all_useless then Some Useless |
| 798 |
else Some (PartiallyUseless (List.flatten (List.rev useless_locs))) |
| 799 |
|
| 800 |
let rec simple_utility env ts pats q = |
| 801 |
match q with |
| 802 |
| [] -> begin match pats with [] -> true | _::_ -> false end |
| 803 |
| pat :: q -> match pat.CAst.v with |
| 804 |
| PatAs (p, _) -> simple_utility env ts pats (p :: q) |
| 805 |
| PatRef (ctor, args) -> |
| 806 |
let ctor = OtherCtor ctor in |
| 807 |
simple_utility env (specialized_types env ts ctor) |
| 808 |
(specialized_matrix pats ctor) |
| 809 |
(args @ q) |
| 810 |
| PatAtm atm -> |
| 811 |
let ctor = AtomCtor atm in |
| 812 |
simple_utility env (specialized_types env ts ctor) |
| 813 |
(specialized_matrix pats ctor) |
| 814 |
q |
| 815 |
| PatOr ps -> List.exists (fun p -> simple_utility env ts pats (p :: q)) ps |
| 816 |
| PatVar _ -> |
| 817 |
let root_ctors = List.map_append root_ctors (List.map List.hd pats) in |
| 818 |
match missing_ctors_from env (List.hd ts) root_ctors with |
| 819 |
| NoMissing ctors -> |
| 820 |
List.exists (fun ctor -> |
| 821 |
let gctor = OtherCtor ctor in |
| 822 |
simple_utility env (specialized_types env ts gctor) |
| 823 |
(specialized_matrix pats gctor) |
| 824 |
(List.make ctor.cnargs catchall @ q)) |
| 825 |
ctors |
| 826 |
| Missing _ -> simple_utility env (List.tl ts) (default_matrix pats) q |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
let rec utility env ((tP, tQ, tR) as t) ((preP, preQ, preR) as prefix) (p, q, r) = |
| 832 |
match p with |
| 833 |
| p1 :: p -> begin match p1.CAst.v with |
| 834 |
| PatAs (p1, _) -> utility env t prefix (p1 :: p, q, r) |
| 835 |
| PatRef (ctor, pats) -> |
| 836 |
let ctor = OtherCtor ctor in |
| 837 |
let t = specialized_types env tP ctor, tQ, tR in |
| 838 |
let prefix = specialized_multi_matrix prefix ctor in |
| 839 |
utility env t prefix (pats @ p, q, r) |
| 840 |
| PatAtm atm -> |
| 841 |
let ctor = AtomCtor atm in |
| 842 |
let t = specialized_types env tP ctor, tQ, tR in |
| 843 |
let prefix = specialized_multi_matrix prefix ctor in |
| 844 |
utility env t prefix (p, q, r) |
| 845 |
| PatVar _ -> |
| 846 |
let t = (List.tl tP, List.hd tP :: tQ, tR) in |
| 847 |
let prefix = |
| 848 |
(List.map List.tl preP, |
| 849 |
List.map2 (fun preP preQ -> List.hd preP :: preQ) preP preQ, |
| 850 |
preR) |
| 851 |
in |
| 852 |
utility env t prefix (p, p1 :: q, r) |
| 853 |
| PatOr _ -> |
| 854 |
let t = (List.tl tP, tQ, List.hd tP :: tR) in |
| 855 |
let prefix = |
| 856 |
(List.map List.tl preP, |
| 857 |
preQ, |
| 858 |
List.map2 (fun preP preR -> List.hd preP :: preR) preP preR) |
| 859 |
in |
| 860 |
utility env t prefix (p, q, p1 :: r) |
| 861 |
end |
| 862 |
| [] -> match r with |
| 863 |
| [] -> if simple_utility env tQ preQ q then None else Some Useless |
| 864 |
| _ :: _ -> |
| 865 |
let utilities = List.map_i (fun j rj -> |
| 866 |
let t = ([List.nth tR j], (List.filteri (fun j' _ -> not (Int.equal j j')) tR) @ tQ, []) in |
| 867 |
let r_no_j = List.filteri (fun j' _ -> not (Int.equal j j')) r in |
| 868 |
let preRj = List.map (fun x -> [List.nth x j]) preR in |
| 869 |
let preR_no_j = List.map (fun x -> List.filteri (fun j' _ -> not (Int.equal j j')) x) preR in |
| 870 |
let r_no_j_plus_q = r_no_j @ q in |
| 871 |
let pats = match rj.v with |
| 872 |
| PatOr pats -> pats |
| 873 |
| _ -> assert false |
| 874 |
in |
| 875 |
let fold ((preP, preQ, preR) as prefix) pat = |
| 876 |
let u = utility env t prefix ([pat], r_no_j_plus_q, []) in |
| 877 |
|
| 878 |
let prefix = (preP @ [[pat]], preQ @ [r_no_j_plus_q], [] :: preR) in |
| 879 |
prefix, (pat.loc, u) |
| 880 |
in |
| 881 |
let prefix = (preRj, List.map2 (@) preR_no_j preQ, List.make (List.length preRj) []) in |
| 882 |
let _, us = List.fold_left_map fold prefix pats in |
| 883 |
rj.loc, combine_utilities us) |
| 884 |
0 r |
| 885 |
in |
| 886 |
combine_utilities utilities |
| 887 |
|
| 888 |
let warn_redundant_pattern = |
| 889 |
CWarnings.create ~name:"redundant-pattern" ~category:CWarnings.CoreCategories.ltac2 |
| 890 |
(fun partial -> str ("This " ^ (if partial then "pattern" else "clause") ^ " is redundant.")) |
| 891 |
|
| 892 |
let check_redundant_clauses env t pats = |
| 893 |
let fold (prefix, dummies) pat = |
| 894 |
let () = match utility env ([t],[],[]) (prefix,dummies,dummies) ([pat],[],[]) with |
| 895 |
| None -> () |
| 896 |
| Some Useless -> warn_redundant_pattern ?loc:pat.loc false |
| 897 |
| Some (PartiallyUseless locs) -> List.iter (fun loc -> warn_redundant_pattern ?loc true) locs |
| 898 |
in |
| 899 |
prefix @ [[pat]], [] :: dummies |
| 900 |
in |
| 901 |
let _, _ = List.fold_left fold ([],[]) pats in |
| 902 |
() |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
type glb_patexpr = |
| 907 |
| GEPatVar of Name.t |
| 908 |
| GEPatRef of ctor_data_for_patterns * glb_patexpr list |
| 909 |
|
| 910 |
exception HardCase |
| 911 |
|
| 912 |
let rec to_patexpr env {loc;v=pat} = match pat with |
| 913 |
| PatVar na -> GEPatVar na |
| 914 |
| PatRef (ctor, pl) -> |
| 915 |
GEPatRef (ctor, List.map (fun p -> to_patexpr env p) pl) |
| 916 |
| PatAtm _ | PatOr _ | PatAs _ -> |
| 917 |
raise HardCase |
| 918 |
|
| 919 |
type pattern_kind = |
| 920 |
| PKind_empty |
| 921 |
| PKind_variant of type_constant or_tuple |
| 922 |
| PKind_open |
| 923 |
| PKind_any |
| 924 |
|
| 925 |
let get_pattern_kind env pl = match pl with |
| 926 |
| [] -> PKind_empty |
| 927 |
| p :: pl -> |
| 928 |
let rec get_kind ((p:wip_pat), _) pl = match to_patexpr env p with |
| 929 |
| GEPatVar _ -> |
| 930 |
begin match pl with |
| 931 |
| [] -> PKind_any |
| 932 |
| p :: pl -> get_kind p pl |
| 933 |
end |
| 934 |
| GEPatRef ({ctyp=Some ctyp} as kn, pl) -> begin match kn.cindx with |
| 935 |
| Open kn -> PKind_open |
| 936 |
| Closed _ -> PKind_variant (Other ctyp) |
| 937 |
end |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
| GEPatRef ({ctyp=None; cnargs=k}, tp) -> PKind_variant (Tuple k) |
| 942 |
in |
| 943 |
get_kind p pl |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
let to_simple_case env ?loc (e,t) pl = |
| 948 |
let todo () = raise HardCase in |
| 949 |
match get_pattern_kind env pl with |
| 950 |
| PKind_any -> |
| 951 |
let (pat, b) = List.hd pl in |
| 952 |
let na = match to_patexpr env pat with |
| 953 |
| GEPatVar na -> na |
| 954 |
| _ -> assert false |
| 955 |
in |
| 956 |
GTacLet (false, [na, e], b) |
| 957 |
| PKind_empty -> |
| 958 |
let kn = check_elt_empty loc env t in |
| 959 |
GTacCse (e, Other kn, [||], [||]) |
| 960 |
| PKind_variant kn -> |
| 961 |
let (nconst, nnonconst, arities) = match kn with |
| 962 |
| Tuple 0 -> 1, 0, [0] |
| 963 |
| Tuple n -> 0, 1, [n] |
| 964 |
| Other kn -> |
| 965 |
let (_, def) = Tac2env.interp_type kn in |
| 966 |
let galg = match def with |
| 967 |
| GTydAlg c -> c |
| 968 |
| GTydRec _ -> raise HardCase |
| 969 |
| _ -> assert false |
| 970 |
in |
| 971 |
let arities = List.map (fun (_, _, args) -> List.length args) galg.galg_constructors in |
| 972 |
galg.galg_nconst, galg.galg_nnonconst, arities |
| 973 |
in |
| 974 |
let const = Array.make nconst None in |
| 975 |
let nonconst = Array.make nnonconst None in |
| 976 |
let rec intern_branch = function |
| 977 |
| [] -> () |
| 978 |
| (pat, br) :: rem -> |
| 979 |
let () = match pat.v with |
| 980 |
| PatAtm _ | PatOr _ | PatAs _ -> |
| 981 |
raise HardCase |
| 982 |
| PatVar (Name _) -> todo () |
| 983 |
| PatVar Anonymous -> |
| 984 |
|
| 985 |
let fill (ncst, narg) arity = |
| 986 |
if Int.equal arity 0 then |
| 987 |
let () = |
| 988 |
if Option.is_empty const.(ncst) then const.(ncst) <- Some br |
| 989 |
in |
| 990 |
(succ ncst, narg) |
| 991 |
else |
| 992 |
let () = |
| 993 |
if Option.is_empty nonconst.(narg) then |
| 994 |
let ids = Array.make arity Anonymous in |
| 995 |
nonconst.(narg) <- Some (ids, br) |
| 996 |
in |
| 997 |
(ncst, succ narg) |
| 998 |
in |
| 999 |
let _, _ = List.fold_left fill (0, 0) arities in |
| 1000 |
() |
| 1001 |
| PatRef (ctor, args) -> |
| 1002 |
let index = match ctor.cindx with |
| 1003 |
| Closed i -> i |
| 1004 |
| Open _ -> assert false |
| 1005 |
in |
| 1006 |
let get_id pat = match pat.v with |
| 1007 |
| PatVar na -> na |
| 1008 |
| _ -> todo () |
| 1009 |
in |
| 1010 |
let ids = List.map get_id args in |
| 1011 |
let () = |
| 1012 |
if List.is_empty args then |
| 1013 |
if Option.is_empty const.(index) then const.(index) <- Some br |
| 1014 |
else () |
| 1015 |
else |
| 1016 |
let ids = Array.of_list ids in |
| 1017 |
if Option.is_empty nonconst.(index) then nonconst.(index) <- Some (ids, br) |
| 1018 |
else () |
| 1019 |
in |
| 1020 |
() |
| 1021 |
in |
| 1022 |
intern_branch rem |
| 1023 |
in |
| 1024 |
let () = intern_branch pl in |
| 1025 |
let map n is_const = function |
| 1026 |
| None -> assert false |
| 1027 |
| Some x -> x |
| 1028 |
in |
| 1029 |
let const = Array.mapi (fun i o -> map i true o) const in |
| 1030 |
let nonconst = Array.mapi (fun i o -> map i false o) nonconst in |
| 1031 |
GTacCse (e, kn, const, nonconst) |
| 1032 |
| PKind_open -> |
| 1033 |
let rec intern_branch map = function |
| 1034 |
| [] -> |
| 1035 |
user_err ?loc (str "Missing default case") |
| 1036 |
| (pat, br) :: rem -> |
| 1037 |
match to_patexpr env pat with |
| 1038 |
| GEPatVar na -> |
| 1039 |
let def = (na, br) in |
| 1040 |
(map, def) |
| 1041 |
| GEPatRef (knc, args) -> |
| 1042 |
let get = function |
| 1043 |
| GEPatVar na -> na |
| 1044 |
| GEPatRef _ -> todo () |
| 1045 |
in |
| 1046 |
let knc = match knc.cindx with |
| 1047 |
| Open knc -> knc |
| 1048 |
| Closed _ -> assert false |
| 1049 |
in |
| 1050 |
let ids = List.map get args in |
| 1051 |
let map = |
| 1052 |
if KerName.Map.mem knc map then |
| 1053 |
map |
| 1054 |
else |
| 1055 |
KerName.Map.add knc (Anonymous, Array.of_list ids, br) map |
| 1056 |
in |
| 1057 |
intern_branch map rem |
| 1058 |
in |
| 1059 |
let (map, def) = intern_branch KerName.Map.empty pl in |
| 1060 |
GTacWth { opn_match = e; opn_branch = map; opn_default = def } |
| 1061 |
|
| 1062 |
let check ?loc env tycon (e,t as et) = |
| 1063 |
match tycon with |
| 1064 |
| None -> et |
| 1065 |
| Some tycon -> |
| 1066 |
let () = unify ?loc env t tycon in |
| 1067 |
e,tycon |
| 1068 |
|
| 1069 |
let tycon_fun_body ?loc env tycon dom = |
| 1070 |
match kind env tycon with |
| 1071 |
| GTypVar _ -> |
| 1072 |
let codom = GTypVar (fresh_id env) in |
| 1073 |
let () = unify ?loc env (GTypArrow (dom,codom)) tycon in |
| 1074 |
codom |
| 1075 |
| GTypArrow (dom',codom) -> |
| 1076 |
let () = unify ?loc env (GTypArrow (dom,codom)) tycon in |
| 1077 |
codom |
| 1078 |
| GTypRef _ -> |
| 1079 |
CErrors.user_err ?loc |
| 1080 |
Pp.(str "This expression should not be a function, the expected type is" ++ spc() ++ |
| 1081 |
pr_glbtype env tycon ++ str ".") |
| 1082 |
|
| 1083 |
let tycon_app ?loc env ~ft t = |
| 1084 |
match kind env t with |
| 1085 |
| GTypVar _ -> |
| 1086 |
let dom = GTypVar (fresh_id env) in |
| 1087 |
let codom = GTypVar (fresh_id env) in |
| 1088 |
let () = unify ?loc env (GTypArrow (dom,codom)) t in |
| 1089 |
dom, codom |
| 1090 |
| GTypArrow (dom,codom) -> dom, codom |
| 1091 |
| GTypRef _ -> |
| 1092 |
let is_fun = match kind env ft with |
| 1093 |
| GTypArrow _ -> true |
| 1094 |
| _ -> false |
| 1095 |
in |
| 1096 |
if is_fun then |
| 1097 |
CErrors.user_err ?loc |
| 1098 |
Pp.(str "This function has type" ++ spc() ++ pr_glbtype env ft ++ |
| 1099 |
spc() ++ str "and is applied to too many arguments.") |
| 1100 |
else |
| 1101 |
CErrors.user_err ?loc |
| 1102 |
Pp.(str "This expression has type" ++ spc() ++ pr_glbtype env ft ++ str"." ++ |
| 1103 |
spc() ++ str "It is not a function and cannot be applied.") |
| 1104 |
|
| 1105 |
let warn_useless_record_with = CWarnings.create ~name:"ltac2-useless-record-with" ~default:AsError |
| 1106 |
~category:CWarnings.CoreCategories.ltac2 |
| 1107 |
Pp.(fun () -> |
| 1108 |
str "All the fields are explicitly listed in this record:" ++ |
| 1109 |
spc() ++ str "the 'with' clause is useless.") |
| 1110 |
|
| 1111 |
let expand_notation ?loc el kn = |
| 1112 |
match Tac2env.interp_notation kn with |
| 1113 |
| UntypedNota body -> |
| 1114 |
let el = List.map (fun (pat, e) -> CAst.map (fun na -> CPatVar na) pat, e) el in |
| 1115 |
let v = if CList.is_empty el then body else CAst.make ?loc @@ CTacLet(false, el, body) in |
| 1116 |
v |
| 1117 |
| TypedNota {nota_prms=prms; nota_argtys=argtys; nota_ty=ty; nota_body=body} -> |
| 1118 |
let argtys, el = List.fold_left_map (fun argtys (na,arg) -> |
| 1119 |
let argty, argtys = match na.CAst.v with |
| 1120 |
| Anonymous -> None, argtys |
| 1121 |
| Name id -> Some (Id.Map.get id argtys), Id.Map.remove id argtys |
| 1122 |
in |
| 1123 |
argtys ,(na, arg, argty)) |
| 1124 |
argtys |
| 1125 |
el |
| 1126 |
in |
| 1127 |
assert (Id.Map.is_empty argtys); |
| 1128 |
CAst.make ?loc @@ CTacGlb (prms, el, body, ty) |
| 1129 |
|
| 1130 |
let warn_unused_variables = CWarnings.create ~name:"ltac2-unused-variable" |
| 1131 |
~category:CWarnings.CoreCategories.ltac2 |
| 1132 |
Pp.(fun ids -> str "Unused " ++ str (String.lplural ids "variable") ++ str ":" ++ spc() ++ prlist_with_sep spc Id.print ids ++ str ".") |
| 1133 |
|
| 1134 |
let list_unused_variables env bnd = |
| 1135 |
let unused, _seen = |
| 1136 |
List.fold_right (fun na (unused,seen) -> |
| 1137 |
match na with |
| 1138 |
| Anonymous -> (unused, seen) |
| 1139 |
| Name id -> |
| 1140 |
|
| 1141 |
|
| 1142 |
let unused = |
| 1143 |
if CString.is_prefix "_" (Id.to_string id) |
| 1144 |
|| (not (Id.Set.mem id seen) && is_used_var id env) |
| 1145 |
then unused |
| 1146 |
else id :: unused |
| 1147 |
in |
| 1148 |
unused, (Id.Set.add id seen)) |
| 1149 |
bnd |
| 1150 |
([], Id.Set.empty) |
| 1151 |
in |
| 1152 |
unused |
| 1153 |
|
| 1154 |
let check_unused_variables ?loc env bnd = |
| 1155 |
let unused = list_unused_variables env bnd in |
| 1156 |
if CList.is_empty unused then () |
| 1157 |
else warn_unused_variables ?loc unused |
| 1158 |
|
| 1159 |
let rec intern_rec env tycon {loc;v=e} = |
| 1160 |
let check et = check ?loc env tycon et in |
| 1161 |
match e with |
| 1162 |
| CTacAtm atm -> check (intern_atm env atm) |
| 1163 |
| CTacRef qid -> |
| 1164 |
begin match get_variable env qid with |
| 1165 |
| ArgVar {CAst.v=id} -> |
| 1166 |
let sch = find_var id env in |
| 1167 |
check (GTacVar id, fresh_mix_type_scheme env sch) |
| 1168 |
| ArgArg (TacConstant kn) -> |
| 1169 |
let { Tac2env.gdata_type = sch; gdata_deprecation = depr } = |
| 1170 |
try Tac2env.interp_global kn |
| 1171 |
with Not_found -> |
| 1172 |
CErrors.anomaly (str "Missing hardwired primitive " ++ KerName.print kn) |
| 1173 |
in |
| 1174 |
let () = check_deprecated_ltac2 ?loc qid (TacConstant kn) in |
| 1175 |
check (GTacRef kn, fresh_type_scheme env sch) |
| 1176 |
| ArgArg (TacAlias kn) -> |
| 1177 |
let e = |
| 1178 |
try Tac2env.interp_alias kn |
| 1179 |
with Not_found -> |
| 1180 |
CErrors.anomaly (str "Missing hardwired alias " ++ KerName.print kn) |
| 1181 |
in |
| 1182 |
let () = check_deprecated_ltac2 ?loc qid (TacAlias kn) in |
| 1183 |
intern_rec env tycon e.alias_body |
| 1184 |
end |
| 1185 |
| CTacCst qid -> |
| 1186 |
let kn = get_constructor env qid in |
| 1187 |
intern_constructor env loc tycon kn [] |
| 1188 |
| CTacFun (bnd, e) -> |
| 1189 |
let bnd = List.map extract_pattern_type bnd in |
| 1190 |
let map (_, t) = match t with |
| 1191 |
| None -> GTypVar (fresh_id env) |
| 1192 |
| Some t -> intern_type env t |
| 1193 |
in |
| 1194 |
let tl = List.map map bnd in |
| 1195 |
let (nas, exp) = expand_pattern (bound_vars env) bnd in |
| 1196 |
let env, tycon = List.fold_left2 (fun (env,tycon) na t -> |
| 1197 |
let tycon = Option.map (fun tycon -> tycon_fun_body ?loc env tycon t) tycon in |
| 1198 |
let env = push_name na (monomorphic t) env in |
| 1199 |
env, tycon) (env,tycon) nas tl |
| 1200 |
in |
| 1201 |
let (e, t) = intern_rec env tycon (exp e) in |
| 1202 |
let () = |
| 1203 |
|
| 1204 |
check_unused_variables ?loc env nas |
| 1205 |
in |
| 1206 |
let t = match tycon with |
| 1207 |
| None -> List.fold_right (fun t accu -> GTypArrow (t, accu)) tl t |
| 1208 |
| Some tycon -> tycon |
| 1209 |
in |
| 1210 |
(GTacFun (nas, e), t) |
| 1211 |
| CTacApp ({loc;v=CTacCst qid}, args) -> |
| 1212 |
let kn = get_constructor env qid in |
| 1213 |
intern_constructor env loc tycon kn args |
| 1214 |
| CTacApp ({v=CTacRef qid; loc=aloc}, args) when is_alias env qid -> |
| 1215 |
let kn = match get_variable env qid with |
| 1216 |
| ArgArg (TacAlias kn) -> kn |
| 1217 |
| ArgVar _ | (ArgArg (TacConstant _)) -> assert false |
| 1218 |
in |
| 1219 |
let e = Tac2env.interp_alias kn in |
| 1220 |
let () = check_deprecated_ltac2 ?loc:aloc qid (TacAlias kn) in |
| 1221 |
let map arg = |
| 1222 |
|
| 1223 |
let loc = arg.loc in |
| 1224 |
let t_unit = CAst.make ?loc @@ CTypRef (AbsKn (Tuple 0), []) in |
| 1225 |
let var = CAst.make ?loc @@ CPatCnv (CAst.make ?loc @@ CPatVar Anonymous, t_unit) in |
| 1226 |
CAst.make ?loc @@ CTacFun ([var], arg) |
| 1227 |
in |
| 1228 |
let args = List.map map args in |
| 1229 |
intern_rec env tycon (CAst.make ?loc @@ CTacApp (e.alias_body, args)) |
| 1230 |
| CTacApp (f, args) -> |
| 1231 |
let loc = f.loc in |
| 1232 |
let (f, ft) = intern_rec env None f in |
| 1233 |
let fold t arg = |
| 1234 |
let dom, codom = tycon_app ?loc env ~ft t in |
| 1235 |
let arg = intern_rec_with_constraint env arg dom in |
| 1236 |
(codom, arg) |
| 1237 |
in |
| 1238 |
let (t, args) = CList.fold_left_map fold ft args in |
| 1239 |
check (GTacApp (f, args), t) |
| 1240 |
| CTacLet (is_rec, el, e) -> |
| 1241 |
let map (pat, e) = |
| 1242 |
let (pat, ty) = extract_pattern_type pat in |
| 1243 |
(pat, ty, e) |
| 1244 |
in |
| 1245 |
let el = List.map map el in |
| 1246 |
let fold accu (pat, _, e) = |
| 1247 |
let ids = ids_of_pattern Id.Set.empty pat in |
| 1248 |
let common = Id.Set.inter ids accu in |
| 1249 |
if Id.Set.is_empty common then Id.Set.union ids accu |
| 1250 |
else |
| 1251 |
let id = Id.Set.choose common in |
| 1252 |
user_err ?loc:pat.loc (str "Variable " ++ Id.print id ++ str " is bound several \ |
| 1253 |
times in this matching") |
| 1254 |
in |
| 1255 |
let ids = List.fold_left fold Id.Set.empty el in |
| 1256 |
if is_rec then intern_let_rec env loc el tycon e |
| 1257 |
else intern_let env loc ids el tycon e |
| 1258 |
| CTacSyn (el, kn) -> |
| 1259 |
let v = expand_notation ?loc el kn in |
| 1260 |
intern_rec env tycon v |
| 1261 |
| CTacCnv (e, tc) -> |
| 1262 |
let tc = intern_type env tc in |
| 1263 |
let e = intern_rec_with_constraint env e tc in |
| 1264 |
check (e, tc) |
| 1265 |
| CTacSeq (e1, e2) -> |
| 1266 |
let loc1 = e1.loc in |
| 1267 |
let (e1, t1) = intern_rec env None e1 in |
| 1268 |
let (e2, t2) = intern_rec env tycon e2 in |
| 1269 |
let () = check_elt_unit loc1 env t1 in |
| 1270 |
(GTacLet (false, [Anonymous, e1], e2), t2) |
| 1271 |
| CTacIft (e, e1, e2) -> |
| 1272 |
let e = intern_rec_with_constraint env e (GTypRef (Other t_bool, [])) in |
| 1273 |
let (e1, t1) = intern_rec env tycon e1 in |
| 1274 |
let t = Option.default t1 tycon in |
| 1275 |
let e2 = intern_rec_with_constraint env e2 t in |
| 1276 |
(GTacCse (e, Other t_bool, [|e1; e2|], [||]), t) |
| 1277 |
| CTacCse (e, pl) -> |
| 1278 |
let e,brs,rt = intern_case env loc e tycon pl in |
| 1279 |
begin try |
| 1280 |
let cse = to_simple_case env ?loc e brs in |
| 1281 |
cse, rt |
| 1282 |
with HardCase -> |
| 1283 |
let e, _ = e in |
| 1284 |
let brs = List.map (fun (p,br) -> glb_of_wip_pat p, br) brs in |
| 1285 |
GTacFullMatch (e,brs), rt |
| 1286 |
end |
| 1287 |
| CTacRec (def, fs) -> |
| 1288 |
let kn, tparam, args = intern_record env loc fs in |
| 1289 |
let def, args = match def with |
| 1290 |
| None -> |
| 1291 |
let args = Array.map (function |
| 1292 |
| PresentField _ as arg -> arg |
| 1293 |
| MissingField (_, field, _, _) -> user_err ?loc (str "Field " ++ Id.print field ++ str " is undefined")) |
| 1294 |
args |
| 1295 |
in |
| 1296 |
None, args |
| 1297 |
| Some def -> |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
let deftparam = Array.init (List.length tparam) (fun _ -> GTypVar (fresh_id env)) in |
| 1304 |
let used = ref false in |
| 1305 |
let args = Array.map (function |
| 1306 |
| PresentField _ as arg -> arg |
| 1307 |
| MissingField (i, _, ftyp, expectedtyp) -> |
| 1308 |
used := true; |
| 1309 |
let deftyp = subst_type (fun i -> deftparam.(i)) ftyp in |
| 1310 |
|
| 1311 |
let () = unify ?loc env deftyp expectedtyp in |
| 1312 |
MissingField i) |
| 1313 |
args |
| 1314 |
in |
| 1315 |
let () = if not !used then warn_useless_record_with ?loc (); in |
| 1316 |
let def = intern_rec_with_constraint env def (GTypRef (Other kn, Array.to_list deftparam)) in |
| 1317 |
let var = match def with |
| 1318 |
| GTacVar _ | GTacRef _ -> None |
| 1319 |
| _ -> Some (fresh_var (bound_vars env)) |
| 1320 |
in |
| 1321 |
Some (var, def), args |
| 1322 |
in |
| 1323 |
let args = CArray.map_to_list (function |
| 1324 |
| PresentField (arg,argty) -> intern_rec_with_constraint env arg argty |
| 1325 |
| MissingField i -> match def with |
| 1326 |
| None -> assert false |
| 1327 |
| Some (None, def) -> GTacPrj (kn, def, i) |
| 1328 |
| Some (Some var, _) -> GTacPrj (kn, GTacVar var, i)) |
| 1329 |
args |
| 1330 |
in |
| 1331 |
let e = GTacCst (Other kn, 0, args) in |
| 1332 |
let e = match def with |
| 1333 |
| None -> e |
| 1334 |
| Some (None, _) -> e |
| 1335 |
| Some (Some var, def) -> |
| 1336 |
GTacLet (false, [Name var, def], e) |
| 1337 |
in |
| 1338 |
check (e, GTypRef (Other kn, tparam)) |
| 1339 |
| CTacPrj (e, proj) -> |
| 1340 |
let pinfo = get_projection proj in |
| 1341 |
let loc = e.loc in |
| 1342 |
let (e, t) = intern_rec env None e in |
| 1343 |
let subst = Array.init pinfo.pdata_prms (fun _ -> fresh_id env) in |
| 1344 |
let params = Array.map_to_list (fun i -> GTypVar i) subst in |
| 1345 |
let exp = GTypRef (Other pinfo.pdata_type, params) in |
| 1346 |
let () = unify ?loc env t exp in |
| 1347 |
let substf i = GTypVar subst.(i) in |
| 1348 |
let ret = subst_type substf pinfo.pdata_ptyp in |
| 1349 |
check (GTacPrj (pinfo.pdata_type, e, pinfo.pdata_indx), ret) |
| 1350 |
| CTacSet (e, proj, r) -> |
| 1351 |
let pinfo = get_projection proj in |
| 1352 |
let () = |
| 1353 |
if not pinfo.pdata_mutb then |
| 1354 |
let loc = match proj with |
| 1355 |
| RelId {CAst.loc} -> loc |
| 1356 |
| AbsKn _ -> None |
| 1357 |
in |
| 1358 |
user_err ?loc (str "Field is not mutable") |
| 1359 |
in |
| 1360 |
let subst = Array.init pinfo.pdata_prms (fun _ -> fresh_id env) in |
| 1361 |
let params = Array.map_to_list (fun i -> GTypVar i) subst in |
| 1362 |
let exp = GTypRef (Other pinfo.pdata_type, params) in |
| 1363 |
let e = intern_rec_with_constraint env e exp in |
| 1364 |
let substf i = GTypVar subst.(i) in |
| 1365 |
let ret = subst_type substf pinfo.pdata_ptyp in |
| 1366 |
let r = intern_rec_with_constraint env r ret in |
| 1367 |
check (GTacSet (pinfo.pdata_type, e, pinfo.pdata_indx, r), GTypRef (Tuple 0, [])) |
| 1368 |
| CTacExt (tag, arg) -> |
| 1369 |
let open Genintern in |
| 1370 |
let obj = interp_ml_object tag in |
| 1371 |
|
| 1372 |
|
| 1373 |
let genv = Global.env_of_context Environ.empty_named_context_val in |
| 1374 |
let ist = empty_glob_sign ~strict:(env_strict env) genv in |
| 1375 |
let ist = { ist with extra = Store.set ist.extra ltac2_env env } in |
| 1376 |
let arg, tpe = obj.ml_intern ist arg in |
| 1377 |
let e = match arg with |
| 1378 |
| GlbVal arg -> GTacExt (tag, arg) |
| 1379 |
| GlbTacexpr e -> e |
| 1380 |
in |
| 1381 |
check (e, tpe) |
| 1382 |
| CTacGlb (prms, args, body, ty) -> |
| 1383 |
let tysubst = Array.init prms (fun _ -> fresh_id env) in |
| 1384 |
let tysubst i = GTypVar tysubst.(i) in |
| 1385 |
let ty = subst_type tysubst ty in |
| 1386 |
let ty = match tycon with |
| 1387 |
| None -> ty |
| 1388 |
| Some tycon -> |
| 1389 |
let () = unify ?loc env ty tycon in |
| 1390 |
tycon |
| 1391 |
in |
| 1392 |
let args = List.map (fun (na, arg, ty) -> |
| 1393 |
let ty = Option.map (subst_type tysubst) ty in |
| 1394 |
let () = match na.CAst.v, ty with |
| 1395 |
| Anonymous, None | Name _, Some _ -> () |
| 1396 |
| Anonymous, Some _ | Name _, None -> assert false |
| 1397 |
in |
| 1398 |
let e, _ = intern_rec env ty arg in |
| 1399 |
na.CAst.v, e) |
| 1400 |
args |
| 1401 |
in |
| 1402 |
if CList.is_empty args then body, ty |
| 1403 |
else GTacLet (false, args, body), ty |
| 1404 |
|
| 1405 |
and intern_rec_with_constraint env e exp = |
| 1406 |
let (er, t) = intern_rec env (Some exp) e in |
| 1407 |
er |
| 1408 |
|
| 1409 |
and intern_let env loc ids el tycon e = |
| 1410 |
let avoid = Id.Set.union ids (bound_vars env) in |
| 1411 |
let fold avoid (pat, t, e) = |
| 1412 |
let nas, exp = expand_pattern avoid [pat, t] in |
| 1413 |
let na = match nas with [x] -> x | _ -> assert false in |
| 1414 |
let avoid = List.fold_left add_name avoid nas in |
| 1415 |
(avoid, (na, exp, t, e)) |
| 1416 |
in |
| 1417 |
let (_, el) = List.fold_left_map fold avoid el in |
| 1418 |
let fold body (na, exp, tc, e) = |
| 1419 |
let tc = Option.map (intern_type env) tc in |
| 1420 |
let (e, t) = intern_rec env tc e in |
| 1421 |
let t = if Option.is_empty (check_value e) then abstract_var env t else monomorphic t in |
| 1422 |
(exp body, (na, e, t)) |
| 1423 |
in |
| 1424 |
let (e, elp) = List.fold_left_map fold e el in |
| 1425 |
let env = List.fold_left (fun accu (na, _, t) -> push_name na t accu) env elp in |
| 1426 |
let (e, t) = intern_rec env tycon e in |
| 1427 |
let () = check_unused_variables ?loc env (List.map pi1 elp) in |
| 1428 |
let el = List.map (fun (na, e, _) -> na, e) elp in |
| 1429 |
(GTacLet (false, el, e), t) |
| 1430 |
|
| 1431 |
and intern_let_rec env loc el tycon e = |
| 1432 |
let map env (pat, t, e) = |
| 1433 |
let na = match pat.v with |
| 1434 |
| CPatVar na -> na |
| 1435 |
| CPatAtm _ | CPatRef _ | CPatCnv _ | CPatOr _ | CPatAs _ | CPatRecord _ -> |
| 1436 |
user_err ?loc:pat.loc (str "This kind of pattern is forbidden in let-rec bindings") |
| 1437 |
in |
| 1438 |
let t = match t with |
| 1439 |
| None -> GTypVar (fresh_id env) |
| 1440 |
| Some t -> intern_type env t |
| 1441 |
in |
| 1442 |
let env = push_name na (monomorphic t) env in |
| 1443 |
(env, (na, t, e)) |
| 1444 |
in |
| 1445 |
let (env, el) = List.fold_left_map map env el in |
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
let map (na, t, e) = match e.v with |
| 1453 |
| CTacCnv (e',t') -> |
| 1454 |
let t' = intern_type env t' in |
| 1455 |
let () = unify ?loc:e.loc env t' t in |
| 1456 |
na, t', e' |
| 1457 |
| CTacFun (bnd,e') -> |
| 1458 |
let bnd = List.map extract_pattern_type bnd in |
| 1459 |
let map (_, t) = match t with |
| 1460 |
| None -> GTypVar (fresh_id env) |
| 1461 |
| Some t -> intern_type env t |
| 1462 |
in |
| 1463 |
let tl = List.map map bnd in |
| 1464 |
let nas, exp = expand_pattern (bound_vars env) bnd in |
| 1465 |
let t = List.fold_left2 (fun t na tna -> tycon_fun_body ?loc:e.loc env t tna) t nas tl in |
| 1466 |
let e', t' = match e'.v with |
| 1467 |
| CTacCnv (e',t') -> |
| 1468 |
let t' = intern_type env t' in |
| 1469 |
let () = unify ?loc:e'.loc env t' t in |
| 1470 |
e', t' |
| 1471 |
| _ -> e', t |
| 1472 |
in |
| 1473 |
let t' = List.fold_right (fun tna t' -> GTypArrow (tna, t')) tl t' in |
| 1474 |
let pats = List.map (fun na -> CAst.make (CPatVar na)) nas in |
| 1475 |
let e' = exp e' in |
| 1476 |
(na, t', CAst.make ?loc:e.loc (CTacFun (pats, e'))) |
| 1477 |
| _ -> (na, t, e) |
| 1478 |
in |
| 1479 |
let el = List.map map el in |
| 1480 |
let map (na, tc, e) = |
| 1481 |
let loc_e = e.loc in |
| 1482 |
let e = intern_rec_with_constraint env e tc in |
| 1483 |
let () = |
| 1484 |
if not (is_rec_rhs e) then |
| 1485 |
user_err ?loc:loc_e (str "This kind of expression is not allowed as \ |
| 1486 |
right-hand side of a recursive binding") |
| 1487 |
in |
| 1488 |
(na, e) |
| 1489 |
in |
| 1490 |
let el = List.map map el in |
| 1491 |
let (e, t) = intern_rec env tycon e in |
| 1492 |
let () = |
| 1493 |
|
| 1494 |
check_unused_variables ?loc env (List.map fst el) |
| 1495 |
in |
| 1496 |
(GTacLet (true, el, e), t) |
| 1497 |
|
| 1498 |
and intern_constructor env loc tycon kn args = match kn with |
| 1499 |
| Other kn -> |
| 1500 |
let cstr = interp_constructor kn in |
| 1501 |
let nargs = List.length cstr.cdata_args in |
| 1502 |
if Int.equal nargs (List.length args) then |
| 1503 |
let subst = Array.init cstr.cdata_prms (fun _ -> fresh_id env) in |
| 1504 |
let substf i = GTypVar subst.(i) in |
| 1505 |
let types = List.map (fun t -> subst_type substf t) cstr.cdata_args in |
| 1506 |
let targs = List.init cstr.cdata_prms (fun i -> GTypVar subst.(i)) in |
| 1507 |
let ans = GTypRef (Other cstr.cdata_type, targs) in |
| 1508 |
let ans = match tycon with |
| 1509 |
| None -> ans |
| 1510 |
| Some tycon -> |
| 1511 |
let () = unify ?loc env ans tycon in |
| 1512 |
tycon |
| 1513 |
in |
| 1514 |
let map arg tpe = intern_rec_with_constraint env arg tpe in |
| 1515 |
let args = List.map2 map args types in |
| 1516 |
match cstr.cdata_indx with |
| 1517 |
| Some idx -> |
| 1518 |
(GTacCst (Other cstr.cdata_type, idx, args), ans) |
| 1519 |
| None -> |
| 1520 |
(GTacOpn (kn, args), ans) |
| 1521 |
else |
| 1522 |
error_nargs_mismatch ?loc kn nargs (List.length args) |
| 1523 |
| Tuple n -> |
| 1524 |
let () = if not (Int.equal n (List.length args)) then begin |
| 1525 |
if Int.equal 0 n then |
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
let t = GTypRef (Tuple 0, []) in |
| 1530 |
CErrors.user_err ?loc Pp.( |
| 1531 |
str "This expression has type" ++ spc () ++ pr_glbtype env t ++ |
| 1532 |
spc () ++ str "and is not a function") |
| 1533 |
else assert false |
| 1534 |
end |
| 1535 |
in |
| 1536 |
let types = List.init n (fun i -> GTypVar (fresh_id env)) in |
| 1537 |
let ans = GTypRef (Tuple n, types) in |
| 1538 |
let ans = match tycon with |
| 1539 |
| None -> ans |
| 1540 |
| Some tycon -> |
| 1541 |
let () = unify ?loc env ans tycon in |
| 1542 |
tycon |
| 1543 |
in |
| 1544 |
let map arg tpe = intern_rec_with_constraint env arg tpe in |
| 1545 |
let args = List.map2 map args types in |
| 1546 |
GTacCst (Tuple n, 0, args), ans |
| 1547 |
|
| 1548 |
and intern_case env loc e tycon pl = |
| 1549 |
let e, et = intern_rec env None e in |
| 1550 |
let rt = match tycon with |
| 1551 |
| Some t -> t |
| 1552 |
| None -> GTypVar (fresh_id env) |
| 1553 |
in |
| 1554 |
let pl = List.map (fun (cpat,cbr) -> |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
let patvars, pat = intern_pat env cpat et in |
| 1560 |
let patenv = push_ids patvars env in |
| 1561 |
let br = intern_rec_with_constraint patenv cbr rt in |
| 1562 |
let () = check_unused_variables ?loc patenv |
| 1563 |
(List.map (fun (id,_) -> Name id) (Id.Map.bindings patvars)) |
| 1564 |
in |
| 1565 |
pat, br) |
| 1566 |
pl |
| 1567 |
in |
| 1568 |
let just_patterns = List.map fst pl in |
| 1569 |
let () = check_no_missing_pattern env et just_patterns in |
| 1570 |
let () = check_redundant_clauses env et just_patterns in |
| 1571 |
((e,et),pl,rt) |
| 1572 |
|
| 1573 |
type context = (Id.t * type_scheme) list |
| 1574 |
|
| 1575 |
let intern ~strict ctx e = |
| 1576 |
let env = empty_env ~strict () in |
| 1577 |
|
| 1578 |
let fold accu (id, t) = push_name (Name id) (polymorphic t) accu in |
| 1579 |
let env = List.fold_left fold env ctx in |
| 1580 |
let (e, t) = intern_rec env None e in |
| 1581 |
let count = ref 0 in |
| 1582 |
let vars = ref TVar.Map.empty in |
| 1583 |
let t = normalize env (count, vars) t in |
| 1584 |
(e, (!count, t)) |
| 1585 |
|
| 1586 |
let intern_typedef self (ids, t) : glb_quant_typedef = |
| 1587 |
let env = set_rec self (empty_env ()) in |
| 1588 |
|
| 1589 |
let map id = get_alias id env in |
| 1590 |
let ids = List.map map ids in |
| 1591 |
let count = ref (List.length ids) in |
| 1592 |
let vars = ref TVar.Map.empty in |
| 1593 |
let iter n id = vars := TVar.Map.add id (GTypVar n) !vars in |
| 1594 |
let () = List.iteri iter ids in |
| 1595 |
|
| 1596 |
let env = reject_unbound_tvar env in |
| 1597 |
let intern t = |
| 1598 |
let t = intern_type env t in |
| 1599 |
normalize env (count, vars) t |
| 1600 |
in |
| 1601 |
let count = !count in |
| 1602 |
match t with |
| 1603 |
| CTydDef None -> (count, GTydDef None) |
| 1604 |
| CTydDef (Some t) -> (count, GTydDef (Some (intern t))) |
| 1605 |
| CTydAlg constrs -> |
| 1606 |
let map (atts, c, t) = |
| 1607 |
let warn = Attributes.parse Attributes.user_warns atts in |
| 1608 |
let t = List.map intern t in |
| 1609 |
(warn, c.CAst.v, t) |
| 1610 |
in |
| 1611 |
let constrs = List.map map constrs in |
| 1612 |
let getn (const, nonconst) (_, c, args) = match args with |
| 1613 |
| [] -> (succ const, nonconst) |
| 1614 |
| _ :: _ -> (const, succ nonconst) |
| 1615 |
in |
| 1616 |
let nconst, nnonconst = List.fold_left getn (0, 0) constrs in |
| 1617 |
let galg = { |
| 1618 |
galg_constructors = constrs; |
| 1619 |
galg_nconst = nconst; |
| 1620 |
galg_nnonconst = nnonconst; |
| 1621 |
} in |
| 1622 |
(count, GTydAlg galg) |
| 1623 |
| CTydRec fields -> |
| 1624 |
let map (c, mut, t) = (c, mut, intern t) in |
| 1625 |
let fields = List.map map fields in |
| 1626 |
(count, GTydRec fields) |
| 1627 |
| CTydOpn -> (count, GTydOpn) |
| 1628 |
|
| 1629 |
let intern_open_type t = |
| 1630 |
let env = empty_env () in |
| 1631 |
let t = intern_type env t in |
| 1632 |
let count = ref 0 in |
| 1633 |
let vars = ref TVar.Map.empty in |
| 1634 |
let t = normalize env (count, vars) t in |
| 1635 |
(!count, t) |
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
let check_subtype t1 t2 = |
| 1640 |
let env = empty_env () in |
| 1641 |
let t1 = fresh_type_scheme env t1 in |
| 1642 |
|
| 1643 |
let rigid i = GTypRef (Tuple (i + 1), []) in |
| 1644 |
let (n, t2) = t2 in |
| 1645 |
let subst = Array.init n rigid in |
| 1646 |
let substf i = subst.(i) in |
| 1647 |
let t2 = subst_type substf t2 in |
| 1648 |
try unify0 env t1 t2; true with CannotUnify _ -> false |
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
let get_projection0 var = match var with |
| 1653 |
| RelId qid -> |
| 1654 |
let kn = try Tac2env.locate_projection qid with Not_found -> |
| 1655 |
user_err ?loc:qid.CAst.loc (pr_qualid qid ++ str " is not a projection") |
| 1656 |
in |
| 1657 |
kn |
| 1658 |
| AbsKn kn -> kn |
| 1659 |
|
| 1660 |
type raw_ext = RawExt : ('a, _) Tac2dyn.Arg.tag * 'a -> raw_ext |
| 1661 |
|
| 1662 |
let globalize_gen ~tacext ids tac = |
| 1663 |
let rec globalize ids ({loc;v=er} as e) = match er with |
| 1664 |
| CTacAtm _ -> e |
| 1665 |
| CTacRef ref -> |
| 1666 |
let mem id = Id.Set.mem id ids in |
| 1667 |
begin match get_variable0 mem ref with |
| 1668 |
| ArgVar _ -> e |
| 1669 |
| ArgArg kn -> |
| 1670 |
let () = check_deprecated_ltac2 ?loc ref kn in |
| 1671 |
CAst.make ?loc @@ CTacRef (AbsKn kn) |
| 1672 |
end |
| 1673 |
| CTacCst qid -> |
| 1674 |
let knc = get_constructor () qid in |
| 1675 |
CAst.make ?loc @@ CTacCst (AbsKn knc) |
| 1676 |
| CTacFun (bnd, e) -> |
| 1677 |
let fold (pats, accu) pat = |
| 1678 |
let accu = ids_of_pattern accu pat in |
| 1679 |
let pat = globalize_pattern ids pat in |
| 1680 |
(pat :: pats, accu) |
| 1681 |
in |
| 1682 |
let bnd, ids = List.fold_left fold ([], ids) bnd in |
| 1683 |
let bnd = List.rev bnd in |
| 1684 |
let e = globalize ids e in |
| 1685 |
CAst.make ?loc @@ CTacFun (bnd, e) |
| 1686 |
| CTacApp (e, el) -> |
| 1687 |
let e = globalize ids e in |
| 1688 |
let el = List.map (fun e -> globalize ids e) el in |
| 1689 |
CAst.make ?loc @@ CTacApp (e, el) |
| 1690 |
| CTacLet (isrec, bnd, e) -> |
| 1691 |
let fold accu (pat, _) = ids_of_pattern accu pat in |
| 1692 |
let ext = List.fold_left fold Id.Set.empty bnd in |
| 1693 |
let eids = Id.Set.union ext ids in |
| 1694 |
let e = globalize eids e in |
| 1695 |
let map (qid, e) = |
| 1696 |
let ids = if isrec then eids else ids in |
| 1697 |
let qid = globalize_pattern ids qid in |
| 1698 |
(qid, globalize ids e) |
| 1699 |
in |
| 1700 |
let bnd = List.map map bnd in |
| 1701 |
CAst.make ?loc @@ CTacLet (isrec, bnd, e) |
| 1702 |
| CTacSyn (el, kn) -> |
| 1703 |
let v = expand_notation ?loc el kn in |
| 1704 |
globalize ids v |
| 1705 |
| CTacCnv (e, t) -> |
| 1706 |
let e = globalize ids e in |
| 1707 |
CAst.make ?loc @@ CTacCnv (e, t) |
| 1708 |
| CTacSeq (e1, e2) -> |
| 1709 |
let e1 = globalize ids e1 in |
| 1710 |
let e2 = globalize ids e2 in |
| 1711 |
CAst.make ?loc @@ CTacSeq (e1, e2) |
| 1712 |
| CTacIft (e, e1, e2) -> |
| 1713 |
let e = globalize ids e in |
| 1714 |
let e1 = globalize ids e1 in |
| 1715 |
let e2 = globalize ids e2 in |
| 1716 |
CAst.make ?loc @@ CTacIft (e, e1, e2) |
| 1717 |
| CTacCse (e, bl) -> |
| 1718 |
let e = globalize ids e in |
| 1719 |
let bl = List.map (fun b -> globalize_case ids b) bl in |
| 1720 |
CAst.make ?loc @@ CTacCse (e, bl) |
| 1721 |
| CTacRec (def, r) -> |
| 1722 |
let def = Option.map (globalize ids) def in |
| 1723 |
let map (p, e) = |
| 1724 |
let p = get_projection0 p in |
| 1725 |
let e = globalize ids e in |
| 1726 |
(AbsKn p, e) |
| 1727 |
in |
| 1728 |
CAst.make ?loc @@ CTacRec (def, List.map map r) |
| 1729 |
| CTacPrj (e, p) -> |
| 1730 |
let e = globalize ids e in |
| 1731 |
let p = get_projection0 p in |
| 1732 |
CAst.make ?loc @@ CTacPrj (e, AbsKn p) |
| 1733 |
| CTacSet (e, p, e') -> |
| 1734 |
let e = globalize ids e in |
| 1735 |
let p = get_projection0 p in |
| 1736 |
let e' = globalize ids e' in |
| 1737 |
CAst.make ?loc @@ CTacSet (e, AbsKn p, e') |
| 1738 |
| CTacExt (tag, arg) -> tacext ?loc (RawExt (tag, arg)) |
| 1739 |
| CTacGlb (prms, args, body, ty) -> |
| 1740 |
let args = List.map (fun (na, arg, ty) -> na, globalize ids arg, ty) args in |
| 1741 |
CAst.make ?loc @@ CTacGlb (prms, args, body, ty) |
| 1742 |
|
| 1743 |
and globalize_case ids (p, e) = |
| 1744 |
(globalize_pattern ids p, globalize ids e) |
| 1745 |
|
| 1746 |
and globalize_pattern ids ({loc;v=pr} as p) = match pr with |
| 1747 |
| CPatVar _ | CPatAtm _ -> p |
| 1748 |
| CPatRef (cst, pl) -> |
| 1749 |
let knc = get_constructor () cst in |
| 1750 |
let cst = AbsKn knc in |
| 1751 |
let pl = List.map (fun p -> globalize_pattern ids p) pl in |
| 1752 |
CAst.make ?loc @@ CPatRef (cst, pl) |
| 1753 |
| CPatCnv (pat, ty) -> |
| 1754 |
let pat = globalize_pattern ids pat in |
| 1755 |
CAst.make ?loc @@ CPatCnv (pat, ty) |
| 1756 |
| CPatOr pl -> |
| 1757 |
let pl = List.map (fun p -> globalize_pattern ids p) pl in |
| 1758 |
CAst.make ?loc @@ CPatOr pl |
| 1759 |
| CPatAs (p,x) -> |
| 1760 |
CAst.make ?loc @@ CPatAs (globalize_pattern ids p, x) |
| 1761 |
| CPatRecord pats -> |
| 1762 |
let map (p, e) = |
| 1763 |
let p = get_projection0 p in |
| 1764 |
let e = globalize_pattern ids e in |
| 1765 |
(AbsKn p, e) |
| 1766 |
in |
| 1767 |
CAst.make ?loc @@ CPatRecord (List.map map pats) |
| 1768 |
|
| 1769 |
in |
| 1770 |
globalize ids tac |
| 1771 |
|
| 1772 |
let globalize ids tac = |
| 1773 |
let tacext ?loc (RawExt (tag,_)) = |
| 1774 |
let arg = str (Tac2dyn.Arg.repr tag) in |
| 1775 |
CErrors.user_err ?loc (str "Cannot globalize generic arguments of type" ++ spc () ++ arg) |
| 1776 |
in |
| 1777 |
globalize_gen ~tacext ids tac |
| 1778 |
|
| 1779 |
let debug_globalize_allow_ext ids tac = |
| 1780 |
let tacext ?loc (RawExt (tag,arg)) = CAst.make ?loc @@ CTacExt (tag,arg) in |
| 1781 |
globalize_gen ~tacext ids tac |
| 1782 |
|
| 1783 |
let { Goptions.get = typed_notations } = |
| 1784 |
Goptions.declare_bool_option_and_ref |
| 1785 |
~key:["Ltac2";"Typed";"Notations"] ~value:true () |
| 1786 |
|
| 1787 |
let intern_notation_data ids body = |
| 1788 |
if typed_notations () then |
| 1789 |
let env = empty_env ~strict:true () in |
| 1790 |
let fold id (env,argtys) = |
| 1791 |
let ty = GTypVar (fresh_id env) in |
| 1792 |
let env = push_name (Name id) (monomorphic ty) env in |
| 1793 |
env, Id.Map.add id ty argtys |
| 1794 |
in |
| 1795 |
let env, argtys = Id.Set.fold fold ids (env,Id.Map.empty) in |
| 1796 |
let body, ty = intern_rec env None body in |
| 1797 |
let () = check_unused_variables env |
| 1798 |
(List.map (fun (id,_) -> Name id) (Id.Map.bindings argtys)) |
| 1799 |
in |
| 1800 |
let count = ref 0 in |
| 1801 |
let vars = ref TVar.Map.empty in |
| 1802 |
let argtys = Id.Map.map (fun ty -> normalize env (count, vars) ty) argtys in |
| 1803 |
let ty = normalize env (count, vars) ty in |
| 1804 |
let prms = !count in |
| 1805 |
Tac2env.TypedNota { |
| 1806 |
nota_prms = prms; |
| 1807 |
nota_argtys = argtys; |
| 1808 |
nota_ty = ty; |
| 1809 |
nota_body = body; |
| 1810 |
} |
| 1811 |
else |
| 1812 |
let body = globalize ids body in |
| 1813 |
Tac2env.UntypedNota body |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
open Mod_subst |
| 1818 |
|
| 1819 |
let subst_or_tuple f subst o = match o with |
| 1820 |
| Tuple _ -> o |
| 1821 |
| Other v -> |
| 1822 |
let v' = f subst v in |
| 1823 |
if v' == v then o else Other v' |
| 1824 |
|
| 1825 |
let rec subst_type subst t = match t with |
| 1826 |
| GTypVar _ -> t |
| 1827 |
| GTypArrow (t1, t2) -> |
| 1828 |
let t1' = subst_type subst t1 in |
| 1829 |
let t2' = subst_type subst t2 in |
| 1830 |
if t1' == t1 && t2' == t2 then t |
| 1831 |
else GTypArrow (t1', t2') |
| 1832 |
| GTypRef (kn, tl) -> |
| 1833 |
let kn' = subst_or_tuple subst_kn subst kn in |
| 1834 |
let tl' = List.Smart.map (fun t -> subst_type subst t) tl in |
| 1835 |
if kn' == kn && tl' == tl then t else GTypRef (kn', tl') |
| 1836 |
|
| 1837 |
let rec subst_glb_pat subst = function |
| 1838 |
| (GPatVar _ | GPatAtm _) as pat0 -> pat0 |
| 1839 |
| GPatRef (ctor,pats) as pat0 -> |
| 1840 |
let ctor' = |
| 1841 |
let ctyp' = Option.Smart.map (subst_kn subst) ctor.ctyp in |
| 1842 |
if ctyp' == ctor.ctyp then ctor |
| 1843 |
else {ctor with ctyp = ctyp'} |
| 1844 |
in |
| 1845 |
let pats' = List.Smart.map (subst_glb_pat subst) pats in |
| 1846 |
if ctor' == ctor && pats' == pats then pat0 |
| 1847 |
else GPatRef (ctor', pats') |
| 1848 |
| GPatOr pats as pat0 -> |
| 1849 |
let pats' = List.Smart.map (subst_glb_pat subst) pats in |
| 1850 |
if pats' == pats then pat0 |
| 1851 |
else GPatOr pats' |
| 1852 |
| GPatAs (p,x) as pat0 -> |
| 1853 |
let p' = subst_glb_pat subst p in |
| 1854 |
if p' == p then pat0 |
| 1855 |
else GPatAs (p',x) |
| 1856 |
|
| 1857 |
let rec subst_expr subst e = match e with |
| 1858 |
| GTacAtm _ | GTacVar _ | GTacPrm _ -> e |
| 1859 |
| GTacRef kn -> GTacRef (subst_kn subst kn) |
| 1860 |
| GTacFun (ids, e) -> GTacFun (ids, subst_expr subst e) |
| 1861 |
| GTacApp (f, args) -> |
| 1862 |
GTacApp (subst_expr subst f, List.map (fun e -> subst_expr subst e) args) |
| 1863 |
| GTacLet (r, bs, e) -> |
| 1864 |
let bs = List.map (fun (na, e) -> (na, subst_expr subst e)) bs in |
| 1865 |
GTacLet (r, bs, subst_expr subst e) |
| 1866 |
| GTacCst (t, n, el) as e0 -> |
| 1867 |
let t' = subst_or_tuple subst_kn subst t in |
| 1868 |
let el' = List.Smart.map (fun e -> subst_expr subst e) el in |
| 1869 |
if t' == t && el' == el then e0 else GTacCst (t', n, el') |
| 1870 |
| GTacCse (e, ci, cse0, cse1) -> |
| 1871 |
let cse0' = Array.map (fun e -> subst_expr subst e) cse0 in |
| 1872 |
let cse1' = Array.map (fun (ids, e) -> (ids, subst_expr subst e)) cse1 in |
| 1873 |
let ci' = subst_or_tuple subst_kn subst ci in |
| 1874 |
GTacCse (subst_expr subst e, ci', cse0', cse1') |
| 1875 |
| GTacWth { opn_match = e; opn_branch = br; opn_default = (na, def) } as e0 -> |
| 1876 |
let e' = subst_expr subst e in |
| 1877 |
let def' = subst_expr subst def in |
| 1878 |
let fold kn (self, vars, p) accu = |
| 1879 |
let kn' = subst_kn subst kn in |
| 1880 |
let p' = subst_expr subst p in |
| 1881 |
if kn' == kn && p' == p then accu |
| 1882 |
else KerName.Map.add kn' (self, vars, p') (KerName.Map.remove kn accu) |
| 1883 |
in |
| 1884 |
let br' = KerName.Map.fold fold br br in |
| 1885 |
if e' == e && br' == br && def' == def then e0 |
| 1886 |
else GTacWth { opn_match = e'; opn_default = (na, def'); opn_branch = br' } |
| 1887 |
| GTacFullMatch (e,brs) as e0 -> |
| 1888 |
let e' = subst_expr subst e in |
| 1889 |
let brs' = List.Smart.map (fun (pat,br as pbr) -> |
| 1890 |
let pat' = subst_glb_pat subst pat in |
| 1891 |
let br' = subst_expr subst br in |
| 1892 |
if pat' == pat && br' == br then pbr |
| 1893 |
else (pat',br')) |
| 1894 |
brs |
| 1895 |
in |
| 1896 |
if e' == e && brs' == brs then e0 |
| 1897 |
else GTacFullMatch (e', brs') |
| 1898 |
| GTacPrj (kn, e, p) as e0 -> |
| 1899 |
let kn' = subst_kn subst kn in |
| 1900 |
let e' = subst_expr subst e in |
| 1901 |
if kn' == kn && e' == e then e0 else GTacPrj (kn', e', p) |
| 1902 |
| GTacSet (kn, e, p, r) as e0 -> |
| 1903 |
let kn' = subst_kn subst kn in |
| 1904 |
let e' = subst_expr subst e in |
| 1905 |
let r' = subst_expr subst r in |
| 1906 |
if kn' == kn && e' == e && r' == r then e0 else GTacSet (kn', e', p, r') |
| 1907 |
| GTacExt (tag, arg) -> |
| 1908 |
let tpe = interp_ml_object tag in |
| 1909 |
let arg' = tpe.ml_subst subst arg in |
| 1910 |
if arg' == arg then e else GTacExt (tag, arg') |
| 1911 |
| GTacOpn (kn, el) as e0 -> |
| 1912 |
let kn' = subst_kn subst kn in |
| 1913 |
let el' = List.Smart.map (fun e -> subst_expr subst e) el in |
| 1914 |
if kn' == kn && el' == el then e0 else GTacOpn (kn', el') |
| 1915 |
|
| 1916 |
let subst_typedef subst e = match e with |
| 1917 |
| GTydDef t -> |
| 1918 |
let t' = Option.Smart.map (fun t -> subst_type subst t) t in |
| 1919 |
if t' == t then e else GTydDef t' |
| 1920 |
| GTydAlg galg -> |
| 1921 |
let map (warn, c, tl as p) = |
| 1922 |
let tl' = List.Smart.map (fun t -> subst_type subst t) tl in |
| 1923 |
if tl' == tl then p else (warn, c, tl') |
| 1924 |
in |
| 1925 |
let constrs' = List.Smart.map map galg.galg_constructors in |
| 1926 |
if constrs' == galg.galg_constructors then e |
| 1927 |
else GTydAlg { galg with galg_constructors = constrs' } |
| 1928 |
| GTydRec fields -> |
| 1929 |
let map (c, mut, t as p) = |
| 1930 |
let t' = subst_type subst t in |
| 1931 |
if t' == t then p else (c, mut, t') |
| 1932 |
in |
| 1933 |
let fields' = List.Smart.map map fields in |
| 1934 |
if fields' == fields then e else GTydRec fields' |
| 1935 |
| GTydOpn -> GTydOpn |
| 1936 |
|
| 1937 |
let subst_quant_typedef subst (prm, def as qdef) = |
| 1938 |
let def' = subst_typedef subst def in |
| 1939 |
if def' == def then qdef else (prm, def') |
| 1940 |
|
| 1941 |
let subst_type_scheme subst (prm, t as sch) = |
| 1942 |
let t' = subst_type subst t in |
| 1943 |
if t' == t then sch else (prm, t') |
| 1944 |
|
| 1945 |
let subst_or_relid subst ref = match ref with |
| 1946 |
| RelId _ -> ref |
| 1947 |
| AbsKn kn -> |
| 1948 |
let kn' = subst_or_tuple subst_kn subst kn in |
| 1949 |
if kn' == kn then ref else AbsKn kn' |
| 1950 |
|
| 1951 |
let rec subst_rawtype subst ({loc;v=tr} as t) = match tr with |
| 1952 |
| CTypVar _ -> t |
| 1953 |
| CTypArrow (t1, t2) -> |
| 1954 |
let t1' = subst_rawtype subst t1 in |
| 1955 |
let t2' = subst_rawtype subst t2 in |
| 1956 |
if t1' == t1 && t2' == t2 then t else CAst.make ?loc @@ CTypArrow (t1', t2') |
| 1957 |
| CTypRef (ref, tl) -> |
| 1958 |
let ref' = subst_or_relid subst ref in |
| 1959 |
let tl' = List.Smart.map (fun t -> subst_rawtype subst t) tl in |
| 1960 |
if ref' == ref && tl' == tl then t else CAst.make ?loc @@ CTypRef (ref', tl') |
| 1961 |
|
| 1962 |
let subst_tacref subst ref = match ref with |
| 1963 |
| RelId _ -> ref |
| 1964 |
| AbsKn (TacConstant kn) -> |
| 1965 |
let kn' = subst_kn subst kn in |
| 1966 |
if kn' == kn then ref else AbsKn (TacConstant kn') |
| 1967 |
| AbsKn (TacAlias kn) -> |
| 1968 |
let kn' = subst_kn subst kn in |
| 1969 |
if kn' == kn then ref else AbsKn (TacAlias kn') |
| 1970 |
|
| 1971 |
let subst_projection subst prj = match prj with |
| 1972 |
| RelId _ -> prj |
| 1973 |
| AbsKn kn -> |
| 1974 |
let kn' = subst_kn subst kn in |
| 1975 |
if kn' == kn then prj else AbsKn kn' |
| 1976 |
|
| 1977 |
let rec subst_rawpattern subst ({loc;v=pr} as p) = match pr with |
| 1978 |
| CPatVar _ | CPatAtm _ -> p |
| 1979 |
| CPatRef (c, pl) -> |
| 1980 |
let pl' = List.Smart.map (fun p -> subst_rawpattern subst p) pl in |
| 1981 |
let c' = subst_or_relid subst c in |
| 1982 |
if pl' == pl && c' == c then p else CAst.make ?loc @@ CPatRef (c', pl') |
| 1983 |
| CPatCnv (pat, ty) -> |
| 1984 |
let pat' = subst_rawpattern subst pat in |
| 1985 |
let ty' = subst_rawtype subst ty in |
| 1986 |
if pat' == pat && ty' == ty then p else CAst.make ?loc @@ CPatCnv (pat', ty') |
| 1987 |
| CPatOr pl -> |
| 1988 |
let pl' = List.Smart.map (fun p -> subst_rawpattern subst p) pl in |
| 1989 |
if pl' == pl then p else CAst.make ?loc @@ CPatOr pl' |
| 1990 |
| CPatAs (pat,x) -> |
| 1991 |
let pat' = subst_rawpattern subst pat in |
| 1992 |
if pat' == pat then p else CAst.make ?loc @@ CPatAs (pat', x) |
| 1993 |
| CPatRecord el -> |
| 1994 |
let map (prj, e as p) = |
| 1995 |
let prj' = subst_projection subst prj in |
| 1996 |
let e' = subst_rawpattern subst e in |
| 1997 |
if prj' == prj && e' == e then p else (prj', e') |
| 1998 |
in |
| 1999 |
let el' = List.Smart.map map el in |
| 2000 |
if el' == el then p else CAst.make ?loc @@ CPatRecord el' |
| 2001 |
|
| 2002 |
|
| 2003 |
let rec subst_rawexpr subst ({loc;v=tr} as t) = match tr with |
| 2004 |
| CTacAtm _ -> t |
| 2005 |
| CTacRef ref -> |
| 2006 |
let ref' = subst_tacref subst ref in |
| 2007 |
if ref' == ref then t else CAst.make ?loc @@ CTacRef ref' |
| 2008 |
| CTacCst ref -> |
| 2009 |
let ref' = subst_or_relid subst ref in |
| 2010 |
if ref' == ref then t else CAst.make ?loc @@ CTacCst ref' |
| 2011 |
| CTacFun (bnd, e) -> |
| 2012 |
let map pat = subst_rawpattern subst pat in |
| 2013 |
let bnd' = List.Smart.map map bnd in |
| 2014 |
let e' = subst_rawexpr subst e in |
| 2015 |
if bnd' == bnd && e' == e then t else CAst.make ?loc @@ CTacFun (bnd', e') |
| 2016 |
| CTacApp (e, el) -> |
| 2017 |
let e' = subst_rawexpr subst e in |
| 2018 |
let el' = List.Smart.map (fun e -> subst_rawexpr subst e) el in |
| 2019 |
if e' == e && el' == el then t else CAst.make ?loc @@ CTacApp (e', el') |
| 2020 |
| CTacLet (isrec, bnd, e) -> |
| 2021 |
let map (na, e as p) = |
| 2022 |
let na' = subst_rawpattern subst na in |
| 2023 |
let e' = subst_rawexpr subst e in |
| 2024 |
if na' == na && e' == e then p else (na', e') |
| 2025 |
in |
| 2026 |
let bnd' = List.Smart.map map bnd in |
| 2027 |
let e' = subst_rawexpr subst e in |
| 2028 |
if bnd' == bnd && e' == e then t else CAst.make ?loc @@ CTacLet (isrec, bnd', e') |
| 2029 |
| CTacCnv (e, c) -> |
| 2030 |
let e' = subst_rawexpr subst e in |
| 2031 |
let c' = subst_rawtype subst c in |
| 2032 |
if c' == c && e' == e then t else CAst.make ?loc @@ CTacCnv (e', c') |
| 2033 |
| CTacSeq (e1, e2) -> |
| 2034 |
let e1' = subst_rawexpr subst e1 in |
| 2035 |
let e2' = subst_rawexpr subst e2 in |
| 2036 |
if e1' == e1 && e2' == e2 then t else CAst.make ?loc @@ CTacSeq (e1', e2') |
| 2037 |
| CTacIft (e, e1, e2) -> |
| 2038 |
let e' = subst_rawexpr subst e in |
| 2039 |
let e1' = subst_rawexpr subst e1 in |
| 2040 |
let e2' = subst_rawexpr subst e2 in |
| 2041 |
if e' == e && e1' == e1 && e2' == e2 then t else CAst.make ?loc @@ CTacIft (e', e1', e2') |
| 2042 |
| CTacCse (e, bl) -> |
| 2043 |
let map (p, e as x) = |
| 2044 |
let p' = subst_rawpattern subst p in |
| 2045 |
let e' = subst_rawexpr subst e in |
| 2046 |
if p' == p && e' == e then x else (p', e') |
| 2047 |
in |
| 2048 |
let e' = subst_rawexpr subst e in |
| 2049 |
let bl' = List.Smart.map map bl in |
| 2050 |
if e' == e && bl' == bl then t else CAst.make ?loc @@ CTacCse (e', bl') |
| 2051 |
| CTacRec (def, el) -> |
| 2052 |
let def' = Option.Smart.map (subst_rawexpr subst) def in |
| 2053 |
let map (prj, e as p) = |
| 2054 |
let prj' = subst_projection subst prj in |
| 2055 |
let e' = subst_rawexpr subst e in |
| 2056 |
if prj' == prj && e' == e then p else (prj', e') |
| 2057 |
in |
| 2058 |
let el' = List.Smart.map map el in |
| 2059 |
if def' == def && el' == el then t else CAst.make ?loc @@ CTacRec (def',el') |
| 2060 |
| CTacPrj (e, prj) -> |
| 2061 |
let prj' = subst_projection subst prj in |
| 2062 |
let e' = subst_rawexpr subst e in |
| 2063 |
if prj' == prj && e' == e then t else CAst.make ?loc @@ CTacPrj (e', prj') |
| 2064 |
| CTacSet (e, prj, r) -> |
| 2065 |
let prj' = subst_projection subst prj in |
| 2066 |
let e' = subst_rawexpr subst e in |
| 2067 |
let r' = subst_rawexpr subst r in |
| 2068 |
if prj' == prj && e' == e && r' == r then t else CAst.make ?loc @@ CTacSet (e', prj', r') |
| 2069 |
| CTacGlb (prms, args, body, ty) -> |
| 2070 |
let args' = List.Smart.map (fun (na, arg, ty as o) -> |
| 2071 |
let arg' = subst_rawexpr subst arg in |
| 2072 |
let ty' = Option.Smart.map (subst_type subst) ty in |
| 2073 |
if arg' == arg && ty' == ty then o |
| 2074 |
else (na, arg', ty')) |
| 2075 |
args |
| 2076 |
in |
| 2077 |
let body' = subst_expr subst body in |
| 2078 |
let ty' = subst_type subst ty in |
| 2079 |
if args' == args && body' == body && ty' == ty then t |
| 2080 |
else CAst.make ?loc @@ CTacGlb (prms, args', body', ty') |
| 2081 |
| CTacSyn _ | CTacExt _ -> assert false |
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
let genintern_core ?(check_unused=true) ist locals expected v = |
| 2086 |
let open Genintern in |
| 2087 |
let env = match Genintern.Store.get ist.extra ltac2_env with |
| 2088 |
| None -> |
| 2089 |
|
| 2090 |
empty_env ~strict:ist.strict_check () |
| 2091 |
| Some env -> env |
| 2092 |
in |
| 2093 |
let env = List.fold_left (fun env (na,t) -> push_name na t env) env locals in |
| 2094 |
let loc = v.CAst.loc in |
| 2095 |
let v, t = intern_rec env expected v in |
| 2096 |
let () = if check_unused then check_unused_variables ?loc env (List.map fst locals) in |
| 2097 |
env, v, t |
| 2098 |
|
| 2099 |
let genintern_warn_not_unit ?check_unused ist locals ({CAst.loc} as v) = |
| 2100 |
let env, v, t = genintern_core ?check_unused ist locals None v in |
| 2101 |
let () = check_elt_unit loc env t in |
| 2102 |
v |
| 2103 |
|
| 2104 |
let genintern ?check_unused ist locals expected v = |
| 2105 |
let _, v, _ = genintern_core ?check_unused ist locals (Some expected) v in |
| 2106 |
v |
| 2107 |
|
| 2108 |
let () = |
| 2109 |
let open Genintern in |
| 2110 |
let intern ist tac = |
| 2111 |
let t_preterm = monomorphic (GTypRef (Other t_preterm, [])) in |
| 2112 |
let ntn_vars = ist.intern_sign.notation_variable_status in |
| 2113 |
let locals = |
| 2114 |
Id.Map.fold (fun id _ acc -> (Name id, t_preterm) :: acc) ntn_vars [] |
| 2115 |
in |
| 2116 |
|
| 2117 |
|
| 2118 |
let env, v, t = genintern_core ~check_unused:false ist locals None tac in |
| 2119 |
let () = check_elt_unit tac.loc env t in |
| 2120 |
let unused = list_unused_variables env (List.map fst locals) in |
| 2121 |
let ids = Id.Map.domain ntn_vars in |
| 2122 |
let ids = List.fold_left (fun ids id -> Id.Set.remove id ids) ids unused in |
| 2123 |
let () = Id.Set.iter (fun id -> |
| 2124 |
let status = Id.Map.get id ist.intern_sign.notation_variable_status in |
| 2125 |
match status.Genintern.ntnvar_typ with |
| 2126 |
| NtnInternTypeAny _ -> () |
| 2127 |
| NtnInternTypeOnlyBinder -> |
| 2128 |
CErrors.user_err ?loc:tac.loc Pp.(str "Cannot use binder notation variable " ++ Id.print id ++ str " as a preterm.")) |
| 2129 |
ids |
| 2130 |
in |
| 2131 |
(ist, (ids, v)) |
| 2132 |
in |
| 2133 |
Genintern.register_intern0 wit_ltac2_constr intern |
| 2134 |
|
| 2135 |
let () = |
| 2136 |
let open Genintern in |
| 2137 |
let intern ist tac = |
| 2138 |
|
| 2139 |
let env = empty_env ~strict:ist.strict_check () in |
| 2140 |
let tac, _ = intern_rec env (Some (GTypRef (Tuple 0, []))) tac in |
| 2141 |
ist, tac |
| 2142 |
in |
| 2143 |
Genintern.register_intern0 wit_ltac2_tac intern |
| 2144 |
|
| 2145 |
let () = Gensubst.register_subst0 wit_ltac2_constr (fun s (ids, e) -> ids, subst_expr s e) |
| 2146 |
let () = Gensubst.register_subst0 wit_ltac2_tac subst_expr |
| 2147 |
|
| 2148 |
let intern_var_quotation_gen ~ispat ist (kind, { CAst.v = id; loc }) = |
| 2149 |
let open Genintern in |
| 2150 |
let kind = match kind with |
| 2151 |
| None -> ConstrVar |
| 2152 |
| Some kind -> match Id.to_string kind.CAst.v with |
| 2153 |
| "constr" -> ConstrVar |
| 2154 |
| "preterm" -> PretermVar |
| 2155 |
| "pattern" -> PatternVar |
| 2156 |
| "hyp" -> HypVar |
| 2157 |
| _ -> |
| 2158 |
CErrors.user_err ?loc:kind.loc |
| 2159 |
Pp.(str "Unknown Ltac2 variable quotation kind" ++ spc() ++ Id.print kind.v) |
| 2160 |
in |
| 2161 |
let typ = match kind with |
| 2162 |
| ConstrVar -> |
| 2163 |
if ispat |
| 2164 |
then CErrors.user_err ?loc Pp.(str "constr quotations not supported in tactic patterns.") |
| 2165 |
else t_constr |
| 2166 |
| PretermVar -> |
| 2167 |
if ispat |
| 2168 |
then CErrors.user_err ?loc Pp.(str "preterm quotations not supported in tactic patterns.") |
| 2169 |
else t_preterm |
| 2170 |
| PatternVar -> |
| 2171 |
if not ispat |
| 2172 |
then CErrors.user_err ?loc Pp.(str "pattern quotations not supported outside tactic patterns.") |
| 2173 |
else t_pattern |
| 2174 |
| HypVar -> |
| 2175 |
|
| 2176 |
if ispat |
| 2177 |
then CErrors.user_err ?loc Pp.(str "hyp quotations not supported in tactic patterns.") |
| 2178 |
else t_ident |
| 2179 |
in |
| 2180 |
let env = match Genintern.Store.get ist.extra ltac2_env with |
| 2181 |
| None -> |
| 2182 |
|
| 2183 |
empty_env ~strict:ist.strict_check () |
| 2184 |
| Some env -> env |
| 2185 |
in |
| 2186 |
|
| 2187 |
let () = |
| 2188 |
if Id.Map.mem id ist.intern_sign.notation_variable_status then |
| 2189 |
|
| 2190 |
unify ?loc env (GTypRef (Other t_preterm, [])) (GTypRef (Other typ, [])) |
| 2191 |
in |
| 2192 |
let t = |
| 2193 |
try find_var id env |
| 2194 |
with Not_found -> |
| 2195 |
CErrors.user_err ?loc (str "Unbound value " ++ Id.print id) |
| 2196 |
in |
| 2197 |
let t = fresh_mix_type_scheme env t in |
| 2198 |
let () = unify ?loc env t (GTypRef (Other typ, [])) in |
| 2199 |
(ist, (kind, id)) |
| 2200 |
|
| 2201 |
let intern_var_quotation = intern_var_quotation_gen ~ispat:false |
| 2202 |
|
| 2203 |
let () = Genintern.register_intern0 wit_ltac2_var_quotation intern_var_quotation |
| 2204 |
|
| 2205 |
let intern_var_quotation_pat ?loc ist v = |
| 2206 |
intern_var_quotation_gen ~ispat:true ist v |
| 2207 |
|
| 2208 |
let () = Genintern.register_intern_pat wit_ltac2_var_quotation |
| 2209 |
intern_var_quotation_pat |
| 2210 |
|
| 2211 |
let () = Gensubst.register_subst0 wit_ltac2_var_quotation (fun _ v -> v) |