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