--- /dev/null
+(*
+ ||M|| This file is part of HELM, an Hypertextual, Electronic
+ ||A|| Library of Mathematics, developed at the Computer Science
+ ||T|| Department, University of Bologna, Italy.
+ ||I||
+ ||T|| HELM is free software; you can redistribute it and/or
+ ||A|| modify it under the terms of the GNU General Public License
+ \ / version 2 or (at your option) any later version.
+ \ / This software is distributed as is, NO WARRANTY.
+ V_______________________________________________________________ *)
+
+module U = NUri
+module H = U.UriHash
+module C = Cps
+module O = Options
+module Y = Entity
+module A = Aut
+module D = Crg
+
+(* qualified identifier: uri, name, qualifiers *)
+type qid = D.uri * D.id * D.id list
+
+type context = Y.attrs * D.term list
+
+type context_node = qid option (* context node: None = root *)
+
+type status = {
+ path: D.id list; (* current section path *)
+ node: context_node; (* current context node *)
+ nodes: context_node list; (* context node list *)
+ line: int; (* line number *)
+ mk_uri:O.uri_generator (* uri generator *)
+}
+
+type resolver = Local of int
+ | Global of context
+
+let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
+
+let henv = H.create henv_size (* optimized global environment *)
+
+let hcnt = H.create hcnt_size (* optimized context *)
+
+(* Internal functions *******************************************************)
+
+let empty_cnt = [], []
+
+let add_abst (a, ws) id w =
+ Y.Name (id, true) :: a, w :: ws
+
+let lenv_of_cnt (a, ws) =
+ D.push_bind C.start D.empty_lenv a (D.Abst ws)
+
+let mk_lref f i j k = f (D.TLRef ([Y.Apix k], i, j))
+
+let id_of_name (id, _, _) = id
+
+let mk_qid f st id path =
+ let str = String.concat "/" path in
+ let str = Filename.concat str id in
+ let str = st.mk_uri str in
+ f (U.uri_of_string str, id, path)
+
+let uri_of_qid (uri, _, _) = uri
+
+let complete_qid f st (id, is_local, qs) =
+ let f path = C.list_rev_append (mk_qid f st id) path ~tail:qs in
+ let rec skip f = function
+ | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
+ | _ :: ptl, _ :: _ -> skip f (ptl, qs)
+ | _ -> f []
+ in
+ if is_local then f st.path else skip f (st.path, qs)
+
+let relax_qid f st (_, id, path) =
+ let f = function
+ | _ :: tl -> C.list_rev (mk_qid f st id) tl
+ | [] -> assert false
+ in
+ C.list_rev f path
+
+let relax_opt_qid f st = function
+ | None -> f None
+ | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
+
+let resolve_gref err f st qid =
+ try let cnt = H.find henv (uri_of_qid qid) in f qid cnt
+ with Not_found -> err qid
+
+let resolve_gref_relaxed f st qid =
+(* this is not tail recursive *)
+ let rec err qid = relax_qid (resolve_gref err f st) st qid in
+ resolve_gref err f st qid
+
+let get_cnt err f st = function
+ | None -> f empty_cnt
+ | Some qid as node ->
+ try let cnt = H.find hcnt (uri_of_qid qid) in f cnt
+ with Not_found -> err node
+
+let get_cnt_relaxed f st =
+(* this is not tail recursive *)
+ let rec err node = relax_opt_qid (get_cnt err f st) st node in
+ get_cnt err f st st.node
+
+(* this is not tail recursive in the GRef branch *)
+let rec xlate_term f st lenv = function
+ | A.Sort s ->
+ let f h = f (D.TSort ([], h)) in
+ if s then f 0 else f 1
+ | A.Appl (v, t) ->
+ let f vv tt = f (D.TAppl ([], [vv], tt)) in
+ let f vv = xlate_term (f vv) st lenv t in
+ xlate_term f st lenv v
+ | A.Abst (name, w, t) ->
+ let f ww =
+ let a, b = [Y.Name (name, true)], (D.Abst [ww]) in
+ let f tt = f (D.TBind (a, b, tt)) in
+ let f lenv = xlate_term f st lenv t in
+ D.push_bind f lenv a b
+ in
+ xlate_term f st lenv w
+ | A.GRef (name, args) ->
+ let map1 f = function
+ | Y.Name (id, _) -> f (A.GRef ((id, true, []), []))
+ | _ -> C.err ()
+ in
+ let map2 f = xlate_term f st lenv in
+ let g qid (a, _) =
+ let gref = D.TGRef ([], uri_of_qid qid) in
+ match args, a with
+ | [], [] -> f gref
+ | _ ->
+ let f args = f (D.TAppl ([], args, gref)) in
+ let f args = f (List.rev_map (map2 C.start) args) in
+ let f a = C.list_rev_map_append f map1 a ~tail:args in
+ C.list_sub_strict f a args
+ in
+ let g qid = resolve_gref_relaxed g st qid in
+ let err () = complete_qid g st name in
+ D.resolve_lref err (mk_lref f) (id_of_name name) lenv
+
+let xlate_entity err f st = function
+ | A.Section (Some (_, name)) ->
+ err {st with path = name :: st.path; nodes = st.node :: st.nodes}
+ | A.Section None ->
+ begin match st.path, st.nodes with
+ | _ :: ptl, nhd :: ntl ->
+ err {st with path = ptl; node = nhd; nodes = ntl}
+ | _ -> assert false
+ end
+ | A.Context None ->
+ err {st with node = None}
+ | A.Context (Some name) ->
+ let f name = err {st with node = Some name} in
+ complete_qid f st name
+ | A.Block (name, w) ->
+ let f qid =
+ let f cnt =
+ let lenv = lenv_of_cnt cnt in
+ let ww = xlate_term C.start st lenv w in
+ H.add hcnt (uri_of_qid qid) (add_abst cnt name ww);
+ err {st with node = Some qid}
+ in
+ get_cnt_relaxed f st
+ in
+ complete_qid f st (name, true, [])
+ | A.Decl (name, w) ->
+ let f cnt =
+ let a, ws = cnt in
+ let lenv = lenv_of_cnt cnt in
+ let f qid =
+ let ww = xlate_term C.start st lenv w in
+ H.add henv (uri_of_qid qid) cnt;
+ let t = match ws with
+ | [] -> ww
+ | _ -> D.TBind (a, D.Abst ws, ww)
+ in
+(*
+ print_newline (); CrgOutput.pp_term print_string t;
+*)
+ let b = Y.Abst t in
+ let entity = [Y.Mark st.line], uri_of_qid qid, b in
+ f {st with line = succ st.line} entity
+ in
+ complete_qid f st (name, true, [])
+ in
+ get_cnt_relaxed f st
+ | A.Def (name, w, trans, v) ->
+ let f cnt =
+ let a, ws = cnt in
+ let lenv = lenv_of_cnt cnt in
+ let f qid =
+ let ww = xlate_term C.start st lenv w in
+ let vv = xlate_term C.start st lenv v in
+ H.add henv (uri_of_qid qid) cnt;
+ let t = match ws with
+ | [] -> D.TCast ([], ww, vv)
+ | _ -> D.TBind (a, D.Abst ws, D.TCast ([], ww, vv))
+ in
+(*
+ print_newline (); CrgOutput.pp_term print_string t;
+*)
+ let b = Y.Abbr t in
+ let a = Y.Mark st.line :: if trans then [] else [Y.Priv] in
+ let entity = a, uri_of_qid qid, b in
+ f {st with line = succ st.line} entity
+ in
+ complete_qid f st (name, true, [])
+ in
+ get_cnt_relaxed f st
+
+(* Interface functions ******************************************************)
+
+let initial_status () =
+ H.clear henv; H.clear hcnt; {
+ path = []; node = None; nodes = []; line = 1; mk_uri = O.get_mk_uri ()
+}
+
+let refresh_status st = {st with
+ mk_uri = O.get_mk_uri ()
+}
+
+let crg_of_aut = xlate_entity