MyNixOS website logo
Description

Generically derived enumerations.

This package provides a way to generically obtain every possible value of a type, provided that the generic representation of the type is compatible.

generic-enumeration

This is a library that provides generics-based enumeration of all possible values for a type.

It differs from deriving Enum in that:

  • It supports non-nullary data constructors.
  • It does not try for integer encoding/decoding. It works by providing a list containing every value, and also providing succMay and predMay analogs to Enum's succ and pred
  • It does not allow user-defined instances. Instances either work generically or they don't.

Because of this it is a little less powerful that Enum in some cases, but it is also a lot more safe. There are no partial functions, and disallowing user-defined instances means that not even a human can mess it up.

Use case

I wrote this to solve a particular kind of problem I often have. When I have a type:

data A
  = A1
  | A2
  | A3
  deriving (Enum, Bounded)

And I want to produce a list of all the values:

allAs :: [A]
allAs = [minBound .. maxBound]

This deriving (Enum, Bounded) and [minBound .. maxBound] dance is a little awkward, and it is not always the case Enum or Bounded is meaningful for the type except as an implementation detail for getting the full list of values. Worse, this doesn't even work for types with non-nullary constructors. E.g. this won't compile:

data A
  = A1 B
  | A2 C
  | A3
  deriving (Enum, Bounded)

data B
  = B1
  | B2
  deriving (Enum, Bounded)

data C
  = C1
  | C2
  deriving (Enum, Bounded)

On the other hand, this will compile just fine:

data A
  = A1 B
  | A2 C
  | A3
  deriving (Generic)

data B
  = B1
  | B2
  deriving (Generic)

data C
  = C1
  | C2
  deriving (Generic)

allAs :: [A]
allAs = enumeration
  -- where `enumeration` is a function from this package.
  -- Has the value `[A1 B1, A1 B2, A2 C1, A2 C2, A3]`
Metadata

Version

0.1.0.3

License

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