1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 exception UnificationFailed;;
28 exception OccurCheck;;
29 exception RelToHiddenHypothesis;;
34 (* the delift function takes in input an ordered list of integers [n1,...,nk]
35 and a term t, and relocates rel(nk) to k. Typically, the list of integers
36 is a parameter of a metavariable occurrence. *)
38 exception NotInTheList;;
43 [] -> raise NotInTheList
44 | (Some (Cic.Rel m))::_ when m=n -> k
45 | _::tl -> aux (k+1) tl in
49 let restrict to_be_restricted =
53 | _::tl when List.mem (n,i) to_be_restricted ->
54 None::(erase (i+1) n tl)
55 | he::tl -> he::(erase (i+1) n tl) in
59 | (n,context,t)::tl -> (n,erase 1 n context,t)::(aux tl) in
64 let delift context metasenv l t =
65 let module S = CicSubstitution in
66 let to_be_restricted = ref [] in
72 C.Rel m (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
73 (*CSC: deliftato la regola per il LetIn *)
75 (match List.nth context (m-k-1) with
76 Some (_,C.Def t) -> deliftaux k (S.lift m t)
77 | Some (_,C.Decl t) ->
78 (* It may augment to_be_restricted *)
79 ignore (deliftaux k (S.lift m t)) ;
80 C.Rel ((position (m-k) l) + k)
81 | None -> raise RelToHiddenHypothesis)
83 | C.Meta (i, l1) as t ->
87 | None::tl -> None::(deliftl (j+1) tl)
89 let l1' = (deliftl (j+1) tl) in
91 Some (deliftaux k t)::l1'
95 to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
97 let l' = deliftl 1 l1 in
100 | C.Implicit as t -> t
101 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
102 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
103 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
104 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
105 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
106 | C.Const (uri,exp_named_subst) ->
107 let exp_named_subst' =
108 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
110 C.Const (uri,exp_named_subst')
111 | C.MutInd (uri,typeno,exp_named_subst) ->
112 let exp_named_subst' =
113 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
115 C.MutInd (uri,typeno,exp_named_subst')
116 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
117 let exp_named_subst' =
118 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
120 C.MutConstruct (uri,typeno,consno,exp_named_subst')
121 | C.MutCase (sp,i,outty,t,pl) ->
122 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
123 List.map (deliftaux k) pl)
125 let len = List.length fl in
128 (fun (name, i, ty, bo) ->
129 (name, i, deliftaux k ty, deliftaux (k+len) bo))
134 let len = List.length fl in
137 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
140 C.CoFix (i, liftedfl)
142 let res = deliftaux 0 t in
143 res, restrict !to_be_restricted metasenv
146 (**** END OF DELIFT ****)
148 type substitution = (int * Cic.term) list
150 (* NUOVA UNIFICAZIONE *)
151 (* A substitution is a (int * Cic.term) list that associates a
152 metavariable i with its body.
153 A metaenv is a (int * Cic.term) list that associate a metavariable
155 fo_unif_new takes a metasenv, a context, two terms t1 and t2 and gives back
156 a new substitution which is _NOT_ unwinded. It must be unwinded before
159 let rec fo_unif_subst subst context metasenv t1 t2 =
160 let module C = Cic in
161 let module R = CicReduction in
162 let module S = CicSubstitution in
164 (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
172 | Some t1', Some t2' ->
173 (* First possibility: restriction *)
174 (* Second possibility: unification *)
175 (* Third possibility: convertibility *)
176 R.are_convertible context t1' t2'
179 if ok then subst,metasenv else raise UnificationFailed
180 | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
181 fo_unif_subst subst context metasenv t2 t1
183 | (t, C.Meta (n,l)) ->
184 let subst',metasenv' =
186 let oldt = (List.assoc n subst) in
187 let lifted_oldt = S.lift_meta l oldt in
188 fo_unif_subst subst context metasenv lifted_oldt t
190 let t',metasenv' = delift context metasenv l t in
191 (n, t')::subst, metasenv'
193 let (_,_,meta_type) =
194 List.find (function (m,_,_) -> m=n) metasenv' in
195 let tyt = CicTypeChecker.type_of_aux' metasenv' context t in
196 fo_unif_subst subst' context metasenv' (S.lift_meta l meta_type) tyt
205 if R.are_convertible context t1 t2 then
208 raise UnificationFailed
209 | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
210 | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
211 | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) ->
212 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
213 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
214 | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) ->
215 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
216 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
217 | (C.LetIn (_,s1,t1), t2)
218 | (t2, C.LetIn (_,s1,t1)) ->
219 fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
220 | (C.Appl l1, C.Appl l2) ->
221 let lr1 = List.rev l1 in
222 let lr2 = List.rev l2 in
223 let rec fo_unif_l subst metasenv =
226 | _,[] -> assert false
228 fo_unif_subst subst context metasenv h1 h2
231 fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
232 | ((h1::l1),(h2::l2)) ->
233 let subst', metasenv' =
234 fo_unif_subst subst context metasenv h1 h2
236 fo_unif_l subst' metasenv' (l1,l2)
238 fo_unif_l subst metasenv (lr1, lr2)
243 | (C.MutConstruct _, _)
244 | (_, C.MutConstruct _) ->
245 if R.are_convertible context t1 t2 then
248 raise UnificationFailed
249 | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
250 let subst', metasenv' =
251 fo_unif_subst subst context metasenv outt1 outt2 in
252 let subst'',metasenv'' =
253 fo_unif_subst subst' context metasenv' t1 t2 in
255 (function (subst,metasenv) ->
256 fo_unif_subst subst context metasenv
257 ) (subst'',metasenv'') pl1 pl2
262 if R.are_convertible context t1 t2 then
265 raise UnificationFailed
266 | (_,_) -> raise UnificationFailed
269 (*CSC: ???????????????
270 (* m is the index of a metavariable to restrict, k is nesting depth
271 of the occurrence m, and l is its relocation list. canonical_context
272 is the context of the metavariable we are instantiating - containing
273 m - Only rel in the domain of canonical_context are accessible.
274 This function takes in input a metasenv and gives back a metasenv.
275 A rel(j) in the canonical context of m, is rel(List.nth l j) for the
276 instance of m under consideration, that is rel (List.nth l j) - k
277 in canonical_context. *)
279 let restrict canonical_context m k l =
283 | None::tl -> None::(erase (i+1) tl)
285 let i' = (List.nth l (i-1)) in
287 then he::(erase (i+1) tl) (* local variable *)
290 (try List.nth canonical_context (i'-k-1)
291 with Failure _ -> None) in
293 then None::(erase (i+1) tl)
294 else he::(erase (i+1) tl) in
298 | (n,context,t)::tl when n=m -> (n,erase 1 context,t)::tl
299 | hd::tl -> hd::(aux tl)
305 let check_accessibility metasenv i =
306 let module C = Cic in
307 let module S = CicSubstitution in
308 let (_,canonical_context,_) =
309 List.find (function (m,_,_) -> m=i) metasenv in
313 delift canonical_context metasenv ? t
319 let rec aux metasenv k =
326 match List.nth canonical_context (i-k-1) with
328 | Some (_,C.Def t) -> aux metasenv k (S.lift i t)
329 | None -> raise RelToHiddenHypothesis
331 Failure _ -> raise OpenTerm
333 | C.Var _ -> metasenv
334 | C.Meta (i,l) -> restrict canonical_context i k l metasenv
335 | C.Sort _ -> metasenv
336 | C.Implicit -> metasenv
338 let metasenv' = aux metasenv k te in
343 let metasenv' = aux metasenv k s in
344 aux metasenv' (k+1) t
347 (function metasenv -> aux metasenv k) metasenv l
350 | C.MutConstruct _ -> metasenv
351 | C.MutCase (_,_,_,outty,t,pl) ->
352 let metasenv' = aux metasenv k outty in
353 let metasenv'' = aux metasenv' k t in
355 (function metasenv -> aux metasenv k) metasenv'' pl
357 let len = List.length fl in
360 let (_,_,ty,bo) = f in
361 let metasenv' = aux metasenv k ty in
362 aux metasenv' (k+len) bo
365 let len = List.length fl in
369 let metasenv' = aux metasenv k ty in
370 aux metasenv' (k+len) bo
377 let unwind metasenv subst unwinded t =
378 let unwinded = ref unwinded in
379 let frozen = ref [] in
380 let rec um_aux metasenv =
381 let module C = Cic in
382 let module S = CicSubstitution in
384 C.Rel _ as t -> t,metasenv
385 | C.Var _ as t -> t,metasenv
388 S.lift_meta l (List.assoc i !unwinded), metasenv
390 if List.mem i !frozen then raise OccurCheck
392 let saved_frozen = !frozen in
393 frozen := i::!frozen ;
396 let t = List.assoc i subst in
397 let t',metasenv' = um_aux metasenv t in
399 let (_,canonical_context,_) =
400 List.find (function (m,_,_) -> m=i) metasenv
402 prerr_endline ("DELIFT(" ^ CicPp.ppterm t' ^ ")") ; flush stderr ;
403 List.iter (function (Some t) -> prerr_endline ("l: " ^ CicPp.ppterm t) | None -> prerr_endline " _ ") l ; flush stderr ;
404 prerr_endline "<DELIFT" ; flush stderr ;
405 delift canonical_context metasenv' l t'
407 unwinded := (i,t')::!unwinded ;
408 S.lift_meta l t', metasenv'
411 (* not constrained variable, i.e. free in subst*)
414 (fun t (tl,metasenv) ->
416 None -> None::tl,metasenv
418 let t',metasenv' = um_aux metasenv t in
419 (Some t')::tl, metasenv'
422 C.Meta (i,l'), metasenv'
424 frozen := saved_frozen ;
428 | C.Implicit as t -> t,metasenv
430 let te',metasenv' = um_aux metasenv te in
431 let ty',metasenv'' = um_aux metasenv' ty in
432 C.Cast (te',ty'),metasenv''
434 let s',metasenv' = um_aux metasenv s in
435 let t',metasenv'' = um_aux metasenv' t in
436 C.Prod (n, s', t'), metasenv''
437 | C.Lambda (n,s,t) ->
438 let s',metasenv' = um_aux metasenv s in
439 let t',metasenv'' = um_aux metasenv' t in
440 C.Lambda (n, s', t'), metasenv''
442 let s',metasenv' = um_aux metasenv s in
443 let t',metasenv'' = um_aux metasenv' t in
444 C.LetIn (n, s', t'), metasenv''
448 (fun t (tl,metasenv) ->
449 let t',metasenv' = um_aux metasenv t in
454 match um_aux metasenv' he with
455 (C.Appl l, metasenv'') -> C.Appl (l@tl'),metasenv''
456 | (he', metasenv'') -> C.Appl (he'::tl'),metasenv''
458 | C.Appl _ -> assert false
459 | C.Const (uri,exp_named_subst) ->
460 let exp_named_subst', metasenv' =
462 (fun (uri,t) (tl,metasenv) ->
463 let t',metasenv' = um_aux metasenv t in
464 (uri,t')::tl, metasenv'
465 ) exp_named_subst ([],metasenv)
467 C.Const (uri,exp_named_subst'),metasenv'
468 | C.MutInd (uri,typeno,exp_named_subst) ->
469 let exp_named_subst', metasenv' =
471 (fun (uri,t) (tl,metasenv) ->
472 let t',metasenv' = um_aux metasenv t in
473 (uri,t')::tl, metasenv'
474 ) exp_named_subst ([],metasenv)
476 C.MutInd (uri,typeno,exp_named_subst'),metasenv'
477 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
478 let exp_named_subst', metasenv' =
480 (fun (uri,t) (tl,metasenv) ->
481 let t',metasenv' = um_aux metasenv t in
482 (uri,t')::tl, metasenv'
483 ) exp_named_subst ([],metasenv)
485 C.MutConstruct (uri,typeno,consno,exp_named_subst'),metasenv'
486 | C.MutCase (sp,i,outty,t,pl) ->
487 let outty',metasenv' = um_aux metasenv outty in
488 let t',metasenv'' = um_aux metasenv' t in
489 let pl',metasenv''' =
491 (fun p (pl,metasenv) ->
492 let p',metasenv' = um_aux metasenv p in
496 C.MutCase (sp, i, outty', t', pl'),metasenv'''
498 let len = List.length fl in
499 let liftedfl,metasenv' =
501 (fun (name, i, ty, bo) (fl,metasenv) ->
502 let ty',metasenv' = um_aux metasenv ty in
503 let bo',metasenv'' = um_aux metasenv' bo in
504 (name, i, ty', bo')::fl,metasenv''
507 C.Fix (i, liftedfl),metasenv'
509 let len = List.length fl in
510 let liftedfl,metasenv' =
512 (fun (name, ty, bo) (fl,metasenv) ->
513 let ty',metasenv' = um_aux metasenv ty in
514 let bo',metasenv'' = um_aux metasenv' bo in
515 (name, ty', bo')::fl,metasenv''
518 C.CoFix (i, liftedfl),metasenv'
520 let t',metasenv' = um_aux metasenv t in
521 t',metasenv',!unwinded
524 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t *)
525 (* performs as (apply_subst subst t) until it finds an application of *)
526 (* (META [meta_to_reduce]) that, once unwinding is performed, creates *)
527 (* a new beta-redex; in this case up to [reductions_no] consecutive *)
528 (* beta-reductions are performed. *)
529 (* Hint: this function is usually called when [reductions_no] *)
530 (* eta-expansions have been performed and the head of the new *)
531 (* application has been unified with (META [meta_to_reduce]): *)
532 (* during the unwinding the eta-expansions are undone. *)
534 let apply_subst_reducing subst meta_to_reduce t =
535 let unwinded = ref subst in
537 let module C = Cic in
538 let module S = CicSubstitution in
542 | C.Meta (i,l) as t ->
544 S.lift_meta l (List.assoc i !unwinded)
548 | C.Implicit as t -> t
549 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
550 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
551 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
552 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
554 let tl' = List.map um_aux tl in
557 C.Appl l -> C.Appl (l@tl')
558 | _ as he' -> C.Appl (he'::tl')
561 match meta_to_reduce,he with
562 Some (mtr,reductions_no), C.Meta (m,_) when m = mtr ->
563 let rec beta_reduce =
565 (n,(C.Appl (C.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
566 let he'' = CicSubstitution.subst he' t in
570 beta_reduce (n-1,C.Appl(he''::tl'))
573 beta_reduce (reductions_no,t')
576 | C.Appl _ -> assert false
577 | C.Const (uri,exp_named_subst) ->
578 let exp_named_subst' =
579 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
581 C.Const (uri,exp_named_subst')
582 | C.MutInd (uri,typeno,exp_named_subst) ->
583 let exp_named_subst' =
584 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
586 C.MutInd (uri,typeno,exp_named_subst')
587 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
588 let exp_named_subst' =
589 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
591 C.MutConstruct (uri,typeno,consno,exp_named_subst')
592 | C.MutCase (sp,i,outty,t,pl) ->
593 C.MutCase (sp, i, um_aux outty, um_aux t,
596 let len = List.length fl in
599 (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo))
604 let len = List.length fl in
607 (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo))
610 C.CoFix (i, liftedfl)
615 (* UNWIND THE MGU INSIDE THE MGU *)
616 let unwind_subst metasenv subst =
617 let identity_relocation_list_for_metavariable i =
618 let (_,canonical_context,_) =
619 List.find (function (m,_,_) -> m=i) metasenv
621 let canonical_context_length = List.length canonical_context in
624 n when n > canonical_context_length -> []
625 | n -> (Some (Cic.Rel n))::(aux (n+1))
630 (fun (unwinded,metasenv) (i,_) ->
631 let identity_relocation_list =
632 identity_relocation_list_for_metavariable i
634 let (_,metasenv',subst') =
635 unwind metasenv subst unwinded (Cic.Meta (i,identity_relocation_list))
638 ) ([],metasenv) subst
641 let apply_subst subst t =
642 (* metasenv will not be used nor modified. So, let's use a dummy empty one *)
644 let (t',_,_) = unwind metasenv [] subst t in
648 (* A substitution is a (int * Cic.term) list that associates a *)
649 (* metavariable i with its body. *)
650 (* metasenv is of type Cic.metasenv *)
651 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back *)
652 (* a new substitution which is already unwinded and ready to be applied and *)
653 (* a new metasenv in which some hypothesis in the contexts of the *)
654 (* metavariables may have been restricted. *)
655 let fo_unif metasenv context t1 t2 =
656 prerr_endline "INIZIO FASE 1" ; flush stderr ;
657 let subst_to_unwind,metasenv' = fo_unif_subst [] context metasenv t1 t2 in
658 prerr_endline "FINE FASE 1" ; flush stderr ;
660 unwind_subst metasenv' subst_to_unwind
662 prerr_endline "FINE FASE 2" ; flush stderr ; res