Composing and zooming several effects #242
-
I found it quite common when writing MTL style code to implement whatever functionality I needed on top of existing types. For example to have a program which can throw errors and record warning, I might nest I couldn't seem to find such functionality in effectful, but I'm thinking of doing something like this and am wondering if I'm repeating work done elsewhere, of if there's a cleaner way to do this. The error/warning example data ErrorsAndWarnings (e :: Type) :: Effect
type instance DispatchOf (ErrorsAndWarnings e) = Static NoSideEffects
data instance StaticRep (ErrorsAndWarnings e) = ErrorsAndWarnings (StaticRep (Error e)) (StaticRep (Writer [e]))
runErrorsAndWarnings :: forall e es a. Eff (ErrorsAndWarnings e : es) a -> Eff es (Either e a, [e])
runErrorsAndWarnings m = runWriter . runErrorNoCallStack $ do
w <- getStaticRep
e <- getStaticRep
raise . raise $ evalStaticRep (ErrorsAndWarnings e w) m
warn :: forall e es. (ErrorsAndWarnings e :> es) => e -> Eff es ()
warn e = stateStaticRepM \(ErrorsAndWarnings a b) ->
second (a `ErrorsAndWarnings`) <$> runStaticRep b (tell [e])
fail :: forall e es a. (ErrorsAndWarnings e :> es) => e -> Eff es a
fail e = stateStaticRepM \(ErrorsAndWarnings a b) ->
second (`ErrorsAndWarnings` b) <$> runStaticRep a (throwError e) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yeah, |
Beta Was this translation helpful? Give feedback.
Yeah,
reinterpret
is for this. Have a look at the introduction section of theEffectful.Dispatch.Dynamic
.runFileSystemPure
uses a privateState
effect not visible from outside.