]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicUnivUtils.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / cic_proof_checking / cicUnivUtils.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 (*****************************************************************************)
27 (*                                                                           *)
28 (*                              PROJECT HELM                                 *)
29 (*                                                                           *)
30 (*                     Enrico Tassi <tassi@cs.unibo.it>                      *)
31 (*                                23/04/2004                                 *)
32 (*                                                                           *)
33 (* This module implements some useful function regarding univers graphs      *)
34 (*                                                                           *)
35 (*****************************************************************************)
36
37 module C = Cic
38 module H = UriManager.UriHashtbl 
39 let eq  = UriManager.eq
40
41 (* uri is the uri of the actual object that must be 'skipped' *)
42 let universes_of_obj uri t =
43   (* don't the same work twice *)
44   let visited_objs = H.create 31 in
45   let visited u = H.replace visited_objs u true in 
46   let is_not_visited u = not (H.mem visited_objs u) in 
47   visited uri;
48   (* the result *)
49   let results = ref [] in
50   let add_result l = results := l :: !results in
51   (* the iterators *)
52   let rec aux = function
53     | C.Const (u,exp_named_subst) when is_not_visited u ->
54         aux_uri u;
55         visited u;
56         C.Const (u, List.map (fun (x,t) -> x,aux t) exp_named_subst)
57     | C.Var (u,exp_named_subst) when is_not_visited u ->
58         aux_uri u;
59         visited u;
60         C.Var (u,  List.map (fun (x,t) -> x,aux t) exp_named_subst)
61     | C.Const (u,exp_named_subst) ->
62         C.Const (u, List.map (fun (x,t) -> x,aux t) exp_named_subst)
63     | C.Var (u,exp_named_subst) ->
64         C.Var (u,  List.map (fun (x,t) -> x,aux t) exp_named_subst)
65     | C.MutInd (u,x,exp_named_subst) when is_not_visited u ->
66         aux_uri u;
67         visited u;
68         C.MutInd (u,x,List.map (fun (x,t) -> x,aux t) exp_named_subst)
69     | C.MutInd (u,x,exp_named_subst) ->
70         C.MutInd (u,x, List.map (fun (x,t) -> x,aux t) exp_named_subst)
71     | C.MutConstruct (u,x,y,exp_named_subst) when is_not_visited u ->
72         aux_uri u;
73         visited u;
74         C.MutConstruct (u,x,y,List.map (fun (x,t) -> x,aux t) exp_named_subst)
75     | C.MutConstruct (x,y,z,exp_named_subst) ->
76         C.MutConstruct (x,y,z,List.map (fun (x,t) -> x,aux t) exp_named_subst)
77     | C.Meta (n,l1) -> C.Meta (n, List.map (HExtlib.map_option aux) l1)
78     | C.Sort (C.Type i) -> add_result [i]; 
79       C.Sort (C.Type (CicUniv.name_universe i uri))
80     | C.Rel _ 
81     | C.Sort _
82     | C.Implicit _ as x -> x
83     | C.Cast (v,t) -> C.Cast (aux v, aux t)
84     | C.Prod (b,s,t) -> C.Prod (b,aux s, aux t)
85     | C.Lambda (b,s,t) ->  C.Lambda (b,aux s, aux t)
86     | C.LetIn (b,s,t) -> C.LetIn (b,aux s, aux t)
87     | C.Appl li -> C.Appl (List.map aux li)
88     | C.MutCase (uri,n1,ty,te,patterns) ->
89         C.MutCase (uri,n1,aux ty,aux te, List.map aux patterns)
90     | C.Fix (no, funs) -> 
91         C.Fix(no, List.map (fun (x,y,b,c) -> (x,y,aux b,aux c)) funs)
92     | C.CoFix (no,funs) -> 
93         C.CoFix(no, List.map (fun (x,b,c) -> (x,aux b,aux c)) funs)
94   and aux_uri u =
95     if is_not_visited u then
96       let _, _, l = 
97         CicEnvironment.get_cooked_obj_with_univlist CicUniv.empty_ugraph u in 
98       add_result l
99   and aux_obj = function
100     | C.Constant (x,Some te,ty,v,y) ->
101         List.iter aux_uri v;
102         C.Constant (x,Some (aux te),aux ty,v,y)
103     | C.Variable (x,Some te,ty,v,y) -> 
104         List.iter aux_uri v;
105         C.Variable (x,Some (aux te),aux ty,v,y)
106     | C.Constant (x,None, ty, v,y) ->
107         List.iter aux_uri v;
108         C.Constant (x,None, aux ty, v,y)
109     | C.Variable (x,None, ty, v,y) ->
110         List.iter aux_uri v;
111         C.Variable (x,None, aux ty, v,y)
112     | C.CurrentProof (_,conjs,te,ty,v,_) -> assert false
113     | C.InductiveDefinition (l,v,x,y) -> 
114         List.iter aux_uri v; 
115         C.InductiveDefinition (
116           List.map
117            (fun (x,y,t,l') ->
118              (x,y,aux t, List.map (fun (x,t) -> x,aux t) l'))
119           l,v,x,y)  
120   in 
121   let o = aux_obj t in
122   List.flatten !results, o
123
124 let rec list_uniq = function 
125   | [] -> []
126   | h::[] -> [h]
127   | h1::h2::tl when CicUniv.eq h1 h2 -> list_uniq (h2 :: tl) 
128   | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
129
130 let list_uniq l = 
131   list_uniq (List.fast_sort CicUniv.compare l)
132   
133 let profiler = (HExtlib.profile "clean_and_fill").HExtlib.profile
134   
135 let clean_and_fill uri obj ugraph =
136   (* universes of obj fills the universes of the obj with the right uri *)
137   let list_of_universes, obj = universes_of_obj uri obj in
138   let list_of_universes = list_uniq list_of_universes in
139 (*  CicUniv.print_ugraph ugraph;*)
140 (*  List.iter (fun u -> prerr_endline (CicUniv.string_of_universe u))*)
141 (*  list_of_universes;*)
142   let ugraph = CicUniv.clean_ugraph ugraph list_of_universes in
143 (*  CicUniv.print_ugraph ugraph;*)
144   let ugraph, list_of_universes = 
145     CicUniv.fill_empty_nodes_with_uri ugraph list_of_universes uri 
146   in
147   ugraph, list_of_universes, obj
148
149 let clean_and_fill u o g =
150   profiler (clean_and_fill u o) g
151