Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Demonstration of incorrect (Enum x, Enum y) => Enum (x, y) instance
-- See
instance (Enum x, Enum y) => Enum (x, y) where
fromEnum (x,y) =
k^2 + 2*j + if permuted then 1 else 0
k = max (fromEnum x) (fromEnum y)
j = min (fromEnum x) (fromEnum y)
permuted = (fromEnum y) > (fromEnum x)
toEnum n =
let k = floor . sqrt $ fromIntegral n
(j, permdAdd) = (n-k) `divMod` 2
permute (x,y) | permdAdd>0 = (toEnum y, toEnum x)
| otherwise = (toEnum x, toEnum y)
in permute (k, j)
data A = A1 | A2 deriving (Show, Enum)
data B = B1 | B2 | B3 deriving (Show, Enum)
main = do
print [(A1,A1)..(A2,A2)]
print [(B1,B1)..(B3,B3)]
print [(A1,B1)..(A2,B3)]
print [(B1,A1)..(B3,A2)]
-- Program crashes, as expected, since creating such an instance is impossible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.