Skip to content

Instantly share code, notes, and snippets.

@aitoroses
Created April 12, 2022 08:30
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 aitoroses/90013b01569f16bac9d94711a7c7361d to your computer and use it in GitHub Desktop.
Save aitoroses/90013b01569f16bac9d94711a7c7361d to your computer and use it in GitHub Desktop.
Purescript Example: Application Monad (AppM) with parallelization
type Env
= { hello :: String }
newtype AppM a
= AppM (ReaderT Env Aff a)
runAppM ∷ Env → AppM ~> Aff
runAppM env (AppM m) = runReaderT m env
derive newtype instance Functor AppM
derive newtype instance Apply AppM
derive newtype instance Applicative AppM
derive newtype instance Bind AppM
derive newtype instance Monad AppM
derive newtype instance MonadEffect AppM
derive newtype instance MonadAff AppM
derive newtype instance MonadAsk Env AppM
derive newtype instance MonadThrow Exception.Error AppM
derive newtype instance MonadError Exception.Error AppM
-- | https://github.com/purescript/purescript-parallel/pull/31/files
-- | The 'parallel' version of our application's monad.
-- | The base monad here is the parallel version of `Aff`: `ParAff`
newtype ParAppM a
= ParAppM (ReaderT Env ParAff a)
derive newtype instance functorParAppM ∷ Functor ParAppM
derive newtype instance applyParAppM ∷ Apply ParAppM
derive newtype instance applicativeParAppM ∷ Applicative ParAppM
-- Now we can implement Parallel for our AppM
-- by delegating it to the underlying base monads
instance parallelAppM ∷ Parallel ParAppM AppM where
parallel (AppM readerT) = ParAppM $ parallel readerT
sequential (ParAppM readerT) = AppM $ sequential readerT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment