Skip to content

More class instances for Identity. #54

Merged
blamario merged 4 commits intoblamario:masterfrom
jonathanknowles:jonathanknowles/more-instances-for-identity
Jan 11, 2025
Merged

More class instances for Identity. #54
blamario merged 4 commits intoblamario:masterfrom
jonathanknowles:jonathanknowles/more-instances-for-identity

Conversation

@jonathanknowles
Copy link
Contributor

This PR defines more class instances for the Identity type.

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

newtype Identity a = Identity { runIdentity :: 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 Identity.
  • adds basic test coverage for all Identity class instances.

This commit removes trailing whitespace from a few files. In the
interest of not polluting other commits, all such changes are grouped
into this commit.
This commit uses `GeneralizedNewtypeDeriving` to make the `Identity`
newtype an instance of the following classes:

- `DistributiveGCDMonoid`
- `DistributiveLCMMonoid`
- `Factorial`
- `FactorialMonoid`
- `GCDMonoid`
- `LCMMonoid`
- `LeftDistributiveGCDMonoid`
- `LeftGCDMonoid`
- `Monus`
- `OverlappingGCDMonoid`
- `RightDistributiveGCDMonoid`
- `RightGCDMonoid`

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

```
newtype Identity a = Identity { runIdentity :: 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 uses `GeneralizedNewtypeDeriving` to simplify all
pre-existing class instance definitions for the `Identity` newtype.
@blamario
Copy link
Owner

My intention was to keep the language extensions minimal in this library, but I guess that ship has sailed a long time ago.

@blamario blamario merged commit a69b060 into blamario:master Jan 11, 2025
11 checks passed
@jonathanknowles jonathanknowles deleted the jonathanknowles/more-instances-for-identity branch January 11, 2025 04:20
@jonathanknowles
Copy link
Contributor Author

@blamario thanks for merging this!

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