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_______________________________________________________________ *)
22 (* qualified identifier: uri, name, qualifiers *)
23 type qid = D.uri * D.id * D.id list
25 type context_node = qid option (* context node: None = root *)
28 path: D.id list; (* current section path *)
29 node: context_node; (* current context node *)
30 nodes: context_node list; (* context node list *)
31 line: int; (* line number *)
32 mk_uri: G.uri_generator; (* uri generator *)
33 lenv: N.status; (* level environment *)
36 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
38 let henv = K.create henv_size (* optimized global environment *)
40 let hcnt = K.create hcnt_size (* optimized context *)
42 (* Internal functions *******************************************************)
44 let empty_cnt = D.ESort
46 let add_abst cnt id w =
47 D.EBind (cnt, E.node_attrs ~name:(id, true) (), D.Abst (N.two, w))
49 let mk_lref f a i = f a.E.n_degr (D.TLRef (E.empty_node, i))
51 let id_of_name (id, _, _) = id
53 let mk_qid f st id path =
54 let str = String.concat "/" path in
55 let str = Filename.concat str id in
56 let str = st.mk_uri str in
57 f (U.uri_of_string str, id, path)
59 let uri_of_qid (uri, _, _) = uri
61 let complete_qid f st (id, is_local, qs) =
62 let f path = C.list_rev_append (mk_qid f st id) path ~tail:qs in
63 let rec skip f = function
64 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
65 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
68 if is_local then f st.path else skip f (st.path, qs)
70 let relax_qid f st (_, id, path) =
72 | _ :: tl -> C.list_rev (mk_qid f st id) tl
77 let relax_opt_qid f st = function
79 | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
81 let resolve_gref err f st qid =
82 try let a, cnt = K.find henv (uri_of_qid qid) in f qid a cnt
83 with Not_found -> err qid
85 let resolve_gref_relaxed f st qid =
86 (* this is not tail recursive *)
87 let rec err qid = relax_qid (resolve_gref err f st) st qid in
88 resolve_gref err f st qid
90 let get_cnt err f st = function
93 try let cnt = K.find hcnt (uri_of_qid qid) in f cnt
94 with Not_found -> err node
96 let get_cnt_relaxed f st =
97 (* this is not tail recursive *)
98 let rec err node = relax_opt_qid (get_cnt err f st) st node in
99 get_cnt err f st st.node
101 let push_abst f a w lenv =
102 let bw = D.Abst (N.infinite, w) in
103 D.push_bind f a bw lenv
105 let add_proj e t = match e with
107 | D.EBind (D.ESort, a, b) -> D.TBind (a, b, t)
108 | _ -> D.TProj (E.empty_node, e, t)
110 (* this is not tail recursive in the GRef branch *)
111 let rec xlate_term f st lenv = function
113 let f h = f 0 (D.TSort (E.empty_node, h)) in
114 if s then f 0 else f 1
116 let f vv d tt = f d (D.TAppl (E.empty_node, vv, tt)) in
117 let f _ vv = xlate_term (f vv) st lenv t in
118 xlate_term f st lenv v
119 | A.Abst (name, w, t) ->
121 let a = E.node_attrs ~name:(name, true) () in
125 | 1 -> N.unknown st.lenv (J.new_mark ())
129 let b = D.Abst (l, ww) in
130 f d (D.TBind (a, b, tt))
132 let f lenv = xlate_term f st lenv t in
133 push_abst f {a with E.n_degr = succ d} ww lenv
135 xlate_term f st lenv w
136 | A.GRef (name, args) ->
137 let map1 args (id, _) = A.GRef ((id, true, []), []) :: args in
138 let map2 f arg args =
139 let f _ arg = f (D.EAppl (args, E.empty_node, arg)) in
140 xlate_term f st lenv arg
143 let gref = D.TGRef (a, uri_of_qid qid) in
144 if cnt = D.ESort then f a.E.n_degr gref else
146 | D.EAppl (D.ESort, a, v) -> f a.E.n_degr (D.TAppl (a, v, gref))
147 | args -> f a.E.n_degr (D.TProj (E.empty_node, args, gref))
149 let f args = C.list_fold_right f map2 args D.ESort in
150 D.sub_list_strict (D.fold_names f map1 args) cnt args
152 let g qid = resolve_gref_relaxed g st qid in
153 let err () = complete_qid g st name in
154 D.resolve_lref err (mk_lref f) (id_of_name name) lenv
156 let xlate_entity err f st = function
157 | A.Section (Some (_, name)) ->
158 err {st with path = name :: st.path; nodes = st.node :: st.nodes}
160 begin match st.path, st.nodes with
161 | _ :: ptl, nhd :: ntl ->
162 err {st with path = ptl; node = nhd; nodes = ntl}
166 err {st with node = None}
167 | A.Context (Some name) ->
168 let f name = err {st with node = Some name} in
169 complete_qid f st name
170 | A.Block (name, w) ->
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 cnt w
181 complete_qid f st (name, true, [])
182 | A.Decl (name, w) ->
186 let a = E.node_attrs ~apix:st.line ~degr:(succ d) () in
187 K.add henv (uri_of_qid qid) (a, lenv);
188 let t = add_proj lenv ww in
190 print_newline (); CrgOutput.pp_term print_string t;
193 let entity = E.empty_root, a, uri_of_qid qid, b in
194 f {st with line = succ st.line} entity
196 xlate_term f st lenv w
198 complete_qid f st (name, true, [])
201 | A.Def (name, w, trans, v) ->
206 let na = E.node_attrs ~apix:st.line ~degr:d () in
207 K.add henv (uri_of_qid qid) (na, lenv);
208 let t = add_proj lenv (D.TCast (E.empty_node, ww, vv)) in
210 print_newline (); CrgOutput.pp_term print_string t;
213 let ra = if trans then E.empty_root else E.root_attrs ~meta:[E.Private] () in
214 let entity = ra, na, uri_of_qid qid, b in
215 f {st with line = succ st.line} entity
217 xlate_term f st lenv v
219 xlate_term f st lenv w
221 complete_qid f st (name, true, [])
225 (* Interface functions ******************************************************)
227 let initial_status () =
228 K.clear henv; K.clear hcnt; {
229 path = []; node = None; nodes = []; line = 1; mk_uri = G.get_mk_uri ();
230 lenv = N.initial_status ();
233 let refresh_status st = {st with
234 mk_uri = G.get_mk_uri ()
237 let crg_of_aut = xlate_entity