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 qid = M.id * M.id list (* qualified identifier: name, qualifiers *)
33 type environment = (qid, M.pars) H.t
35 type context_node = qid option (* context node: None = root *)
38 henv: environment; (* optimized global environment *)
39 path: M.id list; (* current section path *)
40 hcnt: environment; (* optimized context *)
41 node: context_node; (* current context node *)
42 nodes: context_node list; (* context node list *)
43 line: int; (* line number *)
44 explicit: bool (* need explicit context root? *)
47 type resolver = Local of int
50 let hsize = 11 (* hash tables initial size *)
52 (* Internal functions *******************************************************)
54 let initial_status size = {
55 path = []; node = None; nodes = []; line = 1; explicit = true;
56 henv = H.create size; hcnt = H.create size
59 let id_of_name (id, _, _) = id
61 let uri_of_qid (id, path) =
62 let path = String.concat "/" path in
63 let str = Filename.concat path id in
64 U.uri_of_string ("ld:/" ^ str)
66 let complete_qid f st (id, is_local, qs) =
67 let f qs = f (id, qs) in
68 let f path = Cps.list_rev_append f path ~tail:qs in
69 let rec skip f = function
70 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
71 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
74 if is_local then f st.path else skip f (st.path, qs)
76 let relax_qid f (id, path) =
77 let f path = f (id, path) in
79 | _ :: tl -> Cps.list_rev f tl
84 let relax_opt_qid f = function
86 | Some qid -> let f qid = f (Some qid) in relax_qid f qid
88 let resolve_lref f st l lenv id =
89 let rec aux f i = function
91 | (name, _) :: _ when name = id -> f (Some (M.LRef (l, i)))
92 | _ :: tl -> aux f (succ i) tl
96 let resolve_lref_strict f st l lenv id =
99 | None -> assert false
101 resolve_lref f st l lenv id
103 let resolve_gref f st qid =
104 try let args = H.find st.henv qid in f qid (Some args)
105 with Not_found -> f qid None
107 let resolve_gref_relaxed f st qid =
108 let rec g qid = function
109 | None -> relax_qid (resolve_gref g st) qid
110 | Some args -> f qid args
112 resolve_gref g st qid
114 let get_pars f st = function
116 | Some name as node ->
117 try let pars = H.find st.hcnt name in f pars None
118 with Not_found -> f [] (Some node)
120 let get_pars_relaxed f st =
121 let rec g pars = function
123 | Some node -> relax_opt_qid (get_pars g st) node
125 get_pars g st st.node
127 let rec xlate_term f st lenv = function
131 let f vv tt = f (M.Appl (vv, tt)) in
132 let f vv = xlate_term (f vv) st lenv t in
133 xlate_term f st lenv v
134 | A.Abst (name, w, t) ->
135 let add name w lenv = (name, w) :: lenv in
136 let f ww tt = f (M.Abst (name, ww, tt)) in
137 let f ww = xlate_term (f ww) st (add name ww lenv) t in
138 xlate_term f st lenv w
139 | A.GRef (name, args) ->
140 let l = List.length lenv in
142 let map1 f = xlate_term f st lenv in
143 let map2 f (id, _) = resolve_lref_strict f st l lenv id in
145 let f args = f (M.GRef (l, uri_of_qid qid, args)) in
146 let f defs = Cps.list_rev_map_append f map2 defs ~tail in
147 Cps.list_sub_strict f defs args
149 Cps.list_map f map1 args
151 let g qid = resolve_gref_relaxed g st qid in
154 | None -> complete_qid g st name
156 resolve_lref f st l lenv (id_of_name name)
158 let xlate_item f st = function
159 | A.Section (Some name) ->
160 f {st with path = name :: st.path; nodes = st.node :: st.nodes} None
162 begin match st.path, st.nodes with
163 | _ :: ptl, nhd :: ntl ->
164 f {st with path = ptl; node = nhd; nodes = ntl} None
168 f {st with node = None} None
169 | A.Context (Some name) ->
170 let f name = f {st with node = Some name} None in
171 complete_qid f st name
172 | A.Block (name, w) ->
176 H.add st.hcnt qid ((name, ww) :: pars);
177 f {st with node = Some qid} None
179 xlate_term f st pars w
181 get_pars_relaxed f st
183 complete_qid f st (name, true, [])
184 | A.Decl (name, w) ->
188 let entry = (st.line, pars, uri_of_qid qid, ww, None) in
189 H.add st.henv qid pars;
190 f {st with line = succ st.line} (Some entry)
192 xlate_term f st pars w
194 complete_qid f st (name, true, [])
196 get_pars_relaxed f st
197 | A.Def (name, w, trans, v) ->
201 let entry = (st.line, pars, uri_of_qid qid, ww, Some (trans, vv)) in
202 H.add st.henv qid pars;
203 f {st with line = succ st.line} (Some entry)
205 let f ww = xlate_term (f ww) st pars v in
206 xlate_term f st pars w
208 complete_qid f st (name, true, [])
210 get_pars_relaxed f st
212 (* Interface functions ******************************************************)
214 let initial_status = initial_status hsize
216 let meta_of_aut = xlate_item