Skip to content

Instantly share code, notes, and snippets.

@joneshf
Last active May 28, 2016 16:15
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 joneshf/956172c8c54afc7bf0e482dbe6ceb6f9 to your computer and use it in GitHub Desktop.
Save joneshf/956172c8c54afc7bf0e482dbe6ceb6f9 to your computer and use it in GitHub Desktop.
-- Direct translation of https://gist.github.com/rjmk/d28a706f9b1fa2ac30a5fcd7d767b334
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeOperators #-}
module TypedCompose where
import GHC.TypeLits
infixr 4 :.:
data Compose (n :: Nat) a b where
Nil :: Compose 0 a a
(:.:) :: (a -> b) -> Compose n c a -> Compose (n + 1) c b
typedCompose :: Compose n a b -> a -> b
typedCompose Nil = id
typedCompose (f :.: compose) = f . typedCompose compose
foo :: Int -> Int
foo = typedCompose ((+ 3) :.: (+ 4) :.: (* 6) :.: Nil)
main :: IO ()
main = putStrLn . show $ foo 5
-- Slightly simpler translation of https://gist.github.com/rjmk/d28a706f9b1fa2ac30a5fcd7d767b334
{-# LANGUAGE GADTs #-}
module TypedComposeSimpler where
infixr 4 :.:
data Compose a b where
Nil :: Compose a a
(:.:) :: (a -> b) -> Compose c a -> Compose c b
typedCompose :: Compose a b -> a -> b
typedCompose Nil = id
typedCompose (f :.: compose) = f . typedCompose compose
foo :: Int -> Int
foo = typedCompose ((+ 3) :.: (+ 4) :.: (* 6) :.: Nil)
bar :: Int -> String
bar = typedCompose (show :.: (+ 3) :.: (+ 4) :.: (* 6) :.: Nil)
main :: IO ()
main = do
print $ foo 5
print $ bar 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment