Last active
February 24, 2017 17:34
-
-
Save iokasimov/87a8277a6e9ca1021c23838e1e4d257b to your computer and use it in GitHub Desktop.
This file contains 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
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