purescript-dsl-example
purescript-dsl-example copied to clipboard
A simple CRUD DSL example with an asynchronious interpreter written as Cofree
A simple crud DSL with an interpreter written as cofree commonad in Purescript
The store type is simply
type Store = Array User
where
newtype User = User
{ id :: Int
, name :: String
}
There are four commands in the DSL:
data Command a = Add User a
| Remove Int a
| ChangeName Int String a
| GetUsers (Array User -> a)
| SaveUser User a
The type of the DSL is
type StoreDSL a = Free Command a
There are two interpreters
-
synchronous one
newtype Run a = Run { addUser :: User -> a , remove :: Int -> a , changeName :: Int -> String -> a , getUsers :: Unit -> Tuple (Array User) a , saveUser :: User -> a } type Interp a = Cofree Run aThe pairing is given by
pair :: forall x y. Command (x -> y) -> Run x -> yWe pair the
FreeandCofreeusing theexplorefunction fromControl.Comonad.Cofreemodule. -
asynchronous one with computations in the
Affmonadnewtype RunAff eff a = RunAff { addUser :: User -> Aff eff a , remove :: Int -> Aff eff a , changeName :: Int -> String -> Aff eff a , getUsers :: Unit -> Aff eff (Tuple (Array User) a) , saveUser :: User -> Aff eff a } type AffInterp eff a = Cofree (RunAff eff) aThe pairing is given by
pairInAff :: forall eff x y. Command (x -> y) -> RunAff eff x -> Aff eff yHere we pair using a custom
exploreAfffunction.