]> matita.cs.unibo.it Git - helm.git/blob - components/cic/cicUtil.ml
experimental branch with no set baseuri command and no developments
[helm.git] / components / 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 module C = Cic
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 _ -> 
65          assert false)
66
67 let is_closed =
68  let module C = Cic in
69  let rec is_closed k =
70   function
71       C.Rel m when m > k -> false
72     | C.Rel m -> true
73     | C.Meta (_,l) ->
74        List.fold_left
75         (fun i t -> i && (match t with None -> true | Some t -> is_closed k t)
76         ) true l
77     | C.Sort _ -> true
78     | C.Implicit _ -> assert false
79     | C.Cast (te,ty) -> is_closed k te && is_closed k ty
80     | C.Prod (name,so,dest) -> is_closed k so && is_closed (k+1) dest
81     | C.Lambda (_,so,dest) -> is_closed k so && is_closed (k+1) dest
82     | C.LetIn (_,so,dest) -> is_closed k so && is_closed (k+1) dest
83     | C.Appl l ->
84        List.fold_right (fun x i -> i && is_closed k x) l true
85     | C.Var (_,exp_named_subst)
86     | C.Const (_,exp_named_subst)
87     | C.MutInd (_,_,exp_named_subst)
88     | C.MutConstruct (_,_,_,exp_named_subst) ->
89        List.fold_right (fun (_,x) i -> i && is_closed k x)
90         exp_named_subst true
91     | C.MutCase (_,_,out,te,pl) ->
92        is_closed k out && is_closed k te &&
93         List.fold_right (fun x i -> i && is_closed k x) pl true
94     | C.Fix (_,fl) ->
95        let len = List.length fl in
96         let k_plus_len = k + len in
97          List.fold_right
98           (fun (_,_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
99           ) fl true
100     | C.CoFix (_,fl) ->
101        let len = List.length fl in
102         let k_plus_len = k + len in
103          List.fold_right
104           (fun (_,ty,bo) i -> i && is_closed k ty && is_closed k_plus_len bo
105           ) fl true
106 in 
107  is_closed 0
108 ;;
109
110 let rec is_meta_closed =
111   function
112       C.Rel _ -> true
113     | C.Meta _ -> false
114     | C.Sort _ -> true
115     | C.Implicit _ -> assert false
116     | C.Cast (te,ty) -> is_meta_closed te && is_meta_closed ty
117     | C.Prod (name,so,dest) -> is_meta_closed so && is_meta_closed dest
118     | C.Lambda (_,so,dest) -> is_meta_closed so && is_meta_closed dest
119     | C.LetIn (_,so,dest) -> is_meta_closed so && is_meta_closed dest
120     | C.Appl l ->
121        not (List.exists (fun x -> not (is_meta_closed x)) l)
122     | C.Var (_,exp_named_subst)
123     | C.Const (_,exp_named_subst)
124     | C.MutInd (_,_,exp_named_subst)
125     | C.MutConstruct (_,_,_,exp_named_subst) ->
126        not (List.exists (fun (_,x) -> not (is_meta_closed x)) exp_named_subst)
127     | C.MutCase (_,_,out,te,pl) ->
128        is_meta_closed out && is_meta_closed te &&
129         not (List.exists (fun x -> not (is_meta_closed x)) pl)
130     | C.Fix (_,fl) ->
131         not (List.exists 
132               (fun (_,_,ty,bo) -> 
133                   not (is_meta_closed ty) || not (is_meta_closed bo)) 
134               fl)
135     | C.CoFix (_,fl) ->
136         not (List.exists 
137               (fun (_,ty,bo) -> 
138                   not (is_meta_closed ty) || not (is_meta_closed bo)) 
139               fl)
140 ;;
141
142 let xpointer_RE = Str.regexp "\\([^#]+\\)#xpointer(\\(.*\\))"
143 let slash_RE = Str.regexp "/"
144
145 let term_of_uri uri =
146   let s = UriManager.string_of_uri uri in
147   try
148     (if UriManager.uri_is_con uri then
149       C.Const (uri, [])
150     else if UriManager.uri_is_var uri then
151       C.Var (uri, [])
152     else if not (Str.string_match xpointer_RE s 0) then
153       raise (UriManager.IllFormedUri s)
154     else
155       let (baseuri,xpointer) = (Str.matched_group 1 s, Str.matched_group 2 s) in
156       let baseuri = UriManager.uri_of_string baseuri in
157       (match Str.split slash_RE xpointer with
158       | [_; tyno] -> C.MutInd (baseuri, int_of_string tyno - 1, [])
159       | [_; tyno; consno] ->
160           C.MutConstruct
161             (baseuri, int_of_string tyno - 1, int_of_string consno, [])
162       | _ -> raise Exit))
163   with
164   | Exit
165   | Failure _
166   | Not_found -> raise (UriManager.IllFormedUri s)
167
168 let uri_of_term = function
169   | C.Const (uri, _)
170   | C.Var (uri, _) -> uri
171   | C.MutInd (baseuri, tyno, _) ->
172      UriManager.uri_of_string
173       (Printf.sprintf "%s#xpointer(1/%d)" (UriManager.string_of_uri baseuri) (tyno+1))
174   | C.MutConstruct (baseuri, tyno, consno, _) ->
175      UriManager.uri_of_string
176       (Printf.sprintf "%s#xpointer(1/%d/%d)" (UriManager.string_of_uri baseuri)
177         (tyno + 1) consno)
178   | _ -> raise (Invalid_argument "uri_of_term")
179
180
181 (*
182 let pack terms =
183   List.fold_right
184     (fun term acc -> C.Prod (C.Anonymous, term, acc))
185     terms (C.Sort (C.Type (CicUniv.fresh ())))
186
187 let rec unpack = function
188   | C.Prod (C.Anonymous, term, C.Sort (C.Type _)) -> [term]
189   | C.Prod (C.Anonymous, term, tgt) -> term :: unpack tgt
190   | _ -> assert false
191 *)
192
193 let rec strip_prods n = function
194   | t when n = 0 -> t
195   | C.Prod (_, _, tgt) when n > 0 -> strip_prods (n-1) tgt
196   | _ -> failwith "not enough prods"
197
198 let params_of_obj = function
199   | C.Constant (_, _, _, params, _)
200   | C.Variable (_, _, _, params, _)
201   | C.CurrentProof (_, _, _, _, params, _)
202   | C.InductiveDefinition (_, params, _, _) ->
203       params
204
205 let attributes_of_obj = function
206   | C.Constant (_, _, _, _, attributes)
207   | C.Variable (_, _, _, _, attributes)
208   | C.CurrentProof (_, _, _, _, _, attributes)
209   | C.InductiveDefinition (_, _, _, attributes) ->
210       attributes
211
212 let is_generated obj = List.exists ((=) `Generated) (attributes_of_obj obj)
213
214 let arity_of_composed_coercion obj =
215   let attrs = attributes_of_obj obj in
216   try
217     let tag=List.find (function `Class (`Coercion _) -> true|_->false) attrs in
218     match tag with
219     |  `Class (`Coercion n) -> n
220     | _-> assert false 
221   with Not_found -> 0
222 ;;
223
224 let projections_of_record obj uri =
225   let attrs = attributes_of_obj obj in
226   try
227     let tag=List.find (function `Class (`Record _) -> true|_->false) attrs in
228     match tag with
229     |  `Class (`Record l) -> 
230          List.map (fun (name,_,_) ->
231            let buri = UriManager.buri_of_uri uri in
232            let puri = UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") in
233            puri) l
234     | _-> assert false 
235   with Not_found -> []
236 ;;
237       
238 let rec mk_rels howmany from =
239   match howmany with 
240   | 0 -> []
241   | _ -> (C.Rel (howmany + from)) :: (mk_rels (howmany-1) from)
242
243 let id_of_annterm =
244   function
245   | C.ARel (id,_,_,_)
246   | C.AVar (id,_,_)
247   | C.AMeta (id,_,_)
248   | C.ASort (id,_)
249   | C.AImplicit (id,_)
250   | C.ACast (id,_,_)
251   | C.AProd (id,_,_,_)
252   | C.ALambda (id,_,_,_)
253   | C.ALetIn (id,_,_,_)
254   | C.AAppl (id,_)
255   | C.AConst (id,_,_)
256   | C.AMutInd (id,_,_,_)
257   | C.AMutConstruct (id,_,_,_,_)
258   | C.AMutCase (id,_,_,_,_,_)
259   | C.AFix (id,_,_)
260   | C.ACoFix (id,_,_) -> id
261
262
263 let rec rehash_term =
264   let module C = Cic in
265   let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
266   function
267    | (C.Rel _) as t -> t
268    | C.Var (uri,exp_named_subst) ->
269       let uri' = recons uri in
270       let exp_named_subst' =
271        List.map
272         (function (uri,t) ->(recons uri,rehash_term t)) 
273          exp_named_subst
274       in
275        C.Var (uri',exp_named_subst')
276    | C.Meta (i,l) ->
277       let l' =
278        List.map
279         (function
280             None -> None
281           | Some t -> Some (rehash_term t)
282         ) l
283       in
284        C.Meta(i,l')
285    | C.Sort (C.Type u) -> 
286        CicUniv.assert_univ u;
287        C.Sort (C.Type (CicUniv.recons_univ u))
288    | C.Sort _ as t -> t
289    | C.Implicit _ as t -> t
290    | C.Cast (te,ty) -> C.Cast (rehash_term te, rehash_term ty)
291    | C.Prod (n,s,t) -> C.Prod (n, rehash_term s, rehash_term t)
292    | C.Lambda (n,s,t) -> C.Lambda (n, rehash_term s, rehash_term t)
293    | C.LetIn (n,s,t) -> C.LetIn (n, rehash_term s, rehash_term t)
294    | C.Appl l -> C.Appl (List.map rehash_term l)
295    | C.Const (uri,exp_named_subst) ->
296       let uri' = recons uri in
297       let exp_named_subst' = 
298        List.map
299         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
300       in
301        C.Const (uri',exp_named_subst')
302    | C.MutInd (uri,tyno,exp_named_subst) ->
303       let uri' = recons uri in
304       let exp_named_subst' = 
305        List.map
306         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
307       in
308        C.MutInd (uri',tyno,exp_named_subst')
309    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
310       let uri' = recons uri in
311       let exp_named_subst' = 
312        List.map
313         (function (uri,t) -> (recons uri,rehash_term t)) exp_named_subst
314       in
315        C.MutConstruct (uri',tyno,consno,exp_named_subst')
316    | C.MutCase (uri,i,outty,t,pl) ->
317       C.MutCase (recons uri, i, rehash_term outty, rehash_term t,
318        List.map rehash_term pl)
319    | C.Fix (i, fl) ->
320       let liftedfl =
321        List.map
322         (fun (name, i, ty, bo) ->
323           (name, i, rehash_term ty, rehash_term bo))
324          fl
325       in
326        C.Fix (i, liftedfl)
327    | C.CoFix (i, fl) ->
328       let liftedfl =
329        List.map
330         (fun (name, ty, bo) -> (name, rehash_term ty, rehash_term bo))
331          fl
332       in
333        C.CoFix (i, liftedfl)
334
335 let rehash_obj =
336  let module C = Cic in
337  let recons uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
338  function 
339    C.Constant (name,bo,ty,params,attrs) ->
340      let bo' =
341        match bo with
342          None -> None
343        | Some bo -> Some (rehash_term bo)
344      in
345      let ty' = rehash_term ty in
346      let params' = List.map recons params in
347      C.Constant (name, bo', ty', params',attrs)
348  | C.CurrentProof (name,conjs,bo,ty,params,attrs) ->
349      let conjs' =
350        List.map
351          (function (i,hyps,ty) ->
352            (i,
353            List.map (function
354                None -> None
355              | Some (name,C.Decl t) ->
356                  Some (name,C.Decl (rehash_term t))
357              | Some (name,C.Def (bo,ty)) ->
358                  let ty' =
359                    match ty with
360                      None -> None
361                    | Some ty'' -> Some (rehash_term ty'')
362                  in
363                  Some (name,C.Def (rehash_term bo, ty'))) hyps,
364            rehash_term ty))
365          conjs
366      in
367      let bo' = rehash_term bo in
368      let ty' = rehash_term ty in
369      let params' = List.map recons params in
370      C.CurrentProof (name, conjs', bo', ty', params',attrs)
371  | C.Variable (name,bo,ty,params,attrs) ->
372      let bo' =
373        match bo with
374          None -> None
375        | Some bo -> Some (rehash_term bo)
376      in
377      let ty' = rehash_term ty in
378      let params' = List.map recons params in
379      C.Variable (name, bo', ty', params',attrs)
380  | C.InductiveDefinition (tl,params,paramsno,attrs) ->
381      let params' = List.map recons params in
382      let tl' =
383        List.map (function (name, inductive, ty, constructors) ->
384          name,
385          inductive,
386          rehash_term ty,
387          (List.map
388            (function (name, ty) -> name, rehash_term ty)
389            constructors))
390          tl
391      in
392      C.InductiveDefinition (tl', params', paramsno, attrs)
393
394 let rec metas_of_term = function
395   | C.Meta (i, c) -> [i,c]
396   | C.Var (_, ens) 
397   | C.Const (_, ens) 
398   | C.MutInd (_, _, ens) 
399   | C.MutConstruct (_, _, _, ens) ->
400       List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
401   | C.Cast (s, t)
402   | C.Prod (_, s, t)
403   | C.Lambda (_, s, t)
404   | C.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
405   | C.Appl l -> List.flatten (List.map metas_of_term l)
406   | C.MutCase (uri, i, s, t, l) ->
407       (metas_of_term s) @ (metas_of_term t) @
408         (List.flatten (List.map metas_of_term l))
409   | C.Fix (i, il) ->
410       List.flatten
411         (List.map (fun (s, i, t1, t2) ->
412                      (metas_of_term t1) @ (metas_of_term t2)) il)
413   | C.CoFix (i, il) ->
414       List.flatten
415         (List.map (fun (s, t1, t2) ->
416                      (metas_of_term t1) @ (metas_of_term t2)) il)
417   | _ -> []
418 ;;      
419
420 module MetaOT = struct
421   type t = int * C.term option list
422   let compare = Pervasives.compare
423 end
424
425 module S = Set.Make(MetaOT)
426
427 let rec metas_of_term_set = function
428   | C.Meta (i, c) -> S.singleton (i,c)
429   | C.Var (_, ens) 
430   | C.Const (_, ens) 
431   | C.MutInd (_, _, ens) 
432   | C.MutConstruct (_, _, _, ens) ->
433       List.fold_left 
434         (fun s (_,t) -> S.union s (metas_of_term_set t)) 
435         S.empty ens
436   | C.Cast (s, t)
437   | C.Prod (_, s, t)
438   | C.Lambda (_, s, t)
439   | C.LetIn (_, s, t) -> S.union (metas_of_term_set s) (metas_of_term_set t)
440   | C.Appl l -> 
441       List.fold_left 
442         (fun s t -> S.union s (metas_of_term_set t)) 
443         S.empty l
444   | C.MutCase (uri, i, s, t, l) ->
445       S.union 
446         (S.union (metas_of_term_set s)  (metas_of_term_set t))
447         (List.fold_left 
448           (fun s t -> S.union s (metas_of_term_set t)) 
449           S.empty l)
450   | C.Fix (_, il) ->
451       (List.fold_left 
452         (fun s (_,_,t1,t2) -> 
453           S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
454         S.empty il
455   | C.CoFix (i, il) ->
456       (List.fold_left 
457         (fun s (_,t1,t2) -> 
458           S.union s (S.union (metas_of_term_set t1) (metas_of_term_set t2))))
459         S.empty il
460   | _ -> S.empty
461 ;;      
462
463 let metas_of_term_set t = 
464   let s = metas_of_term_set t in
465   S.elements s
466 ;;
467
468 (* syntactic_equality up to the                 *)
469 (* distinction between fake dependent products  *)
470 (* and non-dependent products, alfa-conversion  *)
471 let alpha_equivalence =
472   let rec aux t t' =
473    if t = t' then true
474    else
475     match t,t' with
476        C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2) ->
477         UriManager.eq uri1 uri2 &&
478          aux_exp_named_subst exp_named_subst1 exp_named_subst2
479      | C.Cast (te,ty), C.Cast (te',ty') ->
480         aux te te' && aux ty ty'
481      | C.Prod (_,s,t), C.Prod (_,s',t') ->
482         aux s s' && aux t t'
483      | C.Lambda (_,s,t), C.Lambda (_,s',t') ->
484         aux s s' && aux t t'
485      | C.LetIn (_,s,t), C.LetIn(_,s',t') ->
486         aux s s' && aux t t'
487      | C.Appl l, C.Appl l' when List.length l = List.length l' ->
488         (try
489           List.fold_left2
490            (fun b t1 t2 -> b && aux t1 t2) true l l'
491          with
492           Invalid_argument _ -> false)
493      | C.Const (uri,exp_named_subst1), C.Const (uri',exp_named_subst2) ->
494         UriManager.eq uri uri' &&
495          aux_exp_named_subst exp_named_subst1 exp_named_subst2
496      | C.MutInd (uri,i,exp_named_subst1), C.MutInd (uri',i',exp_named_subst2) ->
497         UriManager.eq uri uri' && i = i' &&
498          aux_exp_named_subst exp_named_subst1 exp_named_subst2
499      | C.MutConstruct (uri,i,j,exp_named_subst1),
500        C.MutConstruct (uri',i',j',exp_named_subst2) ->
501         UriManager.eq uri uri' && i = i' && j = j' &&
502          aux_exp_named_subst exp_named_subst1 exp_named_subst2
503      | C.MutCase (sp,i,outt,t,pl), C.MutCase (sp',i',outt',t',pl') ->
504         UriManager.eq sp sp' && i = i' &&
505          aux outt outt' && aux t t' &&
506           (try
507             List.fold_left2
508              (fun b t1 t2 -> b && aux t1 t2) true pl pl'
509            with
510             Invalid_argument _ -> false)
511      | C.Fix (i,fl), C.Fix (i',fl') ->
512         i = i' &&
513         (try
514           List.fold_left2
515            (fun b (_,i,ty,bo) (_,i',ty',bo') ->
516              b && i = i' && aux ty ty' && aux bo bo'
517            ) true fl fl'
518          with
519           Invalid_argument _ -> false)
520      | C.CoFix (i,fl), C.CoFix (i',fl') ->
521         i = i' &&
522         (try
523           List.fold_left2
524            (fun b (_,ty,bo) (_,ty',bo') ->
525              b && aux ty ty' && aux bo bo'
526            ) true fl fl'
527          with
528           Invalid_argument _ -> false)
529      | C.Meta (i, subst), C.Meta (i', subst') ->
530         i = i' &&
531         (try
532           List.fold_left2
533            (fun b xt xt' -> match xt,xt' with
534              | Some t, Some t' -> b && aux t t'
535              | _               -> b
536            ) true subst subst'
537          with
538           Invalid_argument _ -> false)
539      | C.Appl [t], t' | t, C.Appl [t'] -> assert false
540 (* FG: are we _really_ sure of these?      
541      | C.Sort (C.Type u), C.Sort (C.Type u') -> u = u' 
542      | C.Implicit a, C.Implicit a' -> a = a'
543    we insert an unused variable below to genarate a warning at compile time
544 *)     
545      | _,_ -> false (* we already know that t != t' *)
546   and aux_exp_named_subst exp_named_subst1 exp_named_subst2 =
547    try
548      List.fold_left2
549       (fun b (uri1,t1) (uri2,t2) ->
550         b && UriManager.eq uri1 uri2 && aux t1 t2
551       ) true exp_named_subst1 exp_named_subst2
552     with
553      Invalid_argument _ -> false
554   in
555    aux
556
557 let is_sober t =
558    let rec sober_term g = function
559       | C.Rel _ 
560       | C.Sort _  
561       | C.Implicit _                    -> g      
562       | C.Const (_, xnss) 
563       | C.Var (_, xnss) 
564       | C.MutConstruct (_, _, _, xnss)
565       | C.MutInd (_, _, xnss)           -> sober_xnss g xnss
566       | C.Meta (_, xss)                 -> sober_xss g xss
567       | C.LetIn (_, v, t)
568       | C.Lambda (_, v, t)
569       | C.Prod (_, v, t)
570       | C.Cast (t, v)                   -> sober_term (sober_term g t) v
571       | C.Appl []                       
572       | C.Appl [_]                      -> fun b -> false
573       | C.Appl ts                       -> sober_terms g ts
574       | C.MutCase (_, _, t, v, ts)      -> 
575          sober_terms (sober_term (sober_term g t) v) ts
576       | C.Fix (_, ifs)                  -> sober_ifs g ifs
577       | C.CoFix (_, cifs)               -> sober_cifs g cifs
578    and sober_terms g = List.fold_left sober_term g
579    and sober_xnss g =
580       let map g (_, t) = sober_term g t in
581       List.fold_left map g
582    and sober_xss g =
583       let map g = function 
584          | None   -> g
585          | Some t -> sober_term g t
586       in
587       List.fold_left map g
588    and sober_ifs g =
589       let map g (_, _, t, v) = sober_term (sober_term g t) v in
590       List.fold_left map g
591    and sober_cifs g =
592       let map g (_, t, v) = sober_term (sober_term g t) v in
593       List.fold_left map g
594    in 
595    sober_term (fun b -> b) t true