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