]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_generator/mQueryLevels.ml
* changed name of shared library
[helm.git] / helm / ocaml / mathql_generator / mQueryLevels.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 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 let levels_of_term metasenv context term =
30    let module TC = CicTypeChecker in
31    let module Red = CicReduction in
32    let module Util = MQueryUtil in
33    let degree t =
34       let rec degree_aux = function
35          | Cic.Sort _         -> 1 
36          | Cic.Cast (u, _)    -> degree_aux u
37          | Cic.Prod (_, _, t) -> degree_aux t
38          | _                  -> 2
39       in 
40       let u = TC.type_of_aux' metasenv context t in
41       degree_aux (Red.whd context u)
42    in
43    let entry_eq (s1, b1, v1) (s2, b2, v2) =
44       s1 = s2 && b1 = b2 
45    in
46    let rec entry_in e = function
47       | []           -> [e]
48       | head :: tail -> 
49          head :: if entry_eq head e then tail else entry_in e tail
50    in
51    let inspect_uri main l uri tc v term =
52       let d = degree term in 
53       entry_in (Util.string_of_uriref (uri, tc), main, 2 * v + d - 1) l 
54    in
55    let rec inspect_term main l v term = match term with
56         Cic.Rel _                        -> l
57       | Cic.Meta _                       -> l
58       | Cic.Sort _                       -> l 
59       | Cic.Implicit                     -> l 
60       | Cic.Var (u,exp_named_subst)      ->
61          let l' = inspect_uri main l u [] v term in
62           inspect_exp_named_subst l' (v+1) exp_named_subst
63       | Cic.Const (u,exp_named_subst)    ->
64          let l' = inspect_uri main l u [] v term in
65           inspect_exp_named_subst l' (v+1) exp_named_subst
66       | Cic.MutInd (u, t, exp_named_subst) ->
67          let l' = inspect_uri main l u [t] v term in
68           inspect_exp_named_subst l' (v+1) exp_named_subst
69       | Cic.MutConstruct (u, t, c, exp_named_subst)    ->
70          let l' = inspect_uri main l u [t; c] v term in
71           inspect_exp_named_subst l' (v+1) exp_named_subst
72       | Cic.Cast (uu, _)                 -> 
73          inspect_term main l v uu
74       | Cic.Prod (_, uu, tt)             ->
75          let luu = inspect_term false l (v + 1) uu in
76          inspect_term main luu (v + 1) tt         
77       | Cic.Lambda (_, uu, tt)           ->
78          let luu = inspect_term false l (v + 1) uu in
79          inspect_term false luu (v + 1) tt 
80       | Cic.LetIn (_, uu, tt)            ->
81          let luu = inspect_term false l (v + 1) uu in
82          inspect_term false luu (v + 1) tt
83       | Cic.Appl m                       -> inspect_list main l true v m 
84       | Cic.MutCase (u, t, tt, uu, m) -> 
85          let lu = inspect_uri main l u [t] (v + 1) term in
86          let ltt = inspect_term false lu (v + 1) tt in
87          let luu = inspect_term false ltt (v + 1) uu in
88          inspect_list main luu false (v + 1) m
89       | Cic.Fix (_, m)                   -> inspect_ind l (v + 1) m 
90       | Cic.CoFix (_, m)                 -> inspect_coind l (v + 1) m 
91    and inspect_list main l head v = function
92       | []      -> l
93       | tt :: m -> 
94          let ltt = inspect_term main l (if head then v else v + 1) tt in
95          inspect_list false ltt false v m
96    and inspect_exp_named_subst l v = function
97         []      -> l
98       | (_,t) :: tl -> 
99          let l' = inspect_term false l v t in
100           inspect_exp_named_subst l' v tl
101    and inspect_ind l v = function
102       | []                  -> l
103       | (_, _, tt, uu) :: m ->  
104          let ltt = inspect_term false l v tt in
105          let luu = inspect_term false ltt v uu in
106          inspect_ind luu v m
107    and inspect_coind l v = function
108       | []               -> l
109       | (_, tt, uu) :: m ->
110          let ltt = inspect_term false l v tt in
111          let luu = inspect_term false ltt v uu in
112          inspect_coind luu v m
113    in
114    let rec inspect_backbone = function
115       | Cic.Cast (uu, _)      -> inspect_backbone uu
116       | Cic.Prod (_, _, tt)   -> inspect_backbone tt                
117       | Cic.LetIn (_, uu, tt) -> inspect_backbone tt
118       | t                     -> inspect_term true [] 0 t
119    in 
120    inspect_backbone term  
121
122 let out_restr e c t =   
123   let can = levels_of_term e c t in  (* can restrictions *)
124 prerr_endline "";
125 prerr_endline
126  ("#### IN LEVELS @@@@ lunghezza can: " ^ string_of_int (List.length can));
127 prerr_endline "";
128 (*  let rest = restrict level levels in   *)
129   let uri_pos (u,b,v) = (u,b) in
130   let can_use = List.map uri_pos can in
131   let lofl (u,b,v) = [(u,b)] in
132   let rec organize_restr rlist prev_r= 
133     match rlist with
134       [] -> []
135     | r::tl ->let curr_r = r@prev_r in
136                 curr_r::(organize_restr tl curr_r)
137   in
138   let mrest = List.map lofl can in
139   let must_use = organize_restr mrest [] in (* must restrictions *)
140   (must_use,can_use)
141 ;;