Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divisible decidable alt apply instances #2014

Open
wants to merge 2 commits into
base: series/7.3.x
Choose a base branch
from

Conversation

beezee
Copy link

@beezee beezee commented Oct 8, 2018

This adds several new instances that serve the techniques laid out in the derivation chapter of FP for Mortals.

  • Alt in terms of ApplicativePlus
  • Apply for any F[A, ?] in terms of Arrow[F]
  • Decidable for any F[?, A] in terms of Arrow[F], Choice[F] and Monoid[A]
  • Divide for any F[?, A] in terms of Arrow[F] and Semigroup[A]

A few notes on this.

  1. These are "orphaned" instances in the sense that you need to import them to use them. They are all defined in terms of other typeclasses. Putting them in the companion objects of their own typeclasses seems wrong because it creates ambiguity, but they target polymorphic types so there's no other companion object to house them. I couldn't determine whether there's precedent for this sort of thing in the project as of now.

  2. There's a TODO on some hand-wavy garbage Equal instance I threw in a test for functions that return Int. I figured I'd see if this was super offensive to anyone before deep diving into the laws to see if I could target a type with less inhabitants. If anyone is offended and doesn't mind guiding me in a direction to do better, I'd appreciate the help.

  3. It seems unfortunate to me to have Decidable extend Divisible. Here are the instances I ported - https://gist.github.com/beezee/533285b2123fb7cf6fd77456f061853e which target slightly different definitions of Decidable and Divide. The constraints for those implementation are much less, and they serve the purpose of typeclass derivation faithfully with the weaker definitions.

Awaiting feedback if people find value in this.

@edmundnoble edmundnoble added scalaz7 Scalaz 7.x and removed scalaz8 labels Oct 11, 2018
Copy link
Member

@xuwei-k xuwei-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://travis-ci.org/scalaz/scalaz/jobs/438902776#L519

[info] Updating /home/travis/build/scalaz/scalaz/core/src/main/scala/scalaz/ApplicativePlus.scala
[info] Updating /home/travis/build/scalaz/scalaz/core/src/main/scala/scalaz/Arrow.scala
[error] java.lang.RuntimeException: Map(Updated -> List(/home/travis/build/scalaz/scalaz/core/src/main/scala/scalaz/ApplicativePlus.scala, /home/travis/build/scalaz/scalaz/core/src/main/scala/scalaz/Arrow.scala))
[error] 	at scala.sys.package$.error(package.scala:27)
[error] 	at build$.$anonfun$standardSettings$24(build.scala:218)
[error] 	at build$.$anonfun$standardSettings$24$adapted(build.scala:215)
[error] 	at scala.Function1.$anonfun$compose$1(Function1.scala:44)
[error] 	at sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:40)
[error] 	at sbt.std.Transform$$anon$4.work(System.scala:67)
[error] 	at sbt.Execute.$anonfun$submit$2(Execute.scala:269)
[error] 	at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:16)
[error] 	at sbt.Execute.work(Execute.scala:278)
[error] 	at sbt.Execute.$anonfun$submit$1(Execute.scala:269)
[error] 	at sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:178)
[error] 	at sbt.CompletionService$$anon$2.call(CompletionService.scala:37)
[error] 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] 	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[error] 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[error] 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[error] 	at java.lang.Thread.run(Thread.java:748)
[error] (coreJVM / checkGenTypeClasses) Map(Updated -> List(/home/travis/build/scalaz/scalaz/core/src/main/scala/scalaz/ApplicativePlus.scala, /home/travis/build/scalaz/scalaz/core/src/main/scala/scalaz/Arrow.scala))

@lemastero
Copy link
Contributor

lemastero commented Nov 22, 2018

ad 3)
Purescript hierarchy for contravariant functors is more granular:

class Contravariant f <= Divide f where
  divide :: forall a b c. (a -> Tuple b c) -> f b -> f c -> f a

class Divide f <= Divisible f where
  conquer :: forall a. f a

class Divide f <= Decide f where
  choose :: forall a b c. (a -> Either b c) -> f b -> f c -> f a

class (Decide f, Divisible f) <= Decidable f where
  lose :: forall a. (a -> Void) -> f a

@beezee this would satisfy your needs? I wonder how laws would look like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scalaz7 Scalaz 7.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants