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_______________________________________________________________ *)
19 (* qualified identifier: uri, name, qualifiers *)
20 type qid = M.uri * M.id * M.id list
22 type environment = M.pars H.t
24 type context_node = qid option (* context node: None = root *)
27 henv: environment; (* optimized global environment *)
28 path: M.id list; (* current section path *)
29 hcnt: environment; (* optimized context *)
30 node: context_node; (* current context node *)
31 nodes: context_node list; (* context node list *)
32 line: int; (* line number *)
33 cover: string (* initial segment of URI hierarchy *)
36 type resolver = Local of int
39 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
41 (* Internal functions *******************************************************)
43 let initial_status cover = {
44 path = []; node = None; nodes = []; line = 1; cover = cover;
45 henv = H.create henv_size; hcnt = H.create hcnt_size
48 let id_of_name (id, _, _) = id
50 let mk_qid st id path =
51 let uripath = if st.cover = "" then path else st.cover :: path in
52 let str = String.concat "/" uripath in
53 let str = Filename.concat str id in
54 U.uri_of_string ("ld:/" ^ str ^ ".ld"), id, path
56 let uri_of_qid (uri, _, _) = uri
58 let complete_qid f st (id, is_local, qs) =
59 let f qs = f (mk_qid st id qs) in
60 let f path = C.list_rev_append f path ~tail:qs in
61 let rec skip f = function
62 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
63 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
66 if is_local then f st.path else skip f (st.path, qs)
68 let relax_qid f st (_, id, path) =
69 let f path = f (mk_qid st id path) in
71 | _ :: tl -> C.list_rev f tl
76 let relax_opt_qid f st = function
78 | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
80 let resolve_lref f st l lenv id =
81 let rec aux f i = function
83 | (name, _) :: _ when name = id -> f (Some (M.LRef (l, i)))
84 | _ :: tl -> aux f (succ i) tl
88 let resolve_lref_strict f st l lenv id =
91 | None -> assert false
93 resolve_lref f st l lenv id
95 let resolve_gref f st qid =
96 try let args = H.find st.henv (uri_of_qid qid) in f qid (Some args)
97 with Not_found -> f qid None
99 let resolve_gref_relaxed f st qid =
100 let rec g qid = function
101 | None -> relax_qid (resolve_gref g st) st qid
102 | Some args -> f qid args
104 resolve_gref g st qid
106 let get_pars f st = function
108 | Some qid as node ->
109 try let pars = H.find st.hcnt (uri_of_qid qid) 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) st node
117 get_pars g st st.node
119 let rec xlate_term f st lenv = function
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 = (name, w) :: lenv in
128 let f ww tt = f (M.Abst (name, ww, tt)) in
129 let f ww = xlate_term (f ww) st (add name ww lenv) t in
130 xlate_term f st lenv w
131 | A.GRef (name, args) ->
132 let l = List.length lenv in
134 let map1 f = xlate_term f st lenv in
135 let map2 f (id, _) = resolve_lref_strict f st l lenv id in
137 let f args = f (M.GRef (l, uri_of_qid qid, args)) in
138 let f defs = C.list_rev_map_append f map2 defs ~tail in
139 C.list_sub_strict f defs args
141 C.list_map f map1 args
143 let g qid = resolve_gref_relaxed g st qid in
146 | None -> complete_qid g st name
148 resolve_lref f st l lenv (id_of_name name)
150 let xlate_entity err f st = function
151 | A.Section (Some (_, name)) ->
152 err {st with path = name :: st.path; nodes = st.node :: st.nodes}
154 begin match st.path, st.nodes with
155 | _ :: ptl, nhd :: ntl ->
156 err {st with path = ptl; node = nhd; nodes = ntl}
160 err {st with node = None}
161 | A.Context (Some name) ->
162 let f name = err {st with node = Some name} in
163 complete_qid f st name
164 | A.Block (name, w) ->
168 H.add st.hcnt (uri_of_qid qid) ((name, ww) :: pars);
169 err {st with node = Some qid}
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 H.add st.henv (uri_of_qid qid) pars;
181 let a = [Y.Mark st.line] in
182 let entry = pars, ww, None in
183 let entity = a, uri_of_qid qid, Y.Abst entry in
184 f {st with line = succ st.line} entity
186 xlate_term f st pars w
188 complete_qid f st (name, true, [])
190 get_pars_relaxed f st
191 | A.Def (name, w, trans, v) ->
195 H.add st.henv (uri_of_qid qid) pars;
196 let a = Y.Mark st.line :: if trans then [] else [Y.Priv] in
197 let entry = pars, ww, Some vv in
198 let entity = a, uri_of_qid qid, Y.Abbr entry in
199 f {st with line = succ st.line} entity
201 let f ww = xlate_term (f ww) st pars v in
202 xlate_term f st pars w
204 complete_qid f st (name, true, [])
206 get_pars_relaxed f st
208 (* Interface functions ******************************************************)
210 let initial_status ?(cover="") () =
213 let meta_of_aut = xlate_entity