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 UnificationFailure of string Lazy.t;;
15 exception Uncertain of string Lazy.t;;
16 exception AssertFailure of string Lazy.t;;
18 let (===) x y = Pervasives.compare x y = 0 ;;
20 let uncert_exc metasenv subst context t1 t2 =
22 "Can't unify " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^
23 " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2))
26 let fail_exc metasenv subst context t1 t2 =
27 UnificationFailure (lazy (
28 "Can't unify " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^
29 " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2));
32 let mk_appl ~upto hd tl =
33 NCicReduction.head_beta_reduce ~upto
35 | NCic.Appl l -> NCic.Appl (l@tl)
36 | _ -> NCic.Appl (hd :: tl))
39 exception WrongShape;;
41 let eta_reduce subst t =
42 let delift_if_not_occur body =
44 Some (NCicSubstitution.psubst ~avoid_beta_redexes:true
45 (fun () -> raise WrongShape) [()] body)
46 with WrongShape -> None
48 let rec eat_lambdas ctx = function
49 | NCic.Lambda (name, src, tgt) ->
50 eat_lambdas ((name, src) :: ctx) tgt
51 | NCic.Meta (i,lc) as t->
53 let _,_,t,_ = NCicUtils.lookup_subst i subst in
54 let t = NCicSubstitution.subst_meta lc t in
56 with Not_found -> ctx, t)
59 let context_body = eat_lambdas [] t in
60 let rec aux = function
62 | (name, src)::ctx, (NCic.Appl (hd::[NCic.Rel 1]) as bo) ->
63 (match delift_if_not_occur hd with
64 | None -> aux (ctx,NCic.Lambda(name,src, bo))
65 | Some bo -> aux (ctx,bo))
66 | (name, src)::ctx, (NCic.Appl args as bo)
67 when HExtlib.list_last args = NCic.Rel 1 ->
68 let args, _ = HExtlib.split_nth (List.length args - 1) args in
69 (match delift_if_not_occur (NCic.Appl args) with
70 | None -> aux (ctx,NCic.Lambda(name,src, bo))
71 | Some bo -> aux (ctx,bo))
72 | (name, src) :: ctx, t ->
73 aux (ctx,NCic.Lambda(name,src, t))
79 module Ref = NReference;;
81 let debug = ref false;;
85 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
90 indent := !indent ^ String.make 1 c;
91 if !debug then prerr_endline ("{{{" ^ !indent ^ " ")
94 if !debug then prerr_endline "}}}";
95 if not ok then pp (lazy "exception raised!");
97 indent := String.sub !indent 0 (String.length !indent -1)
99 Invalid_argument _ -> indent := "??"; ()
102 let ppcontext ~metasenv ~subst c =
103 "\nctx:\n"^ NCicPp.ppcontext ~metasenv ~subst c
105 let ppmetasenv ~subst m = "\nmenv:\n" ^ NCicPp.ppmetasenv ~subst m;;
107 let ppcontext ~metasenv:_metasenv ~subst:_subst _context = "";;
108 let ppmetasenv ~subst:_subst _metasenv = "";;
110 let fix_sorts swap exc t =
111 let rec aux () = function
112 | NCic.Sort (NCic.Type u) as orig ->
114 match NCicEnvironment.sup u with
116 prerr_endline ("no sup for " ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] orig) ;
118 | Some u1 -> if u = u1 then orig else NCic.Sort (NCic.Type u1)
120 NCic.Sort (NCic.Type (
121 match NCicEnvironment.sup NCicEnvironment.type0 with
123 | _ -> assert false))
124 | NCic.Meta _ as orig -> orig
125 | t -> NCicUtils.map (fun _ _ -> ()) () aux t
130 let is_locked n subst =
132 match NCicUtils.lookup_subst n subst with
133 | tag, _,_,_ when NCicMetaSubst.is_out_scope_tag tag -> true
135 with NCicUtils.Subst_not_found _ -> false
138 let rec mk_irl stop base =
139 if base > stop then []
140 else (NCic.Rel base) :: mk_irl stop (base+1)
143 (* the argument must be a term in whd *)
144 let rec could_reduce =
147 | C.Appl (C.Const (Ref.Ref (_,Ref.Fix (_,recno,_)))::args)
148 when List.length args > recno -> could_reduce (List.nth args recno)
149 | C.Match (_,_,arg,_) -> could_reduce arg
150 | C.Appl (he::_) -> could_reduce he
151 | C.Sort _ | C.Rel _ | C.Prod _ | C.Lambda _ | C.Const _ -> false
152 | C.Appl [] | C.LetIn _ | C.Implicit _ -> assert false
155 let rec lambda_intros rdb metasenv subst context t args =
156 let tty = NCicTypeChecker.typeof ~metasenv ~subst context t in
159 (fun arg -> arg, NCicTypeChecker.typeof ~metasenv ~subst context arg) args in
160 let context_of_args = context in
161 let rec mk_lambda metasenv subst context n processed_args = function
163 let metasenv, _, bo, _ =
164 NCicMetaSubst.mk_meta metasenv context
165 (`WithType (NCicSubstitution.lift n tty))
170 NCicPp.ppterm ~metasenv ~subst ~context:context_of_args arg ^ " : " ^
171 NCicPp.ppterm ~metasenv ~subst ~context:context_of_args ty));
172 let metasenv, subst, telescopic_ty =
173 if processed_args = [] then metasenv, subst, ty else
174 let _ = pp(lazy("delift")) in
175 delift_type_wrt_terms rdb metasenv subst context
176 (NCicSubstitution.lift n ty)
177 (List.map (NCicSubstitution.lift n) (List.rev processed_args))
179 pp(lazy("arg} "^NCicPp.ppterm ~metasenv ~subst ~context telescopic_ty));
180 let name = "HBeta"^string_of_int n in
181 let metasenv, subst, bo =
182 mk_lambda metasenv subst ((name,NCic.Decl telescopic_ty)::context) (n+1)
183 (arg::processed_args) tail
185 metasenv, subst, NCic.Lambda (name, telescopic_ty, bo)
187 pp(lazy("LAMBDA_INTROS{ " ^
188 NCicPp.ppterm ~metasenv ~subst ~context t ^ ":" ^
189 NCicPp.ppterm ~metasenv ~subst ~context tty ^ " over " ^
190 String.concat "," (List.map (NCicPp.ppterm ~metasenv ~subst ~context)args)));
191 let rc = mk_lambda metasenv subst context 0 [] argsty in
192 pp(lazy("LAMBDA_INTROS}"));
195 and instantiate rdb test_eq_only metasenv subst context n lc t swap =
196 (*D*) inside 'I'; try let rc =
197 pp (lazy(string_of_int n ^ " :=?= "^
198 NCicPp.ppterm ~metasenv ~subst ~context t));
199 let unify test_eq_only m s c t1 t2 =
200 if swap then unify rdb test_eq_only m s c t2 t1
201 else unify rdb test_eq_only m s c t1 t2
203 let has_tag = List.exists in
204 let tags, _, ty = NCicUtils.lookup_meta n metasenv in
206 let metasenv, subst, t =
208 | NCic.Implicit (`Typeof _) ->
209 pp(lazy("meta with no type"));
210 assert(has_tag ((=)`IsSort) tags);
213 let exc_to_be = fail_exc metasenv subst context (NCic.Meta (n,lc)) t in
215 try t, NCicTypeChecker.typeof ~subst ~metasenv context t
217 | NCicTypeChecker.AssertFailure msg as exn ->
218 pp(lazy("we try to fix the sort\n"^
219 Lazy.force msg^"\n"^NCicPp.ppmetasenv ~subst metasenv));
220 let ft = fix_sorts swap exc_to_be t in
221 pp(lazy("unable to fix the sort"));
222 if ft == t then raise exn;
223 (try ft, NCicTypeChecker.typeof ~subst ~metasenv context ft
224 with NCicTypeChecker.AssertFailure _ -> raise exn)
225 | NCicTypeChecker.TypeCheckerFailure msg ->
226 prerr_endline (Lazy.force msg);
227 prerr_endline (NCicPp.ppterm ~metasenv ~subst ~context t);
228 prerr_endline (ppcontext ~metasenv ~subst context);
229 prerr_endline (ppmetasenv ~subst metasenv);
233 | NCic.Implicit (`Typeof _) ->
234 raise (UnificationFailure(lazy "trying to unify a term with a type"))
236 let lty = NCicSubstitution.subst_meta lc ty in
237 pp (lazy ("On the types: " ^
238 NCicPp.ppterm ~metasenv ~subst ~context lty ^ " === " ^
239 NCicPp.ppterm ~metasenv ~subst ~context ty_t));
241 try unify test_eq_only metasenv subst context lty ty_t
242 with NCicEnvironment.BadConstraint _ as exc ->
243 let ty_t = fix_sorts swap exc_to_be ty_t in
244 try unify test_eq_only metasenv subst context lty ty_t
246 | NCicEnvironment.BadConstraint _
247 | UnificationFailure _ -> raise exc
251 (* viral sortification *)
252 let is_sort metasenv subst context t =
253 match NCicReduction.whd ~subst context t with
255 let tags, _, _ = NCicUtils.lookup_meta i metasenv in
256 has_tag ((=) `IsSort) tags
257 | NCic.Sort _ -> true
260 let rec sortify metasenv subst = function
261 | NCic.Implicit (`Typeof _) -> assert false
262 | NCic.Sort _ as t -> metasenv, subst, t, 0
263 | NCic.Meta (i,_) as t ->
264 let tags, context, ty = NCicUtils.lookup_meta i metasenv in
265 if has_tag ((=) `IsSort) tags then metasenv, subst, t, i
267 let ty = NCicReduction.whd ~subst context ty in
268 let metasenv, subst, ty, _ = sortify metasenv subst ty in
269 let metasenv, j, m, _ =
270 NCicMetaSubst.mk_meta metasenv ~attrs:[`IsSort] [] (`WithType ty)
272 pp(lazy("rimpiazzo " ^ string_of_int i^" con "^string_of_int j));
273 let subst_entry = i, (tags, context, m, ty) in
274 let subst = subst_entry :: subst in
275 let metasenv = List.filter (fun x,_ -> i <> x) metasenv in
276 metasenv, subst, m, j
277 | NCic.Appl (NCic.Meta _ as hd :: args) as t ->
278 let metasenv, subst, lambda_Mj =
279 lambda_intros rdb metasenv subst context t args
281 let metasenv,subst= unify true metasenv subst context hd lambda_Mj in
282 let t = NCicReduction.whd ~subst context t in
283 let _result = sortify metasenv subst t in
284 (* untested, maybe dead, code *) assert false;
286 if could_reduce t then raise (Uncertain(lazy "not a sort"))
287 else raise (UnificationFailure(lazy "not a sort"))
289 let metasenv, subst, _, n =
290 if has_tag ((=) `IsSort) tags then
291 let m,s,x,_ = sortify metasenv subst (NCicReduction.whd ~subst context t)
293 else if is_sort metasenv subst context t then
294 sortify metasenv subst (NCic.Meta (n,lc))
296 metasenv, subst, NCic.Rel ~-1,n
298 let tags, ctx, ty = NCicUtils.lookup_meta n metasenv in
300 pp (lazy(string_of_int n ^ " := 111 = "^
301 NCicPp.ppterm ~metasenv ~subst ~context t));
302 let (metasenv, subst), t =
305 ~unify:(fun m s c t1 t2 ->
308 try Some (unify test_eq_only m s c t1 t2 )
309 with UnificationFailure _ | Uncertain _ -> None
312 metasenv subst context n lc t
313 with NCicMetaSubst.Uncertain msg ->
314 pp (lazy ("delift fails: " ^ Lazy.force msg));
315 raise (Uncertain msg)
316 | NCicMetaSubst.MetaSubstFailure msg ->
317 pp (lazy ("delift fails: " ^ Lazy.force msg));
318 raise (UnificationFailure msg)
320 pp (lazy(string_of_int n ^ " := 222 = "^
321 NCicPp.ppterm ~metasenv ~subst ~context:ctx t
322 ^ ppmetasenv ~subst metasenv));
323 (* Unifying the types may have already instantiated n. *)
325 let _, _,oldt,_ = NCicUtils.lookup_subst n subst in
326 let oldt = NCicSubstitution.subst_meta lc oldt in
327 let t = NCicSubstitution.subst_meta lc t in
328 (* conjecture: always fail --> occur check *)
329 unify test_eq_only metasenv subst context oldt t
330 with NCicUtils.Subst_not_found _ ->
332 let rec aux = function
333 | NCic.Meta (j,lc) ->
335 let _, _, t, _ = NCicUtils.lookup_subst j subst in
336 aux (NCicSubstitution.subst_meta lc t)
337 with NCicUtils.Subst_not_found _ ->
338 let tags', ctx, ty = NCicUtils.lookup_meta j metasenv in
339 let metasenv = List.remove_assoc j metasenv in
340 let tags = tags @ tags' in
341 (j, (tags, ctx, ty)) :: metasenv, tags)
342 | _ -> metasenv, tags
346 (* by cumulativity when unify(?,Type_i)
347 * we could ? := Type_j with j <= i... *)
348 let subst = (n, (tags, ctx, t, ty)) :: subst in
349 pp (lazy ("?"^string_of_int n^" := "^NCicPp.ppterm
350 ~metasenv ~subst ~context (NCicSubstitution.subst_meta lc t)));
352 List.filter (fun (m,_) -> not (n = m)) metasenv
355 (*D*) in outside true; rc with exn -> outside false; raise exn
357 and unify rdb test_eq_only metasenv subst context t1 t2 =
358 (*D*) inside 'U'; try let rc =
359 let fo_unif test_eq_only metasenv subst t1 t2 =
360 (*D*) inside 'F'; try let rc =
361 pp (lazy(" " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " ==?== " ^
362 NCicPp.ppterm ~metasenv ~subst ~context t2 ^ ppmetasenv
368 | C.Appl [_], _ | _, C.Appl [_] | C.Appl [], _ | _, C.Appl []
369 | C.Appl (C.Appl _::_), _ | _, C.Appl (C.Appl _::_) ->
370 prerr_endline "Appl [Appl _;_] or Appl [] or Appl [_] invariant";
372 | (C.Sort (C.Type a), C.Sort (C.Type b)) when not test_eq_only ->
373 if NCicEnvironment.universe_leq a b then metasenv, subst
374 else raise (fail_exc metasenv subst context t1 t2)
375 | (C.Sort (C.Type a), C.Sort (C.Type b)) ->
376 if NCicEnvironment.universe_eq a b then metasenv, subst
377 else raise (fail_exc metasenv subst context t1 t2)
378 | (C.Sort C.Prop,C.Sort (C.Type _)) ->
379 if (not test_eq_only) then metasenv, subst
380 else raise (fail_exc metasenv subst context t1 t2)
382 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2))
383 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
384 let metasenv, subst = unify rdb true metasenv subst context s1 s2 in
385 unify rdb test_eq_only metasenv subst ((name1, C.Decl s1)::context) t1 t2
386 | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
387 let metasenv,subst=unify rdb test_eq_only metasenv subst context ty1 ty2 in
388 let metasenv,subst=unify rdb test_eq_only metasenv subst context s1 s2 in
389 let context = (name1, C.Def (s1,ty1))::context in
390 unify rdb test_eq_only metasenv subst context t1 t2
392 | (C.Meta (n1,(s1,l1 as lc1)),C.Meta (n2,(s2,l2 as lc2))) when n1 = n2 ->
394 let l1 = NCicUtils.expand_local_context l1 in
395 let l2 = NCicUtils.expand_local_context l2 in
396 let metasenv, subst, to_restrict, _ =
398 (fun t1 t2 (metasenv, subst, to_restrict, i) ->
400 let metasenv, subst =
401 unify rdb test_eq_only metasenv subst context
402 (NCicSubstitution.lift s1 t1) (NCicSubstitution.lift s2 t2)
404 metasenv, subst, to_restrict, i-1
405 with UnificationFailure _ | Uncertain _ ->
406 metasenv, subst, i::to_restrict, i-1)
407 l1 l2 (metasenv, subst, [], List.length l1)
409 if to_restrict <> [] then
410 let metasenv, subst, _ =
411 NCicMetaSubst.restrict metasenv subst n1 to_restrict
416 | Invalid_argument _ -> assert false
417 | NCicMetaSubst.MetaSubstFailure msg ->
419 let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
420 let term1 = NCicSubstitution.subst_meta lc1 term in
421 let term2 = NCicSubstitution.subst_meta lc2 term in
422 unify rdb test_eq_only metasenv subst context term1 term2
423 with NCicUtils.Subst_not_found _-> raise (UnificationFailure msg))
425 | _, NCic.Meta (n, _) when is_locked n subst ->
426 (let (metasenv, subst), i =
427 match NCicReduction.whd ~subst context t1 with
428 | NCic.Appl (NCic.Meta (i,l)::args) ->
429 let metasenv, subst, lambda_Mj =
430 lambda_intros rdb metasenv subst context t1 args
432 unify rdb test_eq_only metasenv subst context
433 (C.Meta (i,l)) lambda_Mj,
435 | NCic.Meta (i,_) -> (metasenv, subst), i
437 raise (UnificationFailure (lazy "Locked term vs non
438 flexible term; probably not saturated enough yet!"))
440 let t1 = NCicReduction.whd ~subst context t1 in
442 match t1 with NCic.Meta (j,l) -> j, l | _ -> assert false
444 let metasenv, subst =
445 instantiate rdb test_eq_only metasenv subst context j lj t2 true
447 (* We need to remove the out_scope_tags to avoid propagation of
448 them that triggers again the ad-hoc case *)
450 List.map (fun (i,(tag,ctx,bo,ty)) ->
453 (function `InScope | `OutScope _ -> false | _ -> true) tag
459 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
460 let term = eta_reduce subst term in
461 let subst = List.filter (fun (j,_) -> j <> i) subst in
462 metasenv, ((i, (name, ctx, term, ty)) :: subst)
463 with Not_found -> assert false))
465 | C.Meta (n,lc), t ->
467 let _,_,term,_ = NCicUtils.lookup_subst n subst in
468 let term = NCicSubstitution.subst_meta lc term in
469 unify rdb test_eq_only metasenv subst context term t
470 with NCicUtils.Subst_not_found _->
471 instantiate rdb test_eq_only metasenv subst context n lc
472 (NCicReduction.head_beta_reduce ~subst t) false)
474 | t, C.Meta (n,lc) ->
476 let _,_,term,_ = NCicUtils.lookup_subst n subst in
477 let term = NCicSubstitution.subst_meta lc term in
478 unify rdb test_eq_only metasenv subst context t term
479 with NCicUtils.Subst_not_found _->
480 instantiate rdb test_eq_only metasenv subst context n lc
481 (NCicReduction.head_beta_reduce ~subst t) true)
483 | NCic.Appl (NCic.Meta (i,l)::args), _ when List.mem_assoc i subst ->
484 let _,_,term,_ = NCicUtils.lookup_subst i subst in
485 let term = NCicSubstitution.subst_meta l term in
486 unify rdb test_eq_only metasenv subst context
487 (mk_appl ~upto:(List.length args) term args) t2
489 | _, NCic.Appl (NCic.Meta (i,l)::args) when List.mem_assoc i subst ->
490 let _,_,term,_ = NCicUtils.lookup_subst i subst in
491 let term = NCicSubstitution.subst_meta l term in
492 unify rdb test_eq_only metasenv subst context t1
493 (mk_appl ~upto:(List.length args) term args)
495 | NCic.Appl (NCic.Meta (i,_)::_ as l1),
496 NCic.Appl (NCic.Meta (j,_)::_ as l2) when i=j ->
499 (fun (metasenv, subst) t1 t2 ->
500 unify rdb test_eq_only metasenv subst context t1 t2)
501 (metasenv,subst) l1 l2
502 with Invalid_argument _ ->
503 raise (fail_exc metasenv subst context t1 t2))
505 | NCic.Appl (NCic.Meta (i,l)::args), _ ->
506 let metasenv, subst, lambda_Mj =
507 lambda_intros rdb metasenv subst context t1 args
509 let metasenv, subst =
510 unify rdb test_eq_only metasenv subst context
511 (C.Meta (i,l)) lambda_Mj
513 let metasenv, subst =
514 unify rdb test_eq_only metasenv subst context t1 t2
517 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
518 let term = eta_reduce subst term in
519 let subst = List.filter (fun (j,_) -> j <> i) subst in
520 metasenv, ((i, (name, ctx, term, ty)) :: subst)
521 with Not_found -> assert false)
523 | _, NCic.Appl (NCic.Meta (i,l)::args) ->
524 let metasenv, subst, lambda_Mj =
525 lambda_intros rdb metasenv subst context t2 args
527 let metasenv, subst =
528 unify rdb test_eq_only metasenv subst context
529 lambda_Mj (C.Meta (i,l))
531 let metasenv, subst =
532 unify rdb test_eq_only metasenv subst context t1 t2
535 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
536 let term = eta_reduce subst term in
537 let subst = List.filter (fun (j,_) -> j <> i) subst in
538 metasenv, ((i, (name, ctx, term, ty)) :: subst)
539 with Not_found -> assert false)
541 (* processing this case here we avoid a useless small delta step *)
542 | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2))
544 let relevance = NCicEnvironment.get_relevance r1 in
545 let relevance = match r1 with
546 | Ref.Ref (_,Ref.Con (_,_,lno)) ->
548 try snd (HExtlib.split_nth lno relevance)
551 HExtlib.mk_list false lno @ relevance
554 let metasenv, subst, _ =
557 (fun (metasenv, subst, relevance) t1 t2 ->
559 match relevance with b::tl -> b,tl | _ -> true, [] in
560 let metasenv, subst =
561 try unify rdb test_eq_only metasenv subst context t1 t2
562 with UnificationFailure _ | Uncertain _ when not b ->
565 metasenv, subst, relevance)
566 (metasenv, subst, relevance) tl1 tl2
567 with Invalid_argument _ ->
568 raise (uncert_exc metasenv subst context t1 t2)
572 | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
573 C.Match (ref2,outtype2,term2,pl2)) ->
574 let _,_,itl,_,_ = NCicEnvironment.get_checked_indtys ref1 in
575 let _,_,ty,_ = List.nth itl tyno in
576 let rec remove_prods ~subst context ty =
577 let ty = NCicReduction.whd ~subst context ty in
580 | C.Prod (name,so,ta) ->
581 remove_prods ~subst ((name,(C.Decl so))::context) ta
585 match remove_prods ~subst [] ty with
586 | C.Sort C.Prop -> true
589 if not (Ref.eq ref1 ref2) then
590 raise (uncert_exc metasenv subst context t1 t2)
592 let metasenv, subst =
593 unify rdb test_eq_only metasenv subst context outtype1 outtype2 in
594 let metasenv, subst =
595 try unify rdb test_eq_only metasenv subst context term1 term2
596 with UnificationFailure _ | Uncertain _ when is_prop ->
601 (fun (metasenv,subst) ->
602 unify rdb test_eq_only metasenv subst context)
603 (metasenv, subst) pl1 pl2
604 with Invalid_argument _ -> assert false)
605 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
606 | _ when NCicUntrusted.metas_of_term subst context t1 = [] &&
607 NCicUntrusted.metas_of_term subst context t2 = [] ->
608 raise (fail_exc metasenv subst context t1 t2)
609 | _ -> raise (uncert_exc metasenv subst context t1 t2)
610 (*D*) in outside true; rc with exn -> outside false; raise exn
612 let try_hints metasenv subst t1 t2 (* exc*) =
613 (*D*) inside 'H'; try let rc =
614 pp(lazy ("\nProblema:\n" ^
615 NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " =?= " ^
616 NCicPp.ppterm ~metasenv ~subst ~context t2));
618 NCicUnifHint.look_for_hint rdb metasenv subst context t1 t2
620 let rec cand_iter = function
621 | [] -> None (* raise exc *)
622 | (metasenv,(c1,c2),premises)::tl ->
623 pp (lazy ("\nProvo il candidato:\n" ^
627 NCicPp.ppterm ~metasenv ~subst ~context a ^ " =?= " ^
628 NCicPp.ppterm ~metasenv ~subst ~context b) premises) ^
629 "\n-------------------------------------------\n"^
630 NCicPp.ppterm ~metasenv ~subst ~context c1 ^ " = " ^
631 NCicPp.ppterm ~metasenv ~subst ~context c2));
633 (*D*) inside 'K'; try let rc =
635 fo_unif test_eq_only metasenv subst t1 c1 in
637 fo_unif test_eq_only metasenv subst c2 t2 in
640 (fun (metasenv, subst) (x,y) ->
641 unify rdb test_eq_only metasenv subst context x y)
642 (metasenv, subst) premises
644 pp(lazy("FUNZIONA!"));
645 Some (metasenv, subst)
646 (*D*) in outside true; rc with exn -> outside false; raise exn
648 UnificationFailure _ | Uncertain _ ->
652 (*D*) in outside true; rc with exn -> outside false; raise exn
654 let height_of = function
655 | NCic.Const (Ref.Ref (_,Ref.Def h))
656 | NCic.Const (Ref.Ref (_,Ref.Fix (_,_,h)))
657 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Def h))::_)
658 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
661 let put_in_whd m1 m2 =
662 NCicReduction.reduce_machine ~delta:max_int ~subst context m1,
663 NCicReduction.reduce_machine ~delta:max_int ~subst context m2
665 let small_delta_step ~subst
666 ((_,_,t1,_ as m1, norm1) as x1) ((_,_,t2,_ as m2, norm2) as x2)
668 assert (not (norm1 && norm2));
670 x1,NCicReduction.reduce_machine ~delta:(height_of t2 -1) ~subst context m2
672 NCicReduction.reduce_machine ~delta:(height_of t1 -1) ~subst context m1,x2
674 let h1 = height_of t1 in
675 let h2 = height_of t2 in
676 let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
677 NCicReduction.reduce_machine ~delta ~subst context m1,
678 NCicReduction.reduce_machine ~delta ~subst context m2
680 let rec unif_machines metasenv subst =
682 | ((k1,e1,t1,s1),norm1 as m1),((k2,e2,t2,s2),norm2 as m2) ->
683 (*D*) inside 'M'; try let rc =
685 NCicPp.ppterm ~metasenv ~subst ~context
686 (NCicReduction.unwind (k1,e1,t1,s1)) ^
688 NCicPp.ppterm ~metasenv ~subst ~context
689 (NCicReduction.unwind (k2,e2,t2,s2))));
690 pp (lazy (string_of_bool norm1 ^ " ?? " ^ string_of_bool norm2));
691 let relevance = [] (* TO BE UNDERSTOOD
693 | C.Const r -> NCicEnvironment.get_relevance r
695 let unif_from_stack t1 t2 b metasenv subst =
697 let t1 = NCicReduction.from_stack ~delta:max_int t1 in
698 let t2 = NCicReduction.from_stack ~delta:max_int t2 in
699 unif_machines metasenv subst (put_in_whd t1 t2)
700 with UnificationFailure _ | Uncertain _ when not b ->
703 let rec check_stack l1 l2 r todo =
705 | x1::tl1, x2::tl2, r::tr-> check_stack tl1 tl2 tr ((x1,x2,r)::todo)
706 | x1::tl1, x2::tl2, []-> check_stack tl1 tl2 [] ((x1,x2,true)::todo)
708 NCicReduction.unwind (k1,e1,t1,List.rev l1),
709 NCicReduction.unwind (k2,e2,t2,List.rev l2),
712 let hh1,hh2,todo=check_stack (List.rev s1) (List.rev s2) relevance [] in
714 let metasenv,subst = fo_unif test_eq_only metasenv subst hh1 hh2 in
716 (fun (metasenv,subst) (x1,x2,r) ->
717 unif_from_stack x1 x2 r metasenv subst
718 ) (metasenv,subst) todo
719 with UnificationFailure _ | Uncertain _ when not (norm1 && norm2) ->
720 unif_machines metasenv subst (small_delta_step ~subst m1 m2)
721 (*D*) in outside true; rc with exn -> outside false; raise exn
723 try fo_unif test_eq_only metasenv subst t1 t2
725 | UnificationFailure msg as exn ->
727 unif_machines metasenv subst
728 (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
730 | UnificationFailure _ -> raise (UnificationFailure msg)
731 | Uncertain _ -> raise exn)
732 | Uncertain msg as exn ->
734 prerr_endline "PROBLEMA";
735 prerr_endline (NCicPp.ppterm ~metasenv ~subst ~context t1);
736 prerr_endline (NCicPp.ppterm ~metasenv ~subst ~context t2);
738 let (t1,_ as t1m),(t2,_ as t2m) = put_in_whd (0,[],t1,[]) (0,[],t2,[]) in
740 try_hints metasenv subst
741 (NCicReduction.unwind t1) (NCicReduction.unwind t2)
746 unif_machines metasenv subst (t1m, t2m)
748 | UnificationFailure _ -> raise (UnificationFailure msg)
749 | Uncertain _ -> raise exn
750 (*D*) in outside true; rc with exn -> outside false; raise exn
752 and delift_type_wrt_terms rdb metasenv subst context t args =
753 let lc = List.rev args @ mk_irl (List.length context) (List.length args+1) in
754 let (metasenv, subst), t =
757 ~unify:(fun m s c t1 t2 ->
760 try Some (unify rdb false m s c t1 t2 )
761 with UnificationFailure _ | Uncertain _ -> None
764 metasenv subst context 0 (0,NCic.Ctx lc) t
765 with NCicMetaSubst.MetaSubstFailure _ -> (metasenv, subst), t
771 let unify rdb ?(test_eq_only=false) =
773 unify rdb test_eq_only;;
775 let fix_sorts = fix_sorts true (UnificationFailure (lazy "no sup"));;