MyNixOS website logo
Description

Create strippable HKD via TH.

Please see Data.Barbie.TH

barbies-th

Hackage Haskell CI

A wrapper library for barbies to generate strippable HKDs. It transforms the following declaration

declareBareB [d|
  data Foo = Foo
    { foo :: Int
    , bar :: String
    }  |]

into:

data Foo sw h = Foo
    { foo :: Wear sw h Int,
    , bar :: Wear sw h String
    }
instance BareB Foo
instance FieldNamesB (Foo Covered) where
  bfieldNames = Foo (Const "foo") (Const "bar")
instance ProductB (Foo Covered) where
  bprod (Foo xfoo xbar) (Foo yfoo ybar)
    = Foo (Pair xfoo yfoo) (Pair xbar ybar)
instance FunctorB (Foo Covered) where ...
instance TraversableB (Foo Covered) where ...
instance ConstraintsB (Foo Covered)
instance ProductBC (Foo Covered)

passthroughBareB generates a type synonym for the bare type, preserving the semantics of the original definition.

data FooB sw h = Foo
    { foo :: Wear sw h Int,
    , bar :: Wear sw h String
    }
type Foo = Foo Bare Identity
type FooH = Foo Covered

Typically you need the following extensions to make declareBareB work:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}

GHC sometimes takes very long time to compile code with generically derived instances, and it often fails to inline functions properly too. This package generates most instance methods by TH, reducing large amount of compilation time of the declarations and use sites.

Unlike higgledy which relies on in-memory representation using GHC.Generic, you don't have to worry about the performance, and you can benefit from various language features (e.g. -Wmissing-fields, RecordWildCards etc) even in higher-kinded form.

Deriving pass-through

stock deriving does not work on HKDs. Instead, it transforms deriving clauses into standalone ones via the Barbie wrapper, as well as ones for the Bare counterpart. For example,

data Foo = ... deriving (Show, Eq)

generates

deriving instance Show (Foo Bare Identity)
deriving instance Eq (Foo Bare Identity)
deriving via Barbie (Foo Covered) h instance Show (Barbie (Foo Covered) h) => Show (Foo Covered h)
deriving via Barbie (Foo Covered) h instance Eq (Barbie (Foo Covered) h) => Eq (Foo Covered h)

Note that Barbies module must be imported manually in order to make use of coercions.

Matryoshka barbies

Barbies can contain other barbies if they're declared in the same splice, it pretty much works as you'd expect.

declareBareB [d|
  data Inner = Inner
    { inner :: Int
    }
  data Outer = Outer
    { outer :: Inner
    , other :: Bool
    }
|]

into:

data Inner sw h = Inner
    { inner :: Wear sw h Int
    }
data Outer sw h = Outer
    { outer :: Inner sw h
    , other :: Wear sw h Bool
    }
-- And all the instances as above
Metadata

Version

0.1.11

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