]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/dual_rg/drgAut.ml
- common/entity: new format for kernel entities
[helm.git] / helm / software / lambda-delta / dual_rg / drgAut.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 A = Aut
17 module D = Drg
18
19 (* qualified identifier: uri, name, qualifiers *)
20 type qid = D.uri * D.id * D.id list
21
22 type context = Y.attrs * D.term list
23
24 type environment = context H.t
25
26 type context_node = qid option (* context node: None = root *)
27
28 type 'a status = {
29    henv: environment;        (* optimized global environment *)
30    path: D.id list;          (* current section path *) 
31    hcnt: environment;        (* optimized context *)   
32    node: context_node;       (* current context node *)
33    nodes: context_node list; (* context node list *)
34    line: int;                (* line number *)
35    mk_uri:'a Y.uri_generator (* uri generator *) 
36 }
37
38 type resolver = Local of int
39               | Global of context
40
41 let henv_size, hcnt_size = 7000, 4300 (* hash tables initial sizes *)
42
43 (* Internal functions *******************************************************)
44
45 let initial_status mk_uri = {
46    path = []; node = None; nodes = []; line = 1; mk_uri = mk_uri;
47    henv = H.create henv_size; hcnt = H.create hcnt_size
48 }
49
50 let empty_cnt = [], []
51
52 let add_abst (a, ws) id w = 
53    Y.Name (id, true) :: a, w :: ws 
54
55 let lenv_of_cnt (a, ws) = 
56    D.push C.start D.empty_lenv a (D.Abst ws)
57
58 let mk_lref f i j k = f (D.LRef ([Y.Apix k], i, j))
59
60 let id_of_name (id, _, _) = id
61
62 let mk_qid f st id path =
63    let str = String.concat "/" path in
64    let str = Filename.concat str id in 
65    let f str = f (U.uri_of_string str, id, path) in
66    st.mk_uri f str
67    
68 let uri_of_qid (uri, _, _) = uri
69
70 let complete_qid f st (id, is_local, qs) =
71    let f path = C.list_rev_append (mk_qid f st id) path ~tail:qs in
72    let rec skip f = function
73       | phd :: ptl, qshd :: _ when phd = qshd -> f ptl
74       | _ :: ptl, _ :: _                      -> skip f (ptl, qs)
75       | _                                     -> f []
76    in
77    if is_local then f st.path else skip f (st.path, qs)
78
79 let relax_qid f st (_, id, path) =
80    let f = function
81       | _ :: tl -> C.list_rev (mk_qid f st id) tl
82       | []      -> assert false
83    in
84    C.list_rev f path
85
86 let relax_opt_qid f st = function
87    | None     -> f None
88    | Some qid -> let f qid = f (Some qid) in relax_qid f st qid
89
90 let resolve_gref err f st qid =
91    try let cnt = H.find st.henv (uri_of_qid qid) in f qid cnt
92    with Not_found -> err () 
93
94 let resolve_gref_relaxed f st qid =
95    let rec err () = relax_qid (resolve_gref err f st) st qid in
96    resolve_gref err f st qid
97
98 let get_cnt err f st = function
99    | None              -> f empty_cnt
100    | Some qid as node ->
101       try let cnt = H.find st.hcnt (uri_of_qid qid) in f cnt
102       with Not_found -> err node
103
104 let get_cnt_relaxed f st =
105    let rec err node = relax_opt_qid (get_cnt err f st) st node in
106    get_cnt err f st st.node
107
108 let rec xlate_term f st lenv = function
109    | A.Sort s            -> 
110       let f h = f (D.Sort ([], h)) in
111       if s then f 0 else f 1
112    | A.Appl (v, t)       ->
113       let f vv tt = f (D.Appl ([], [vv], tt)) in
114       let f vv = xlate_term (f vv) st lenv t in
115       xlate_term f st lenv v
116    | A.Abst (name, w, t) ->
117       let f ww = 
118          let a, b = [Y.Name (name, true)], (D.Abst [ww]) in
119          let f tt = f (D.Bind (a, b, tt)) in
120          let f lenv = xlate_term f st lenv t in
121          D.push f lenv a b
122       in
123       xlate_term f st lenv w
124    | A.GRef (name, args) ->
125       let g qid (a, _) =
126          let map1 f = xlate_term f st lenv in       
127          let map2 f = function
128             | Y.Name (id, _) -> D.resolve_lref Cps.err (mk_lref f) id lenv
129             | _              -> assert false
130          in
131          let f tail = 
132             let f args = f (D.Appl ([], args, D.GRef ([], uri_of_qid qid))) in
133             let f a = C.list_rev_map_append f map2 a ~tail in
134             C.list_sub_strict f a args
135          in   
136          C.list_map f map1 args
137       in
138       let g qid = resolve_gref_relaxed g st qid in
139       let err () = complete_qid g st name in
140       D.resolve_lref err (mk_lref f) (id_of_name name) lenv
141
142 let xlate_entity err f st = function
143    | A.Section (Some (_, name))     ->
144       err {st with path = name :: st.path; nodes = st.node :: st.nodes}
145    | A.Section None            ->
146       begin match st.path, st.nodes with
147          | _ :: ptl, nhd :: ntl -> 
148             err {st with path = ptl; node = nhd; nodes = ntl}
149          | _                    -> assert false
150       end
151    | A.Context None            ->
152       err {st with node = None}
153    | A.Context (Some name)     ->
154       let f name = err {st with node = Some name} in
155       complete_qid f st name 
156    | A.Block (name, w)         ->
157       let f qid = 
158          let f cnt =
159             let lenv = lenv_of_cnt cnt in
160             let f ww = 
161                H.add st.hcnt (uri_of_qid qid) (add_abst cnt name ww);
162                err {st with node = Some qid}
163             in
164             xlate_term f st lenv w
165          in
166          get_cnt_relaxed f st
167       in
168       complete_qid f st (name, true, [])
169    | A.Decl (name, w)          ->
170       let f cnt =
171          let a, ws = cnt in
172          let lenv = lenv_of_cnt cnt in
173          let f qid = 
174             let f ww =
175                H.add st.henv (uri_of_qid qid) cnt;            
176                let b = Y.Abst (D.Bind (a, D.Abst ws, ww)) in
177                let entity = [Y.Mark st.line], uri_of_qid qid, b in
178                f {st with line = succ st.line} entity
179             in
180             xlate_term f st lenv w
181          in
182          complete_qid f st (name, true, [])
183       in
184       get_cnt_relaxed f st
185    | A.Def (name, w, trans, v) ->
186       let f cnt = 
187          let a, ws = cnt in
188          let lenv = lenv_of_cnt cnt in
189          let f qid = 
190             let f ww vv = 
191                H.add st.henv (uri_of_qid qid) cnt;
192                let b = Y.Abbr (D.Bind (a, D.Abst ws, D.Cast ([], ww, vv))) in
193                let a =
194                   if trans then [Y.Mark st.line] else [Y.Mark st.line; Y.Priv]
195                in
196                let entity = a, uri_of_qid qid, b in
197                f {st with line = succ st.line} entity
198             in
199             let f ww = xlate_term (f ww) st lenv v in
200             xlate_term f st lenv w
201          in
202          complete_qid f st (name, true, [])
203       in
204       get_cnt_relaxed f st
205
206 (* Interface functions ******************************************************)
207
208 let initial_status mk_uri =
209    initial_status mk_uri
210
211 let drg_of_aut = xlate_entity