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 (******************************************************************************)
30 (* Ferruccio Guidi <fguidi@cs.unibo.it> *)
34 (******************************************************************************)
36 (* level managing functions *************************************************)
38 type levels_spec = (string * bool * int) list
40 let levels_of_term metasenv context term =
41 let module TC = CicTypeChecker in
42 let module Red = CicReduction in
43 let module Util = MQueryUtil in
45 let rec degree_aux = function
47 | Cic.Cast (u, _) -> degree_aux u
48 | Cic.Prod (_, _, t) -> degree_aux t
51 let u = TC.type_of_aux' metasenv context t in
52 degree_aux (Red.whd context u)
54 let entry_eq (s1, b1, v1) (s2, b2, v2) =
57 let rec entry_in e = function
60 head :: if entry_eq head e then tail else entry_in e tail
62 let inspect_uri main l uri tc v term =
63 let d = degree term in
64 entry_in (Util.string_of_uriref (uri, tc), main, 2 * v + d - 1) l
66 let rec inspect_term main l v term = match term with
71 | Cic.Var (u,exp_named_subst) ->
72 let l' = inspect_uri main l u [] v term in
73 inspect_exp_named_subst l' (v+1) exp_named_subst
74 | Cic.Const (u,exp_named_subst) ->
75 let l' = inspect_uri main l u [] v term in
76 inspect_exp_named_subst l' (v+1) exp_named_subst
77 | Cic.MutInd (u, t, exp_named_subst) ->
78 let l' = inspect_uri main l u [t] v term in
79 inspect_exp_named_subst l' (v+1) exp_named_subst
80 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
81 let l' = inspect_uri main l u [t; c] v term in
82 inspect_exp_named_subst l' (v+1) exp_named_subst
84 inspect_term main l v uu
85 | Cic.Prod (_, uu, tt) ->
86 let luu = inspect_term false l (v + 1) uu in
87 inspect_term main luu (v + 1) tt
88 | Cic.Lambda (_, uu, tt) ->
89 let luu = inspect_term false l (v + 1) uu in
90 inspect_term false luu (v + 1) tt
91 | Cic.LetIn (_, uu, tt) ->
92 let luu = inspect_term false l (v + 1) uu in
93 inspect_term false luu (v + 1) tt
94 | Cic.Appl m -> inspect_list main l true v m
95 | Cic.MutCase (u, t, tt, uu, m) ->
96 let lu = inspect_uri main l u [t] (v + 1) term in
97 let ltt = inspect_term false lu (v + 1) tt in
98 let luu = inspect_term false ltt (v + 1) uu in
99 inspect_list main luu false (v + 1) m
100 | Cic.Fix (_, m) -> inspect_ind l (v + 1) m
101 | Cic.CoFix (_, m) -> inspect_coind l (v + 1) m
102 and inspect_list main l head v = function
105 let ltt = inspect_term main l (if head then v else v + 1) tt in
106 inspect_list false ltt false v m
107 and inspect_exp_named_subst l v = function
110 let l' = inspect_term false l v t in
111 inspect_exp_named_subst l' v tl
112 and inspect_ind l v = function
114 | (_, _, tt, uu) :: m ->
115 let ltt = inspect_term false l v tt in
116 let luu = inspect_term false ltt v uu in
118 and inspect_coind l v = function
120 | (_, tt, uu) :: m ->
121 let ltt = inspect_term false l v tt in
122 let luu = inspect_term false ltt v uu in
123 inspect_coind luu v m
125 let rec inspect_backbone = function
126 | Cic.Cast (uu, _) -> inspect_backbone uu
127 | Cic.Prod (_, _, tt) -> inspect_backbone tt
128 | Cic.LetIn (_, uu, tt) -> inspect_backbone tt
129 | t -> inspect_term true [] 0 t
131 inspect_backbone term
133 let string_of_levels l sep =
134 let entry_out (s, b, v) =
135 let pos = if b then " HEAD: " else " TAIL: " in
136 string_of_int v ^ pos ^ s ^ sep
138 let rec levels_out = function
140 | head :: tail -> entry_out head ^ levels_out tail