]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_generator/cGMatchConclusion.ml
- new generated query "unreferred" implemented at server side
[helm.git] / helm / ocaml / mathql_generator / cGMatchConclusion.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 text_of_entries out entries =
30    out "(** MatchConclusion: results of the term inspection **)\n";
31    let text_of_entry (u, b, v) =
32       out (string_of_int v ^ " ");
33       out (if b then "$MC " else "$IC ");   
34       out (u ^ "\n")
35    in List.iter text_of_entry entries
36
37 let sort_entries entries =
38    let comparator (_, _, v1) (_, _, v2) = compare v1 v2 in 
39    List.fast_sort comparator entries
40    
41 let levels_of_term metasenv context term =
42    let module TC = CicTypeChecker in
43    let module Red = CicReduction in
44    let module Util = MQueryUtil in
45    let degree t =
46       let rec degree_aux = function
47          | Cic.Sort _         -> 1 
48          | Cic.Cast (u, _)    -> degree_aux u
49          | Cic.Prod (_, _, t) -> degree_aux t
50          | _                  -> 2
51       in 
52       let u = TC.type_of_aux' metasenv context t in
53       degree_aux (Red.whd context u)
54    in
55    let entry_eq (s1, b1, v1) (s2, b2, v2) =
56       s1 = s2 && b1 = b2 
57    in
58    let rec entry_in e = function
59       | []           -> [e]
60       | head :: tail -> 
61          head :: if entry_eq head e then tail else entry_in e tail
62    in
63    let inspect_uri main l uri tc v term =
64       let d = degree term in 
65       entry_in (Util.string_of_uriref (uri, tc), main, 2 * v + d - 1) l 
66    in
67    let rec inspect_term main l v term = match term with
68         Cic.Rel _                        -> l
69       | Cic.Meta _                       -> l
70       | Cic.Sort _                       -> l 
71       | Cic.Implicit                     -> l 
72       | Cic.Var (u,exp_named_subst)      ->
73          let l' = inspect_uri main l u [] v term in
74           inspect_exp_named_subst l' (succ v) exp_named_subst
75       | Cic.Const (u,exp_named_subst)    ->
76          let l' = inspect_uri main l u [] v term in
77           inspect_exp_named_subst l' (succ v) exp_named_subst
78       | Cic.MutInd (u, t, exp_named_subst) ->
79          let l' = inspect_uri main l u [t] v term in
80           inspect_exp_named_subst l' (succ v) exp_named_subst
81       | Cic.MutConstruct (u, t, c, exp_named_subst)    ->
82          let l' = inspect_uri main l u [t; c] v term in
83           inspect_exp_named_subst l' (succ v) exp_named_subst
84       | Cic.Cast (uu, _)                 -> 
85          inspect_term main l v uu
86       | Cic.Prod (_, uu, tt)             ->
87          let luu = inspect_term false l (succ v) uu in
88          inspect_term main luu (succ v) tt         
89       | Cic.Lambda (_, uu, tt)           ->
90          let luu = inspect_term false l (succ v) uu in
91          inspect_term false luu (succ v) tt 
92       | Cic.LetIn (_, uu, tt)            ->
93          let luu = inspect_term false l (succ v) uu in
94          inspect_term false luu (succ v) tt
95       | Cic.Appl m                       -> inspect_list main l true v m 
96       | Cic.MutCase (u, t, tt, uu, m) -> 
97          let lu = inspect_uri main l u [t] (succ v) term in
98          let ltt = inspect_term false lu (succ v) tt in
99          let luu = inspect_term false ltt (succ v) uu in
100          inspect_list main luu false (succ v) m
101       | Cic.Fix (_, m)                   -> inspect_ind l (succ v) m 
102       | Cic.CoFix (_, m)                 -> inspect_coind l (succ v) m 
103    and inspect_list main l head v = function
104       | []      -> l
105       | tt :: m -> 
106          let ltt = inspect_term main l (if head then v else v + 1) tt in
107          inspect_list false ltt false v m
108    and inspect_exp_named_subst l v = function
109         []      -> l
110       | (_,t) :: tl -> 
111          let l' = inspect_term false l v t in
112           inspect_exp_named_subst l' v tl
113    and inspect_ind l v = function
114       | []                  -> l
115       | (_, _, tt, uu) :: m ->  
116          let ltt = inspect_term false l v tt in
117          let luu = inspect_term false ltt v uu in
118          inspect_ind luu v m
119    and inspect_coind l v = function
120       | []               -> l
121       | (_, tt, uu) :: m ->
122          let ltt = inspect_term false l v tt in
123          let luu = inspect_term false ltt v uu in
124          inspect_coind luu v m
125    in
126    let rec inspect_backbone = function
127       | Cic.Cast (uu, _)      -> inspect_backbone uu
128       | Cic.Prod (_, _, tt)   -> inspect_backbone tt                
129       | Cic.LetIn (_, uu, tt) -> inspect_backbone tt
130       | t                     -> inspect_term true [] 0 t
131    in 
132    inspect_backbone term  
133
134 let get_constraints e c t =   
135    let can = sort_entries (levels_of_term e c t) in  (* can restrictions *)
136    text_of_entries prerr_string can; flush stderr;   (* logging *)
137    let rest_of (u, b, _) =
138       let p = if b then `MainConclusion None else `InConclusion in (p, u)
139    in
140    let rec split vp = function
141       | [], ((_, _, v) as hd) :: tl -> split v ([rest_of hd], tl)
142       | prev, ((_, _, ve) as hd) :: tl  when vp = ve ->
143          split vp (rest_of hd :: prev, tl)
144       | p, l -> p, l
145    in
146    let rec mk_musts prev acc = function
147       | [] -> prev, acc
148       | l  -> 
149          let slice, next = split 0 ([], l) in
150          let acc = acc @ slice in
151          mk_musts (prev @ [acc]) acc next
152    in
153    mk_musts [] [] can   
154 (*
155   let uri_pos (u,b,v) = (u,b) in
156   let can_use = List.map uri_pos can in
157   let lofl (u,b,v) = [(u,b)] in
158   let rec organize_restr rlist prev_r= 
159     match rlist with
160       [] -> []
161     | r::tl ->let curr_r = r@prev_r in
162                 curr_r::(organize_restr tl curr_r)
163   in
164   let mrest = List.map lofl can in
165   let must_use = organize_restr mrest [] in (* must restrictions *)
166   (must_use,can_use)
167 *)