]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgOutput.ml
dc697bff424769f61b30522edbd1e13538fc0427
[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 (a, b, t) -> 
87       let f c e = count_term f c e t in
88       let f c = B.push (f c) e a 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 (* context/term pretty printing *********************************************)
134
135 let id frm a =
136    let err () = assert false in
137    let f n = function 
138       | true  -> F.fprintf frm "%s" n
139       | false -> F.fprintf frm "^%s" n
140    in      
141    B.name err f a
142
143 let rec pp_term c frm = function
144    | B.Sort (_, h)           -> 
145       let f = function 
146          | Some s -> F.fprintf frm "@[%s@]" s
147          | None   -> F.fprintf frm "@[*%u@]" h
148       in
149       H.get_sort f h 
150    | B.LRef (_, i)           -> 
151       let err i = F.fprintf frm "@[#%u@]" i in
152       let f _ a _ = F.fprintf frm "@[%a@]" id a in
153       if !O.indexes then err i else B.get err f c i
154    | B.GRef (_, s)           -> F.fprintf frm "@[$%s@]" (U.string_of_uri s)
155    | B.Cast (_, u, t)        ->
156       F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
157    | B.Appl (_, v, t)        ->
158       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
159    | B.Bind (a, B.Abst w, t) ->
160       let f cc =
161          F.fprintf frm "@[[%a:%a].%a@]" id a (pp_term c) w (pp_term cc) t
162       in
163       B.push f c a (B.Abst w)
164    | B.Bind (a, B.Abbr v, t) ->
165       let f cc = 
166          F.fprintf frm "@[[%a=%a].%a@]" id a (pp_term c) v (pp_term cc) t
167       in
168       B.push f c a (B.Abbr v)
169    | B.Bind (a, B.Void, t)   ->
170       let f cc = F.fprintf frm "@[[%a].%a@]" id a (pp_term cc) t in
171       B.push f c a B.Void
172
173 let pp_context frm c =
174    let pp_entry f c a = function
175       | B.Abst w -> 
176          F.fprintf frm "@,@[%a : %a@]" id a (pp_term c) w; f ()
177       | B.Abbr v -> 
178          F.fprintf frm "@,@[%a = %a@]" id a (pp_term c) v; f ()
179       | B.Void   -> 
180          F.fprintf frm "@,%a" id a; f ()
181    in
182    B.rev_iter C.start pp_entry c
183
184 let specs = {
185    L.pp_term = pp_term; L.pp_context = pp_context
186 }
187
188 (* term xml printing ********************************************************)
189
190 let id frm a =
191    let f s = function
192       | true  -> F.fprintf frm " name=%S" s
193       | false -> F.fprintf frm " name=%S" ("^" ^ s)
194    in      
195    B.name C.start f a
196
197 let rec exp_term frm = function
198    | B.Sort (a, l)    ->
199       F.fprintf frm "<Sort position=%S%a/>" (string_of_int l) id a
200    | B.LRef (a, i)    ->
201       F.fprintf frm "<LRef position=%S%a/>" (string_of_int i) id a
202    | B.GRef (a, u)    ->
203       F.fprintf frm "<GRef uri=%S%a/>" (U.string_of_uri u) id a
204    | B.Cast (a, w, t) ->
205       F.fprintf frm "<Cast%a/>%a%a" id a exp_boxed w exp_term t 
206    | B.Appl (a, v, t) ->
207       F.fprintf frm "<Appl%a/>%a%a" id a exp_boxed v exp_term t 
208    | B.Bind (a, b, t) ->
209       F.fprintf frm "%a%a" (exp_bind a) b exp_term t
210
211 and exp_boxed frm t =
212    F.fprintf frm "@,@[<v3>   %a@]@," exp_term t
213
214 and exp_bind a frm = function
215    | B.Abst w ->
216       F.fprintf frm "<Abst%a/>%a" id a exp_boxed w
217    | B.Abbr v ->
218       F.fprintf frm "<Abbr%a/>%a" id a exp_boxed v
219    | B.Void   ->
220       F.fprintf frm "<Void%a/>" id a
221
222 let exp_obj frm = function
223    | _, uri, B.Abst w -> 
224       let str = U.string_of_uri uri in
225       F.fprintf frm "<ABST uri=%S/>@,%a" str exp_term w
226    | _, uri, B.Abbr v -> 
227       let str = U.string_of_uri uri in
228       F.fprintf frm "<ABBR uri=%S/>@,%a" str exp_term v
229    | _, uri, B.Void   -> 
230       let str = U.string_of_uri uri in
231       F.fprintf frm "<VOID uri=%S/>" str
232
233 let export_obj frm obj =
234    F.fprintf frm "@,@[<v3>   %a@]@," exp_obj obj