typelevel / scala

Typelevel Scala, a fork of Scala
http://typelevel.org/scala/
372 stars 21 forks source link

add comonadic comprehensions (keyword 'cofor') #142

Closed shimib closed 7 years ago

shimib commented 7 years ago

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.

currently 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.

Some discussion and example can is in: https://contributors.scala-lang.org/t/providing-co-monadic-comprehensions/518

A SIP was also submitted: http://docs.scala-lang.org/sips/pending/comonadic-comprehensions.html

And the same PR is open at LBS: https://github.com/scala/scala/pull/5725

milessabin commented 7 years ago

Ahh ... possible confusion here. To be merged in TLS this PR has to be against scala/scala.

shimib commented 7 years ago

So, the original PR is enough? Do I need to do something?

On Fri, 31 Mar 2017 at 9:45 Miles Sabin notifications@github.com wrote:

Ahh ... possible confusion here. To be merged in TLS this PR has to be against scala/scala.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/typelevel/scala/pull/142#issuecomment-290630872, or mute the thread https://github.com/notifications/unsubscribe-auth/ACBGFXalTGoz6Y16_Oei8-48ZwcgUFqiks5rrKEBgaJpZM4MuwzT .

milessabin commented 7 years ago

If the original PR is up to date and rebased against the latest 2.12.x that's all you need to do. I'll merge in the next TLS release.

There'll be a TLS release for 2.11.9 very shortly ... if you could backport this to 2.11.x I'd be very happy to included that in that release too. Again the PR would need to be against scala/scala.

shimib commented 7 years ago

Ok, i'll try.

shimib commented 7 years ago

@milessabin i've created https://github.com/scala/scala/pull/5818 It seems to fail on binary incompatibility because of the additional compiler flag. Is it something that i need to address?

milessabin commented 7 years ago

@shimib yes it is. You need to add it to the MiMa whitelist ... see the literal types PR for an example.

You'll also need to squash after that.

shimib commented 7 years ago

@milessabin done (2.11.x) I'll do it on 2.12.x shortly