Skip to content

Conversation

@jonathanknowles
Copy link
Contributor

@jonathanknowles jonathanknowles commented Jan 13, 2025

This PR defines more class instances for the Const type.

The approach used is similar to base, which uses GeneralizedNewtypeDeriving to auto-derive instances of Semigroup and Monoid for Const:

newtype Const a b = Const { getConst :: a }
    deriving ( ...
             , Semigroup  -- ^ @since base-4.9.0.0
             , Monoid     -- ^ @since base-4.9.0.0
             , ...
             )

(See source here.)

In addition, this PR:

  • uses GeneralizedNewtypeDeriving to simplify the remaining existing class instances for Const.
  • adds test coverage for Const class instances.

And finally:

This commit uses `GeneralizedNewtypeDeriving` to simplify existing class
instance definitions for `Const`.

The approach used is similar to that used in `base`, which uses
`GeneralizedNewtypeDeriving` for `Semigroup` and `Monoid` (among others):

```hs
newtype Const a b = Const { getConst :: a }
    deriving ( ...
             , Semigroup  -- ^ @SInCE base-4.9.0.0
             , Monoid     -- ^ @SInCE base-4.9.0.0
             , ...
             )
```

In practice, this mechanism generates method definitions that use
zero-cost coercions (see `Data.Coerce`).
This commit makes `Const` an instance of the following classes:

- `DistributiveGCDMonoid`
- `DistributiveLCMMonoid`
- `Factorial`
- `FactorialMonoid`
- `GCDMonoid`
- `LCMMonoid`
- `LeftDistributiveGCDMonoid`
- `LeftGCDMonoid`
- `Monus`
- `OverlappingGCDMonoid`
- `RightDistributiveGCDMonoid`
- `RightGCDMonoid`
- `StableFactorial`
Copy link
Owner

@blamario blamario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you!

@blamario blamario merged commit d0cb954 into blamario:master Jan 14, 2025
11 checks passed
@jonathanknowles jonathanknowles deleted the jonathanknowles/const branch January 15, 2025 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants