]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/toplevel/metaAut.ml
the old intermediate language (meta) is now obsolete
[helm.git] / helm / software / lambda-delta / src / 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 U = NUri
13 module K = U.UriHash
14 module C = Cps
15 module G = Options
16 module E = Entity
17 module N = Level
18 module M = Meta
19 module A = Aut
20
21 (* qualified identifier: uri, name, qualifiers *)
22 type qid = M.uri * M.id * M.id list
23
24 type context_node = qid option (* context node: None = root *)
25
26 type status = {
27    path: M.id list;          (* current section path *) 
28    node: context_node;       (* current context node *)
29    nodes: context_node list; (* context node list *)
30    line: int;                (* line number *)
31    cover: string             (* initial segment of URI hierarchy *) 
32 }
33
34 type resolver = Local of int
35               | Global of M.pars
36
37 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
38
39 let henv = K.create henv_size (* optimized global environment *)
40
41 let hcnt = K.create hcnt_size (* optimized context *) 
42
43 (* Internal functions *******************************************************)
44
45 let id_of_name (id, _, _) = id
46
47 let mk_qid st id path =
48    let uripath = if st.cover = "" then path else st.cover :: path in
49    let str = String.concat "/" uripath in
50    let str = Filename.concat str id in 
51    U.uri_of_string ("ld:/" ^ str ^ ".ld"), id, path
52
53 let uri_of_qid (uri, _, _) = uri
54
55 let complete_qid f st (id, is_local, qs) =
56    let f qs = f (mk_qid st id qs) in
57    let f path = C.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 st (_, id, path) =
66    let f path = f (mk_qid st id path) in
67    let f = function
68       | _ :: tl -> C.list_rev f tl
69       | []      -> assert false
70    in
71    C.list_rev f path
72
73 let relax_opt_qid f st = function
74    | None     -> f None
75    | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
76
77 let resolve_lref f st l lenv id =
78    let rec aux f i = function
79      | []                            -> f None
80      | (name, _) :: _ when name = id -> f (Some (M.LRef (l, i)))
81      | _ :: tl                       -> aux f (succ i) tl
82    in
83    aux f 0 lenv
84
85 let resolve_lref_strict f st l lenv id =
86    let f = function
87       | Some t -> f t
88       | None   -> assert false
89    in
90    resolve_lref f st l lenv id
91
92 let resolve_gref f st qid =
93    try let args = K.find henv (uri_of_qid qid) in f qid (Some args)
94    with Not_found -> f qid None
95
96 let resolve_gref_relaxed f st qid =
97 (* this is not tail recursive *)
98    let rec g qid = function
99       | None      -> relax_qid (resolve_gref g st) st qid
100       | Some args -> f qid args
101    in
102    resolve_gref g st qid
103
104 let get_pars f st = function
105    | None              -> f [] None
106    | Some qid as node ->
107       try let pars = K.find hcnt (uri_of_qid qid) in f pars None
108       with Not_found -> f [] (Some node)
109
110 let get_pars_relaxed f st =
111 (* this is not tail recursive *)
112    let rec g pars = function
113       | None      -> f pars 
114       | Some node -> relax_opt_qid (get_pars g st) st node
115    in
116    get_pars g st st.node
117
118 (* this is not tail recursive on the GRef branch *)
119 let rec xlate_term f st lenv = function
120    | A.Sort sort         -> 
121       f (M.Sort sort)
122    | A.Appl (v, t)       ->
123       let f vv tt = f (M.Appl (vv, tt)) in
124       let f vv = xlate_term (f vv) st lenv t in
125       xlate_term f st lenv v
126    | A.Abst (name, w, t) ->
127       let add name w lenv = (name, w) :: lenv in
128       let f ww tt = f (M.Abst (name, ww, tt)) in
129       let f ww = xlate_term (f ww) st (add name ww lenv) t in
130       xlate_term f st lenv w
131    | A.GRef (name, args) ->
132       let l = List.length lenv in
133       let g qid defs =
134          let map1 f = xlate_term f st lenv in       
135          let map2 f (id, _) = resolve_lref_strict f st l lenv id in
136          let f tail = 
137             let f args = f (M.GRef (l, uri_of_qid qid, args)) in
138             let f defs = C.list_rev_map_append f map2 defs ~tail in
139             C.list_sub_strict f defs args
140          in   
141          C.list_map f map1 args
142       in
143       let g qid = resolve_gref_relaxed g st qid in
144       let f = function
145          | Some t -> f t
146          | None   -> complete_qid g st name
147       in
148       resolve_lref f st l lenv (id_of_name name)
149
150 let xlate_entity err f st = function
151    | A.Section (Some (_, name))     ->
152       err {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             err {st with path = ptl; node = nhd; nodes = ntl}
157          | _                    -> assert false
158       end
159    | A.Context None            ->
160       err {st with node = None}
161    | A.Context (Some name)     ->
162       let f name = err {st with node = Some name} in
163       complete_qid f st name 
164    | A.Block (name, w)         ->
165       let f qid = 
166          let f pars =
167             let f ww = 
168                K.add hcnt (uri_of_qid qid) ((name, ww) :: pars);
169                err {st with node = Some qid}
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 qid = 
179             let f ww =
180                K.add henv (uri_of_qid qid) pars;
181                let a = [E.Mark st.line] in
182                let entry = pars, ww, None in
183                let entity = a, uri_of_qid qid, E.Abst (N.infinite, entry) in
184                f {st with line = succ st.line} entity
185             in
186             xlate_term f st pars w
187          in
188          complete_qid f st (name, true, [])
189       in
190       get_pars_relaxed f st
191    | A.Def (name, w, trans, v) ->
192       let f pars = 
193          let f qid = 
194             let f ww vv = 
195                K.add henv (uri_of_qid qid) pars;
196                let a = E.Mark st.line :: if trans then [] else [E.Priv] in 
197                let entry = pars, ww, Some vv in
198                let entity = a, uri_of_qid qid, E.Abbr entry in
199                f {st with line = succ st.line} entity
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 (* Interface functions ******************************************************)
209
210 let initial_status () =
211    K.clear henv; K.clear hcnt; {
212    path = []; node = None; nodes = []; line = 1; cover = !G.cover
213 }
214
215 let refresh_status st = {st with
216   cover = !G.cover
217 }  
218
219 let meta_of_aut = xlate_entity