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