Module

Run

Package
purescript-run
Repository
natefaubion/purescript-run

#Run Source

newtype Run (r :: Row Type) a

An extensible effect Monad, indexed by a set of effect functors. Effects are eliminated by interpretation into a pure value or into some base effect Monad. The Run Monad is an alternative to Monad Transformers, and can represent both associative and non-associative effects.

An example using State and Except:

type MyEffects =
  ( state ∷ STATE Int
  , except ∷ EXCEPT String
  , base ∷ BaseEff (console ∷ CONSOLE)
  )

yesProgram ∷ Run MyEffects Unit
yesProgram = do
  whenM (gets (_ < 0)) do
    throw "Number is less than 0"
  whileM_ (gets (_ > 0)) do
    liftEff $ log "Yes"
    modify (_ - 1)

main =
  yesProgram
    # catch (liftEff <<< log)
    # runState 10
    # runBase
    # void

Instances

#liftEffect Source

liftEffect :: forall a f r2 r1 sym. RowCons sym (FProxy f) r1 r2 => IsSymbol sym => Functor f => SProxy sym -> f a -> Run r2 a

Lifts an effect functor into the Run Monad according to the provided SProxy slot.

#withEffect Source

withEffect :: forall a f r2 r1 sym. RowCons sym (FProxy f) r1 r2 => SProxy sym -> FProxy f -> Run r1 a -> Run r2 a

#liftBase Source

liftBase :: forall a f r. Functor f => f a -> Run (base :: FProxy f | r) a

Lifts a base effect into the Run Monad (eg. Eff, Aff, or IO).

#peel Source

peel :: forall r a. Run r a -> Either (VariantF r (Run r a)) a

Reflects the next instruction or the final value if there are no more instructions.

#send Source

send :: forall r a. VariantF r a -> Run r a

Enqueues an instruction in the Run Monad.

#run Source

run :: forall a. Run () a -> a

Extracts the value from a fully interpreted program.

#runBase Source

runBase :: forall a f. MonadRec f => Run (base :: FProxy f) a -> f a

Extracts the value from a program with only base effects.

#interpret Source

interpret :: forall a r3 r2 r1 m f sym. RowCons sym (FProxy f) r2 r1 => RowCons "base" (FProxy m) r2 r3 => IsSymbol sym => Functor m => SProxy sym -> (f ~> m) -> Run r1 a -> Run r3 a

Interprets an effect functor into the base effect, eliminating the label.

#BaseEff Source

type BaseEff eff = FProxy (Eff eff)

#BaseAff Source

type BaseAff eff = FProxy (Aff eff)

Re-exports from Data.Functor.Variant

#VariantF Source

data VariantF (f :: Row Type) a

Instances

#FProxy Source

data FProxy (a :: Type -> Type)

Constructors

#on Source

on :: forall r2 r1 b a f sym. RowCons sym (FProxy f) r1 r2 => IsSymbol sym => SProxy sym -> (f a -> b) -> (VariantF r1 a -> b) -> VariantF r2 a -> b

Attempt to read a variant at a given label by providing branches. The failure branch receives the provided variant, but with the label removed.

#inj Source

inj :: forall r2 r1 a f sym. RowCons sym (FProxy f) r1 r2 => IsSymbol sym => Functor f => SProxy sym -> f a -> VariantF r2 a

Inject into the variant at a given label.

maybeAtFoo :: forall r. VariantF (foo :: FProxy Maybe | r) Int
maybeAtFoo = inj (SProxy :: SProxy "foo") (Just 42)

#default Source

default :: forall r b a. a -> VariantF r b -> a

Combinator for partial matching with a default value in case of failure.

caseFn :: forall r. VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String) | r) Int -> String
caseFn = default "No match"
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> maybe "nothing" show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> show (snd bar))

#case_ Source

case_ :: forall b a. VariantF () a -> b

Combinator for exhaustive pattern matching.

caseFn :: VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String), baz :: FProxy (Either String)) Int -> String
caseFn = case_
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> maybe "nothing" show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> show (snd bar))
 # on (SProxy :: SProxy "baz") (\baz -> "Baz: " <> either id show baz)

Re-exports from Data.Symbol

#SProxy Source

data SProxy (sym :: Symbol)

A value-level proxy for a type-level symbol.

Constructors