polysemy-research / polysemy-zoo

:monkey::panda_face: Experimental, user-contributed effects and interpreters for polysemy
BSD 3-Clause "New" or "Revised" License
70 stars 21 forks source link

Add Final effect #35

Closed KingoftheHomeless closed 5 years ago

KingoftheHomeless commented 5 years ago

This is the Lift'/Final effect originally discussed at https://github.com/polysemy-research/polysemy/issues/163, but touched up, and with the introduction of a Tactics-like environment to make it easier to write interpreters using it.

I'm going to soon add a few more modules to showcase how this can be used, but you should be able to get the gist from Polysemy.Final

KingoftheHomeless commented 5 years ago

Oh! This hasn't been updated to use the changed names for Yo (since polysemy-zoo is still based on the old version). Perhaps we should resolve that before merging in?

KingoftheHomeless commented 5 years ago

Alright, I've now added a slew of uses of Final.

Now I'll write some tests and see how everything works.

KingoftheHomeless commented 5 years ago

From what I can tell, -Final interpreters act exactly like the kind of interpreters that take explicit lowering actions, such as runAsyncInIO and runErrorInIO, only are more flexible, as they work through weavings, which can be provided by Sem implicitly, rather than straight-up natural transformations.

The difference is that you can place -Final interpreters anywhere, whereas interpreters with explicit lowering functions need to run after return-value changing effects in order to be composed using .@.

You could actually place such interpreters before return-value changing interpreters by providing an explicit lowering function based upon the interpreter chain, using fmap and the like to massage the interpreter chain into a natural transformation. In this case, these interpreters will exhibit the same strange semantics as -Final interpreters (as effects that care about state semantics typically have return-value changing interpreters.)

Final, in a sense, makes it a bit "too easy" to do that kind of evil interpreter chaining, but has a much better UX. Providing lowering functions is very verbose, and working with .@ is rather finicky. Because of that, I'd like for Final to eventually become part of Polysemy proper, but at the moment, it can live here.

KingoftheHomeless commented 5 years ago

By the way, the tests in FinalIOSpec (edit: now a part of FinalSpec) work through IO in order to test how -Final interpreters work within it. Is that OK with you?

isovector commented 5 years ago

Let me know when this is ready for me to take a look!

KingoftheHomeless commented 5 years ago

It's ready now; I've added tests. Although, I do need to update this to use the new name for Yo. I'd wait until #36 is resolved before merging this in, as in resolving that issue we will work out how to properly update polysemy-zoo.

KingoftheHomeless commented 5 years ago

Perhaps you want me to ping you? @isovector

KingoftheHomeless commented 5 years ago

"It's ready", I say, as I notice "hey we can do stuff with resource too".

Blegh. Behaviour of runResourceFinal is very straight-forward, and works just like runResourceInIO.

KingoftheHomeless commented 5 years ago

Rebasing is a nefarious beast. Anyway, what's your opinion on merging this, @isovector ? We still have the unresolved question on whether or not we should remove runFinalLift and runFinalLiftIO.

isovector commented 5 years ago

I'll merge anything that's green into the zoo! Here be dragons!

KingoftheHomeless commented 5 years ago

Then let's do it.