1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (* AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
33 let text_of_entries out entries =
34 out "(** MatchConclusion: results of the term inspection **)\n";
35 let text_of_entry (u, b, v) =
36 out (string_of_int v ^ " ");
37 out (if b then "$MC " else "$IC ");
39 in List.iter text_of_entry entries
41 let sort_entries entries =
42 let comparator (_, _, v1) (_, _, v2) = compare v1 v2 in
43 List.fast_sort comparator entries
45 let levels_of_term metasenv context term =
46 let module TC = CicTypeChecker in
47 let module Red = CicReduction in
49 let rec degree_aux = function
51 | Cic.Cast (u, _) -> degree_aux u
52 | Cic.Prod (_, _, t) -> degree_aux t
55 let u,_ = TC.type_of_aux' metasenv context t CicUniv.empty_ugraph in
56 degree_aux (Red.whd context u)
58 let entry_eq (s1, b1, v1) (s2, b2, v2) =
61 let rec entry_in e = function
64 head :: if entry_eq head e then tail else entry_in e tail
66 let inspect_uri main l uri tc v term =
67 let d = degree term in
68 entry_in (UriManager.string_of_uriref (uri, tc), main, 2 * v + d - 1) l
70 let rec inspect_term main l v term = match term with
75 | Cic.Var (u,exp_named_subst) ->
76 inspect_exp_named_subst l (succ v) 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
81 | Cic.Const (u,exp_named_subst) ->
82 let l' = inspect_uri main l u [] v term in
83 inspect_exp_named_subst l' (succ v) exp_named_subst
84 | Cic.MutInd (u, t, exp_named_subst) ->
85 let l' = inspect_uri main l u [t] v term in
86 inspect_exp_named_subst l' (succ v) exp_named_subst
87 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
88 let l' = inspect_uri main l u [t; c] v term in
89 inspect_exp_named_subst l' (succ v) exp_named_subst
91 inspect_term main l v uu
92 | Cic.Prod (_, uu, tt) ->
93 let luu = inspect_term false l (succ v) uu in
94 inspect_term main luu (succ v) tt
95 | Cic.Lambda (_, uu, tt) ->
96 let luu = inspect_term false l (succ v) uu in
97 inspect_term false luu (succ v) tt
98 | Cic.LetIn (_, uu, tt) ->
99 let luu = inspect_term false l (succ v) uu in
100 inspect_term false luu (succ v) tt
101 | Cic.Appl m -> inspect_list main l true v m
102 | Cic.MutCase (u, t, tt, uu, m) ->
103 let lu = inspect_uri main l u [t] (succ v) term in
104 let ltt = inspect_term false lu (succ v) tt in
105 let luu = inspect_term false ltt (succ v) uu in
106 inspect_list main luu false (succ v) m
107 | Cic.Fix (_, m) -> inspect_ind l (succ v) m
108 | Cic.CoFix (_, m) -> inspect_coind l (succ v) m
109 and inspect_list main l head v = function
112 let ltt = inspect_term main l (if head then v else v + 1) tt in
113 inspect_list false ltt false v m
114 and inspect_exp_named_subst l v = function
117 let l' = inspect_term false l v t in
118 inspect_exp_named_subst l' v tl
119 and inspect_ind l v = function
121 | (_, _, tt, uu) :: m ->
122 let ltt = inspect_term false l v tt in
123 let luu = inspect_term false ltt v uu in
125 and inspect_coind l v = function
127 | (_, tt, uu) :: m ->
128 let ltt = inspect_term false l v tt in
129 let luu = inspect_term false ltt v uu in
130 inspect_coind luu v m
132 let rec inspect_backbone = function
133 | Cic.Cast (uu, _) -> inspect_backbone uu
134 | Cic.Prod (_, _, tt) -> inspect_backbone tt
135 | Cic.LetIn (_, uu, tt) -> inspect_backbone tt
136 | t -> inspect_term true [] 0 t
138 inspect_backbone term
140 let get_constraints e c t =
141 let can = sort_entries (levels_of_term e c t) in (* can restrictions *)
142 text_of_entries prerr_string can; flush stderr; (* logging *)
143 let rest_of (u, b, _) =
144 let p = if b then `MainConclusion None else `InConclusion in (p, u)
146 let rec split vp = function
147 | [], ((_, _, v) as hd) :: tl -> split v ([rest_of hd], tl)
148 | prev, ((_, _, ve) as hd) :: tl when vp = ve ->
149 split vp (rest_of hd :: prev, tl)
152 let rec mk_musts prev acc = function
155 let slice, next = split 0 ([], l) in
156 let acc = acc @ slice in
157 mk_musts (prev @ [acc]) acc next
161 let universe = [T.MainConclusion; T.InConclusion]