Co/Contra-variance in Dependent Types
Hi all. The following piece of code will not compile and any help at all would be much appreciated.
What I am trying to do is provide a verified less-than checker on Peano natural numbers.
However, upon compiling I get the errors
-
LTEZero(x)
incase (Z,x) => Some(LTEZero(x))
has typeLTEZero[B]
and notLTENat[A,B]
, as expected. -
Similarly,
LTESucc(pf)
has typeLTESucc[Nat, Nat]
and notLTENat[A,B]
, as expected.
I suspect (please correct me if I’m wrong) that the issue lies in some co and contra variance relationships.
If so, could someone suggest a better definition of LTENat
? Any and all related pages you could suggest would be helpful! Thanks!
// singleton Nats. E.g., One is S(Z): S[Z]
sealed trait Nat
trait Z extends Nat
case object Z extends Z
case class S[N <: Nat](n: N) extends Nat
// LTE is less-than-or-equal-to
abstract class LTENat[A <: Nat, B <: Nat]
case class LTEZero[A <: Nat](x: A) extends LTENat[Z, A]
case class LTESucc[A <: Nat, B <: Nat](x: LTENat [A, B])
extends LTENat[S[A], S[B]]
object Proofs extends App {
def queryLT[A <: Nat, B <: Nat](n:A, m: B): Option[LTENat[A,B]] = {
(n,m) match {
case (Z, x) => Some(LTEZero(x))
case (_, Z) => None
case (S(x), S(y)) => None
queryLT(x,y) match {
case Some(pf) => Some(LTESucc(pf))
case None => None
}
}
}
}