ekmett / kan-extensions

Kan extensions, Kan lifts, the Yoneda lemma, and (co)monads generated by a functor
Other
78 stars 33 forks source link

send :: (forall x. (a -> Free f x) -> f (Free f x)) -> Codensity (Free f) a #75

Open Icelandjack opened 1 year ago

Icelandjack commented 1 year ago

I was reading Oleg's Extensible Effects: An Alternative to Monad Transformers.

The coroutine for arbitrary effects Eff r is the same as Codensity (Free r).

data VE w r = Val w | E (r (VE w r))

newtype Eff r a = Eff{runEff :: ∀ w. (a → VE w r) → VE w r}

So I wondered if the send function had some usefulness in the library, admin is already a specialization of lowerCodensity

send :: (∀ w. (a → VE w r) → r (VE w r)) → Eff r a
send f = Eff $ \k → E (f k)

admin :: Eff r w → VE w r
admin (Eff m) = m Val

but send, producing a Codensity (Free f) a, is not in the library:

send :: (forall x. (a -> Free f x) -> f (Free f x)) -> Codensity (Free f) a
send eff = Codensity \next -> Free (eff next)
Icelandjack commented 1 year ago

The function send dispatches these requests and waits for a reply. It obtains the suspension k of the current computation (a return address of type a → VE w r), passes k to the user-specified request builder f obtaining the request body (of the type r (VE w r)), incorporates it into the request E, and delivers it to the waiting admin.