[Haskell-cafe] question about GADT and deriving automatically a Show instance
TP
paratribulations at free.fr
Fri May 17 23:11:17 CEST 2013
Adam Gundry wrote:
[...]
> To use standalone deriving, you need to write
>
>> deriving instance Show (Person gender)
>
> and everything will work fine. By writing
>
>> instance Show (Person gender)
>
> you are instead giving an instance with no methods, and the default
> methods in the Show class contain a loop (so that one can define either
> show or showsPrec).
Thanks a lot. I did not remember that "Standalone Deriving" has a meaning as
a GHC extension. My idea was correct, but the employed syntax was incorrect.
Just for reference (future Google search by others), the corresponding link
in the GHC documentation:
http://www.haskell.org/ghc/docs/7.6.2/html/users_guide/deriving.html
> P.S. You might like to check out the new DataKinds extension, which
> would allow Male and Female to be data constructors rather than type
> constructors.
Thanks a lot for pointing out this subject (it compells me to work on it - I
am not an advanced user of GHC).
I have just tried to understand all this stuff about type and data
constructor promotion:
http://www.haskell.org/ghc/docs/7.6.1/html/users_guide/promotion.html
If I understand well, your idea is to avoid letting someone write:
let a = Alive "Joe" 60 Dead :: Person Int
which is nonsense (a Person cannot have a gender of type "Integer"), but
legal code.
I have tried to use the technique described at the beginning of the article
"Given Haskell a Promotion", but I'm stuck. See the code below. My problem
is that now Gender is a kind, no more a type, such that I cannot use it in
the type definition of the GADT; but I am compelled to write something after
"::", and I cannot write for instance "Dead :: Person Male" because I want a
dead person to be either a man or woman, of course. In fact, what I need is
Gender both as a type and as a kind, if I am correct? What do I miss?
So the following version does not work:
----------------------------------------
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
data Gender = Male | Female
data Person :: Gender -> * where
Dead :: Person Gender -- WHAT DO I PUT HERE
Alive :: { name :: String
, weight :: Float
, father :: Person Gender } -> Person Gender
deriving instance Show (Person Gender)
main = do
let a = Alive "Joe" 60 Dead :: Person Male
let b = Alive "Jim" 70 a :: Person Male
print a
print b
----------------------------------------
How to modify it?
Thanks a lot,
TP
More information about the Haskell-Cafe
mailing list