bluefin-0.10.0.0: The Bluefin effect system
Safe HaskellSafe-Inferred
LanguageHaskell2010

Bluefin.Prim

Description

For defining PrimMonad instances, for example:

-- Define a capability which includes Prim
data ExAndPrim e1 e2 = MkExAndPrim (Throw String e2) (P.Prim e1 e2)
  -- Give it a Handle instance, as per Bluefin.Compound
  deriving (Handle) via OneWayCoercibleHandle (ExAndPrim e1)
  deriving stock (Generic)

instance (e2 <: es) => OneWayCoercible (ExAndPrim e1 e2) (ExAndPrim e1 es) where
  oneWayCoercibleImpl = gOneWayCoercible

-- Define a monad M containing the Prim capability
newtype M e a = MkM (DslBuilder (ExAndPrim e) a)
  deriving newtype (Functor, Applicative, Monad)

-- Define a way of running M
runM ::
  (e1 <: es, e2 <: es) =>
  Throw String e1 ->
  P.Prim e e2 ->
  M e r ->
  Eff es r
runM ex prim (MkM m) =
  runDslBuilder (MkExAndPrim (mapHandle ex) (mapHandle prim)) m

-- Give M a PrimMonad instance
instance PrimMonad (M e) where
  type PrimState (M e) = P.PrimStateEff e
  primitive f =
    MkM (dslBuilder (\(MkExAndPrim _ prim) -> P.primitive prim f))

-- ghci> example
-- Right ["Hello","World"]
example :: Either String [String]
example = runPureEff $ try $ \ex -> P.runPrim $ \prim -> do
  runM ex prim $ do
    arr <- A.newArray 2 "Hello"
    A.writeArray arr 1 "World"
    for [0, 1] (A.readArray arr)

Documentation

data Prim (e1 :: Effects) (e2 :: Effects) #

Instances

Instances details
e2 <: es => OneWayCoercible (Prim e1 e2 :: Type) (Prim e1 es :: Type) 
Instance details

Defined in Bluefin.Internal.Prim

Methods

oneWayCoercibleImpl :: OneWayCoercibleD (Prim e1 e2) (Prim e1 es) #

Handle (Prim e1) 
Instance details

Defined in Bluefin.Internal.Prim

Methods

handleImpl :: HandleD (Prim e1) #

runPrim :: forall (es :: Effects) r. (forall (e :: Effects). Prim e e -> Eff (e :& es) r) -> Eff es r #

data PrimStateEff (es :: Effects) #

primitive :: forall (e1 :: Effects) (e2 :: Effects) (es :: Effects) a. e2 <: es => Prim e1 e2 -> (State# (PrimStateEff e1) -> (# State# (PrimStateEff e1), a #)) -> Eff es a #