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 henv: environment; (* optimized global environment *)
37 path: M.id list; (* current section path *)
38 hcnt: environment; (* optimized context *)
39 node: context_node; (* current context node *)
40 nodes: context_node list; (* context node list *)
41 line: int; (* line number *)
42 explicit: bool (* need explicit context root? *)
45 type resolver = Local of int
48 let hsize = 11 (* hash tables initial size *)
50 (* Internal functions *******************************************************)
52 let initial_status size = {
53 path = []; node = None; nodes = []; line = 1; explicit = true;
54 henv = H.create size; hcnt = H.create size
57 let complete_qid f st (id, is_local, qs) =
58 let f qs = f (id, qs) in
59 let f path = Cps.list_rev_append f path ~tail:qs in
60 let rec skip f = function
61 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
62 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
65 if is_local then f st.path else skip f (st.path, qs)
67 let relax_qid f (id, path) =
68 let f path = f (id, path) in
70 | _ :: tl -> Cps.list_rev f tl
75 let relax_opt_qid f = function
77 | Some qid -> let f qid = f (Some qid) in relax_qid f qid
79 let resolve_gref f st local lenv gref =
80 let rec get_local f i = function
82 | (name, _) :: _ when fst name = fst gref -> f (Some i)
83 | _ :: tl -> get_local f (succ i) tl
87 let args = H.find st.henv gref in f (Some args)
88 with Not_found -> f None
91 | Some args -> f gref (Some (Global args))
95 | Some i -> f gref (Some (Local i))
96 | None -> get_global g
98 if local then get_local f 0 lenv else f None
100 let resolve_gref_relaxed f st lenv gref =
101 let rec g gref = function
102 | None -> relax_qid (resolve_gref g st false lenv) gref
103 | Some resolved -> f gref resolved
105 resolve_gref g st true lenv gref
107 let get_pars f st = function
109 | Some name as node ->
110 try let pars = H.find st.hcnt name in f pars None
111 with Not_found -> f [] (Some node)
113 let get_pars_relaxed f st =
114 let rec g pars = function
116 | Some node -> relax_opt_qid (get_pars g st) node
118 get_pars g st st.node
120 let rec xlate_term f st lenv = function
121 | A.Sort sort -> f (M.Sort sort)
123 let f vv tt = f (M.Appl (vv, tt)) in
124 let f vv = xlate_term (f vv) st lenv t in
125 xlate_term f st lenv v
126 | A.Abst (name, w, t) ->
127 let add name w lenv =
128 let f name = (name, w) :: lenv in
129 complete_qid f st (name, true, [])
131 let f ww tt = f (M.Abst (name, ww, tt)) in
132 let f ww = xlate_term (f ww) st (add name ww lenv) t in
133 xlate_term f st lenv w
134 | A.GRef (name, args) ->
135 let f name = function
136 | Local i -> f (M.LRef i)
138 let l = List.length lenv in
139 let map1 f = xlate_term f st lenv in
140 let map2 f (name, _) = f (M.GRef (l, name, [])) in
142 let f args = f (M.GRef (l, name, args)) in
143 let f defs = Cps.list_rev_map_append f map2 defs ~tail in
144 Cps.list_sub_strict f defs args
146 Cps.list_map f map1 args
148 let f name = resolve_gref_relaxed f st lenv name in
149 complete_qid f st name
151 let xlate_item f st = function
152 | A.Section (Some name) ->
153 f {st with path = name :: st.path; nodes = st.node :: st.nodes} None
155 begin match st.path, st.nodes with
156 | _ :: ptl, nhd :: ntl ->
157 f {st with path = ptl; node = nhd; nodes = ntl} None
161 f {st with node = None} None
162 | A.Context (Some name) ->
163 let f name = f {st with node = Some name} None in
164 complete_qid f st name
165 | A.Block (name, w) ->
169 H.add st.hcnt name ((name, ww) :: pars);
170 f {st with node = Some name} None
172 xlate_term f st pars w
174 get_pars_relaxed f st
176 complete_qid f st (name, true, [])
177 | A.Decl (name, w) ->
181 let entry = (st.line, pars, name, ww, None) in
182 H.add st.henv name pars;
183 f {st with line = succ st.line} (Some entry)
185 xlate_term f st pars w
187 complete_qid f st (name, true, [])
189 get_pars_relaxed f st
190 | A.Def (name, w, trans, v) ->
194 let entry = (st.line, pars, name, ww, Some (trans, vv)) in
195 H.add st.henv name pars;
196 f {st with line = succ st.line} (Some entry)
198 let f ww = xlate_term (f ww) st pars v in
199 xlate_term f st pars w
201 complete_qid f st (name, true, [])
203 get_pars_relaxed f st
205 (* Interface functions ******************************************************)
207 let initial_status = initial_status hsize
209 let meta_of_aut = xlate_item