]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_ag/bagOutput.ml
Additional contribs.
[helm.git] / helm / software / lambda-delta / basic_ag / bagOutput.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 Y = Entity
17 module H = Hierarchy
18 module O = Output
19 module B = Bag
20
21 type counters = {
22    eabsts: int;
23    eabbrs: int;
24    tsorts: int;
25    tlrefs: int;
26    tgrefs: int;
27    tcasts: int;
28    tappls: int;
29    tabsts: int;
30    tabbrs: int
31 }
32
33 let initial_counters = {
34    eabsts = 0; eabbrs = 0; tsorts = 0; tlrefs = 0; tgrefs = 0;
35    tcasts = 0; tappls = 0; tabsts = 0; tabbrs = 0
36 }
37
38 let rec count_term_binder f c = function
39    | B.Abst w ->
40       let c = {c with tabsts = succ c.tabsts} in
41       count_term f c w
42    | B.Abbr v -> 
43       let c = {c with tabbrs = succ c.tabbrs} in
44       count_term f c v
45    | B.Void   -> f c
46
47 and count_term f c = function
48    | B.Sort _            -> 
49       f {c with tsorts = succ c.tsorts}
50    | B.LRef _            -> 
51       f {c with tlrefs = succ c.tlrefs}
52    | B.GRef _            -> 
53       f {c with tgrefs = succ c.tgrefs}
54    | B.Cast (v, t)       -> 
55       let c = {c with tcasts = succ c.tcasts} in
56       let f c = count_term f c t in
57       count_term f c v
58    | B.Appl (v, t)       -> 
59       let c = {c with tappls = succ c.tappls} in
60       let f c = count_term f c t in
61       count_term f c v
62    | B.Bind (_, _, b, t) -> 
63       let f c = count_term_binder f c b in
64       count_term f c t
65
66 let count_entity f c = function
67    | _, _, Y.Abst w -> 
68       let c = {c with eabsts = succ c.eabsts} in
69       count_term f c w
70    | _, _, Y.Abbr v -> 
71       let c = {c with eabbrs = succ c.eabbrs} in
72       count_term f c v
73    | _, _, Y.Void   -> assert false
74
75 let print_counters f c =
76    let terms =
77       c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
78       c.tabbrs
79    in
80    let items = c.eabsts + c.eabbrs in
81    let locations = B.locations () in
82    L.warn (P.sprintf "  Kernel representation summary (basic_ag)");
83    L.warn (P.sprintf "    Total entry items:        %7u" items);
84    L.warn (P.sprintf "      Declaration items:      %7u" c.eabsts);
85    L.warn (P.sprintf "      Definition items:       %7u" c.eabbrs);
86    L.warn (P.sprintf "    Total term items:         %7u" terms);
87    L.warn (P.sprintf "      Sort items:             %7u" c.tsorts);
88    L.warn (P.sprintf "      Local reference items:  %7u" c.tlrefs);
89    L.warn (P.sprintf "      Global reference items: %7u" c.tgrefs);
90    L.warn (P.sprintf "      Explicit Cast items:    %7u" c.tcasts);
91    L.warn (P.sprintf "      Application items:      %7u" c.tappls);
92    L.warn (P.sprintf "      Abstraction items:      %7u" c.tabsts);
93    L.warn (P.sprintf "      Abbreviation items:     %7u" c.tabbrs);
94    L.warn (P.sprintf "    Total binder locations:   %7u" locations);   
95    f ()
96
97 let res l id =
98    if !O.indexes then P.sprintf "#%u" l else id
99
100 let rec pp_term c frm = function
101    | B.Sort h                 -> 
102       let err () = F.fprintf frm "@[*%u@]" h in
103       let f s = F.fprintf frm "@[%s@]" s in
104       H.get_sort err f h 
105    | B.LRef i                 -> 
106       let f = function
107          | Some (id, _) -> F.fprintf frm "@[%s@]" id
108          | None         -> F.fprintf frm "@[#%u@]" i
109       in
110       if !O.indexes then f None else B.get f c i
111    | B.GRef s                    -> F.fprintf frm "@[$%s@]" (U.string_of_uri s)
112    | B.Cast (u, t)               ->
113       F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
114    | B.Appl (v, t)               ->
115       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
116    | B.Bind (l, id, B.Abst w, t) ->
117       let f cc =
118          F.fprintf frm "@[[%s:%a].%a@]" (res l id) (pp_term c) w (pp_term cc) t
119       in
120       B.push "output abst" f c l id (B.Abst w)
121    | B.Bind (l, id, B.Abbr v, t) ->
122       let f cc = 
123          F.fprintf frm "@[[%s=%a].%a@]" (res l id) (pp_term c) v (pp_term cc) t
124       in
125       B.push "output abbr" f c l id (B.Abbr v)
126    | B.Bind (l, id, B.Void, t)   ->
127       let f cc = F.fprintf frm "@[[%s].%a@]" (res l id) (pp_term cc) t in
128       B.push "output void" f c l id B.Void
129
130 let pp_lenv frm c =
131    let pp_entry frm = function
132       | l, id, B.Abst w -> 
133          F.fprintf frm "@,@[%s : %a@]" (res l id) (pp_term c) w
134       | l, id, B.Abbr v -> 
135          F.fprintf frm "@,@[%s = %a@]" (res l id) (pp_term c) v
136       | l, id, B.Void   -> 
137          F.fprintf frm "@,%s" (res l id)
138    in
139    let iter map frm l = List.iter (map frm) l in
140    let f es = F.fprintf frm "%a" (iter pp_entry) (List.rev es) in
141    B.contents f c
142
143 let specs = {
144    L.pp_term = pp_term; L.pp_lenv = pp_lenv
145 }