]> matita.cs.unibo.it Git - helm.git/blob - components/cic/cicInspect.ml
tagged 0.5.0-rc1
[helm.git] / components / cic / cicInspect.ml
1 (* Copyright (C) 2003-2005, 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 module UM = UriManager
27 module C  = Cic
28
29 module Int = struct
30    type t = int
31    let compare = compare 
32 end
33 module S = Set.Make (Int) 
34
35 let overlaps s1 s2 =
36    let predicate x = S.mem x s1 in
37    S.exists predicate s2
38
39 let get_rels_from_premise h t = 
40    let rec aux d g = function
41       | C.Sort _
42       | C.Implicit _       -> g
43       | C.Rel i            -> 
44          if i < d then g else fun a -> g (S.add (i - d + h + 1) a)
45       | C.Appl ss          -> List.fold_left (aux d) g ss
46       | C.Const (_, ss)
47       | C.MutInd (_, _, ss)
48       | C.MutConstruct (_, _, _, ss)
49       | C.Var (_, ss)      -> 
50          let map g (_, t) = aux d g t in 
51          List.fold_left map g ss
52       | C.Meta (_, ss)     ->
53          let map g = function 
54             | None   -> g
55             | Some t -> aux d g t
56          in
57          List.fold_left map g ss
58       | C.Cast (t1, t2)    -> aux d (aux d g t2) t1
59       | C.Lambda (_, t1, t2)
60       | C.Prod (_, t1, t2) -> aux d (aux (succ d) g t2) t1
61       | C.LetIn (_, t1, ty, t2) ->
62          aux d (aux d (aux (succ d) g t2) ty) t1
63       | C.MutCase (_, _, t1, t2, ss) ->
64          aux d (aux d (List.fold_left (aux d) g ss) t2) t1
65       | C.Fix (_, ss) ->
66          let k = List.length ss in
67          let map g (_, _, t1, t2) = aux d (aux (d + k) g t2) t1 in
68          List.fold_left map g ss
69       | C.CoFix (_, ss) ->
70          let k = List.length ss in
71          let map g (_, t1, t2) = aux d (aux (d + k) g t2) t1 in
72          List.fold_left map g ss
73    in
74    let g a = a in
75    aux 1 g t S.empty
76
77 let get_mutinds_of_uri u t = 
78    let rec aux g = function
79       | C.Sort _
80       | C.Implicit _
81       | C.Rel _                      -> g
82       | C.Appl ss                    -> List.fold_left aux g ss
83       | C.Const (_, ss)
84       | C.MutConstruct (_, _, _, ss)
85       | C.Var (_, ss)                -> 
86          let map g (_, t) = aux g t in 
87          List.fold_left map g ss
88       | C.MutInd (uri, tyno, ss)     ->
89          let map g (_, t) = aux g t in 
90          let g = List.fold_left map g ss in
91          if UM.eq u uri then fun a -> g (S.add tyno a) else g
92       | C.Meta (_, ss)               ->
93          let map g = function 
94             | None   -> g
95             | Some t -> aux g t
96          in
97          List.fold_left map g ss
98       | C.Cast (t1, t2)              -> aux (aux g t2) t1
99       | C.Lambda (_, t1, t2)
100       | C.Prod (_, t1, t2) -> aux (aux g t2) t1
101       | C.LetIn (_, t1, ty, t2) -> aux (aux (aux g t2) ty) t1
102       | C.MutCase (_, _, t1, t2, ss) ->
103          aux (aux (List.fold_left aux g ss) t2) t1
104       | C.Fix (_, ss)                ->
105          let map g (_, _, t1, t2) = aux (aux g t2) t1 in
106          List.fold_left map g ss
107       | C.CoFix (_, ss)              ->
108          let map g (_, t1, t2) = aux (aux g t2) t1 in
109          List.fold_left map g ss
110    in
111    let g a = a in
112    aux g t S.empty
113
114 let rec aux n = function
115    | C.Sort _
116    | C.Implicit _
117    | C.Rel _                      -> succ n
118    | C.Appl ts                    -> List.fold_left aux (succ n) ts
119    | C.Const (_, ss)
120    | C.MutConstruct (_, _, _, ss)
121    | C.MutInd (_, _, ss)
122    | C.Var (_, ss)                -> 
123       let map n (_, t) = aux n t in 
124       List.fold_left map (succ n) ss
125    | C.Meta (_, ss)               ->
126       let map n = function 
127          | None   -> n
128          | Some t -> aux n t
129       in
130       List.fold_left map (succ n) ss
131    | C.Cast (t1, t2)
132    | C.Lambda (_, t1, t2)
133    | C.Prod (_, t1, t2) -> aux (aux (succ n) t2) t1
134    | C.LetIn (_, t1, ty, t2) -> aux (aux (aux (succ n) t2) ty) t1
135    | C.MutCase (_, _, t1, t2, ss) ->
136       aux (aux (List.fold_left aux (succ n) ss) t2) t1
137    | C.Fix (_, ss)                ->
138       let map n (_, _, t1, t2) = aux (aux n t2) t1 in
139       List.fold_left map (succ n) ss
140    | C.CoFix (_, ss)              ->
141       let map n (_, t1, t2) = aux (aux n t2) t1 in
142       List.fold_left map (succ n) ss
143
144 let count_nodes = aux