--------------------------------------------------------------------------------
--                                                                            --
-- interpreter building block for trace info                                  --
--                                                                            --
--------------------------------------------------------------------------------

-- T

data T x = Trace Name x

instance Functor T where
  map g (Trace n x) = Trace n (g x)
  
-- parse

instance Parser m x => Parser m (T x) where
  parser 
    = do { n <- pLow ; pLit "@" ; x <- parser' ; result (Trace n x) }
     where
      parser' = parser :: Parser m x => m x  

-- unparse
  
instance Algebra T (IO ()) where
  phi (Trace n x) 
    = do { uLow n ; uLit "@" ; x }

-- interpret

instance (StateMonad Level m, GenMonad (IO ()) m)
        => Algebra T (m v) where
  phi (Trace n mx) 
    = do { l <- act (\x -> x+1) ;
           gen (do { put (space (2*l)) ;  put "enter " ; put n ; 
                     put " : " ; put l ;  put "\n    " }) ;
           x <- mx ;
           gen (do { put (space (2*l)) ; put "leave " ; put n ;
                     put " : " ; put l ; put "\n    " } ) ;
           act (\x -> x-1) ;
           result x }
       
--------------------------------------------------------------------------------


