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;;
17 exception KeepReducing of string Lazy.t;;
18 exception KeepReducingThis of
19 string Lazy.t * (NCicReduction.machine * bool) *
20 (NCicReduction.machine * bool) ;;
22 let (===) x y = Pervasives.compare x y = 0 ;;
24 let mk_msg metasenv subst context t1 t2 =
26 "Can't unify " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^
27 " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2))
29 let mk_appl ~upto hd tl =
30 NCicReduction.head_beta_reduce ~upto
32 | NCic.Appl l -> NCic.Appl (l@tl)
33 | _ -> NCic.Appl (hd :: tl))
36 exception WrongShape;;
38 let eta_reduce subst t =
39 let delift_if_not_occur body =
41 Some (NCicSubstitution.psubst ~avoid_beta_redexes:true
42 (fun () -> raise WrongShape) [()] body)
43 with WrongShape -> None
45 let rec eat_lambdas ctx = function
46 | NCic.Lambda (name, src, tgt) ->
47 eat_lambdas ((name, src) :: ctx) tgt
48 | NCic.Meta (i,lc) as t->
50 let _,_,t,_ = NCicUtils.lookup_subst i subst in
51 let t = NCicSubstitution.subst_meta lc t in
53 with Not_found -> ctx, t)
56 let context_body = eat_lambdas [] t in
57 let rec aux = function
59 | (name, src)::ctx, (NCic.Appl (hd::[NCic.Rel 1]) as bo) ->
60 (match delift_if_not_occur hd with
61 | None -> aux (ctx,NCic.Lambda(name,src, bo))
62 | Some bo -> aux (ctx,bo))
63 | (name, src)::ctx, (NCic.Appl args as bo)
64 when HExtlib.list_last args = NCic.Rel 1 ->
65 let args, _ = HExtlib.split_nth (List.length args - 1) args in
66 (match delift_if_not_occur (NCic.Appl args) with
67 | None -> aux (ctx,NCic.Lambda(name,src, bo))
68 | Some bo -> aux (ctx,bo))
69 | (name, src) :: ctx, t ->
70 aux (ctx,NCic.Lambda(name,src, t))
76 module Ref = NReference;;
78 let debug = ref false;;
83 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
88 let time1 = Unix.gettimeofday () in
89 indent := !indent ^ String.make 1 c;
90 times := time1 :: !times;
91 prerr_endline ("{{{" ^ !indent ^ " ")
97 let time2 = Unix.gettimeofday () in
99 match !times with time1::tl -> times := tl; time1 | [] -> assert false in
100 prerr_endline ("}}} " ^ string_of_float (time2 -. time1));
102 | Some e -> prerr_endline ("exception raised: " ^ Printexc.to_string e)
105 indent := String.sub !indent 0 (String.length !indent -1)
107 Invalid_argument _ -> indent := "??"; ()
111 let ppcontext ~metasenv ~subst c =
112 "\nctx:\n"^ NCicPp.ppcontext ~metasenv ~subst c
114 let ppmetasenv ~subst m = "\nmenv:\n" ^ NCicPp.ppmetasenv ~subst m;;
116 let ppcontext ~metasenv:_metasenv ~subst:_subst _context = "";;
117 let ppmetasenv ~subst:_subst _metasenv = "";;
119 let fix_sorts swap exc t =
120 let rec aux () = function
121 | NCic.Sort (NCic.Type u) as orig ->
123 match NCicEnvironment.sup u with
125 prerr_endline ("no sup for " ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] orig) ;
127 | Some u1 -> if u = u1 then orig else NCic.Sort (NCic.Type u1)
129 NCic.Sort (NCic.Type (
130 match NCicEnvironment.sup NCicEnvironment.type0 with
132 | _ -> assert false))
133 | NCic.Meta _ as orig -> orig
134 | t -> NCicUtils.map (fun _ _ -> ()) () aux t
139 let is_locked n subst =
141 match NCicUtils.lookup_subst n subst with
142 | tag, _,_,_ when NCicMetaSubst.is_out_scope_tag tag -> true
144 with NCicUtils.Subst_not_found _ -> false
147 let rec mk_irl stop base =
148 if base > stop then []
149 else (NCic.Rel base) :: mk_irl stop (base+1)
152 (* the argument must be a term in whd *)
153 let rec could_reduce =
156 | C.Appl (C.Const (Ref.Ref (_,Ref.Fix (_,recno,_)))::args)
157 when List.length args > recno -> could_reduce (List.nth args recno)
158 | C.Match (_,_,arg,_) -> could_reduce arg
159 | C.Appl (he::_) -> could_reduce he
160 | C.Sort _ | C.Rel _ | C.Prod _ | C.Lambda _ | C.Const _ -> false
161 | C.Appl [] | C.LetIn _ | C.Implicit _ -> assert false
164 let rec lambda_intros rdb metasenv subst context argsno ty =
165 pp (lazy ("LAMBDA INTROS: " ^ NCicPp.ppterm ~metasenv ~subst ~context ty));
168 let metasenv, _, bo, _ =
169 NCicMetaSubst.mk_meta metasenv context (`WithType ty)
173 (match NCicReduction.whd ~subst context ty with
176 lambda_intros rdb metasenv subst
177 ((n,C.Decl so)::context) (argsno - 1) ta
179 metasenv,C.Lambda (n,so,bo)
183 let rec instantiate rdb test_eq_only metasenv subst context n lc t swap =
184 (*D*) inside 'I'; try let rc =
185 pp (lazy(string_of_int n ^ " :=?= "^
186 NCicPp.ppterm ~metasenv ~subst ~context t));
187 let unify test_eq_only m s c t1 t2 =
188 if swap then unify rdb test_eq_only m s c t2 t1
189 else unify rdb test_eq_only m s c t1 t2
191 let has_tag = List.exists in
192 let tags, _, ty = NCicUtils.lookup_meta n metasenv in
194 let metasenv, subst, t =
196 | NCic.Implicit (`Typeof _) ->
197 pp(lazy("meta with no type"));
198 assert(has_tag ((=)`IsSort) tags);
202 UnificationFailure (mk_msg metasenv subst context (NCic.Meta (n,lc)) t)
205 try t, NCicTypeChecker.typeof ~subst ~metasenv context t
207 | NCicTypeChecker.AssertFailure msg as exn ->
208 pp(lazy("we try to fix the sort\n"^
209 Lazy.force msg^"\n"^NCicPp.ppmetasenv ~subst metasenv));
210 let ft = fix_sorts swap exc_to_be t in
211 pp(lazy("unable to fix the sort"));
212 if ft == t then raise exn;
213 (try ft, NCicTypeChecker.typeof ~subst ~metasenv context ft
214 with NCicTypeChecker.AssertFailure _ -> raise exn)
215 | NCicTypeChecker.TypeCheckerFailure msg ->
216 prerr_endline (Lazy.force msg);
217 prerr_endline (NCicPp.ppterm ~metasenv ~subst ~context t);
218 prerr_endline (ppcontext ~metasenv ~subst context);
219 prerr_endline (ppmetasenv ~subst metasenv);
223 | NCic.Implicit (`Typeof _) ->
224 raise (UnificationFailure(lazy "trying to unify a term with a type"))
226 let lty = NCicSubstitution.subst_meta lc ty in
227 pp (lazy ("On the types: " ^
228 NCicPp.ppterm ~metasenv ~subst ~context lty ^ " === " ^
229 NCicPp.ppterm ~metasenv ~subst ~context ty_t));
231 try unify test_eq_only metasenv subst context lty ty_t
232 with NCicEnvironment.BadConstraint _ as exc ->
233 let ty_t = fix_sorts swap exc_to_be ty_t in
234 try unify test_eq_only metasenv subst context lty ty_t
236 | NCicEnvironment.BadConstraint _
237 | UnificationFailure _ -> raise exc
241 (* viral sortification *)
242 let is_sort metasenv subst context t =
243 match NCicReduction.whd ~subst context t with
245 let tags, _, _ = NCicUtils.lookup_meta i metasenv in
246 has_tag ((=) `IsSort) tags
247 | NCic.Sort _ -> true
250 let rec sortify metasenv subst = function
251 | NCic.Implicit (`Typeof _) -> assert false
252 | NCic.Sort _ as t -> metasenv, subst, t, 0
253 | NCic.Meta (i,_) as t ->
254 let tags, context, ty = NCicUtils.lookup_meta i metasenv in
255 if has_tag ((=) `IsSort) tags then metasenv, subst, t, i
257 let ty = NCicReduction.whd ~subst context ty in
258 let metasenv, subst, ty, _ = sortify metasenv subst ty in
259 let metasenv, j, m, _ =
260 NCicMetaSubst.mk_meta metasenv ~attrs:[`IsSort] [] (`WithType ty)
262 pp(lazy("rimpiazzo " ^ string_of_int i^" con "^string_of_int j));
263 let subst_entry = i, (tags, context, m, ty) in
264 let subst = subst_entry :: subst in
265 let metasenv = List.filter (fun x,_ -> i <> x) metasenv in
266 metasenv, subst, m, j
267 | NCic.Appl (NCic.Meta _ as hd :: args) as t ->
268 let metasenv, lambda_Mj =
269 lambda_intros rdb metasenv subst context (List.length args)
270 (NCicTypeChecker.typeof ~metasenv ~subst context hd)
272 let metasenv,subst= unify true metasenv subst context hd lambda_Mj in
273 let t = NCicReduction.whd ~subst context t in
274 let _result = sortify metasenv subst t in
275 (* untested, maybe dead, code *) assert false;
277 if could_reduce t then raise (Uncertain(lazy "not a sort"))
278 else raise (UnificationFailure(lazy "not a sort"))
280 let metasenv, subst, _, n =
281 if has_tag ((=) `IsSort) tags then
282 let m,s,x,_ = sortify metasenv subst (NCicReduction.whd ~subst context t)
284 else if is_sort metasenv subst context t then
285 sortify metasenv subst (NCic.Meta (n,lc))
287 metasenv, subst, NCic.Rel ~-1,n
289 let tags, ctx, ty = NCicUtils.lookup_meta n metasenv in
291 pp (lazy(string_of_int n ^ " := 111 = "^
292 NCicPp.ppterm ~metasenv ~subst ~context t));
293 let (metasenv, subst), t =
296 ~unify:(fun m s c t1 t2 ->
299 try Some (unify test_eq_only m s c t1 t2 )
300 with UnificationFailure _ | Uncertain _ -> None
303 metasenv subst context n lc t
304 with NCicMetaSubst.Uncertain msg ->
305 pp (lazy ("delift fails: " ^ Lazy.force msg));
306 raise (Uncertain msg)
307 | NCicMetaSubst.MetaSubstFailure msg ->
308 pp (lazy ("delift fails: " ^ Lazy.force msg));
309 raise (UnificationFailure msg)
311 pp (lazy(string_of_int n ^ " := 222 = "^
312 NCicPp.ppterm ~metasenv ~subst ~context:ctx t
313 ^ ppmetasenv ~subst metasenv));
314 (* Unifying the types may have already instantiated n. *)
316 let _, _,oldt,_ = NCicUtils.lookup_subst n subst in
317 let oldt = NCicSubstitution.subst_meta lc oldt in
318 let t = NCicSubstitution.subst_meta lc t in
319 (* conjecture: always fail --> occur check *)
320 unify test_eq_only metasenv subst context oldt t
321 with NCicUtils.Subst_not_found _ ->
323 let rec aux = function
324 | NCic.Meta (j,lc) ->
326 let _, _, t, _ = NCicUtils.lookup_subst j subst in
327 aux (NCicSubstitution.subst_meta lc t)
328 with NCicUtils.Subst_not_found _ ->
329 let tags', ctx, ty = NCicUtils.lookup_meta j metasenv in
330 let metasenv = List.remove_assoc j metasenv in
331 let tags = tags @ tags' in
332 (j, (tags, ctx, ty)) :: metasenv, tags)
333 | _ -> metasenv, tags
337 (* by cumulativity when unify(?,Type_i)
338 * we could ? := Type_j with j <= i... *)
339 let subst = (n, (tags, ctx, t, ty)) :: subst in
340 pp (lazy ("?"^string_of_int n^" := "^NCicPp.ppterm
341 ~metasenv ~subst ~context (NCicSubstitution.subst_meta lc t)));
343 List.filter (fun (m,_) -> not (n = m)) metasenv
346 (*D*) in outside None; rc with exn -> outside (Some exn); raise exn
348 and unify rdb test_eq_only metasenv subst context t1 t2 =
349 (*D*) inside 'U'; try let rc =
350 let fo_unif test_eq_only metasenv subst (norm1,t1) (norm2,t2) =
351 (*D*) inside 'F'; try let rc =
352 pp (lazy(" " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " ==?== " ^
353 NCicPp.ppterm ~metasenv ~subst ~context t2 ^ ppmetasenv
355 pp (lazy(" " ^ NCicPp.ppterm ~metasenv ~subst:[] ~context t1 ^ " ==??== " ^
356 NCicPp.ppterm ~metasenv ~subst:[] ~context t2 ^ ppmetasenv
362 | C.Appl [_], _ | _, C.Appl [_] | C.Appl [], _ | _, C.Appl []
363 | C.Appl (C.Appl _::_), _ | _, C.Appl (C.Appl _::_) ->
364 prerr_endline "Appl [Appl _;_] or Appl [] or Appl [_] invariant";
366 | (C.Sort (C.Type a), C.Sort (C.Type b)) when not test_eq_only ->
367 if NCicEnvironment.universe_leq a b then metasenv, subst
368 else raise (UnificationFailure (mk_msg metasenv subst context t1 t2))
369 | (C.Sort (C.Type a), C.Sort (C.Type b)) ->
370 if NCicEnvironment.universe_eq a b then metasenv, subst
371 else raise (UnificationFailure (mk_msg metasenv subst context t1 t2))
372 | (C.Sort C.Prop,C.Sort (C.Type _)) ->
373 if (not test_eq_only) then metasenv, subst
374 else raise (UnificationFailure (mk_msg metasenv subst context t1 t2))
376 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2))
377 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
378 let metasenv, subst = unify rdb true metasenv subst context s1 s2 in
379 unify rdb test_eq_only metasenv subst ((name1, C.Decl s1)::context) t1 t2
380 | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
381 let metasenv,subst=unify rdb test_eq_only metasenv subst context ty1 ty2 in
382 let metasenv,subst=unify rdb test_eq_only metasenv subst context s1 s2 in
383 let context = (name1, C.Def (s1,ty1))::context in
384 unify rdb test_eq_only metasenv subst context t1 t2
386 | (C.Meta (n1,(s1,l1 as lc1)),C.Meta (n2,(s2,l2 as lc2))) when n1 = n2 ->
388 let l1 = NCicUtils.expand_local_context l1 in
389 let l2 = NCicUtils.expand_local_context l2 in
390 let metasenv, subst, to_restrict, _ =
392 (fun t1 t2 (metasenv, subst, to_restrict, i) ->
394 let metasenv, subst =
395 unify rdb test_eq_only metasenv subst context
396 (NCicSubstitution.lift s1 t1) (NCicSubstitution.lift s2 t2)
398 metasenv, subst, to_restrict, i-1
399 with UnificationFailure _ | Uncertain _ ->
400 metasenv, subst, i::to_restrict, i-1)
401 l1 l2 (metasenv, subst, [], List.length l1)
403 if to_restrict <> [] then
404 let metasenv, subst, _ =
405 NCicMetaSubst.restrict metasenv subst n1 to_restrict
410 | Invalid_argument _ -> assert false
411 | NCicMetaSubst.MetaSubstFailure msg ->
413 let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
414 let term1 = NCicSubstitution.subst_meta lc1 term in
415 let term2 = NCicSubstitution.subst_meta lc2 term in
416 unify rdb test_eq_only metasenv subst context term1 term2
417 with NCicUtils.Subst_not_found _-> raise (UnificationFailure msg))
419 | NCic.Appl (NCic.Meta (i,_)::_ as l1),
420 NCic.Appl (NCic.Meta (j,_)::_ as l2) when i=j ->
423 (fun (metasenv, subst) t1 t2 ->
424 unify rdb test_eq_only metasenv subst context t1 t2)
425 (metasenv,subst) l1 l2
426 with Invalid_argument _ ->
427 raise (UnificationFailure (mk_msg metasenv subst context t1 t2)))
429 | _, NCic.Meta (n, _) when is_locked n subst ->
430 (let (metasenv, subst), i =
431 match NCicReduction.whd ~subst context t1 with
432 | NCic.Appl (NCic.Meta (i,l) as meta :: args) ->
433 let metasenv, lambda_Mj =
434 lambda_intros rdb metasenv subst context (List.length args)
435 (NCicTypeChecker.typeof ~metasenv ~subst context meta)
437 unify rdb test_eq_only metasenv subst context
438 (C.Meta (i,l)) lambda_Mj,
440 | NCic.Meta (i,_) -> (metasenv, subst), i
442 raise (UnificationFailure (lazy "Locked term vs non
443 flexible term; probably not saturated enough yet!"))
445 let t1 = NCicReduction.whd ~subst context t1 in
447 match t1 with NCic.Meta (j,l) -> j, l | _ -> assert false
449 let metasenv, subst =
450 instantiate rdb test_eq_only metasenv subst context j lj t2 true
452 (* We need to remove the out_scope_tags to avoid propagation of
453 them that triggers again the ad-hoc case *)
455 List.map (fun (i,(tag,ctx,bo,ty)) ->
458 (function `InScope | `OutScope _ -> false | _ -> true) tag
464 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
465 let term = eta_reduce subst term in
466 let subst = List.filter (fun (j,_) -> j <> i) subst in
467 metasenv, ((i, (name, ctx, term, ty)) :: subst)
468 with Not_found -> assert false))
470 | C.Meta (n,lc), t when List.mem_assoc n subst ->
471 let _,_,term,_ = NCicUtils.lookup_subst n subst in
472 let term = NCicSubstitution.subst_meta lc term in
473 unify rdb test_eq_only metasenv subst context term t
475 | t, C.Meta (n,lc) when List.mem_assoc n subst ->
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
480 | NCic.Appl (NCic.Meta (i,l)::args), _ when List.mem_assoc i subst ->
481 let _,_,term,_ = NCicUtils.lookup_subst i subst in
482 let term = NCicSubstitution.subst_meta l term in
483 unify rdb test_eq_only metasenv subst context
484 (mk_appl ~upto:(List.length args) term args) t2
486 | _, NCic.Appl (NCic.Meta (i,l)::args) when List.mem_assoc i subst ->
487 let _,_,term,_ = NCicUtils.lookup_subst i subst in
488 let term = NCicSubstitution.subst_meta l term in
489 unify rdb test_eq_only metasenv subst context t1
490 (mk_appl ~upto:(List.length args) term args)
492 | C.Meta (n,lc), t ->
493 instantiate rdb test_eq_only metasenv subst context n lc
494 (NCicReduction.head_beta_reduce ~subst t) false
496 | t, C.Meta (n,lc) ->
497 instantiate rdb test_eq_only metasenv subst context n lc
498 (NCicReduction.head_beta_reduce ~subst t) true
500 | NCic.Appl (NCic.Meta (i,l) as meta :: args), _ ->
501 let metasenv, lambda_Mj =
502 lambda_intros rdb metasenv subst context (List.length args)
503 (NCicTypeChecker.typeof ~metasenv ~subst context meta)
505 let metasenv, subst =
507 unify rdb test_eq_only metasenv subst context
508 (C.Meta (i,l)) lambda_Mj
509 with UnificationFailure msg | Uncertain msg when not norm2->
510 (* failure: let's try again argument vs argument *)
511 raise (KeepReducing msg)
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) as meta :: args) ->
524 let metasenv, lambda_Mj =
525 lambda_intros rdb metasenv subst context (List.length args)
526 (NCicTypeChecker.typeof ~metasenv ~subst context meta)
528 let metasenv, subst =
530 unify rdb test_eq_only metasenv subst context
531 lambda_Mj (C.Meta (i,l))
532 with UnificationFailure msg | Uncertain msg when not norm1 ->
533 (* failure: let's try again argument vs argument *)
534 raise (KeepReducing msg)
536 let metasenv, subst =
537 unify rdb test_eq_only metasenv subst context t1 t2
540 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
541 let term = eta_reduce subst term in
542 let subst = List.filter (fun (j,_) -> j <> i) subst in
543 metasenv, ((i, (name, ctx, term, ty)) :: subst)
544 with Not_found -> assert false)
546 (* processing this case here we avoid a useless small delta step *)
547 | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2))
549 let relevance = NCicEnvironment.get_relevance r1 in
550 let metasenv, subst, _ =
553 (fun (metasenv, subst, relevance) t1 t2 ->
555 match relevance with b::tl -> b,tl | _ -> true, [] in
556 let metasenv, subst =
557 try unify rdb test_eq_only metasenv subst context t1 t2
558 with UnificationFailure _ | Uncertain _ when not b ->
561 metasenv, subst, relevance)
562 (metasenv, subst, relevance) tl1 tl2
564 Invalid_argument _ ->
565 raise (Uncertain (mk_msg metasenv subst context t1 t2))
566 | UnificationFailure _ | Uncertain _ when not (norm1 && norm2) ->
567 raise (KeepReducing (mk_msg metasenv subst context t1 t2))
568 | KeepReducing _ | KeepReducingThis _ -> assert false
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 (Uncertain (mk_msg 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 norm1 && norm2 ->
607 if (could_reduce t1 || could_reduce t2) then
608 raise (Uncertain (mk_msg metasenv subst context t1 t2))
610 raise (UnificationFailure (mk_msg metasenv subst context t1 t2))
611 | _ -> raise (KeepReducing (mk_msg metasenv subst context t1 t2))
612 (*D*) in outside None; rc with exn -> outside (Some exn); raise exn
614 let try_hints metasenv subst (_,t1 as mt1) (_,t2 as mt2) (* exc*) =
615 (*D*) inside 'H'; try let rc =
616 pp(lazy ("\nProblema:\n" ^
617 NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " =?= " ^
618 NCicPp.ppterm ~metasenv ~subst ~context t2));
620 NCicUnifHint.look_for_hint rdb metasenv subst context t1 t2
622 let rec cand_iter = function
623 | [] -> None (* raise exc *)
624 | (metasenv,(c1,c2),premises)::tl ->
625 pp (lazy ("\nProvo il candidato:\n" ^
629 NCicPp.ppterm ~metasenv ~subst ~context a ^ " =?= " ^
630 NCicPp.ppterm ~metasenv ~subst ~context b) premises) ^
631 "\n-------------------------------------------\n"^
632 NCicPp.ppterm ~metasenv ~subst ~context c1 ^ " = " ^
633 NCicPp.ppterm ~metasenv ~subst ~context c2));
635 (*D*) inside 'K'; try let rc =
637 fo_unif test_eq_only metasenv subst mt1 (false,c1) in
639 fo_unif test_eq_only metasenv subst (false,c2) mt2 in
642 (fun (metasenv, subst) (x,y) ->
643 unify rdb test_eq_only metasenv subst context x y)
644 (metasenv, subst) premises
646 pp(lazy("FUNZIONA!"));
647 Some (metasenv, subst)
648 (*D*) in outside None; rc with exn -> outside (Some exn); raise exn
650 KeepReducing _ | UnificationFailure _ | Uncertain _ -> cand_iter tl
651 | KeepReducingThis _ -> assert false
654 (*D*) in outside None; rc with exn -> outside (Some exn); raise exn
656 let put_in_whd m1 m2 =
657 NCicReduction.reduce_machine ~delta:max_int ~subst context m1,
658 NCicReduction.reduce_machine ~delta:max_int ~subst context m2
660 let fo_unif_w_hints test_eq_only metasenv subst (xx1,t1 as m1) (xx2,t2 as m2) =
661 try fo_unif test_eq_only metasenv subst m1 m2
663 | UnificationFailure _ as exn -> raise exn
664 | KeepReducing _ | Uncertain _ as exn ->
665 let (t1,norm1 as tm1),(t2,norm2 as tm2) =
666 put_in_whd (0,[],t1,[]) (0,[],t2,[])
669 try_hints metasenv subst
670 (norm1,NCicReduction.unwind t1) (norm2,NCicReduction.unwind t2)
675 | KeepReducing msg -> raise (KeepReducingThis (msg,tm1,tm2))
676 | Uncertain _ as exn -> raise exn
679 let height_of = function
680 | NCic.Const (Ref.Ref (_,Ref.Def h))
681 | NCic.Const (Ref.Ref (_,Ref.Fix (_,_,h)))
682 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Def h))::_)
683 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
686 let small_delta_step ~subst
687 ((_,_,t1,_ as m1, norm1) as x1) ((_,_,t2,_ as m2, norm2) as x2)
689 assert (not (norm1 && norm2));
691 x1,NCicReduction.reduce_machine ~delta:0 ~subst context m2
693 NCicReduction.reduce_machine ~delta:0 ~subst context m1,x2
695 let h1 = height_of t1 in
696 let h2 = height_of t2 in
697 let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
698 NCicReduction.reduce_machine ~delta ~subst context m1,
699 NCicReduction.reduce_machine ~delta ~subst context m2
701 let rec unif_machines metasenv subst =
703 | ((k1,e1,t1,s1),norm1 as m1),((k2,e2,t2,s2),norm2 as m2) ->
704 (*D*) inside 'M'; try let rc =
706 NCicPp.ppterm ~metasenv ~subst ~context
707 (NCicReduction.unwind (k1,e1,t1,s1)) ^
709 NCicPp.ppterm ~metasenv ~subst ~context
710 (NCicReduction.unwind (k2,e2,t2,s2))));
711 pp (lazy (string_of_bool norm1 ^ " ?? " ^ string_of_bool norm2));
712 let relevance = [] (* TO BE UNDERSTOOD
714 | C.Const r -> NCicEnvironment.get_relevance r
716 let unif_from_stack t1 t2 b metasenv subst =
718 let t1 = NCicReduction.from_stack ~delta:max_int t1 in
719 let t2 = NCicReduction.from_stack ~delta:max_int t2 in
720 unif_machines metasenv subst (put_in_whd t1 t2)
721 with UnificationFailure _ | Uncertain _ when not b ->
724 let rec check_stack l1 l2 r todo =
726 | x1::tl1, x2::tl2, r::tr-> check_stack tl1 tl2 tr ((x1,x2,r)::todo)
727 | x1::tl1, x2::tl2, []-> check_stack tl1 tl2 [] ((x1,x2,true)::todo)
729 NCicReduction.unwind (k1,e1,t1,List.rev l1),
730 NCicReduction.unwind (k2,e2,t2,List.rev l2),
733 let hh1,hh2,todo=check_stack (List.rev s1) (List.rev s2) relevance [] in
736 fo_unif_w_hints test_eq_only metasenv subst (norm1,hh1) (norm2,hh2) in
738 (fun (metasenv,subst) (x1,x2,r) ->
739 unif_from_stack x1 x2 r metasenv subst
740 ) (metasenv,subst) todo
742 | KeepReducing _ -> assert false
743 | KeepReducingThis _ ->
744 assert (not (norm1 && norm2));
745 unif_machines metasenv subst (small_delta_step ~subst m1 m2)
746 | UnificationFailure _ | Uncertain _ when (not (norm1 && norm2))
747 -> unif_machines metasenv subst (small_delta_step ~subst m1 m2)
748 | UnificationFailure msg
749 when could_reduce (NCicReduction.unwind (fst m1))
750 || could_reduce (NCicReduction.unwind (fst m2))
751 -> raise (Uncertain msg)
752 (*D*) in outside None; rc with exn -> outside (Some exn); raise exn
754 try fo_unif_w_hints test_eq_only metasenv subst (false,t1) (false,t2)
756 | KeepReducingThis (msg,tm1,tm2) ->
758 unif_machines metasenv subst (tm1,tm2)
760 | UnificationFailure _ -> raise (UnificationFailure msg)
761 | Uncertain _ -> raise (Uncertain msg)
762 | KeepReducing _ -> assert false)
763 | KeepReducing _ -> assert false
765 (*D*) in outside None; rc with KeepReducing _ -> assert false | exn -> outside (Some exn); raise exn
767 and delift_type_wrt_terms rdb metasenv subst context t args =
768 let lc = List.rev args @ mk_irl (List.length context) (List.length args+1) in
769 let (metasenv, subst), t =
772 ~unify:(fun m s c t1 t2 ->
775 try Some (unify rdb false m s c t1 t2 )
776 with UnificationFailure _ | Uncertain _ -> None
779 metasenv subst context 0 (0,NCic.Ctx lc) t
781 NCicMetaSubst.MetaSubstFailure _
782 | NCicMetaSubst.Uncertain _ -> (metasenv, subst), t
788 let unify rdb ?(test_eq_only=false) =
790 unify rdb test_eq_only;;
792 let fix_sorts = fix_sorts true (UnificationFailure (lazy "no sup"));;