Created
March 21, 2018 21:44
-
-
Save chessai/203fe97fb74c6ebc99eae4f7b8c09045 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE PolyKinds #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE TypeFamilies #-} | |
module ComposeN where | |
type family FunctionType (as :: [k]) (r :: k) :: k where | |
FunctionType '[] r = r | |
FunctionType (a ': as) r = a -> FunctionType as r | |
data Nat (as :: [k]) where | |
Z :: Nat '[a] | |
S :: Nat as -> Nat (a ': as) | |
composeN :: Nat as -> (b -> c) -> FunctionType as b -> FunctionType as c | |
composeN Z f g = f . g | |
composeN (S as) f g = \x -> composeN as f (g x) | |
add3 :: Int -> Int -> Int -> Int | |
add3 a b c = a + b + c | |
test1 :: Int | |
test1 = composeN (S Z) (+1) (+) 2 3 | |
test2 :: Int | |
test2 = composeN (S (S Z)) (+1) add3 1 2 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment