]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/metaAut.ml
we tranlate an Automath book in an itermediate format where:
[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 M = Meta
27 module A = Aut
28
29 type context_node = M.qid option (* context node: None = root *)
30
31 type context = (M.qid * M.term * context_node) list (* context: son, parent *)
32
33 type status = {
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? *)
40 }
41
42 type resolver = Local of int
43               | Global of M.pars
44
45 let initial_status = {
46    genv = []; path = []; cnt = []; node = None; nodes = []; explicit = true
47 }
48
49 let find f cnt qid =
50    let rec aux f = function
51       | (name, w, node) :: tl when name = qid -> f tl (Some (w, node))
52       | _ :: tl                               -> aux f tl
53       | []                                    -> f cnt None
54    in
55    aux f cnt
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 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 rec get_global f = function
86      | []                                       -> f None
87      | (args, name, _, _) :: _ when name = gref -> f (Some args)
88      | _ :: tl                                  -> get_global f tl
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 st.genv
97   in
98   get_local f 0 lenv
99
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
104    in
105    resolve_gref g st lenv gref
106
107 let get_pars f st pars node =
108    let rec aux f cnt pars = function
109       | None              -> 
110          let f pars = f pars None in
111          Cps.list_rev f pars
112       | Some name as node ->
113          let f cnt = function
114             | Some (w, node) -> aux f cnt ((name, w) :: pars) node
115             | None           -> f pars (Some node)
116          in
117          find f cnt name
118    in
119    aux f st.cnt pars node
120
121 let get_pars_relaxed f st =
122    let rec g pars = function
123       | None      -> f pars 
124       | Some node -> relax_opt_qid (get_pars g st pars) node
125    in
126    get_pars g st [] st.node
127
128 let rec xlate_term f st lenv = function
129    | A.Sort sort         -> f (M.Sort sort)
130    | A.Appl (v, t)       ->
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, []) 
138       in
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)
145          | Global defs -> 
146             let map1 f = xlate_term f st lenv in
147             let map2 f (name, _) = f (M.GRef (name, [])) in
148             let f tail =           
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
152             in
153             Cps.list_map f map1 args
154       in
155       let f name = resolve_gref_relaxed f st lenv name in
156       complete_qid f st name
157
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}
161    | A.Section None            ->
162       begin match st.path, st.nodes with
163          | _ :: ptl, nhd :: ntl -> 
164             f {st with path = ptl; node = nhd; nodes = ntl}
165          | _                    -> assert false
166       end
167    | A.Context None            ->
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)         ->
173       let f name = 
174          let f ww = 
175             let st = {st with cnt = (name, ww, st.node) :: st.cnt} in
176             f {st with node = Some name}
177          in
178          let f pars = xlate_term f st pars w in
179          get_pars_relaxed f st
180       in
181       complete_qid f st (name, true, [])
182    | A.Decl (name, w)          ->
183       let f pars = 
184          let f name = 
185             let f ww =
186                let entry = (pars, name, ww, None) in
187                f {st with genv = entry :: st.genv}
188             in
189             xlate_term f st pars w
190          in
191          complete_qid f st (name, true, [])
192       in
193       get_pars_relaxed f st
194    | A.Def (name, w, trans, v) ->
195       let f pars = 
196          let f name = 
197             let f ww vv = 
198                let entry = (pars, name, ww, Some (trans, vv)) in
199                f {st with genv = entry :: st.genv}
200             in
201             let f ww = xlate_term (f ww) st pars v in
202             xlate_term f st pars w
203          in
204          complete_qid f st (name, true, [])
205       in
206       get_pars_relaxed f st
207
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