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/.
28 exception AssertFailure of string;;
29 exception UnificationFailure of string;;
31 let debug_print = prerr_endline
33 let type_of_aux' metasenv subst context term =
35 CicMetaSubst.type_of_aux' metasenv subst context term
37 | CicMetaSubst.MetaSubstFailure msg ->
40 "Type checking error: %s in context\n%s.\nException: %s.\nBroken invariant: unification must be invoked only on well typed terms"
41 (CicMetaSubst.ppterm subst term)
42 (CicMetaSubst.ppcontext subst context) msg)))
44 (* NUOVA UNIFICAZIONE *)
45 (* A substitution is a (int * Cic.term) list that associates a
46 metavariable i with its body.
47 A metaenv is a (int * Cic.term) list that associate a metavariable
49 fo_unif_new takes a metasenv, a context, two terms t1 and t2 and gives back
50 a new substitution which is _NOT_ unwinded. It must be unwinded before
53 let rec fo_unif_subst subst context metasenv t1 t2 =
55 let module R = CicMetaSubst in
56 let module S = CicSubstitution in
58 (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
59 let ok,subst,metasenv =
61 (fun (b,subst,metasenv) t1 t2 ->
62 if b then true,subst,metasenv else
65 | _,None -> true,subst,metasenv
66 | Some t1', Some t2' ->
67 (* First possibility: restriction *)
68 (* Second possibility: unification *)
69 (* Third possibility: convertibility *)
70 if R.are_convertible subst context t1' t2' then
75 fo_unif_subst subst context metasenv t1' t2'
79 Not_found -> false,subst,metasenv)
80 ) (true,subst,metasenv) ln lm
85 raise (UnificationFailure (sprintf
86 "Error trying to unify %s with %s: the algorithm tried to check whether the two substitutions are convertible; if they are not, it tried to unify the two substitutions. No restriction was attempted."
87 (CicPp.ppterm t1) (CicPp.ppterm t2)))
88 | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
89 fo_unif_subst subst context metasenv t2 t1
91 | (t, C.Meta (n,l)) ->
92 let subst'',metasenv' =
94 let oldt = (List.assoc n subst) in
95 let lifted_oldt = S.lift_meta l oldt in
96 fo_unif_subst subst context metasenv lifted_oldt t
98 let t',metasenv',subst' =
99 CicMetaSubst.delift n subst context metasenv l t
101 (n, t')::subst', metasenv'
103 let (_,_,meta_type) = CicUtil.lookup_meta n metasenv' in
106 type_of_aux' metasenv' subst'' context t
108 fo_unif_subst subst'' context metasenv' (S.lift_meta l meta_type) tyt
109 with AssertFailure _ ->
110 (* TODO huge hack!!!!
111 * we keep on unifying/refining in the hope that the problem will be
112 * eventually solved. In the meantime we're breaking a big invariant:
113 * the terms that we are unifying are no longer well typed in the
114 * current context (in the worst case we could even diverge)
117 prerr_endline "********* FROM NOW ON EVERY REASONABLE INVARIANT IS BROKEN.";
118 prerr_endline "********* PROCEED AT YOUR OWN RISK. AND GOOD LUCK." ;
120 (subst'', metasenv'))
121 | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
122 | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
123 if UriManager.eq uri1 uri2 then
124 fo_unif_subst_exp_named_subst subst context metasenv
125 exp_named_subst1 exp_named_subst2
127 raise (UnificationFailure (sprintf
128 "Can't unify %s with %s due to different constants"
129 (CicPp.ppterm t1) (CicPp.ppterm t1)))
130 | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
131 if UriManager.eq uri1 uri2 && i1 = i2 then
132 fo_unif_subst_exp_named_subst subst context metasenv
133 exp_named_subst1 exp_named_subst2
135 raise (UnificationFailure (sprintf
136 "Can't unify %s with %s due to different inductive principles"
137 (CicPp.ppterm t1) (CicPp.ppterm t1)))
138 | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
139 C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
140 if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 then
141 fo_unif_subst_exp_named_subst subst context metasenv
142 exp_named_subst1 exp_named_subst2
144 raise (UnificationFailure (sprintf
145 "Can't unify %s with %s due to different inductive constructors"
146 (CicPp.ppterm t1) (CicPp.ppterm t1)))
147 | (C.Implicit, _) | (_, C.Implicit) -> assert false
148 | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
149 | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
150 | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) ->
151 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
152 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
153 | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) ->
154 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
155 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
156 | (C.LetIn (_,s1,t1), t2)
157 | (t2, C.LetIn (_,s1,t1)) ->
158 fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
159 | (C.Appl l1, C.Appl l2) ->
160 let lr1 = List.rev l1 in
161 let lr2 = List.rev l2 in
162 let rec fo_unif_l subst metasenv =
165 | _,[] -> assert false
167 fo_unif_subst subst context metasenv h1 h2
170 fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
171 | ((h1::l1),(h2::l2)) ->
172 let subst', metasenv' =
173 fo_unif_subst subst context metasenv h1 h2
175 fo_unif_l subst' metasenv' (l1,l2)
177 fo_unif_l subst metasenv (lr1, lr2)
178 | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
179 let subst', metasenv' =
180 fo_unif_subst subst context metasenv outt1 outt2 in
181 let subst'',metasenv'' =
182 fo_unif_subst subst' context metasenv' t1 t2 in
184 (function (subst,metasenv) ->
185 fo_unif_subst subst context metasenv
186 ) (subst'',metasenv'') pl1 pl2
187 | (C.Rel _, _) | (_, C.Rel _)
188 | (C.Sort _ ,_) | (_, C.Sort _)
189 | (C.Const _, _) | (_, C.Const _)
190 | (C.MutInd _, _) | (_, C.MutInd _)
191 | (C.MutConstruct _, _) | (_, C.MutConstruct _)
192 | (C.Fix _, _) | (_, C.Fix _)
193 | (C.CoFix _, _) | (_, C.CoFix _) ->
194 if R.are_convertible subst context t1 t2 then
197 raise (UnificationFailure (sprintf
198 "Can't unify %s with %s because they are not convertible"
199 (CicPp.ppterm t1) (CicPp.ppterm t2)))
201 if R.are_convertible subst context t1 t2 then
204 raise (UnificationFailure (sprintf
205 "Can't unify %s with %s because they are not convertible"
206 (CicPp.ppterm t1) (CicPp.ppterm t2)))
208 and fo_unif_subst_exp_named_subst subst context metasenv
209 exp_named_subst1 exp_named_subst2
213 (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
215 fo_unif_subst subst context metasenv t1 t2
216 ) (subst,metasenv) exp_named_subst1 exp_named_subst2
219 let uri = UriManager.uri_of_string "cic:/dummy.var" in
220 debug_print ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
221 " <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
223 (* A substitution is a (int * Cic.term) list that associates a *)
224 (* metavariable i with its body. *)
225 (* metasenv is of type Cic.metasenv *)
226 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back *)
227 (* a new substitution which is already unwinded and ready to be applied and *)
228 (* a new metasenv in which some hypothesis in the contexts of the *)
229 (* metavariables may have been restricted. *)
230 let fo_unif metasenv context t1 t2 = fo_unif_subst [] context metasenv t1 t2 ;;
232 let fo_unif_subst subst context metasenv t1 t2 =
234 sprintf "Unification error unifying %s of type %s with %s of type %s in context\n%s\nand metasenv\n%s\nbecause %s"
235 (CicMetaSubst.ppterm subst t1)
237 CicPp.ppterm (type_of_aux' metasenv subst context t1)
238 with _ -> "MALFORMED")
239 (CicMetaSubst.ppterm subst t2)
241 CicPp.ppterm (type_of_aux' metasenv subst context t2)
242 with _ -> "MALFORMED")
243 (CicMetaSubst.ppcontext subst context)
244 (CicMetaSubst.ppmetasenv metasenv subst) msg
247 fo_unif_subst subst context metasenv t1 t2
249 | AssertFailure msg -> raise (AssertFailure (enrich_msg msg))
250 | UnificationFailure msg -> raise (UnificationFailure (enrich_msg msg))