0

learning Scalaz shows how to make an Algebraic Data Type in Haskell and scalaz:

data TrafficLight = Red | Yellow | Green deriving Eq

and

sealed trait TrafficLight
case object Red    extends TrafficLight
case object Yellow extends TrafficLight
case object Green  extends TrafficLight

But, as the tutorial explains, Equal.scala is invariant.

Does Haskell have variance? If so/no, does it play a role when comparing Red to Yellow?

Prelude> Red == Red
True
Prelude> Red == Yellow
False
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • 2
    If I'm reading this right, those two links are talking about two different things both called "variance". The first one has to do with subtyping (Haskell doesn't have subtyping, so this won't apply to Haskell) and the other one has to do with covariance and contravariance in functors. – David Young Apr 03 '15 at 18:44
  • 5
    in particular, note that the Scala code defines not only the objects `Red`, `Yellow` and `Green`, but also the types `Red.type`, `Yellow.type` and `Green.type`, which are all sub-types of `TrafficLight`, where the Haskell code defines three values `Red`, `Yellow`, and `Green`, but only one type, `TrafficLight`. – rampion Apr 03 '15 at 18:50
  • As mentioned, variance doesn't make sense because there's no subtyping (at all). The Haskell answer to subtyping is ad-hoc polymorphism, and in practical terms: type-classes. You might wanna check out the lecture "Adventure with Types in Haskell" by Simon Peyton Jones. – MasterMastic Apr 03 '15 at 20:30

0 Answers0