How do I implement dynamic withCurrentDir effect #164
-
Hello, I am trying to implement a dynamic version of something very similar to I am having trouble working out what the data constructor and handler for import Path ( Path, Abs, Dir, File )
import qualified Path.IO as PIO
import Prelude
import qualified Prelude as P
import BasePrelude (IOException)
import Control.Monad.Catch (catch)
import Effectful as EF (
Dispatch (Dynamic),
DispatchOf,
Eff,
Effect,
IOE,
liftIO,
type (:>),
)
import Effectful.Dispatch.Dynamic
import Effectful.Dispatch.Static (unsafeLiftMapIO)
import Effectful.Error.Static as E
import Effectful.TH (makeEffect)
import qualified DSL.FileSystem.Raw as PO
data FileSystem :: Effect where
CreateDir :: Path b Dir -> FileSystem m ()
-- ??? what should the type of _b be?
WithCurrentDir :: Path Abs Dir -> _b -> FileSystem m a
makeEffect ''FileSystem
type instance DispatchOf FileSystem = Dynamic
newtype FSException = FSException IOException
deriving (Show)
instance Exception FSException
adaptException :: (HasCallStack, IOE :> es, E.Error FSException :> es) => IO b -> Eff es b
adaptException m = EF.liftIO m `catch` \(e :: IOException) -> throwError . FSException $ e
runFileSystem :: forall es a. (HasCallStack, IOE :> es, E.Error FSException :> es) => Eff (FileSystem : es) a -> Eff es a
runFileSystem =
let
er :: IO b -> Eff es b
er = adaptException
in
interpret $ \_ -> \case
CreateDir d -> er $ PO.createDir d
WithCurrentDir p action ->
-- ??? what goes here including adapting errors?
unsafeLiftMapIO (PIO.withCurrentDir p) action -- this doesn't compile
{-
Static effect as implemented in Effectful.FileSystem
-- | Lifted 'withCurrentDir'.
withCurrentDir :: (FileSystem :> es) => Path b Dir -> Eff es a -> Eff es a
withCurrentDir path = unsafeLiftMapIO (R.withCurrentDir path)
-}
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Did you have a look at https://hackage.haskell.org/package/effectful-core-2.2.2.2/docs/Effectful-Dispatch-Dynamic.html#g:3? It should have answers to both questions in the attached code. |
Beta Was this translation helpful? Give feedback.
Did you have a look at https://hackage.haskell.org/package/effectful-core-2.2.2.2/docs/Effectful-Dispatch-Dynamic.html#g:3? It should have answers to both questions in the attached code.