]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/toplevel/metaAut.ml
- common/entity: new format for kernel entities
[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 U = NUri
13 module H = U.UriHash
14 module C = Cps
15 module Y = Entity
16 module M = Meta
17 module A = Aut
18
19 (* qualified identifier: uri, name, qualifiers *)
20 type qid = M.uri * M.id * M.id list
21
22 type environment = M.pars H.t
23
24 type context_node = qid option (* context node: None = root *)
25
26 type status = {
27    henv: environment;        (* optimized global environment *)
28    path: M.id list;          (* current section path *) 
29    hcnt: environment;        (* optimized context *)   
30    node: context_node;       (* current context node *)
31    nodes: context_node list; (* context node list *)
32    line: int;                (* line number *)
33    cover: string             (* initial segment of URI hierarchy *) 
34 }
35
36 type resolver = Local of int
37               | Global of M.pars
38
39 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
40
41 (* Internal functions *******************************************************)
42
43 let initial_status cover = {
44    path = []; node = None; nodes = []; line = 1; cover = cover;
45    henv = H.create henv_size; hcnt = H.create hcnt_size
46 }
47
48 let id_of_name (id, _, _) = id
49
50 let mk_qid st id path =
51    let uripath = if st.cover = "" then path else st.cover :: path in
52    let str = String.concat "/" uripath in
53    let str = Filename.concat str id in 
54    U.uri_of_string ("ld:/" ^ str ^ ".ld"), id, path
55
56 let uri_of_qid (uri, _, _) = uri
57
58 let complete_qid f st (id, is_local, qs) =
59    let f qs = f (mk_qid st id qs) in
60    let f path = C.list_rev_append f path ~tail:qs in
61    let rec skip f = function
62       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
63       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
64       | _                                     -> f []
65    in
66    if is_local then f st.path else skip f (st.path, qs)
67
68 let relax_qid f st (_, id, path) =
69    let f path = f (mk_qid st id path) in
70    let f = function
71       | _ :: tl -> C.list_rev f tl
72       | []      -> assert false
73    in
74    C.list_rev f path
75
76 let relax_opt_qid f st = function
77    | None     -> f None
78    | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
79
80 let resolve_lref f st l lenv id =
81    let rec aux f i = function
82      | []                            -> f None
83      | (name, _) :: _ when name = id -> f (Some (M.LRef (l, i)))
84      | _ :: tl                       -> aux f (succ i) tl
85    in
86    aux f 0 lenv
87
88 let resolve_lref_strict f st l lenv id =
89    let f = function
90       | Some t -> f t
91       | None   -> assert false
92    in
93    resolve_lref f st l lenv id
94
95 let resolve_gref f st qid =
96    try let args = H.find st.henv (uri_of_qid qid) in f qid (Some args)
97    with Not_found -> f qid None
98
99 let resolve_gref_relaxed f st qid =
100    let rec g qid = function
101       | None      -> relax_qid (resolve_gref g st) st qid
102       | Some args -> f qid args
103    in
104    resolve_gref g st qid
105
106 let get_pars f st = function
107    | None              -> f [] None
108    | Some qid as node ->
109       try let pars = H.find st.hcnt (uri_of_qid qid) 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) 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         -> 
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                H.add st.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                H.add st.henv (uri_of_qid qid) pars;
181                let a = [Y.Mark st.line] in
182                let entry = pars, ww, None in
183                let entity = a, uri_of_qid qid, Y.Abst 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                H.add st.henv (uri_of_qid qid) pars;
196                let a = Y.Mark st.line :: if trans then [] else [Y.Priv] in 
197                let entry = pars, ww, Some vv in
198                let entity = a, uri_of_qid qid, Y.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 ?(cover="") () =
211    initial_status cover
212
213 let meta_of_aut = xlate_entity