Comments (20)
I'm sympathetic to this. Would you mind pasting a simple example here (i.e. functor.Invariant[_]
) just so we could see what it would look like?
EDIT: To be clear, I just want to see what trait InvariantLaws[F[_]] { ... }
would look like.
from cats.
@non here is one way to do it: https://github.com/fthomas/cats/compare/wip/move-laws
from cats.
maybe it would read better if we would use syntax for laws?
from cats.
Updated the branch to use syntax for Eq
. It looks a little bit better now.
from cats.
So here's one concern -- if the laws fail you probably get little or no useful output.
In algebra, we used the ?==
operator to get output when the ===
tests fail. Here's the relevant code:
https://github.com/non/algebra/blob/master/laws/src/main/scala/algebra/laws/package.scala#L23
Obviously we don't want core
to depend on ScalaCheck. Is there some way we can put the laws in core while supporting using the ScalaCheck labels to get useful output?
from cats.
I wouldn't call it a solution, but we could let the laws return two things that should be equal instead of a Boolean
:
def invariantIdentity[A](fa: F[A]): (F[A], F[A]) =
(F.imap(fa)(identity[A])(identity[A]), fa)
ScalaCheck could then take care of comparing those two values.
from cats.
Right, or even just return the (A, A, (A, A) => Boolean)
that algebra would be passing to Ops.run
if you see what I mean.
from cats.
You could argue this makes all the laws less readable. I'm willing to create some kind of DSL here, ideally we can have laws that are clearly specified in a useful place, but which also produce useful messages when they fail.
from cats.
I think a small DSL could make the laws as readable as the Boolean
-returning versions. Just with something like type Identity[A] = (A, A)
and ->
it reads a lot better.
def invariantIdentity[A](fa: F[A]): Identity[F[A]] =
F.imap(fa)(identity[A])(identity[A]) -> fa
from cats.
If Identity[A]
is defined as anything other than Identity[A] = A
I think it's going to really confuse people. Other than that a mini DSL sounds fine to me :)
from cats.
I think the benefit of a DSL would be to enable other kinds of laws besides those based on identical pairs, so I think we'd want something slightly different here.
(OTOH if we really only need to test that LHS is equal to RHS, pairs are fine.)
from cats.
I've now moved all current laws next to the type classes in my WIP branch (https://github.com/fthomas/cats/compare/wip/move-laws). The laws are now inner trait
s of the type classes which avoids the evidence parameters and the F.
prefixes. What do you think?
from cats.
I should add that the definitions of the laws in my WIP branch are similiar to how they are defined in scalaz.
from cats.
@fthomas not sure to understand what's the benefit of having the law as inner trait?
from cats.
While I agree that defining the laws in the same source file as the type class has a lot of benefits, it still feels like it's test code that's included in non-test artifacts.
Also, with unidoc, each type class can link directly to a laws trait defined in the laws JAR, providing similar benefits to the suggested solution, without bloating the core JAR.
Ultimately, I wonder if we can separate the laws from their binding to Discipline like in the suggestion above, but still include them in the laws JAR.
from cats.
@julien-truffaut as top-level class:
trait Invariant[F[_]] {
def imap[A, B](fa: F[A])(f: A => B)(fi: B => A): F[B]
}
class InvariantLaws[F[_]](implicit F: Invariant[F]) {
def invariantIdentity[A](fa: F[A]): (F[A], F[A]) =
F.imap(fa)(identity[A])(identity[A]) -> fa
}
as inner trait:
trait Invariant[F[_]] {
def imap[A, B](fa: F[A])(f: A => B)(fi: B => A): F[B]
trait InvariantLaws {
def invariantIdentity[A](fa: F[A]): (F[A], F[A]) =
imap(fa)(identity[A])(identity[A]) -> fa
}
}
The inner trait does not require the context bound implicit F: Invariant[F]
and instead of F.imap
you can just write imap
.
from cats.
@mpilquist In retrospect my main concern was the tight coupling between the laws and discipline code. If there are proper ScalaDoc links from the type classes to the law traits, I think that is also ok for me.
from cats.
I would also prefer the laws to be defined outside the TC trait. I agree with @fthomas a scaladoc link to reference the laws is a good documentation.
from cats.
Here is another proposal: https://github.com/fthomas/cats/compare/wip/move-laws2
- each typeclass has now its dedicated
Laws
class in thecats.laws
package - the
discipline
test code is moved into thecats.laws.discipline
package
What do you think? Is this good enough to open a PR with these changes?
from cats.
Looks good to me
On Feb 8, 2015, at 8:03 AM, "Frank S. Thomas" [email protected] wrote:
Here is another proposal: https://github.com/fthomas/cats/compare/wip/move-laws2
each typeclass has now its dedicated Laws class in the cats.laws package
the discipline test code is moved into the cats.laws.discipline package
What do you think? Is this good enough to open a PR with these changes?—
Reply to this email directly or view it on GitHub.
from cats.
Related Issues (20)
- `Apply` syntax is allocating HOT 2
- add `sizeIs` method like std-lib to `cats.Reducible` and/or NonEmpty collections HOT 1
- Maybe you are looking for `as`? :) HOT 2
- Fix documentation (or design) of Free Monad
- Type-classes for Prepend (Cons) and Append (Snoc) operations HOT 5
- Scala 3: No given instance of type cats.Applicative[[_] =>> Any] was found for parameter F of method apply in class PurePartiallyApplied
- Unusual EqLaws definition HOT 4
- Outdated Link in Docs - Seeking Guidance for Fix HOT 1
- algebra-core/src HOT 1
- Option size method implicitly selected from UnorderedFoldable HOT 2
- IndexedStateT has superfluous parts HOT 4
- .splitWhen
- Difference in the Applicative inferred for Seq[Seq[?]]#sequence between Scala 2 and 3
- Inconsistent behaviour when using Eval as Applicative
- `Tuple1SemigroupalOps` methods have different names from other `TupleNSemigroupalOps` classes HOT 3
- Instances for Currency HOT 7
- EitherT[Option, ?, ?]] can't be used as a bifunctor HOT 5
- trait EuclidianRing should not be a Ring HOT 4
- OutOfMemoryError when IO.uncancelable is used in recursive function HOT 1
- ambiguous implicit resolution of `Show.ContravariantShow[immutable.SortedMap[K, V]]` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cats.