]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_unification/cicUnification.ml
added annotations to Cic.Implicit
[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         (CicMetaSubst.ppterm 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,subst,metasenv =
60         List.fold_left2
61          (fun (b,subst,metasenv) t1 t2 ->
62            if b then true,subst,metasenv else
63             match t1,t2 with
64                None,_
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
71                  true,subst,metasenv
72                 else
73                  (try
74                    let subst,metasenv =
75                     fo_unif_subst subst context metasenv t1' t2'
76                    in
77                     true,subst,metasenv
78                  with
79                   Not_found -> false,subst,metasenv)
80          ) (true,subst,metasenv) ln lm
81        in
82         if ok then
83           subst,metasenv
84         else
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
90    | (C.Meta (n,l), t)   
91    | (t, C.Meta (n,l)) ->
92        let subst'',metasenv' =
93         try
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
97         with Not_found ->
98          let t',metasenv',subst' =
99            CicMetaSubst.delift n subst context metasenv l t
100          in
101           (n, t')::subst', metasenv'
102        in
103         let (_,_,meta_type) =  CicUtil.lookup_meta n metasenv' in
104         (try
105           let tyt =
106             type_of_aux' metasenv' subst'' context t
107           in
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)
115            *)
116 (*
117 prerr_endline "********* FROM NOW ON EVERY REASONABLE INVARIANT IS BROKEN.";
118 prerr_endline "********* PROCEED AT YOUR OWN RISK. AND GOOD LUCK." ;
119 *)
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
126       else
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
134       else
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
143       else
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 =
163         function
164            [],_
165          | _,[] -> assert false
166          | ([h1],[h2]) ->
167              fo_unif_subst subst context metasenv h1 h2
168          | ([h],l) 
169          | (l,[h]) ->
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
174             in 
175              fo_unif_l subst' metasenv' (l1,l2)
176        in
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
183        List.fold_left2 
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
195         subst, metasenv
196        else
197         raise (UnificationFailure (sprintf
198           "Can't unify %s with %s because they are not convertible"
199           (CicPp.ppterm t1) (CicPp.ppterm t2)))
200    | (_,_) ->
201        if R.are_convertible subst context t1 t2 then
202         subst, metasenv
203        else
204         raise (UnificationFailure (sprintf
205           "Can't unify %s with %s because they are not convertible"
206           (CicPp.ppterm t1) (CicPp.ppterm t2)))
207
208 and fo_unif_subst_exp_named_subst subst context metasenv
209  exp_named_subst1 exp_named_subst2
210 =
211 try
212  List.fold_left2
213   (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
214     assert (uri1=uri2) ;
215     fo_unif_subst subst context metasenv t1 t2
216   ) (subst,metasenv) exp_named_subst1 exp_named_subst2
217 with
218 e ->
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
222
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 ;;
231
232 let fo_unif_subst subst context metasenv t1 t2 =
233   let enrich_msg msg =
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)
236       (try
237         CicPp.ppterm (type_of_aux' metasenv subst context t1)
238       with _ -> "MALFORMED")
239       (CicMetaSubst.ppterm subst t2)
240       (try
241         CicPp.ppterm (type_of_aux' metasenv subst context t2)
242       with _ -> "MALFORMED")
243       (CicMetaSubst.ppcontext subst context)
244       (CicMetaSubst.ppmetasenv metasenv subst) msg
245   in
246   try
247     fo_unif_subst subst context metasenv t1 t2
248   with
249   | AssertFailure msg -> raise (AssertFailure (enrich_msg msg))
250   | UnificationFailure msg -> raise (UnificationFailure (enrich_msg msg))
251 ;;
252