Skip to content

Instantly share code, notes, and snippets.

@chessai
Created March 21, 2018 21:44
Show Gist options
  • Save chessai/203fe97fb74c6ebc99eae4f7b8c09045 to your computer and use it in GitHub Desktop.
Save chessai/203fe97fb74c6ebc99eae4f7b8c09045 to your computer and use it in GitHub Desktop.
{-# 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