]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgOutput.ml
66c7ed3d87a980350f00e2532ca5992e276fb841
[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 type counters = {
21    eabsts: int;
22    eabbrs: int;
23    tsorts: int;
24    tlrefs: int;
25    tgrefs: int;
26    tcasts: int;
27    tappls: int;
28    tabsts: int;
29    tabbrs: int
30 }
31
32 let indexes = ref false
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:        %6u" items);
91    L.warn (P.sprintf "      Declaration items:      %6u" c.eabsts);
92    L.warn (P.sprintf "      Definition items:       %6u" c.eabbrs);
93    L.warn (P.sprintf "    Total term items:         %6u" terms);
94    L.warn (P.sprintf "      Sort items:             %6u" c.tsorts);
95    L.warn (P.sprintf "      Local reference items:  %6u" c.tlrefs);
96    L.warn (P.sprintf "      Global reference items: %6u" c.tgrefs);
97    L.warn (P.sprintf "      Explicit Cast items:    %6u" c.tcasts);
98    L.warn (P.sprintf "      Application items:      %6u" c.tappls);
99    L.warn (P.sprintf "      Abstraction items:      %6u" c.tabsts);
100    L.warn (P.sprintf "      Abbreviation items:     %6u" c.tabbrs);
101    f ()
102
103 let rec pp_term c frm = function
104    | B.Sort h                 -> 
105       let f = function 
106          | Some s -> F.fprintf frm "@[%s@]" s
107          | None   -> F.fprintf frm "@[*%u@]" h
108       in
109       H.get_sort f h 
110    | B.LRef i                 -> 
111       let f = function
112          | Some (id, _) -> F.fprintf frm "@[%s@]" id
113          | None         -> F.fprintf frm "@[#%u@]" i
114       in
115       if !indexes then f None else B.get f c i
116    | B.GRef s                 -> F.fprintf frm "@[$%s@]" (U.string_of_uri s)
117    | B.Cast (u, t)            ->
118       F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
119    | B.Appl (v, t)            ->
120       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
121    | B.Bind (id, B.Abst w, t) ->
122       let f cc =
123          F.fprintf frm "@[[%s:%a].%a@]" id (pp_term c) w (pp_term cc) t
124       in
125       B.push f c id (B.Abst w)
126    | B.Bind (id, B.Abbr v, t) ->
127       let f cc = 
128          F.fprintf frm "@[[%s=%a].%a@]" id (pp_term c) v (pp_term cc) t
129       in
130       B.push f c id (B.Abbr v)
131    | B.Bind (id, B.Void, t)   ->
132       let f cc = F.fprintf frm "@[[%s].%a@]" id (pp_term cc) t in
133       B.push f c id B.Void
134
135 let pp_context frm c =
136    let pp_entry frm = function
137       | id, B.Abst w -> 
138          F.fprintf frm "@,@[%s : %a@]" id (pp_term c) w
139       | id, B.Abbr v -> 
140          F.fprintf frm "@,@[%s = %a@]" id (pp_term c) v
141       | id, B.Void   -> 
142          F.fprintf frm "@,%s" id
143    in
144    let iter map frm l = List.iter (map frm) l in
145    let f _ es = F.fprintf frm "%a" (iter pp_entry) (List.rev es) in
146    B.contents f c
147
148 let specs = {
149    L.pp_term = pp_term; L.pp_context = pp_context
150 }