user361633
user361633

Reputation: 203

Haskell type synonym issue

This gives me the following error

Not in scope: data constructor Blah

Why? I thought that I can use the type synonym everywhere I can use Person

data Person = Person { weight :: Int, height :: Int }

type Blah = Person

person1 :: Blah
person1 = Blah 80 187 

Upvotes: 3

Views: 225

Answers (2)

wit
wit

Reputation: 1622

In the hottest GHC 7.8 you could write in such manner:

{-# LANGUAGE PatternSynonyms #-}

data Person = Person { weight :: Int, height :: Int }

type Bar = Person     -- type synonym
pattern Baz = Person  -- constructor synonym


person1 :: Bar
person1 = Baz 80 187

But sure, don't forget Person is a type and Person ia a constructor and both are in different scope.

Upvotes: 2

bheklilr
bheklilr

Reputation: 54058

You've aliased the type Person to the name Blah, but the constructor for Person is still Person {weight :: Int, height :: Int}. Type constructors and Type names are different and are even kept in different namespaces in Haskell.

As an example:

> data MyBool = MyFalse | MyTrue deriving (Show, Eq)
> type Blah = MyBool

Here the constructors for MyBool are MyFalse and MyTrue, each with kind * (no type parameters). I then alias MyBool to Blah:

> MyTrue :: MyBool
MyTrue
> MyTrue :: Blah
MyTrue

This should help enforce the idea that while a type's constructor might share the same name as the type itself, they are not the same things.

Upvotes: 6

Related Questions