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/.
31 type environment = (M.qid, M.pars) H.t
33 type context_node = M.qid option (* context node: None = root *)
36 genv: M.environment; (* global environment *)
37 henv: environment; (* optimized global environment *)
38 path: M.id list; (* current section path *)
39 hcnt: environment; (* optimized context *)
40 node: context_node; (* current context node *)
41 nodes: context_node list; (* context node list *)
42 explicit: bool (* need explicit context root? *)
45 type resolver = Local of int
48 let hsize = 11 (* hash tables initial size *)
50 let initial_status size = {
51 genv = []; path = []; node = None; nodes = []; explicit = true;
52 henv = H.create size; hcnt = H.create size
55 let complete_qid f st (id, is_local, qs) =
56 let f qs = f (id, qs) in
57 let f path = Cps.list_rev_append f path ~tail:qs in
58 let rec skip f = function
59 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
60 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
63 if is_local then f st.path else skip f (st.path, qs)
65 let relax_qid f (id, path) =
66 let f path = f (id, path) in
68 | _ :: tl -> Cps.list_rev f tl
73 let relax_opt_qid f = function
75 | Some qid -> let f qid = f (Some qid) in relax_qid f qid
77 let resolve_gref f st lenv gref =
78 let rec get_local f i = function
80 | (name, _) :: _ when fst name = fst gref -> f (Some i)
81 | _ :: tl -> get_local f (succ i) tl
85 let args = H.find st.henv gref in f (Some args)
86 with Not_found -> f None
89 | Some args -> f gref (Some (Global args))
93 | Some i -> f gref (Some (Local i))
94 | None -> get_global g
98 let resolve_gref_relaxed f st lenv gref =
99 let rec g gref = function
100 | None -> relax_qid (resolve_gref g st lenv) gref
101 | Some resolved -> f gref resolved
103 resolve_gref g st lenv gref
105 let get_pars f st = function
107 | Some name as node ->
108 try let pars = H.find st.hcnt name in f pars None
109 with Not_found -> f [] (Some node)
111 let get_pars_relaxed f st =
112 let rec g pars = function
114 | Some node -> relax_opt_qid (get_pars g st) node
116 get_pars g st st.node
118 let rec xlate_term f st lenv = function
119 | A.Sort sort -> f (M.Sort sort)
121 let f vv tt = f (M.Appl (vv, tt)) in
122 let f vv = xlate_term (f vv) st lenv t in
123 xlate_term f st lenv v
124 | A.Abst (name, w, t) ->
125 let add name w lenv =
126 let f name = (name, w) :: lenv in
127 complete_qid f st (name, true, [])
129 let f ww tt = f (M.Abst (name, ww, tt)) in
130 let f ww = xlate_term (f ww) st (add name ww lenv) t in
131 xlate_term f st lenv w
132 | A.GRef (name, args) ->
133 let f name = function
134 | Local i -> f (M.LRef i)
136 let map1 f = xlate_term f st lenv in
137 let map2 f (name, _) = f (M.GRef (name, [])) in
139 let f args = f (M.GRef (name, args)) in
140 let f defs = Cps.list_rev_map_append f map2 defs ~tail in
141 Cps.list_sub_strict f defs args
143 Cps.list_map f map1 args
145 let f name = resolve_gref_relaxed f st lenv name in
146 complete_qid f st name
148 let xlate_item f st = function
149 | A.Section (Some name) ->
150 f {st with path = name :: st.path; nodes = st.node :: st.nodes}
152 begin match st.path, st.nodes with
153 | _ :: ptl, nhd :: ntl ->
154 f {st with path = ptl; node = nhd; nodes = ntl}
158 f {st with node = None}
159 | A.Context (Some name) ->
160 let f name = f {st with node = Some name} in
161 complete_qid f st name
162 | A.Block (name, w) ->
166 H.add st.hcnt name ((name, ww) :: pars);
167 f {st with node = Some name}
169 xlate_term f st pars w
171 get_pars_relaxed f st
173 complete_qid f st (name, true, [])
174 | A.Decl (name, w) ->
178 let entry = (pars, name, ww, None) in
179 H.add st.henv name pars;
180 f {st with genv = entry :: st.genv}
182 xlate_term f st pars w
184 complete_qid f st (name, true, [])
186 get_pars_relaxed f st
187 | A.Def (name, w, trans, v) ->
191 let entry = (pars, name, ww, Some (trans, vv)) in
192 H.add st.henv name pars;
193 f {st with genv = entry :: st.genv}
195 let f ww = xlate_term (f ww) st pars v in
196 xlate_term f st pars w
198 complete_qid f st (name, true, [])
200 get_pars_relaxed f st
202 let meta_of_aut f book =
203 let f st = f st.genv in
204 Cps.list_fold_left f xlate_item (initial_status hsize) book