Skip to content

Instantly share code, notes, and snippets.

@yanns
Created November 5, 2015 12:23
Show Gist options
  • Save yanns/c2ccb9391b7babc5bd27 to your computer and use it in GitHub Desktop.
Save yanns/c2ccb9391b7babc5bd27 to your computer and use it in GitHub Desktop.
Natural numbers in scala types
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