Peano numbers type-level implementation works with classes but not traits

63 views Asked by At

The following type level implementation of subtraction on Peano numbers using match types works

sealed trait Nat
class O extends Nat
class S[N <: Nat] extends Nat

type _1 = S[O]
type _2 = S[_1]
type _3 = S[_2]

type minus[a <: Nat, b <: Nat] = (a, b) match
  case (O, _) => O
  case (S[n], O) => a
  case (S[n], S[m]) => n minus m

type x = _2 minus _1
summon[x =:= _1]
// val res0: S[O] =:= S[O] = generalized constraint

however if classes are changed to traits

trait O extends Nat
trait S[N <: Nat] extends Nat

then why does it not work

scala> type x = _2 minus _1
// defined alias type x = minus[_2, _1]

scala> summon[x =:= _1]
1 |summon[x =:= _1]
  |                ^
  |                Cannot prove that x =:= _1.
0

There are 0 answers