haskus-utils-variant-3.0: Variant and EADT

Safe HaskellNone
LanguageHaskell2010

Haskus.Utils.EADT.TH

Description

Template-Haskell helpers for EADTs

Synopsis

Documentation

eadtPattern Source #

Arguments

:: Name

Actual constructor (e.g., ConsF)

-> String

Name of the pattern (e.g., Cons)

-> Q [Dec] 

Create a pattern synonym for an EADT constructor

E.g.

data ConsF a e = ConsF a e deriving (Functor)
$(eadtPattern 'ConsF "Cons")

====>

pattern Cons :: ConsF a :<: xs => a -> EADT xs -> EADT xs
pattern Cons a l = VF (ConsF a l)

eadtInfixPattern Source #

Arguments

:: Name

Actual constructor (e.g., ConsF)

-> String

Name of the pattern (e.g., Cons)

-> Q [Dec] 

Create an infix pattern synonym for an EADT constructor

E.g.

data ConsF a e = ConsF a e deriving (Functor)
$(eadtInfixPattern 'ConsF ":->")

====>

pattern (:->) :: ConsF a :<: xs => a -> EADT xs -> EADT xs
pattern a :-> l = VF (ConsF a l)

eadtPatternT Source #

Arguments

:: Name

Actual constructor (e.g., ConsF)

-> String

Name of the pattern (e.g., Cons)

-> Q Type

Type of the EADT (e.g., [t|forall a. List a|])

-> Q [Dec] 

Create a pattern synonym for an EADT constructor that is part of a specified EADT.

This can be useful to help the type inference because instead of using a generic "EADT xs" type, the pattern uses the provided type.

E.g.

data ConsF a e = ConsF a e deriving (Functor)
data NilF    e = NilF      deriving (Functor)

type List a = EADT '[ConsF a, NilF]

$(eadtPatternT 'ConsF "ConsList" [t|forall a. List a|])

====>

pattern ConsList ::
 ( List a ~ EADT xs
 , ConsF a :<: xs
 ) => a -> List a -> List a
pattern ConsList a l = VF (ConsF a l)

Note that you have to quantify free variables explicitly with forall

eadtInfixPatternT Source #

Arguments

:: Name

Actual constructor (e.g., ConsF)

-> String

Name of the pattern (e.g., Cons)

-> Q Type

Type of the EADT (e.g., [t|forall a. List a|])

-> Q [Dec] 

Like eadtPatternT but generating an infix pattern synonym