Skip to content

Commit

Permalink
Only use e/en/es for variables of type Effects
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjaguarpaw committed Aug 1, 2024
1 parent 32501e7 commit b64d94a
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions bluefin-internal/src/Bluefin/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,17 @@ useImplIn ::
useImplIn f h = inContext (f h)

-- | Handle to a capability to create strict mutable state handles
data StateSource (st :: Effects) = StateSource
data StateSource (e :: Effects) = StateSource

-- | Handle to an exception of type @e@
newtype Exception e (ex :: Effects) = UnsafeMkException (forall a. e -> IO a)
-- | Handle to an exception of type @exn@
newtype Exception exn (e :: Effects) = UnsafeMkException (forall a. exn -> IO a)

-- | A handle to a strict mutable state of type @s@
newtype State s (st :: Effects) = UnsafeMkState (IORef s)
newtype State s (e :: Effects) = UnsafeMkState (IORef s)

-- | A handle to a coroutine that expects values of type @a@ and then
-- yields values of type @b@.
newtype Coroutine a b (s :: Effects) = MkCoroutine (a -> Eff s b)
newtype Coroutine a b (e :: Effects) = MkCoroutine (a -> Eff e b)

-- | A handle to a stream that yields values of type @a@. It is
-- implemented as a handle to a coroutine that expects values of type
Expand Down Expand Up @@ -356,10 +356,10 @@ have = unsafeCoerce (Dict @(a :> (a :& b)))
-- Left 42
-- @
try ::
forall e (es :: Effects) a.
(forall ex. Exception e ex -> Eff (ex :& es) a) ->
forall exn (es :: Effects) a.
(forall e. Exception exn e -> Eff (e :& es) a) ->
-- | @Left@ if the exception was thrown, @Right@ otherwise
Eff es (Either e a)
Eff es (Either exn a)
try f =
UnsafeMkEff $ withScopedException_ (\throw_ -> unsafeUnEff (f (UnsafeMkException throw_)))

Expand All @@ -372,21 +372,21 @@ try f =
-- "42"
-- @
handle ::
forall e (es :: Effects) a.
forall exn (es :: Effects) a.
-- | If the exception is thrown, apply this handler
(e -> Eff es a) ->
(forall ex. Exception e ex -> Eff (ex :& es) a) ->
(exn -> Eff es a) ->
(forall e. Exception exn e -> Eff (e :& es) a) ->
Eff es a
handle h f =
try f >>= \case
Left e -> h e
Right a -> pure a

catch ::
forall e (es :: Effects) a.
(forall ex. Exception e ex -> Eff (ex :& es) a) ->
forall exn (es :: Effects) a.
(forall e. Exception exn e -> Eff (e :& es) a) ->
-- | If the exception is thrown, apply this handler
(e -> Eff es a) ->
(exn -> Eff es a) ->
Eff es a
catch f h = handle h f

Expand Down Expand Up @@ -436,8 +436,8 @@ bracket before after body =
-- (20,10)
-- @
get ::
(st :> es) =>
State s st ->
(e :> es) =>
State s e ->
-- | The current value of the state
Eff es s
get (UnsafeMkState r) = UnsafeMkEff (readIORef r)
Expand All @@ -450,8 +450,8 @@ get (UnsafeMkState r) = UnsafeMkEff (readIORef r)
-- ((), 30)
-- @
put ::
(st :> es) =>
State s st ->
(e :> es) =>
State s e ->
-- | The new value of the state. The new value is forced before
-- writing it to the state.
s ->
Expand All @@ -465,8 +465,8 @@ put (UnsafeMkState r) s = UnsafeMkEff (writeIORef r $! s)
-- ((), 20)
-- @
modify ::
(st :> es) =>
State s st ->
(e :> es) =>
State s e ->
-- | Apply this function to the state. The new value of the state
-- is forced before writing it to the state.
(s -> s) ->
Expand Down Expand Up @@ -548,7 +548,7 @@ runState ::
-- | Initial state
s ->
-- | Stateful computation
(forall st. State s st -> Eff (st :& es) a) ->
(forall e. State s e -> Eff (e :& es) a) ->
-- | Result and final state
Eff es (a, s)
runState s f = do
Expand Down Expand Up @@ -669,7 +669,7 @@ type EarlyReturn = Exception
-- "Returned early with 5"
-- @
withEarlyReturn ::
(forall er. EarlyReturn r er -> Eff (er :& es) r) ->
(forall e. EarlyReturn r e -> Eff (e :& es) r) ->
-- | ͘
Eff es r
withEarlyReturn = handle pure
Expand All @@ -684,8 +684,8 @@ withEarlyReturn = handle pure
-- "Returned early with 5"
-- @
returnEarly ::
(er :> es) =>
EarlyReturn r er ->
(e :> es) =>
EarlyReturn r e ->
-- | Return early to the handler, with this value.
r ->
Eff es a
Expand All @@ -702,7 +702,7 @@ evalState ::
-- | Initial state
s ->
-- | Stateful computation
(forall st. State s st -> Eff (st :& es) a) ->
(forall e. State s e -> Eff (e :& es) a) ->
-- | Result
Eff es a
evalState s f = fmap fst (runState s f)
Expand Down Expand Up @@ -893,7 +893,7 @@ type Jump = EarlyReturn ()
-- 15
-- @
withJump ::
(forall j. Jump j -> Eff (j :& es) ()) ->
(forall e. Jump e -> Eff (e :& es) ()) ->
-- | ͘
Eff es ()
withJump = withEarlyReturn
Expand All @@ -914,13 +914,13 @@ withJump = withEarlyReturn
-- 15
-- @
jumpTo ::
(j :> es) =>
Jump j ->
(e :> es) =>
Jump e ->
-- | ͘
Eff es a
jumpTo tag = throw tag ()

unwrap :: (j :> es) => Jump j -> Maybe a -> Eff es a
unwrap :: (e :> es) => Jump e -> Maybe a -> Eff es a
unwrap j = \case
Nothing -> jumpTo j
Just a -> pure a
Expand Down

0 comments on commit b64d94a

Please sign in to comment.