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

Weakening & strengthening discard functions forms a monoid: we should test this. #210

Closed
barrucadu opened this issue Feb 16, 2018 · 0 comments · Fixed by #253
Closed

Weakening & strengthening discard functions forms a monoid: we should test this. #210

barrucadu opened this issue Feb 16, 2018 · 0 comments · Fixed by #253

Comments

@barrucadu
Copy link
Owner

weakenDiscard and strengthenDiscard each form a commutative monoid. In fact, they correspond to the monoid operation which picks the least (or greatest) element, according to some ordering. Each has a unit:

  • For weakenDiscard it's the strongest: const (Just DiscardResultAndTrace)
  • For strengthenDiscard it's the weakest: const Nothing

I think the properties we want to check are:

-- ordering (for appropriately-defined min/max functions):
\d1 d2 a -> weakenDiscard     d1 d2 a = min (d1 a) (d2 a)
\d1 d2 a -> strengthenDiscard d1 d2 a = max (d1 a) (d2 a)

-- commutativity
\d1 d2 a -> weakenDiscard     d1 d2 a == weakenDiscard     d2 d1 a
\d1 d2 a -> strengthenDiscard d1 d2 a == strengthenDiscard d2 d1 a

-- identity
\d a -> weakenDiscard     (const (Just DiscardResultAndTrace)) d a == d a
\d a -> strengthenDiscard (const Nothing)                      d a == d a

These could also serve as nice documentation, if presented well. Perhaps we should even add newtype'd monoids!

newtype Weaken     { getWeakDiscarder   :: Either Failure a -> Maybe Discard }
newtype Strengthen { getStrongDiscarder :: Either Failure a -> Maybe Discard }

instance Semigroup Weaken     where ...
instance Monoid    Weaken     where ...
instance Semigroup Strengthen where ...
instance Monoid    Strengthen where ...

weakenDiscard     d1 d2 = getWeakDiscarder   (Weaken     d1 <> Weaken     d2)
strengthenDiscard d1 d2 = getStrongDiscarder (Strengthen d1 <> Strengthen d2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant