]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/lambda-delta/basic_rg/brgOutput.ml
basic_rg: reduction was not tail recursive by mistake
[helm.git] / helm / software / lambda-delta / basic_rg / brgOutput.ml
index fb33da537a7b59bc2eb4a6cf113e1641702e1dda..a9cec439ac06c5aafcc1719828e8f034db432244 100644 (file)
 
 module P = Printf
 module F = Format
+module C = Cps
 module U = NUri
 module L = Log
+module Y = Entity
+module X = Library
 module H = Hierarchy
 module O = Output
 module B = Brg
@@ -44,13 +47,13 @@ let initial_counters = {
 }
 
 let rec count_term_binder f c e = function
-   | B.Abst w ->
+   | B.Abst (_, w) ->
       let c = {c with tabsts = succ c.tabsts; nodes = succ c.nodes} in
       count_term f c e w
-   | B.Abbr v -> 
+   | B.Abbr (_, v) -> 
       let c = {c with tabbrs = succ c.tabbrs; xnodes = succ c.xnodes} in
       count_term f c e v
-   | B.Void   ->
+   | B.Void _      ->
       let c = {c with tvoids = succ c.tvoids; xnodes = succ c.xnodes} in   
       f c
 
@@ -58,15 +61,15 @@ and count_term f c e = function
    | B.Sort _         -> 
       f {c with tsorts = succ c.tsorts; nodes = succ c.nodes}
    | B.LRef (_, i)    -> 
+      let err _ = f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes} in
       let f _ = function
-         | None
-        | Some (_, B.Abst _) ->
+        | B.Abst _ 
+        | B.Void _ ->
            f {c with tlrefs = succ c.tlrefs; nodes = succ c.nodes}
-        | Some (_, B.Abbr _)
-        | Some (_, B.Void)   ->
+        | B.Abbr _ ->
            f {c with tlrefs = succ c.tlrefs; xnodes = succ c.xnodes}
       in        
-      B.get f e i
+      B.get err f e i
    | B.GRef (_, u)    -> 
       let c =    
         if Cps.list_mem ~eq:U.eq u c.uris
@@ -82,27 +85,20 @@ and count_term f c e = function
       let c = {c with tappls = succ c.tappls; nodes = succ c.nodes} in
       let f c = count_term f c e t in
       count_term f c e v
-   | B.Bind (a, b, t) -> 
-      let f c e = count_term f c e t in
-      let f c = B.push (f c) e a b in
+   | B.Bind (b, t) -> 
+      let f c = count_term f c (B.push e b) t in
       count_term_binder f c e b
 
-let count_obj f c = function
-   | (_, u, B.Abst w) -> 
+let count_entity f c = function
+   | _, u, Y.Abst w -> 
       let c = {c with
          eabsts = succ c.eabsts; nodes = succ c.nodes; uris = u :: c.uris
       } in
-      count_term f c B.empty_context w
-   | (_, _, B.Abbr v) ->  
+      count_term f c B.empty_lenv w
+   | _, _, Y.Abbr v ->  
       let c = {c with eabbrs = succ c.eabbrs; xnodes = succ c.xnodes} in
-      count_term f c B.empty_context v
-   | (_, _, B.Void)   ->
-      let c = {c with evoids = succ c.evoids; xnodes = succ c.xnodes} in
-      f c
-
-let count_item f c = function
-   | Some obj -> count_obj f c obj
-   | None     -> f c
+      count_term f c B.empty_lenv v
+   | _, _, Y.Void -> assert false
 
 let print_counters f c =
    let terms =
@@ -127,110 +123,151 @@ let print_counters f c =
    L.warn (P.sprintf "      + Abbreviation nodes:   %7u" nodes);
    f ()
 
-(* context/term pretty printing *********************************************)
+(* supplementary annotation *************************************************)
+
+let attrs_of_binder f = function
+   | B.Abst (a, _) 
+   | B.Abbr (a, _)
+   | B.Void a      -> f a
+
+let rec does_not_occur f n r = function
+   | B.Null           -> f true
+   | B.Cons (c, _, b) ->
+      let f n1 r1 =
+         if n1 = n && r1 = r then f false else does_not_occur f n r c
+      in
+      attrs_of_binder (Y.name C.err f) b 
+
+let rename f c a =
+   let rec aux f c n r =
+      let f = function
+         | true  -> f n r
+        | false -> aux f c (n ^ "'") r
+      in
+      does_not_occur f n r c
+   in
+   let f n0 r0 =
+      let f n r = if n = n0 && r = r0 then f a else f (Y.Name (n, r) :: a) in
+      aux f c n0 r0 
+   in
+   Y.name C.err f a
+
+let rename_bind f c = function
+   | B.Abst (a, w) -> let f a = f (B.abst a w) in rename f c a
+   | B.Abbr (a, v) -> let f a = f (B.abbr a v) in rename f c a
+   | B.Void a      -> let f a = f (B.Void a) in rename f c a
 
