Skip to content

Commit

Permalink
Use ErrorEmpty instead of ()
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak committed Sep 15, 2024
1 parent 97ba2e0 commit 7ce9972
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions effectful-core/src/Effectful/NonDet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ runNonDetKeep
:: HasCallStack
=> Eff (NonDet : es) a
-> Eff es (Either CallStack a)
runNonDetKeep = reinterpret (fmap noError . runError @()) $ \env -> \case
Empty -> throwError ()
runNonDetKeep = reinterpret (fmap noError . runError @ErrorEmpty) $ \env -> \case
Empty -> throwError ErrorEmpty
m1 :<|>: m2 -> localSeqUnlift env $ \unlift -> do
mr <- (Just <$> unlift m1) `catchError` \_ () -> pure Nothing
mr <- (Just <$> unlift m1) `catchError` \_ ErrorEmpty -> pure Nothing
case mr of
Just r -> pure r
Nothing -> unlift m2
Expand All @@ -81,11 +81,11 @@ runNonDetRollback
=> Eff (NonDet : es) a
-> Eff es (Either CallStack a)
runNonDetRollback = reinterpret setup $ \env -> \case
Empty -> throwError ()
Empty -> throwError ErrorEmpty
m1 :<|>: m2 -> do
backupEnv <- cloneLocalEnv env
localSeqUnlift env $ \unlift -> do
mr <- (Just <$> unlift m1) `catchError` \_ () -> do
mr <- (Just <$> unlift m1) `catchError` \_ ErrorEmpty -> do
-- If m1 failed, roll back the environment.
restoreLocalEnv env backupEnv
pure Nothing
Expand All @@ -95,7 +95,7 @@ runNonDetRollback = reinterpret setup $ \env -> \case
where
setup action = do
backupEs <- unsafeEff cloneEnv
runError @() action >>= \case
runError @ErrorEmpty action >>= \case
Right r -> pure $ Right r
Left (cs, _) -> do
-- If the whole action failed, roll back the environment.
Expand All @@ -121,6 +121,12 @@ sumEff = foldr (<|>) emptyEff
----------------------------------------
-- Internal helpers

-- | Internal error type for the Empty action. Better than '()' in case it
-- escapes the scope of 'runNonDet' and shows up in error messages.
data ErrorEmpty = ErrorEmpty
instance Show ErrorEmpty where
show ErrorEmpty = "Effectful.NonDet.ErrorEmpty"

noError :: Either (cs, e) a -> Either cs a
noError = either (Left . fst) Right

Expand Down

0 comments on commit 7ce9972

Please sign in to comment.