-
Notifications
You must be signed in to change notification settings - Fork 454
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
Conversation
…g with default and named parameters.
Codecov Report
@@ Coverage Diff @@
## master #120 +/- ##
=========================================
Coverage ? 55.46%
Complexity ? 199
=========================================
Files ? 80
Lines ? 1161
Branches ? 165
=========================================
Hits ? 644
Misses ? 449
Partials ? 68
Continue to review full report at Codecov.
|
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 { |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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())) |
There was a problem hiding this comment.
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>> { |
There was a problem hiding this comment.
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>> { |
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
This PR adds some missing functions and equality check required for some monads to be lawful.