matchable-th-0.2: Generates Matchable instances using TemplateHaskell
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Matchable.TH

Synopsis

Documentation

deriveInstances :: Q [Dec] -> Q [Dec] Source #

This function transforms multiple instance declarations written in StandaloneDeriving format to instances derived by TemplateHaskell.

Example

  {-# LANGUAGE DeriveFunctor #-}
  {-# LANGUAGE StandaloneDeriving #-}
  [-# LANGUAGE TemplateHaskell #-}
  data Foo a b = Foo a b (Either a b)
     deriving (Show, Eq, Functor)
  

To use deriveInstances for Foo, write as below:

  deriveInstances [d|
    deriving instance Eq a => Eq1 (Foo a)
    deriving instance Eq a => Matchable (Foo a)
    deriving instance Eq2 Foo
    deriving instance Bifunctor Foo
    deriving instance Bimatchable Foo
    |]
  

deriveMatchable :: Name -> Q [Dec] Source #

Build an instance of Matchable for a data type.

e.g.

data Exp a = Plus a a | Times a a
deriveMatchable ''Exp

will create

instance Matchable Exp where
  zipMatchWith f (Plus  l1 l2) (Plus  r1 r2) = pure Plus  * f l1 r1 * f l2 r2
  zipMatchWith f (Times l1 l2) (Times r1 r2) = pure Times * f l1 r1 * f l2 r2
  zipMatchWith _ _ _ = Nothing

deriveBimatchable :: Name -> Q [Dec] Source #

Build an instance of Bimatchable for a data type.

e.g.

data Sum a b = InL a | InR b
deriveMatchable ''Sum

will create

instance Matchable Sum where
  bizipMatchWith f _ (InL l1) (InL r1) = pure InL $ f l1 r1
  bizipMatchWith _ g (InR l1) (InR r1) = pure InR $ g l1 r1