Skip to content

Instantly share code, notes, and snippets.

@aratama
Created December 13, 2016 09:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aratama/316e796a5792a7da040be6754fc51938 to your computer and use it in GitHub Desktop.
Save aratama/316e796a5792a7da040be6754fc51938 to your computer and use it in GitHub Desktop.
Type level natural numbers
module Main where
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Data.Function (const)
import Prelude (Unit, (+))
import Type.Proxy (Proxy(..))
data Zero
data Succ a
type One = Succ Zero
type Two = Succ One
type Three = Succ Two
numPred :: forall a. Proxy (Succ a) -> Proxy a
numPred = const Proxy
class Number a where
numValue :: Proxy a -> Int
instance numberZero :: Number Zero where
numValue = const 0
instance numberSucc :: Number x => Number (Succ x) where
numValue x = numValue (numPred x) + 1
three :: Proxy Three
three = Proxy
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = logShow (numValue three)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment