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

Add fixes to Monads found by new Law checks #120

Merged
merged 14 commits into from
Jun 27, 2017
Merged

Add fixes to Monads found by new Law checks #120

merged 14 commits into from
Jun 27, 2017

Conversation

pakoito
Copy link
Member

@pakoito pakoito commented Jun 25, 2017

This PR adds some missing functions and equality check required for some monads to be lawful.

@codecov-io
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (master@970bacf). Click here to learn what that means.
The diff coverage is 45.45%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #120   +/-   ##
=========================================
  Coverage          ?   55.46%           
  Complexity        ?      199           
=========================================
  Files             ?       80           
  Lines             ?     1161           
  Branches          ?      165           
=========================================
  Hits              ?      644           
  Misses            ?      449           
  Partials          ?       68
Impacted Files Coverage Δ Complexity Δ
.../src/main/kotlin/kategory/instances/OptionMonad.kt 70% <ø> (ø) 0 <0> (?)
...gory/src/main/kotlin/kategory/instances/IdMonad.kt 71.42% <0%> (ø) 0 <0> (?)
...rc/main/kotlin/kategory/instances/TryMonadError.kt 83.33% <100%> (ø) 0 <0> (?)
...main/kotlin/kategory/instances/Function0Bimonad.kt 71.42% <25%> (ø) 0 <0> (?)
...ategory/src/main/kotlin/kategory/typeclasses/Eq.kt 60% <60%> (ø) 0 <0> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 970bacf...f4fa45c. Read the comment docs.

@pakoito
Copy link
Member Author

pakoito commented Jun 25, 2017

Note that the Eq instance in the laws cannot be a default value because of this bug: https://youtrack.jetbrains.com/issue/KT-18660

@@ -0,0 +1,18 @@
package kategory

interface Eq<in F> : Typeclass {
Copy link
Member

Choose a reason for hiding this comment

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

👏

!eqv(a, b)

companion object {
operator fun <F> invoke() = object : Eq<F> {
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be a default instance of Eq[Any] outside of this object.

@@ -10,7 +10,7 @@ import org.junit.runner.RunWith
class EitherTTest : UnitSpec() {
init {

testLaws(MonadLaws.laws(EitherTMonad<Id.F, Int>()))
testLaws(MonadLaws.laws(EitherTMonad<Id.F, Int>(), Eq()))
Copy link
Member

Choose a reason for hiding this comment

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

Probably better named as EqAny

@@ -8,7 +8,10 @@ import org.junit.runner.RunWith
class EvalTest : UnitSpec() {
init {

testLaws(MonadLaws.laws(Eval))
testLaws(MonadLaws.laws(Eval, object : Eq<HK<Eval.F, Int>> {
Copy link
Member

Choose a reason for hiding this comment

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

We should have this instance in the core not just in tests and there should be an instance for each one of the datatypes we implement.

@@ -8,7 +8,10 @@ import org.junit.runner.RunWith
class Function0Test : UnitSpec() {
init {

testLaws(MonadLaws.laws(Function0))
testLaws(MonadLaws.laws(Function0, object : Eq<HK<Function0.F, Int>> {
Copy link
Member

Choose a reason for hiding this comment

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

We should have this instance in the core not just in tests.

@@ -2,3 +2,5 @@ package kategory

data class Law(val name: String, val test: () -> Unit)

inline fun <reified A> A.equalUnderTheLaw(b: A, eq: Eq<A> = Eq()): Boolean =
Copy link
Member

Choose a reason for hiding this comment

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

Can we have extension functions that are inline reify... <F, ..> Once we have instances for the datatypes as part of our effort to provide syntax all datatypes will include syntax for eqv and neqv which can be used directly here provided thre is a registeres Eq instance for the datatype being compared.

Law("Monad Laws: kleisli left identity", { kleisliLeftIdentity(M) }),
Law("Monad Laws: kleisli right identity", { kleisliRightIdentity(M) }),
Law("Monad Laws: map / flatMap coherence", { mapFlatMapCoherence(M) }),
Law("Monad / JVM: stack safe", { mapFlatMapCoherence(M) })
Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

@raulraja raulraja merged commit 51426be into master Jun 27, 2017
@raulraja raulraja deleted the paco-lawfixes branch June 27, 2017 10:55
@pakoito pakoito mentioned this pull request Jun 27, 2017
@wiyarmir wiyarmir mentioned this pull request Jul 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants