]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgOutput.ml
basic_rg: bugfix in AST to allow attributes in global entries
[helm.git] / helm / software / lambda-delta / basic_rg / brgOutput.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 P = Printf
13 module F = Format
14 module C = Cps
15 module U = NUri
16 module L = Log
17 module H = Hierarchy
18 module O = Output
19 module B = Brg
20
21 (* nodes count **************************************************************)
22
23 type counters = {
24    eabsts: int;
25    eabbrs: int;
26    evoids: int;
27    tsorts: int;
28    tlrefs: int;
29    tgrefs: int;
30    tcasts: int;
31    tappls: int;
32    tabsts: int;
33    tabbrs: int;
34    tvoids: int;
35    uris  : B.uri list;
36    nodes : int;
37    xnodes: int
38 }
39
40 let initial_counters = {
41    eabsts = 0; eabbrs = 0; evoids = 0; 
42    tsorts = 0; tlrefs = 0; tgrefs = 0; tcasts = 0; tappls = 0;
43    tabsts = 0; tabbrs = 0; tvoids = 0;
44    uris = []; nodes = 0; xnodes = 0
45 }
46
47 let rec count_term_binder f c e = function
48    | B.Abst (_, w) ->
49       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
50       count_term f c e w
51    | B.Abbr (_, v) -> 
52       let c = {c with tabbrs = succ c.tabbrs; xnodes = succ c.xnodes} in
53       count_term f c e v
54    | B.Void _      ->
55       let c = {c with tvoids = succ c.tvoids; xnodes = succ c.xnodes} in   
56       f c
57
58 and count_term f c e = function
59    | B.Sort _         -> 
60       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
61    | B.LRef (_, i)    -> 
62       let err _ = f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes} in
63       let f _ = function
64          | B.Abst _ 
65          | B.Void _ ->
66             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
67          | B.Abbr _ ->
68             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
69       in         
70       B.get err f e i
71    | B.GRef (_, u)    -> 
72       let c =    
73          if Cps.list_mem ~eq:U.eq u c.uris
74          then {c with nodes = succ c.nodes}
75          else {c with xnodes = succ c.xnodes}
76       in
77       f {c with tgrefs = succ c.tgrefs}
78    | B.Cast (_, v, t) -> 
79       let c = {c with tcasts = succ c.tcasts} in
80       let f c = count_term f c e t in
81       count_term f c e v
82    | B.Appl (_, v, t) -> 
83       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
84       let f c = count_term f c e t in
85       count_term f c e v
86    | B.Bind (b, t) -> 
87       let f c e = count_term f c e t in
88       let f c = B.push (f c) e b in
89       count_term_binder f c e b
90
91 let count_obj f c = function
92    | (_, u, B.Abst (_, w)) -> 
93       let c = {c with
94          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
95       } in
96       count_term f c B.empty_context w
97    | (_, _, B.Abbr (_, v)) ->  
98       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
99       count_term f c B.empty_context v
100    | (_, u, B.Void _)      ->
101       let c = {c with 
102          evoids = succ c.evoids; nodes = succ c.nodes; uris = u :: c.uris
103       } in
104       f c
105
106 let count_item f c = function
107    | Some obj -> count_obj f c obj
108    | None     -> f c
109
110 let print_counters f c =
111    let terms =
112       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
113       c.tabbrs
114    in
115    let items = c.eabsts + c.eabbrs in
116    let nodes = c.nodes + c.xnodes in
117    L.warn (P.sprintf "  Kernel representation summary (basic_rg)");
118    L.warn (P.sprintf "    Total entry items:        %7u" items);
119    L.warn (P.sprintf "      Declaration items:      %7u" c.eabsts);
120    L.warn (P.sprintf "      Definition items:       %7u" c.eabbrs);
121    L.warn (P.sprintf "    Total term items:         %7u" terms);
122    L.warn (P.sprintf "      Sort items:             %7u" c.tsorts);
123    L.warn (P.sprintf "      Local reference items:  %7u" c.tlrefs);
124    L.warn (P.sprintf "      Global reference items: %7u" c.tgrefs);
125    L.warn (P.sprintf "      Explicit Cast items:    %7u" c.tcasts);
126    L.warn (P.sprintf "      Application items:      %7u" c.tappls);
127    L.warn (P.sprintf "      Abstraction items:      %7u" c.tabsts);
128    L.warn (P.sprintf "      Abbreviation items:     %7u" c.tabbrs);
129    L.warn (P.sprintf "    Global Int. Complexity:   %7u" c.nodes);
130    L.warn (P.sprintf "      + Abbreviation nodes:   %7u" nodes);
131    f ()
132
133 (* supplementary annotation *************************************************)
134
135 let attrs_of_binder f = function
136    | B.Abst (a, _) 
137    | B.Abbr (a, _)
138    | B.Void a      -> f a
139
140 let rec does_not_occur f n r = function
141    | B.Null           -> f true
142    | B.Cons (c, _, b) ->
143       let f n1 r1 =
144          if n1 = n && r1 = r then f false else does_not_occur f n r c
145       in
146       attrs_of_binder (B.name C.err f) b 
147
148 let rename f c a =
149    let rec aux f c n r =
150       let f = function
151          | true  -> f n r
152          | false -> aux f c (n ^ "'") r
153       in
154       does_not_occur f n r c
155    in
156    let f n0 r0 =
157       let f n r = if n = n0 && r = r0 then f a else f (B.Name (r, n) :: a) in
158       aux f c n0 r0 
159    in
160    B.name C.err f a
161
162 let rename_bind f c = function
163    | B.Abst (a, w) -> let f a = f (B.abst a w) in rename f c a
164    | B.Abbr (a, v) -> let f a = f (B.abbr a v) in rename f c a
165    | B.Void a      -> let f a = f (B.Void a) in rename f c a
166
167 (* context/term pretty printing *********************************************)
168
169 let id frm a =
170    let f n = function 
171       | true  -> F.fprintf frm "%s" n
172       | false -> F.fprintf frm "^%s" n
173    in      
174    B.name C.err f a
175
176 let rec pp_term c frm = function
177    | B.Sort (_, h)             -> 
178       let err () = F.fprintf frm "@[*%u@]" h in
179       let f s = F.fprintf frm "@[%s@]" s in
180       H.get_sort err f h 
181    | B.LRef (_, i)             -> 
182       let err i = F.fprintf frm "@[#%u@]" i in
183       let f _ = function
184          | B.Abst (a, _) 
185          | B.Abbr (a, _)
186          | B.Void a      -> F.fprintf frm "@[%a@]" id a
187       in
188       if !O.indexes then err i else B.get err f c i
189    | B.GRef (_, s)             ->
190       F.fprintf frm "@[$%s@]" (U.string_of_uri s)
191    | B.Cast (_, u, t)          ->
192       F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
193    | B.Appl (_, v, t)          ->
194       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
195    | B.Bind (B.Abst (a, w), t) ->
196       let f a cc =
197          F.fprintf frm "@[[%a:%a].%a@]" id a (pp_term c) w (pp_term cc) t
198       in
199       let f a = B.push (f a) c (B.abst a w) in
200       rename f c a
201    | B.Bind (B.Abbr (a, v), t) ->
202       let f a cc = 
203          F.fprintf frm "@[[%a=%a].%a@]" id a (pp_term c) v (pp_term cc) t
204       in
205       let f a = B.push (f a) c (B.abbr a v) in
206       rename f c a
207    | B.Bind (B.Void a, t)      ->
208       let f a cc = F.fprintf frm "@[[%a].%a@]" id a (pp_term cc) t in
209       let f a = B.push (f a) c (B.Void a) in
210       rename f c a
211
212 let pp_context frm c =
213    let pp_entry f c = function
214       | B.Abst (a, w) -> 
215          let f a = F.fprintf frm "@,@[%a : %a@]" id a (pp_term c) w; f () in
216          rename f c a
217       | B.Abbr (a, v) -> 
218          let f a = F.fprintf frm "@,@[%a = %a@]" id a (pp_term c) v; f () in
219          rename f c a
220       | B.Void a      -> 
221          let f a = F.fprintf frm "@,%a" id a; f () in
222          rename f c a
223    in
224    B.rev_iter C.start pp_entry c
225
226 let specs = {
227    L.pp_term = pp_term; L.pp_context = pp_context
228 }
229
230 (* term xml printing ********************************************************)
231
232 let id frm a =
233    let f s = function
234       | true  -> F.fprintf frm " name=%S" s
235       | false -> F.fprintf frm " name=%S" ("^" ^ s)
236    in      
237    B.name C.start f a
238
239 let rec exp_term c frm = function
240    | B.Sort (a, l)    ->
241       let a =
242          let err _ = a in
243          let f s = B.Name (true, s) :: a in
244          H.get_sort err f l
245       in
246       F.fprintf frm "<Sort position=%S%a/>" (string_of_int l) id a
247    | B.LRef (a, i)    ->
248       let a = 
249          let err _ = a in
250          let f n r = B.Name (r, n) :: a in
251          let f _ b = attrs_of_binder (B.name err f) b in
252          B.get err f c i
253       in
254       F.fprintf frm "<LRef position=%S%a/>" (string_of_int i) id a
255    | B.GRef (a, u)    ->
256       let a = B.Name (true, U.name_of_uri u) :: a in
257       F.fprintf frm "<GRef uri=%S%a/>" (U.string_of_uri u) id a
258    | B.Cast (a, w, t) ->
259       F.fprintf frm "<Cast%a>%a</Cast>@,%a" id a (exp_boxed c) w (exp_term c) t 
260    | B.Appl (a, v, t) ->
261       F.fprintf frm "<Appl%a>%a</Appl>@,%a" id a (exp_boxed c) v (exp_term c) t 
262    | B.Bind (b, t) ->
263       let f b cc = F.fprintf frm "%a@,%a" (exp_bind c) b (exp_term cc) t in 
264       let f b = B.push (f b) c b in
265       rename_bind f c b
266
267 and exp_boxed c frm t =
268    F.fprintf frm "@,@[<v3>   %a@]@," (exp_term c) t
269
270 and exp_bind c frm = function
271    | B.Abst (a, w) ->
272       F.fprintf frm "<Abst%a>%a</Abst>" id a (exp_boxed c) w
273    | B.Abbr (a, v) ->
274       F.fprintf frm "<Abbr%a/>%a</Abbr>" id a (exp_boxed c) v
275    | B.Void a      ->
276       F.fprintf frm "<Void%a/>" id a
277
278 let exp_obj c frm = function
279    | _, u, B.Abst (a, w) -> 
280       let str = U.string_of_uri u in
281       let a = B.Name (true, U.name_of_uri u) :: a in
282       F.fprintf frm "<ABST uri=%S%a>%a</ABST>" str id a (exp_boxed c) w
283    | _, u, B.Abbr (a, v) -> 
284       let str = U.string_of_uri u in
285       let a = B.Name (true, U.name_of_uri u) :: a in
286       F.fprintf frm "<ABBR uri=%S%a>%a</ABBR>" str id a (exp_boxed c) v
287    | _, u, B.Void a      -> 
288       let str = U.string_of_uri u in
289       let a = B.Name (true, U.name_of_uri u) :: a in
290       F.fprintf frm "<VOID uri=%S%a/>" str id a
291
292 let export_obj frm obj =
293    F.fprintf frm "@,@[<v3>   %a@]@," (exp_obj B.empty_context) obj