]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgOutput.ml
- brgOutput: the nodes count is now implemented
[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 B = Brg
19
20 (* nodes count **************************************************************)
21
22 type counters = {
23    eabsts: int;
24    eabbrs: int;
25    evoids: int;
26    tsorts: int;
27    tlrefs: int;
28    tgrefs: int;
29    tcasts: int;
30    tappls: int;
31    tabsts: int;
32    tabbrs: int;
33    tvoids: int;
34    uris  : U.uri list;
35    nodes : int;
36    xnodes: int
37 }
38
39 let initial_counters = {
40    eabsts = 0; eabbrs = 0; evoids = 0; 
41    tsorts = 0; tlrefs = 0; tgrefs = 0; tcasts = 0; tappls = 0;
42    tabsts = 0; tabbrs = 0; tvoids = 0;
43    uris = []; nodes = 0; xnodes = 0
44 }
45
46 let rec count_term_binder f c e = function
47    | B.Abst w ->
48       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
49       count_term f c e w
50    | B.Abbr v -> 
51       let c = {c with tabbrs = succ c.tabbrs; xnodes = succ c.xnodes} in
52       count_term f c e v
53    | B.Void   ->
54       let c = {c with tvoids = succ c.tvoids; xnodes = succ c.xnodes} in   
55       f c
56
57 and count_term f c e = function
58    | B.Sort _         -> 
59       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
60    | B.LRef (_, i)    -> 
61       let f _ = function
62          | None
63          | Some (_, B.Abst _) ->
64             f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
65          | Some (_, B.Abbr _)
66          | Some (_, B.Void)   ->
67             f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
68       in         
69       B.get f e i
70    | B.GRef (_, u)    -> 
71       let c =    
72          if Cps.list_mem ~eq:U.eq u c.uris
73          then {c with nodes = succ c.nodes}
74          else {c with xnodes = succ c.xnodes}
75       in
76       f {c with tgrefs = succ c.tgrefs}
77    | B.Cast (_, v, t) -> 
78       let c = {c with tcasts = succ c.tcasts} in
79       let f c = count_term f c e t in
80       count_term f c e v
81    | B.Appl (_, v, t) -> 
82       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
83       let f c = count_term f c e t in
84       count_term f c e v
85    | B.Bind (a, b, t) -> 
86       let f c e = count_term f c e t in
87       let f c = B.push (f c) e a b in
88       count_term_binder f c e b
89
90 let count_obj f c = function
91    | (_, u, B.Abst w) -> 
92       let c = {c with
93          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
94       } in
95       count_term f c B.empty_context w
96    | (_, _, B.Abbr v) ->  
97       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
98       count_term f c B.empty_context v
99    | (_, _, B.Void)   ->
100       let c = {c with evoids = succ c.evoids; xnodes = succ c.xnodes} in
101       f c
102
103 let count_item f c = function
104    | Some obj -> count_obj f c obj
105    | None     -> f c
106
107 let print_counters f c =
108    let terms =
109       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
110       c.tabbrs
111    in
112    let items = c.eabsts + c.eabbrs in
113    let nodes = c.nodes + c.xnodes in
114    L.warn (P.sprintf "  Kernel representation summary (basic_rg)");
115    L.warn (P.sprintf "    Total entry items:        %7u" items);
116    L.warn (P.sprintf "      Declaration items:      %7u" c.eabsts);
117    L.warn (P.sprintf "      Definition items:       %7u" c.eabbrs);
118    L.warn (P.sprintf "    Total term items:         %7u" terms);
119    L.warn (P.sprintf "      Sort items:             %7u" c.tsorts);
120    L.warn (P.sprintf "      Local reference items:  %7u" c.tlrefs);
121    L.warn (P.sprintf "      Global reference items: %7u" c.tgrefs);
122    L.warn (P.sprintf "      Explicit Cast items:    %7u" c.tcasts);
123    L.warn (P.sprintf "      Application items:      %7u" c.tappls);
124    L.warn (P.sprintf "      Abstraction items:      %7u" c.tabsts);
125    L.warn (P.sprintf "      Abbreviation items:     %7u" c.tabbrs);
126    L.warn (P.sprintf "    Global Int. Complexity:   %7u" c.nodes);
127    L.warn (P.sprintf "      + Abbreviation nodes:   %7u" nodes);
128    f ()
129
130 (* context/term pretty printing *********************************************)
131
132 let id frm a =
133    let f = function
134       | None            -> assert false
135       | Some (true, n)  -> F.fprintf frm "%s" n
136       | Some (false, n) -> F.fprintf frm "^%s" n
137    in      
138    B.name f a
139
140 let rec pp_term c frm = function
141    | B.Sort (_, h)           -> 
142       let f = function 
143          | Some s -> F.fprintf frm "@[%s@]" s
144          | None   -> F.fprintf frm "@[*%u@]" h
145       in
146       H.get_sort f h 
147    | B.LRef (_, i)           -> 
148       let f _ = function
149          | Some (a, _) -> F.fprintf frm "@[%a@]" id a
150          | None        -> F.fprintf frm "@[#%u@]" i
151       in
152       if !O.indexes then f 0 None else B.get f c i
153    | B.GRef (_, s)           -> F.fprintf frm "@[$%s@]" (U.string_of_uri s)
154    | B.Cast (_, u, t)        ->
155       F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
156    | B.Appl (_, v, t)        ->
157       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
158    | B.Bind (a, B.Abst w, t) ->
159       let f cc =
160          F.fprintf frm "@[[%a:%a].%a@]" id a (pp_term c) w (pp_term cc) t
161       in
162       B.push f c a (B.Abst w)
163    | B.Bind (a, B.Abbr v, t) ->
164       let f cc = 
165          F.fprintf frm "@[[%a=%a].%a@]" id a (pp_term c) v (pp_term cc) t
166       in
167       B.push f c a (B.Abbr v)
168    | B.Bind (a, B.Void, t)   ->
169       let f cc = F.fprintf frm "@[[%a].%a@]" id a (pp_term cc) t in
170       B.push f c a B.Void
171
172 let pp_context frm c =
173    let pp_entry frm = function
174       | a, B.Abst w -> 
175          F.fprintf frm "@,@[%a : %a@]" id a (pp_term c) w
176       | a, B.Abbr v -> 
177          F.fprintf frm "@,@[%a = %a@]" id a (pp_term c) v
178       | a, B.Void   -> 
179          F.fprintf frm "@,%a" id a
180    in
181    let iter map frm l = List.iter (map frm) l in
182    let f es = F.fprintf frm "%a" (iter pp_entry) (List.rev es) in
183    B.contents f c
184
185 let specs = {
186    L.pp_term = pp_term; L.pp_context = pp_context
187 }