]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/metaAut.ml
031e74cf2a731b17b7c5dc113c1e0b67b6266d01
[helm.git] / helm / software / lambda-delta / toplevel / metaAut.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 module H = Hashtbl
27
28 module M = Meta
29 module A = Aut
30
31 type environment = (M.qid, M.pars) H.t
32
33 type context_node = M.qid option (* context node: None = root *)
34
35 type status = {
36    henv: environment;        (* optimized global environment *)
37    path: M.id list;          (* current section path *) 
38    hcnt: environment;        (* optimized context *)   
39    node: context_node;       (* current context node *)
40    nodes: context_node list; (* context node list *)
41    line: int;                (* line number *)
42    explicit: bool            (* need explicit context root? *)
43 }
44
45 type resolver = Local of int
46               | Global of M.pars
47
48 let hsize = 11 (* hash tables initial size *)
49
50 (* Internal functions *******************************************************)
51
52 let initial_status size = {
53    path = []; node = None; nodes = []; line = 1; explicit = true;
54    henv = H.create size; hcnt = H.create size
55 }
56
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)
63       | _                                     -> f []
64    in
65    if is_local then f st.path else skip f (st.path, qs)
66
67 let relax_qid f (id, path) =
68    let f path = f (id, path) in
69    let f = function
70       | _ :: tl -> Cps.list_rev f tl
71       | []      -> assert false
72    in
73    Cps.list_rev f path
74
75 let relax_opt_qid f = function
76    | None     -> f None
77    | Some qid -> let f qid = f (Some qid) in relax_qid f qid
78
79 let resolve_gref f st local lenv gref =
80   let rec get_local f i = function
81      | []                                      -> f None
82      | (name, _) :: _ when fst name = fst gref -> f (Some i)
83      | _ :: tl                                 -> get_local f (succ i) tl
84   in
85   let get_global f =
86      try 
87         let args = H.find st.henv gref in f (Some args)
88      with Not_found -> f None
89   in
90   let g = function
91       | Some args -> f gref (Some (Global args))
92       | None      -> f gref None
93   in
94   let f = function
95       | Some i -> f gref (Some (Local i))
96       | None   -> get_global g
97   in
98   if local then get_local f 0 lenv else f None
99
100 let resolve_gref_relaxed f st lenv gref =
101    let rec g gref = function
102       | None          -> relax_qid (resolve_gref g st false lenv) gref
103       | Some resolved -> f gref resolved
104    in
105    resolve_gref g st true lenv gref
106
107 let get_pars f st = function
108    | None              -> f [] None
109    | Some name as node ->
110       try let pars = H.find st.hcnt name in f pars None
111       with Not_found -> f [] (Some node)
112
113 let get_pars_relaxed f st =
114    let rec g pars = function
115       | None      -> f pars 
116       | Some node -> relax_opt_qid (get_pars g st) node
117    in
118    get_pars g st st.node
119
120 let rec xlate_term f st lenv = function
121    | A.Sort sort         -> f (M.Sort sort)
122    | A.Appl (v, t)       ->
123       let f vv tt = f (M.Appl (vv, tt)) in
124       let f vv = xlate_term (f vv) st lenv t in
125       xlate_term f st lenv v
126    | A.Abst (name, w, t) ->
127       let add name w lenv = 
128          let f name = (name, w) :: lenv in
129          complete_qid f st (name, true, []) 
130       in
131       let f ww tt = f (M.Abst (name, ww, tt)) in
132       let f ww = xlate_term (f ww) st (add name ww lenv) t in
133       xlate_term f st lenv w
134    | A.GRef (name, args) ->
135       let f name = function
136          | Local i     -> f (M.LRef i)
137          | Global defs -> 
138             let l = List.length lenv in
139             let map1 f = xlate_term f st lenv in
140             let map2 f (name, _) = f (M.GRef (l, name, [])) in
141             let f tail =           
142                let f args = f (M.GRef (l, name, args)) in
143                let f defs = Cps.list_rev_map_append f map2 defs ~tail in
144                Cps.list_sub_strict f defs args
145             in
146             Cps.list_map f map1 args
147       in
148       let f name = resolve_gref_relaxed f st lenv name in
149       complete_qid f st name
150
151 let xlate_item f st = function
152    | A.Section (Some name)     ->
153       f {st with path = name :: st.path; nodes = st.node :: st.nodes} None
154    | A.Section None            ->
155       begin match st.path, st.nodes with
156          | _ :: ptl, nhd :: ntl -> 
157             f {st with path = ptl; node = nhd; nodes = ntl} None
158          | _                    -> assert false
159       end
160    | A.Context None            ->
161       f {st with node = None} None
162    | A.Context (Some name)     ->
163       let f name = f {st with node = Some name} None in
164       complete_qid f st name 
165    | A.Block (name, w)         ->
166       let f name = 
167          let f pars =
168             let f ww = 
169                H.add st.hcnt name ((name, ww) :: pars);
170                f {st with node = Some name} None
171             in
172             xlate_term f st pars w
173          in
174          get_pars_relaxed f st
175       in
176       complete_qid f st (name, true, [])
177    | A.Decl (name, w)          ->
178       let f pars = 
179          let f name = 
180             let f ww =
181                let entry = (st.line, pars, name, ww, None) in
182                H.add st.henv name pars;
183                f {st with line = succ st.line} (Some entry)
184             in
185             xlate_term f st pars w
186          in
187          complete_qid f st (name, true, [])
188       in
189       get_pars_relaxed f st
190    | A.Def (name, w, trans, v) ->
191       let f pars = 
192          let f name = 
193             let f ww vv = 
194                let entry = (st.line, pars, name, ww, Some (trans, vv)) in
195                H.add st.henv name pars;
196                f {st with line = succ st.line} (Some entry)
197             in
198             let f ww = xlate_term (f ww) st pars v in
199             xlate_term f st pars w
200          in
201          complete_qid f st (name, true, [])
202       in
203       get_pars_relaxed f st
204
205 (* Interface functions ******************************************************)
206
207 let initial_status = initial_status hsize
208
209 let meta_of_aut = xlate_item