add comonadic comprehensions (keyword 'cofor') #5725
Conversation
|
Nice work :) |
|
10x. |
|
@shimib my understanding is that these days it's maintained by hand. |
|
@shimib yep I confirm what @milessabin said, I had to modify it by hand for my PR |
1d1b78c
to
38e4f30
|
Wow! I'm open to a discussion and a proof of concept, but we're going to have to take some time to figure out whether we all agree to add this to the language. Until then, you're welcome to experiment, but the full implementation, including the scanner/parser changes will have to be hidden by a flag, which doesn't seem to be the case. Instead of a new keyword, could we drive the expansion by the type of the monad? Could we generalize this pattern to allow macros to customize for comprehension, so that we can have Since this is a proof of concept, I'm going to schedule this for the WIP milestone. |
|
I spent some time working on comonads (see http://tomasp.net/coeffects for more) with Dominic who wrote the paper above - I find it quite funny how hard it is to find useful comonads in contrast to finding useful monads - but there are two things that can make comonads more useful:
To make this even more useful, it might be worth considering whether the language extension can cover these possible use cases. For merging of contexts, you'd need to change the translation so that |
|
@adriaanm thanks. Added the flag to parsers (was already in place for scanners). @tpetricek thanks. i'll take a look at it. |
|
Before we get too far into the nitty gritty implementation details, I'd like to have a discussion about adding this as a new language feature, as per my previous comments. Even behind a flag, this needs to be discussed prior to this PR being merged. In other words, we need at least a draft SIP and a thread on https://contributors.scala-lang.org/ Experience has shown that flags don't properly convey that something may not end up becoming part of the language, so, in order to ship with the official compiler, under a flag or not, there has to be some consensus that we eventually want this in the language from a design, implementation and maintenance point of view. |
|
Is this something that could be done as a macro first? |
|
This is something that would be a good candidate to merge in Typelevel Scala, with or without a SIP. I think it's important that discussion of implementation details happen here early rather than on the TLS repo. @smarter syntax makes a significant contribution to the usability of this feature and that's something which can't be experimented with practically in a macro or plugin. |
|
@milessabin @smarter I think it can be done in a compiler plugin, just hijack the parser via reflection. Paul did something similar for trailing commas support. |
|
@jvican it can but it's extra effort and the results are fragile. It's also completely unnecessary given that we can get experience in TLS. |
|
@milessabin i've started a topic at https://contributors.scala-lang.org/t/providing-co-monadic-comprehensions. I'll be happy to discuss implementation details here. I think that new keywords for "cofor and apfor" will be the most reasonable choice. |
|
@shimib It will be necessary eventually to include |
|
@edmundnoble sure. I'm waiting to see what will happen with |
|
Is |
|
Is this PR compatible with |
|
@Atry Regarding Currently, desugaring already works and i'm focusing now on quasi-quotes. After that, |
|
I did not find any mention to Cofree in the original paper. Could you post a link, please?
2017-02-27 15:26 GMT+08:00 Shimi Bandiel <notifications@github.com>:
… @Atry <https://github.com/Atry> Cofree + for and cofor can both express
the same computations. However, cofor is more elegant when you want to do
deep plumbing in your context (the original article mentions this).
Regarding scala.meta, i don't think this PR is compatible yet, i need to
look at it.
Currently, desugaring already works and i'm focusing now on quasi-quotes.
After that, scala.meta is a good candidate.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5725 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAktuhYWEziuDAmNKafMZT3mhNUBjPMTks5rgnq9gaJpZM4MHS50>
.
--
杨博 (Yang Bo)
|
|
I assumed you meant arrow notation for Comonads ( |
|
Ah, I see. I should mean |
|
@Atry yes it is. However, for complex extractions and context manipulations, |
|
@milessabin I think everything is in place here at LBS for submitting a PR against TLS and start a discussion there. WDYT? |
|
@shimib yes, I think so. |
|
@edmundnoble about hybrid monadic-applicative comprehensions: I believe this is exactly what I propose here: https://contributors.scala-lang.org/t/new-syntax-applicative-desugaring-in-for-comprehensions-with-pr/690 |
|
@ghik Indeed, I got to take a look. Like it a lot, but I hope this doesn't get Martin off the hook for providing irrefutable patterns without withFilter ;) |
|
closing for inactivity. the SIP committee considered a related SIP to be premature |
|
Pretty interesting work anyway! Lots of good references. |
This is a proposal (POC) to provide a new keyword (cofor) for co-monadic comprehensions in Scala.
The work is based over Dominic Orchard and Alan Mycroft paper: https://www.cl.cam.ac.uk/~dao29/publ/codo-notation-orchard-ifl12.pdf
If you find the implementation flawed, it is me to blame and not them!
the 'cofor' expression returns a function: (T[A] => B).
The function's type MUST be stated explicitly in the call-site. e.g.:
val result: Zipper[Person] => Boolean = cofor...
the syntax:
cofor(inputpatten) {
pattern1 <- gen1
pattern2 <- gen2
...
} yield body
the 'cofor' and generator must use types that provide:
def map(A => B)
def extract: A
def coflatMap(T[A] => B)
implementation is much more complex than 'for' desugaring as stated in the paper.
current implementation supports full Scala patterns in the input and generator patterns.
missing:
desugared tree positioning validation (should not affect surrounding code)
quasiquotes/macros/reification -- this is the next phase in the implementation.
the flag to control the keyword: -Ycofor-extension
if the flag is not enabled, current Scala code should not be affected.
compatibility: adding a new keyword breaks existing use of 'cofor' identifier and it should be back-quoted.
Plz let me know your thoughts on this.