]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgOutput.ml
2d1fa96ae846b1f81cc10fa06ebc637af621ad2f
[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    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 (* context/term pretty printing *********************************************)
104
105 let id frm a =
106    let f = function
107       | None            -> assert false
108       | Some (true, n)  -> F.fprintf frm "%s" n
109       | Some (false, n) -> F.fprintf frm "^%s" n
110    in      
111    B.name f a
112
113 let rec pp_term c frm = function
114    | B.Sort (_, h)           -> 
115       let f = function 
116          | Some s -> F.fprintf frm "@[%s@]" s
117          | None   -> F.fprintf frm "@[*%u@]" h
118       in
119       H.get_sort f h 
120    | B.LRef (_, i)           -> 
121       let f _ = function
122          | Some (a, _) -> F.fprintf frm "@[%a@]" id a
123          | None        -> F.fprintf frm "@[#%u@]" i
124       in
125       if !O.indexes then f 0 None else B.get f c i
126    | B.GRef (_, s)           -> F.fprintf frm "@[$%s@]" (U.string_of_uri s)
127    | B.Cast (_, u, t)        ->
128       F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
129    | B.Appl (_, v, t)        ->
130       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
131    | B.Bind (a, B.Abst w, t) ->
132       let f cc =
133          F.fprintf frm "@[[%a:%a].%a@]" id a (pp_term c) w (pp_term cc) t
134       in
135       B.push f c a (B.Abst w)
136    | B.Bind (a, B.Abbr v, t) ->
137       let f cc = 
138          F.fprintf frm "@[[%a=%a].%a@]" id a (pp_term c) v (pp_term cc) t
139       in
140       B.push f c a (B.Abbr v)
141    | B.Bind (a, B.Void, t)   ->
142       let f cc = F.fprintf frm "@[[%a].%a@]" id a (pp_term cc) t in
143       B.push f c a B.Void
144
145 let pp_context frm c =
146    let pp_entry frm = function
147       | a, B.Abst w -> 
148          F.fprintf frm "@,@[%a : %a@]" id a (pp_term c) w
149       | a, B.Abbr v -> 
150          F.fprintf frm "@,@[%a = %a@]" id a (pp_term c) v
151       | a, B.Void   -> 
152          F.fprintf frm "@,%a" id a
153    in
154    let iter map frm l = List.iter (map frm) l in
155    let f es = F.fprintf frm "%a" (iter pp_entry) (List.rev es) in
156    B.contents f c
157
158 let specs = {
159    L.pp_term = pp_term; L.pp_context = pp_context
160 }