lexi-lambda / freer-simple

A friendly effect system for Haskell
https://hackage.haskell.org/package/freer-simple
BSD 3-Clause "New" or "Revised" License
227 stars 19 forks source link

How are you meant to use `interpose`? #32

Closed Hannes-Steffenhagen-bd closed 3 years ago

Hannes-Steffenhagen-bd commented 3 years ago

The way I thought interpose was meant to be used was to respond to events without actually handling them – I imagined this was maybe for cross cutting concerns like tracing. But surprisingly interpose requires a naturall transformation eff ~> Eff effs, meaning I have to "respond" with the correct type with the handler I give to interpose which doesn't make a lot of sense to me with my imagination. What also seems strange to me is that apparently interpose and interposeWith require me to produce a response... so how is that not handling the effect?

So, what am I missing here? What is interpose actually meant to be used for?

To put it in code, I'd have expected interpose to be more like

Member eff effs => (forall v. eff v -> (v -> Eff effs v)) -> Eff effs ~> Eff effs

I.e. we get a look at the incoming "request" and the outgoing "response" but we're not responsible for actually producing the response.


NVM. What I missed was that you can still use the effect you're interposing within interpose itself, so you can do what I just described with

interposeLikeIThought :: Member eff effs => (forall v. eff v -> (v -> Eff effs v)) -> Eff effs ~> Eff effs
interposeLikeIThought f = interpose $ \req -> do
  send req >>= f req