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 = E.attrs * D.term list
26 type context_node = qid option (* context node: None = root *)
29 path: D.id list; (* current section path *)
30 node: context_node; (* current context node *)
31 nodes: context_node list; (* context node list *)
32 line: int; (* line number *)
33 mk_uri: G.uri_generator; (* uri generator *)
36 type resolver = Local of int
39 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
41 let henv = K.create henv_size (* optimized global environment *)
43 let hcnt = K.create hcnt_size (* optimized context *)
45 (* Internal functions *******************************************************)
47 let empty_cnt = [], []
49 let add_abst (a, ws) id w =
50 E.Name (id, true) :: a, w :: ws
52 let mk_lref f i j k = f (D.TLRef ([E.Apix k], i, j))
54 let id_of_name (id, _, _) = id
56 let mk_qid f st id path =
57 let str = String.concat "/" path in
58 let str = Filename.concat str id in
59 let str = st.mk_uri str in
60 f (U.uri_of_string str, id, path)
62 let uri_of_qid (uri, _, _) = uri
64 let complete_qid f st (id, is_local, qs) =
65 let f path = C.list_rev_append (mk_qid f st id) path ~tail:qs in
66 let rec skip f = function
67 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
68 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
71 if is_local then f st.path else skip f (st.path, qs)
73 let relax_qid f st (_, id, path) =
75 | _ :: tl -> C.list_rev (mk_qid f st id) tl
80 let relax_opt_qid f st = function
82 | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
84 let resolve_gref err f st qid =
85 try let cnt = K.find henv (uri_of_qid qid) in f qid cnt
86 with Not_found -> err qid
88 let resolve_gref_relaxed f st qid =
89 (* this is not tail recursive *)
90 let rec err qid = relax_qid (resolve_gref err f st) st qid in
91 resolve_gref err f st qid
93 let get_cnt err f st = function
96 try let cnt = K.find hcnt (uri_of_qid qid) in f cnt
97 with Not_found -> err node
99 let get_cnt_relaxed f st =
100 (* this is not tail recursive *)
101 let rec err node = relax_opt_qid (get_cnt err f st) st node in
102 get_cnt err f st st.node
104 (****************************************************************************)
106 let push_abst f lenv a w =
107 let bw = D.Abst (N.infinite, [w]) in
108 let f lenv = f lenv in
109 D.push_bind f lenv a bw
111 let lenv_of_cnt (a, ws) =
112 D.push_bind C.start D.empty_lenv a (D.Abst (N.infinite, ws))
114 (* this is not tail recursive in the GRef branch *)
115 let rec xlate_term f st lenv = function
117 let f h = f (D.TSort ([], h)) in
118 if s then f 0 else f 1
120 let f vv tt = f (D.TAppl ([], [vv], tt)) in
121 let f vv = xlate_term (f vv) st lenv t in
122 xlate_term f st lenv v
123 | A.Abst (name, w, t) ->
125 let a = [E.Name (name, true)] in
127 let b = D.Abst (N.infinite, [ww]) in
128 f (D.TBind (a, b, tt))
130 let f lenv = xlate_term f st lenv t in
131 push_abst f lenv a ww
133 xlate_term f st lenv w
134 | A.GRef (name, args) ->
135 let map1 f = function
136 | E.Name (id, _) -> f (A.GRef ((id, true, []), []))
139 let map2 f t = xlate_term f st lenv t in
141 let gref age = D.TGRef ([age], uri_of_qid qid) in
143 | [], [age] -> f (gref age)
145 let f args = f (D.TAppl ([], args, gref age)) in
146 let f args = C.list_rev_map f map2 args in
147 let f a = C.list_rev_map_append f map1 a ~tail:args in
148 C.list_sub_strict f a args
151 let g qid = resolve_gref_relaxed g st qid in
152 let err () = complete_qid g st name in
153 D.resolve_lref err (mk_lref f) (id_of_name name) lenv
155 let xlate_entity err f st = function
156 | A.Section (Some (_, name)) ->
157 err {st with path = name :: st.path; nodes = st.node :: st.nodes}
159 begin match st.path, st.nodes with
160 | _ :: ptl, nhd :: ntl ->
161 err {st with path = ptl; node = nhd; nodes = ntl}
165 err {st with node = None}
166 | A.Context (Some name) ->
167 let f name = err {st with node = Some name} in
168 complete_qid f st name
169 | A.Block (name, w) ->
172 let lenv = lenv_of_cnt cnt in
174 K.add hcnt (uri_of_qid qid) (add_abst cnt name ww);
175 err {st with node = Some qid}
177 xlate_term f st lenv w
181 complete_qid f st (name, true, [])
182 | A.Decl (name, w) ->
185 let lenv = lenv_of_cnt cnt in
188 let age = E.Apix st.line in
189 K.add henv (uri_of_qid qid) (age :: a, ws);
190 let t = match ws with
192 | _ -> D.TBind (a, D.Abst (N.infinite, ws), ww)
195 print_newline (); CrgOutput.pp_term print_string t;
197 let b = E.Abst (N.infinite, t) in
198 let entity = [age], uri_of_qid qid, b in
199 f {st with line = succ st.line} entity
201 xlate_term f st lenv w
203 complete_qid f st (name, true, [])
206 | A.Def (name, w, trans, v) ->
209 let lenv = lenv_of_cnt cnt in
213 let age = E.Apix st.line in
214 K.add henv (uri_of_qid qid) (age :: a, ws);
215 let t = match ws with
216 | [] -> D.TCast ([], ww, vv)
217 | _ -> D.TBind (a, D.Abst (N.infinite, ws), D.TCast ([], ww, vv))
220 print_newline (); CrgOutput.pp_term print_string t;
223 let a = age :: if trans then [] else [E.Meta [E.Private]] in
224 let entity = a, uri_of_qid qid, b in
225 f {st with line = succ st.line} entity
227 xlate_term f st lenv v
229 xlate_term f st lenv w
231 complete_qid f st (name, true, [])
235 (* Interface functions ******************************************************)
237 let initial_status () =
238 K.clear henv; K.clear hcnt; {
239 path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ()
242 let refresh_status st = {st with
243 mk_uri = G.get_mk_uri ()
246 let crg_of_aut = xlate_entity