@hackage matchable-th0.1.2.1

Generates Matchable instances using TemplateHaskell

matchable-th

This package provides TemplateHaskell functions to generate instances of Matchable and Bimatchable type classes, which are from matchable package.

Examples

{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TemplateHaskell #-}

import Data.Functor.Classes (Eq1(..))
import Data.Matchable
import Data.Matchable.TH

newtype G a = G [(a, Int, a)]
  deriving (Show, Eq, Functor)

$(deriveMatchable ''G)

-- @deriveMatchable@ generates a @Matchable@ instance only,
-- so you also have to declare @Functor G@ and @Eq1 G@.
-- There is a handy @DeriveFunctor@ extension.
-- Also, you can use @liftEqDefault@ to easily implement @liftEq@.
instance Eq1 G where
  liftEq = liftEqDefault
{-# LANGUAGE TemplateHaskell #-}

import Data.Functor.Classes (Eq2(..))
import Data.Bimatchable
import Data.Matchable.TH

-- Most simple case
data BiF a b = BiF0 | BiF1 a b
  deriving (Show, Eq)

$(deriveBimatchable ''BiF)

instance Eq a => Eq1 (BiF a) where
  liftEq = liftEq2Default (==)

instance Eq2 BiF where
  liftEq2 = liftEq2Default

instance Functor (BiF a) where
  fmap = bimapRecovered id

instance Bifunctor BiF where
  bimap = bimapRecovered

-- Test case for using [], tuple, and another Bimatchable instance
data BiG a b = BiG0 | BiG1 [a] [b] | BiG2 (Int, BiF a b)
  deriving (Show, Eq)

$(deriveBimatchable ''BiG)

instance Eq a => Eq1 (BiG a) where
  liftEq = liftEq2Default (==)

instance Eq2 BiG where
  liftEq2 = liftEq2Default

instance Functor (BiG a) where
  fmap = bimapRecovered id

instance Bifunctor BiG where
  bimap = bimapRecovered