]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic/cicUtil.ml
test branch
[helm.git] / helm / ocaml / cic / cicUtil.ml
1 (* Copyright (C) 2004, 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://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 exception Meta_not_found of int
31 exception Subst_not_found of int
32
33 let lookup_meta index metasenv =
34   try
35     List.find (fun (index', _, _) -> index = index') metasenv
36   with Not_found -> raise (Meta_not_found index)
37
38 let lookup_subst n subst =
39   try
40     List.assoc n subst
41   with Not_found -> raise (Subst_not_found n)
42
43 let exists_meta index = List.exists (fun (index', _, _) -> (index = index'))
44
45 (* clean_up_meta take a substitution, a metasenv a meta_inex and a local
46 context l and clean up l with respect to the hidden hipothesis in the 
47 canonical context *)
48
49 let clean_up_local_context subst metasenv n l =
50   let cc =
51     (try
52        let (cc,_,_) = lookup_subst n subst in cc
53      with Subst_not_found _ ->
54        try
55          let (_,cc,_) = lookup_meta n metasenv in cc
56        with Meta_not_found _ -> assert false) in
57   (try 
58      List.map2
59        (fun t1 t2 ->
60           match t1,t2 with 
61               None , _ -> None
62             | _ , t -> t) cc l
63    with 
64        Invalid_argument _ -> assert false)
65
66 let is_closed =
67  let module C = Cic in
68  let rec is_closed k =
69   function
70       C.Rel m when m > k -> false
71     | C.Rel m -> true
72     | C.Meta (_,l) ->
73        List.fold_left
74         (fun i t -> i && (match t with None -> true | Some t -> is_closed k t)
75         ) true l
76     | C.Sort _ -> true
77     | C.Implicit _ -> assert false
78     | C.Cast (te,ty) -> is_closed k te && is_closed k ty
79     | C.Prod (name,so,dest) -> is_closed k so && is_closed (k+1) dest
80     | C.Lambda (_,so,dest) -> is_closed k so && is_closed (k+1) dest
81     | C.LetIn (_,so,dest) -> is_closed k so && is_closed (k+1) dest
82     | C.Appl l ->
83        List.fold_right (fun x i -> i && is_closed k x) l true
84     | C.Var (_,exp_named_subst)
85     | C.Const (_,exp_named_subst)
86     | C.MutInd (_,_,exp_named_subst)
87     | C.MutConstruct (_,_,_,exp_named_subst) ->
88        List.fold_right (fun (_,x) i -> i && is_closed k x)
89         exp_named_subst true
90     | C.MutCase (_,_,out,te,pl) ->
91        is_closed k out && is_closed k te &&
92         List.fold_right (fun x i -> i && is_closed k x) pl true
93     | C.Fix (_,fl) ->
94        let len = List.length fl in
95         let k_plus_len = k + len in
96          List.fold_right
97           (fun (_,_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
98           ) fl true
99     | C.CoFix (_,fl) ->
100        let len = List.length fl in
101         let k_plus_len = k + len in
102          List.fold_right
103           (fun (_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
104           ) fl true
105 in 
106  is_closed 0
107 ;;
108
109 let rec is_meta_closed =
110   function
111       Cic.Rel _ -> true
112     | Cic.Meta _ -> false
113     | Cic.Sort _ -> true
114     | Cic.Implicit _ -> assert false
115     | Cic.Cast (te,ty) -> is_meta_closed te && is_meta_closed ty
116     | Cic.Prod (name,so,dest) -> is_meta_closed so && is_meta_closed dest
117     | Cic.Lambda (_,so,dest) -> is_meta_closed so && is_meta_closed dest
118     | Cic.LetIn (_,so,dest) -> is_meta_closed so && is_meta_closed dest
119     | Cic.Appl l ->
120        not (List.exists (fun x -> not (is_meta_closed x)) l)
121     | Cic.Var (_,exp_named_subst)
122     | Cic.Const (_,exp_named_subst)
123     | Cic.MutInd (_,_,exp_named_subst)
124     | Cic.MutConstruct (_,_,_,exp_named_subst) ->
125        not (List.exists (fun (_,x) -> not (is_meta_closed x)) exp_named_subst)
126     | Cic.MutCase (_,_,out,te,pl) ->
127        is_meta_closed out && is_meta_closed te &&
128         not (List.exists (fun x -> not (is_meta_closed x)) pl)
129     | Cic.Fix (_,fl) ->
130         not (List.exists 
131               (fun (_,_,ty,bo) -> 
132                   not (is_meta_closed ty) || not (is_meta_closed bo)) 
133               fl)
134     | Cic.CoFix (_,fl) ->
135         not (List.exists 
136               (fun (_,ty,bo) -> 
137                   not (is_meta_closed ty) || not (is_meta_closed bo)) 
138               fl)
139 ;;
140
141 let xpointer_RE = Str.regexp "\\([^#]+\\)#xpointer(\\(.*\\))"
142 let slash_RE = Str.regexp "/"
143
144 let term_of_uri uri =
145   let s = UriManager.string_of_uri uri in
146   try
147     (if UriManager.uri_is_con uri then
148       Cic.Const (uri, [])
149     else if UriManager.uri_is_var uri then
150       Cic.Var (uri, [])
151     else if not (Str.string_match xpointer_RE s 0) then
152       raise (UriManager.IllFormedUri s)
153     else
154       let (baseuri,xpointer) = (Str.matched_group 1 s, Str.matched_group 2 s) in
155       let baseuri = UriManager.uri_of_string baseuri in
156       (match Str.split slash_RE xpointer with
157       | [_; tyno] -> Cic.MutInd (baseuri, int_of_string tyno - 1, [])
158       | [_; tyno; consno] ->
159           Cic.MutConstruct
160             (baseuri, int_of_string tyno - 1, int_of_string consno, [])
161       | _ -> raise Exit))
162   with
163   | Exit
164   | Failure _
165   | Not_found -> raise (UriManager.IllFormedUri s)
166
167 let uri_of_term = function
168   | Cic.Const (uri, [])
169   | Cic.Var (uri, []) -> uri
170   | Cic.MutInd (baseuri, tyno, []) ->
171      UriManager.uri_of_string
172       (sprintf "%s#xpointer(1/%d)" (UriManager.string_of_uri baseuri) (tyno+1))
173   | Cic.MutConstruct (baseuri, tyno, consno, []) ->
174      UriManager.uri_of_string
175       (sprintf "%s#xpointer(1/%d/%d)" (UriManager.string_of_uri baseuri)
176         (tyno + 1) consno)
177   | _ -> raise (Invalid_argument "uri_of_term")
178
179
180 (*
181 let pack terms =
182   List.fold_right
183     (fun term acc -> Cic.Prod (Cic.Anonymous, term, acc))
184     terms (Cic.Sort (Cic.Type (CicUniv.fresh ())))
185
186 let rec unpack = function
187   | Cic.Prod (Cic.Anonymous, term, Cic.Sort (Cic.Type _)) -> [term]
188   | Cic.Prod (Cic.Anonymous, term, tgt) -> term :: unpack tgt
189   | _ -> assert false
190 *)
191
192 let rec strip_prods n = function
193   | t when n = 0 -> t
194   | Cic.Prod (_, _, tgt) when n > 0 -> strip_prods (n-1) tgt
195   | _ -> failwith "not enough prods"
196
197 let params_of_obj = function
198   | Cic.Constant (_, _, _, params, _)
199   | Cic.Variable (_, _, _, params, _)
200   | Cic.CurrentProof (_, _, _, _, params, _)
201   | Cic.InductiveDefinition (_, params, _, _) ->
202       params
203
204 let attributes_of_obj = function
205   | Cic.Constant (_, _, _, _, attributes)
206   | Cic.Variable (_, _, _, _, attributes)
207   | Cic.CurrentProof (_, _, _, _, _, attributes)
208   | Cic.InductiveDefinition (_, _, _, attributes) ->
209       attributes
210 let rec mk_rels howmany from =
211   match howmany with 
212   | 0 -> []
213   | _ -> (Cic.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
214
215 let id_of_annterm =
216   function
217   | Cic.ARel (id,_,_,_)
218   | Cic.AVar (id,_,_)
219   | Cic.AMeta (id,_,_)
220   | Cic.ASort (id,_)
221   | Cic.AImplicit (id,_)
222   | Cic.ACast (id,_,_)
223   | Cic.AProd (id,_,_,_)
224   | Cic.ALambda (id,_,_,_)
225   | Cic.ALetIn (id,_,_,_)
226   | Cic.AAppl (id,_)
227   | Cic.AConst (id,_,_)
228   | Cic.AMutInd (id,_,_,_)
229   | Cic.AMutConstruct (id,_,_,_,_)
230   | Cic.AMutCase (id,_,_,_,_,_)
231   | Cic.AFix (id,_,_)
232   | Cic.ACoFix (id,_,_) -> id
233
234
235 let rec rehash_term =
236   let module C = Cic in
237   let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
238   function
239    | (C.Rel _) as t -> t
240    | C.Var (uri,exp_named_subst) ->
241       let uri' = recons uri in
242       let exp_named_subst' =
243        List.map
244         (function (uri,t) ->(recons uri,rehash_term t)) 
245          exp_named_subst
246       in
247        C.Var (uri',exp_named_subst')
248    | C.Meta (i,l) ->
249       let l' =
250        List.map
251         (function
252             None -> None
253           | Some t -> Some (rehash_term t)
254         ) l
255       in
256        C.Meta(i,l')
257    | C.Sort (C.Type u) -> 
258        CicUniv.assert_univ u;
259        C.Sort (C.Type (CicUniv.recons_univ u))
260    | C.Sort _ as t -> t
261    | C.Implicit _ as t -> t
262    | C.Cast (te,ty) -> C.Cast (rehash_term te, rehash_term ty)
263    | C.Prod (n,s,t) -> C.Prod (n, rehash_term s, rehash_term t)
264    | C.Lambda (n,s,t) -> C.Lambda (n, rehash_term s, rehash_term t)
265    | C.LetIn (n,s,t) -> C.LetIn (n, rehash_term s, rehash_term t)
266    | C.Appl l -> C.Appl (List.map rehash_term l)
267    | C.Const (uri,exp_named_subst) ->
268       let uri' = recons uri in
269       let exp_named_subst' = 
270        List.map
271         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
272       in
273        C.Const (uri',exp_named_subst')
274    | C.MutInd (uri,tyno,exp_named_subst) ->
275       let uri' = recons uri in
276       let exp_named_subst' = 
277        List.map
278         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
279       in
280        C.MutInd (uri',tyno,exp_named_subst')
281    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
282       let uri' = recons uri in
283       let exp_named_subst' = 
284        List.map
285         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
286       in
287        C.MutConstruct (uri',tyno,consno,exp_named_subst')
288    | C.MutCase (uri,i,outty,t,pl) ->
289       C.MutCase (recons uri, i, rehash_term outty, rehash_term t,
290        List.map rehash_term pl)
291    | C.Fix (i, fl) ->
292       let liftedfl =
293        List.map
294         (fun (name, i, ty, bo) ->
295           (name, i, rehash_term ty, rehash_term bo))
296          fl
297       in
298        C.Fix (i, liftedfl)
299    | C.CoFix (i, fl) ->
300       let liftedfl =
301        List.map
302         (fun (name, ty, bo) -> (name, rehash_term ty, rehash_term bo))
303          fl
304       in
305        C.CoFix (i, liftedfl)
306
307 let rehash_obj =
308  let module C = Cic in
309  let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
310  function 
311    C.Constant (name,bo,ty,params,attrs) ->
312      let bo' =
313        match bo with
314          None -> None
315        | Some bo -> Some (rehash_term bo)
316      in
317      let ty' = rehash_term ty in
318      let params' = List.map recons params in
319      C.Constant (name, bo', ty', params',attrs)
320  | C.CurrentProof (name,conjs,bo,ty,params,attrs) ->
321      let conjs' =
322        List.map
323          (function (i,hyps,ty) ->
324            (i,
325            List.map (function
326                None -> None
327              | Some (name,C.Decl t) ->
328                  Some (name,C.Decl (rehash_term t))
329              | Some (name,C.Def (bo,ty)) ->
330                  let ty' =
331                    match ty with
332                      None -> None
333                    | Some ty'' -> Some (rehash_term ty'')
334                  in
335                  Some (name,C.Def (rehash_term bo, ty'))) hyps,
336            rehash_term ty))
337          conjs
338      in
339      let bo' = rehash_term bo in
340      let ty' = rehash_term ty in
341      let params' = List.map recons params in
342      C.CurrentProof (name, conjs', bo', ty', params',attrs)
343  | C.Variable (name,bo,ty,params,attrs) ->
344      let bo' =
345        match bo with
346          None -> None
347        | Some bo -> Some (rehash_term bo)
348      in
349      let ty' = rehash_term ty in
350      let params' = List.map recons params in
351      C.Variable (name, bo', ty', params',attrs)
352  | C.InductiveDefinition (tl,params,paramsno,attrs) ->
353      let params' = List.map recons params in
354      let tl' =
355        List.map (function (name, inductive, ty, constructors) ->
356          name,
357          inductive,
358          rehash_term ty,
359          (List.map
360            (function (name, ty) -> name, rehash_term ty)
361            constructors))
362          tl
363      in
364      C.InductiveDefinition (tl', params', paramsno, attrs)
365