Created
September 26, 2017 07:31
-
-
Save TerrorJack/4f3cbf0827d03baa2b3ad09e50ecf8a2 to your computer and use it in GitHub Desktop.
Runtime type-level naturals
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 DataKinds #-} | |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE TypeOperators #-} | |
import Data.Type.Equality | |
data Nat | |
= Zero | |
| Succ Nat | |
data SingNat n where | |
SZero :: SingNat 'Zero | |
SSucc :: SingNat n -> SingNat ('Succ n) | |
instance TestEquality SingNat where | |
testEquality SZero SZero = Just Refl | |
testEquality (SSucc n) (SSucc m) = | |
case testEquality n m of | |
Just Refl -> Just Refl | |
_ -> Nothing | |
testEquality _ _ = Nothing | |
data SomeNat where | |
SomeNat :: SingNat n -> SomeNat | |
toSomeNat :: Int -> SomeNat | |
toSomeNat 0 = SomeNat SZero | |
toSomeNat n = | |
case toSomeNat $ n - 1 of | |
SomeNat m -> SomeNat $ SSucc m | |
instance Eq SomeNat where | |
SomeNat n == SomeNat m = | |
case testEquality n m of | |
Just Refl -> True | |
_ -> False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment