Created
December 5, 2017 17:24
-
-
Save CarstenKoenig/d8888f4b0c2125caa14e9623edc13094 to your computer and use it in GitHub Desktop.
"dependent" example in Haskell
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
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE KindSignatures #-} | |
module DepFun where | |
data Nat = NatZ | NatS Nat | |
type family Codom (n :: Nat) where | |
Codom NatZ = Int | |
Codom (NatS n) = Int -> Codom n | |
data NatP (a :: Nat) where | |
Z :: NatP NatZ | |
S :: NatP n -> NatP (NatS n) | |
sumN :: NatP n -> Codom n | |
sumN n = sumNacc n 0 | |
sumNacc :: NatP n -> Int -> Codom n | |
sumNacc Z acc = acc | |
sumNacc (S n) acc = \x -> sumNacc n (acc + x) | |
-- examples: | |
zero = sumN Z | |
five = sumN (S Z) 5 | |
eleven = sumN (S (S Z)) 5 6 | |
threeParams :: Int -> Int -> Int -> Int | |
threeParams = sumN (S (S (S Z))) | |
typeError :: Int -> Int -> Int | |
typeError = sumN (S Z) -- Couldn't match type `Int` with `Int -> Int` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
have a look at the
sumN
functionit's type depend on the very first value you give it