-let id frm a =
-   let f = function
-      | None            -> assert false
-      | Some (true, n)  -> F.fprintf frm "%s" n
-      | Some (false, n) -> F.fprintf frm "^%s" n
+(* lenv/term pretty printing ************************************************)
+
+let name frm a =
+   let f n = function 
+      | true  -> F.fprintf frm "%s" n
+      | false -> F.fprintf frm "^%s" n
    in      
-   B.name f a
+   Y.name C.err f a
 
 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)           -> 
+   | B.Sort (_, h)             -> 
+      let err _ = F.fprintf frm "@[*%u@]" h in
+      let f s = F.fprintf frm "@[%s@]" s in
+      H.get_sort err f h 
+   | B.LRef (_, i)             -> 
+      let err _ = F.fprintf frm "@[#%u@]" i in
       let f _ = function
-         | Some (a, _) -> F.fprintf frm "@[%a@]" id a
-         | None        -> F.fprintf frm "@[#%u@]" i
+         | B.Abst (a, _) 
+        | B.Abbr (a, _)
+        | B.Void a      -> F.fprintf frm "@[%a@]" name a
       in
-      if !O.indexes then f 0 None else B.get f c i
-   | B.GRef (_, s)           -> F.fprintf frm "@[$%s@]" (U.string_of_uri s)
-   | B.Cast (_, u, t)        ->
+      if !O.indexes then err () else B.get err 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)        ->
+   | B.Appl (_, v, t)          ->
       F.fprintf frm "@[(%a).%a@]" (pp_term c) v (pp_term c) t
-   | B.Bind (a, B.Abst w, t) ->
-      let f cc =
-         F.fprintf frm "@[[%a:%a].%a@]" id a (pp_term c) w (pp_term cc) t
+   | B.Bind (B.Abst (a, w), t) ->
+      let f a =
+         let cc = B.push c (B.abst a w) in
+        F.fprintf frm "@[[%a:%a].%a@]" name a (pp_term c) w (pp_term cc) t
+      in
+      rename f c a
+   | B.Bind (B.Abbr (a, v), t) ->
+      let f a = 
+         let cc = B.push c (B.abbr a v) in
+        F.fprintf frm "@[[%a=%a].%a@]" name a (pp_term c) v (pp_term cc) t
       in
-      B.push f c a (B.Abst w)
-   | B.Bind (a, B.Abbr v, t) ->
-      let f cc = 
-         F.fprintf frm "@[[%a=%a].%a@]" id a (pp_term c) v (pp_term cc) t
+      rename f c a
+   | B.Bind (B.Void a, t)      ->
+      let f a = 
+         let cc = B.push c (B.Void a) in
+         F.fprintf frm "@[[%a].%a@]" name a (pp_term cc) t
       in
-      B.push f c a (B.Abbr v)
-   | B.Bind (a, B.Void, t)   ->
-      let f cc = F.fprintf frm "@[[%a].%a@]" id a (pp_term cc) t in
-      B.push f c a B.Void
-
-let pp_context frm c =
-   let pp_entry frm = function
-      | a, B.Abst w -> 
-         F.fprintf frm "@,@[%a : %a@]" id a (pp_term c) w
-      | a, B.Abbr v -> 
-         F.fprintf frm "@,@[%a = %a@]" id a (pp_term c) v
-      | a, B.Void   -> 
-         F.fprintf frm "@,%a" id a
+      rename f c a
+
+let pp_lenv frm c =
+   let pp_entry f c = function
+      | B.Abst (a, w) -> 
+         let f a = F.fprintf frm "@,@[%a : %a@]" name a (pp_term c) w; f () in
+         rename f c a
+      | B.Abbr (a, v) -> 
+         let f a = F.fprintf frm "@,@[%a = %a@]" name a (pp_term c) v; f () in
+        rename f c a
+      | B.Void a      -> 
+         let f a = F.fprintf frm "@,%a" name a; f () in
+        rename f c a
    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
