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 *)
81 (match List.nth context (m-k-1) with
82 Some (_,C.Def (t,_)) -> deliftaux k (S.lift m t)
83 | Some (_,C.Decl t) ->
84 (* It may augment to_be_restricted *)
85 ignore (deliftaux k (S.lift m t)) ;
86 C.Rel ((position (m-k) l) + k)
87 | None -> raise RelToHiddenHypothesis)
88 | C.Var (uri,exp_named_subst) ->
89 let exp_named_subst' =
90 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
92 C.Var (uri,exp_named_subst')
93 | C.Meta (i, l1) as t ->
97 | None::tl -> None::(deliftl (j+1) tl)
99 let l1' = (deliftl (j+1) tl) in
101 Some (deliftaux k t)::l1'
103 RelToHiddenHypothesis
105 to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
107 let l' = deliftl 1 l1 in
110 | C.Implicit as t -> t
111 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
112 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
113 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
114 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
115 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
116 | C.Const (uri,exp_named_subst) ->
117 let exp_named_subst' =
118 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
120 C.Const (uri,exp_named_subst')
121 | C.MutInd (uri,typeno,exp_named_subst) ->
122 let exp_named_subst' =
123 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
125 C.MutInd (uri,typeno,exp_named_subst')
126 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
127 let exp_named_subst' =
128 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
130 C.MutConstruct (uri,typeno,consno,exp_named_subst')
131 | C.MutCase (sp,i,outty,t,pl) ->
132 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
133 List.map (deliftaux k) pl)
135 let len = List.length fl in
138 (fun (name, i, ty, bo) ->
139 (name, i, deliftaux k ty, deliftaux (k+len) bo))
144 let len = List.length fl in
147 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
150 C.CoFix (i, liftedfl)
157 (* This is the case where we fail even first order unification. *)
158 (* The reason is that our delift function is weaker than first *)
159 (* order (in the sense of alpha-conversion). See comment above *)
160 (* related to the delift function. *)
161 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 !!!!!!!!!!!!!!!!" ;
162 raise UnificationFailed
164 res, restrict !to_be_restricted metasenv
167 (**** END OF DELIFT ****)
169 type substitution = (int * Cic.term) list
171 (* NUOVA UNIFICAZIONE *)
172 (* A substitution is a (int * Cic.term) list that associates a
173 metavariable i with its body.
174 A metaenv is a (int * Cic.term) list that associate a metavariable
176 fo_unif_new takes a metasenv, a context, two terms t1 and t2 and gives back
177 a new substitution which is _NOT_ unwinded. It must be unwinded before
180 let rec fo_unif_subst subst context metasenv t1 t2 =
181 let module C = Cic in
182 let module R = CicReduction in
183 let module S = CicSubstitution in
185 (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
193 | Some t1', Some t2' ->
194 (* First possibility: restriction *)
195 (* Second possibility: unification *)
196 (* Third possibility: convertibility *)
197 R.are_convertible context t1' t2'
200 if ok then subst,metasenv else raise UnificationFailed
201 | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
202 fo_unif_subst subst context metasenv t2 t1
204 | (t, C.Meta (n,l)) ->
205 let subst',metasenv' =
207 let oldt = (List.assoc n subst) in
208 let lifted_oldt = S.lift_meta l oldt in
209 fo_unif_subst subst context metasenv lifted_oldt t
211 let t',metasenv' = delift context metasenv l t in
212 (n, t')::subst, metasenv'
214 let (_,_,meta_type) =
215 List.find (function (m,_,_) -> m=n) metasenv' in
216 let tyt = CicTypeChecker.type_of_aux' metasenv' context t in
217 fo_unif_subst subst' context metasenv' (S.lift_meta l meta_type) tyt
218 | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
219 | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
220 if UriManager.eq uri1 uri2 then
221 fo_unif_subst_exp_named_subst subst context metasenv
222 exp_named_subst1 exp_named_subst2
224 raise UnificationFailed
225 | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
226 if UriManager.eq uri1 uri2 && i1 = i2 then
227 fo_unif_subst_exp_named_subst subst context metasenv
228 exp_named_subst1 exp_named_subst2
230 raise UnificationFailed
231 | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
232 C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
233 if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 then
234 fo_unif_subst_exp_named_subst subst context metasenv
235 exp_named_subst1 exp_named_subst2
237 raise UnificationFailed
244 if R.are_convertible context t1 t2 then
247 raise UnificationFailed
248 | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
249 | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
250 | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) ->
251 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
252 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
253 | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) ->
254 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
255 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
256 | (C.LetIn (_,s1,t1), t2)
257 | (t2, C.LetIn (_,s1,t1)) ->
258 fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
259 | (C.Appl l1, C.Appl l2) ->
260 let lr1 = List.rev l1 in
261 let lr2 = List.rev l2 in
262 let rec fo_unif_l subst metasenv =
265 | _,[] -> assert false
267 fo_unif_subst subst context metasenv h1 h2
270 fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
271 | ((h1::l1),(h2::l2)) ->
272 let subst', metasenv' =
273 fo_unif_subst subst context metasenv h1 h2
275 fo_unif_l subst' metasenv' (l1,l2)
277 fo_unif_l subst metasenv (lr1, lr2)
282 | (C.MutConstruct _, _)
283 | (_, C.MutConstruct _) ->
284 if R.are_convertible context t1 t2 then
287 raise UnificationFailed
288 | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
289 let subst', metasenv' =
290 fo_unif_subst subst context metasenv outt1 outt2 in
291 let subst'',metasenv'' =
292 fo_unif_subst subst' context metasenv' t1 t2 in
294 (function (subst,metasenv) ->
295 fo_unif_subst subst context metasenv
296 ) (subst'',metasenv'') pl1 pl2
301 if R.are_convertible context t1 t2 then
304 raise UnificationFailed
306 if R.are_convertible context t1 t2 then
309 raise UnificationFailed
311 and fo_unif_subst_exp_named_subst subst context metasenv
312 exp_named_subst1 exp_named_subst2
316 (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
318 fo_unif_subst subst context metasenv t1 t2
319 ) (subst,metasenv) exp_named_subst1 exp_named_subst2
322 let uri = UriManager.uri_of_string "cic:/dummy.var" in
323 prerr_endline ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
324 " <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
327 let unwind metasenv subst unwinded t =
328 let unwinded = ref unwinded in
329 let frozen = ref [] in
330 let rec um_aux metasenv =
331 let module C = Cic in
332 let module S = CicSubstitution in
334 C.Rel _ as t -> t,metasenv
335 | C.Var _ as t -> t,metasenv
338 S.lift_meta l (List.assoc i !unwinded), metasenv
340 if List.mem i !frozen then raise OccurCheck
342 let saved_frozen = !frozen in
343 frozen := i::!frozen ;
346 let t = List.assoc i subst in
347 let t',metasenv' = um_aux metasenv t in
349 let (_,canonical_context,_) =
350 List.find (function (m,_,_) -> m=i) metasenv
352 delift canonical_context metasenv' l t'
354 unwinded := (i,t')::!unwinded ;
355 S.lift_meta l t', metasenv'
358 (* not constrained variable, i.e. free in subst*)
361 (fun t (tl,metasenv) ->
363 None -> None::tl,metasenv
365 let t',metasenv' = um_aux metasenv t in
366 (Some t')::tl, metasenv'
369 C.Meta (i,l'), metasenv'
371 frozen := saved_frozen ;
375 | C.Implicit as t -> t,metasenv
377 let te',metasenv' = um_aux metasenv te in
378 let ty',metasenv'' = um_aux metasenv' ty in
379 C.Cast (te',ty'),metasenv''
381 let s',metasenv' = um_aux metasenv s in
382 let t',metasenv'' = um_aux metasenv' t in
383 C.Prod (n, s', t'), metasenv''
384 | C.Lambda (n,s,t) ->
385 let s',metasenv' = um_aux metasenv s in
386 let t',metasenv'' = um_aux metasenv' t in
387 C.Lambda (n, s', t'), metasenv''
389 let s',metasenv' = um_aux metasenv s in
390 let t',metasenv'' = um_aux metasenv' t in
391 C.LetIn (n, s', t'), metasenv''
395 (fun t (tl,metasenv) ->
396 let t',metasenv' = um_aux metasenv t in
401 match um_aux metasenv' he with
402 (C.Appl l, metasenv'') -> C.Appl (l@tl'),metasenv''
403 | (he', metasenv'') -> C.Appl (he'::tl'),metasenv''
405 | C.Appl _ -> assert false
406 | C.Const (uri,exp_named_subst) ->
407 let exp_named_subst', metasenv' =
409 (fun (uri,t) (tl,metasenv) ->
410 let t',metasenv' = um_aux metasenv t in
411 (uri,t')::tl, metasenv'
412 ) exp_named_subst ([],metasenv)
414 C.Const (uri,exp_named_subst'),metasenv'
415 | C.MutInd (uri,typeno,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.MutInd (uri,typeno,exp_named_subst'),metasenv'
424 | C.MutConstruct (uri,typeno,consno,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.MutConstruct (uri,typeno,consno,exp_named_subst'),metasenv'
433 | C.MutCase (sp,i,outty,t,pl) ->
434 let outty',metasenv' = um_aux metasenv outty in
435 let t',metasenv'' = um_aux metasenv' t in
436 let pl',metasenv''' =
438 (fun p (pl,metasenv) ->
439 let p',metasenv' = um_aux metasenv p in
443 C.MutCase (sp, i, outty', t', pl'),metasenv'''
445 let len = List.length fl in
446 let liftedfl,metasenv' =
448 (fun (name, i, ty, bo) (fl,metasenv) ->
449 let ty',metasenv' = um_aux metasenv ty in
450 let bo',metasenv'' = um_aux metasenv' bo in
451 (name, i, ty', bo')::fl,metasenv''
454 C.Fix (i, liftedfl),metasenv'
456 let len = List.length fl in
457 let liftedfl,metasenv' =
459 (fun (name, ty, bo) (fl,metasenv) ->
460 let ty',metasenv' = um_aux metasenv ty in
461 let bo',metasenv'' = um_aux metasenv' bo in
462 (name, ty', bo')::fl,metasenv''
465 C.CoFix (i, liftedfl),metasenv'
467 let t',metasenv' = um_aux metasenv t in
468 t',metasenv',!unwinded
471 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t *)
472 (* performs as (apply_subst subst t) until it finds an application of *)
473 (* (META [meta_to_reduce]) that, once unwinding is performed, creates *)
474 (* a new beta-redex; in this case up to [reductions_no] consecutive *)
475 (* beta-reductions are performed. *)
476 (* Hint: this function is usually called when [reductions_no] *)
477 (* eta-expansions have been performed and the head of the new *)
478 (* application has been unified with (META [meta_to_reduce]): *)
479 (* during the unwinding the eta-expansions are undone. *)
481 let apply_subst_reducing subst meta_to_reduce t =
482 let unwinded = ref subst in
484 let module C = Cic in
485 let module S = CicSubstitution in
489 | C.Meta (i,l) as t ->
491 S.lift_meta l (List.assoc i !unwinded)
495 | C.Implicit as t -> t
496 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
497 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
498 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
499 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
501 let tl' = List.map um_aux tl in
504 C.Appl l -> C.Appl (l@tl')
505 | _ as he' -> C.Appl (he'::tl')
508 match meta_to_reduce,he with
509 Some (mtr,reductions_no), C.Meta (m,_) when m = mtr ->
510 let rec beta_reduce =
512 (n,(C.Appl (C.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
513 let he'' = CicSubstitution.subst he' t in
517 beta_reduce (n-1,C.Appl(he''::tl'))
520 beta_reduce (reductions_no,t')
523 | C.Appl _ -> assert false
524 | C.Const (uri,exp_named_subst) ->
525 let exp_named_subst' =
526 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
528 C.Const (uri,exp_named_subst')
529 | C.MutInd (uri,typeno,exp_named_subst) ->
530 let exp_named_subst' =
531 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
533 C.MutInd (uri,typeno,exp_named_subst')
534 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
535 let exp_named_subst' =
536 List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
538 C.MutConstruct (uri,typeno,consno,exp_named_subst')
539 | C.MutCase (sp,i,outty,t,pl) ->
540 C.MutCase (sp, i, um_aux outty, um_aux t,
543 let len = List.length fl in
546 (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo))
551 let len = List.length fl in
554 (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo))
557 C.CoFix (i, liftedfl)
562 (* UNWIND THE MGU INSIDE THE MGU *)
563 let unwind_subst metasenv subst =
564 let identity_relocation_list_for_metavariable i =
565 let (_,canonical_context,_) =
566 List.find (function (m,_,_) -> m=i) metasenv
568 let canonical_context_length = List.length canonical_context in
571 n when n > canonical_context_length -> []
572 | n -> (Some (Cic.Rel n))::(aux (n+1))
577 (fun (unwinded,metasenv) (i,_) ->
578 let identity_relocation_list =
579 identity_relocation_list_for_metavariable i
581 let (_,metasenv',subst') =
582 unwind metasenv subst unwinded (Cic.Meta (i,identity_relocation_list))
585 ) ([],metasenv) subst
588 let apply_subst subst t =
589 (* metasenv will not be used nor modified. So, let's use a dummy empty one *)
591 let (t',_,_) = unwind metasenv [] subst t in
595 (* A substitution is a (int * Cic.term) list that associates a *)
596 (* metavariable i with its body. *)
597 (* metasenv is of type Cic.metasenv *)
598 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back *)
599 (* a new substitution which is already unwinded and ready to be applied and *)
600 (* a new metasenv in which some hypothesis in the contexts of the *)
601 (* metavariables may have been restricted. *)
602 let fo_unif metasenv context t1 t2 =
603 let subst_to_unwind,metasenv' = fo_unif_subst [] context metasenv t1 t2 in
604 unwind_subst metasenv' subst_to_unwind