]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgOutput.ml
1aa3f9db7d4ecc0ddda91c04138d7bb9db7c140c
[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 U = NUri
15 module L = Log
16 module H = Hierarchy
17 module O = Output
18 module CL = CommonLibrary
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  : U.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 f _ = function
63          | None
64          | Some (_, B.Abst _) ->
65             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
66          | Some (_, B.Abbr _)
67          | Some (_, B.Void)   ->
68             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
69       in         
70       B.get 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    | (_, _, B.Void)   ->
101       let c = {c with evoids = succ c.evoids; xnodes = succ c.xnodes} in
102       f c
103
104 let count_item f c = function
105    | Some obj -> count_obj f c obj
106    | None     -> f c
107
108 let print_counters f c =
109    let terms =
110       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
111       c.tabbrs
112    in
113    let items = c.eabsts + c.eabbrs in
114    let nodes = c.nodes + c.xnodes in
115    L.warn (P.sprintf "  Kernel representation summary (basic_rg)");
116    L.warn (P.sprintf "    Total entry items:        %7u" items);
117    L.warn (P.sprintf "      Declaration items:      %7u" c.eabsts);
118    L.warn (P.sprintf "      Definition items:       %7u" c.eabbrs);
119    L.warn (P.sprintf "    Total term items:         %7u" terms);
120    L.warn (P.sprintf "      Sort items:             %7u" c.tsorts);
121    L.warn (P.sprintf "      Local reference items:  %7u" c.tlrefs);
122    L.warn (P.sprintf "      Global reference items: %7u" c.tgrefs);
123    L.warn (P.sprintf "      Explicit Cast items:    %7u" c.tcasts);
124    L.warn (P.sprintf "      Application items:      %7u" c.tappls);
125    L.warn (P.sprintf "      Abstraction items:      %7u" c.tabsts);
126    L.warn (P.sprintf "      Abbreviation items:     %7u" c.tabbrs);
127    L.warn (P.sprintf "    Global Int. Complexity:   %7u" c.nodes);
128    L.warn (P.sprintf "      + Abbreviation nodes:   %7u" nodes);
129    f ()
130
131 (* context/term pretty printing *********************************************)
132
133 let id frm a =
134    let f = function
135       | None            -> assert false
136       | Some (true, n)  -> F.fprintf frm "%s" n
137       | Some (false, n) -> F.fprintf frm "^%s" n
138    in      
139    B.name f a
140
141 let rec pp_term c frm = function
142    | B.Sort (_, h)           -> 
143       let f = function 
144          | Some s -> F.fprintf frm "@[%s@]" s
145          | None   -> F.fprintf frm "@[*%u@]" h
146       in
147       H.get_sort f h 
148    | B.LRef (_, i)           -> 
149       let f _ = function
150          | Some (a, _) -> F.fprintf frm "@[%a@]" id a
151          | None        -> F.fprintf frm "@[#%u@]" i
152       in
153       if !O.indexes then f 0 None else B.get 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 frm = function
175       | a, B.Abst w -> 
176          F.fprintf frm "@,@[%a : %a@]" id a (pp_term c) w
177       | a, B.Abbr v -> 
178          F.fprintf frm "@,@[%a = %a@]" id a (pp_term c) v
179       | a, B.Void   -> 
180          F.fprintf frm "@,%a" id a
181    in
182    let iter map frm l = List.iter (map frm) l in
183    let f es = F.fprintf frm "%a" (iter pp_entry) (List.rev es) in
184    B.contents f c
185
186 let specs = {
187    L.pp_term = pp_term; L.pp_context = pp_context
188 }
189
190 (* term xml printing ********************************************************)
191
192 let id frm a =
193    let f = function
194       | None            -> ()
195       | Some (true, s)  -> F.fprintf frm " name=%S" s
196       | Some (false, n) -> F.fprintf frm " name=%S" ("^" ^ n)
197    in      
198    B.name f a
199
200 let rec exp_term frm = function
201    | B.Sort (a, l)    ->
202       F.fprintf frm "<Sort position=%S%a/>" (string_of_int l) id a
203    | B.LRef (a, i)    ->
204       F.fprintf frm "<LRef position=%S%a/>" (string_of_int i) id a
205    | B.GRef (a, u)    ->
206       F.fprintf frm "<GRef uri=%S%a/>" (U.string_of_uri u) id a
207    | B.Cast (a, w, t) ->
208       F.fprintf frm "<Cast%a/>%a%a" id a exp_boxed w exp_term t 
209    | B.Appl (a, v, t) ->
210       F.fprintf frm "<Appl%a/>%a%a" id a exp_boxed v exp_term t 
211    | B.Bind (a, b, t) ->
212       F.fprintf frm "%a%a" (exp_bind a) b exp_term t
213
214 and exp_boxed frm t =
215    F.fprintf frm "@,@[<v3>   %a@]@," exp_term t
216
217 and exp_bind a frm = function
218    | B.Abst w ->
219       F.fprintf frm "<Abst%a/>%a" id a exp_boxed w
220    | B.Abbr v ->
221       F.fprintf frm "<Abbr%a/>%a" id a exp_boxed v
222    | B.Void   ->
223       F.fprintf frm "<Void%a/>" id a
224
225 let export_obj frm = function
226    | _, uri, B.Abst w -> 
227       let str = U.string_of_uri uri in
228       F.fprintf frm "%s@,@,<ABST uri=%S>%a</ABST>" 
229          (CL.doctype "ABST") str exp_boxed w
230    | _, uri, B.Abbr v -> 
231       let str = U.string_of_uri uri in
232       F.fprintf frm "%s@,@,<ABBR uri=%S>%a</ABBR>"
233          (CL.doctype "ABBR") str exp_boxed v
234    | _, uri, B.Void   -> 
235       let str = U.string_of_uri uri in
236       F.fprintf frm "%s@,@,<VOID uri=%S/>" 
237          (CL.doctype "VOID") str