From cd744287dfa13030248edffde118baa954d8d821 Mon Sep 17 00:00:00 2001 From: solomon Date: Mon, 19 Dec 2022 11:34:59 -0800 Subject: [PATCH] Small haddock and function name changes. --- README.md | 4 ++-- chat-bots-contrib/src/Data/Chat/Bot/Calculator.hs | 6 +++--- chat-bots-contrib/src/Data/Chat/Bot/CoinFlip.hs | 9 ++++----- chat-bots-contrib/src/Data/Chat/Bot/GHCI.hs | 4 ++-- chat-bots-contrib/src/Data/Chat/Bot/Hello.hs | 4 ++-- chat-bots-contrib/src/Data/Chat/Bot/Jitsi.hs | 4 ++-- chat-bots-contrib/src/Data/Chat/Bot/Magic8Ball.hs | 4 ++-- chat-bots-contrib/src/Data/Chat/Bot/Updog.hs | 2 +- chat-bots-contrib/src/Data/Chat/Server/Matrix.hs | 8 +++++--- chat-bots-contrib/test/Spec.hs | 6 +++--- chat-bots/src/Data/Chat/Serialization.hs | 12 ++++++------ cofree-bot/app/Main.hs | 6 +++--- 12 files changed, 35 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 326f9dd..7280c29 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ helloBot = Bot $ \s msg -> Lifting Bots Over more complex inputs and outputs ------------------------------------------------- ```Haskell -liftSimpleBot :: Functor m => Bot m s Text Text -> Bot m s (RoomID, Event) (RoomID, Event) -liftSimpleBot (Bot bot) = Bot $ \s (rid, i) -> +embedTextBot :: Functor m => Bot m s Text Text -> Bot m s (RoomID, Event) (RoomID, Event) +embedTextBot (Bot bot) = Bot $ \s (rid, i) -> fmap (\(i', s') -> ((rid, mkMsg i'), s')) $ bot s (viewBody i) viewBody :: Event -> T.Text diff --git a/chat-bots-contrib/src/Data/Chat/Bot/Calculator.hs b/chat-bots-contrib/src/Data/Chat/Bot/Calculator.hs index 2e6e2ab..22bcaea 100644 --- a/chat-bots-contrib/src/Data/Chat/Bot/Calculator.hs +++ b/chat-bots-contrib/src/Data/Chat/Bot/Calculator.hs @@ -22,7 +22,7 @@ import Data.Chat.Bot (Bot) import Data.Chat.Bot.Calculator.Language as Language import Data.Chat.Serialization (TextSerializer) import Data.Chat.Serialization qualified as S -import Data.Chat.Server.Matrix (RoomID, liftSimpleBot) +import Data.Chat.Server.Matrix (RoomID, embedTextBot) import Data.Chat.Utils (type (\/)) import Data.Text (Text) import Data.Text qualified as Text @@ -34,10 +34,10 @@ calculatorBot :: Bot IO CalcState Statement (CalcError \/ CalcResp) calculatorBot = ask >>= state . execCalculator calculatorBot' :: Bot IO CalcState Text Text -calculatorBot' = S.simplifyBot calculatorBot calculatorSerializer +calculatorBot' = S.translateBot calculatorBot calculatorSerializer calculatorMatrixBot :: Bot IO CalcState (RoomID, Event) (RoomID, Event) -calculatorMatrixBot = liftSimpleBot calculatorBot' +calculatorMatrixBot = embedTextBot calculatorBot' -------------------------------------------------------------------------------- diff --git a/chat-bots-contrib/src/Data/Chat/Bot/CoinFlip.hs b/chat-bots-contrib/src/Data/Chat/Bot/CoinFlip.hs index c3293b4..1d841c7 100644 --- a/chat-bots-contrib/src/Data/Chat/Bot/CoinFlip.hs +++ b/chat-bots-contrib/src/Data/Chat/Bot/CoinFlip.hs @@ -14,8 +14,9 @@ import Data.Attoparsec.Text import Data.Chat.Bot import Data.Chat.Serialization (TextSerializer) import Data.Chat.Serialization qualified as S -import Data.Chat.Server.Matrix (RoomID, liftSimpleBot) +import Data.Chat.Server.Matrix (RoomID, embedTextBot) import Data.Text (Text) +import Data.Text qualified as Text import Network.Matrix.Client (Event) import System.Random (randomIO) @@ -25,7 +26,7 @@ coinFlipBot :: Bot IO () () Bool coinFlipBot = randomIO coinFlipMatrixBot :: Bot IO () (RoomID, Event) (RoomID, Event) -coinFlipMatrixBot = liftSimpleBot $ S.simplifyBot coinFlipBot coinFlipSerializer +coinFlipMatrixBot = embedTextBot $ S.translateBot coinFlipBot coinFlipSerializer -------------------------------------------------------------------------------- @@ -36,6 +37,4 @@ parser :: Text -> Maybe () parser = either (const Nothing) Just . parseOnly ("flip a coin" *> pure ()) printer :: Bool -> Text -printer = \case - True -> "Coin Flip Result: True" - False -> "Coin Flip Result: False" +printer p = "Coin Flip Result: " <> Text.pack (show p) diff --git a/chat-bots-contrib/src/Data/Chat/Bot/GHCI.hs b/chat-bots-contrib/src/Data/Chat/Bot/GHCI.hs index 196f48f..a3c0de0 100644 --- a/chat-bots-contrib/src/Data/Chat/Bot/GHCI.hs +++ b/chat-bots-contrib/src/Data/Chat/Bot/GHCI.hs @@ -21,7 +21,7 @@ import Data.Attoparsec.Text as A import Data.Chat.Bot import Data.Chat.Serialization (TextSerializer) import Data.Chat.Serialization qualified as S -import Data.Chat.Server.Matrix (Event, liftSimpleBot) +import Data.Chat.Server.Matrix (Event, embedTextBot) import Data.Text (Text) import Data.Text qualified as Text import GHC.Conc (threadDelay) @@ -42,7 +42,7 @@ ghciBot p = Bot $ pure (Text.pack o, s) ghciMatrixBot :: Process Handle Handle () -> Bot IO () (RoomID, Event) (RoomID, Event) -ghciMatrixBot handle = liftSimpleBot $ S.simplifyBot (ghciBot handle) ghciSerializer +ghciMatrixBot handle = embedTextBot $ S.translateBot (ghciBot handle) ghciSerializer -------------------------------------------------------------------------------- diff --git a/chat-bots-contrib/src/Data/Chat/Bot/Hello.hs b/chat-bots-contrib/src/Data/Chat/Bot/Hello.hs index b3d1d26..8bf804a 100644 --- a/chat-bots-contrib/src/Data/Chat/Bot/Hello.hs +++ b/chat-bots-contrib/src/Data/Chat/Bot/Hello.hs @@ -28,9 +28,9 @@ helloBot :: Monad m => Bot m s () Text helloBot = Bot $ \s () -> pure ("Are you talking to me, punk?", s) -- | We can then embed our bot in the Matrix API using --- 'liftSimpleBot'. +-- 'embedTextBot'. helloMatrixBot :: Monad m => Bot m () (RoomID, Event) (RoomID, Event) -helloMatrixBot = liftSimpleBot $ S.simplifyBot helloBot helloBotSerializer +helloMatrixBot = embedTextBot $ S.translateBot helloBot helloBotSerializer -------------------------------------------------------------------------------- diff --git a/chat-bots-contrib/src/Data/Chat/Bot/Jitsi.hs b/chat-bots-contrib/src/Data/Chat/Bot/Jitsi.hs index 903b1cd..5a8db35 100644 --- a/chat-bots-contrib/src/Data/Chat/Bot/Jitsi.hs +++ b/chat-bots-contrib/src/Data/Chat/Bot/Jitsi.hs @@ -14,7 +14,7 @@ import Data.Chat.Bot (Bot, liftEffect) import Data.Chat.Bot.Jitsi.Dictionary (adjectives, adverbs, pluralNouns, verbs) import Data.Chat.Serialization (TextSerializer) import Data.Chat.Serialization qualified as S -import Data.Chat.Server.Matrix (Event, RoomID, liftSimpleBot) +import Data.Chat.Server.Matrix (Event, RoomID, embedTextBot) import Data.Text (Text) import Data.Vector qualified as V import System.Random (randomRIO) @@ -25,7 +25,7 @@ jitsiBot :: Bot IO () () Text jitsiBot = liftEffect jitsiUrl jitsiMatrixBot :: Bot IO () (RoomID, Event) (RoomID, Event) -jitsiMatrixBot = liftSimpleBot $ S.simplifyBot jitsiBot jitsiSerializer +jitsiMatrixBot = embedTextBot $ S.translateBot jitsiBot jitsiSerializer -------------------------------------------------------------------------------- diff --git a/chat-bots-contrib/src/Data/Chat/Bot/Magic8Ball.hs b/chat-bots-contrib/src/Data/Chat/Bot/Magic8Ball.hs index 6e3cbfd..4dc1220 100644 --- a/chat-bots-contrib/src/Data/Chat/Bot/Magic8Ball.hs +++ b/chat-bots-contrib/src/Data/Chat/Bot/Magic8Ball.hs @@ -14,7 +14,7 @@ import Data.Attoparsec.Text import Data.Chat.Bot import Data.Chat.Serialization (TextSerializer) import Data.Chat.Serialization qualified as S -import Data.Chat.Server.Matrix (RoomID, liftSimpleBot) +import Data.Chat.Server.Matrix (RoomID, embedTextBot) import Data.Text (Text) import Network.Matrix.Client (Event) import System.Random @@ -26,7 +26,7 @@ magic8BallBot = do randomRIO (1, 20) magic8BallMatrixBot :: Bot IO () (RoomID, Event) (RoomID, Event) -magic8BallMatrixBot = liftSimpleBot $ S.simplifyBot magic8BallBot magic8BallSerializer +magic8BallMatrixBot = embedTextBot $ S.translateBot magic8BallBot magic8BallSerializer -------------------------------------------------------------------------------- diff --git a/chat-bots-contrib/src/Data/Chat/Bot/Updog.hs b/chat-bots-contrib/src/Data/Chat/Bot/Updog.hs index 32cd249..49bc5c0 100644 --- a/chat-bots-contrib/src/Data/Chat/Bot/Updog.hs +++ b/chat-bots-contrib/src/Data/Chat/Bot/Updog.hs @@ -31,7 +31,7 @@ updogBot = Bot $ \s -> \case OPP -> toListT [("yo, you know me!", s), ("HAH GOTTEM", s)] updogMatrixBot :: Monad m => Bot m () (RoomID, Event) (RoomID, Event) -updogMatrixBot = liftSimpleBot $ S.simplifyBot updogBot updogSerializer +updogMatrixBot = embedTextBot $ S.translateBot updogBot updogSerializer -------------------------------------------------------------------------------- diff --git a/chat-bots-contrib/src/Data/Chat/Server/Matrix.hs b/chat-bots-contrib/src/Data/Chat/Server/Matrix.hs index d895787..de7baec 100644 --- a/chat-bots-contrib/src/Data/Chat/Server/Matrix.hs +++ b/chat-bots-contrib/src/Data/Chat/Server/Matrix.hs @@ -3,7 +3,7 @@ module Data.Chat.Server.Matrix MatrixBot, matrix, simplifyMatrixBot, - liftSimpleBot, + embedTextBot, RoomID, Event, ) @@ -93,8 +93,10 @@ simplifyMatrixBot (Bot bot) = Bot $ \s i -> do (responses, nextState) <- bot s (RoomID mempty, mkMsg i) pure (viewBody $ snd responses, nextState) -liftSimpleBot :: Functor m => Bot m s Text Text -> MatrixBot m s -liftSimpleBot (Bot bot) = Bot $ \s (rid, i) -> +-- | Map the input and output of a @Bot m s Text Text@ to the Matrix +-- I/O types. +embedTextBot :: Functor m => Bot m s Text Text -> MatrixBot m s +embedTextBot (Bot bot) = Bot $ \s (rid, i) -> fmap (\(i', s') -> ((rid, mkMsg i'), s')) $ bot s (viewBody i) viewBody :: Event -> Text diff --git a/chat-bots-contrib/test/Spec.hs b/chat-bots-contrib/test/Spec.hs index 9418c23..1418636 100644 --- a/chat-bots-contrib/test/Spec.hs +++ b/chat-bots-contrib/test/Spec.hs @@ -24,7 +24,7 @@ main = hspec $ do helloBotSpec :: Spec helloBotSpec = describe "Hello Bot" $ do - let bot = S.simplifyBot helloBot helloBotSerializer + let bot = S.translateBot helloBot helloBotSerializer it "responds to precisely its trigger phrase" $ do let scenario = [mkScript| @@ -46,7 +46,7 @@ helloBotSpec = calculatorBotSpec :: Spec calculatorBotSpec = describe "Calculator Bot" $ do - let bot = S.simplifyBot calculatorBot calculatorSerializer + let bot = S.translateBot calculatorBot calculatorSerializer it "performs arithmetic" $ do let scenario = [mkScript| @@ -74,7 +74,7 @@ calculatorBotSpec = sessionizedBotSpec :: Spec sessionizedBotSpec = describe "Sessionized Bot" $ do - let bot = S.simplifyBot (sessionize mempty calculatorBot) (sessionSerializer calculatorSerializer) + let bot = S.translateBot (sessionize mempty calculatorBot) (sessionSerializer calculatorSerializer) it "can instantiate a session" $ do let scenario = [mkScript| diff --git a/chat-bots/src/Data/Chat/Serialization.hs b/chat-bots/src/Data/Chat/Serialization.hs index 77e661c..7a374d6 100644 --- a/chat-bots/src/Data/Chat/Serialization.hs +++ b/chat-bots/src/Data/Chat/Serialization.hs @@ -11,12 +11,12 @@ import Data.These -------------------------------------------------------------------------------- -simplifyBot :: +translateBot :: Monad m => Bot m s bi bo -> Serializer so si bo bi -> Bot m s so si -simplifyBot (Bot bot) (Serializer parser printer) = Bot $ \s i -> +translateBot (Bot bot) (Serializer parser printer) = Bot $ \s i -> case parser i of Nothing -> emptyListT Just i' -> do @@ -32,12 +32,12 @@ data Serializer so si bo bi = Serializer -- | A 'Serializer' whose 'Server' I/O has been specialized to 'Text'. type TextSerializer = Serializer Text Text --- | P +-- | Modify a 'Serializer' to parse and print a prefix string. prefix :: Text -> TextSerializer x y -> TextSerializer x y -prefix prefix Serializer {..} = +prefix txt Serializer {..} = Serializer - { parser = \so -> parser (prefix <> ": " <> so), - printer = \bo -> prefix <> ":" <> printer bo + { parser = \so -> parser (txt <> ": " <> so), + printer = \bo -> txt <> ":" <> printer bo } infixr 6 /+\ diff --git a/cofree-bot/app/Main.hs b/cofree-bot/app/Main.hs index def6014..10ec338 100644 --- a/cofree-bot/app/Main.hs +++ b/cofree-bot/app/Main.hs @@ -20,7 +20,7 @@ import Data.Chat.Bot.Sessions import Data.Chat.Bot.Updog (updogBot, updogSerializer) import Data.Chat.Serialization qualified as S import Data.Chat.Server (annihilate, loop) -import Data.Chat.Server.Matrix (liftSimpleBot, matrix) +import Data.Chat.Server.Matrix (embedTextBot, matrix) import Data.Chat.Server.Repl (repl) import Data.Foldable (fold) import GHC.Conc (threadDelay) @@ -70,7 +70,7 @@ serializer' = S./+\ ghciSerializer S./+\ sessionSerializer calculatorSerializer -bot process = S.simplifyBot (bot' process) serializer' +bot process = S.translateBot (bot' process) serializer' -------------------------------------------------------------------------------- @@ -92,5 +92,5 @@ matrixMain session xdgCache = withProcessWait_ ghciConfig $ \process -> do void $ threadDelay 1e6 void $ hGetOutput (getStdout process) state <- readState xdgCache - fixedBot <- flip (fixBotPersistent xdgCache) (fold state) $ hoistBot liftIO $ liftSimpleBot $ bot process + fixedBot <- flip (fixBotPersistent xdgCache) (fold state) $ hoistBot liftIO $ embedTextBot $ bot process unsafeCrashInIO $ loop $ annihilate (matrix session xdgCache) $ batch $ fixedBot