Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Created November 29, 2013 22:22
Show Gist options
  • Select an option

  • Save danidiaz/7712739 to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/7712739 to your computer and use it in GitHub Desktop.
Using StaticArrow to track the number of steps in a computation.
module Main where
import Data.Monoid
import Control.Monad
import Control.Applicative
import Control.Arrow
import Control.Arrow.Transformer
import Control.Arrow.Transformer.Static
type SteppedIO a b = StaticArrow ((,) (Sum Int)) (Kleisli IO) a b
step :: (a -> IO b) -> SteppedIO a b
step cmd = wrap (Sum 1, Kleisli cmd)
countSteps :: SteppedIO a b -> Int
countSteps = getSum . fst . unwrap
exec :: SteppedIO a b -> a -> IO b
exec = runKleisli . snd . unwrap
program :: SteppedIO () ()
program =
step (\_ -> putStrLn "What is your name?")
>>>
step (\_ -> getLine)
>>>
step (putStrLn . mappend "Hello, ")
main :: IO ()
main = do
putStrLn $ "Number of steps: " ++ show (countSteps program)
exec program ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment