]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/metaAut.ml
- lambda-delta: some speed up (not very much :) actually)
[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    genv: M.environment;      (* global environment *)
37    henv: environment;        (* optimized global environment *)
38    path: M.id list;          (* current section path *) 
39    hcnt: environment;        (* optimized context *)   
40    node: context_node;       (* current context node *)
41    nodes: context_node list; (* context node list *)
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 let initial_status size = {
51    genv = []; path = []; node = None; nodes = []; explicit = true;
52    henv = H.create size; hcnt = H.create size
53 }
54
55 let complete_qid f st (id, is_local, qs) =
56    let f qs = f (id, qs) in
57    let f path = Cps.list_rev_append f path ~tail:qs in
58    let rec skip f = function
59       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
60       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
61       | _                                     -> f []
62    in
63    if is_local then f st.path else skip f (st.path, qs)
64
65 let relax_qid f (id, path) =
66    let f path = f (id, path) in
67    let f = function
68       | _ :: tl -> Cps.list_rev f tl
69       | []      -> assert false
70    in
71    Cps.list_rev f path
72
73 let relax_opt_qid f = function
74    | None     -> f None
75    | Some qid -> let f qid = f (Some qid) in relax_qid f qid
76
77 let resolve_gref f st local lenv gref =
78   let rec get_local f i = function
79      | []                                      -> f None
80      | (name, _) :: _ when fst name = fst gref -> f (Some i)
81      | _ :: tl                                 -> get_local f (succ i) tl
82   in
83   let get_global f =
84      try 
85         let args = H.find st.henv gref in f (Some args)
86      with Not_found -> f None
87   in
88   let g = function
89       | Some args -> f gref (Some (Global args))
90       | None      -> f gref None
91   in
92   let f = function
93       | Some i -> f gref (Some (Local i))
94       | None   -> get_global g
95   in
96   if local then get_local f 0 lenv else f None
97
98 let resolve_gref_relaxed f st lenv gref =
99    let rec g gref = function
100       | None          -> relax_qid (resolve_gref g st false lenv) gref
101       | Some resolved -> f gref resolved
102    in
103    resolve_gref g st true lenv gref
104
105 let get_pars f st = function
106    | None              -> f [] None
107    | Some name as node ->
108       try let pars = H.find st.hcnt name in f pars None
109       with Not_found -> f [] (Some node)
110
111 let get_pars_relaxed f st =
112    let rec g pars = function
113       | None      -> f pars 
114       | Some node -> relax_opt_qid (get_pars g st) node
115    in
116    get_pars g st st.node
117
118 let rec xlate_term f st lenv = function
119    | A.Sort sort         -> f (M.Sort sort)
120    | A.Appl (v, t)       ->
121       let f vv tt = f (M.Appl (vv, tt)) in
122       let f vv = xlate_term (f vv) st lenv t in
123       xlate_term f st lenv v
124    | A.Abst (name, w, t) ->
125       let add name w lenv = 
126          let f name = (name, w) :: lenv in
127          complete_qid f st (name, true, []) 
128       in
129       let f ww tt = f (M.Abst (name, ww, tt)) in
130       let f ww = xlate_term (f ww) st (add name ww lenv) t in
131       xlate_term f st lenv w
132    | A.GRef (name, args) ->
133       let f name = function
134          | Local i     -> f (M.LRef i)
135          | Global defs -> 
136             let map1 f = xlate_term f st lenv in
137             let map2 f (name, _) = f (M.GRef (name, [])) in
138             let f tail =           
139                let f args = f (M.GRef (name, args)) in
140                let f defs = Cps.list_rev_map_append f map2 defs ~tail in
141                Cps.list_sub_strict f defs args
142             in
143             Cps.list_map f map1 args
144       in
145       let f name = resolve_gref_relaxed f st lenv name in
146       complete_qid f st name
147
148 let xlate_item f st = function
149    | A.Section (Some name)     ->
150       f {st with path = name :: st.path; nodes = st.node :: st.nodes}
151    | A.Section None            ->
152       begin match st.path, st.nodes with
153          | _ :: ptl, nhd :: ntl -> 
154             f {st with path = ptl; node = nhd; nodes = ntl}
155          | _                    -> assert false
156       end
157    | A.Context None            ->
158       f {st with node = None}
159    | A.Context (Some name)     ->
160       let f name = f {st with node = Some name} in
161       complete_qid f st name
162    | A.Block (name, w)         ->
163       let f name = 
164          let f pars =
165             let f ww = 
166                H.add st.hcnt name ((name, ww) :: pars);
167                f {st with node = Some name}
168             in
169             xlate_term f st pars w
170          in
171          get_pars_relaxed f st
172       in
173       complete_qid f st (name, true, [])
174    | A.Decl (name, w)          ->
175       let f pars = 
176          let f name = 
177             let f ww =
178                let entry = (pars, name, ww, None) in
179                H.add st.henv name pars;
180                f {st with genv = entry :: st.genv}
181             in
182             xlate_term f st pars w
183          in
184          complete_qid f st (name, true, [])
185       in
186       get_pars_relaxed f st
187    | A.Def (name, w, trans, v) ->
188       let f pars = 
189          let f name = 
190             let f ww vv = 
191                let entry = (pars, name, ww, Some (trans, vv)) in
192                H.add st.henv name pars;
193                f {st with genv = entry :: st.genv}
194             in
195             let f ww = xlate_term (f ww) st pars v in
196             xlate_term f st pars w
197          in
198          complete_qid f st (name, true, [])
199       in
200       get_pars_relaxed f st
201
202 let meta_of_aut f book =
203    let f st = f st.genv in
204    Cps.list_fold_left f xlate_item (initial_status hsize) book