]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/metaAut.ml
metaAut.xlate_item started
[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 module M = Meta
28 module A = Aut
29
30 type context_node = M.qid option (* context node: None = root *)
31
32 type context = (M.qid, M.term * context_node) H.t (* context: son, parent *)
33
34 type status = {
35    genv: M.environment; (* global environment *)
36    path: M.id list;     (* current section path *) 
37    cnt: context;        (* context *)
38    node: context_node;  (* current context node *)
39    explicit: bool       (* need explicit context root? *)
40 }
41
42 type resolver = Local of int
43               | Global of M.pars
44               | Unresolved
45
46 let initial_status = {
47    genv = []; path = []; cnt = H.create 11; node = None; explicit = true
48 }
49
50 let complete_qid f st (id, is_local, qs) =
51    let f qs = f (id, qs) in
52    if is_local then Cps.list_rev_append f st.path ~tail:qs else f qs
53
54 let resolve_gref f st lenv gref =
55   let rec get_local f i = function
56      | []                              -> f None
57      | (name, _) :: _ when name = gref -> f (Some i)
58      | _ :: tl                         -> get_local f (succ i) tl
59   in
60   let rec get_global f = function
61      | []                                       -> f None
62      | (args, name, _, _) :: _ when name = gref -> f (Some args)
63      | _ :: tl                                  -> get_global f tl
64   in
65   let g = function
66       | Some args -> f (Global args)
67       | None      -> f Unresolved
68   in
69   let f = function
70       | Some i -> f (Local i)
71       | None   -> get_global g st.genv
72   in
73   get_local f 0 lenv
74
75 let rec xlate_term f st lenv = function
76    | A.Sort sort         -> f (M.Sort sort)
77    | A.Appl (v, t)       ->
78       let f vv tt = f (M.Appl (vv, tt)) in
79       let f vv = xlate_term (f vv) st lenv t in
80       xlate_term f st lenv v
81    | A.Abst (name, w, t) ->
82       let add name w lenv = 
83          let f name = (name, w) :: lenv in
84          complete_qid f st (name, true, []) 
85       in
86       let f ww tt = f (M.Abst (name, ww, tt)) in
87       let f ww = xlate_term (f ww) st (add name ww lenv) t in
88       xlate_term f st lenv w
89    | A.GRef (name, args) ->
90       let f name = function
91          | Local i     -> f (M.LRef i)
92          | Global defs -> 
93             let map1 f = xlate_term f st lenv in
94             let map2 f (name, _) = f (M.GRef (name, [])) in
95             let f tail =           
96                let f args = f (M.GRef (name, args)) in
97                let f defs = Cps.list_rev_map_append f map2 defs ~tail in
98                Cps.list_sub_strict f defs args
99             in
100             Cps.list_map f map1 args
101          | Unresolved  -> assert false
102       in
103       let f name = resolve_gref (f name) st lenv name in
104       complete_qid f st name
105
106 let xlate_item f st = function
107    | A.Section (Some name)     ->
108       f {st with path = name :: st.path}
109    | A.Section None            ->
110       begin match st.path with
111          | []      -> assert false
112          | _ :: tl -> f {st with path = tl}
113       end
114    | A.Context None            ->
115       f {st with node = None}
116    | A.Context (Some name)     ->
117       let f name = f {st with node = Some name} in
118       complete_qid f st name
119    | A.Block (name, w)         ->
120       let f name ww = H.add st.cnt name (ww, st.node); f st in
121       let f name = xlate_term (f name) st [] w in
122       complete_qid f st (name, true, [])   
123    | A.Decl (name, w)          -> f st 
124    | A.Def (name, w, trans, v) -> f st
125
126 let meta_of_aut f book =
127    let f st = f st.genv in
128    Cps.list_fold_left f xlate_item initial_status book