one-liner-instances

Generics-based implementations for common typeclasses

https://github.com/mstksg/one-liner-instances#readme

Version on this page:0.1.1.0
LTS Haskell 22.14:0.1.3.0
Stackage Nightly 2024-03-28:0.1.3.0
Latest on Hackage:0.1.3.0

See all snapshots one-liner-instances appears in

BSD-3-Clause licensed by Justin Le
Maintained by [email protected]
This version can be pinned in stack with:one-liner-instances-0.1.1.0@sha256:f394fea49c433116235506e9d8f09f3783ecfa469b40ce814d0c8a31c120b360,1538

Module documentation for 0.1.1.0

Depends on 2 packages(full list with versions):

one-liner-instances

This package uses machinery from one-liner in order to provide default implementations for methods from Num, Fractional, Floating, Semigroup, Monoid, Bounded, Eq, and Ord. These will work for any types (deriving Generic) whose fields are all instances of that typeclass.

For Num, Fractional, Floating, Semigroup, and Monoid, the types also must have only a single constructor.

So, gPlus (generic addition) will work for:

data Tup1 a b = Tup1 a b            -- requires Num a, Num b
data Tup2 a   = Tup2 Int a          -- requires Num a, Num b
data Tup3     = Tup3 Int Double
data Tup4 a b = Tup4 Int Double     -- no constraint on a or b

But not on:

data Tup5 a   = Tup2 String a       -- String is not an instance of Num

These are implemented by applying the operation to every field.

Newtype wrappers

Similar to WrappedMonoid and WarppedMonad from base, some convenient newtype wrappers are provided that will give free instances of Num, etc. for appropriate types:

If a is a data type (deriving Generic) with a single constructor whose fields all have instances of Num, then GNum a has a Num instance (and same for Fractional, Floating, etc.).

If a is a data type (deriving Generic) with a single constructor whose fields all have instances of Semigroup, then GMonoid a has a Semigroup instance (and same for Monoid).

If a is a data type (deriving Generic) whose fields all have instances of Bounded, then GBounded a has a Bounded instance.

If a is a data type (deriving Generic) whose fields all have instances of Eq, then GOrd a has a Eq instance (and same for Ord).

Comparisons

This package provides very similar functionality to generic-deriving.

There are a few major design differences between generic-deriving and one-liner, the package that this one is built on.

generic-deriving creates a separate “deriving” typeclass for every typeclass one wants to generalize. So, there is a separate GMonoid typeclass, a separate GEnum typeclass, etc.

one-liner instead creates a single typeclass (ADTRecord and Constraints) to unify all generalizable typeclasses. Both the generic Monoid and generic Num instances are built upon the same Constraints typeclass. From a usability standpoint, one-liner allows one to easily create generic versions of their own, custom typeclasses – something that generic-deriving does not help with.

one-liner-instances, however, is simply a package using the one-liner engine to provide generic instances for common classes where it is possible.

The main difference in practical usability between one-liner-instances and generic-deriving themselves are few, but are mainly:

  • one-liner-instances has generic implementations for Num/Fractional/Floating, and generic-deriving doesn’t. This is a superficial difference, however, since nothing fundamental is preventing generic-deriving from adding them in the future.

  • one-liner-instances provides newtype wrappers that can automatically imbue appropriate types with instances, which can be used with the upcoming DerivingVia syntax to automatically derive instances, or just used on their own for convenience purposes.

    generic-deriving does not aim to do this at this moment.

  • Integrates with the rest of the one-liner ecosystem, if one is already using it to provide constraints for custom typeclasses.

Changes

Changelog

Version 0.1.1.0

Feb 5, 2018

https://github.com/mstksg/one-liner-instances/releases/tag/v0.1.1.0

  • Newtype instances actually didn’t work before! They do now :)
  • Added generic implementations and newtypes for Bounded, Ord, and Eq.
  • Some aggressive inlining in all modules.
  • Added clarification and comparisons to other libraries to README

Version 0.1.0.0

Feb 3, 2018

Deprecated

https://github.com/mstksg/one-liner-instances/releases/tag/v0.1.0.0

  • Initial release