Skip to content

Instantly share code, notes, and snippets.

@alexbiehl
Last active April 22, 2016 17:10
Show Gist options
  • Save alexbiehl/7ccc886fbc25d1acc83d5fb2d05fd636 to your computer and use it in GitHub Desktop.
Save alexbiehl/7ccc886fbc25d1acc83d5fb2d05fd636 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ExistentialQuantification #-}
import Conduit
import Data.Foldable
data Event = Event
data Command = Command
data Die = Die
-- | A bot is a mealy machine
data Bot m = forall x. Bot (x -> Event -> m (Either Die [Command], x)) (m x)
bot :: Bot IO
bot = Bot step start
where
start = undefined
step = undefined
runBot :: Monad m => Bot m -> Conduit Event m Command
runBot (Bot step start) = lift start >>= go
where
go st = do
eventOrEnd <- await
case eventOrEnd of
Nothing -> return ()
Just event -> do
(cmdOrDie, st') <- lift (step st event)
case cmdOrDie of
Left Die -> return ()
Right cmds -> yieldMany cmds >> go st'
main :: IO ()
main = stdinC
=$= linesUnboundedC
=$= eventsC
=$= runBot bot
=$= commandsC
=$= unlinesC
$$ stdoutC
where
eventsC :: Conduit String IO Event
eventsC = awaitForever (\_s -> undefined )
commandsC :: Conduit Command IO String
commandsC = awaitForever (\_c -> undefined )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment