{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} class Twice f a b | f a -> b, f b -> a where twice :: f -> a -> b instance (Apply f a b, Apply f b c) => Twice f a c where twice f a = apply f (apply f a) class Apply f a b | f a -> b, f b -> a where apply :: f -> a -> b data Head = Head deriving Show instance Apply Head [a] a where apply Head a = head a data Tail = Tail deriving Show instance Apply Tail [a] [a] where apply Tail a = tail a data Succ = Succ deriving Show instance Enum a => Apply Succ a a where apply Succ a = succ a data Singleton = Singleton deriving Show instance Apply Singleton a [a] where apply Singleton a = [a] data TwiceF f = TwiceF f deriving Show instance Twice f a b => Apply (TwiceF f) a b where apply (TwiceF f) a = twice f a test = twice (TwiceF (TwiceF (TwiceF (TwiceF Succ)))) $ twice (TwiceF (TwiceF Head)) $ twice (TwiceF (TwiceF Singleton)) $ head $ twice (TwiceF (TwiceF (TwiceF Tail))) $ map (+10) $ [16,15..0]::Int {- data Wrap a = Wrap a deriving Show instance Apply (Wrap (a->b)) a b where apply (Wrap f) a = f a -}