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