Skip to content

Instantly share code, notes, and snippets.

@throughnothing
Forked from LukaJCB/Algebra.purs
Created December 4, 2018 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save throughnothing/00ba3b2f4c461abd8506b91b8655faea to your computer and use it in GitHub Desktop.
Save throughnothing/00ba3b2f4c461abd8506b91b8655faea to your computer and use it in GitHub Desktop.
Alternative Tagless Final encoding in PureScript
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)
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
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