1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
29 type context_node = M.qid option (* context node: None = root *)
31 type context = (M.qid * M.term * context_node) list (* context: son, parent *)
34 genv: M.environment; (* global environment *)
35 path: M.id list; (* current section path *)
36 cnt: context; (* context *)
37 node: context_node; (* current context node *)
38 nodes: context_node list; (* context node list *)
39 explicit: bool (* need explicit context root? *)
42 type resolver = Local of int
45 let initial_status = {
46 genv = []; path = []; cnt = []; node = None; nodes = []; explicit = true
50 let rec aux f = function
51 | (name, w, node) :: tl when name = qid -> f tl (Some (w, node))
57 let complete_qid f st (id, is_local, qs) =
58 let f qs = f (id, qs) in
59 let f path = Cps.list_rev_append f path ~tail:qs in
60 let rec skip f = function
61 | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
62 | _ :: ptl, _ :: _ -> skip f (ptl, qs)
65 if is_local then f st.path else skip f (st.path, qs)
67 let relax_qid f (id, path) =
68 let f path = f (id, path) in
70 | _ :: tl -> Cps.list_rev f tl
75 let relax_opt_qid f = function
77 | Some qid -> let f qid = f (Some qid) in relax_qid f qid
79 let resolve_gref f st lenv gref =
80 let rec get_local f i = function
82 | (name, _) :: _ when fst name = fst gref -> f (Some i)
83 | _ :: tl -> get_local f (succ i) tl
85 let rec get_global f = function
87 | (args, name, _, _) :: _ when name = gref -> f (Some args)
88 | _ :: tl -> get_global f tl
91 | Some args -> f gref (Some (Global args))
95 | Some i -> f gref (Some (Local i))
96 | None -> get_global g st.genv
100 let resolve_gref_relaxed f st lenv gref =
101 let rec g gref = function
102 | None -> relax_qid (resolve_gref g st lenv) gref
103 | Some resolved -> f gref resolved
105 resolve_gref g st lenv gref
107 let get_pars f st pars node =
108 let rec aux f cnt pars = function
110 let f pars = f pars None in
112 | Some name as node ->
114 | Some (w, node) -> aux f cnt ((name, w) :: pars) node
115 | None -> f pars (Some node)
119 aux f st.cnt pars node
121 let get_pars_relaxed f st =
122 let rec g pars = function
124 | Some node -> relax_opt_qid (get_pars g st pars) node
126 get_pars g st [] st.node
128 let rec xlate_term f st lenv = function
129 | A.Sort sort -> f (M.Sort sort)
131 let f vv tt = f (M.Appl (vv, tt)) in
132 let f vv = xlate_term (f vv) st lenv t in
133 xlate_term f st lenv v
134 | A.Abst (name, w, t) ->
135 let add name w lenv =
136 let f name = (name, w) :: lenv in
137 complete_qid f st (name, true, [])
139 let f ww tt = f (M.Abst (name, ww, tt)) in
140 let f ww = xlate_term (f ww) st (add name ww lenv) t in
141 xlate_term f st lenv w
142 | A.GRef (name, args) ->
143 let f name = function
144 | Local i -> f (M.LRef i)
146 let map1 f = xlate_term f st lenv in
147 let map2 f (name, _) = f (M.GRef (name, [])) in
149 let f args = f (M.GRef (name, args)) in
150 let f defs = Cps.list_rev_map_append f map2 defs ~tail in
151 Cps.list_sub_strict f defs args
153 Cps.list_map f map1 args
155 let f name = resolve_gref_relaxed f st lenv name in
156 complete_qid f st name
158 let xlate_item f st = function
159 | A.Section (Some name) ->
160 f {st with path = name :: st.path; nodes = st.node :: st.nodes}
162 begin match st.path, st.nodes with
163 | _ :: ptl, nhd :: ntl ->
164 f {st with path = ptl; node = nhd; nodes = ntl}
168 f {st with node = None}
169 | A.Context (Some name) ->
170 let f name = f {st with node = Some name} in
171 complete_qid f st name
172 | A.Block (name, w) ->
175 let st = {st with cnt = (name, ww, st.node) :: st.cnt} in
176 f {st with node = Some name}
178 let f pars = xlate_term f st pars w in
179 get_pars_relaxed f st
181 complete_qid f st (name, true, [])
182 | A.Decl (name, w) ->
186 let entry = (pars, name, ww, None) in
187 f {st with genv = entry :: st.genv}
189 xlate_term f st pars w
191 complete_qid f st (name, true, [])
193 get_pars_relaxed f st
194 | A.Def (name, w, trans, v) ->
198 let entry = (pars, name, ww, Some (trans, vv)) in
199 f {st with genv = entry :: st.genv}
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 let meta_of_aut f book =
209 let f st = f st.genv in
210 Cps.list_fold_left f xlate_item initial_status book