Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Polysemy/NonDet.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Description: Interpreters for 'NonDet'
module Polysemy.NonDet
Expand All @@ -18,6 +22,7 @@ import Data.Maybe

import Polysemy
import Polysemy.Error
import Polysemy.Final (Final, interpretFinal, liftS, runS)
import Polysemy.Internal
import Polysemy.Internal.NonDet
import Polysemy.Internal.Union
Expand Down Expand Up @@ -72,6 +77,18 @@ nonDetToError (e :: e) = interpretH $ \case
{-# INLINE nonDetToError #-}


------------------------------------------------------------------------------
-- | Run a 'NonDet' effect in terms of the 'Final' 'Alternative' @m@.
runNonDetFinal :: (Member (Final m) r, Alternative m) => Sem (NonDet ': r) a -> Sem r a
runNonDetFinal = interpretFinal $ \case
Empty -> liftS empty
Choose semA semB -> do
a <- runS semA
b <- runS semB
pure $ a <|> b
{-# INLINE runNonDetFinal #-}


--------------------------------------------------------------------------------
-- This stuff is lifted from 'fused-effects'. Thanks guys!
runNonDetC :: (Alternative f, Applicative m) => NonDetC m a -> m (f a)
Expand Down