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 line: int; (* line number *)
43 explicit: bool (* need explicit context root? *)
46 type resolver = Local of int
49 let hsize = 11 (* hash tables initial size *)
51 let initial_status size = {
52 genv = []; path = []; node = None; nodes = []; line = 1; explicit = true;
53 henv = H.create size; hcnt = H.create size
56 let complete_qid f st (id, is_local, qs) =
57 let f qs = f (id, qs) in
58 let f path = Cps.list_rev_append f path ~tail:qs in
59 let rec skip f = function
60 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
61 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
64 if is_local then f st.path else skip f (st.path, qs)
66 let relax_qid f (id, path) =
67 let f path = f (id, path) in
69 | _ :: tl -> Cps.list_rev f tl
74 let relax_opt_qid f = function
76 | Some qid -> let f qid = f (Some qid) in relax_qid f qid
78 let resolve_gref f st local lenv gref =
79 let rec get_local f i = function
81 | (name, _) :: _ when fst name = fst gref -> f (Some i)
82 | _ :: tl -> get_local f (succ i) tl
86 let args = H.find st.henv gref in f (Some args)
87 with Not_found -> f None
90 | Some args -> f gref (Some (Global args))
94 | Some i -> f gref (Some (Local i))
95 | None -> get_global g
97 if local then get_local f 0 lenv else f None
99 let resolve_gref_relaxed f st lenv gref =
100 let rec g gref = function
101 | None -> relax_qid (resolve_gref g st false lenv) gref
102 | Some resolved -> f gref resolved
104 resolve_gref g st true lenv gref
106 let get_pars f st = function
108 | Some name as node ->
109 try let pars = H.find st.hcnt name in f pars None
110 with Not_found -> f [] (Some node)
112 let get_pars_relaxed f st =
113 let rec g pars = function
115 | Some node -> relax_opt_qid (get_pars g st) node
117 get_pars g st st.node
119 let rec xlate_term f st lenv = function
120 | A.Sort sort -> f (M.Sort sort)
122 let f vv tt = f (M.Appl (vv, tt)) in
123 let f vv = xlate_term (f vv) st lenv t in
124 xlate_term f st lenv v
125 | A.Abst (name, w, t) ->
126 let add name w lenv =
127 let f name = (name, w) :: lenv in
128 complete_qid f st (name, true, [])
130 let f ww tt = f (M.Abst (name, ww, tt)) in
131 let f ww = xlate_term (f ww) st (add name ww lenv) t in
132 xlate_term f st lenv w
133 | A.GRef (name, args) ->
134 let f name = function
135 | Local i -> f (M.LRef i)
137 let l = List.length lenv in
138 let map1 f = xlate_term f st lenv in
139 let map2 f (name, _) = f (M.GRef (l, name, [])) in
141 let f args = f (M.GRef (l, name, args)) in
142 let f defs = Cps.list_rev_map_append f map2 defs ~tail in
143 Cps.list_sub_strict f defs args
145 Cps.list_map f map1 args
147 let f name = resolve_gref_relaxed f st lenv name in
148 complete_qid f st name
150 let xlate_item f st = function
151 | A.Section (Some name) ->
152 f {st with path = name :: st.path; nodes = st.node :: st.nodes}
154 begin match st.path, st.nodes with
155 | _ :: ptl, nhd :: ntl ->
156 f {st with path = ptl; node = nhd; nodes = ntl}
160 f {st with node = None}
161 | A.Context (Some name) ->
162 let f name = f {st with node = Some name} in
163 complete_qid f st name
164 | A.Block (name, w) ->
168 H.add st.hcnt name ((name, ww) :: pars);
169 f {st with node = Some name}
171 xlate_term f st pars w
173 get_pars_relaxed f st
175 complete_qid f st (name, true, [])
176 | A.Decl (name, w) ->
180 let entry = (st.line, pars, name, ww, None) in
181 H.add st.henv name pars;
182 f {st with genv = entry :: st.genv; line = succ st.line}
184 xlate_term f st pars w
186 complete_qid f st (name, true, [])
188 get_pars_relaxed f st
189 | A.Def (name, w, trans, v) ->
193 let entry = (st.line, pars, name, ww, Some (trans, vv)) in
194 H.add st.henv name pars;
195 f {st with genv = entry :: st.genv; line = succ st.line}
197 let f ww = xlate_term (f ww) st pars v in
198 xlate_term f st pars w
200 complete_qid f st (name, true, [])
202 get_pars_relaxed f st
204 let meta_of_aut f book =
205 let f st = f st.genv in
206 Cps.list_fold_left f xlate_item (initial_status hsize) book