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

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Fri Jun 27 22:55:42 UTC 2014


On Fri, Jun 27, 2014 at 10:30:45PM +0200, Niklas Haas wrote:
> 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

Oh, that looks like exactly what I wanted!  I didn't know about
LiberalTypeSynonyms.

Thanks a lot,

Tom


More information about the Haskell-Cafe mailing list