Created
November 5, 2015 12:23
-
-
Save yanns/c2ccb9391b7babc5bd27 to your computer and use it in GitHub Desktop.
Natural numbers in scala types
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
sealed trait Nat { | |
type plus[That <: Nat] <: Nat | |
} | |
sealed trait Nat0 extends Nat { | |
type plus[That <: Nat] = That | |
} | |
sealed trait NatN[Prev <: Nat] extends Nat { | |
type plus[That <: Nat] = NatN[Prev#plus[That]] | |
} | |
object Nat { | |
type +[A <: Nat, B <: Nat] = A#plus[B] | |
} | |
object NatSpecs { | |
import Nat._ | |
type Nat1 = NatN[Nat0] | |
type Nat2 = NatN[Nat1] | |
type Nat3 = NatN[Nat2] | |
implicitly[Nat0 =:= Nat0] | |
implicitly[Nat0 + Nat1 =:= Nat1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment