Adventures with types in Haskell: GADT: why the following typechecks?

I follow this Simon Peyton-Jones lecture on GADT. The following data type is declared there:

data T a where
  T0 :: Bool -> T Bool
  T1 :: T a

And then the question is what is the type of the following function:

f x y = case x of
          T0 _ -> True
          T1   -> y

It seems to me that the only possible type is:

f :: T a -> Bool -> Bool

However, the following type:

f :: T a -> a -> a

also true! And indeed, you can use fas follows:

 f (T1) "hello"

My question is: why is the signature of the second type fvalid?

+4
source share
2 answers

Usually for type checking

case e of
  K1 ... -> e1
  K2 ... -> e2
  ...

all expressions are required to eihave a common type.

GADT, , T ~ T', , , . , , ei , , , , .

:

f :: T a -> a -> a
f x y = -- we know x :: T a , y :: a
   case x of
      T0 _ -> -- provides a ~ Bool
              True   -- has type Bool
      T1   -> -- provides a ~ a (useless)
              y      -- has type a

Bool ~ a, , , a ~ Bool. !

( , - , , , ( ) - . GADT .)

, GADT - , , -, .

+3

f , :

T0 _ -> True

T Bool, - Bool. , a ~ Bool.

T1 ->  y

T a, y, a. , a.

, , : , f, ? -, a, T a a?

: , . T0 ( a is Bool), Bool. T1, , a. f (T1 :: T Int) "notAnInt", , . : , , . f .

+6

Source: https://habr.com/ru/post/1658233/


All Articles