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
22 let inside c = indent := !indent ^ String.make 1 c;;
23 let outside () = indent := String.sub !indent 0 (String.length !indent -1);;
27 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
32 let wrap_exc msg = function
33 | NCicUnification.Uncertain _ -> Uncertain msg
34 | NCicUnification.UnificationFailure _ -> RefineFailure msg
35 | NCicTypeChecker.TypeCheckerFailure _ -> RefineFailure msg
39 let exp_implicit ~localise metasenv context expty t =
40 let foo x = match expty with Some t -> `WithType t | None -> x in
42 | `Closed -> NCicMetaSubst.mk_meta metasenv [] (foo `Term)
43 | `Type -> NCicMetaSubst.mk_meta metasenv context (foo `Type)
44 | `Term -> NCicMetaSubst.mk_meta metasenv context (foo `Term)
46 NCicMetaSubst.mk_meta ~attrs:[`Name s] metasenv context (foo `Term)
48 raise (RefineFailure (lazy (localise t, "A vector of implicit terms " ^
49 "can only be used in argument position")))
53 let check_allowed_sort_elimination rdb localise r orig =
56 | C.Appl l -> C.Appl (l @ [arg])
57 | t -> C.Appl [t;arg] in
58 (* ctx, ind_type @ lefts, sort_of_ind_ty@lefts, outsort *)
59 let rec aux metasenv subst context ind arity1 arity2 =
60 (*D*)inside 'S'; try let rc =
61 let arity1 = NCicReduction.whd ~subst context arity1 in
62 pp (lazy(NCicPp.ppterm ~subst ~metasenv ~context arity1 ^ " elimsto " ^
63 NCicPp.ppterm ~subst ~metasenv ~context arity2 ^ "\nMENV:\n"^
64 NCicPp.ppmetasenv ~subst metasenv));
66 | C.Prod (name,so1,de1) (* , t ==?== C.Prod _ *) ->
67 let metasenv, _, meta, _ =
68 NCicMetaSubst.mk_meta metasenv ((name,C.Decl so1)::context) `Type
71 try NCicUnification.unify rdb metasenv subst context
72 arity2 (C.Prod (name, so1, meta))
73 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
74 "expected %s, found %s" (* XXX localizzare meglio *)
75 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod (name, so1, meta)))
76 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
78 aux metasenv subst ((name, C.Decl so1)::context)
79 (mkapp (NCicSubstitution.lift 1 ind) (C.Rel 1)) de1 meta
80 | C.Sort _ (* , t ==?== C.Prod _ *) ->
81 let metasenv, _, meta, _ = NCicMetaSubst.mk_meta metasenv [] `Type in
83 try NCicUnification.unify rdb metasenv subst context
84 arity2 (C.Prod ("_", ind, meta))
85 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
86 "expected %s, found %s" (* XXX localizzare meglio *)
87 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod ("_", ind, meta)))
88 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
90 (try NCicTypeChecker.check_allowed_sort_elimination
91 ~metasenv ~subst r context ind arity1 arity2;
93 with exc -> raise (wrap_exc (lazy (localise orig,
94 "Sort elimination not allowed ")) exc))
96 (*D*)in outside(); rc with exc -> outside (); raise exc
102 ?(localise=fun _ -> Stdpp.dummy_loc)
103 metasenv subst context term expty
105 let force_ty skip_lambda skip_appl metasenv subst context orig t infty expty =
106 (*D*)inside 'F'; try let rc =
110 | C.Implicit _ -> assert false
111 | C.Lambda _ when skip_lambda -> metasenv, subst, t, expty
112 | C.Appl _ when skip_appl -> metasenv, subst, t, expty
114 pp (lazy ("forcing infty=expty: "^
115 (NCicPp.ppterm ~metasenv ~subst ~context infty) ^ " === " ^
116 (NCicPp.ppterm ~metasenv ~subst:[] ~context expty)));
118 let metasenv, subst =
119 (*D*)inside 'U'; try let rc =
120 NCicUnification.unify rdb metasenv subst context infty expty
121 (*D*)in outside(); rc with exc -> outside (); raise exc
123 metasenv, subst, t, expty
125 | NCicUnification.Uncertain _
126 | NCicUnification.UnificationFailure _ as exc ->
127 try_coercions rdb ~localise
128 metasenv subst context t orig infty expty true exc)
129 | None -> metasenv, subst, t, infty
130 (*D*)in outside(); rc with exc -> outside (); raise exc
132 let rec typeof_aux metasenv subst context expty =
134 (*D*)inside 'R'; try let rc =
135 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t ^ " ::exp:: " ^
136 match expty with None -> "None" | Some e ->
137 NCicPp.ppterm ~metasenv ~subst ~context e));
138 let metasenv, subst, t, infty =
143 match List.nth context (n - 1) with
144 | (_,C.Decl ty) -> NCicSubstitution.lift n ty
145 | (_,C.Def (_,ty)) -> NCicSubstitution.lift n ty
147 raise (RefineFailure (lazy (localise t,"unbound variable"))))
149 metasenv, subst, t, infty
151 (try metasenv, subst, t, C.Sort (NCicEnvironment.typeof_sort s)
153 | NCicEnvironment.UntypableSort msg ->
154 raise (RefineFailure (lazy (localise t, Lazy.force msg)))
155 | NCicEnvironment.AssertFailure msg -> raise (AssertFailure msg))
156 | C.Implicit infos ->
157 let metasenv,_,t,ty =
158 exp_implicit ~localise metasenv context expty t infos
160 metasenv, subst, t, ty
161 | C.Meta (n,l) as t ->
164 let _,_,_,ty = NCicUtils.lookup_subst n subst in ty
165 with NCicUtils.Subst_not_found _ -> try
166 let _,_,ty = NCicUtils.lookup_meta n metasenv in
167 match ty with C.Implicit _ ->
168 prerr_endline (string_of_int n);
169 prerr_endline (NCicPp.ppmetasenv ~subst metasenv);
170 prerr_endline (NCicPp.ppsubst ~metasenv subst);
171 assert false | _ -> ty
172 with NCicUtils.Meta_not_found _ ->
173 raise (AssertFailure (lazy (Printf.sprintf
174 "%s not found" (NCicPp.ppterm ~subst ~metasenv ~context t))))
176 metasenv, subst, t, NCicSubstitution.subst_meta l ty
178 metasenv, subst, t, NCicTypeChecker.typeof ~subst ~metasenv context t
179 | C.Prod (name,(s as orig_s),(t as orig_t)) ->
180 let metasenv, subst, s, s1 = typeof_aux metasenv subst context None s in
181 let metasenv, subst, s, s1 =
183 metasenv subst context s orig_s localise s1 in
184 let context1 = (name,(C.Decl s))::context in
185 let metasenv, subst, t, s2 = typeof_aux metasenv subst context1 None t in
186 let metasenv, subst, t, s2 =
188 metasenv subst context1 t orig_t localise s2 in
189 let metasenv, subst, s, t, ty =
190 sort_of_prod localise metasenv subst
191 context orig_s orig_t (name,s) t (s1,s2)
193 metasenv, subst, NCic.Prod(name,s,t), ty
194 | C.Lambda (n,(s as orig_s),t) as orig ->
195 let exp_s, exp_ty_t, force_after =
197 | None -> None, None, false
199 match NCicReduction.whd ~subst context expty with
200 | C.Prod (_,s,t) -> Some s, Some t, false
201 | _ -> None, None, true
203 let metasenv, subst, s, ty_s =
204 typeof_aux metasenv subst context None s in
205 let metasenv, subst, s, _ =
207 metasenv subst context s orig_s localise ty_s in
208 let (metasenv,subst), exp_ty_t =
212 pp(lazy("Force source to: "^NCicPp.ppterm ~metasenv ~subst
214 NCicUnification.unify rdb metasenv subst context s exp_s,exp_ty_t
215 with exc -> raise (wrap_exc (lazy (localise orig_s, Printf.sprintf
216 "Source type %s was expected to be %s" (NCicPp.ppterm ~metasenv
217 ~subst ~context s) (NCicPp.ppterm ~metasenv ~subst ~context
219 | None -> (metasenv, subst), None
221 let context_for_t = (n,C.Decl s) :: context in
222 let metasenv, subst, t, ty_t =
223 typeof_aux metasenv subst context_for_t exp_ty_t t
226 force_ty false true metasenv subst context orig
227 (C.Lambda(n,s,t)) (C.Prod (n,s,ty_t)) expty
229 metasenv, subst, C.Lambda(n,s,t), C.Prod (n,s,ty_t)
230 | C.LetIn (n,(ty as orig_ty),t,bo) ->
231 let metasenv, subst, ty, ty_ty =
232 typeof_aux metasenv subst context None ty in
233 let metasenv, subst, ty, _ =
235 metasenv subst context ty orig_ty localise ty_ty in
236 let metasenv, subst, t, _ =
237 typeof_aux metasenv subst context (Some ty) t in
238 let context1 = (n, C.Def (t,ty)) :: context in
239 let metasenv, subst, expty1 =
241 | None -> metasenv, subst, None
244 NCicUnification.delift_type_wrt_terms
245 rdb metasenv subst context x [t]
249 let metasenv, subst, bo, bo_ty =
250 typeof_aux metasenv subst context1 expty1 bo
252 let bo_ty = NCicSubstitution.subst ~avoid_beta_redexes:true t bo_ty in
253 metasenv, subst, C.LetIn (n, ty, t, bo), bo_ty
254 | C.Appl ((he as orig_he)::(_::_ as args)) ->
255 let upto = match orig_he with C.Meta _ -> List.length args | _ -> 0 in
257 if upto > 0 then NCicReduction.head_beta_reduce ~upto t else t
260 let metasenv, subst, he, ty_he =
261 typeof_aux metasenv subst context None he in
262 let metasenv, subst, t, ty =
263 eat_prods rdb ~localise force_ty metasenv subst context expty t
264 orig_he he ty_he args in
265 metasenv, subst, hbr t, ty
267 if args = [C.Implicit `Vector] && expty <> None then
268 (* we try here to expand the vector a 0 implicits, but we use
269 * the expected type *)
271 let metasenv, subst, he, ty_he =
272 typeof_aux metasenv subst context expty he in
273 metasenv, subst, hbr he, ty_he
274 with Uncertain _ | RefineFailure _ -> refine_appl ()
276 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
277 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,
278 outtype,(term as orig_term),pl) as orig ->
279 let _, leftno, itl, _, _ = NCicEnvironment.get_checked_indtys r in
280 let _, _, arity, cl = List.nth itl tyno in
281 let constructorsno = List.length cl in
282 let _, metasenv, args =
283 NCicMetaSubst.saturate metasenv subst context arity 0 in
284 let ind = if args = [] then C.Const r else C.Appl (C.Const r::args) in
285 let metasenv, subst, term, _ =
286 typeof_aux metasenv subst context (Some ind) term in
287 let parameters, arguments = HExtlib.split_nth leftno args in
290 | C.Implicit _ as ot ->
291 let rec aux = function
292 | [] -> NCic.Lambda ("_",NCic.Implicit `Type,ot)
293 | _::tl -> NCic.Lambda ("_",NCic.Implicit `Type,aux tl)
298 let metasenv, subst, outtype, outsort =
299 typeof_aux metasenv subst context None outtype in
301 (* next lines are to do a subst-expansion of the outtype, so
302 that when it becomes a beta-abstraction, the beta-redex is
303 fired during substitution *)
304 (*CSC: this is instantiate! should we move it from tactics to the
305 refiner? I think so! *)
306 let metasenv,metanoouttype,newouttype,metaoutsort =
307 NCicMetaSubst.mk_meta metasenv context `Term in
309 NCicUnification.unify rdb metasenv subst context outsort metaoutsort in
311 List.filter (function (j,_) -> j <> metanoouttype) metasenv in
313 (metanoouttype,([`Name "outtype"],context,outtype,metaoutsort))::subst in
314 let outtype = newouttype in
316 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
318 if parameters = [] then C.Const r
319 else C.Appl ((C.Const r)::parameters) in
320 let metasenv, subst, ind, ind_ty =
321 typeof_aux metasenv subst context None ind in
322 let metasenv, subst =
323 check_allowed_sort_elimination rdb localise r orig_term metasenv subst
324 context ind ind_ty outsort
326 (* let's check if the type of branches are right *)
327 if List.length pl <> constructorsno then
328 raise (RefineFailure (lazy (localise orig,
329 "Wrong number of cases in a match")));
331 let metasenv, subst =
333 | None -> metasenv, subst
335 NCicUnification.unify rdb metasenv subst context resty expty
338 let _, metasenv, subst, pl =
340 (fun p (j, metasenv, subst, branches) ->
342 let cons = Ref.mk_constructor j r in
343 if parameters = [] then C.Const cons
344 else C.Appl (C.Const cons::parameters)
346 let metasenv, subst, cons, ty_cons =
347 typeof_aux metasenv subst context None cons in
349 NCicTypeChecker.type_of_branch
350 ~subst context leftno outtype cons ty_cons in
351 pp (lazy ("TYPEOFBRANCH: " ^
352 NCicPp.ppterm ~metasenv ~subst ~context p ^ " ::inf:: " ^
353 NCicPp.ppterm ~metasenv ~subst ~context ty_branch ));
354 let metasenv, subst, p, _ =
355 typeof_aux metasenv subst context (Some ty_branch) p in
356 j-1, metasenv, subst, p :: branches)
357 pl (List.length pl, metasenv, subst, [])
359 let resty = C.Appl (outtype::arguments@[term]) in
360 let resty = NCicReduction.head_beta_reduce ~subst resty in
361 metasenv, subst, C.Match (r, outtype, term, pl),resty
362 | C.Match _ -> assert false
364 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t ^ " ::inf:: "^
365 NCicPp.ppterm ~metasenv ~subst ~context infty ));
366 force_ty true true metasenv subst context orig t infty expty
367 (*D*)in outside(); rc with exc -> outside (); raise exc
369 typeof_aux metasenv subst context expty term
371 and try_coercions rdb
373 metasenv subst context t orig_t infty expty perform_unification exc
375 (* we try with a coercion *)
376 let rec first exc = function
378 raise (wrap_exc (lazy (localise orig_t, Printf.sprintf
379 "The term %s has type %s but is here used with type %s"
380 (NCicPp.ppterm ~metasenv ~subst ~context t)
381 (NCicPp.ppterm ~metasenv ~subst ~context infty)
382 (NCicPp.ppterm ~metasenv ~subst ~context expty))) exc)
383 | (_,metasenv, newterm, newtype, meta)::tl ->
385 pp (lazy("K=" ^ NCicPp.ppterm ~metasenv ~subst ~context newterm));
386 pp (lazy ( "UNIFICATION in CTX:\n"^
387 NCicPp.ppcontext ~metasenv ~subst context
389 NCicPp.ppmetasenv metasenv ~subst
391 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
392 NCicPp.ppterm ~metasenv ~subst ~context meta ^ "\n"));
393 let metasenv, subst =
394 NCicUnification.unify rdb metasenv subst context t meta
396 pp (lazy ( "UNIFICATION in CTX:\n"^
397 NCicPp.ppcontext ~metasenv ~subst context
399 NCicPp.ppmetasenv metasenv ~subst
401 NCicPp.ppterm ~metasenv ~subst ~context newtype ^ " === " ^
402 NCicPp.ppterm ~metasenv ~subst ~context expty ^ "\n"));
403 let metasenv, subst =
404 if perform_unification then
405 NCicUnification.unify rdb metasenv subst context newtype expty
408 metasenv, subst, newterm, newtype
410 | NCicUnification.UnificationFailure _ -> first exc tl
411 | NCicUnification.Uncertain _ as exc -> first exc tl
413 pp(lazy("try_coercion " ^
414 NCicPp.ppterm ~metasenv ~subst ~context infty ^ " |---> " ^
415 NCicPp.ppterm ~metasenv ~subst ~context expty));
417 (NCicCoercion.look_for_coercion
418 rdb metasenv subst context infty expty)
420 and force_to_sort rdb metasenv subst context t orig_t localise ty =
421 match NCicReduction.whd ~subst context ty with
422 | C.Meta (_,(0,(C.Irl 0 | C.Ctx []))) as ty ->
423 metasenv, subst, t, ty
424 | C.Meta (_i,(_,(C.Irl 0 | C.Ctx []))) -> assert false (*CSC: ???
425 metasenv, subst, t, C.Meta(i,(0,C.Irl 0)) *)
426 | C.Meta (i,(_,lc)) ->
427 let len = match lc with C.Irl len->len | C.Ctx l->List.length l in
428 let metasenv, subst, newmeta =
430 NCicMetaSubst.restrict metasenv subst i (HExtlib.list_seq 1 (len+1))
431 else metasenv, subst, i
433 metasenv, subst, t, C.Meta (newmeta,(0,C.Irl 0))
434 | C.Sort _ as ty -> metasenv, subst, t, ty
436 try_coercions rdb ~localise metasenv subst context
437 t orig_t ty (NCic.Sort (NCic.Type NCicEnvironment.type0)) false
438 (Uncertain (lazy (localise orig_t,
439 "The type of " ^ NCicPp.ppterm ~metasenv ~subst ~context t
440 ^ " is not a sort: " ^ NCicPp.ppterm ~metasenv ~subst ~context ty)))
443 localise metasenv subst context orig_s orig_t (name,s) t (t1, t2)
445 (* force to sort is done in the Prod case in typeof *)
447 | C.Sort _, C.Sort C.Prop -> metasenv, subst, s, t, t2
448 | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
449 metasenv, subst, s, t, C.Sort (C.Type (NCicEnvironment.max u1 u2))
450 | C.Sort C.Prop,C.Sort (C.Type _) -> metasenv, subst, s, t, t2
452 | C.Meta _, C.Meta (_,(_,_))
453 | C.Sort _, C.Meta (_,(_,_)) -> metasenv, subst, s, t, t2
454 | x, (C.Sort _ | C.Meta _) | _, x ->
455 let y, context, orig =
456 if x == t1 then s, context, orig_s
457 else t, ((name,C.Decl s)::context), orig_t
459 raise (RefineFailure (lazy (localise orig,Printf.sprintf
460 "%s is expected to be a type, but its type is %s that is not a sort"
461 (NCicPp.ppterm ~subst ~metasenv ~context y)
462 (NCicPp.ppterm ~subst ~metasenv ~context x))))
464 and guess_name subst ctx ty =
465 let aux initial = "#" ^ String.make 1 initial in
467 | C.Const (NReference.Ref (u,_))
468 | C.Appl (C.Const (NReference.Ref (u,_)) :: _) ->
469 aux (String.sub (NUri.name_of_uri u) 0 1).[0]
470 | C.Prod _ -> aux 'f'
473 let _,_,t,_ = NCicUtils.lookup_subst n subst in
474 guess_name subst ctx (NCicSubstitution.subst_meta lc t)
475 with NCicUtils.Subst_not_found _ -> aux 'M')
478 and eat_prods rdb ~localise force_ty metasenv subst context expty orig_t orig_he he ty_he args =
479 (*D*)inside 'E'; try let rc =
480 let rec aux metasenv subst args_so_far he ty_he = function
482 let res = NCicUntrusted.mk_appl he (List.rev args_so_far) in
483 pp(lazy("FORCE FINAL APPL: " ^
484 NCicPp.ppterm ~metasenv ~subst ~context res ^
485 " of type " ^ NCicPp.ppterm ~metasenv ~subst ~context ty_he
486 ^ " to type " ^ match expty with None -> "None" | Some x ->
487 NCicPp.ppterm ~metasenv ~subst ~context x));
488 (* whatever the term is, we force the type. in case of ((Lambda..) ?...)
489 * the application may also be a lambda! *)
490 force_ty false false metasenv subst context orig_t res ty_he expty
491 | NCic.Implicit `Vector::tl ->
492 let has_some_more_pis x =
493 match NCicReduction.whd ~subst context x with
494 | NCic.Meta _ | NCic.Appl (NCic.Meta _::_) -> false
498 aux metasenv subst args_so_far he ty_he tl
501 | RefineFailure _ as exc when has_some_more_pis ty_he ->
503 aux metasenv subst args_so_far he ty_he
504 (NCic.Implicit `Term :: NCic.Implicit `Vector :: tl)
506 Uncertain msg | RefineFailure msg -> raise (wrap_exc msg exc)))
508 match NCicReduction.whd ~subst context ty_he with
510 let metasenv, subst, arg, _ =
511 typeof rdb ~localise metasenv subst context arg (Some s) in
512 let t = NCicSubstitution.subst ~avoid_beta_redexes:true arg t in
513 aux metasenv subst (arg :: args_so_far) he t tl
515 | C.Appl (C.Meta _ :: _) as t ->
516 let metasenv, subst, arg, ty_arg =
517 typeof rdb ~localise metasenv subst context arg None in
518 let name = guess_name subst context ty_arg in
519 let metasenv, _, meta, _ =
520 NCicMetaSubst.mk_meta metasenv
521 ((name,C.Decl ty_arg) :: context) `Type
523 let flex_prod = C.Prod (name, ty_arg, meta) in
524 (* next line grants that ty_args is a type *)
525 let metasenv,subst, flex_prod, _ =
526 typeof rdb ~localise metasenv subst context flex_prod None in
528 pp (lazy ( "UNIFICATION in CTX:\n"^
529 NCicPp.ppcontext ~metasenv ~subst context
531 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
532 NCicPp.ppterm ~metasenv ~subst ~context flex_prod ^ "\n"));
534 let metasenv, subst =
535 try NCicUnification.unify rdb metasenv subst context t flex_prod
536 with exc -> raise (wrap_exc (lazy (localise orig_he, Printf.sprintf
537 ("The term %s has an inferred type %s, but is applied to the" ^^
538 " argument %s of type %s")
539 (NCicPp.ppterm ~metasenv ~subst ~context he)
540 (NCicPp.ppterm ~metasenv ~subst ~context t)
541 (NCicPp.ppterm ~metasenv ~subst ~context arg)
542 (NCicPp.ppterm ~metasenv ~subst ~context ty_arg)))
544 | NCicUnification.UnificationFailure m ->
545 NCicUnification.Uncertain m
547 (* XXX coerce to funclass *)
549 let meta = NCicSubstitution.subst ~avoid_beta_redexes:true arg meta in
550 aux metasenv subst (arg :: args_so_far) he meta tl
551 | C.Match (_,_,C.Meta _,_)
552 | C.Match (_,_,C.Appl (C.Meta _ :: _),_)
553 | C.Appl (C.Const (NReference.Ref (_, NReference.Fix _)) :: _) ->
554 raise (Uncertain (lazy (localise orig_he, Printf.sprintf
555 ("The term %s is here applied to %d arguments but expects " ^^
556 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
557 (List.length args) (List.length args_so_far))))
559 let metasenv, subst, newhead, newheadty =
560 try_coercions rdb ~localise metasenv subst context
561 (NCicUntrusted.mk_appl he (List.rev args_so_far)) orig_he ty
562 (NCic.Prod ("_",NCic.Implicit `Term,NCic.Implicit `Term))
564 (RefineFailure (lazy (localise orig_he, Printf.sprintf
565 ("The term %s is here applied to %d arguments but expects " ^^
566 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
567 (List.length args) (List.length args_so_far))))
569 aux metasenv subst [] newhead newheadty (arg :: tl)
571 (* We need to reverse the order of the new created metas since they
572 are pushed on top of the metasenv in the wrong order *)
573 let highest_meta = NCicMetaSubst.maxmeta () in
574 let metasenv, subst, newhead, newheadty =
575 aux metasenv subst [] he ty_he args in
576 let metasenv_old,metasenv_new =
577 List.partition (fun (i,_) -> i <= highest_meta) metasenv
579 (List.rev metasenv_new) @ metasenv_old, subst, newhead, newheadty
580 (*D*)in outside(); rc with exc -> outside (); raise exc
583 let rec first f l1 l2 =
585 | x1::tl1, x2::tl2 ->
586 (try f x1 x2 with Not_found -> first f tl1 tl2)
587 | _ -> raise Not_found
590 let rec find add dt t =
595 | C.Meta (_,(_,C.Ctx dl)), C.Meta (_,(_,C.Ctx l))
596 | C.Appl dl,C.Appl l -> dl,l
597 | C.Lambda (_,ds,dt), C.Lambda (_,s,t)
598 | C.Prod (_,ds,dt), C.Prod (_,s,t) -> [ds;dt],[s;t]
599 | C.LetIn (_,ds,db,dt), C.LetIn (_,s,b,t) -> [ds;db;dt],[s;b;t]
600 | C.Match (_,dot,dt,dl), C.Match (_,ot,t,l) -> (dot::dt::dl),(ot::t::l)
601 | _ -> raise Not_found
603 first (find add) dl l
606 let relocalise old_localise dt t add =
608 (try find add dt t with Not_found -> assert false)
611 let undebruijnate inductive ref t rev_fl =
612 NCicSubstitution.psubst (fun x -> x)
613 (List.rev (HExtlib.list_mapi
614 (fun (_,_,rno,_,_,_) i ->
616 (if inductive then NReference.mk_fix i rno ref
617 else NReference.mk_cofix i ref))
624 rdb ?(localise=fun _ -> Stdpp.dummy_loc) (uri,height,metasenv,subst,obj)
626 let check_type metasenv subst context (ty as orig_ty) = (* XXX fattorizza *)
627 let metasenv, subst, ty, sort =
628 typeof rdb ~localise metasenv subst context ty None
630 let metasenv, subst, ty, sort =
631 force_to_sort rdb metasenv subst context ty orig_ty localise sort
633 metasenv, subst, ty, sort
636 | C.Constant (relevance, name, bo, ty , attr) ->
637 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
638 let metasenv, subst, bo, ty, height =
641 let metasenv, subst, bo, ty =
642 typeof rdb ~localise metasenv subst [] bo (Some ty) in
643 let height = (* XXX recalculate *) height in
644 metasenv, subst, Some bo, ty, height
645 | None -> metasenv, subst, None, ty, 0
647 uri, height, metasenv, subst,
648 C.Constant (relevance, name, bo, ty, attr)
649 | C.Fixpoint (inductive, fl, attr) ->
650 let len = List.length fl in
651 let types, metasenv, subst, rev_fl =
653 (fun (types, metasenv, subst, fl) (relevance,name,k,ty,bo) ->
654 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
655 let dbo = NCicTypeChecker.debruijn uri len [] ~subst bo in
656 let localise = relocalise localise dbo bo in
657 (name,C.Decl ty)::types,
658 metasenv, subst, (relevance,name,k,ty,dbo,localise)::fl
659 ) ([], metasenv, subst, []) fl (* XXX kl rimosso nel nucleo *)
661 let metasenv, subst, fl =
663 (fun (metasenv,subst,fl) (relevance,name,k,ty,dbo,localise) ->
664 let metasenv, subst, dbo, ty =
665 typeof rdb ~localise metasenv subst types dbo (Some ty)
667 metasenv, subst, (relevance,name,k,ty,dbo)::fl)
668 (metasenv, subst, []) rev_fl
670 let height = (* XXX recalculate *) height in
673 (fun (relevance,name,k,ty,dbo) ->
675 undebruijnate inductive
676 (NReference.reference_of_spec uri
677 (if inductive then NReference.Fix (0,k,0)
678 else NReference.CoFix 0)) dbo rev_fl
680 relevance,name,k,ty,bo)
683 uri, height, metasenv, subst,
684 C.Fixpoint (inductive, fl, attr)
685 | C.Inductive (ind, leftno, itl, attr) ->
686 let len = List.length itl in
687 let metasenv,subst,rev_itl,tys =
689 (fun (metasenv,subst,res,ctx) (relevance,n,ty,cl) ->
690 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
691 metasenv,subst,(relevance,n,ty,cl)::res,(n,NCic.Decl ty)::ctx
692 ) (metasenv,subst,[],[]) itl in
693 let metasenv,subst,itl,_ =
695 (fun (metasenv,subst,res,i) (it_relev,n,ty,cl) ->
696 let context,ty_sort = NCicReduction.split_prods ~subst [] ~-1 ty in
697 let sx_context_ty_rev,_= HExtlib.split_nth leftno (List.rev context) in
698 let metasenv,subst,cl =
700 (fun (k_relev,n,te) (metasenv,subst,res) ->
702 try snd (HExtlib.split_nth leftno k_relev)
703 with Failure _ -> k_relev in
704 let te = NCicTypeChecker.debruijn uri len [] ~subst te in
705 let metasenv, subst, te, _ = check_type metasenv subst tys te in
706 let context,te = NCicReduction.split_prods ~subst tys leftno te in
707 let _,chopped_context_rev =
708 HExtlib.split_nth (List.length tys) (List.rev context) in
709 let sx_context_te_rev,_ =
710 HExtlib.split_nth leftno chopped_context_rev in
711 let metasenv,subst,_ =
714 (fun (metasenv,subst,context) item1 item2 ->
715 let (metasenv,subst),convertible =
717 match item1,item2 with
718 (n1,C.Decl ty1),(n2,C.Decl ty2) ->
720 NCicUnification.unify rdb ~test_eq_only:true metasenv
721 subst context ty1 ty2,true
723 (metasenv,subst),false
724 | (n1,C.Def (bo1,ty1)),(n2,C.Def (bo2,ty2)) ->
727 NCicUnification.unify rdb ~test_eq_only:true metasenv
728 subst context ty1 ty2
730 NCicUnification.unify rdb ~test_eq_only:true metasenv
731 subst context bo1 bo2,true
733 (metasenv,subst),false
734 | _,_ -> (metasenv,subst),false
736 | NCicUnification.Uncertain _
737 | NCicUnification.UnificationFailure _ ->
738 (metasenv,subst),false
743 | _,C.Def (b,_) -> b in
744 if not convertible then
745 raise (RefineFailure (lazy (localise term2,
746 ("Mismatch between the left parameters of the constructor " ^
747 "and those of its inductive type"))))
749 metasenv,subst,item1::context
750 ) (metasenv,subst,tys) sx_context_ty_rev sx_context_te_rev
751 with Invalid_argument "List.fold_left2" -> assert false in
752 let con_sort= NCicTypeChecker.typeof ~subst ~metasenv context te in
754 NCicReduction.whd ~subst context con_sort,
755 NCicReduction.whd ~subst [] ty_sort
757 (C.Sort (C.Type u1) as s1), (C.Sort (C.Type u2) as s2) ->
758 if not (NCicEnvironment.universe_leq u1 u2) then
761 (lazy(localise te, "The type " ^
762 NCicPp.ppterm ~metasenv ~subst ~context s1 ^
763 " of the constructor is not included in the inductive"^
765 NCicPp.ppterm ~metasenv ~subst ~context s2)))
766 | C.Sort _, C.Sort C.Prop
767 | C.Sort _, C.Sort C.Type _ -> ()
772 "Wrong constructor or inductive arity shape"))));
773 (* let's check also the positivity conditions *)
776 (NCicTypeChecker.are_all_occurrences_positive
777 ~subst context uri leftno (i+leftno) leftno (len+leftno) te)
782 "Non positive occurence in " ^
783 NCicPp.ppterm ~metasenv ~subst ~context te)))
785 let relsno = List.length itl + leftno in
787 NCicSubstitution.psubst
792 NCic.Const (NReference.reference_of_spec uri
793 (NReference.Ind (ind,relsno - i,leftno))))
794 (HExtlib.list_seq 1 (relsno+1))
798 (fun (name,decl) te ->
800 NCic.Decl ty -> NCic.Prod (name,ty,te)
801 | NCic.Def (bo,ty) -> NCic.LetIn (name,ty,bo,te)
802 ) sx_context_te_rev te
804 metasenv,subst,(k_relev,n,te)::res
805 ) cl (metasenv,subst,[])
807 metasenv,subst,(it_relev,n,ty,cl)::res,i+1
808 ) (metasenv,subst,[],1) rev_itl
810 uri, height, metasenv, subst, C.Inductive (ind, leftno, itl, attr)
813 (* vim:set foldmethod=marker: *)