Skip to content

Add divide typeclasses #1311

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

Merged
merged 8 commits into from
Feb 24, 2019
Merged

Conversation

1Jajen1
Copy link
Member

@1Jajen1 1Jajen1 commented Feb 22, 2019

Partially solves #284 (Only the divide parts)

Again like stated in #1181 there some annoying parts:

  • The docs are there, I don't really like them, but could not come up with better text/examples 🙈
  • There are no laws for decidable atm, there are some here, but I don't really understand them well enough to attempt to port them :/ Scala cats and/or scalaz don't include them either afaik.
  • Some monad transformer instances are missing tests because we don't have instances that fulfill the constraints for them (don't really know what to do there)

Any ideas for better examples/texts do describe them? I'll think about it some more, but don't know if I can come up with something better :/

@1Jajen1
Copy link
Member Author

1Jajen1 commented Feb 22, 2019

Hmm I don't think I understand the error ank throws here. It happens locally as well, but not when the code is just run in a standalone file (copy pasted over basically). I am confused :/

@pakoito
Copy link
Member

pakoito commented Feb 22, 2019

I'll review this tonight or tomorrow!

cf: (Int) -> Kind<F, Int>,
EQ: Eq<Kind<F, Int>>
): Unit =
forAll(Gen.int().map(cf)) { fa ->
Copy link
Member

Choose a reason for hiding this comment

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

Cute. I like it :)

EQ: Eq<Kind<F, Int>>
): Unit =
forAll(Gen.int().map(cf)) { fa ->
EQ.run {
Copy link
Member

Choose a reason for hiding this comment

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

I believe this should use equalsUnderTheLaw instead of EQ directly.

EQ: Eq<Kind<F, Int>>
): Unit =
forAll(Gen.int().map(cf)) { fa ->
EQ.run {
Copy link
Member

Choose a reason for hiding this comment

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

Same as above, equalsUnderTheLaw

*
* fun main(args: Array<String>) {
* //sampleStart
* val stringOrInt: Serializer<Either<String, Int>> = Serializer.decidable()
Copy link
Member

Choose a reason for hiding this comment

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

Powerful shit.

fh: Kind<F, H>,
fi: Kind<F, I>,
fj: Kind<F, J>,
f: (Z) -> Either<A, Either<B, Either<C, Either<D, Either<E, Either<FF, Either<G, Either<H, Either<I, J>>>>>>>>>
Copy link
Member

Choose a reason for hiding this comment

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

@raulraja is there any way of generalizing this for Product? Or convert between them?

/**
* [Divide] is a typeclass that models the divide part of divide and conquer.
*
* [Divide] basically states: Given a Kind<F, A> and a Kind<F, B> and a way to turn C into an A and B it gives you a Kind<F, C>
Copy link
Member

Choose a reason for hiding this comment

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

into an A and B may be better phrased as a tuple of A and B, even if it's an implementation detail it's easy to miss.

val (a, b, c, d, e, fff, g, h, i, j) = f(it)
a toT Tuple9(b, c, d, e, fff, g, h, i, j)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

NIT: additional empty line

dummy6: Unit = Unit,
dummy7: Unit = Unit,
dummy8: Unit = Unit
): Kind<F, Tuple10<A, B, C, D, E, FF, G, H, I, J>> =
Copy link
Member

Choose a reason for hiding this comment

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

RIP IntelliJ

Copy link
Member Author

Choose a reason for hiding this comment

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

I've done worse with a small project regarding generators for prop testing (generated ~1k lines of Tuple code for generating tuples and shrinkers for them) :) IntelliJ takes a while to load that file :D

beginner

`Divide` is a typeclass that models the divide part of divide and conquer.
`Divide` basically states: Given a `Kind<F, A>` and a `Kind<F, B>` and a way to turn `C` into an `A` and `B` it provides you a `Kind<F, C>`
Copy link
Member

Choose a reason for hiding this comment

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

Same as above, mention a coproduct or a tuple or something that helps with the association, else it's easy to miss.

Copy link
Member

@pakoito pakoito left a comment

Choose a reason for hiding this comment

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

Really good job getting both parts of the hierarchy! 👏

Approving to unblock, but I'd like to see the fixes in the laws if possible.

@1Jajen1
Copy link
Member Author

1Jajen1 commented Feb 23, 2019

WTF github I never clicked that

@1Jajen1 1Jajen1 closed this Feb 23, 2019
@1Jajen1 1Jajen1 reopened this Feb 23, 2019
@1Jajen1
Copy link
Member Author

1Jajen1 commented Feb 23, 2019

Done the small stuff.
Tests are missing for StateT (we have no monad that implements Divide etc) (although this may be possible with using like OptionT and Const like in their tests, which is already weird but might work) and Sum (again no comonads that implement divide etc)

@1Jajen1
Copy link
Member Author

1Jajen1 commented Feb 23, 2019

Added a test for Sum. Now only StateT is missing, but since we have no instance that is Monad and Divisible that is kinda hard to do. (Function1 is almost it with Monad<Function1PartialOf<A>> but Divisible is defined as Divisible<Conested<ForFunction1, A>>)

@1Jajen1
Copy link
Member Author

1Jajen1 commented Feb 24, 2019

So ank is being funny (or rather what executes the script). The code 100% compiles and runs fine but running in ank crashes with:
Backend Internal error: Exception during code generation Cause: Back-end (JVM) Internal error: Don't know how to generate outer expression: Class: class Companion Cause: Don't know how to generate outer expression: Class: class Companion File being compiled at position: (17,19) in /Line_1.kts The root cause was thrown at: CodegenContext.java:219 File being compiled at position: mock:///Line_1.kts The root cause was thrown at: ExpressionCodegen.java:319
Or another error related to type inference (which has no problems outside the script)
I don't really understand whats going on atm

@pakoito
Copy link
Member

pakoito commented Feb 24, 2019

Do you know what file is that for?

@1Jajen1
Copy link
Member Author

1Jajen1 commented Feb 24, 2019

Do you know what file is that for?

The scary internal error is for Decidable's README and the other type inference error happens for all snippets where Serializer's contramap is defined without this@contramap (every snippet in kdoc for divide etc)

@pakoito
Copy link
Member

pakoito commented Feb 24, 2019

I'd move the setup code to the arrow-docs project instead. You'll probably get codegen right with it too.

@pakoito
Copy link
Member

pakoito commented Feb 24, 2019

@1Jajen1
Copy link
Member Author

1Jajen1 commented Feb 24, 2019

Yeah this fixed it. Too bad the examples now cannot be viewed in full, but better than no examples at all

@pakoito pakoito merged commit 456623b into arrow-kt:master Feb 24, 2019
@@ -0,0 +1,44 @@
package com.example.domain
Copy link
Member

Choose a reason for hiding this comment

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

@1Jajen1 I didn't see this before merging. Could you please make it a nicer package?

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

Successfully merging this pull request may close these issues.

2 participants