]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/metaAut.ml
59801dac7833fc98efe739589ef10bd23a9ebab5
[helm.git] / helm / software / lambda-delta / toplevel / metaAut.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.              
9      \ /   This software is distributed as is, NO WARRANTY.              
10       V_______________________________________________________________ *)
11
12 module H = Hashtbl
13 module U = NUri
14 module M = Meta
15 module A = Aut
16
17 type qid = M.id * M.id list (* qualified identifier: name, qualifiers *)
18
19 type environment = (qid, M.pars) H.t
20
21 type context_node = qid option (* context node: None = root *)
22
23 type status = {
24    henv: environment;        (* optimized global environment *)
25    path: M.id list;          (* current section path *) 
26    hcnt: environment;        (* optimized context *)   
27    node: context_node;       (* current context node *)
28    nodes: context_node list; (* context node list *)
29    line: int;                (* line number *)
30    explicit: bool            (* need explicit context root? *)
31 }
32
33 type resolver = Local of int
34               | Global of M.pars
35
36 let hsize = 7000 (* hash tables initial size *)
37
38 (* Internal functions *******************************************************)
39
40 let initial_status size = {
41    path = []; node = None; nodes = []; line = 1; explicit = true;
42    henv = H.create size; hcnt = H.create size
43 }
44
45 let id_of_name (id, _, _) = id
46
47 let uri_of_qid (id, path) =
48    let path = String.concat "/" path in
49    let str = Filename.concat path id in
50    U.uri_of_string ("ld:/" ^ str)
51
52 let complete_qid f st (id, is_local, qs) =
53    let f qs = f (id, qs) in
54    let f path = Cps.list_rev_append f path ~tail:qs in
55    let rec skip f = function
56       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
57       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
58       | _                                     -> f []
59    in
60    if is_local then f st.path else skip f (st.path, qs)
61
62 let relax_qid f (id, path) =
63    let f path = f (id, path) in
64    let f = function
65       | _ :: tl -> Cps.list_rev f tl
66       | []      -> assert false
67    in
68    Cps.list_rev f path
69
70 let relax_opt_qid f = function
71    | None     -> f None
72    | Some qid -> let f qid = f (Some qid) in relax_qid f qid
73
74 let resolve_lref f st l lenv id =
75    let rec aux f i = function
76      | []                            -> f None
77      | (name, _) :: _ when name = id -> f (Some (M.LRef (l, i)))
78      | _ :: tl                       -> aux f (succ i) tl
79    in
80    aux f 0 lenv
81
82 let resolve_lref_strict f st l lenv id =
83    let f = function
84       | Some t -> f t
85       | None   -> assert false
86    in
87    resolve_lref f st l lenv id
88
89 let resolve_gref f st qid =
90    try let args = H.find st.henv qid in f qid (Some args)
91    with Not_found -> f qid None
92
93 let resolve_gref_relaxed f st qid =
94    let rec g qid = function
95       | None      -> relax_qid (resolve_gref g st) qid
96       | Some args -> f qid args
97    in
98    resolve_gref g st qid
99
100 let get_pars f st = function
101    | None              -> f [] None
102    | Some name as node ->
103       try let pars = H.find st.hcnt name in f pars None
104       with Not_found -> f [] (Some node)
105
106 let get_pars_relaxed f st =
107    let rec g pars = function
108       | None      -> f pars 
109       | Some node -> relax_opt_qid (get_pars g st) node
110    in
111    get_pars g st st.node
112
113 let rec xlate_term f st lenv = function
114    | A.Sort sort         -> 
115       f (M.Sort sort)
116    | A.Appl (v, t)       ->
117       let f vv tt = f (M.Appl (vv, tt)) in
118       let f vv = xlate_term (f vv) st lenv t in
119       xlate_term f st lenv v
120    | A.Abst (name, w, t) ->
121       let add name w lenv = (name, w) :: lenv in
122       let f ww tt = f (M.Abst (name, ww, tt)) in
123       let f ww = xlate_term (f ww) st (add name ww lenv) t in
124       xlate_term f st lenv w
125    | A.GRef (name, args) ->
126       let l = List.length lenv in
127       let g qid defs =
128          let map1 f = xlate_term f st lenv in       
129          let map2 f (id, _) = resolve_lref_strict f st l lenv id in
130          let f tail = 
131             let f args = f (M.GRef (l, uri_of_qid qid, args)) in
132             let f defs = Cps.list_rev_map_append f map2 defs ~tail in
133             Cps.list_sub_strict f defs args
134          in   
135          Cps.list_map f map1 args
136       in
137       let g qid = resolve_gref_relaxed g st qid in
138       let f = function
139          | Some t -> f t
140          | None   -> complete_qid g st name
141       in
142       resolve_lref f st l lenv (id_of_name name)
143
144 let xlate_item f st = function
145    | A.Section (Some name)     ->
146       f {st with path = name :: st.path; nodes = st.node :: st.nodes} None
147    | A.Section None            ->
148       begin match st.path, st.nodes with
149          | _ :: ptl, nhd :: ntl -> 
150             f {st with path = ptl; node = nhd; nodes = ntl} None
151          | _                    -> assert false
152       end
153    | A.Context None            ->
154       f {st with node = None} None
155    | A.Context (Some name)     ->
156       let f name = f {st with node = Some name} None in
157       complete_qid f st name 
158    | A.Block (name, w)         ->
159       let f qid = 
160          let f pars =
161             let f ww = 
162                H.add st.hcnt qid ((name, ww) :: pars);
163                f {st with node = Some qid} None
164             in
165             xlate_term f st pars w
166          in
167          get_pars_relaxed f st
168       in
169       complete_qid f st (name, true, [])
170    | A.Decl (name, w)          ->
171       let f pars = 
172          let f qid = 
173             let f ww =
174                let entry = (st.line, pars, uri_of_qid qid, ww, None) in
175                H.add st.henv qid pars;
176                f {st with line = succ st.line} (Some entry)
177             in
178             xlate_term f st pars w
179          in
180          complete_qid f st (name, true, [])
181       in
182       get_pars_relaxed f st
183    | A.Def (name, w, trans, v) ->
184       let f pars = 
185          let f qid = 
186             let f ww vv = 
187                let entry = (st.line, pars, uri_of_qid qid, ww, Some (trans, vv)) in
188                H.add st.henv qid pars;
189                f {st with line = succ st.line} (Some entry)
190             in
191             let f ww = xlate_term (f ww) st pars v in
192             xlate_term f st pars w
193          in
194          complete_qid f st (name, true, [])
195       in
196       get_pars_relaxed f st
197
198 (* Interface functions ******************************************************)
199
200 let initial_status = initial_status hsize
201
202 let meta_of_aut = xlate_item