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 (CicPp.ppterm (CicMetaSubst.apply_subst 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 ->
66 | Some t1', Some t2' ->
67 (* First possibility: restriction *)
68 (* Second possibility: unification *)
69 (* Third possibility: convertibility *)
70 R.are_convertible subst context t1' t2'
76 raise (UnificationFailure (sprintf
77 "Error trying to unify %s with %s: the algorithm only tried to check convertibility of the two substitutions"
78 (CicPp.ppterm t1) (CicPp.ppterm t2)))
79 | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
80 fo_unif_subst subst context metasenv t2 t1
82 | (t, C.Meta (n,l)) ->
83 let subst'',metasenv' =
85 let oldt = (List.assoc n subst) in
86 let lifted_oldt = S.lift_meta l oldt in
87 fo_unif_subst subst context metasenv lifted_oldt t
89 let t',metasenv',subst' =
90 CicMetaSubst.delift n subst context metasenv l t
92 (n, t')::subst', metasenv'
94 let (_,_,meta_type) = CicUtil.lookup_meta n metasenv' in
97 type_of_aux' metasenv' subst'' context t
99 fo_unif_subst subst'' context metasenv' (S.lift_meta l meta_type) tyt
100 with CicTypeChecker.SortExpectedMetaFound _ ->
101 (* TODO huge hack!!!!
102 * we keep on unifying/refining in the hope that the problem will be
103 * eventually solved. In the meantime we're breaking a big invariant:
104 * the terms that we are unifying are no longer well typed in the
105 * current context (in the worst case we could even diverge)
107 (subst'', metasenv'))
108 | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
109 | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
110 if UriManager.eq uri1 uri2 then
111 fo_unif_subst_exp_named_subst subst context metasenv
112 exp_named_subst1 exp_named_subst2
114 raise (UnificationFailure (sprintf
115 "Can't unify %s with %s due to different constants"
116 (CicPp.ppterm t1) (CicPp.ppterm t1)))
117 | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
118 if UriManager.eq uri1 uri2 && i1 = i2 then
119 fo_unif_subst_exp_named_subst subst context metasenv
120 exp_named_subst1 exp_named_subst2
122 raise (UnificationFailure (sprintf
123 "Can't unify %s with %s due to different inductive principles"
124 (CicPp.ppterm t1) (CicPp.ppterm t1)))
125 | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
126 C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
127 if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 then
128 fo_unif_subst_exp_named_subst subst context metasenv
129 exp_named_subst1 exp_named_subst2
131 raise (UnificationFailure (sprintf
132 "Can't unify %s with %s due to different inductive constructors"
133 (CicPp.ppterm t1) (CicPp.ppterm t1)))
134 | (C.Implicit, _) | (_, C.Implicit) -> assert false
135 | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
136 | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
137 | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) ->
138 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
139 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
140 | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) ->
141 let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
142 fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
143 | (C.LetIn (_,s1,t1), t2)
144 | (t2, C.LetIn (_,s1,t1)) ->
145 fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
146 | (C.Appl l1, C.Appl l2) ->
147 let lr1 = List.rev l1 in
148 let lr2 = List.rev l2 in
149 let rec fo_unif_l subst metasenv =
152 | _,[] -> assert false
154 fo_unif_subst subst context metasenv h1 h2
157 fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
158 | ((h1::l1),(h2::l2)) ->
159 let subst', metasenv' =
160 fo_unif_subst subst context metasenv h1 h2
162 fo_unif_l subst' metasenv' (l1,l2)
164 fo_unif_l subst metasenv (lr1, lr2)
165 | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
166 let subst', metasenv' =
167 fo_unif_subst subst context metasenv outt1 outt2 in
168 let subst'',metasenv'' =
169 fo_unif_subst subst' context metasenv' t1 t2 in
171 (function (subst,metasenv) ->
172 fo_unif_subst subst context metasenv
173 ) (subst'',metasenv'') pl1 pl2
174 | (C.Rel _, _) | (_, C.Rel _)
175 | (C.Sort _ ,_) | (_, C.Sort _)
176 | (C.Const _, _) | (_, C.Const _)
177 | (C.MutInd _, _) | (_, C.MutInd _)
178 | (C.MutConstruct _, _) | (_, C.MutConstruct _)
179 | (C.Fix _, _) | (_, C.Fix _)
180 | (C.CoFix _, _) | (_, C.CoFix _) ->
181 if R.are_convertible subst context t1 t2 then
184 raise (UnificationFailure (sprintf
185 "Can't unify %s with %s because they are not convertible"
186 (CicPp.ppterm t1) (CicPp.ppterm t2)))
188 if R.are_convertible subst context t1 t2 then
191 raise (UnificationFailure (sprintf
192 "Can't unify %s with %s because they are not convertible"
193 (CicPp.ppterm t1) (CicPp.ppterm t2)))
195 and fo_unif_subst_exp_named_subst subst context metasenv
196 exp_named_subst1 exp_named_subst2
200 (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
202 fo_unif_subst subst context metasenv t1 t2
203 ) (subst,metasenv) exp_named_subst1 exp_named_subst2
206 let uri = UriManager.uri_of_string "cic:/dummy.var" in
207 debug_print ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
208 " <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
210 (* A substitution is a (int * Cic.term) list that associates a *)
211 (* metavariable i with its body. *)
212 (* metasenv is of type Cic.metasenv *)
213 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back *)
214 (* a new substitution which is already unwinded and ready to be applied and *)
215 (* a new metasenv in which some hypothesis in the contexts of the *)
216 (* metavariables may have been restricted. *)
217 let fo_unif metasenv context t1 t2 = fo_unif_subst [] context metasenv t1 t2 ;;
219 let fo_unif_subst subst context metasenv t1 t2 =
221 sprintf "Unification error unifying %s of type %s with %s of type %s in context\n%s\nand metasenv\n%s\nbecause %s"
222 (CicPp.ppterm (CicMetaSubst.apply_subst subst t1))
224 CicPp.ppterm (type_of_aux' metasenv subst context t1)
225 with _ -> "MALFORMED")
226 (CicPp.ppterm (CicMetaSubst.apply_subst subst t2))
228 CicPp.ppterm (type_of_aux' metasenv subst context t2)
229 with _ -> "MALFORMED")
230 (CicMetaSubst.ppcontext subst context)
231 (CicMetaSubst.ppmetasenv metasenv subst) msg
234 fo_unif_subst subst context metasenv t1 t2
236 | AssertFailure msg -> raise (AssertFailure (enrich_msg msg))
237 | UnificationFailure msg -> raise (UnificationFailure (enrich_msg msg))