Tristan Ravitch | 13 Feb 18:01 2013

Free lunch with GADTs

Hi cafe,

I'm playing around with GADTs and was hoping they would let me have my
cake and eat it too.  I am trying to use GADTs to tag a type with some
extra information.  This information is very useful in a few cases,
but the more common case is that I just have a list of items without
the tags.  Ideally I'll be able to recover the tags from the common
case via pattern matching.  What I have right now is:

{-# LANGUAGE GADTs, ExistentialQuantification, EmptyDataDecls, RankNTypes, LiberalTypeSynonyms #-}
data Tag1
data Tag2

data T tag where
  T1 :: Int -> T Tag1
  T2 :: Double -> T Tag2

f1 :: [T t] -> Int
f1 = foldr worker 0
    worker (T1 i) a = i+a
    worker _ a = a

f2 :: [T Tag1] -> Int
f2 = foldr worker 0
    worker :: T Tag1 -> Int -> Int
    worker (T1 i) a = a + i

In f2 I can work with just values with one tag, but f1 is also
(Continue reading)

Mark Flamer | 19 Feb 22:53 2013

Re: Free lunch with GADTs

Tristan, Please let me know how this works out for you. I was struggling with
something similar over the last few days. It's expressed in these 2 S.O.
questions. In my case I'm going to probably just move on and stick with a
standard ADT for now. It was interesting to explore the possibilities

View this message in context:
Sent from the Haskell - Haskell-Cafe mailing list archive at