Skip to content

Instantly share code, notes, and snippets.

@k0001
Created April 21, 2016 00:44
Show Gist options
  • Save k0001/5101edb16a67d730446dbdcdaeffbfa9 to your computer and use it in GitHub Desktop.
Save k0001/5101edb16a67d730446dbdcdaeffbfa9 to your computer and use it in GitHub Desktop.
-- | How do I write inductive instances for n-arity functions? Does this have a
-- name? How can I add functional dependencies to 'MapLast'?
class MapLast s t a b where
mapLast :: (a -> b) -> (s -> t)
instance MapLast a b a b where
mapLast = ($)
instance MapLast (z -> a) (z -> b) a b where
mapLast = (.)
instance MapLast (y -> z -> a) (y -> z -> b) a b where
mapLast = (.) . (.)
instance MapLast (x -> y -> z -> a) (x -> y -> z -> b) a b where
mapLast = (.) . (.) . (.)
instance MapLast (w -> x -> y -> z -> a) (w -> x -> y -> z -> b) a b where
mapLast = (.) . (.) . (.) . (.)
-- Example usage:
--
-- > mapLast not True :: Bool
-- False
-- > mapLast not (\() -> True) () :: Bool
-- False
-- > mapLast not (\() () -> True) () () :: Bool
-- False
-- > mapLast not (\() () () -> True) () () () :: Bool
-- False
-- > mapLast not (\() () () () -> True) () () () () :: Bool
-- False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment