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