2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
14 exception RefineFailure of (Stdpp.location * string) Lazy.t;;
15 exception Uncertain of (Stdpp.location * string) Lazy.t;;
16 exception AssertFailure of string Lazy.t;;
19 module Ref = NReference
21 let debug = ref false;;
26 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
31 let time1 = Unix.gettimeofday () in
32 indent := !indent ^ String.make 1 c;
33 times := time1 :: !times;
34 prerr_endline ("{{{" ^ !indent ^ " ")
40 let time2 = Unix.gettimeofday () in
42 match !times with time1::tl -> times := tl; time1 | [] -> assert false in
43 prerr_endline ("}}} " ^ string_of_float (time2 -. time1));
44 if not ok then prerr_endline "exception raised!";
46 indent := String.sub !indent 0 (String.length !indent -1)
48 Invalid_argument _ -> indent := "??"; ()
53 let wrap_exc msg = function
54 | NCicUnification.Uncertain _ -> Uncertain msg
55 | NCicUnification.UnificationFailure _ -> RefineFailure msg
56 | NCicTypeChecker.TypeCheckerFailure _ -> RefineFailure msg
60 let exp_implicit rdb ~localise metasenv subst context with_type t =
63 let metasenv,subst,expty =
65 None -> metasenv,subst,None
67 let (metasenv,subst),typ =
70 ~unify:(fun m s c t1 t2 ->
71 try Some (NCicUnification.unify rdb m s c t1 t2)
72 with NCicUnification.UnificationFailure _ | NCicUnification.Uncertain _ -> None)
73 metasenv subst context 0 (0,NCic.Irl 0) typ
75 NCicMetaSubst.MetaSubstFailure _
76 | NCicMetaSubst.Uncertain _ ->
77 raise (RefineFailure (lazy (localise t,"Trying to create a closed meta with a non closed type: " ^ NCicPp.ppterm ~metasenv ~subst ~context typ)))
80 metasenv,subst,Some typ
82 NCicMetaSubst.mk_meta metasenv [] ?with_type:expty `IsTerm,subst
83 | `Type -> NCicMetaSubst.mk_meta metasenv context ?with_type `IsType,subst
84 | `Term -> NCicMetaSubst.mk_meta metasenv context ?with_type `IsTerm,subst
87 ~attrs:[`Name s] metasenv context ?with_type `IsTerm,subst
89 raise (RefineFailure (lazy (localise t, "A vector of implicit terms " ^
90 "can only be used in argument position")))
94 let check_allowed_sort_elimination rdb localise r orig =
97 | C.Appl l -> C.Appl (l @ [arg])
98 | t -> C.Appl [t;arg] in
99 (* ctx, ind_type @ lefts, sort_of_ind_ty@lefts, outsort *)
100 let rec aux metasenv subst context ind arity1 arity2 =
101 (*D*)inside 'S'; try let rc =
102 let arity1 = NCicReduction.whd ~subst context arity1 in
103 pp (lazy(NCicPp.ppterm ~subst ~metasenv ~context arity1 ^ " elimsto " ^
104 NCicPp.ppterm ~subst ~metasenv ~context arity2 ^ "\nMENV:\n"^
105 NCicPp.ppmetasenv ~subst metasenv));
107 | C.Prod (name,so1,de1) (* , t ==?== C.Prod _ *) ->
108 let metasenv, _, meta, _ =
109 NCicMetaSubst.mk_meta metasenv ((name,C.Decl so1)::context) `IsType
111 let metasenv, subst =
112 try NCicUnification.unify rdb metasenv subst context
113 arity2 (C.Prod (name, so1, meta))
114 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
115 "expected %s, found %s" (* XXX localizzare meglio *)
116 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod (name, so1, meta)))
117 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
119 aux metasenv subst ((name, C.Decl so1)::context)
120 (mkapp (NCicSubstitution.lift 1 ind) (C.Rel 1)) de1 meta
121 | C.Sort _ (* , t ==?== C.Prod _ *) ->
122 let metasenv, _, meta, _ = NCicMetaSubst.mk_meta metasenv [] `IsSort in
123 let metasenv, subst =
124 try NCicUnification.unify rdb metasenv subst context
125 arity2 (C.Prod ("_", ind, meta))
126 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
127 "expected %s, found %s" (* XXX localizzare meglio *)
128 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod ("_", ind, meta)))
129 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
131 (try NCicTypeChecker.check_allowed_sort_elimination
132 ~metasenv ~subst r context ind arity1 arity2;
134 with exc -> raise (wrap_exc (lazy (localise orig,
135 "Sort elimination not allowed ")) exc))
137 (*D*)in outside true; rc with exc -> outside false; raise exc
143 ?(localise=fun _ -> Stdpp.dummy_loc)
144 metasenv subst context term expty
146 let force_ty skip_lambda skip_appl metasenv subst context orig t infty expty =
147 (*D*)inside 'F'; try let rc =
151 | C.Implicit _ -> assert false
152 | C.Lambda _ when skip_lambda -> metasenv, subst, t, expty
153 | C.Appl _ when skip_appl -> metasenv, subst, t, expty
155 pp (lazy ("forcing infty=expty: "^
156 (NCicPp.ppterm ~metasenv ~subst ~context infty) ^ " === " ^
157 (NCicPp.ppterm ~metasenv ~subst:[] ~context expty)));
159 let metasenv, subst =
160 (*D*)inside 'U'; try let rc =
161 NCicUnification.unify rdb metasenv subst context infty expty
162 (*D*)in outside true; rc with exc -> outside false; raise exc
164 metasenv, subst, t, expty
166 | NCicUnification.Uncertain _
167 | NCicUnification.UnificationFailure _ as exc ->
168 try_coercions rdb ~localise
169 metasenv subst context t orig infty expty true exc)
170 | None -> metasenv, subst, t, infty
171 (*D*)in outside true; rc with exc -> outside false; raise exc
173 let rec typeof_aux metasenv subst context expty =
175 (*D*)inside 'R'; try let rc =
176 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t ^ " ::exp:: " ^
177 match expty with None -> "None" | Some e ->
178 NCicPp.ppterm ~metasenv ~subst ~context e));
179 let metasenv, subst, t, infty =
184 match List.nth context (n - 1) with
185 | (_,C.Decl ty) -> NCicSubstitution.lift n ty
186 | (_,C.Def (_,ty)) -> NCicSubstitution.lift n ty
188 raise (RefineFailure (lazy (localise t,"unbound variable"))))
190 metasenv, subst, t, infty
192 (try metasenv, subst, t, C.Sort (NCicEnvironment.typeof_sort s)
194 | NCicEnvironment.UntypableSort msg ->
195 raise (RefineFailure (lazy (localise t, Lazy.force msg)))
196 | NCicEnvironment.AssertFailure msg -> raise (AssertFailure msg))
197 | C.Implicit infos ->
198 let (metasenv,_,t,ty),subst =
199 exp_implicit rdb ~localise metasenv subst context expty t infos
202 typeof_aux metasenv subst context None t
204 metasenv, subst, t, ty
205 | C.Meta (n,l) as t ->
208 let _,_,_,ty = NCicUtils.lookup_subst n subst in metasenv, ty
209 with NCicUtils.Subst_not_found _ ->
210 NCicMetaSubst.extend_meta metasenv n
212 metasenv, subst, t, NCicSubstitution.subst_meta l ty
214 metasenv, subst, t, NCicTypeChecker.typeof ~subst ~metasenv context t
215 | C.Prod (name,(s as orig_s),(t as orig_t)) ->
216 let metasenv, subst, s, s1 = typeof_aux metasenv subst context None s in
217 let metasenv, subst, s, s1 =
219 metasenv subst context s orig_s localise s1 in
220 let context1 = (name,(C.Decl s))::context in
221 let metasenv, subst, t, s2 = typeof_aux metasenv subst context1 None t in
222 let metasenv, subst, t, s2 =
224 metasenv subst context1 t orig_t localise s2 in
225 let metasenv, subst, s, t, ty =
226 sort_of_prod localise metasenv subst
227 context orig_s orig_t (name,s) t (s1,s2)
229 metasenv, subst, NCic.Prod(name,s,t), ty
230 | C.Lambda (n,(s as orig_s),t) as orig ->
231 let exp_s, exp_ty_t, force_after =
233 | None -> None, None, false
235 match NCicReduction.whd ~subst context expty with
236 | C.Prod (_,s,t) -> Some s, Some t, false
237 | _ -> None, None, true
239 let metasenv, subst, s, ty_s =
240 typeof_aux metasenv subst context None s in
241 let metasenv, subst, s, _ =
243 metasenv subst context s orig_s localise ty_s in
244 let (metasenv,subst), exp_ty_t =
248 pp(lazy("Force source to: "^NCicPp.ppterm ~metasenv ~subst
250 NCicUnification.unify ~test_eq_only:true rdb metasenv subst context s exp_s,exp_ty_t
251 with exc -> raise (wrap_exc (lazy (localise orig_s, Printf.sprintf
252 "Source type %s was expected to be %s" (NCicPp.ppterm ~metasenv
253 ~subst ~context s) (NCicPp.ppterm ~metasenv ~subst ~context
255 | None -> (metasenv, subst), None
257 let context_for_t = (n,C.Decl s) :: context in
258 let metasenv, subst, t, ty_t =
259 typeof_aux metasenv subst context_for_t exp_ty_t t
262 force_ty false true metasenv subst context orig
263 (C.Lambda(n,s,t)) (C.Prod (n,s,ty_t)) expty
265 metasenv, subst, C.Lambda(n,s,t), C.Prod (n,s,ty_t)
266 | C.LetIn (n,(ty as orig_ty),t,bo) ->
267 let metasenv, subst, ty, ty_ty =
268 typeof_aux metasenv subst context None ty in
269 let metasenv, subst, ty, _ =
271 metasenv subst context ty orig_ty localise ty_ty in
272 let metasenv, subst, t, _ =
273 typeof_aux metasenv subst context (Some ty) t in
274 let context1 = (n, C.Def (t,ty)) :: context in
275 let metasenv, subst, expty1 =
277 | None -> metasenv, subst, None
280 NCicUnification.delift_type_wrt_terms
281 rdb metasenv subst context1 (NCicSubstitution.lift 1 x)
282 [NCicSubstitution.lift 1 t]
286 let metasenv, subst, bo, bo_ty =
287 typeof_aux metasenv subst context1 expty1 bo
289 let bo_ty = NCicSubstitution.subst ~avoid_beta_redexes:true t bo_ty in
290 metasenv, subst, C.LetIn (n, ty, t, bo), bo_ty
291 | C.Appl ((he as orig_he)::(_::_ as args)) ->
292 let upto = match orig_he with C.Meta _ -> List.length args | _ -> 0 in
294 if upto > 0 then NCicReduction.head_beta_reduce ~upto t else t
297 let metasenv, subst, he, ty_he =
298 typeof_aux metasenv subst context None he in
299 let metasenv, subst, t, ty =
300 eat_prods rdb ~localise force_ty metasenv subst context expty t
301 orig_he he ty_he args in
302 metasenv, subst, hbr t, ty
304 if args = [C.Implicit `Vector] && expty <> None then
305 (* we try here to expand the vector a 0 implicits, but we use
306 * the expected type *)
308 let metasenv, subst, he, ty_he =
309 typeof_aux metasenv subst context expty he in
310 metasenv, subst, hbr he, ty_he
311 with Uncertain _ | RefineFailure _ -> refine_appl ()
313 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
314 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,
315 outtype,(term as orig_term),pl) as orig ->
316 let _, leftno, itl, _, _ = NCicEnvironment.get_checked_indtys r in
317 let _, _, arity, cl = List.nth itl tyno in
318 let constructorsno = List.length cl in
319 let _, metasenv, args =
320 NCicMetaSubst.saturate metasenv subst context arity 0 in
321 let ind = if args = [] then C.Const r else C.Appl (C.Const r::args) in
322 let metasenv, subst, term, _ =
323 typeof_aux metasenv subst context (Some ind) term in
324 let parameters, arguments = HExtlib.split_nth leftno args in
327 | C.Implicit _ as ot ->
328 let rec aux = function
329 | [] -> NCic.Lambda ("_",NCic.Implicit `Type,ot)
330 | _::tl -> NCic.Lambda ("_",NCic.Implicit `Type,aux tl)
335 let metasenv, subst, outtype, outsort =
336 typeof_aux metasenv subst context None outtype in
338 (* next lines are to do a subst-expansion of the outtype, so
339 that when it becomes a beta-abstraction, the beta-redex is
340 fired during substitution *)
341 let _,fresh_metanoouttype,newouttype,_ =
342 NCicMetaSubst.mk_meta metasenv context `IsTerm in
344 (fresh_metanoouttype,([`Name "outtype"],context,outtype,outsort))
346 let outtype = newouttype in
348 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
350 if parameters = [] then C.Const r
351 else C.Appl ((C.Const r)::parameters) in
352 let metasenv, subst, ind, ind_ty =
353 typeof_aux metasenv subst context None ind in
354 let metasenv, subst =
355 check_allowed_sort_elimination rdb localise r orig_term metasenv subst
356 context ind ind_ty outsort
358 (* let's check if the type of branches are right *)
359 if List.length pl <> constructorsno then
360 raise (RefineFailure (lazy (localise orig,
361 "Wrong number of cases in a match")));
363 let metasenv, subst =
365 | None -> metasenv, subst
367 NCicUnification.unify rdb metasenv subst context resty expty
370 let _, metasenv, subst, pl =
372 (fun p (j, metasenv, subst, branches) ->
374 let cons = Ref.mk_constructor j r in
375 if parameters = [] then C.Const cons
376 else C.Appl (C.Const cons::parameters)
378 let metasenv, subst, cons, ty_cons =
379 typeof_aux metasenv subst context None cons in
381 NCicTypeChecker.type_of_branch
382 ~subst context leftno outtype cons ty_cons in
383 pp (lazy ("TYPEOFBRANCH: " ^
384 NCicPp.ppterm ~metasenv ~subst ~context p ^ " ::inf:: " ^
385 NCicPp.ppterm ~metasenv ~subst ~context ty_branch ));
386 let metasenv, subst, p, _ =
387 typeof_aux metasenv subst context (Some ty_branch) p in
388 j-1, metasenv, subst, p :: branches)
389 pl (List.length pl, metasenv, subst, [])
391 let resty = C.Appl (outtype::arguments@[term]) in
392 let resty = NCicReduction.head_beta_reduce ~subst resty in
393 metasenv, subst, C.Match (r, outtype, term, pl),resty
394 | C.Match _ -> assert false
396 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t ^ " ::inf:: "^
397 NCicPp.ppterm ~metasenv ~subst ~context infty ));
398 force_ty true true metasenv subst context orig t infty expty
399 (*D*)in outside true; rc with exc -> outside false; raise exc
401 typeof_aux metasenv subst context expty term
403 and try_coercions rdb
405 metasenv subst context t orig_t infty expty perform_unification exc
407 (* we try with a coercion *)
408 let rec first exc = function
410 raise (wrap_exc (lazy (localise orig_t, Printf.sprintf
411 "The term\n%s\nhas type\n%s\nbut is here used with type\n%s"
412 (NCicPp.ppterm ~metasenv ~subst ~context t)
413 (NCicPp.ppterm ~metasenv ~subst ~context infty)
414 (NCicPp.ppterm ~metasenv ~subst ~context expty))) exc)
415 | (_,metasenv, newterm, newtype, meta)::tl ->
417 pp (lazy("K=" ^ NCicPp.ppterm ~metasenv ~subst ~context newterm));
418 pp (lazy ( "UNIFICATION in CTX:\n"^
419 NCicPp.ppcontext ~metasenv ~subst context
421 NCicPp.ppmetasenv metasenv ~subst
423 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
424 NCicPp.ppterm ~metasenv ~subst ~context meta ^ "\n"));
425 let metasenv, subst =
426 NCicUnification.unify rdb metasenv subst context t meta
428 pp (lazy ( "UNIFICATION in CTX:\n"^
429 NCicPp.ppcontext ~metasenv ~subst context
431 NCicPp.ppmetasenv metasenv ~subst
433 NCicPp.ppterm ~metasenv ~subst ~context newtype ^ " === " ^
434 NCicPp.ppterm ~metasenv ~subst ~context expty ^ "\n"));
435 let metasenv, subst =
436 if perform_unification then
437 NCicUnification.unify rdb metasenv subst context newtype expty
440 metasenv, subst, newterm, newtype
442 | NCicUnification.UnificationFailure _ -> first exc tl
443 | NCicUnification.Uncertain _ as exc -> first exc tl
445 pp(lazy("try_coercion " ^
446 NCicPp.ppterm ~metasenv ~subst ~context infty ^ " |---> " ^
447 NCicPp.ppterm ~metasenv ~subst ~context expty));
449 (NCicCoercion.look_for_coercion
450 rdb metasenv subst context infty expty)
452 and force_to_sort rdb metasenv subst context t orig_t localise ty =
453 match NCicReduction.whd ~subst context ty with
454 | C.Meta (_,(0,(C.Irl 0 | C.Ctx []))) as ty ->
455 metasenv, subst, t, ty
456 | C.Meta (_i,(_,(C.Irl 0 | C.Ctx []))) -> assert false (*CSC: ???
457 metasenv, subst, t, C.Meta(i,(0,C.Irl 0)) *)
458 | C.Meta (i,(_,lc)) ->
459 let len = match lc with C.Irl len->len | C.Ctx l->List.length l in
460 let metasenv, subst, newmeta, _ =
462 NCicMetaSubst.restrict metasenv subst i (HExtlib.list_seq 1 (len+1))
463 else metasenv, subst, i, []
465 metasenv, subst, t, C.Meta (newmeta,(0,C.Irl 0))
466 | C.Sort _ as ty -> metasenv, subst, t, ty
468 try_coercions rdb ~localise metasenv subst context
469 t orig_t ty (NCic.Sort (NCic.Type
470 (match NCicEnvironment.get_universes () with
472 | _ -> assert false))) false
473 (Uncertain (lazy (localise orig_t,
474 "The type of " ^ NCicPp.ppterm ~metasenv ~subst ~context t
475 ^ " is not a sort: " ^ NCicPp.ppterm ~metasenv ~subst ~context ty)))
478 localise metasenv subst context orig_s orig_t (name,s) t (t1, t2)
480 (* force to sort is done in the Prod case in typeof *)
482 | C.Sort _, C.Sort C.Prop -> metasenv, subst, s, t, t2
483 | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
484 metasenv, subst, s, t, C.Sort (C.Type (NCicEnvironment.max u1 u2))
485 | C.Sort C.Prop,C.Sort (C.Type _) -> metasenv, subst, s, t, t2
487 | C.Meta _, C.Meta (_,(_,_))
488 | C.Sort _, C.Meta (_,(_,_)) -> metasenv, subst, s, t, t2
489 | x, (C.Sort _ | C.Meta _) | _, x ->
490 let y, context, orig =
491 if x == t1 then s, context, orig_s
492 else t, ((name,C.Decl s)::context), orig_t
494 raise (RefineFailure (lazy (localise orig,Printf.sprintf
495 "%s is expected to be a type, but its type is %s that is not a sort"
496 (NCicPp.ppterm ~subst ~metasenv ~context y)
497 (NCicPp.ppterm ~subst ~metasenv ~context x))))
499 and guess_name subst ctx ty =
500 let aux initial = "#" ^ String.make 1 initial in
502 | C.Const (NReference.Ref (u,_))
503 | C.Appl (C.Const (NReference.Ref (u,_)) :: _) ->
504 aux (String.sub (NUri.name_of_uri u) 0 1).[0]
505 | C.Prod _ -> aux 'f'
508 let _,_,t,_ = NCicUtils.lookup_subst n subst in
509 guess_name subst ctx (NCicSubstitution.subst_meta lc t)
510 with NCicUtils.Subst_not_found _ -> aux 'M')
513 and eat_prods rdb ~localise force_ty metasenv subst context expty orig_t orig_he he ty_he args =
514 (*D*)inside 'E'; try let rc =
515 let rec aux metasenv subst args_so_far he ty_he xxx =
516 (*D*)inside 'V'; try let rc =
519 let res = NCicUntrusted.mk_appl he (List.rev args_so_far) in
520 pp(lazy("FORCE FINAL APPL: " ^
521 NCicPp.ppterm ~metasenv ~subst ~context res ^
522 " of type " ^ NCicPp.ppterm ~metasenv ~subst ~context ty_he
523 ^ " to type " ^ match expty with None -> "None" | Some x ->
524 NCicPp.ppterm ~metasenv ~subst ~context x));
525 (* whatever the term is, we force the type. in case of ((Lambda..) ?...)
526 * the application may also be a lambda! *)
527 force_ty false false metasenv subst context orig_t res ty_he expty
528 | NCic.Implicit `Vector::tl ->
529 let has_some_more_pis x =
530 match NCicReduction.whd ~subst context x with
531 | NCic.Meta _ | NCic.Appl (NCic.Meta _::_) -> false
535 aux metasenv subst args_so_far he ty_he tl
538 | RefineFailure _ as exc when has_some_more_pis ty_he ->
540 aux metasenv subst args_so_far he ty_he
541 (NCic.Implicit `Term :: NCic.Implicit `Vector :: tl)
543 Uncertain msg | RefineFailure msg -> raise (wrap_exc msg exc))
544 | RefineFailure msg when not (has_some_more_pis ty_he) ->
545 (* instantiating the head could change the has_some_more_pis flag *)
546 raise (Uncertain msg))
548 match NCicReduction.whd ~subst context ty_he with
550 let metasenv, subst, arg, _ =
551 typeof rdb ~localise metasenv subst context arg (Some s) in
552 let t = NCicSubstitution.subst ~avoid_beta_redexes:true arg t in
553 aux metasenv subst (arg :: args_so_far) he t tl
555 | C.Appl (C.Meta _ :: _) as t ->
556 let metasenv, subst, arg, ty_arg =
557 typeof rdb ~localise metasenv subst context arg None in
558 let name = guess_name subst context ty_arg in
559 let metasenv, _, meta, _ =
560 NCicMetaSubst.mk_meta metasenv
561 ((name,C.Decl ty_arg) :: context) `IsType
563 let flex_prod = C.Prod (name, ty_arg, meta) in
564 (* next line grants that ty_args is a type *)
565 let metasenv,subst, flex_prod, _ =
566 typeof rdb ~localise metasenv subst context flex_prod None in
568 pp (lazy ( "UNIFICATION in CTX:\n"^
569 NCicPp.ppcontext ~metasenv ~subst context
571 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
572 NCicPp.ppterm ~metasenv ~subst ~context flex_prod ^ "\n"));
574 let metasenv, subst =
575 try NCicUnification.unify rdb metasenv subst context t flex_prod
576 with exc -> raise (wrap_exc (lazy (localise orig_he, Printf.sprintf
577 ("The term %s has an inferred type %s, but is applied to the" ^^
578 " argument %s of type %s")
579 (NCicPp.ppterm ~metasenv ~subst ~context he)
580 (NCicPp.ppterm ~metasenv ~subst ~context t)
581 (NCicPp.ppterm ~metasenv ~subst ~context arg)
582 (NCicPp.ppterm ~metasenv ~subst ~context ty_arg)))
584 | NCicUnification.UnificationFailure m ->
585 NCicUnification.Uncertain m
587 (* XXX coerce to funclass *)
589 let meta = NCicSubstitution.subst ~avoid_beta_redexes:true arg meta in
590 aux metasenv subst (arg :: args_so_far) he meta tl
591 | C.Match (_,_,C.Meta _,_)
592 | C.Match (_,_,C.Appl (C.Meta _ :: _),_)
593 | C.Appl (C.Const (NReference.Ref (_, NReference.Fix _)) :: _) ->
594 raise (Uncertain (lazy (localise orig_he, Printf.sprintf
595 ("The term %s is here applied to %d arguments but expects " ^^
596 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
597 (List.length args) (List.length args_so_far))))
599 let metasenv, subst, newhead, newheadty =
600 try_coercions rdb ~localise metasenv subst context
601 (NCicUntrusted.mk_appl he (List.rev args_so_far)) orig_he ty
602 (NCic.Prod ("_",NCic.Implicit `Term,NCic.Implicit `Term))
604 (RefineFailure (lazy (localise orig_he, Printf.sprintf
605 ("The term %s is here applied to %d arguments but expects " ^^
606 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
607 (List.length args) (List.length args_so_far))))
609 aux metasenv subst [] newhead newheadty (arg :: tl)
610 (*D*)in outside true; rc with exc -> outside false; raise exc
612 (* We need to reverse the order of the new created metas since they
613 are pushed on top of the metasenv in the wrong order *)
614 let highest_meta = NCicMetaSubst.maxmeta () in
615 let metasenv, subst, newhead, newheadty =
616 aux metasenv subst [] he ty_he args in
617 let metasenv_old,metasenv_new =
618 List.partition (fun (i,_) -> i <= highest_meta) metasenv
620 (List.rev metasenv_new) @ metasenv_old, subst, newhead, newheadty
621 (*D*)in outside true; rc with exc -> outside false; raise exc
624 let rec first f l1 l2 =
626 | x1::tl1, x2::tl2 ->
627 (try f x1 x2 with Not_found -> first f tl1 tl2)
628 | _ -> raise Not_found
631 let rec find add dt t =
636 | C.Meta (_,(_,C.Ctx dl)), C.Meta (_,(_,C.Ctx l))
637 | C.Appl dl,C.Appl l -> dl,l
638 | C.Lambda (_,ds,dt), C.Lambda (_,s,t)
639 | C.Prod (_,ds,dt), C.Prod (_,s,t) -> [ds;dt],[s;t]
640 | C.LetIn (_,ds,db,dt), C.LetIn (_,s,b,t) -> [ds;db;dt],[s;b;t]
641 | C.Match (_,dot,dt,dl), C.Match (_,ot,t,l) -> (dot::dt::dl),(ot::t::l)
642 | _ -> raise Not_found
644 first (find add) dl l
647 let relocalise old_localise dt t add =
649 (try find add dt t with Not_found -> assert false)
652 let undebruijnate inductive ref t rev_fl =
653 NCicSubstitution.psubst (fun x -> x)
654 (List.rev (HExtlib.list_mapi
655 (fun (_,_,rno,_,_,_) i ->
657 (if inductive then NReference.mk_fix i rno ref
658 else NReference.mk_cofix i ref))
665 rdb ?(localise=fun _ -> Stdpp.dummy_loc) (uri,height,metasenv,subst,obj)
667 let check_type metasenv subst context (ty as orig_ty) = (* XXX fattorizza *)
668 let metasenv, subst, ty, sort =
669 typeof rdb ~localise metasenv subst context ty None
671 let metasenv, subst, ty, sort =
672 force_to_sort rdb metasenv subst context ty orig_ty localise sort
674 metasenv, subst, ty, sort
677 | C.Constant (relevance, name, bo, ty , attr) ->
678 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
679 let metasenv, subst, bo, ty, height =
682 let metasenv, subst, bo, ty =
683 typeof rdb ~localise metasenv subst [] bo (Some ty) in
684 let height = (* XXX recalculate *) height in
685 metasenv, subst, Some bo, ty, height
686 | None -> metasenv, subst, None, ty, 0
688 uri, height, metasenv, subst,
689 C.Constant (relevance, name, bo, ty, attr)
690 | C.Fixpoint (inductive, fl, attr) ->
691 let len = List.length fl in
692 let types, metasenv, subst, rev_fl =
694 (fun (types, metasenv, subst, fl) (relevance,name,k,ty,bo) ->
695 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
696 let dbo = NCicTypeChecker.debruijn uri len [] ~subst bo in
697 let localise = relocalise localise dbo bo in
698 (name,C.Decl ty)::types,
699 metasenv, subst, (relevance,name,k,ty,dbo,localise)::fl
700 ) ([], metasenv, subst, []) fl (* XXX kl rimosso nel nucleo *)
702 let metasenv, subst, fl =
704 (fun (metasenv,subst,fl) (relevance,name,k,ty,dbo,localise) ->
705 let metasenv, subst, dbo, ty =
706 typeof rdb ~localise metasenv subst types dbo (Some ty)
708 metasenv, subst, (relevance,name,k,ty,dbo)::fl)
709 (metasenv, subst, []) rev_fl
711 let height = (* XXX recalculate *) height in
714 (fun (relevance,name,k,ty,dbo) ->
716 undebruijnate inductive
717 (NReference.reference_of_spec uri
718 (if inductive then NReference.Fix (0,k,0)
719 else NReference.CoFix 0)) dbo rev_fl
721 relevance,name,k,ty,bo)
724 uri, height, metasenv, subst,
725 C.Fixpoint (inductive, fl, attr)
726 | C.Inductive (ind, leftno, itl, attr) ->
727 let len = List.length itl in
728 let metasenv,subst,rev_itl,tys =
730 (fun (metasenv,subst,res,ctx) (relevance,n,ty,cl) ->
731 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
732 metasenv,subst,(relevance,n,ty,cl)::res,(n,NCic.Decl ty)::ctx
733 ) (metasenv,subst,[],[]) itl in
734 let metasenv,subst,itl,_ =
736 (fun (metasenv,subst,res,i) (it_relev,n,ty,cl) ->
737 let context,ty_sort = NCicReduction.split_prods ~subst [] ~-1 ty in
738 let sx_context_ty_rev,_= HExtlib.split_nth leftno (List.rev context) in
739 let metasenv,subst,cl =
741 (fun (k_relev,n,te) (metasenv,subst,res) ->
743 try snd (HExtlib.split_nth leftno k_relev)
744 with Failure _ -> k_relev in
745 let te = NCicTypeChecker.debruijn uri len [] ~subst te in
746 let metasenv, subst, te, _ = check_type metasenv subst tys te in
747 let context,te = NCicReduction.split_prods ~subst tys leftno te in
748 let _,chopped_context_rev =
749 HExtlib.split_nth (List.length tys) (List.rev context) in
750 let sx_context_te_rev,_ =
751 HExtlib.split_nth leftno chopped_context_rev in
752 let metasenv,subst,_ =
755 (fun (metasenv,subst,context) item1 item2 ->
756 let (metasenv,subst),convertible =
758 match item1,item2 with
759 (n1,C.Decl ty1),(n2,C.Decl ty2) ->
761 NCicUnification.unify rdb ~test_eq_only:true metasenv
762 subst context ty1 ty2,true
764 (metasenv,subst),false
765 | (n1,C.Def (bo1,ty1)),(n2,C.Def (bo2,ty2)) ->
768 NCicUnification.unify rdb ~test_eq_only:true metasenv
769 subst context ty1 ty2
771 NCicUnification.unify rdb ~test_eq_only:true metasenv
772 subst context bo1 bo2,true
774 (metasenv,subst),false
775 | _,_ -> (metasenv,subst),false
777 | NCicUnification.Uncertain _
778 | NCicUnification.UnificationFailure _ ->
779 (metasenv,subst),false
784 | _,C.Def (b,_) -> b in
785 if not convertible then
786 raise (RefineFailure (lazy (localise term2,
787 ("Mismatch between the left parameters of the constructor " ^
788 "and those of its inductive type"))))
790 metasenv,subst,item1::context
791 ) (metasenv,subst,tys) sx_context_ty_rev sx_context_te_rev
792 with Invalid_argument "List.fold_left2" -> assert false in
793 let con_sort= NCicTypeChecker.typeof ~subst ~metasenv context te in
795 NCicReduction.whd ~subst context con_sort,
796 NCicReduction.whd ~subst [] ty_sort
798 (C.Sort (C.Type u1) as s1), (C.Sort (C.Type u2) as s2) ->
799 if not (NCicEnvironment.universe_leq u1 u2) then
802 (lazy(localise te, "The type " ^
803 NCicPp.ppterm ~metasenv ~subst ~context s1 ^
804 " of the constructor is not included in the inductive"^
806 NCicPp.ppterm ~metasenv ~subst ~context s2)))
807 | C.Sort _, C.Sort C.Prop
808 | C.Sort _, C.Sort C.Type _ -> ()
813 "Wrong constructor or inductive arity shape"))));
814 (* let's check also the positivity conditions *)
817 (NCicTypeChecker.are_all_occurrences_positive
818 ~subst context uri leftno (i+leftno) leftno (len+leftno) te)
823 "Non positive occurence in " ^
824 NCicPp.ppterm ~metasenv ~subst ~context te)))
826 let relsno = List.length itl + leftno in
828 NCicSubstitution.psubst
833 NCic.Const (NReference.reference_of_spec uri
834 (NReference.Ind (ind,relsno - i,leftno))))
835 (HExtlib.list_seq 1 (relsno+1))
839 (fun (name,decl) te ->
841 NCic.Decl ty -> NCic.Prod (name,ty,te)
842 | NCic.Def (bo,ty) -> NCic.LetIn (name,ty,bo,te)
843 ) sx_context_te_rev te
845 metasenv,subst,(k_relev,n,te)::res
846 ) cl (metasenv,subst,[])
848 metasenv,subst,(it_relev,n,ty,cl)::res,i+1
849 ) (metasenv,subst,[],1) rev_itl
851 uri, height, metasenv, subst, C.Inductive (ind, leftno, itl, attr)
854 (* vim:set foldmethod=marker: *)