]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_ag/bagOutput.ml
new kernel basic_ag (with absolute local references)
[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    L.warn (P.sprintf "  Kernel representation summary (basic_ag)");
88    L.warn (P.sprintf "    Total entry items:        %6u" items);
89    L.warn (P.sprintf "      Declaration items:      %6u" c.eabsts);
90    L.warn (P.sprintf "      Definition items:       %6u" c.eabbrs);
91    L.warn (P.sprintf "    Total term items:         %6u" terms);
92    L.warn (P.sprintf "      Sort items:             %6u" c.tsorts);
93    L.warn (P.sprintf "      Local reference items:  %6u" c.tlrefs);
94    L.warn (P.sprintf "      Global reference items: %6u" c.tgrefs);
95    L.warn (P.sprintf "      Explicit Cast items:    %6u" c.tcasts);
96    L.warn (P.sprintf "      Application items:      %6u" c.tappls);
97    L.warn (P.sprintf "      Abstraction items:      %6u" c.tabsts);
98    L.warn (P.sprintf "      Abbreviation items:     %6u" c.tabbrs);
99    f ()
100
101 let indexes = ref false
102
103 let res l id =
104    if !indexes then P.sprintf "#%u" l else id
105
106 let rec pp_term c frm = function
107    | B.Sort h                 -> 
108       let f = function 
109          | Some s -> F.fprintf frm "@[%s@]" s
110          | None   -> F.fprintf frm "@[*%u@]" h
111       in
112       H.get_sort f h 
113    | B.LRef i                 -> 
114       let f = function
115          | Some (id, _) -> F.fprintf frm "@[%s@]" id
116          | None         -> F.fprintf frm "@[#%u@]" i
117       in
118       if !indexes then f None else B.get f c i
119    | B.GRef s                    -> F.fprintf frm "@[$%s@]" (U.string_of_uri s)
120    | B.Cast (u, t)               ->
121       F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
122    | B.Appl (v, t)               ->
123       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
124    | B.Bind (l, id, B.Abst w, t) ->
125       let f cc =
126          F.fprintf frm "@[[%s:%a].%a@]" (res l id) (pp_term c) w (pp_term cc) t
127       in
128       B.push f c l id (B.Abst w)
129    | B.Bind (l, id, B.Abbr v, t) ->
130       let f cc = 
131          F.fprintf frm "@[[%s=%a].%a@]" (res l id) (pp_term c) v (pp_term cc) t
132       in
133       B.push f c l id (B.Abbr v)
134    | B.Bind (l, id, B.Void, t)   ->
135       let f cc = F.fprintf frm "@[[%s].%a@]" (res l id) (pp_term cc) t in
136       B.push f c l id B.Void
137
138 let pp_context frm c =
139    let pp_entry frm = function
140       | l, id, B.Abst w -> 
141          F.fprintf frm "@,@[%s : %a@]" (res l id) (pp_term c) w
142       | l, id, B.Abbr v -> 
143          F.fprintf frm "@,@[%s = %a@]" (res l id) (pp_term c) v
144       | l, id, B.Void   -> 
145          F.fprintf frm "@,%s" (res l id)
146    in
147    let iter map frm l = List.iter (map frm) l in
148    let f es = F.fprintf frm "%a" (iter pp_entry) (List.rev es) in
149    B.contents f c
150
151 let specs = {
152    L.pp_term = pp_term; L.pp_context = pp_context
153 }