I am quite new to Haskell/Purescript and currently learning by studying the PureScript by Example book.
In chapter 6 about type classes, exercise 4 has following task:
Here is my attempt:(Medium) Given any type
a
with an instance ofOrd
, we can add a new "infinite" value which is greater than any other value:data Extended a = Finite a | Infinite
Write an
Ord
instance forExtended a
which reuses theOrd
instance fora
.
instance ordExtended :: Ord a => Ord (Extended a) where
compare Infinite Infinite = EQ
compare Infinite _ = GT
compare _ Infinite = LT
compare (Finite f1) (Finite f2) = compare f1 f2
Unfortunately, the code triggers an error:
No type class instance was found for
Data.Eq.Eq (Extended a0)
while checking that expression #dict Eq has type { eq :: Extended a0 -> Extended a0 -> Boolean } in value declaration ordExtended
where a0 is a rigid type variable bound at (line 0, column 0 - line 0, column 0) PureScript(NoInstanceFound)
I cannot quite understand the error message:
- What does
expression #dict Eq
mean? There is nodict
in my code. - What is a rigid type variable?
- The error seems to use different identifiers like
a0
(why? I assume, that isa
)
In my book, Eq
type class instance should be covered by implementing Ord
, as Ord
extends Eq
.