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

Avoiding duplicates vs. having the same law included twice in one Properties instance #27

Open
TomasMikula opened this issue Apr 28, 2016 · 2 comments

Comments

@TomasMikula
Copy link

I'm just checking out this library and have two questions (that go against each other):

  1. Are duplicate properties avoided? For example, monad.all inherits apply.all both via bind.all and applicative.all. Is this duplication eliminated?
  2. If the answer to question 1. is yes, can I have the same law included twice in a property set, for different aspects of the type class? For example, if I have a Ring, I want to check the monoid laws for both addition and multiplication.

discipline can achieve both of the above, but I find its approach rather cumbersome and fragile, which is why I wrote my own tool, Principled. Principled, however, relies on universal equality (==) of type classes, which is not ideal either (although the worst that can happen is failure to avoid duplicates, when the same type class is instantiated twice with the same arguments, but == returns false).

I wonder if and how scalaprops deals with this problem.

@xuwei-k
Copy link
Member

xuwei-k commented May 5, 2016

Are duplicate properties avoided?

Yes. scalaprops.Properties use scalaz.Tree. So, most object Properties methods call Properties.distinctTree. Properties.distinctTree eliminate duplication tests. see also PropertiesTest

If the answer to question 1. is yes, can I have the same law included twice in a property set

This is a good question. I haven't found a good way yet. However scalaprops.Properties has a type parameter. It means we can use not only String but also any types for test id.
There are some methods which does NOT call distinctTree (e.g. Properties#product )

@TomasMikula
Copy link
Author

I see, I could make a disjoint union of properties by using a sum type as the ID. For my Ring example above, it could be

sealed trait RingPropId
case class Additive(id: Foo) extends RingPropId // for props from AbelianGroup under addition
case class Multiplicative(id: Foo) extends RingPropId // for props from Monoid under multiplication
case class Proper(id: Bar) extends RingPropId // for props introduced in Ring

Deciding property equality purely on ID means that two properties that are effectively the same, but have different IDs, will not be de-duplicated. Though that should be a rare enough case.

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

No branches or pull requests

2 participants