]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_unification/cicUnification.ml
- big hack: keep on unifying/refining when SortExpectedMetaFound
[helm.git] / helm / ocaml / cic_unification / cicUnification.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 open Printf
27
28 exception AssertFailure of string;;
29 exception UnificationFailure of string;;
30
31 let debug_print = prerr_endline
32
33 let type_of_aux' metasenv subst context term =
34   try
35     CicMetaSubst.type_of_aux' metasenv subst context term
36   with
37   | CicMetaSubst.MetaSubstFailure msg ->
38     raise (AssertFailure
39       ((sprintf
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)))
43
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
48    i with is type. 
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
51    applying it. *)
52
53 let rec fo_unif_subst subst context metasenv t1 t2 =  
54  let module C = Cic in
55  let module R = CicMetaSubst in
56  let module S = CicSubstitution in
57   match (t1, t2) with
58      (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
59        let ok =
60         List.fold_left2
61          (fun b t1 t2 ->
62            b &&
63             match t1,t2 with
64                None,_
65              | _,None -> true
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'
71          ) true ln lm
72        in
73         if ok then
74           subst,metasenv
75         else
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
81    | (C.Meta (n,l), t)   
82    | (t, C.Meta (n,l)) ->
83        let subst'',metasenv' =
84         try
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
88         with Not_found ->
89          let t',metasenv',subst' =
90            CicMetaSubst.delift n subst context metasenv l t
91          in
92           (n, t')::subst', metasenv'
93        in
94         let (_,_,meta_type) =  CicUtil.lookup_meta n metasenv' in
95         (try
96           let tyt =
97             type_of_aux' metasenv' subst'' context t
98           in
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)
106            *)
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
113       else
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
121       else
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
130       else
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 =
150         function
151            [],_
152          | _,[] -> assert false
153          | ([h1],[h2]) ->
154              fo_unif_subst subst context metasenv h1 h2
155          | ([h],l) 
156          | (l,[h]) ->
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
161             in 
162              fo_unif_l subst' metasenv' (l1,l2)
163        in
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
170        List.fold_left2 
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
182         subst, metasenv
183        else
184         raise (UnificationFailure (sprintf
185           "Can't unify %s with %s because they are not convertible"
186           (CicPp.ppterm t1) (CicPp.ppterm t2)))
187    | (_,_) ->
188        if R.are_convertible subst context t1 t2 then
189         subst, metasenv
190        else
191         raise (UnificationFailure (sprintf
192           "Can't unify %s with %s because they are not convertible"
193           (CicPp.ppterm t1) (CicPp.ppterm t2)))
194
195 and fo_unif_subst_exp_named_subst subst context metasenv
196  exp_named_subst1 exp_named_subst2
197 =
198 try
199  List.fold_left2
200   (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
201     assert (uri1=uri2) ;
202     fo_unif_subst subst context metasenv t1 t2
203   ) (subst,metasenv) exp_named_subst1 exp_named_subst2
204 with
205 e ->
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
209
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 ;;
218
219 let fo_unif_subst subst context metasenv t1 t2 =
220   let enrich_msg msg =
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))
223       (try
224         CicPp.ppterm (type_of_aux' metasenv subst context t1)
225       with _ -> "MALFORMED")
226       (CicPp.ppterm (CicMetaSubst.apply_subst subst t2))
227       (try
228         CicPp.ppterm (type_of_aux' metasenv subst context t2)
229       with _ -> "MALFORMED")
230       (CicMetaSubst.ppcontext subst context)
231       (CicMetaSubst.ppmetasenv metasenv subst) msg
232   in
233   try
234     fo_unif_subst subst context metasenv t1 t2
235   with
236   | AssertFailure msg -> raise (AssertFailure (enrich_msg msg))
237   | UnificationFailure msg -> raise (UnificationFailure (enrich_msg msg))
238 ;;
239