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