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))
43 | NCic.Appl (NCic.Meta _::_) -> true
47 exception WrongShape;;
50 let delift_if_not_occur body orig =
52 NCicSubstitution.psubst ~avoid_beta_redexes:true
53 (fun () -> raise WrongShape) [()] body
54 with WrongShape -> orig
57 | NCic.Lambda(_, _, NCic.Appl [hd; NCic.Rel 1]) as orig ->
58 delift_if_not_occur hd orig
59 | NCic.Lambda(_, _, NCic.Appl (hd :: l)) as orig
60 when HExtlib.list_last l = NCic.Rel 1 ->
62 let args, _ = HExtlib.split_nth (List.length l - 1) l in
65 delift_if_not_occur body orig
70 module Ref = NReference;;
73 let inside c = indent := !indent ^ String.make 1 c;;
74 let outside () = indent := String.sub !indent 0 (String.length !indent -1);;
77 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
82 let fix_sorts swap metasenv subst context meta t =
83 let rec aux () = function
84 | NCic.Sort (NCic.Type u) as orig ->
86 match NCicEnvironment.sup u with
87 | None -> prerr_endline "no sup for" ;
88 raise (fail_exc metasenv subst context meta t)
89 | Some u1 -> if u = u1 then orig else NCic.Sort (NCic.Type u1)
91 NCic.Sort (NCic.Type (
92 match NCicEnvironment.sup NCicEnvironment.type0 with
95 | NCic.Meta _ as orig -> orig
96 | t -> NCicUtils.map (fun _ _ -> ()) () aux t
101 let rec beta_expand_many metasenv subst context t args =
102 let tty = NCicTypeChecker.typeof ~metasenv ~subst context t in
103 let argsty = List.map (NCicTypeChecker.typeof ~metasenv ~subst context) args in
104 let rec mk_lambda context n = function
106 let metasenv, _, bo, _ =
107 NCicMetaSubst.mk_meta metasenv context (`WithType tty) in
110 let name = "HBeta"^string_of_int n in
111 let ty = NCicSubstitution.lift n ty in
112 let metasenv, bo = mk_lambda ((name,NCic.Decl ty)::context) (n+1) tail in
113 metasenv, NCic.Lambda (name, ty, bo)
115 mk_lambda context 0 argsty
117 and instantiate hdb test_eq_only metasenv subst context n lc t swap =
118 (*D*) inside 'I'; try let rc =
119 pp (lazy(string_of_int n ^ " :=?= "^
120 NCicPp.ppterm ~metasenv ~subst ~context t));
121 let unify test_eq_only m s c t1 t2 =
122 if swap then unify hdb test_eq_only m s c t2 t1
123 else unify hdb test_eq_only m s c t1 t2
125 let name, ctx, ty = NCicUtils.lookup_meta n metasenv in
126 let metasenv, subst, t =
128 | NCic.Implicit (`Typeof _) ->
130 (* fix_sorts swap metasenv subst context (NCic.Meta(n,lc)) t *)
133 "typeof: " ^ NCicPp.ppterm ~metasenv ~subst ~context t ^ "\nctx:\n"^
134 NCicPp.ppcontext ~metasenv ~subst context ^ "\nmenv:\n"^
135 NCicPp.ppmetasenv ~subst metasenv));
137 try t, NCicTypeChecker.typeof ~subst ~metasenv context t
139 | NCicTypeChecker.AssertFailure msg ->
140 (pp (lazy "fine typeof (fallimento)");
141 let ft=fix_sorts swap metasenv subst context (NCic.Meta (n,lc)) t in
143 (prerr_endline ( ("ILLTYPED: " ^
144 NCicPp.ppterm ~metasenv ~subst ~context t
145 ^ "\nBECAUSE:" ^ Lazy.force msg ^ "\nCONTEXT:\n" ^
146 NCicPp.ppcontext ~metasenv ~subst context ^ "\nMENV:\n" ^
147 NCicPp.ppmetasenv ~subst metasenv
152 pp (lazy ("typeof: " ^
153 NCicPp.ppterm ~metasenv ~subst ~context ft));
154 ft, NCicTypeChecker.typeof ~subst ~metasenv context ft
155 with NCicTypeChecker.AssertFailure _ ->
157 | NCicTypeChecker.TypeCheckerFailure msg ->
158 prerr_endline (Lazy.force msg);
161 let lty = NCicSubstitution.subst_meta lc ty in
162 pp (lazy ("On the types: " ^
163 NCicPp.ppterm ~metasenv ~subst ~context:ctx ty ^ " ~~~ " ^
164 NCicPp.ppterm ~metasenv ~subst ~context lty ^ " === "
165 ^ NCicPp.ppterm ~metasenv ~subst ~context ty_t));
166 let metasenv,subst= unify test_eq_only metasenv subst context lty ty_t in
169 pp (lazy(string_of_int n ^ " := 111 = "^
170 NCicPp.ppterm ~metasenv ~subst ~context t));
171 let (metasenv, subst), t =
174 ~unify:(fun m s c t1 t2 ->
177 try Some (unify test_eq_only m s c t1 t2 )
178 with UnificationFailure _ | Uncertain _ -> None
181 metasenv subst context n lc t
182 with NCicMetaSubst.Uncertain msg ->
183 pp (lazy ("delift fails: " ^ Lazy.force msg));
184 raise (Uncertain msg)
185 | NCicMetaSubst.MetaSubstFailure msg ->
186 pp (lazy ("delift fails: " ^ Lazy.force msg));
187 raise (UnificationFailure msg)
189 pp (lazy(string_of_int n ^ " := 222 = "^
190 NCicPp.ppterm ~metasenv ~subst ~context:ctx t
191 ^ "\n" ^ NCicPp.ppmetasenv ~subst metasenv));
192 (* Unifying the types may have already instantiated n. *)
194 let _, _,oldt,_ = NCicUtils.lookup_subst n subst in
195 let oldt = NCicSubstitution.subst_meta lc oldt in
196 let t = NCicSubstitution.subst_meta lc t in
197 (* conjecture: always fail --> occur check *)
198 unify test_eq_only metasenv subst context oldt t
199 with NCicUtils.Subst_not_found _ ->
200 (* by cumulativity when unify(?,Type_i)
201 * we could ? := Type_j with j <= i... *)
202 let subst = (n, (name, ctx, t, ty)) :: subst in
203 pp (lazy ("?"^string_of_int n^" := "^NCicPp.ppterm
204 ~metasenv ~subst ~context (NCicSubstitution.subst_meta lc t)));
206 List.filter (fun (m,_) -> not (n = m)) metasenv
209 (*D*) in outside(); rc with exn -> outside (); raise exn
211 and unify hdb test_eq_only metasenv subst context t1 t2 =
212 (*D*) inside 'U'; try let rc =
213 let fo_unif test_eq_only metasenv subst t1 t2 =
214 (*D*) inside 'F'; try let rc =
215 pp (lazy(" " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " ==?== " ^
216 NCicPp.ppterm ~metasenv ~subst ~context t2 ^ "\n" ^ NCicPp.ppmetasenv
222 | C.Appl [_], _ | _, C.Appl [_] | C.Appl [], _ | _, C.Appl []
223 | C.Appl (C.Appl _::_), _ | _, C.Appl (C.Appl _::_) ->
224 prerr_endline "Appl [Appl _;_] or Appl [] or Appl [_] invariant";
226 | (C.Sort (C.Type a), C.Sort (C.Type b)) when not test_eq_only ->
227 if NCicEnvironment.universe_leq a b then metasenv, subst
228 else raise (fail_exc metasenv subst context t1 t2)
229 | (C.Sort (C.Type a), C.Sort (C.Type b)) ->
230 if NCicEnvironment.universe_eq a b then metasenv, subst
231 else raise (fail_exc metasenv subst context t1 t2)
232 | (C.Sort C.Prop,C.Sort (C.Type _)) ->
233 if (not test_eq_only) then metasenv, subst
234 else raise (fail_exc metasenv subst context t1 t2)
236 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2))
237 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
238 let metasenv, subst = unify hdb true metasenv subst context s1 s2 in
239 unify hdb test_eq_only metasenv subst ((name1, C.Decl s1)::context) t1 t2
240 | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
241 let metasenv,subst=unify hdb test_eq_only metasenv subst context ty1 ty2 in
242 let metasenv,subst=unify hdb test_eq_only metasenv subst context s1 s2 in
243 let context = (name1, C.Def (s1,ty1))::context in
244 unify hdb test_eq_only metasenv subst context t1 t2
246 | (C.Meta (n1,(s1,l1 as lc1)),C.Meta (n2,(s2,l2 as lc2))) when n1 = n2 ->
248 let l1 = NCicUtils.expand_local_context l1 in
249 let l2 = NCicUtils.expand_local_context l2 in
250 let metasenv, subst, to_restrict, _ =
252 (fun t1 t2 (metasenv, subst, to_restrict, i) ->
254 let metasenv, subst =
255 unify hdb test_eq_only metasenv subst context
256 (NCicSubstitution.lift s1 t1) (NCicSubstitution.lift s2 t2)
258 metasenv, subst, to_restrict, i-1
259 with UnificationFailure _ | Uncertain _ ->
260 metasenv, subst, i::to_restrict, i-1)
261 l1 l2 (metasenv, subst, [], List.length l1)
263 if to_restrict <> [] then
264 let metasenv, subst, _ =
265 NCicMetaSubst.restrict metasenv subst n1 to_restrict
270 | Invalid_argument _ -> assert false
271 | NCicMetaSubst.MetaSubstFailure msg ->
273 let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
274 let term1 = NCicSubstitution.subst_meta lc1 term in
275 let term2 = NCicSubstitution.subst_meta lc2 term in
276 unify hdb test_eq_only metasenv subst context term1 term2
277 with NCicUtils.Subst_not_found _-> raise (UnificationFailure msg))
279 | C.Meta (n,lc), t ->
281 let _,_,term,_ = NCicUtils.lookup_subst n subst in
282 let term = NCicSubstitution.subst_meta lc term in
283 unify hdb test_eq_only metasenv subst context term t
284 with NCicUtils.Subst_not_found _->
285 instantiate hdb test_eq_only metasenv subst context n lc
286 (NCicReduction.head_beta_reduce ~subst t) false)
288 | t, C.Meta (n,lc) ->
290 let _,_,term,_ = NCicUtils.lookup_subst n subst in
291 let term = NCicSubstitution.subst_meta lc term in
292 unify hdb test_eq_only metasenv subst context t term
293 with NCicUtils.Subst_not_found _->
294 instantiate hdb test_eq_only metasenv subst context n lc
295 (NCicReduction.head_beta_reduce ~subst t) true)
297 | NCic.Appl (NCic.Meta (i,l)::args), _ when List.mem_assoc i subst ->
298 let _,_,term,_ = NCicUtils.lookup_subst i subst in
299 let term = NCicSubstitution.subst_meta l term in
300 unify hdb test_eq_only metasenv subst context
301 (mk_appl ~upto:(List.length args) term args) t2
303 | _, NCic.Appl (NCic.Meta (i,l)::args) when List.mem_assoc i subst ->
304 let _,_,term,_ = NCicUtils.lookup_subst i subst in
305 let term = NCicSubstitution.subst_meta l term in
306 unify hdb test_eq_only metasenv subst context t1
307 (mk_appl ~upto:(List.length args) term args)
309 | NCic.Appl (NCic.Meta (i,_)::_ as l1),
310 NCic.Appl (NCic.Meta (j,_)::_ as l2) when i=j ->
313 (fun (metasenv, subst) t1 t2 ->
314 unify hdb test_eq_only metasenv subst context t1 t2)
315 (metasenv,subst) l1 l2
316 with Invalid_argument _ ->
317 raise (fail_exc metasenv subst context t1 t2))
319 | NCic.Appl (NCic.Meta (i,l)::args), _ when not (flexible args) ->
320 (* we verify that none of the args is a Meta,
321 since beta expanding w.r.t a metavariable makes no sense *)
322 let metasenv, beta_expanded =
323 beta_expand_many metasenv subst context t2 args
325 let metasenv, subst =
326 unify hdb test_eq_only metasenv subst context
327 (C.Meta (i,l)) beta_expanded
330 (* .... eta_reduce ?i ... *)
331 unify hdb test_eq_only metasenv subst context t1 t2
333 | _, NCic.Appl (NCic.Meta (i,l)::args) when not(flexible args) ->
334 let metasenv, beta_expanded =
335 beta_expand_many metasenv subst context t1 args
337 let metasenv, subst =
338 unify hdb test_eq_only metasenv subst context
339 beta_expanded (C.Meta (i,l))
341 unify hdb test_eq_only metasenv subst context t1 t2
343 (* processing this case here we avoid a useless small delta step *)
344 | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2))
346 let relevance = NCicEnvironment.get_relevance r1 in
347 let relevance = match r1 with
348 | Ref.Ref (_,Ref.Con (_,_,lno)) ->
349 let _,relevance = HExtlib.split_nth lno relevance in
350 HExtlib.mk_list false lno @ relevance
353 let metasenv, subst, _ =
356 (fun (metasenv, subst, relevance) t1 t2 ->
358 match relevance with b::tl -> b,tl | _ -> true, [] in
359 let metasenv, subst =
360 try unify hdb test_eq_only metasenv subst context t1 t2
361 with UnificationFailure _ | Uncertain _ when not b ->
364 metasenv, subst, relevance)
365 (metasenv, subst, relevance) tl1 tl2
366 with Invalid_argument _ ->
367 raise (uncert_exc metasenv subst context t1 t2)
371 | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
372 C.Match (ref2,outtype2,term2,pl2)) ->
373 let _,_,itl,_,_ = NCicEnvironment.get_checked_indtys ref1 in
374 let _,_,ty,_ = List.nth itl tyno in
375 let rec remove_prods ~subst context ty =
376 let ty = NCicReduction.whd ~subst context ty in
379 | C.Prod (name,so,ta) ->
380 remove_prods ~subst ((name,(C.Decl so))::context) ta
384 match remove_prods ~subst [] ty with
385 | C.Sort C.Prop -> true
388 if not (Ref.eq ref1 ref2) then
389 raise (uncert_exc metasenv subst context t1 t2)
391 let metasenv, subst =
392 unify hdb test_eq_only metasenv subst context outtype1 outtype2 in
393 let metasenv, subst =
394 try unify hdb test_eq_only metasenv subst context term1 term2
395 with UnificationFailure _ | Uncertain _ when is_prop ->
400 (fun (metasenv,subst) ->
401 unify hdb test_eq_only metasenv subst context)
402 (metasenv, subst) pl1 pl2
403 with Invalid_argument _ ->
404 raise (uncert_exc metasenv subst context t1 t2))
405 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
406 | _ when NCicUntrusted.metas_of_term subst context t1 = [] &&
407 NCicUntrusted.metas_of_term subst context t2 = [] ->
408 raise (fail_exc metasenv subst context t1 t2)
409 | _ -> raise (uncert_exc metasenv subst context t1 t2)
410 (*D*) in outside(); rc with exn -> outside (); raise exn
412 let try_hints metasenv subst t1 t2 (* exc*) =
414 prerr_endline ("\nProblema:\n" ^
415 NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " =?= " ^
416 NCicPp.ppterm ~metasenv ~subst ~context t2);
419 NCicUnifHint.look_for_hint hdb metasenv subst context t1 t2
421 let rec cand_iter = function
422 | [] -> None (* raise exc *)
423 | (metasenv,(c1,c2),premises)::tl ->
425 prerr_endline ("\nProvo il candidato:\n" ^
429 NCicPp.ppterm ~metasenv ~subst ~context a ^ " =?= " ^
430 NCicPp.ppterm ~metasenv ~subst ~context b) premises) ^
431 "\n-------------------------------------------\n"^
432 NCicPp.ppterm ~metasenv ~subst ~context c1 ^ " = " ^
433 NCicPp.ppterm ~metasenv ~subst ~context c2);
437 fo_unif test_eq_only metasenv subst t1 c1 in
439 fo_unif test_eq_only metasenv subst c2 t2 in
442 (fun (metasenv, subst) (x,y) ->
443 unify hdb test_eq_only metasenv subst context x y)
444 (metasenv, subst) premises
446 Some (metasenv, subst)
448 UnificationFailure _ | Uncertain _ ->
453 let height_of = function
454 | NCic.Const (Ref.Ref (_,Ref.Def h))
455 | NCic.Const (Ref.Ref (_,Ref.Fix (_,_,h)))
456 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Def h))::_)
457 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
460 let put_in_whd m1 m2 =
461 NCicReduction.reduce_machine ~delta:max_int ~subst context m1,
462 NCicReduction.reduce_machine ~delta:max_int ~subst context m2
464 let small_delta_step ~subst
465 ((_,_,t1,_ as m1, norm1) as x1) ((_,_,t2,_ as m2, norm2) as x2)
467 assert (not (norm1 && norm2));
469 x1,NCicReduction.reduce_machine ~delta:(height_of t2 -1) ~subst context m2
471 NCicReduction.reduce_machine ~delta:(height_of t1 -1) ~subst context m1,x2
473 let h1 = height_of t1 in
474 let h2 = height_of t2 in
475 let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
476 NCicReduction.reduce_machine ~delta ~subst context m1,
477 NCicReduction.reduce_machine ~delta ~subst context m2
479 let rec unif_machines metasenv subst =
481 | ((k1,e1,t1,s1),norm1 as m1),((k2,e2,t2,s2),norm2 as m2) ->
482 (*D*) inside 'M'; try let rc =
484 NCicPp.ppterm ~metasenv ~subst ~context
485 (NCicReduction.unwind (k1,e1,t1,s1)) ^
487 NCicPp.ppterm ~metasenv ~subst ~context
488 (NCicReduction.unwind (k2,e2,t2,s2))));
489 pp (lazy (string_of_bool norm1 ^ " ?? " ^ string_of_bool norm2));
490 let relevance = [] (* TO BE UNDERSTOOD
492 | C.Const r -> NCicEnvironment.get_relevance r
494 let unif_from_stack t1 t2 b metasenv subst =
496 let t1 = NCicReduction.from_stack t1 in
497 let t2 = NCicReduction.from_stack t2 in
498 unif_machines metasenv subst (put_in_whd t1 t2)
499 with UnificationFailure _ | Uncertain _ when not b ->
502 let rec check_stack l1 l2 r todo =
504 | x1::tl1, x2::tl2, r::tr-> check_stack tl1 tl2 tr ((x1,x2,r)::todo)
505 | x1::tl1, x2::tl2, []-> check_stack tl1 tl2 [] ((x1,x2,true)::todo)
507 NCicReduction.unwind (k1,e1,t1,List.rev l1),
508 NCicReduction.unwind (k2,e2,t2,List.rev l2),
511 let hh1,hh2,todo=check_stack (List.rev s1) (List.rev s2) relevance [] in
513 let metasenv,subst = fo_unif test_eq_only metasenv subst hh1 hh2 in
515 (fun (metasenv,subst) (x1,x2,r) ->
516 unif_from_stack x1 x2 r metasenv subst
517 ) (metasenv,subst) todo
518 with UnificationFailure _ | Uncertain _ when not (norm1 && norm2) ->
519 unif_machines metasenv subst (small_delta_step ~subst m1 m2)
520 (*D*) in outside(); rc with exn -> outside (); raise exn
522 try fo_unif test_eq_only metasenv subst t1 t2
524 | UnificationFailure msg as exn ->
526 unif_machines metasenv subst
527 (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
529 | UnificationFailure _ -> raise (UnificationFailure msg)
530 | Uncertain _ -> raise exn)
531 | Uncertain msg as exn ->
532 match try_hints metasenv subst t1 t2 with
536 unif_machines metasenv subst
537 (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
539 | UnificationFailure _ -> raise (UnificationFailure msg)
540 | Uncertain _ -> raise exn
541 (*D*) in outside(); rc with exn -> outside (); raise exn