Kevin Meredith
Kevin Meredith

Reputation: 41939

Data Constructor - Same Types

Given the following data type:

data JoinList m a = Empty
                  | Single m a
                  | Append m (JoinList m a) (JoinList m a)
    deriving (Eq, Show)

Using ghci, I did:

*JoinList> :t Single 5 3
Single 5 3 :: (Num m, Num a) => JoinList m a

Why are two Num types required here? Since both types are Num, then why couldn't we have:

Single 5 3 :: (Num m) => JoinList m m

Upvotes: 1

Views: 72

Answers (1)

bitemyapp
bitemyapp

Reputation: 1647

It's not a given they're the same type.

You allow the types to vary in the definition of JoinList and you didn't assert 5 and 3 were the same Num in a type signature.

GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l joinlist.hs
[1 of 1] Compiling JoinList         ( joinlist.hs, interpreted )
Ok, modules loaded: JoinList.
Prelude> :t Single 5 3
Single 5 3 :: (Num a, Num m) => JoinList m a
Prelude> let same = Single 5 3 :: Num a => JoinList a a
Prelude> :t same
same :: Num a => JoinList a a

Because the literals are polymorphic Num values, they could be two different concrete types under the hood.

Prelude> let diff = Single 5 3 :: JoinList Int Float
Prelude> diff
Single 5 3.0
Prelude> :t diff
diff :: JoinList Int Float

Upvotes: 5

Related Questions