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_______________________________________________________________ *)
21 (* qualified identifier: uri, name, qualifiers *)
22 type qid = D.uri * D.id * D.id list
24 type context_node = qid option (* context node: None = root *)
27 path: D.id list; (* current section path *)
28 node: context_node; (* current context node *)
29 nodes: context_node list; (* context node list *)
30 line: int; (* line number *)
31 mk_uri: G.uri_generator; (* uri generator *)
34 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
36 let henv = UH.create henv_size (* optimized global environment *)
38 let hcnt = UH.create hcnt_size (* optimized context *)
40 (* Internal functions *******************************************************)
42 let empty_cnt = D.empty_lenv
45 if id.[0] >= '0' && id.[0] <= '9' then !G.alpha ^ id else id
47 let attrs_for_abst id aw =
48 let id = if !G.alpha <> "" then alpha id else id in
49 let main = E.succ aw.E.n_main in
50 E.node_attrs ~name:(id, true) ~side:aw.E.n_main ~main ()
52 let attrs_for_decl aw =
53 let main = E.succ aw.E.n_main in
54 E.node_attrs ~side:aw.E.n_main ~main ()
56 let add_abst cnt id aw w =
57 let a = attrs_for_abst id aw in
58 let l = if !G.infinity then N.infinity else N.two in
59 D.EBind (cnt, a, D.Abst (false, l, w))
61 let mk_lref f a i = f a (D.TLRef (a, i))
63 let id_of_name (id, _, _) =
64 if !G.alpha <> "" then alpha id else id
66 let mk_qid f lst id path =
67 let str = String.concat "/" path in
68 let str = Filename.concat str id in
69 let str = lst.mk_uri str in
70 f (U.uri_of_string str, id, path)
72 let uri_of_qid (uri, _, _) = uri
74 let complete_qid f lst (id, is_local, qs) =
75 let f path = C.list_rev_append (mk_qid f lst id) path ~tail:qs in
76 let rec skip f = function
77 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
78 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
81 if is_local then f lst.path else skip f (lst.path, qs)
83 let relax_qid f lst (_, id, path) =
85 | _ :: tl -> C.list_rev (mk_qid f lst id) tl
90 let relax_opt_qid f lst = function
92 | Some qid -> let f qid = f (Some qid) in relax_qid f lst qid
94 let resolve_gref err f lst qid =
95 try let a, cnt = UH.find henv (uri_of_qid qid) in f qid a cnt
96 with Not_found -> err qid
98 let resolve_gref_relaxed f lst qid =
99 (* this is not tail recursive *)
100 let rec err qid = relax_qid (resolve_gref err f lst) lst qid in
101 resolve_gref err f lst qid
103 let get_cnt err f lst = function
104 | None -> f empty_cnt
105 | Some qid as node ->
106 try let cnt = UH.find hcnt (uri_of_qid qid) in f cnt
107 with Not_found -> err node
109 let get_cnt_relaxed f lst =
110 (* this is not tail recursive *)
111 let rec err node = relax_opt_qid (get_cnt err f lst) lst node in
112 get_cnt err f lst lst.node
114 let push_abst f a w lenv =
115 let bw = D.Abst (false, N.infinity, w) in
116 D.push_bind f a bw lenv
118 let add_proj at e t = match e with
120 | D.EBind (D.ESort, a, b) -> D.TBind (E.compose a at, b, t)
122 IFDEF MANAGER OR OBJS THEN
123 D.TProj (at, D.set_attrs C.start at e, t)
128 (* this is not tail recursive in the GRef branch *)
129 let rec xlate_term f st lst y lenv = function
131 let h = if s then 0 else 1 in
132 let a = E.node_attrs ~main:(h, 0) () in
136 let at = E.compose av at in
137 f at (D.TAppl (at, !G.extended, vv, tt))
139 let f av vv = xlate_term (f av vv) st lst y lenv t in
140 xlate_term f st lst false lenv v
141 | A.Abst (name, w, t) ->
143 let aw = attrs_for_abst name aw in
145 let at = E.compose aw at in
147 if !G.cc then match y, snd at.E.n_main with
150 | _ , 1 -> N.unknown st
155 let b = D.Abst (false, l, ww) in
156 f at (D.TBind (at, b, tt))
158 let f lenv = xlate_term f st lst y lenv t in
159 push_abst f aw ww lenv
161 xlate_term f st lst true lenv w
162 | A.GRef (name, args) ->
163 let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
164 let map2 f arg args =
165 let f av v = f (D.EAppl (args, E.shift av, !G.extended, v)) in
166 xlate_term f st lst false lenv arg
169 let gref = D.TGRef (a, uri_of_qid qid) in
170 if cnt = D.ESort then f a gref else
172 | D.EAppl (D.ESort, av, x, v) ->
173 let a = E.compose av a in
174 f a (D.TAppl (a, x, v, gref))
176 IFDEF MANAGER OR OBJS THEN
177 f a (D.TProj (a, D.set_attrs C.start a args, gref))
179 f a (D.TProj (a, args, gref))
182 let f args = C.list_fold_right f map2 args D.ESort in
183 D.sub_list_strict (D.fold_names f map1 args) cnt args
185 let g qid = resolve_gref_relaxed g lst qid in
186 let err () = complete_qid g lst name in
187 D.resolve_lref err (mk_lref f) (id_of_name name) lenv
189 let xlate_entity err f st lst = function
190 | A.Section (Some (_, name)) ->
191 err {lst with path = name :: lst.path; nodes = lst.node :: lst.nodes}
193 begin match lst.path, lst.nodes with
194 | _ :: ptl, nhd :: ntl ->
195 err {lst with path = ptl; node = nhd; nodes = ntl}
199 err {lst with node = None}
200 | A.Context (Some name) ->
201 let f name = err {lst with node = Some name} in
202 complete_qid f lst name
203 | A.Block (name, w) ->
207 UH.add hcnt (uri_of_qid qid) (add_abst cnt name aw ww);
208 err {lst with node = Some qid}
210 xlate_term f st lst true cnt w
212 get_cnt_relaxed f lst
214 complete_qid f lst (name, true, [])
215 | A.Decl (name, w) ->
219 let a = attrs_for_decl aw in
220 UH.add henv (uri_of_qid qid) (a, lenv);
221 let t = add_proj aw lenv ww in
223 print_newline (); CrgOutput.pp_term print_string t;
225 let na = {aw with E.n_apix = lst.line} in
226 let entity = E.empty_root, na, uri_of_qid qid, E.Abst t in
227 G.set_current_trace lst.line;
228 f {lst with line = succ lst.line} entity
230 xlate_term f st lst true lenv w
232 complete_qid f lst (name, true, [])
234 let f = if !G.infinity then f else D.set_layer f N.one in
235 get_cnt_relaxed f lst
236 | A.Def (name, w, trans, v) ->
241 UH.add henv (uri_of_qid qid) (av, lenv);
242 let t = add_proj av lenv (D.TCast (av, ww, vv)) in
244 print_newline (); CrgOutput.pp_term print_string t;
246 let na = {av with E.n_apix = lst.line} in
247 let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
248 let entity = ra, na, uri_of_qid qid, E.Abbr t in
249 G.set_current_trace lst.line;
250 f {lst with line = succ lst.line} entity
252 xlate_term f st lst false lenv v
254 xlate_term f st lst true lenv w
256 complete_qid f lst (name, true, [])
258 get_cnt_relaxed f lst
260 (* Interface functions ******************************************************)
262 let initial_status () =
263 UH.clear henv; UH.clear hcnt; {
264 path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
267 let refresh_status lst = initial_status ()
269 let crg_of_aut = xlate_entity