+   B.rev_iter C.start pp_entry c
 
 let specs = {
-   L.pp_term = pp_term; L.pp_context = pp_context
+   L.pp_term = pp_term; L.pp_lenv = pp_lenv
 }
 
 (* term xml printing ********************************************************)
 
-let id frm a =
-   let f = function
-      | None            -> ()
-      | Some (true, s)  -> F.fprintf frm " name=%S" s
-      | Some (false, n) -> F.fprintf frm " name=%S" ("^" ^ n)
-   in      
-   B.name f a
-
-let rec exp_term frm = function
+let rec exp_term e t out tab = match t with
    | B.Sort (a, l)    ->
-      F.fprintf frm "<Sort position=%S%a/>" (string_of_int l) id a
+      let a =
+         let err _ = a in
+         let f s = Y.Name (s, true) :: a in
+        H.get_sort err f l
+      in
+      let attrs = [X.position l; X.name a] in
+      X.tag X.sort attrs out tab
    | B.LRef (a, i)    ->
-      F.fprintf frm "<LRef position=%S%a/>" (string_of_int i) id a
-   | B.GRef (a, u)    ->
-      F.fprintf frm "<GRef uri=%S%a/>" (U.string_of_uri u) id a
-   | B.Cast (a, w, t) ->
-      F.fprintf frm "<Cast%a/>%a%a" id a exp_boxed w exp_term t 
+      let a = 
+         let err _ = a in
+        let f n r = Y.Name (n, r) :: a in
+        let f _ b = attrs_of_binder (Y.name err f) b in
+         B.get err f e i
+      in
+      let attrs = [X.position i; X.name a] in
+      X.tag X.lref attrs out tab
+   | B.GRef (a, n)    ->
+      let a = Y.Name (U.name_of_uri n, true) :: a in
+      let attrs = [X.uri n; X.name a] in
+      X.tag X.gref attrs out tab
+   | B.Cast (a, u, t) ->
+      let attrs = [] in
+      X.tag X.cast attrs ~contents:(exp_term e u) out tab;
+      exp_term e t out tab
    | B.Appl (a, v, t) ->
-      F.fprintf frm "<Appl%a/>%a%a" id a exp_boxed v exp_term t 
-   | B.Bind (a, b, t) ->
-      F.fprintf frm "%a%a" (exp_bind a) b exp_term t
-
-and exp_boxed frm t =
-   F.fprintf frm "@,@[<v3>   %a@]@," exp_term t
-
-and exp_bind a frm = function
-   | B.Abst w ->
-      F.fprintf frm "<Abst%a/>%a" id a exp_boxed w
-   | B.Abbr v ->
-      F.fprintf frm "<Abbr%a/>%a" id a exp_boxed v
-   | B.Void   ->
-      F.fprintf frm "<Void%a/>" id a
-
-let exp_obj frm = function
-   | _, uri, B.Abst w -> 
-      let str = U.string_of_uri uri in
-      F.fprintf frm "<ABST uri=%S/>@,%a" str exp_term w
-   | _, uri, B.Abbr v -> 
-      let str = U.string_of_uri uri in
-      F.fprintf frm "<ABBR uri=%S/>@,%a" str exp_term v
-   | _, uri, B.Void   -> 
-      let str = U.string_of_uri uri in
-      F.fprintf frm "<VOID uri=%S/>" str
-
-let export_obj frm obj =
-   F.fprintf frm "@,@[<v3>   %a@]@," exp_obj obj
+      let attrs = [] in
+      X.tag X.appl attrs ~contents:(exp_term e v) out tab;
+      exp_term e t out tab
+   | B.Bind (b, t)    ->
+      let b = rename_bind C.start e b in
+      exp_bind e b out tab; 
+      exp_term (B.push e b) t out tab 
+
+and exp_bind e b out tab = match b with
+   | B.Abst (a, w) ->
+      let attrs = [X.name a; X.mark a] in
+      X.tag X.abst attrs ~contents:(exp_term e w) out tab
+   | B.Abbr (a, v) ->
+      let attrs = [X.name a; X.mark a] in
+      X.tag X.abbr attrs ~contents:(exp_term e v) out tab
+   | B.Void a ->
+      let attrs = [X.name a; X.mark a] in
+      X.tag X.void attrs out tab
+
+let export_term = exp_term B.empty_lenv