Skip to content

Instantly share code, notes, and snippets.

@evincarofautumn
Created November 7, 2016 13:21
Show Gist options
  • Save evincarofautumn/3cc9e50f8b7318000325e7158e5d1caa to your computer and use it in GitHub Desktop.
Save evincarofautumn/3cc9e50f8b7318000325e7158e5d1caa to your computer and use it in GitHub Desktop.
Playing with typeclass instances for continuations
import Control.Monad.Trans.Cont
import GHC.Exts (IsString(..))
instance (Num a) => Num (ContT r m a) where
fromInteger x = ContT (\k -> k (fromInteger x))
{-
fromInteger = ContT . flip ($) . fromInteger
-}
abs (ContT f) = ContT (\k -> f (k . abs))
{-
abs = ContT . unary abs . runContT
where unary c f k = f (k . c)
unary c f = f . (. c)
unary = flip (.) . flip (.)
-}
negate (ContT f) = ContT (\k -> f (k . negate))
signum (ContT f) = ContT (\k -> f (k . signum))
ContT f + ContT g = ContT (\k -> f (\x -> g (k . (x +))))
ContT f - ContT g = ContT (\k -> f (\x -> g (k . (x -))))
{-
It looks like we have to choose an evaluation order.
The alternative is to evaluate the arguments from right to left:
ContT f - ContT g = ContT (\k -> g (\x -> f (k . subtract x)))
-}
ContT f * ContT g = ContT (\k -> f (\x -> g (k . (x *))))
{-
(*) = ContT .: binary (*) `on` runContT
where (.:) = (.) . (.)
binary c f g k = f (\x -> g (k . c x))
binary c f = f .: (unary . c)
binary = flip (.:) . (unary .)
-}
-- Pretty obvious.
instance (IsString a) => IsString (ContT r m a) where
fromString s = ContT (\k -> k (fromString s))
-- I have no idea what to do here.
instance (???) => IsList (ContT r m a) where
type Item (ContT r m a) = ???
fromList l = ContT (\k -> ???)
toList (ContT f) = ???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment