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 raise (RefineFailure (lazy (localise t, "A vector of implicit terms " ^
47 "can only be used in argument position")))
51 let check_allowed_sort_elimination rdb localise r orig =
54 | C.Appl l -> C.Appl (l @ [arg])
55 | t -> C.Appl [t;arg] in
56 (* ctx, ind_type @ lefts, sort_of_ind_ty@lefts, outsort *)
57 let rec aux metasenv subst context ind arity1 arity2 =
58 (*D*)inside 'S'; try let rc =
59 let arity1 = NCicReduction.whd ~subst context arity1 in
60 pp (lazy(NCicPp.ppterm ~subst ~metasenv ~context arity1 ^ " elimsto " ^
61 NCicPp.ppterm ~subst ~metasenv ~context arity2 ^ "\nMENV:\n"^
62 NCicPp.ppmetasenv ~subst metasenv));
64 | C.Prod (name,so1,de1) (* , t ==?== C.Prod _ *) ->
65 let metasenv, _, meta, _ =
66 NCicMetaSubst.mk_meta metasenv ((name,C.Decl so1)::context) `Type
69 try NCicUnification.unify rdb metasenv subst context
70 arity2 (C.Prod (name, so1, meta))
71 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
72 "expected %s, found %s" (* XXX localizzare meglio *)
73 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod (name, so1, meta)))
74 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
76 aux metasenv subst ((name, C.Decl so1)::context)
77 (mkapp (NCicSubstitution.lift 1 ind) (C.Rel 1)) de1 meta
78 | C.Sort _ (* , t ==?== C.Prod _ *) ->
79 let metasenv, _, meta, _ = NCicMetaSubst.mk_meta metasenv [] `Type in
81 try NCicUnification.unify rdb metasenv subst context
82 arity2 (C.Prod ("_", ind, meta))
83 with exc -> raise (wrap_exc (lazy (localise orig, Printf.sprintf
84 "expected %s, found %s" (* XXX localizzare meglio *)
85 (NCicPp.ppterm ~subst ~metasenv ~context (C.Prod ("_", ind, meta)))
86 (NCicPp.ppterm ~subst ~metasenv ~context arity2))) exc)
88 (try NCicTypeChecker.check_allowed_sort_elimination
89 ~metasenv ~subst r context ind arity1 arity2;
91 with exc -> raise (wrap_exc (lazy (localise orig,
92 "Sort elimination not allowed ")) exc))
94 (*D*)in outside(); rc with exc -> outside (); raise exc
100 ?(localise=fun _ -> Stdpp.dummy_loc)
101 metasenv subst context term expty
103 let force_ty skip_lambda metasenv subst context orig t infty expty =
104 (*D*)inside 'F'; try let rc =
108 | C.Implicit _ -> assert false
109 | C.Lambda _ when skip_lambda -> metasenv, subst, t, expty
112 (NCicPp.ppterm ~metasenv ~subst ~context infty) ^ " === " ^
113 (NCicPp.ppterm ~metasenv ~subst:[] ~context expty)));
115 let metasenv, subst =
116 NCicUnification.unify rdb metasenv subst context infty expty
118 metasenv, subst, t, expty
120 try_coercions rdb ~localise
121 metasenv subst context t orig infty expty true exc)
122 | None -> metasenv, subst, t, infty
123 (*D*)in outside(); rc with exc -> outside (); raise exc
125 let rec typeof_aux metasenv subst context expty =
127 (*D*)inside 'R'; try let rc =
128 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t));
129 pp (lazy (if expty = None then "NONE" else "SOME"));
130 if (List.exists (fun (i,_) -> i=29) subst) then
131 pp (lazy (NCicPp.ppsubst ~metasenv subst));
132 let metasenv, subst, t, infty =
137 match List.nth context (n - 1) with
138 | (_,C.Decl ty) -> NCicSubstitution.lift n ty
139 | (_,C.Def (_,ty)) -> NCicSubstitution.lift n ty
141 raise (RefineFailure (lazy (localise t,"unbound variable"))))
143 metasenv, subst, t, infty
144 | C.Sort (C.Type [false,u]) -> metasenv,subst,t,(C.Sort (C.Type [true, u]))
145 | C.Sort (C.Type _) ->
146 raise (AssertFailure (lazy ("Cannot type an inferred type: "^
147 NCicPp.ppterm ~subst ~metasenv ~context t)))
148 | C.Sort _ -> metasenv,subst,t,(C.Sort (C.Type NCicEnvironment.type0))
149 | C.Implicit infos ->
150 let metasenv,_,t,ty =
151 exp_implicit ~localise metasenv context expty t infos
153 metasenv, subst, t, ty
154 | C.Meta (n,l) as t ->
157 let _,_,_,ty = NCicUtils.lookup_subst n subst in ty
158 with NCicUtils.Subst_not_found _ -> try
159 let _,_,ty = NCicUtils.lookup_meta n metasenv in
160 match ty with C.Implicit _ ->
161 prerr_endline (string_of_int n);
162 prerr_endline (NCicPp.ppmetasenv ~subst metasenv);
163 prerr_endline (NCicPp.ppsubst ~metasenv subst);
164 assert false | _ -> ty
165 with NCicUtils.Meta_not_found _ ->
166 raise (AssertFailure (lazy (Printf.sprintf
167 "%s not found" (NCicPp.ppterm ~subst ~metasenv ~context t))))
169 metasenv, subst, t, NCicSubstitution.subst_meta l ty
171 metasenv, subst, t, NCicTypeChecker.typeof ~subst ~metasenv context t
172 | C.Prod (name,(s as orig_s),(t as orig_t)) ->
173 let metasenv, subst, s, s1 = typeof_aux metasenv subst context None s in
174 let metasenv, subst, s, s1 =
176 metasenv subst context s orig_s localise s1 in
177 let context1 = (name,(C.Decl s))::context in
178 let metasenv, subst, t, s2 = typeof_aux metasenv subst context1 None t in
179 let metasenv, subst, t, s2 =
181 metasenv subst context1 t orig_t localise s2 in
182 let metasenv, subst, s, t, ty =
183 sort_of_prod localise metasenv subst
184 context orig_s orig_t (name,s) t (s1,s2)
186 metasenv, subst, NCic.Prod(name,s,t), ty
187 | C.Lambda (n,(s as orig_s),t) as orig ->
188 let exp_s, exp_ty_t, force_after =
190 | None -> None, None, false
192 match NCicReduction.whd ~subst context expty with
193 | C.Prod (_,s,t) -> Some s, Some t, false
194 | _ -> None, None, true
196 let metasenv, subst, s, ty_s =
197 typeof_aux metasenv subst context None s in
198 let metasenv, subst, s, _ =
200 metasenv subst context s orig_s localise ty_s in
201 let (metasenv,subst), exp_ty_t =
204 (try NCicUnification.unify rdb metasenv subst context s exp_s,exp_ty_t
205 with exc -> raise (wrap_exc (lazy (localise orig_s, Printf.sprintf
206 "Source type %s was expected to be %s" (NCicPp.ppterm ~metasenv
207 ~subst ~context s) (NCicPp.ppterm ~metasenv ~subst ~context
209 | None -> (metasenv, subst), None
211 let context_for_t = (n,C.Decl s) :: context in
212 let metasenv, subst, t, ty_t =
213 typeof_aux metasenv subst context_for_t exp_ty_t t
216 force_ty false metasenv subst context orig
217 (C.Lambda(n,s,t)) (C.Prod (n,s,ty_t)) expty
219 metasenv, subst, C.Lambda(n,s,t), C.Prod (n,s,ty_t)
220 | C.LetIn (n,(ty as orig_ty),t,bo) ->
221 let metasenv, subst, ty, ty_ty =
222 typeof_aux metasenv subst context None ty in
223 let metasenv, subst, ty, _ =
225 metasenv subst context ty orig_ty localise ty_ty in
226 let metasenv, subst, t, _ =
227 typeof_aux metasenv subst context (Some ty) t in
228 let context1 = (n, C.Def (t,ty)) :: context in
229 let metasenv, subst, bo, bo_ty =
230 typeof_aux metasenv subst context1 None bo
232 let bo_ty = NCicSubstitution.subst ~avoid_beta_redexes:true t bo_ty in
233 metasenv, subst, C.LetIn (n, ty, t, bo), bo_ty
234 | C.Appl ((he as orig_he)::(_::_ as args)) ->
235 let upto = match orig_he with C.Meta _ -> List.length args | _ -> 0 in
236 let metasenv, subst, he, ty_he =
237 typeof_aux metasenv subst context None he in
238 let metasenv, subst, t, ty =
239 eat_prods rdb ~localise metasenv subst context orig_he he ty_he args in
240 let t = if upto > 0 then NCicReduction.head_beta_reduce ~upto t else t in
241 metasenv, subst, t, ty
242 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
243 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,
244 outtype,(term as orig_term),pl) as orig ->
245 let _, leftno, itl, _, _ = NCicEnvironment.get_checked_indtys r in
246 let _, _, arity, cl = List.nth itl tyno in
247 let constructorsno = List.length cl in
248 let _, metasenv, args =
249 NCicMetaSubst.saturate metasenv subst context arity 0 in
250 let ind = if args = [] then C.Const r else C.Appl (C.Const r::args) in
251 let metasenv, subst, term, _ =
252 typeof_aux metasenv subst context (Some ind) term in
253 let parameters, arguments = HExtlib.split_nth leftno args in
256 | C.Implicit _ as ot ->
257 let rec aux = function
258 | [] -> NCic.Lambda ("_",NCic.Implicit `Type,ot)
259 | _::tl -> NCic.Lambda ("_",NCic.Implicit `Type,aux tl)
264 let metasenv, subst, outtype, outsort =
265 typeof_aux metasenv subst context None outtype in
267 (* next lines are to do a subst-expansion of the outtype, so
268 that when it becomes a beta-abstraction, the beta-redex is
269 fired during substitution *)
270 (*CSC: this is instantiate! should we move it from tactics to the
271 refiner? I think so! *)
272 let metasenv,metanoouttype,newouttype,metaoutsort =
273 NCicMetaSubst.mk_meta metasenv context `Term in
275 NCicUnification.unify rdb metasenv subst context outsort metaoutsort in
277 List.filter (function (j,_) -> j <> metanoouttype) metasenv in
279 (metanoouttype,(Some "outtype",context,outtype,metaoutsort))::subst in
280 let outtype = newouttype in
282 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
284 if parameters = [] then C.Const r
285 else C.Appl ((C.Const r)::parameters) in
286 let metasenv, subst, ind, ind_ty =
287 typeof_aux metasenv subst context None ind in
288 let metasenv, subst =
289 check_allowed_sort_elimination rdb localise r orig_term metasenv subst
290 context ind ind_ty outsort
292 (* let's check if the type of branches are right *)
293 if List.length pl <> constructorsno then
294 raise (RefineFailure (lazy (localise orig,
295 "Wrong number of cases in a match")));
297 let metasenv, subst =
299 | None -> metasenv, subst
301 NCicUnification.unify rdb metasenv subst context resty expty
304 let _, metasenv, subst, pl =
306 (fun p (j, metasenv, subst, branches) ->
308 let cons = Ref.mk_constructor j r in
309 if parameters = [] then C.Const cons
310 else C.Appl (C.Const cons::parameters)
312 let metasenv, subst, cons, ty_cons =
313 typeof_aux metasenv subst context None cons in
315 NCicTypeChecker.type_of_branch
316 ~subst context leftno outtype cons ty_cons in
317 pp (lazy ("TYPEOFBRANCH: " ^
318 NCicPp.ppterm ~metasenv ~subst ~context p ^ " ::: " ^
319 NCicPp.ppterm ~metasenv ~subst ~context ty_branch ));
320 let metasenv, subst, p, _ =
321 typeof_aux metasenv subst context (Some ty_branch) p in
322 j-1, metasenv, subst, p :: branches)
323 pl (List.length pl, metasenv, subst, [])
325 let resty = C.Appl (outtype::arguments@[term]) in
326 let resty = NCicReduction.head_beta_reduce ~subst resty in
327 metasenv, subst, C.Match (r, outtype, term, pl),resty
328 | C.Match _ -> assert false
330 pp (lazy (NCicPp.ppterm ~metasenv ~subst ~context t ^ " :: "^
331 NCicPp.ppterm ~metasenv ~subst ~context infty ));
332 force_ty true metasenv subst context orig t infty expty
333 (*D*)in outside(); rc with exc -> outside (); raise exc
335 typeof_aux metasenv subst context expty term
337 and try_coercions rdb
339 metasenv subst context t orig_t infty expty perform_unification exc
341 (* we try with a coercion *)
342 let rec first exc = function
344 raise (wrap_exc (lazy (localise orig_t, Printf.sprintf
345 "The term %s has type %s but is here used with type %s"
346 (NCicPp.ppterm ~metasenv ~subst ~context t)
347 (NCicPp.ppterm ~metasenv ~subst ~context infty)
348 (NCicPp.ppterm ~metasenv ~subst ~context expty))) exc)
349 | (metasenv, newterm, newtype, meta)::tl ->
351 pp (lazy ( "UNIFICATION in CTX:\n"^
352 NCicPp.ppcontext ~metasenv ~subst context
354 NCicPp.ppmetasenv metasenv ~subst
356 NCicPp.ppterm ~metasenv ~subst ~context meta ^ " === " ^
357 NCicPp.ppterm ~metasenv ~subst ~context t ^ "\n"));
358 let metasenv, subst =
359 NCicUnification.unify rdb metasenv subst context meta t
361 pp (lazy ( "UNIFICATION in CTX:\n"^
362 NCicPp.ppcontext ~metasenv ~subst context
364 NCicPp.ppmetasenv metasenv ~subst
366 NCicPp.ppterm ~metasenv ~subst ~context newtype ^ " === " ^
367 NCicPp.ppterm ~metasenv ~subst ~context expty ^ "\n"));
368 let metasenv, subst =
369 if perform_unification then
370 NCicUnification.unify rdb metasenv subst context newtype expty
373 metasenv, subst, newterm, newtype
375 | NCicUnification.UnificationFailure _ -> first exc tl
376 | NCicUnification.Uncertain _ as exc -> first exc tl
379 (NCicCoercion.look_for_coercion rdb metasenv subst context infty expty)
381 and force_to_sort rdb metasenv subst context t orig_t localise ty =
382 match NCicReduction.whd ~subst context ty with
383 | C.Meta (_,(0,(C.Irl 0 | C.Ctx []))) as ty ->
384 metasenv, subst, t, ty
385 | C.Meta (_i,(_,(C.Irl 0 | C.Ctx []))) -> assert false (*CSC: ???
386 metasenv, subst, t, C.Meta(i,(0,C.Irl 0)) *)
387 | C.Meta (i,(_,lc)) ->
388 let len = match lc with C.Irl len->len | C.Ctx l->List.length l in
389 let metasenv, subst, newmeta =
391 NCicMetaSubst.restrict metasenv subst i (HExtlib.list_seq 1 (len+1))
392 else metasenv, subst, i
394 metasenv, subst, t, C.Meta (newmeta,(0,C.Irl 0))
395 | C.Sort _ as ty -> metasenv, subst, t, ty
397 try_coercions rdb ~localise metasenv subst context
398 t orig_t ty (NCic.Sort (NCic.Type NCicEnvironment.type0)) false
399 (Uncertain (lazy (localise orig_t,
400 "The type of " ^ NCicPp.ppterm ~metasenv ~subst ~context t
401 ^ " is not a sort: " ^ NCicPp.ppterm ~metasenv ~subst ~context ty)))
404 localise metasenv subst context orig_s orig_t (name,s) t (t1, t2)
406 (* force to sort is done in the Prod case in typeof *)
408 | C.Sort _, C.Sort C.Prop -> metasenv, subst, s, t, t2
409 | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
410 metasenv, subst, s, t, C.Sort (C.Type (NCicEnvironment.max u1 u2))
411 | C.Sort C.Prop,C.Sort (C.Type _) -> metasenv, subst, s, t, t2
413 | C.Meta _, C.Meta (_,(_,_))
414 | C.Sort _, C.Meta (_,(_,_)) -> metasenv, subst, s, t, t2
415 | x, (C.Sort _ | C.Meta _) | _, x ->
416 let y, context, orig =
417 if x == t1 then s, context, orig_s
418 else t, ((name,C.Decl s)::context), orig_t
420 raise (RefineFailure (lazy (localise orig,Printf.sprintf
421 "%s is expected to be a type, but its type is %s that is not a sort"
422 (NCicPp.ppterm ~subst ~metasenv ~context y)
423 (NCicPp.ppterm ~subst ~metasenv ~context x))))
425 and guess_name subst ctx ty =
426 let aux initial = "#" ^ String.make 1 initial in
428 | C.Const (NReference.Ref (u,_))
429 | C.Appl (C.Const (NReference.Ref (u,_)) :: _) ->
430 aux (String.sub (NUri.name_of_uri u) 0 1).[0]
431 | C.Prod _ -> aux 'f'
434 let _,_,t,_ = NCicUtils.lookup_subst n subst in
435 guess_name subst ctx (NCicSubstitution.subst_meta lc t)
436 with NCicUtils.Subst_not_found _ -> aux 'M')
439 and eat_prods rdb ~localise metasenv subst context orig_he he ty_he args =
440 (*D*)inside 'E'; try let rc =
441 let rec aux metasenv subst args_so_far he ty_he = function
442 | [] ->metasenv, subst, NCicUntrusted.mk_appl he (List.rev args_so_far), ty_he
443 | NCic.Implicit `Vector::tl -> assert false
445 match NCicReduction.whd ~subst context ty_he with
447 let metasenv, subst, arg, _ =
449 metasenv subst context arg (Some s) in
450 let t = NCicSubstitution.subst ~avoid_beta_redexes:true arg t in
451 aux metasenv subst (arg :: args_so_far) he t tl
453 | C.Appl (C.Meta _ :: _) as t ->
454 let metasenv, subst, arg, ty_arg =
456 metasenv subst context arg None in
457 let name = guess_name subst context ty_arg in
458 let metasenv, _, meta, _ =
459 NCicMetaSubst.mk_meta metasenv
460 ((name,C.Decl ty_arg) :: context) `Type
462 let flex_prod = C.Prod (name, ty_arg, meta) in
463 (* next line grants that ty_args is a type *)
464 let metasenv,subst, flex_prod, _ =
465 typeof rdb ~localise metasenv subst
466 context flex_prod None in
467 pp (lazy ( "UNIFICATION in CTX:\n"^
468 NCicPp.ppcontext ~metasenv ~subst context
470 NCicPp.ppterm ~metasenv ~subst ~context t ^ " === " ^
471 NCicPp.ppterm ~metasenv ~subst ~context flex_prod ^ "\n"));
472 let metasenv, subst =
473 try NCicUnification.unify rdb metasenv subst context t flex_prod
474 with exc -> raise (wrap_exc (lazy (localise orig_he, Printf.sprintf
475 ("The term %s has an inferred type %s, but is applied to the" ^^
476 " argument %s of type %s")
477 (NCicPp.ppterm ~metasenv ~subst ~context he)
478 (NCicPp.ppterm ~metasenv ~subst ~context t)
479 (NCicPp.ppterm ~metasenv ~subst ~context arg)
480 (NCicPp.ppterm ~metasenv ~subst ~context ty_arg))) exc)
481 (* XXX coerce to funclass *)
483 let meta = NCicSubstitution.subst ~avoid_beta_redexes:true arg meta in
484 aux metasenv subst (arg :: args_so_far) he meta tl
485 | C.Match (_,_,C.Meta _,_)
486 | C.Match (_,_,C.Appl (C.Meta _ :: _),_)
487 | C.Appl (C.Const (NReference.Ref (_, NReference.Fix _)) :: _) ->
488 raise (Uncertain (lazy (localise orig_he, Printf.sprintf
489 ("The term %s is here applied to %d arguments but expects " ^^
490 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
491 (List.length args) (List.length args_so_far))))
493 let metasenv, subst, newhead, newheadty =
494 try_coercions rdb ~localise metasenv subst context
495 (NCicUntrusted.mk_appl he (List.rev args_so_far)) orig_he ty
496 (NCic.Prod ("_",NCic.Implicit `Term,NCic.Implicit `Term))
498 (RefineFailure (lazy (localise orig_he, Printf.sprintf
499 ("The term %s is here applied to %d arguments but expects " ^^
500 "only %d arguments") (NCicPp.ppterm ~metasenv ~subst ~context he)
501 (List.length args) (List.length args_so_far))))
503 aux metasenv subst [] newhead newheadty (arg :: tl)
505 (* We need to reverse the order of the new created metas since they
506 are pushed on top of the metasenv in the wrong order *)
507 let highest_meta = NCicMetaSubst.maxmeta () in
508 let metasenv, subst, newhead, newheadty =
509 aux metasenv subst [] he ty_he args in
510 let metasenv_old,metasenv_new =
511 List.partition (fun (i,_) -> i <= highest_meta) metasenv
513 (List.rev metasenv_new) @ metasenv_old, subst, newhead, newheadty
514 (*D*)in outside(); rc with exc -> outside (); raise exc
517 let rec first f l1 l2 =
519 | x1::tl1, x2::tl2 ->
520 (try f x1 x2 with Not_found -> first f tl1 tl2)
521 | _ -> raise Not_found
524 let rec find add dt t =
529 | C.Meta (_,(_,C.Ctx dl)), C.Meta (_,(_,C.Ctx l))
530 | C.Appl dl,C.Appl l -> dl,l
531 | C.Lambda (_,ds,dt), C.Lambda (_,s,t)
532 | C.Prod (_,ds,dt), C.Prod (_,s,t) -> [ds;dt],[s;t]
533 | C.LetIn (_,ds,db,dt), C.LetIn (_,s,b,t) -> [ds;db;dt],[s;b;t]
534 | C.Match (_,dot,dt,dl), C.Match (_,ot,t,l) -> (dot::dt::dl),(ot::t::l)
535 | _ -> raise Not_found
537 first (find add) dl l
540 let relocalise old_localise dt t add =
542 (try find add dt t with Not_found -> assert false)
545 let undebruijnate inductive ref t rev_fl =
546 NCicSubstitution.psubst (fun x -> x)
547 (List.rev (HExtlib.list_mapi
548 (fun (_,_,rno,_,_,_) i ->
550 (if inductive then NReference.mk_fix i rno ref
551 else NReference.mk_cofix i ref))
558 rdb ?(localise=fun _ -> Stdpp.dummy_loc) (uri,height,metasenv,subst,obj)
560 let check_type metasenv subst context (ty as orig_ty) = (* XXX fattorizza *)
561 let metasenv, subst, ty, sort =
562 typeof rdb ~localise metasenv subst context ty None
564 let metasenv, subst, ty, sort =
565 force_to_sort rdb metasenv subst context ty orig_ty localise sort
567 metasenv, subst, ty, sort
570 | C.Constant (relevance, name, bo, ty , attr) ->
571 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
572 let metasenv, subst, bo, ty, height =
575 let metasenv, subst, bo, ty =
576 typeof rdb ~localise metasenv subst [] bo (Some ty) in
577 let height = (* XXX recalculate *) height in
578 metasenv, subst, Some bo, ty, height
579 | None -> metasenv, subst, None, ty, 0
581 uri, height, metasenv, subst,
582 C.Constant (relevance, name, bo, ty, attr)
583 | C.Fixpoint (inductive, fl, attr) ->
584 let len = List.length fl in
585 let types, metasenv, subst, rev_fl =
587 (fun (types, metasenv, subst, fl) (relevance,name,k,ty,bo) ->
588 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
589 let dbo = NCicTypeChecker.debruijn uri len [] ~subst bo in
590 let localise = relocalise localise dbo bo in
591 (name,C.Decl ty)::types,
592 metasenv, subst, (relevance,name,k,ty,dbo,localise)::fl
593 ) ([], metasenv, subst, []) fl (* XXX kl rimosso nel nucleo *)
595 let metasenv, subst, fl =
597 (fun (metasenv,subst,fl) (relevance,name,k,ty,dbo,localise) ->
598 let metasenv, subst, dbo, ty =
599 typeof rdb ~localise metasenv subst types dbo (Some ty)
601 metasenv, subst, (relevance,name,k,ty,dbo)::fl)
602 (metasenv, subst, []) rev_fl
604 let height = (* XXX recalculate *) height in
607 (fun (relevance,name,k,ty,dbo) ->
609 undebruijnate inductive
610 (NReference.reference_of_spec uri
611 (if inductive then NReference.Fix (0,k,0)
612 else NReference.CoFix 0)) dbo rev_fl
614 relevance,name,k,ty,bo)
617 uri, height, metasenv, subst,
618 C.Fixpoint (inductive, fl, attr)
619 | C.Inductive (ind, leftno, itl, attr) ->
620 let len = List.length itl in
621 let metasenv,subst,rev_itl,tys =
623 (fun (metasenv,subst,res,ctx) (relevance,n,ty,cl) ->
624 let metasenv, subst, ty, _ = check_type metasenv subst [] ty in
625 metasenv,subst,(relevance,n,ty,cl)::res,(n,NCic.Decl ty)::ctx
626 ) (metasenv,subst,[],[]) itl in
627 let metasenv,subst,itl,_ =
629 (fun (metasenv,subst,res,i) (it_relev,n,ty,cl) ->
630 let context,ty_sort = NCicReduction.split_prods ~subst [] ~-1 ty in
631 let sx_context_ty_rev,_= HExtlib.split_nth leftno (List.rev context) in
632 let metasenv,subst,cl =
634 (fun (k_relev,n,te) (metasenv,subst,res) ->
636 try snd (HExtlib.split_nth leftno k_relev)
637 with Failure _ -> k_relev in
638 let te = NCicTypeChecker.debruijn uri len [] ~subst te in
639 let metasenv, subst, te, _ = check_type metasenv subst tys te in
640 let context,te = NCicReduction.split_prods ~subst tys leftno te in
641 let _,chopped_context_rev =
642 HExtlib.split_nth (List.length tys) (List.rev context) in
643 let sx_context_te_rev,_ =
644 HExtlib.split_nth leftno chopped_context_rev in
645 let metasenv,subst,_ =
648 (fun (metasenv,subst,context) item1 item2 ->
649 let (metasenv,subst),convertible =
651 match item1,item2 with
652 (n1,C.Decl ty1),(n2,C.Decl ty2) ->
654 NCicUnification.unify rdb ~test_eq_only:true metasenv
655 subst context ty1 ty2,true
657 (metasenv,subst),false
658 | (n1,C.Def (bo1,ty1)),(n2,C.Def (bo2,ty2)) ->
661 NCicUnification.unify rdb ~test_eq_only:true metasenv
662 subst context ty1 ty2
664 NCicUnification.unify rdb ~test_eq_only:true metasenv
665 subst context bo1 bo2,true
667 (metasenv,subst),false
668 | _,_ -> (metasenv,subst),false
670 | NCicUnification.Uncertain _
671 | NCicUnification.UnificationFailure _ ->
672 (metasenv,subst),false
677 | _,C.Def (b,_) -> b in
678 if not convertible then
679 raise (RefineFailure (lazy (localise term2,
680 ("Mismatch between the left parameters of the constructor " ^
681 "and those of its inductive type"))))
683 metasenv,subst,item1::context
684 ) (metasenv,subst,[]) sx_context_ty_rev sx_context_te_rev
685 with Invalid_argument "List.fold_left2" -> assert false in
686 let con_sort= NCicTypeChecker.typeof ~subst ~metasenv context te in
688 NCicReduction.whd ~subst context con_sort,
689 NCicReduction.whd ~subst [] ty_sort
691 (C.Sort (C.Type u1) as s1), (C.Sort (C.Type u2) as s2) ->
692 if not (NCicEnvironment.universe_leq u1 u2) then
695 (lazy(localise te, "The type " ^
696 NCicPp.ppterm ~metasenv ~subst ~context s1 ^
697 " of the constructor is not included in the inductive"^
699 NCicPp.ppterm ~metasenv ~subst ~context s2)))
700 | C.Sort _, C.Sort C.Prop
701 | C.Sort _, C.Sort C.Type _ -> ()
706 "Wrong constructor or inductive arity shape"))));
707 (* let's check also the positivity conditions *)
710 (NCicTypeChecker.are_all_occurrences_positive
711 ~subst context uri leftno (i+leftno) leftno (len+leftno) te)
716 "Non positive occurence in " ^
717 NCicPp.ppterm ~metasenv ~subst ~context te)))
719 let relsno = List.length itl + leftno in
721 NCicSubstitution.psubst
726 NCic.Const (NReference.reference_of_spec uri
727 (NReference.Ind (ind,relsno - i,leftno))))
728 (HExtlib.list_seq 1 (relsno+1))
732 (fun (name,decl) te ->
734 NCic.Decl ty -> NCic.Prod (name,ty,te)
735 | NCic.Def (bo,ty) -> NCic.LetIn (name,ty,bo,te)
736 ) sx_context_te_rev te
738 metasenv,subst,(k_relev,n,te)::res
739 ) cl (metasenv,subst,[])
741 metasenv,subst,(it_relev,n,ty,cl)::res,i+1
742 ) (metasenv,subst,[],1) rev_itl
744 uri, height, metasenv, subst, C.Inductive (ind, leftno, itl, attr)
747 NCicUnification.set_refiner_typeof typeof;;
749 (* vim:set foldmethod=marker: *)