Created
November 29, 2013 22:22
-
-
Save danidiaz/7712739 to your computer and use it in GitHub Desktop.
Using StaticArrow to track the number of steps in a computation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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