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)
82 | C.Var (uri,exp_named_subst) ->
83 let exp_named_subst' =
84 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
86 C.Var (uri,exp_named_subst')
87 | C.Meta (i, l1) as t ->
91 | None::tl -> None::(deliftl (j+1) tl)
93 let l1' = (deliftl (j+1) tl) in
95 Some (deliftaux k t)::l1'
99 to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
101 let l' = deliftl 1 l1 in
104 | C.Implicit as t -> t
105 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
106 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
107 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
108 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
109 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
110 | C.Const (uri,exp_named_subst) ->
111 let exp_named_subst' =
112 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
114 C.Const (uri,exp_named_subst')
115 | C.MutInd (uri,typeno,exp_named_subst) ->
116 let exp_named_subst' =
117 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
119 C.MutInd (uri,typeno,exp_named_subst')
120 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
121 let exp_named_subst' =
122 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
124 C.MutConstruct (uri,typeno,consno,exp_named_subst')
125 | C.MutCase (sp,i,outty,t,pl) ->
126 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
127 List.map (deliftaux k) pl)
129 let len = List.length fl in
132 (fun (name, i, ty, bo) ->
133 (name, i, deliftaux k ty, deliftaux (k+len) bo))
138 let len = List.length fl in
141 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
144 C.CoFix (i, liftedfl)
146 let res = deliftaux 0 t in
147 res, restrict !to_be_restricted metasenv
150 (**** END OF DELIFT ****)
152 type substitution = (int * Cic.term) list
154 (* NUOVA UNIFICAZIONE *)
155 (* A substitution is a (int * Cic.term) list that associates a
156 metavariable i with its body.
157 A metaenv is a (int * Cic.term) list that associate a metavariable
159 fo_unif_new takes a metasenv, a context, two terms t1 and t2 and gives back
160 a new substitution which is _NOT_ unwinded. It must be unwinded before
163 let rec fo_unif_subst subst context metasenv t1 t2 =
164 let module C = Cic in
165 let module R = CicReduction in
166 let module S = CicSubstitution in
168 (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
176 | Some t1', Some t2' ->
177 (* First possibility: restriction *)
178 (* Second possibility: unification *)
179 (* Third possibility: convertibility *)
180 R.are_convertible context t1' t2'
183 if ok then subst,metasenv else raise UnificationFailed
184 | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
185 fo_unif_subst subst context metasenv t2 t1
187 | (t, C.Meta (n,l)) ->
188 let subst',metasenv' =
190 let oldt = (List.assoc n subst) in
191 let lifted_oldt = S.lift_meta l oldt in
192 fo_unif_subst subst context metasenv lifted_oldt t
194 let t',metasenv' = delift context metasenv l t in
195 (n, t')::subst, metasenv'
197 let (_,_,meta_type) =
198 List.find (function (m,_,_) -> m=n) metasenv' in
199 let tyt = CicTypeChecker.type_of_aux' metasenv' context t in
200 fo_unif_subst subst' context metasenv' (S.lift_meta l meta_type) tyt
201 | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
202 | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
203 if UriManager.eq uri1 uri2 then
204 fo_unif_subst_exp_named_subst subst context metasenv
205 exp_named_subst1 exp_named_subst2
207 raise UnificationFailed
208 | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
209 if UriManager.eq uri1 uri2 && i1 = i2 then
210 fo_unif_subst_exp_named_subst subst context metasenv
211 exp_named_subst1 exp_named_subst2
213 raise UnificationFailed
214 | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
215 C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
216 if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 then
217 fo_unif_subst_exp_named_subst subst context metasenv
218 exp_named_subst1 exp_named_subst2
220 raise UnificationFailed
229 if R.are_convertible context t1 t2 then
232 raise UnificationFailed
233 | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
234 | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
235 | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) ->
236 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
237 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
238 | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) ->
239 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
240 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
241 | (C.LetIn (_,s1,t1), t2)
242 | (t2, C.LetIn (_,s1,t1)) ->
243 fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
244 | (C.Appl l1, C.Appl l2) ->
245 let lr1 = List.rev l1 in
246 let lr2 = List.rev l2 in
247 let rec fo_unif_l subst metasenv =
250 | _,[] -> assert false
252 fo_unif_subst subst context metasenv h1 h2
255 fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
256 | ((h1::l1),(h2::l2)) ->
257 let subst', metasenv' =
258 fo_unif_subst subst context metasenv h1 h2
260 fo_unif_l subst' metasenv' (l1,l2)
262 fo_unif_l subst metasenv (lr1, lr2)
267 | (C.MutConstruct _, _)
268 | (_, C.MutConstruct _) ->
269 if R.are_convertible context t1 t2 then
272 raise UnificationFailed
273 | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
274 let subst', metasenv' =
275 fo_unif_subst subst context metasenv outt1 outt2 in
276 let subst'',metasenv'' =
277 fo_unif_subst subst' context metasenv' t1 t2 in
279 (function (subst,metasenv) ->
280 fo_unif_subst subst context metasenv
281 ) (subst'',metasenv'') pl1 pl2
286 if R.are_convertible context t1 t2 then
289 raise UnificationFailed
291 if R.are_convertible context t1 t2 then
294 raise UnificationFailed
296 and fo_unif_subst_exp_named_subst subst context metasenv
297 exp_named_subst1 exp_named_subst2
301 (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
303 fo_unif_subst subst context metasenv t1 t2
304 ) (subst,metasenv) exp_named_subst1 exp_named_subst2
307 let uri = UriManager.uri_of_string "cic:/dummy.var" in
308 prerr_endline ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
309 " <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
312 (*CSC: ???????????????
313 (* m is the index of a metavariable to restrict, k is nesting depth
314 of the occurrence m, and l is its relocation list. canonical_context
315 is the context of the metavariable we are instantiating - containing
316 m - Only rel in the domain of canonical_context are accessible.
317 This function takes in input a metasenv and gives back a metasenv.
318 A rel(j) in the canonical context of m, is rel(List.nth l j) for the
319 instance of m under consideration, that is rel (List.nth l j) - k
320 in canonical_context. *)
322 let restrict canonical_context m k l =
326 | None::tl -> None::(erase (i+1) tl)
328 let i' = (List.nth l (i-1)) in
330 then he::(erase (i+1) tl) (* local variable *)
333 (try List.nth canonical_context (i'-k-1)
334 with Failure _ -> None) in
336 then None::(erase (i+1) tl)
337 else he::(erase (i+1) tl) in
341 | (n,context,t)::tl when n=m -> (n,erase 1 context,t)::tl
342 | hd::tl -> hd::(aux tl)
348 let check_accessibility metasenv i =
349 let module C = Cic in
350 let module S = CicSubstitution in
351 let (_,canonical_context,_) =
352 List.find (function (m,_,_) -> m=i) metasenv in
356 delift canonical_context metasenv ? t
362 let rec aux metasenv k =
369 match List.nth canonical_context (i-k-1) with
371 | Some (_,C.Def t) -> aux metasenv k (S.lift i t)
372 | None -> raise RelToHiddenHypothesis
374 Failure _ -> raise OpenTerm
376 | C.Var _ -> metasenv
377 | C.Meta (i,l) -> restrict canonical_context i k l metasenv
378 | C.Sort _ -> metasenv
379 | C.Implicit -> metasenv
381 let metasenv' = aux metasenv k te in
386 let metasenv' = aux metasenv k s in
387 aux metasenv' (k+1) t
390 (function metasenv -> aux metasenv k) metasenv l
393 | C.MutConstruct _ -> metasenv
394 | C.MutCase (_,_,_,outty,t,pl) ->
395 let metasenv' = aux metasenv k outty in
396 let metasenv'' = aux metasenv' k t in
398 (function metasenv -> aux metasenv k) metasenv'' pl
400 let len = List.length fl in
403 let (_,_,ty,bo) = f in
404 let metasenv' = aux metasenv k ty in
405 aux metasenv' (k+len) bo
408 let len = List.length fl in
412 let metasenv' = aux metasenv k ty in
413 aux metasenv' (k+len) bo
420 let unwind metasenv subst unwinded t =
421 let unwinded = ref unwinded in
422 let frozen = ref [] in
423 let rec um_aux metasenv =
424 let module C = Cic in
425 let module S = CicSubstitution in
427 C.Rel _ as t -> t,metasenv
428 | C.Var _ as t -> t,metasenv
431 S.lift_meta l (List.assoc i !unwinded), metasenv
433 if List.mem i !frozen then raise OccurCheck
435 let saved_frozen = !frozen in
436 frozen := i::!frozen ;
439 let t = List.assoc i subst in
440 let t',metasenv' = um_aux metasenv t in
442 let (_,canonical_context,_) =
443 List.find (function (m,_,_) -> m=i) metasenv
445 delift canonical_context metasenv' l t'
447 unwinded := (i,t')::!unwinded ;
448 S.lift_meta l t', metasenv'
451 (* not constrained variable, i.e. free in subst*)
454 (fun t (tl,metasenv) ->
456 None -> None::tl,metasenv
458 let t',metasenv' = um_aux metasenv t in
459 (Some t')::tl, metasenv'
462 C.Meta (i,l'), metasenv'
464 frozen := saved_frozen ;
468 | C.Implicit as t -> t,metasenv
470 let te',metasenv' = um_aux metasenv te in
471 let ty',metasenv'' = um_aux metasenv' ty in
472 C.Cast (te',ty'),metasenv''
474 let s',metasenv' = um_aux metasenv s in
475 let t',metasenv'' = um_aux metasenv' t in
476 C.Prod (n, s', t'), metasenv''
477 | C.Lambda (n,s,t) ->
478 let s',metasenv' = um_aux metasenv s in
479 let t',metasenv'' = um_aux metasenv' t in
480 C.Lambda (n, s', t'), metasenv''
482 let s',metasenv' = um_aux metasenv s in
483 let t',metasenv'' = um_aux metasenv' t in
484 C.LetIn (n, s', t'), metasenv''
488 (fun t (tl,metasenv) ->
489 let t',metasenv' = um_aux metasenv t in
494 match um_aux metasenv' he with
495 (C.Appl l, metasenv'') -> C.Appl (l@tl'),metasenv''
496 | (he', metasenv'') -> C.Appl (he'::tl'),metasenv''
498 | C.Appl _ -> assert false
499 | C.Const (uri,exp_named_subst) ->
500 let exp_named_subst', metasenv' =
502 (fun (uri,t) (tl,metasenv) ->
503 let t',metasenv' = um_aux metasenv t in
504 (uri,t')::tl, metasenv'
505 ) exp_named_subst ([],metasenv)
507 C.Const (uri,exp_named_subst'),metasenv'
508 | C.MutInd (uri,typeno,exp_named_subst) ->
509 let exp_named_subst', metasenv' =
511 (fun (uri,t) (tl,metasenv) ->
512 let t',metasenv' = um_aux metasenv t in
513 (uri,t')::tl, metasenv'
514 ) exp_named_subst ([],metasenv)
516 C.MutInd (uri,typeno,exp_named_subst'),metasenv'
517 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
518 let exp_named_subst', metasenv' =
520 (fun (uri,t) (tl,metasenv) ->
521 let t',metasenv' = um_aux metasenv t in
522 (uri,t')::tl, metasenv'
523 ) exp_named_subst ([],metasenv)
525 C.MutConstruct (uri,typeno,consno,exp_named_subst'),metasenv'
526 | C.MutCase (sp,i,outty,t,pl) ->
527 let outty',metasenv' = um_aux metasenv outty in
528 let t',metasenv'' = um_aux metasenv' t in
529 let pl',metasenv''' =
531 (fun p (pl,metasenv) ->
532 let p',metasenv' = um_aux metasenv p in
536 C.MutCase (sp, i, outty', t', pl'),metasenv'''
538 let len = List.length fl in
539 let liftedfl,metasenv' =
541 (fun (name, i, ty, bo) (fl,metasenv) ->
542 let ty',metasenv' = um_aux metasenv ty in
543 let bo',metasenv'' = um_aux metasenv' bo in
544 (name, i, ty', bo')::fl,metasenv''
547 C.Fix (i, liftedfl),metasenv'
549 let len = List.length fl in
550 let liftedfl,metasenv' =
552 (fun (name, ty, bo) (fl,metasenv) ->
553 let ty',metasenv' = um_aux metasenv ty in
554 let bo',metasenv'' = um_aux metasenv' bo in
555 (name, ty', bo')::fl,metasenv''
558 C.CoFix (i, liftedfl),metasenv'
560 let t',metasenv' = um_aux metasenv t in
561 t',metasenv',!unwinded
564 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t *)
565 (* performs as (apply_subst subst t) until it finds an application of *)
566 (* (META [meta_to_reduce]) that, once unwinding is performed, creates *)
567 (* a new beta-redex; in this case up to [reductions_no] consecutive *)
568 (* beta-reductions are performed. *)
569 (* Hint: this function is usually called when [reductions_no] *)
570 (* eta-expansions have been performed and the head of the new *)
571 (* application has been unified with (META [meta_to_reduce]): *)
572 (* during the unwinding the eta-expansions are undone. *)
574 let apply_subst_reducing subst meta_to_reduce t =
575 let unwinded = ref subst in
577 let module C = Cic in
578 let module S = CicSubstitution in
582 | C.Meta (i,l) as t ->
584 S.lift_meta l (List.assoc i !unwinded)
588 | C.Implicit as t -> t
589 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
590 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
591 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
592 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
594 let tl' = List.map um_aux tl in
597 C.Appl l -> C.Appl (l@tl')
598 | _ as he' -> C.Appl (he'::tl')
601 match meta_to_reduce,he with
602 Some (mtr,reductions_no), C.Meta (m,_) when m = mtr ->
603 let rec beta_reduce =
605 (n,(C.Appl (C.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
606 let he'' = CicSubstitution.subst he' t in
610 beta_reduce (n-1,C.Appl(he''::tl'))
613 beta_reduce (reductions_no,t')
616 | C.Appl _ -> assert false
617 | C.Const (uri,exp_named_subst) ->
618 let exp_named_subst' =
619 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
621 C.Const (uri,exp_named_subst')
622 | C.MutInd (uri,typeno,exp_named_subst) ->
623 let exp_named_subst' =
624 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
626 C.MutInd (uri,typeno,exp_named_subst')
627 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
628 let exp_named_subst' =
629 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
631 C.MutConstruct (uri,typeno,consno,exp_named_subst')
632 | C.MutCase (sp,i,outty,t,pl) ->
633 C.MutCase (sp, i, um_aux outty, um_aux t,
636 let len = List.length fl in
639 (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo))
644 let len = List.length fl in
647 (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo))
650 C.CoFix (i, liftedfl)
655 (* UNWIND THE MGU INSIDE THE MGU *)
656 let unwind_subst metasenv subst =
657 let identity_relocation_list_for_metavariable i =
658 let (_,canonical_context,_) =
659 List.find (function (m,_,_) -> m=i) metasenv
661 let canonical_context_length = List.length canonical_context in
664 n when n > canonical_context_length -> []
665 | n -> (Some (Cic.Rel n))::(aux (n+1))
670 (fun (unwinded,metasenv) (i,_) ->
671 let identity_relocation_list =
672 identity_relocation_list_for_metavariable i
674 let (_,metasenv',subst') =
675 unwind metasenv subst unwinded (Cic.Meta (i,identity_relocation_list))
678 ) ([],metasenv) subst
681 let apply_subst subst t =
682 (* metasenv will not be used nor modified. So, let's use a dummy empty one *)
684 let (t',_,_) = unwind metasenv [] subst t in
688 (* A substitution is a (int * Cic.term) list that associates a *)
689 (* metavariable i with its body. *)
690 (* metasenv is of type Cic.metasenv *)
691 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back *)
692 (* a new substitution which is already unwinded and ready to be applied and *)
693 (* a new metasenv in which some hypothesis in the contexts of the *)
694 (* metavariables may have been restricted. *)
695 let fo_unif metasenv context t1 t2 =
696 prerr_endline "INIZIO FASE 1" ; flush stderr ;
697 let subst_to_unwind,metasenv' = fo_unif_subst [] context metasenv t1 t2 in
698 prerr_endline "FINE FASE 1" ; flush stderr ;
700 unwind_subst metasenv' subst_to_unwind
702 prerr_endline "FINE FASE 2" ; flush stderr ; res