Created
December 13, 2016 09:22
-
-
Save aratama/316e796a5792a7da040be6754fc51938 to your computer and use it in GitHub Desktop.
Type level natural numbers
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
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