[Haskell-cafe] "Generating" type synonyms without Template Haskell

Niklas Haas haskell at nand.wakku.to
Fri Jun 27 20:30:45 UTC 2014


On Fri, 27 Jun 2014 17:52:39 +0100, Tom Ellis <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk> wrote:
> I'd like to know how to conveniently generate specialisations of a
> product type without resorting to Template Haskell.
> 
> Concretely, I have a type like
> 
>     data MyProduct a b c = MyProduct { foo :: a
>                                      , bar :: b
>                                      , baz :: c }
> 
> and I use it in type signatures in various different specialisations.
> Generally the "base" type of each component stays fixed but it is
> wrapped in zero or more type constructors.  For example
> 
>     a. MyProduct Int Bool String
> 
>     b. MyProduct (Maybe Int) (Maybe Bool) (Maybe String)
> 
>     c. MyProduct [IO Int] [IO Bool] [IO String]
> 
>     d. MyProduct (P Int) (P Bool) (P String)

Using LiberalTypeSynonyms and your original MyProduct, we can define

type Example f = MyProduct (f Int) (f Bool) (f String)

type Id x = x
type ListIO x = [IO x]

type A = Example Id
type B = Example Maybe
type C = Example ListIO
type D = Example P


More information about the Haskell-Cafe mailing list