one-liner-instances

Generics-based implementations for common typeclasses

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

LTS Haskell 23.27:0.1.3.0
Stackage Nightly 2025-07-13:0.1.3.0
Latest on Hackage:0.1.3.0

See all snapshotsone-liner-instances appears in

BSD-3-Clause licensedbyJustin Le
Maintained by[email protected]
This version can be pinned in stack with:one-liner-instances-0.1.3.0@sha256:eddc22122bbce5e4bf12f646187730f504b83741126154b12e9e8c0edeb78cbc,1438

Module documentation for 0.1.3.0

one-liner-instances

This package uses machinery fromone-liner in order to provide defaultimplementations for methods fromNum,Fractional,Floating,Semigroup,Monoid,Bounded,Eq,Ord, andRandom. These will work for any types(derivingGeneric) whose fields are all instances of that typeclass.

ForNum,Fractional,Floating,Semigroup, andMonoid, the types alsomust have only a single constructor.Random methods offer variants withsingle constructors (for performance) and with multiple constructors.

So,gPlus (generic addition) will work for:

data Tup1 a b = Tup1 a b            -- requires Num a, Num bdata Tup2 a   = Tup2 Int a          -- requires Num a, Num bdata Tup3     = Tup3 Int Doubledata 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 toWrappedMonoid andWarppedMonad frombase, some convenientnewtype wrappers are provided that will give free instances ofNum, etc. forappropriate types:

Ifa is a data type (derivingGeneric) with a single constructor whosefields all have instances ofNum, thenGNum a has aNum instance (andsame forFractional,Floating, etc.).

Ifa is a data type (derivingGeneric) with a single constructor whosefields all have instances ofSemigroup, thenGMonoid a has aSemigroupinstance (and same forMonoid).

Ifa is a data type (derivingGeneric) whose fields all have instances ofBounded, thenGBounded a has aBounded instance.

Ifa is a data type (derivingGeneric) whose fields all have instances ofEq, thenGOrd a has aEq instance (and same forOrd).

Comparisons

This package provides very similar functionality togeneric-deriving.

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

generic-deriving creates aseparate “deriving” typeclass for everytypeclass one wants to generalize. So, there is a separateGMonoidtypeclass, a separateGEnum typeclass, etc.

one-liner instead creates a single typeclass (ADTRecord andConstraints)to unify all generalizable typeclasses. Both the genericMonoid and genericNum instances are built upon the sameConstraints typeclass. From ausability standpoint,one-liner allows one to easily create generic versionsof their own, custom typeclasses – something thatgeneric-deriving does nothelp with.

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

The main difference in practical usability betweenone-liner-instances andgeneric-deriving themselves are few, but are mainly:

  • one-liner-instances has generic implementations forNum/Fractional/Floating, andgeneric-deriving doesn’t. This is asuperficial difference, however, since nothing fundamental is preventinggeneric-deriving from adding them in the future.

  • one-liner-instances provides newtype wrappers that can automaticallyimbue appropriate types with instances, which can be used with the upcomingDerivingVia syntax to automatically derive instances, or just used ontheir own for convenience purposes.

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

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

Changes

Changelog

Version 0.1.3.0

Nov 20, 2021

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

  • Fixed builds forrandom-1.2
  • Fixed warnings forbase-4.11
  • Added support for newSystem.Random.Stateful instances forrandom library

Version 0.1.2.1

Apr 21, 2018

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

  • Forgot to exportSystem.Random.OneLiner!

Version 0.1.2.0

Apr 20, 2018

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

  • Generic instances forRandom from therandom package.

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 forBounded,Ord, andEq.
  • 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