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 optional terms *)
35 (* [t1,...,tn] and a term t, and substitutes every tk = Some (rel(nk)) with *)
36 (* rel(k). Typically, the list of optional terms is the explicit substitution *)
37 (* that is applied to a metavariable occurrence and the result of the delift *)
38 (* function is a term the implicit variable can be substituted with to make *)
39 (* the term [t] unifiable with the metavariable occurrence. *)
40 (* In general, the problem is undecidable if we consider equivalence in place *)
41 (* of alpha convertibility. Our implementation, though, is even weaker than *)
42 (* alpha convertibility, since it replace the term [tk] if and only if [tk] *)
43 (* is a Rel (missing all the other cases). Does this matter in practice? *)
45 exception NotInTheList;;
50 [] -> raise NotInTheList
51 | (Some (Cic.Rel m))::_ when m=n -> k
52 | _::tl -> aux (k+1) tl in
56 (*CSC: this restriction function is utterly wrong, since it does not check *)
57 (*CSC: that the variable that is going to be restricted does not occur free *)
58 (*CSC: in a part of the sequent that is not going to be restricted. *)
59 (*CSC: In particular, the whole approach is wrong; if restriction can fail *)
60 (*CSC: (as indeed it is the case), we can not collect all the restrictions *)
61 (*CSC: and restrict everything at the end ;-( *)
62 let restrict to_be_restricted =
66 | _::tl when List.mem (n,i) to_be_restricted ->
67 None::(erase (i+1) n tl)
68 | he::tl -> he::(erase (i+1) n tl) in
72 | (n,context,t)::tl -> (n,erase 1 n context,t)::(aux tl) in
77 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)
78 let delift context metasenv l t =
79 let module S = CicSubstitution in
80 let to_be_restricted = ref [] in
86 C.Rel m (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
87 (*CSC: deliftato la regola per il LetIn *)
88 (*CSC: FALSO! La regola per il LetIn non lo fa *)
90 (match List.nth context (m-k-1) with
91 Some (_,C.Def (t,_)) ->
92 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
93 (*CSC: first order unification. Does it help or does it harm? *)
94 deliftaux k (S.lift m t)
95 | Some (_,C.Decl t) ->
96 (*CSC: The following check seems to be wrong! *)
97 (*CSC: B:Set |- ?2 : Set *)
98 (*CSC: A:Set ; x:?2[A/B] |- ?1[x/A] =?= x *)
99 (*CSC: Why should I restrict ?2 over B? The instantiation *)
100 (*CSC: ?1 := A is perfectly reasonable and well-typed. *)
101 (*CSC: Thus I comment out the following two lines that *)
102 (*CSC: are the incriminated ones. *)
103 (*(* It may augment to_be_restricted *)
104 ignore (deliftaux k (S.lift m t)) ;*)
105 (*CSC: end of bug commented out *)
106 C.Rel ((position (m-k) l) + k)
107 | None -> raise RelToHiddenHypothesis)
108 | C.Var (uri,exp_named_subst) ->
109 let exp_named_subst' =
110 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
112 C.Var (uri,exp_named_subst')
113 | C.Meta (i, l1) as t ->
117 | None::tl -> None::(deliftl (j+1) tl)
119 let l1' = (deliftl (j+1) tl) in
121 Some (deliftaux k t)::l1'
123 RelToHiddenHypothesis
125 to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
127 let l' = deliftl 1 l1 in
130 | C.Implicit as t -> t
131 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
132 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
133 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
134 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
135 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
136 | C.Const (uri,exp_named_subst) ->
137 let exp_named_subst' =
138 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
140 C.Const (uri,exp_named_subst')
141 | C.MutInd (uri,typeno,exp_named_subst) ->
142 let exp_named_subst' =
143 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
145 C.MutInd (uri,typeno,exp_named_subst')
146 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
147 let exp_named_subst' =
148 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
150 C.MutConstruct (uri,typeno,consno,exp_named_subst')
151 | C.MutCase (sp,i,outty,t,pl) ->
152 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
153 List.map (deliftaux k) pl)
155 let len = List.length fl in
158 (fun (name, i, ty, bo) ->
159 (name, i, deliftaux k ty, deliftaux (k+len) bo))
164 let len = List.length fl in
167 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
170 C.CoFix (i, liftedfl)
177 (* This is the case where we fail even first order unification. *)
178 (* The reason is that our delift function is weaker than first *)
179 (* order (in the sense of alpha-conversion). See comment above *)
180 (* related to the delift function. *)
181 prerr_endline "!!!!!!!!!!! First Order UnificationFailed, but maybe it could have been successful even in a first order setting (no conversion, only alpha convertibility)! Please, implement a better delift function !!!!!!!!!!!!!!!!" ;
182 raise UnificationFailed
184 res, restrict !to_be_restricted metasenv
187 (**** END OF DELIFT ****)
189 type substitution = (int * Cic.term) list
191 (* NUOVA UNIFICAZIONE *)
192 (* A substitution is a (int * Cic.term) list that associates a
193 metavariable i with its body.
194 A metaenv is a (int * Cic.term) list that associate a metavariable
196 fo_unif_new takes a metasenv, a context, two terms t1 and t2 and gives back
197 a new substitution which is _NOT_ unwinded. It must be unwinded before
200 let rec fo_unif_subst subst context metasenv t1 t2 =
201 let module C = Cic in
202 let module R = CicReduction in
203 let module S = CicSubstitution in
205 (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
213 | Some t1', Some t2' ->
214 (* First possibility: restriction *)
215 (* Second possibility: unification *)
216 (* Third possibility: convertibility *)
217 R.are_convertible context t1' t2'
220 if ok then subst,metasenv else raise UnificationFailed
221 | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
222 fo_unif_subst subst context metasenv t2 t1
224 | (t, C.Meta (n,l)) ->
225 let subst',metasenv' =
227 let oldt = (List.assoc n subst) in
228 let lifted_oldt = S.lift_meta l oldt in
229 fo_unif_subst subst context metasenv lifted_oldt t
231 let t',metasenv' = delift context metasenv l t in
232 (n, t')::subst, metasenv'
234 let (_,_,meta_type) =
235 List.find (function (m,_,_) -> m=n) metasenv' in
236 let tyt = CicTypeChecker.type_of_aux' metasenv' context t in
237 fo_unif_subst subst' context metasenv' (S.lift_meta l meta_type) tyt
238 | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
239 | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
240 if UriManager.eq uri1 uri2 then
241 fo_unif_subst_exp_named_subst subst context metasenv
242 exp_named_subst1 exp_named_subst2
244 raise UnificationFailed
245 | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
246 if UriManager.eq uri1 uri2 && i1 = i2 then
247 fo_unif_subst_exp_named_subst subst context metasenv
248 exp_named_subst1 exp_named_subst2
250 raise UnificationFailed
251 | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
252 C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
253 if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 then
254 fo_unif_subst_exp_named_subst subst context metasenv
255 exp_named_subst1 exp_named_subst2
257 raise UnificationFailed
264 if R.are_convertible context t1 t2 then
267 raise UnificationFailed
268 | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
269 | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
270 | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) ->
271 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
272 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
273 | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) ->
274 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
275 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
276 | (C.LetIn (_,s1,t1), t2)
277 | (t2, C.LetIn (_,s1,t1)) ->
278 fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
279 | (C.Appl l1, C.Appl l2) ->
280 let lr1 = List.rev l1 in
281 let lr2 = List.rev l2 in
282 let rec fo_unif_l subst metasenv =
285 | _,[] -> assert false
287 fo_unif_subst subst context metasenv h1 h2
290 fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
291 | ((h1::l1),(h2::l2)) ->
292 let subst', metasenv' =
293 fo_unif_subst subst context metasenv h1 h2
295 fo_unif_l subst' metasenv' (l1,l2)
297 fo_unif_l subst metasenv (lr1, lr2)
302 | (C.MutConstruct _, _)
303 | (_, C.MutConstruct _) ->
304 if R.are_convertible context t1 t2 then
307 raise UnificationFailed
308 | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
309 let subst', metasenv' =
310 fo_unif_subst subst context metasenv outt1 outt2 in
311 let subst'',metasenv'' =
312 fo_unif_subst subst' context metasenv' t1 t2 in
314 (function (subst,metasenv) ->
315 fo_unif_subst subst context metasenv
316 ) (subst'',metasenv'') pl1 pl2
321 if R.are_convertible context t1 t2 then
324 raise UnificationFailed
326 if R.are_convertible context t1 t2 then
329 raise UnificationFailed
331 and fo_unif_subst_exp_named_subst subst context metasenv
332 exp_named_subst1 exp_named_subst2
336 (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
338 fo_unif_subst subst context metasenv t1 t2
339 ) (subst,metasenv) exp_named_subst1 exp_named_subst2
342 let uri = UriManager.uri_of_string "cic:/dummy.var" in
343 prerr_endline ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
344 " <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
347 let unwind metasenv subst unwinded t =
348 let unwinded = ref unwinded in
349 let frozen = ref [] in
350 let rec um_aux metasenv =
351 let module C = Cic in
352 let module S = CicSubstitution in
354 C.Rel _ as t -> t,metasenv
355 | C.Var _ as t -> t,metasenv
358 S.lift_meta l (List.assoc i !unwinded), metasenv
360 if List.mem i !frozen then raise OccurCheck
362 let saved_frozen = !frozen in
363 frozen := i::!frozen ;
366 let t = List.assoc i subst in
367 let t',metasenv' = um_aux metasenv t in
369 let (_,canonical_context,_) =
370 List.find (function (m,_,_) -> m=i) metasenv
372 delift canonical_context metasenv' l t'
374 unwinded := (i,t')::!unwinded ;
375 S.lift_meta l t', metasenv'
378 (* not constrained variable, i.e. free in subst*)
381 (fun t (tl,metasenv) ->
383 None -> None::tl,metasenv
385 let t',metasenv' = um_aux metasenv t in
386 (Some t')::tl, metasenv'
389 C.Meta (i,l'), metasenv'
391 frozen := saved_frozen ;
395 | C.Implicit as t -> t,metasenv
397 let te',metasenv' = um_aux metasenv te in
398 let ty',metasenv'' = um_aux metasenv' ty in
399 C.Cast (te',ty'),metasenv''
401 let s',metasenv' = um_aux metasenv s in
402 let t',metasenv'' = um_aux metasenv' t in
403 C.Prod (n, s', t'), metasenv''
404 | C.Lambda (n,s,t) ->
405 let s',metasenv' = um_aux metasenv s in
406 let t',metasenv'' = um_aux metasenv' t in
407 C.Lambda (n, s', t'), metasenv''
409 let s',metasenv' = um_aux metasenv s in
410 let t',metasenv'' = um_aux metasenv' t in
411 C.LetIn (n, s', t'), metasenv''
415 (fun t (tl,metasenv) ->
416 let t',metasenv' = um_aux metasenv t in
421 match um_aux metasenv' he with
422 (C.Appl l, metasenv'') -> C.Appl (l@tl'),metasenv''
423 | (he', metasenv'') -> C.Appl (he'::tl'),metasenv''
425 | C.Appl _ -> assert false
426 | C.Const (uri,exp_named_subst) ->
427 let exp_named_subst', metasenv' =
429 (fun (uri,t) (tl,metasenv) ->
430 let t',metasenv' = um_aux metasenv t in
431 (uri,t')::tl, metasenv'
432 ) exp_named_subst ([],metasenv)
434 C.Const (uri,exp_named_subst'),metasenv'
435 | C.MutInd (uri,typeno,exp_named_subst) ->
436 let exp_named_subst', metasenv' =
438 (fun (uri,t) (tl,metasenv) ->
439 let t',metasenv' = um_aux metasenv t in
440 (uri,t')::tl, metasenv'
441 ) exp_named_subst ([],metasenv)
443 C.MutInd (uri,typeno,exp_named_subst'),metasenv'
444 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
445 let exp_named_subst', metasenv' =
447 (fun (uri,t) (tl,metasenv) ->
448 let t',metasenv' = um_aux metasenv t in
449 (uri,t')::tl, metasenv'
450 ) exp_named_subst ([],metasenv)
452 C.MutConstruct (uri,typeno,consno,exp_named_subst'),metasenv'
453 | C.MutCase (sp,i,outty,t,pl) ->
454 let outty',metasenv' = um_aux metasenv outty in
455 let t',metasenv'' = um_aux metasenv' t in
456 let pl',metasenv''' =
458 (fun p (pl,metasenv) ->
459 let p',metasenv' = um_aux metasenv p in
463 C.MutCase (sp, i, outty', t', pl'),metasenv'''
465 let len = List.length fl in
466 let liftedfl,metasenv' =
468 (fun (name, i, ty, bo) (fl,metasenv) ->
469 let ty',metasenv' = um_aux metasenv ty in
470 let bo',metasenv'' = um_aux metasenv' bo in
471 (name, i, ty', bo')::fl,metasenv''
474 C.Fix (i, liftedfl),metasenv'
476 let len = List.length fl in
477 let liftedfl,metasenv' =
479 (fun (name, ty, bo) (fl,metasenv) ->
480 let ty',metasenv' = um_aux metasenv ty in
481 let bo',metasenv'' = um_aux metasenv' bo in
482 (name, ty', bo')::fl,metasenv''
485 C.CoFix (i, liftedfl),metasenv'
487 let t',metasenv' = um_aux metasenv t in
488 t',metasenv',!unwinded
491 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t *)
492 (* performs as (apply_subst subst t) until it finds an application of *)
493 (* (META [meta_to_reduce]) that, once unwinding is performed, creates *)
494 (* a new beta-redex; in this case up to [reductions_no] consecutive *)
495 (* beta-reductions are performed. *)
496 (* Hint: this function is usually called when [reductions_no] *)
497 (* eta-expansions have been performed and the head of the new *)
498 (* application has been unified with (META [meta_to_reduce]): *)
499 (* during the unwinding the eta-expansions are undone. *)
501 let apply_subst_reducing subst meta_to_reduce t =
502 (* andrea: che senso ha questo ref ?? *)
503 let unwinded = ref subst in
505 let module C = Cic in
506 let module S = CicSubstitution in
510 | C.Meta (i,l) as t ->
512 S.lift_meta l (List.assoc i !unwinded)
516 | C.Implicit as t -> t
517 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
518 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
519 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
520 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
522 let tl' = List.map um_aux tl in
525 C.Appl l -> C.Appl (l@tl')
526 | _ as he' -> C.Appl (he'::tl')
529 match meta_to_reduce,he with
530 Some (mtr,reductions_no), C.Meta (m,_) when m = mtr ->
531 let rec beta_reduce =
533 (n,(C.Appl (C.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
534 let he'' = CicSubstitution.subst he' t in
538 beta_reduce (n-1,C.Appl(he''::tl'))
541 beta_reduce (reductions_no,t')
544 | C.Appl _ -> assert false
545 | C.Const (uri,exp_named_subst) ->
546 let exp_named_subst' =
547 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
549 C.Const (uri,exp_named_subst')
550 | C.MutInd (uri,typeno,exp_named_subst) ->
551 let exp_named_subst' =
552 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
554 C.MutInd (uri,typeno,exp_named_subst')
555 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
556 let exp_named_subst' =
557 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
559 C.MutConstruct (uri,typeno,consno,exp_named_subst')
560 | C.MutCase (sp,i,outty,t,pl) ->
561 C.MutCase (sp, i, um_aux outty, um_aux t,
564 let len = List.length fl in
567 (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo))
572 let len = List.length fl in
575 (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo))
578 C.CoFix (i, liftedfl)
583 (* UNWIND THE MGU INSIDE THE MGU *)
584 let unwind_subst metasenv subst =
585 let identity_relocation_list_for_metavariable i =
586 let (_,canonical_context,_) =
587 List.find (function (m,_,_) -> m=i) metasenv
589 let canonical_context_length = List.length canonical_context in
592 n when n > canonical_context_length -> []
593 | n -> (Some (Cic.Rel n))::(aux (n+1))
598 (fun (unwinded,metasenv) (i,_) ->
599 let identity_relocation_list =
600 identity_relocation_list_for_metavariable i
602 let (_,metasenv',subst') =
603 unwind metasenv subst unwinded (Cic.Meta (i,identity_relocation_list))
606 ) ([],metasenv) subst
609 let apply_subst subst t =
610 (* metasenv will not be used nor modified. So, let's use a dummy empty one *)
612 let (t',_,_) = unwind metasenv [] subst t in
616 (* A substitution is a (int * Cic.term) list that associates a *)
617 (* metavariable i with its body. *)
618 (* metasenv is of type Cic.metasenv *)
619 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back *)
620 (* a new substitution which is already unwinded and ready to be applied and *)
621 (* a new metasenv in which some hypothesis in the contexts of the *)
622 (* metavariables may have been restricted. *)
623 let fo_unif metasenv context t1 t2 =
624 let subst_to_unwind,metasenv' = fo_unif_subst [] context metasenv t1 t2 in
625 unwind_subst metasenv' subst_to_unwind