Here is an implementation of Eq for my algebraic data type (ADT)
data Stateful a =
Advancing a
| Stable a
| Finished a
instance statefulEq :: (Eq a) => Eq (Stateful a)
where
eq (Advancing x) (Advancing y) = eq x y
eq (Stable x) (Stable y) = eq x y
eq (Finished x) (Finished y) = eq x y
eq _ _ = false
What this says (I hope) is that Stateful has an instance of Eq if its element has an instance of Eq
Is there a way I can change this to allow for second/backup implementation as well? In SudoCode pseudocode:
instance statefulEq :: Eq (Stateful a)
where
eq (Advancing (Eq x)) (Advancing (Eq y)) = eq x y
eq (Advancing x) (Advancing y) = true
...
A way to say: if the elements have an instance of Eq, use that, otherwise return true.
or if I had some function
isConstrained :: forall a. TypeClass -> a -> Boolean
then perhaps
instance statefulEq :: Eq (Stateful a)
where
eq (Advancing x) (Advancing y)
| isConstrained Eq x && isConstrained Eq x = eq x y
| otherwise = true
...