GADT type inference problem
Let us consider the following simple code
{-# OPTIONS -fglasgow-exts #-}
module Foo where
data Term a where B :: Bool -> Term Bool C :: Term Bool -> Term t -> Term t I :: Int -> Term Int
shw (I t) = ("I "++) . shows t shw (B t) = ("B "++) . shows t shw (C p q) = ("Cnd "++) . (shw p) . (shw q)
It loads in GHCi 6.4 and 6.4.1: Prelude> :l /tmp/g.hs Compiling Foo ( /tmp/g.hs, interpreted ) Ok, modules loaded: Foo. *Foo> :t I 1 I 1 :: Term Int *Foo> :t B True B True :: Term Bool However, when we do *Foo> :t shw shw :: Term Bool -> String -> [Char] The inferred type of shw shows that it takes the values of Term *Bool*. And yet the very first clause of shw mentions (I t), which GHCi correctly reports to be of the type Term Int... It seems the last clause of shw confuses GHCi: if we remove it, the inferred type of shw is "Term a -> String -> [Char]", as one would expect. That is not a type expression printing problem: *Foo> shw (I 1) <interactive>:1:5: Couldn't match `Bool' against `Int' Expected type: Term Bool Inferred type: Term Int In the application `I 1' the first clause of shw notwithstanding...
oleg@pobox.com writes:
Let us consider the following simple code
{-# OPTIONS -fglasgow-exts #-}
module Foo where
data Term a where B :: Bool -> Term Bool C :: Term Bool -> Term t -> Term t I :: Int -> Term Int
shw (I t) = ("I "++) . shows t shw (B t) = ("B "++) . shows t shw (C p q) = ("Cnd "++) . (shw p) . (shw q)
...
However, when we do
*Foo> :t shw shw :: Term Bool -> String -> [Char]
The inferred type of shw shows that it takes the values of Term *Bool*.
As I understand it, GHC can't infer the most general type signature when GADTs are involved. It should work if shw has an explicit type signature, e.g. shw :: Term a -> ShowS What's strange is that GHC accepted the code at all. There would probably be less confusion if GADT arguments always required a signature, like arguments involving higher-rank polymorphism. -- David Menendez <zednenem@psualum.com> | "In this house, we obey the laws <http://www.eyrie.org/~zednenem> | of thermodynamics!"
participants (2)
-
David Menendez -
oleg@pobox.com