Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active March 10, 2021 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Superstar64/8fd82a5e1e59ec36831a0c05e61936cd to your computer and use it in GitHub Desktop.
Save Superstar64/8fd82a5e1e59ec36831a0c05e61936cd to your computer and use it in GitHub Desktop.
Bound, Enum, Show, and Eq instance for functions
import Data.List
import Data.Maybe
every :: (Bounded a, Enum a) => [a]
every = [minBound ..]
space :: (Bounded a, Enum a, Bounded b, Enum b, Eq a) => [a -> b]
space = spaceImpl every
where
spaceImpl [] = [error "lol"]
spaceImpl (a : as) = do
f <- spaceImpl as
e <- every
let inner x | x == a = e
inner x = f x
in pure inner
instance (Bounded a, Enum a, Bounded b, Enum b, Eq a) => Bounded (a -> b) where
minBound = head space
maxBound = last space
instance (Bounded a, Enum a, Bounded b, Enum b, Eq a, Eq b) => Enum (a -> b) where
toEnum x = space !! x
fromEnum f = fromJust $ elemIndex f space
-- copy paste from prelude doc
enumFrom x = enumFromTo x maxBound
enumFromThen x y = enumFromThenTo x y bound
where
bound
| fromEnum y >= fromEnum x = maxBound
| otherwise = minBound
instance (Bounded a, Enum a, Show a, Show b) => Show (a -> b) where
show f = "let {" ++ (every >>= printer) ++ "} in f"
where
printer x = "f x | x == " ++ show x ++ " = " ++ show (f x) ++ ";"
instance (Bounded a, Enum a, Eq b) => Eq (a -> b) where
f == g = and $ zipWith (==) (f <$> every) (g <$> every)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment