MyNixOS website logo
Description

Generics-based implementations for common typeclasses.

Provides generics-based implementations for common typeclasses using Generics.

Please see the README on Github at https://github.com/mstksg/one-liner-instances#readme

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, Ord, and Random. 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. Random methods offer variants with single 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 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.

Metadata

Version

0.1.3.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows