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