-
Notifications
You must be signed in to change notification settings - Fork 24
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
Laws didn't hold for BoundedEnum #23
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,10 +37,12 @@ derive newtype instance ordCardinality :: Ord (Cardinality a) | |
-- | Type class for enumerations. | ||
-- | | ||
-- | Laws: | ||
-- | - `succ a > pred a` | ||
-- | - `pred a < succ a` | ||
-- | - `pred >=> succ >=> pred = pred` | ||
-- | - `succ >=> pred >=> succ = succ` | ||
-- | - Successor: `maybe true (a < _) (succ a)` | ||
-- | - Predecessor: `maybe true (_ < a) (pred a)` | ||
-- | - Succ retracts pred: `pred a >>= succ >>= pred = pred a` | ||
-- | - Pred retracts succ: `succ a >>= pred >>= succ = succ a` | ||
-- | - Non-skipping succ: `b <= a || maybe false (_ <= b) (succ a)` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I'm not sure either. On the one hand, I have to unpack the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know what I'd like to see in the specs, but for -quickcheck-laws, how about something like this? My beginner mind has less problems parsing it than the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just an observation: your If maybe x f y
-- becomes
case y of
Just z -> f z
Nothing -> x I think it's probably best that the checks in quickcheck-laws match as closely as is reasonable with the actual laws in the documentation, though; in this case I think I would stick with the way they're written. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly - But that's a little academic because in this case I think the best approach is to write the checks the same way as the actual laws, as then it's very easy to verify that the checks are actually equivalent to the laws. If the laws are unnecessarily hard to understand we should consider changing the laws themselves so this is no longer the case. |
||
-- | - Non-skipping pred: `a <= b || maybe false (b <= _) (pred a)` | ||
class Ord a <= Enum a where | ||
succ :: a -> Maybe a | ||
pred :: a -> Maybe a | ||
|
@@ -148,8 +150,8 @@ downFrom = unfoldr (map diag <<< pred) | |
-- | - ```pred top >>= pred >>= pred ... pred [cardinality - 1 times] == bottom``` | ||
-- | - ```forall a > bottom: pred a >>= succ == Just a``` | ||
-- | - ```forall a < top: succ a >>= pred == Just a``` | ||
-- | - ```forall a > bottom: fromEnum <$> pred a = Just (fromEnum a - 1)``` | ||
-- | - ```forall a < top: fromEnum <$> succ a = Just (fromEnum a + 1)``` | ||
-- | - ```forall a > bottom: fromEnum <$> pred a = pred (fromEnum a)``` | ||
-- | - ```forall a < top: fromEnum <$> succ a = succ (fromEnum a)``` | ||
-- | - ```e1 `compare` e2 == fromEnum e1 `compare` fromEnum e2``` | ||
-- | - ```toEnum (fromEnum a) = Just a``` | ||
class (Bounded a, Enum a) <= BoundedEnum a where | ||
|
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.
This can alternatively be written
all (a < _) (succ a)
, which I think sums things up nicely: if there is a successor, it's greater than the number you first had. In other words, every successor is greater.