Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 669
user361633 Avatar asked Feb 22 '26 23:02

user361633


2 Answers

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.

like image 132
bheklilr Avatar answered Feb 24 '26 18:02

bheklilr


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.

like image 35
wit Avatar answered Feb 24 '26 20:02

wit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!