haskell - (Re)-defining (==) for class Eq -
in next example:
data colourname = white | gray | grayness | black | bluish -- ... -- hundreds more of colours -- ... | lastcolor deriving (read, show, eq) i'd redefine (==) grey , gray evaluate equal.
obviously, 1 way not include eq in deriving, however, i'd have define
(==) :: colourname (==) white white = true (==) grayness gray = true (==) gray grey = true (==) grayness gray = true (==) gray grayness = true (==) black black = true -- frickin' log of other colors, hundreds of lines of typing (==) lastcolor lastcolor = true (==) b = false which nil plan do.
i can't do
instance eq colourname (==) :: colourname -> colourname -> bool (==) grayness gray = true (==) gray grayness = true (==) b = (a == b) because leads infinite recursion, underdefined.
is there way out?
(no, don't want utilize data colour = colour string or similar. want valid colours represented enumeration, such providing automatic validation, want allow spelling variation end users of module!)
you can utilize derived enum instance :
data colourname = grayness | gray | ... deriving (read, show, enum) instance eq colourname grayness == gray = true gray == grayness = true == b = fromenum == fromenum b edit: can utilize patternsynonyms ghc 7.8+. works smart constructor, can used in pattern matches.
pattern grayness = gray haskell
No comments:
Post a Comment