mirror of
https://github.com/lexi-lambda/freer-simple.git
synced 2024-12-24 14:43:57 +03:00
Rename Teletype in examples to Console
This commit is contained in:
parent
c3548460a2
commit
ce1e579100
34
README.md
34
README.md
@ -36,7 +36,7 @@ The key features of Freer are:
|
||||
* Core components for defining your own Effects.
|
||||
|
||||
|
||||
# Example: Teletype DSL
|
||||
# Example: Console DSL
|
||||
|
||||
Here's what using Freer looks like:
|
||||
|
||||
@ -45,7 +45,7 @@ Here's what using Freer looks like:
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
{-# LANGUAGE DataKinds #-}
|
||||
module Teletype where
|
||||
module Console where
|
||||
|
||||
import Control.Monad.Freer
|
||||
import Control.Monad.Freer.Internal
|
||||
@ -54,39 +54,39 @@ import System.Exit hiding (ExitSuccess)
|
||||
--------------------------------------------------------------------------------
|
||||
-- Effect Model --
|
||||
--------------------------------------------------------------------------------
|
||||
data Teletype s where
|
||||
PutStrLn :: String -> Teletype ()
|
||||
GetLine :: Teletype String
|
||||
ExitSuccess :: Teletype ()
|
||||
data Console s where
|
||||
PutStrLn :: String -> Console ()
|
||||
GetLine :: Console String
|
||||
ExitSuccess :: Console ()
|
||||
|
||||
putStrLn' :: Member Teletype r => String -> Eff r ()
|
||||
putStrLn' :: Member Console r => String -> Eff r ()
|
||||
putStrLn' = send . PutStrLn
|
||||
|
||||
getLine' :: Member Teletype r => Eff r String
|
||||
getLine' :: Member Console r => Eff r String
|
||||
getLine' = send GetLine
|
||||
|
||||
exitSuccess' :: Member Teletype r => Eff r ()
|
||||
exitSuccess' :: Member Console r => Eff r ()
|
||||
exitSuccess' = send ExitSuccess
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Effectful Interpreter --
|
||||
--------------------------------------------------------------------------------
|
||||
runTeletype :: Eff '[Teletype] w -> IO w
|
||||
runTeletype (Val x) = return x
|
||||
runTeletype (E u q) = case extract u of
|
||||
(PutStrLn msg) -> putStrLn msg >> runTeletype (qApp q ())
|
||||
GetLine -> getLine >>= \s -> runTeletype (qApp q s)
|
||||
runConsole :: Eff '[Console] w -> IO w
|
||||
runConsole (Val x) = return x
|
||||
runConsole (E u q) = case extract u of
|
||||
(PutStrLn msg) -> putStrLn msg >> runConsole (qApp q ())
|
||||
GetLine -> getLine >>= \s -> runConsole (qApp q s)
|
||||
ExitSuccess -> exitSuccess
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Pure Interpreter --
|
||||
--------------------------------------------------------------------------------
|
||||
runTeletypePure :: [String] -> Eff '[Teletype] w -> [String]
|
||||
runTeletypePure inputs req =
|
||||
runConsolePure :: [String] -> Eff '[Console] w -> [String]
|
||||
runConsolePure inputs req =
|
||||
reverse . snd $ run (handleRelayS (inputs, []) (\s _ -> pure s) go req)
|
||||
where
|
||||
go :: ([String], [String])
|
||||
-> Teletype v
|
||||
-> Console v
|
||||
-> (([String], [String]) -> Arr '[] v ([String], [String]))
|
||||
-> Eff '[] ([String], [String])
|
||||
go (is, os) (PutStrLn msg) q = q (is, msg : os) ()
|
||||
|
@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file.
|
||||
[#8](https://github.com/IxpertaSolutions/freer-effects/issues/8)
|
||||
* `NonDetEff` separated into its own module and renamed to `NonDet`.
|
||||
[#11](https://github.com/IxpertaSolutions/freer-effects/issues/11)
|
||||
* Renamed `Teletype` example DSL to `Console`.
|
||||
|
||||
## [0.2.4.1] (November 25, 2016)
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE CPP #-}
|
||||
module Teletype where
|
||||
module Console where
|
||||
|
||||
#if !MIN_VERSION_base(4,8,0)
|
||||
import Control.Applicative (pure)
|
||||
@ -16,27 +16,27 @@ import Control.Monad.Freer.Internal
|
||||
-------------------------------------------------------------------------------
|
||||
-- Effect Model --
|
||||
-------------------------------------------------------------------------------
|
||||
data Teletype s where
|
||||
PutStrLn :: String -> Teletype ()
|
||||
GetLine :: Teletype String
|
||||
ExitSuccess :: Teletype ()
|
||||
data Console s where
|
||||
PutStrLn :: String -> Console ()
|
||||
GetLine :: Console String
|
||||
ExitSuccess :: Console ()
|
||||
|
||||
putStrLn' :: Member Teletype r => String -> Eff r ()
|
||||
putStrLn' :: Member Console r => String -> Eff r ()
|
||||
putStrLn' = send . PutStrLn
|
||||
|
||||
getLine' :: Member Teletype r => Eff r String
|
||||
getLine' :: Member Console r => Eff r String
|
||||
getLine' = send GetLine
|
||||
|
||||
exitSuccess' :: Member Teletype r => Eff r ()
|
||||
exitSuccess' :: Member Console r => Eff r ()
|
||||
exitSuccess' = send ExitSuccess
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Effectful Interpreter Simple --
|
||||
-------------------------------------------------------------------------------
|
||||
runTeletype :: Eff '[Teletype, IO] w -> IO w
|
||||
runTeletype req = runM (handleRelay pure go req)
|
||||
runConsole :: Eff '[Console, IO] w -> IO w
|
||||
runConsole req = runM (handleRelay pure go req)
|
||||
where
|
||||
go :: Teletype v -> Arr '[IO] v w -> Eff '[IO] w
|
||||
go :: Console v -> Arr '[IO] v w -> Eff '[IO] w
|
||||
go (PutStrLn msg) q = send (putStrLn msg) >>= q
|
||||
go GetLine q = send getLine >>= q
|
||||
go ExitSuccess q = send exitSuccess >>= q
|
||||
@ -44,12 +44,12 @@ runTeletype req = runM (handleRelay pure go req)
|
||||
-------------------------------------------------------------------------------
|
||||
-- Pure Interpreter Simple --
|
||||
-------------------------------------------------------------------------------
|
||||
runTeletypePure :: [String] -> Eff '[Teletype] w -> [String]
|
||||
runTeletypePure inputs req =
|
||||
runConsolePure :: [String] -> Eff '[Console] w -> [String]
|
||||
runConsolePure inputs req =
|
||||
reverse . snd $ run (handleRelayS (inputs, []) (\s _ -> pure s) go req)
|
||||
where
|
||||
go :: ([String], [String])
|
||||
-> Teletype v
|
||||
-> Console v
|
||||
-> (([String], [String]) -> Arr '[] v ([String], [String]))
|
||||
-> Eff '[] ([String], [String])
|
||||
go (is, os) (PutStrLn msg) q = q (is, msg : os) ()
|
||||
@ -61,26 +61,26 @@ runTeletypePure inputs req =
|
||||
-------------------------------------------------------------------------------
|
||||
-- Effectful Interpreter for Deeper Stack --
|
||||
-------------------------------------------------------------------------------
|
||||
runTeletypeM :: Member IO r => Eff (Teletype ': r) w -> Eff r w
|
||||
runTeletypeM (Val x) = return x
|
||||
runTeletypeM (E u q) = case decomp u of
|
||||
Right (PutStrLn msg) -> send (putStrLn msg) >> runTeletypeM (qApp q ())
|
||||
Right GetLine -> send getLine >>= \s -> runTeletypeM (qApp q s)
|
||||
runConsoleM :: Member IO r => Eff (Console ': r) w -> Eff r w
|
||||
runConsoleM (Val x) = return x
|
||||
runConsoleM (E u q) = case decomp u of
|
||||
Right (PutStrLn msg) -> send (putStrLn msg) >> runConsoleM (qApp q ())
|
||||
Right GetLine -> send getLine >>= \s -> runConsoleM (qApp q s)
|
||||
Right ExitSuccess -> send exitSuccess
|
||||
Left u' -> E u' (tsingleton (\s -> runTeletypeM (qApp q s)))
|
||||
Left u' -> E u' (tsingleton (\s -> runConsoleM (qApp q s)))
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Pure Interpreter for Deeper Stack --
|
||||
-------------------------------------------------------------------------------
|
||||
runTeletypePureM
|
||||
runConsolePureM
|
||||
:: [String]
|
||||
-> Eff (Teletype ': r) w
|
||||
-> Eff (Console ': r) w
|
||||
-> Eff r (Maybe w,([String],[String]))
|
||||
-- ^ (Nothing for ExitSuccess, (unconsumed input, produced output))
|
||||
runTeletypePureM inputs = f (inputs,[]) where
|
||||
runConsolePureM inputs = f (inputs,[]) where
|
||||
f
|
||||
:: ([String],[String])
|
||||
-> Eff (Teletype ': r) w
|
||||
-> Eff (Console ': r) w
|
||||
-> Eff r (Maybe w,([String],[String]))
|
||||
f st (Val x) = return (Just x, st)
|
||||
f st@(is,os) (E u q) = case decomp u of
|
@ -9,13 +9,13 @@ import System.Environment (getArgs)
|
||||
import Control.Monad.Freer
|
||||
|
||||
import Capitalize
|
||||
import Teletype
|
||||
import Console
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Example
|
||||
-------------------------------------------------------------------------------
|
||||
capitalizingTeletype :: (Member Teletype r, Member Capitalize r) => Eff r ()
|
||||
capitalizingTeletype = forever $ do
|
||||
capitalizingService :: (Member Console r, Member Capitalize r) => Eff r ()
|
||||
capitalizingService = forever $ do
|
||||
putStrLn' "Send something to capitalize..."
|
||||
l <- getLine'
|
||||
when (null l) exitSuccess'
|
||||
@ -24,24 +24,24 @@ capitalizingTeletype = forever $ do
|
||||
|
||||
mainPure :: IO ()
|
||||
mainPure = print . run
|
||||
. runTeletypePureM ["cat", "fish", "dog", "bird", ""]
|
||||
$ runCapitalizeM capitalizingTeletype
|
||||
. runConsolePureM ["cat", "fish", "dog", "bird", ""]
|
||||
$ runCapitalizeM capitalizingService
|
||||
|
||||
mainConsoleA :: IO ()
|
||||
mainConsoleA = runM (runTeletypeM (runCapitalizeM capitalizingTeletype))
|
||||
mainConsoleA = runM (runConsoleM (runCapitalizeM capitalizingService))
|
||||
-- | | | |
|
||||
-- IO () -' | | |
|
||||
-- Eff '[IO] () -' | |
|
||||
-- Eff '[Teletype, IO] () -' |
|
||||
-- Eff '[Capitalize, Teletype, IO] () -'
|
||||
-- Eff '[Console, IO] () -' |
|
||||
-- Eff '[Capitalize, Console, IO] () -'
|
||||
|
||||
mainConsoleB :: IO ()
|
||||
mainConsoleB = runM (runCapitalizeM (runTeletypeM capitalizingTeletype))
|
||||
mainConsoleB = runM (runCapitalizeM (runConsoleM capitalizingService))
|
||||
-- | | | |
|
||||
-- IO () -' | | |
|
||||
-- Eff '[IO] () -' | |
|
||||
-- Eff '[Capitalize, IO] () -' |
|
||||
-- Eff '[Teletype, Capitalize, IO] () -'
|
||||
-- Eff '[Console, Capitalize, IO] () -'
|
||||
|
||||
main :: IO ()
|
||||
main = getArgs >>= \case
|
||||
|
@ -90,11 +90,11 @@ executable freer-examples
|
||||
other-modules:
|
||||
Capitalize
|
||||
, Common
|
||||
, Console
|
||||
, Coroutine
|
||||
, Cut
|
||||
, Fresh
|
||||
, NonDet
|
||||
, Teletype
|
||||
, Trace
|
||||
|
||||
default-language: Haskell2010
|
||||
|
Loading…
Reference in New Issue
Block a user