chener
chener

Reputation: 764

If I just construct data with pure haskell type, what does that mean?

    data Number = Int | Float deriving(Eq,Ord)

What does Int and Float mean here? A constructor or a haskell type?

And how is Bool defined in haskell? LYAH says

We can think Bool implemented like this:

   data Bool = False | True deriving (Ord)

Upvotes: 1

Views: 93

Answers (2)

Random Dev
Random Dev

Reputation: 52280

For the Bool part: you can just ask GHCi:

λ> :i Bool
data Bool = False | True
    -- Defined in `ghc-prim-0.4.0.0:GHC.Types'

so yes - it's indeed defined like this ;)

(sorry about the comment-like answer - it might be borderline - just let me know and I move/delete)


BTW: you can indeed find the Source on Hackage:

module GHC.Types

...

data {-# CTYPE "HsBool" #-} Bool = False | True

Upvotes: 4

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

data Number = Int | Float deriving(Eq,Ord)

In the above Int and Float are just constructors. They have nothing to do with the Int or Float types that can take on values such as 1, or 42.

Prelude> data Number = Int | Float deriving (Show)
Prelude> Int
Int
Prelude> :type Int
Int :: Number
Prelude> Float
Float
Prelude> :type Float
Float :: Number

If you wanted, you could add fields to these constructors so they can carry Int and Float types inside:

Prelude> data Number' = Int' Int | Float' Float deriving (Show)
Prelude> :type Int'
Int' :: Int -> Number'
Prelude> :type Int' 494
Int' 494 :: Number'
Prelude> Int' 42
Int' 42

Upvotes: 4

Related Questions