Georg Natter
Georg Natter

Reputation: 25

Haskell - Making Int an instance of my class

When I try to make Int an Instance of my class Gueltig by:

instance Gueltig Int where
    ist_gueltig (Int a) = Ja

, why do I get the error message "Undefined data constructor "Int""? How do I make instances of Int?

Thanks for your help!

Upvotes: 1

Views: 75

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120711

There's nothing to pattern-match here. You don't care about the value of the integer, just that it is an integer. Which is already proven by the compiler before even choosing that instance. So, just make it

instance Gültig Int where
  istGültig _ = Ja

Alternatively you could also write istGültig a = Ja, but variables that aren't used should not have names (in fact, -Wall will trigger a warning if they do).

OTOH, Int a would only be valid if there was a data type like

data Int = Int {intData :: ???}

If fact Int does kind of look like this, but this is a GHC implementation detail:

data {-# CTYPE "HsInt" #-} Int = I# Int#

Here, #Int is a hard-baked machine representation type. It's an unlifted type, meaning it behaves in some regards different from standard Haskell values. You don't want to deal with this unless you really know why.

Normally, you should just treat Int as an abstract type that can't be further unwrapped or anything. Just use it directly with standard numeric / comparison operators.

Upvotes: 8

Related Questions