Skip to content

Instantly share code, notes, and snippets.

@iokasimov
Last active February 24, 2017 17:34
Show Gist options
  • Save iokasimov/87a8277a6e9ca1021c23838e1e4d257b to your computer and use it in GitHub Desktop.
Save iokasimov/87a8277a6e9ca1021c23838e1e4d257b to your computer and use it in GitHub Desktop.
import Data.Void
import Control.Monad.Trans.Cont
type Program = Cont Context Void
data Status = Failure | Success deriving Show
data Context = Read (String -> Context) | Write String Context | Exit Status
readline :: Cont Context String
readline = cont Read
writeline :: String -> Cont Context ()
writeline str = cont $ \f -> Write str (f ())
exit :: Status -> Cont Context Void
exit status = cont $ \_ -> Exit status
program :: Program
program = readline >>= writeline >> exit Success
kernel :: Cont Context Void -> IO ()
kernel p = parse (fromvoid p) where
fromvoid p = runCont p absurd
parse next = case next of
Read f -> parse (f "lol")
Write s c -> parse c
Exit s -> print s
main = kernel program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment