Last active
February 6, 2024 04:05
-
-
Save julianpeeters/ab2946b6491d099cbdc436b3cd826dc9 to your computer and use it in GitHub Desktop.
Scala example of Exercise 1 from CM 500 Logical Frameworks (see https://github.com/julianpeeters/hello-twelf/tree/main)
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 | |
case object Zero extends Nat | |
case class Successor[N <: Nat](nat: N) extends Nat | |
sealed trait Even[+N] | |
case object EvenZ extends Even[Zero.type] | |
case class EvenS[N <: Nat](even: Even[N]) extends Even[Successor[Successor[N]]] | |
sealed trait Odd[N] | |
case class OddS[N <: Nat](even: Even2[N]) extends Odd[Successor[N]] | |
sealed trait Even2[+N] | |
case object Even2Z extends Even2[Zero.type] | |
case class Even2S[N <: Nat](odd: Odd[N]) extends Even2[Successor[N]] | |
// theorem: For all Nat, Even implies Even2 | |
def evenImpliesEven2(even: Even[Nat]): Even2[Nat] = | |
even match | |
case EvenZ => Even2Z | |
case EvenS(n) => Even2S(OddS(evenImpliesEven2(n))) | |
// proof of the claim `evenImpliesEven2` | |
println(evenImpliesEven2(EvenS(EvenZ))) | |
// Even2S(OddS(Even2Z)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment