Last active
May 28, 2016 16:15
-
-
Save joneshf/956172c8c54afc7bf0e482dbe6ceb6f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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