generated at
barbies-th
Template Haskellを使って、HKDを通常のレコード宣言から生成するライブラリ。


使い方

declareBareB関数でデータ型宣言を包む。すると、HKDに変換したデータ型が定義され、それに伴った各種インスタンスも導出される。また、deriving節に対応するインスタンスは、元と同型(Bare)の型およびBarbieラッパーを通したものの二種類がそれぞれ定義される。
haskell
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE DerivingVia #-} {-# LANGUAGE UndecidableInstances #-} declareBareB [d| data Foo = Foo { foo :: Int , bar :: String } deriving (Show, Eq)|]

haskell
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) 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)

パススルー定義(v0.1.9)
passthroughBareBは、型シノニムを活用し、元の定義と一致する型を生成する。例えば、
haskell
passthroughBareB [d| data Foo = Foo { foo :: Int , bar :: String } deriving (Show, Eq)|]
haskell
data FooB sw h = Foo { foo :: Wear sw h Int, , bar :: Wear sw h String } type Foo = FooB Bare Identity type FooH = Foo Covered
になる。既存のコードベースに対し、最小限の変更でHKDの導入を可能にする。
GitHub上の最新版で利用可能