2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
17 type qid = M.id * M.id list (* qualified identifier: name, qualifiers *)
19 type environment = (qid, M.pars) H.t
21 type context_node = qid option (* context node: None = root *)
24 henv: environment; (* optimized global environment *)
25 path: M.id list; (* current section path *)
26 hcnt: environment; (* optimized context *)
27 node: context_node; (* current context node *)
28 nodes: context_node list; (* context node list *)
29 line: int; (* line number *)
30 explicit: bool (* need explicit context root? *)
33 type resolver = Local of int
36 let hsize = 7000 (* hash tables initial size *)
38 (* Internal functions *******************************************************)
40 let initial_status size = {
41 path = []; node = None; nodes = []; line = 1; explicit = true;
42 henv = H.create size; hcnt = H.create size
45 let id_of_name (id, _, _) = id
47 let uri_of_qid (id, path) =
48 let path = String.concat "/" path in
49 let str = Filename.concat path id in
50 U.uri_of_string ("ld:/" ^ str)
52 let complete_qid f st (id, is_local, qs) =
53 let f qs = f (id, qs) in
54 let f path = Cps.list_rev_append f path ~tail:qs in
55 let rec skip f = function
56 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
57 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
60 if is_local then f st.path else skip f (st.path, qs)
62 let relax_qid f (id, path) =
63 let f path = f (id, path) in
65 | _ :: tl -> Cps.list_rev f tl
70 let relax_opt_qid f = function
72 | Some qid -> let f qid = f (Some qid) in relax_qid f qid
74 let resolve_lref f st l lenv id =
75 let rec aux f i = function
77 | (name, _) :: _ when name = id -> f (Some (M.LRef (l, i)))
78 | _ :: tl -> aux f (succ i) tl
82 let resolve_lref_strict f st l lenv id =
85 | None -> assert false
87 resolve_lref f st l lenv id
89 let resolve_gref f st qid =
90 try let args = H.find st.henv qid in f qid (Some args)
91 with Not_found -> f qid None
93 let resolve_gref_relaxed f st qid =
94 let rec g qid = function
95 | None -> relax_qid (resolve_gref g st) qid
96 | Some args -> f qid args
100 let get_pars f st = function
102 | Some name as node ->
103 try let pars = H.find st.hcnt name in f pars None
104 with Not_found -> f [] (Some node)
106 let get_pars_relaxed f st =
107 let rec g pars = function
109 | Some node -> relax_opt_qid (get_pars g st) node
111 get_pars g st st.node
113 let rec xlate_term f st lenv = function
117 let f vv tt = f (M.Appl (vv, tt)) in
118 let f vv = xlate_term (f vv) st lenv t in
119 xlate_term f st lenv v
120 | A.Abst (name, w, t) ->
121 let add name w lenv = (name, w) :: lenv in
122 let f ww tt = f (M.Abst (name, ww, tt)) in
123 let f ww = xlate_term (f ww) st (add name ww lenv) t in
124 xlate_term f st lenv w
125 | A.GRef (name, args) ->
126 let l = List.length lenv in
128 let map1 f = xlate_term f st lenv in
129 let map2 f (id, _) = resolve_lref_strict f st l lenv id in
131 let f args = f (M.GRef (l, uri_of_qid qid, args)) in
132 let f defs = Cps.list_rev_map_append f map2 defs ~tail in
133 Cps.list_sub_strict f defs args
135 Cps.list_map f map1 args
137 let g qid = resolve_gref_relaxed g st qid in
140 | None -> complete_qid g st name
142 resolve_lref f st l lenv (id_of_name name)
144 let xlate_item f st = function
145 | A.Section (Some name) ->
146 f {st with path = name :: st.path; nodes = st.node :: st.nodes} None
148 begin match st.path, st.nodes with
149 | _ :: ptl, nhd :: ntl ->
150 f {st with path = ptl; node = nhd; nodes = ntl} None
154 f {st with node = None} None
155 | A.Context (Some name) ->
156 let f name = f {st with node = Some name} None in
157 complete_qid f st name
158 | A.Block (name, w) ->
162 H.add st.hcnt qid ((name, ww) :: pars);
163 f {st with node = Some qid} None
165 xlate_term f st pars w
167 get_pars_relaxed f st
169 complete_qid f st (name, true, [])
170 | A.Decl (name, w) ->
174 let entry = (st.line, pars, uri_of_qid qid, ww, None) in
175 H.add st.henv qid pars;
176 f {st with line = succ st.line} (Some entry)
178 xlate_term f st pars w
180 complete_qid f st (name, true, [])
182 get_pars_relaxed f st
183 | A.Def (name, w, trans, v) ->
187 let entry = (st.line, pars, uri_of_qid qid, ww, Some (trans, vv)) in
188 H.add st.henv qid pars;
189 f {st with line = succ st.line} (Some entry)
191 let f ww = xlate_term (f ww) st pars v in
192 xlate_term f st pars w
194 complete_qid f st (name, true, [])
196 get_pars_relaxed f st
198 (* Interface functions ******************************************************)
200 let initial_status = initial_status hsize
202 let meta_of_aut = xlate_item