MyNixOS website logo
Description

Family of families: featherweight defunctionalization.

Promote regular type families to first-class, without polluting the type namespace.

See README.

Family of families: featherweight defunctionalization

Motivation

This package transforms regular type families into "first-class families". It does so without declaring dummy data types as defunctionalization symbols, avoiding namespace pollution.

Featherweight defunctionalization

The idea is to use strings. There is an unambiguous way to refer to an existing type family: by its fully qualified name---a triple of a package name, module name, and base name.

GHC.TypeNats.+
-- as a Name --
MkName "base" "GHC.TypeNats" "+"

All type families can be promoted to first-class using a single symbol indexed by qualified names. That is the Family of type families.

  • No namespace pollution: Promoting a type family requires no new data type, only an Eval instance for its name.
  • Decentralization: Every type family induces a unique Eval instance, and multiple packages may declare the same instance without conflicts.
type instance Eval (Family_ (MkName "base" "GHC.TypeNats" "+") _ '(x, '(y, '()))) = x + y

For more details on this encoding, see the module Fcf.Family.

Usage

Promote a type family

To promote a type family, the necessary boilerplate can be generated with Template Haskell, using the function Fcf.Family.TH.fcfify.

fcfify ''MyTypeFamily

-- Examples
fcfify ''(GHC.TypeNats.+)
fcfify ''Data.Type.Bool.If

This generates instances of Eval and auxiliary type families for that former instance to be well-defined.

Use the promoted type family

This allows using the defunctionalization symbol Family indexed by the type family's name---a tuple of strings constructed with MkName.

  Eval (Family (MkName "base" "GHC.TypeNats" "+") P0 '(1, '(2, '())))
= 1 + 2
= 3

  Eval (Family (MkName "base" "GHC.Type.Bool" "If") P1 '( 'True, '("yes", '("no", '()))))
= If 'True "yes" "no"
= "yes"

The familyName function converts the name of an actual family to an fcf-family name in Template Haskell.

  Eval (Family $(familyName ''+) P0 '(1, '(2, '())))
= 3

  Eval (Family $(familyName ''If) P1 '( 'True, '("yes", '("no", '()))))
= "yes"

Existing instances

  • The library fcf-base contains instances for type families defined in base.
  • The module Fcf.Family.Meta contains the instance for Eval.

Limitations

  • There is partial support for dependently typed type families. The kind of the result may depend on its arguments. This package does not yet support type families where the kind of arguments depends on other visible arguments.
  • Invisible arguments are currently all assumed to be of kind Type. Lifting this restriction requires either access to more typing information from the compiler via TH, or some implementation of kind inference in TH.
Metadata

Version

0.2.0.1

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