]> matita.cs.unibo.it Git - helm.git/blobdiff - 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
diff --git a/helm/software/lambda-delta/basic_ag/bagOutput.ml b/helm/software/lambda-delta/basic_ag/bagOutput.ml
new file mode 100644 (file)
index 0000000..589ce60
--- /dev/null
@@ -0,0 +1,153 @@
+(*
+    ||M||  This file is part of HELM, an Hypertextual, Electronic        
+    ||A||  Library of Mathematics, developed at the Computer Science     
+    ||T||  Department, University of Bologna, Italy.                     
+    ||I||                                                                
+    ||T||  HELM is free software; you can redistribute it and/or         
+    ||A||  modify it under the terms of the GNU General Public License   
+    \   /  version 2 or (at your option) any later version.              
+     \ /   This software is distributed as is, NO WARRANTY.              
+      V_______________________________________________________________ *)
+
+module P = Printf
+module F = Format
+module U = NUri
+module C = Cps
+module L = Log
+module H = Hierarchy
+module B = Bag
+
+type counters = {
+   eabsts: int;
+   eabbrs: int;
+   tsorts: int;
+   tlrefs: int;
+   tgrefs: int;
+   tcasts: int;
+   tappls: int;
+   tabsts: int;
+   tabbrs: int
+}
+
+let initial_counters = {
+   eabsts = 0; eabbrs = 0; tsorts = 0; tlrefs = 0; tgrefs = 0;
+   tcasts = 0; tappls = 0; tabsts = 0; tabbrs = 0
+}
+
+let rec count_term_binder f c = function
+   | B.Abst w ->
+      let c = {c with tabsts = succ c.tabsts} in
+      count_term f c w
+   | B.Abbr v -> 
+      let c = {c with tabbrs = succ c.tabbrs} in
+      count_term f c v
+   | B.Void   -> f c
+
+and count_term f c = function
+   | B.Sort _            -> 
+      f {c with tsorts = succ c.tsorts}
+   | B.LRef _            -> 
+      f {c with tlrefs = succ c.tlrefs}
+   | B.GRef _            -> 
+      f {c with tgrefs = succ c.tgrefs}
+   | B.Cast (v, t)       -> 
+      let c = {c with tcasts = succ c.tcasts} in
+      let f c = count_term f c t in
+      count_term f c v
+   | B.Appl (v, t)       -> 
+      let c = {c with tappls = succ c.tappls} in
+      let f c = count_term f c t in
+      count_term f c v
+   | B.Bind (_, _, b, t) -> 
+      let f c = count_term_binder f c b in
+      count_term f c t
+
+let count_obj_binder f c = function
+   | B.Abst w -> 
+      let c = {c with eabsts = succ c.eabsts} in
+      count_term f c w
+   | B.Abbr v -> 
+      let c = {c with eabbrs = succ c.eabbrs} in
+      count_term f c v
+   | B.Void   -> f c
+
+let count_obj f c (_, _, b) =
+   count_obj_binder f c b
+
+let count_item f c = function
+   | Some obj -> count_obj f c obj
+   | None     -> f c
+
+let print_counters f c =
+   let terms =
+      c.tsorts + c.tgrefs + c.tgrefs + c.tcasts + c.tappls + c.tabsts +
+      c.tabbrs
+   in
+   let items = c.eabsts + c.eabbrs in
+   L.warn (P.sprintf "  Kernel representation summary (basic_ag)");
+   L.warn (P.sprintf "    Total entry items:        %6u" items);
+   L.warn (P.sprintf "      Declaration items:      %6u" c.eabsts);
+   L.warn (P.sprintf "      Definition items:       %6u" c.eabbrs);
+   L.warn (P.sprintf "    Total term items:         %6u" terms);
+   L.warn (P.sprintf "      Sort items:             %6u" c.tsorts);
+   L.warn (P.sprintf "      Local reference items:  %6u" c.tlrefs);
+   L.warn (P.sprintf "      Global reference items: %6u" c.tgrefs);
+   L.warn (P.sprintf "      Explicit Cast items:    %6u" c.tcasts);
+   L.warn (P.sprintf "      Application items:      %6u" c.tappls);
+   L.warn (P.sprintf "      Abstraction items:      %6u" c.tabsts);
+   L.warn (P.sprintf "      Abbreviation items:     %6u" c.tabbrs);
+   f ()
+
+let indexes = ref false
+
+let res l id =
+   if !indexes then P.sprintf "#%u" l else id
+
+let rec pp_term c frm = function
+   | B.Sort h                 -> 
+      let f = function 
+         | Some s -> F.fprintf frm "@[%s@]" s
+        | None   -> F.fprintf frm "@[*%u@]" h
+      in
+      H.get_sort f h 
+   | B.LRef i                 -> 
+      let f = function
+         | Some (id, _) -> F.fprintf frm "@[%s@]" id
+         | None         -> F.fprintf frm "@[#%u@]" i
+      in
+      if !indexes then f None else B.get f c i
+   | B.GRef s                    -> F.fprintf frm "@[$%s@]" (U.string_of_uri s)
+   | B.Cast (u, t)               ->
+      F.fprintf frm "@[{%a}.%a@]" (pp_term c) u (pp_term c) t
+   | B.Appl (v, t)               ->
+      F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
+   | B.Bind (l, id, B.Abst w, t) ->
+      let f cc =
+         F.fprintf frm "@[[%s:%a].%a@]" (res l id) (pp_term c) w (pp_term cc) t
+      in
+      B.push f c l id (B.Abst w)
+   | B.Bind (l, id, B.Abbr v, t) ->
+      let f cc = 
+         F.fprintf frm "@[[%s=%a].%a@]" (res l id) (pp_term c) v (pp_term cc) t
+      in
+      B.push f c l id (B.Abbr v)
+   | B.Bind (l, id, B.Void, t)   ->
+      let f cc = F.fprintf frm "@[[%s].%a@]" (res l id) (pp_term cc) t in
+      B.push f c l id B.Void
+
+let pp_context frm c =
+   let pp_entry frm = function
+      | l, id, B.Abst w -> 
+         F.fprintf frm "@,@[%s : %a@]" (res l id) (pp_term c) w
+      | l, id, B.Abbr v -> 
+         F.fprintf frm "@,@[%s = %a@]" (res l id) (pp_term c) v
+      | l, id, B.Void   -> 
+         F.fprintf frm "@,%s" (res l id)
+   in
+   let iter map frm l = List.iter (map frm) l in
+   let f es = F.fprintf frm "%a" (iter pp_entry) (List.rev es) in
+   B.contents f c
+
+let specs = {
+   L.pp_term = pp_term; L.pp_context = pp_context
+}