4
votes

I'm working with the following algebraic data type in PureScript...

data Extended a = Infinite | Finite a

v1 = Finite 11
v2 = Infinite

I'm having trouble figuring out how to pattern match the "Infinite" case, since it appears that v2 has type forall t140. Extended t140. I'm assuming t140 is some sort of placeholder the compiler automatically fills in. The type of v1 is Extended Int. So if I setup an instance of Eq to compare values of Extended, the Infinite case does not match...

instance extendedEq :: (Eq a) => Eq (Extended a) where
  eq (Finite a) (Finite b) = eq a b
  eq Infinite Infinite = true
  eq Infinite _ = false
  eq _ Infinite = false

So when I try and run v2 == v2 I get the error...

No type class instance was found for Prelude.Eq (Extended _0)

Which makes sense, since I imagine it's trying to find an Eq instance for t140.

So my question is, how can I pattern matching on the Infinite type?

1

1 Answers

6
votes

The problem is not about pattern matching or your instance implementation. Your ADT has the same structure as Maybe, and if I try

main = print (Nothing == Nothing)

I get the error code: https://github.com/purescript/purescript/wiki/Error-Code-NoInstanceFound

Your type parameter t140 could be anything in the Eq type class, so the compiler is not able to select an instance. You need to add a type annotation to at least one of the operands of ==:

v2 = Infinite :: Extended Int

But I admit it would be more satisfactory if the compiler could figure out that Infinite == Infinite for any (same) type parameter...