-
-
Save throughnothing/00ba3b2f4c461abd8506b91b8655faea to your computer and use it in GitHub Desktop.
Alternative Tagless Final encoding in PureScript
This file contains hidden or 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
module Algebra where | |
import Prelude | |
import Control.Monad.Eff (Eff) | |
import Data.Maybe (Maybe(..)) | |
newtype ConsoleAlg f = ConsoleAlg | |
{ printLn :: String -> f Unit | |
, readLn :: f String | |
} | |
newtype KVStoreAlg f = KVStoreAlg | |
{ put :: String -> String -> f Unit | |
, get :: String -> f (Maybe String) | |
} | |
class ConsoleAlgC f where | |
printLn :: String -> f Unit | |
readLn :: f String | |
class KVStoreAlgC f where | |
put :: String -> String -> f Unit | |
get :: String -> f (Maybe String) |
This file contains hidden or 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
module Interpreter where | |
import Prelude | |
import Control.Monad.Eff (Eff) | |
import Data.Maybe (Maybe(..)) | |
consoleInterpreter :: forall e. ConsoleAlg (Eff e) | |
consoleInterpreter = ConsoleAlg | |
{ printLn : \_ -> pure unit | |
, readLn : pure "John" | |
} | |
kvStoreInterpreter :: forall e. KVStoreAlg (Eff e) | |
kvStoreInterpreter = KVStoreAlg | |
{ put : \_ _ -> pure unit | |
, get : pure <<< Just | |
} | |
instance consoleInterpreterC :: ConsoleAlgC (Eff e) where | |
printLn _ = pure unit | |
readLn = pure "John" | |
instance kvStoreInterpreterC :: KVStoreAlgC (Eff e) where | |
put _ _ = pure unit | |
get = pure <<< Just | |
This file contains hidden or 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
module Program where | |
import Prelude | |
import Control.Monad.Eff (Eff) | |
import Data.Maybe (Maybe(..)) | |
program :: forall f. Bind f => ConsoleAlg f -> KVStoreAlg f -> f Unit | |
program (ConsoleAlg c) (KVStoreAlg k) = do | |
c.printLn "What's your name?" | |
name <- c.readLn | |
k.put "name" name | |
programC :: forall f. Bind f => ConsoleAlgC f => KVStoreAlgC f => f Unit | |
programC = do | |
printLn "What's your name?" | |
name <- readLn | |
put "name" name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment