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