Skip to content

Instantly share code, notes, and snippets.

@ehamberg
Last active August 23, 2017 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehamberg/1bcee0949c5cddd02cfe3425c0f5485b to your computer and use it in GitHub Desktop.
Save ehamberg/1bcee0949c5cddd02cfe3425c0f5485b to your computer and use it in GitHub Desktop.
-- code based on https://www.youtube.com/watch?v=umiUJNcvPl0
data Command : Type -> Type where
PutStr : String -> Command ()
GetStr : Command String
data InfIO : Type where
Do : Command a -> (a -> Inf InfIO) -> InfIO
(>>=) : Command a -> (a -> Inf InfIO) -> InfIO
(>>=) = Do
runCommand : Command a -> IO a
runCommand (PutStr x) = putStr x
runCommand GetStr = getLine
-- a run function:
run : InfIO -> IO ()
run (Do command rest) = do res <- runCommand command
run (rest res)
-- expanding `do` notation and doing eta reduction fails:
run (Do command rest) = runCommand command >>= run . rest
-- `-- test.idr line 17 col 51:
-- When checking right hand side of run with expected type
-- IO ()
--
-- When checking an application of function Prelude.Basics..:
-- Type mismatch between
-- a -> Inf InfIO (Type of rest)
-- and
-- a -> InfIO (Expected type)
--
-- Specifically:
-- Type mismatch between
-- Inf InfIO
-- and
-- InfIO
-- inserting a `Force` fixes it:
run (Do command rest) = runCommand command >>= run . Force . rest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment