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 *)(* that is applied to a metavariable occurrence and the result of the delift *)
37 (* function is a term the implicit variable can be substituted with to make *)
38 (* the term [t] unifiable with the metavariable occurrence. *)
39 (* In general, the problem is undecidable if we consider equivalence in place *)
40 (* of alpha convertibility. Our implementation, though, is even weaker than *)
41 (* alpha convertibility, since it replace the term [tk] if and only if [tk] *)
42 (* is a Rel (missing all the other cases). Does this matter in practice? *)
44 exception NotInTheList;;
49 [] -> raise NotInTheList
50 | (Some (Cic.Rel m))::_ when m=n -> k
51 | _::tl -> aux (k+1) tl in
55 let restrict to_be_restricted =
59 | _::tl when List.mem (n,i) to_be_restricted ->
60 None::(erase (i+1) n tl)
61 | he::tl -> he::(erase (i+1) n tl) in
65 | (n,context,t)::tl -> (n,erase 1 n context,t)::(aux tl) in
70 let delift context metasenv l t =
71 let module S = CicSubstitution in
72 let to_be_restricted = ref [] in
78 C.Rel m (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
79 (*CSC: deliftato la regola per il LetIn *)
80 (*CSC: FALSO! La regola per il LetIn non lo fa *)
82 (match List.nth context (m-k-1) with
83 Some (_,C.Def (t,_)) ->
84 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
85 (*CSC: first order unification. Does it help or does it harm? *)
86 deliftaux k (S.lift m t)
87 | Some (_,C.Decl t) ->
88 (* It may augment to_be_restricted *)
89 (*CSC: Really? Even in the case of well-typed terms? *)
90 (*CSC: I am no longer sure of the usefulness of the check *)
91 ignore (deliftaux k (S.lift m t)) ;
92 C.Rel ((position (m-k) l) + k)
93 | None -> raise RelToHiddenHypothesis)
94 | C.Var (uri,exp_named_subst) ->
95 let exp_named_subst' =
96 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
98 C.Var (uri,exp_named_subst')
99 | C.Meta (i, l1) as t ->
100 (* CSC: BIG BUG HERE! In the explicit substitution l1 = [t1 ; t2], *)
101 (* CSC: it is NOT true that Rel(1) in t2 refers to t1 (i.e. the explicit *)
102 (* CSC: substitution is simultaneous, not telescopic. To be fixes ASAP. *)
106 | None::tl -> None::(deliftl (j+1) tl)
108 let l1' = (deliftl (j+1) tl) in
110 Some (deliftaux k t)::l1'
112 RelToHiddenHypothesis
114 to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
116 let l' = deliftl 1 l1 in
119 | C.Implicit as t -> t
120 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
121 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
122 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
123 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
124 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
125 | C.Const (uri,exp_named_subst) ->
126 let exp_named_subst' =
127 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
129 C.Const (uri,exp_named_subst')
130 | C.MutInd (uri,typeno,exp_named_subst) ->
131 let exp_named_subst' =
132 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
134 C.MutInd (uri,typeno,exp_named_subst')
135 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
136 let exp_named_subst' =
137 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
139 C.MutConstruct (uri,typeno,consno,exp_named_subst')
140 | C.MutCase (sp,i,outty,t,pl) ->
141 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
142 List.map (deliftaux k) pl)
144 let len = List.length fl in
147 (fun (name, i, ty, bo) ->
148 (name, i, deliftaux k ty, deliftaux (k+len) bo))
153 let len = List.length fl in
156 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
159 C.CoFix (i, liftedfl)
166 (* This is the case where we fail even first order unification. *)
167 (* The reason is that our delift function is weaker than first *)
168 (* order (in the sense of alpha-conversion). See comment above *)
169 (* related to the delift function. *)
170 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 !!!!!!!!!!!!!!!!" ;
171 raise UnificationFailed
173 res, restrict !to_be_restricted metasenv
176 (**** END OF DELIFT ****)
178 type substitution = (int * Cic.term) list
180 (* NUOVA UNIFICAZIONE *)
181 (* A substitution is a (int * Cic.term) list that associates a
182 metavariable i with its body.
183 A metaenv is a (int * Cic.term) list that associate a metavariable
185 fo_unif_new takes a metasenv, a context, two terms t1 and t2 and gives back
186 a new substitution which is _NOT_ unwinded. It must be unwinded before
189 let rec fo_unif_subst subst context metasenv t1 t2 =
190 let module C = Cic in
191 let module R = CicReduction in
192 let module S = CicSubstitution in
194 (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
202 | Some t1', Some t2' ->
203 (* First possibility: restriction *)
204 (* Second possibility: unification *)
205 (* Third possibility: convertibility *)
206 R.are_convertible context t1' t2'
209 if ok then subst,metasenv else raise UnificationFailed
210 | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
211 fo_unif_subst subst context metasenv t2 t1
213 | (t, C.Meta (n,l)) ->
214 let subst',metasenv' =
216 let oldt = (List.assoc n subst) in
217 let lifted_oldt = S.lift_meta l oldt in
218 fo_unif_subst subst context metasenv lifted_oldt t
220 let t',metasenv' = delift context metasenv l t in
221 (n, t')::subst, metasenv'
223 let (_,_,meta_type) =
224 List.find (function (m,_,_) -> m=n) metasenv' in
225 let tyt = CicTypeChecker.type_of_aux' metasenv' context t in
226 fo_unif_subst subst' context metasenv' (S.lift_meta l meta_type) tyt
227 | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
228 | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
229 if UriManager.eq uri1 uri2 then
230 fo_unif_subst_exp_named_subst subst context metasenv
231 exp_named_subst1 exp_named_subst2
233 raise UnificationFailed
234 | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
235 if UriManager.eq uri1 uri2 && i1 = i2 then
236 fo_unif_subst_exp_named_subst subst context metasenv
237 exp_named_subst1 exp_named_subst2
239 raise UnificationFailed
240 | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
241 C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
242 if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 then
243 fo_unif_subst_exp_named_subst subst context metasenv
244 exp_named_subst1 exp_named_subst2
246 raise UnificationFailed
253 if R.are_convertible context t1 t2 then
256 raise UnificationFailed
257 | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
258 | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
259 | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) ->
260 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
261 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
262 | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) ->
263 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
264 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
265 | (C.LetIn (_,s1,t1), t2)
266 | (t2, C.LetIn (_,s1,t1)) ->
267 fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
268 | (C.Appl l1, C.Appl l2) ->
269 let lr1 = List.rev l1 in
270 let lr2 = List.rev l2 in
271 let rec fo_unif_l subst metasenv =
274 | _,[] -> assert false
276 fo_unif_subst subst context metasenv h1 h2
279 fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
280 | ((h1::l1),(h2::l2)) ->
281 let subst', metasenv' =
282 fo_unif_subst subst context metasenv h1 h2
284 fo_unif_l subst' metasenv' (l1,l2)
286 fo_unif_l subst metasenv (lr1, lr2)
291 | (C.MutConstruct _, _)
292 | (_, C.MutConstruct _) ->
293 if R.are_convertible context t1 t2 then
296 raise UnificationFailed
297 | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
298 let subst', metasenv' =
299 fo_unif_subst subst context metasenv outt1 outt2 in
300 let subst'',metasenv'' =
301 fo_unif_subst subst' context metasenv' t1 t2 in
303 (function (subst,metasenv) ->
304 fo_unif_subst subst context metasenv
305 ) (subst'',metasenv'') pl1 pl2
310 if R.are_convertible context t1 t2 then
313 raise UnificationFailed
315 if R.are_convertible context t1 t2 then
318 raise UnificationFailed
320 and fo_unif_subst_exp_named_subst subst context metasenv
321 exp_named_subst1 exp_named_subst2
325 (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
327 fo_unif_subst subst context metasenv t1 t2
328 ) (subst,metasenv) exp_named_subst1 exp_named_subst2
331 let uri = UriManager.uri_of_string "cic:/dummy.var" in
332 prerr_endline ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
333 " <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
336 let unwind metasenv subst unwinded t =
337 let unwinded = ref unwinded in
338 let frozen = ref [] in
339 let rec um_aux metasenv =
340 let module C = Cic in
341 let module S = CicSubstitution in
343 C.Rel _ as t -> t,metasenv
344 | C.Var _ as t -> t,metasenv
347 S.lift_meta l (List.assoc i !unwinded), metasenv
349 if List.mem i !frozen then raise OccurCheck
351 let saved_frozen = !frozen in
352 frozen := i::!frozen ;
355 let t = List.assoc i subst in
356 let t',metasenv' = um_aux metasenv t in
358 let (_,canonical_context,_) =
359 List.find (function (m,_,_) -> m=i) metasenv
361 delift canonical_context metasenv' l t'
363 unwinded := (i,t')::!unwinded ;
364 S.lift_meta l t', metasenv'
367 (* not constrained variable, i.e. free in subst*)
370 (fun t (tl,metasenv) ->
372 None -> None::tl,metasenv
374 let t',metasenv' = um_aux metasenv t in
375 (Some t')::tl, metasenv'
378 C.Meta (i,l'), metasenv'
380 frozen := saved_frozen ;
384 | C.Implicit as t -> t,metasenv
386 let te',metasenv' = um_aux metasenv te in
387 let ty',metasenv'' = um_aux metasenv' ty in
388 C.Cast (te',ty'),metasenv''
390 let s',metasenv' = um_aux metasenv s in
391 let t',metasenv'' = um_aux metasenv' t in
392 C.Prod (n, s', t'), metasenv''
393 | C.Lambda (n,s,t) ->
394 let s',metasenv' = um_aux metasenv s in
395 let t',metasenv'' = um_aux metasenv' t in
396 C.Lambda (n, s', t'), metasenv''
398 let s',metasenv' = um_aux metasenv s in
399 let t',metasenv'' = um_aux metasenv' t in
400 C.LetIn (n, s', t'), metasenv''
404 (fun t (tl,metasenv) ->
405 let t',metasenv' = um_aux metasenv t in
410 match um_aux metasenv' he with
411 (C.Appl l, metasenv'') -> C.Appl (l@tl'),metasenv''
412 | (he', metasenv'') -> C.Appl (he'::tl'),metasenv''
414 | C.Appl _ -> assert false
415 | C.Const (uri,exp_named_subst) ->
416 let exp_named_subst', metasenv' =
418 (fun (uri,t) (tl,metasenv) ->
419 let t',metasenv' = um_aux metasenv t in
420 (uri,t')::tl, metasenv'
421 ) exp_named_subst ([],metasenv)
423 C.Const (uri,exp_named_subst'),metasenv'
424 | C.MutInd (uri,typeno,exp_named_subst) ->
425 let exp_named_subst', metasenv' =
427 (fun (uri,t) (tl,metasenv) ->
428 let t',metasenv' = um_aux metasenv t in
429 (uri,t')::tl, metasenv'
430 ) exp_named_subst ([],metasenv)
432 C.MutInd (uri,typeno,exp_named_subst'),metasenv'
433 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
434 let exp_named_subst', metasenv' =
436 (fun (uri,t) (tl,metasenv) ->
437 let t',metasenv' = um_aux metasenv t in
438 (uri,t')::tl, metasenv'
439 ) exp_named_subst ([],metasenv)
441 C.MutConstruct (uri,typeno,consno,exp_named_subst'),metasenv'
442 | C.MutCase (sp,i,outty,t,pl) ->
443 let outty',metasenv' = um_aux metasenv outty in
444 let t',metasenv'' = um_aux metasenv' t in
445 let pl',metasenv''' =
447 (fun p (pl,metasenv) ->
448 let p',metasenv' = um_aux metasenv p in
452 C.MutCase (sp, i, outty', t', pl'),metasenv'''
454 let len = List.length fl in
455 let liftedfl,metasenv' =
457 (fun (name, i, ty, bo) (fl,metasenv) ->
458 let ty',metasenv' = um_aux metasenv ty in
459 let bo',metasenv'' = um_aux metasenv' bo in
460 (name, i, ty', bo')::fl,metasenv''
463 C.Fix (i, liftedfl),metasenv'
465 let len = List.length fl in
466 let liftedfl,metasenv' =
468 (fun (name, ty, bo) (fl,metasenv) ->
469 let ty',metasenv' = um_aux metasenv ty in
470 let bo',metasenv'' = um_aux metasenv' bo in
471 (name, ty', bo')::fl,metasenv''
474 C.CoFix (i, liftedfl),metasenv'
476 let t',metasenv' = um_aux metasenv t in
477 t',metasenv',!unwinded
480 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t *)
481 (* performs as (apply_subst subst t) until it finds an application of *)
482 (* (META [meta_to_reduce]) that, once unwinding is performed, creates *)
483 (* a new beta-redex; in this case up to [reductions_no] consecutive *)
484 (* beta-reductions are performed. *)
485 (* Hint: this function is usually called when [reductions_no] *)
486 (* eta-expansions have been performed and the head of the new *)
487 (* application has been unified with (META [meta_to_reduce]): *)
488 (* during the unwinding the eta-expansions are undone. *)
490 let apply_subst_reducing subst meta_to_reduce t =
491 let unwinded = ref subst in
493 let module C = Cic in
494 let module S = CicSubstitution in
498 | C.Meta (i,l) as t ->
500 S.lift_meta l (List.assoc i !unwinded)
504 | C.Implicit as t -> t
505 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
506 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
507 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
508 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
510 let tl' = List.map um_aux tl in
513 C.Appl l -> C.Appl (l@tl')
514 | _ as he' -> C.Appl (he'::tl')
517 match meta_to_reduce,he with
518 Some (mtr,reductions_no), C.Meta (m,_) when m = mtr ->
519 let rec beta_reduce =
521 (n,(C.Appl (C.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
522 let he'' = CicSubstitution.subst he' t in
526 beta_reduce (n-1,C.Appl(he''::tl'))
529 beta_reduce (reductions_no,t')
532 | C.Appl _ -> assert false
533 | C.Const (uri,exp_named_subst) ->
534 let exp_named_subst' =
535 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
537 C.Const (uri,exp_named_subst')
538 | C.MutInd (uri,typeno,exp_named_subst) ->
539 let exp_named_subst' =
540 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
542 C.MutInd (uri,typeno,exp_named_subst')
543 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
544 let exp_named_subst' =
545 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
547 C.MutConstruct (uri,typeno,consno,exp_named_subst')
548 | C.MutCase (sp,i,outty,t,pl) ->
549 C.MutCase (sp, i, um_aux outty, um_aux t,
552 let len = List.length fl in
555 (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo))
560 let len = List.length fl in
563 (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo))
566 C.CoFix (i, liftedfl)
571 (* UNWIND THE MGU INSIDE THE MGU *)
572 let unwind_subst metasenv subst =
573 let identity_relocation_list_for_metavariable i =
574 let (_,canonical_context,_) =
575 List.find (function (m,_,_) -> m=i) metasenv
577 let canonical_context_length = List.length canonical_context in
580 n when n > canonical_context_length -> []
581 | n -> (Some (Cic.Rel n))::(aux (n+1))
586 (fun (unwinded,metasenv) (i,_) ->
587 let identity_relocation_list =
588 identity_relocation_list_for_metavariable i
590 let (_,metasenv',subst') =
591 unwind metasenv subst unwinded (Cic.Meta (i,identity_relocation_list))
594 ) ([],metasenv) subst
597 let apply_subst subst t =
598 (* metasenv will not be used nor modified. So, let's use a dummy empty one *)
600 let (t',_,_) = unwind metasenv [] subst t in
604 (* A substitution is a (int * Cic.term) list that associates a *)
605 (* metavariable i with its body. *)
606 (* metasenv is of type Cic.metasenv *)
607 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back *)
608 (* a new substitution which is already unwinded and ready to be applied and *)
609 (* a new metasenv in which some hypothesis in the contexts of the *)
610 (* metavariables may have been restricted. *)
611 let fo_unif metasenv context t1 t2 =
612 let subst_to_unwind,metasenv' = fo_unif_subst [] context metasenv t1 t2 in
613 unwind_subst metasenv' subst_to_unwind