Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions lisa-examples/src/main/scala/ADTExample.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ADTExamples

import lisa.maths.SetTheory.Functions.Pi.->:
import lisa.maths.SetTheory.Types.ADT.InductiveInfixSyntax.SelfRef
import lisa.maths.SetTheory.Types.ADT.InductiveInfixSyntax.inductive
import lisa.maths.SetTheory.Types.ADT.InductiveInfixSyntax.|
import lisa.maths.SetTheory.Types.ADT._
import lisa.maths.SetTheory.Types.Tactics.Typecheck
import lisa.maths.SetTheory.Types.TypingHelpers

/**
* Run with: sbt "lisa-examples/runMain ADTExamples.Example".
*/
object Example extends lisa.Main {

val A, x, y, k, head, second, tail, xs, value = variable[Ind]

// Declare field variables first. Empty parentheses introduce a nullary constructor.
val Nat = inductive(
| zero (),
| succ (k :: SelfRef)
)
import Nat.{zero, succ}

// Fields are declared parameters or SelfRef, which implicitly uses all parameters.
// Arbitrary field types are unsupported. For several parameters, use inductive((A, B))(...).
val List = inductive(A)(
| nil (),
| cons (head :: A, tail :: SelfRef)
)
import List.{nil, cons}

// Parentheses supply type arguments; * applies set-theoretic functions to values.
val singleton = cons(Nat) * zero * nil(Nat)
val singletonTyping = Theorem(singleton :: List(Nat)) {
have(thesis) by Typecheck.prove
}

// Generated constructor and carrier theorems can be used directly in ordinary proofs.
val nilTyping = Theorem(nil(Nat) :: List(Nat)) {
have(thesis) by Restate.from(nil.intro.of(Nat))
}
val natCases = Theorem(∀(x, x :: Nat ==> ((x === zero) \/ ∃(k, (k :: Nat) /\ (x === succ * k))))) {
have(thesis) by Restate.from(Nat.elim)
}
val successorInjective = Theorem(∀(x, ∀(y, ((x :: Nat) /\ (y :: Nat)) ==> ((succ * x === succ * y) <=> (x === y))))) {
have(thesis) by Restate.from(succ.injectivity)
}
val zeroNotSuccessor = Theorem(∀(k, k :: Nat ==> !(zero === succ * k))) {
have(thesis) by Restate.from(Nat.disjointness(zero, succ))
}

// specialize fixes the domain's type arguments. This function has no remaining type parameters.
// Cases must be exhaustive and nonoverlapping, with distinct binders in each case.
val secondOrZero = fun(List.specialize(Nat), Nat) {
Case(nil):
zero
Case(cons, head, nil(Nat)):
zero
Case(cons, head, cons(Nat) * second * tail):
second
}
val secondOrZeroTyping = Theorem(secondOrZero :: (List(Nat) ->: Nat)) {
have(thesis) by Restate.from(secondOrZero.intro)
}

// A generic domain supplies its type parameters to recFun. self is already specialized at A.
// Recursive calls are on recursive subterms supplied by the patterns.
val copy = recFun(List, List(A)) { self =>
Case(nil):
nil(A)
Case(cons, head, tail):
cons(A) * head * (self * tail)
}

// elim gives a defining equation. Type arguments use .of(type); free binders use .of(v := term).
val copySingleton = Theorem(copy(Nat) * singleton === singleton) {
val base = have(copy(Nat) * nil(Nat) === nil(Nat)) by Restate.from(copy.elim(nil).of(Nat))
val step = have(copy(Nat) * singleton === cons(Nat) * zero * (copy(Nat) * nil(Nat))) by Tautology.from(
copy.elim(cons).of(Nat).of(head := zero, tail := nil(Nat)),
zero.intro,
nil.intro.of(Nat)
)
have(thesis) by Congruence.from(base, step)
}

// Each induction case has field typings and recursive hypotheses; introduce them with assume.
// Induction() can also infer the variable and datatype from the goal's typing assumption.
val copyIdentity = Theorem(xs :: List(A) |- copy(A) * xs === xs) {
have(thesis) by Induction(xs, List.specialize(A)) {
Case(nil) subproof {
have(thesis) by Restate.from(copy.elim(nil).of(A))
}
Case(cons, head, tail) subproof {
assume(head :: A, tail :: List(A), copy(A) * tail === tail)
val step = have(copy(A) * (cons(A) * head * tail) === cons(A) * head * (copy(A) * tail)) by
Tautology.from(copy.elim(cons).of(A))
have(thesis) by Congruence.from(step)
}
}
}

// Explicit function parameters need not come from the domain: repeat[A] : Nat -> A -> List[A].
// TypingHelpers.fun builds a set-theoretic lambda, distinct from the ADT pattern-matching fun.
val repeat = recFun(A)(Nat.specialize(), A ->: List(A)) { self =>
Case(zero):
TypingHelpers.fun(value :: A, nil(A))
Case(succ, k):
TypingHelpers.fun(value :: A, cons(A) * value * (self * k * value))
}
val repeatTyping = Theorem(repeat(Nat) * (succ * zero) * zero :: List(Nat)) {
have(thesis) by Typecheck.prove
}
}
19 changes: 19 additions & 0 deletions lisa-examples/src/test/scala/ADTExampleSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import lisa.SetTheoryLibrary
import org.scalatest.funsuite.AnyFunSuite

/**
* Executes the public ADT example and checks its generated LCF proofs.
*/
class ADTExampleSuite extends AnyFunSuite:
test("the unchanged ADT example constructs checked proofs without admissions"):
val _ = ADTExamples.Example.repeatTyping
val proofs = SetTheoryLibrary.theorems.all.filter { theorem =>
val source = theorem.file.value.replace('\\', '/')
source.endsWith("/ADTExample.scala") || source.contains("/src/main/scala/lisa/maths/SetTheory/Types/ADT/")
}.toSeq
assert(proofs.nonEmpty)
for theorem <- proofs do
withClue(s"${theorem.fullName.value}: ") {
assert(theorem.errors.isEmpty, theorem.errors.mkString("\n"))
assert(!theorem.kernel.usesSorry)
}
2 changes: 1 addition & 1 deletion lisa-sets/src/main/scala/lisa/SetTheoryLibrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object SetTheoryLibrary extends lisa.utils.prooflib.Library {

type Proof = lisa.utils.prooflib.Proof

given lisa.utils.prooflib.Library = this
given given_Library: SetTheoryLibrary.type = this

/**
* A class is nothing more than a first-order formula with a free
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,13 @@ object Subset extends lisa.Main {
thenHave(thesis) by Substitute(singleton.definition)
}

/**
* Theorem --- A superset of a non-empty set is non-empty.
*/
val supersetNotEmpty = Theorem((x ⊆ y, !(x === ∅)) |- !(y === ∅)) {
val subst = have(y === ∅ |- y === ∅) by Hypothesis
have((x ⊆ ∅, y === ∅) |- (x === ∅)) by Tautology.from(rightEmpty)
have((x ⊆ y, y === ∅) |- (x === ∅)) by Congruence.from(subst, lastStep)
}

}
42 changes: 42 additions & 0 deletions lisa-sets/src/main/scala/lisa/maths/SetTheory/Base/Union.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,46 @@ object Union extends lisa.Main {
thenHave(thesis) by Extensionality
}

/**
* Theorem --- The unary union is monotonic: if `x ⊆ y` then `⋃x ⊆ ⋃y`.
*/
val unaryMonotonic = Theorem(x ⊆ y |- ⋃(x) ⊆ ⋃(y)) {
have(z ∈ b /\ b ∈ x |- z ∈ b /\ b ∈ x) by Hypothesis
thenHave((x ⊆ y) /\ z ∈ b /\ b ∈ x |- b ∈ x) by Weakening

have(x ⊆ y |- forall(b, (b ∈ x) ==> (b ∈ y))) by Tautology.from(subsetAxiom of (x := x, y := y))
thenHave(x ⊆ y |- (b ∈ x) ==> (b ∈ y)) by InstantiateForall(b)

have((x ⊆ y) /\ (b ∈ x) |- b ∈ y) by Tautology.from(lastStep)
have((x ⊆ y) /\ z ∈ b /\ b ∈ x |- b ∈ y) by Tautology.from(lastStep)

have((x ⊆ y) /\ z ∈ b /\ b ∈ x |- z ∈ b /\ b ∈ y) by Tautology.from(lastStep)
thenHave((x ⊆ y) /\ z ∈ b /\ b ∈ x |- exists(a, z ∈ a /\ a ∈ y)) by RightExists
thenHave(z ∈ b /\ b ∈ x |- (x ⊆ y) ==> exists(a, z ∈ a /\ a ∈ y)) by Tautology
thenHave(exists(b, z ∈ b /\ b ∈ x) |- (x ⊆ y) ==> exists(a, z ∈ a /\ a ∈ y)) by LeftExists
have(z ∈ ⋃(x) |- (x ⊆ y) ==> exists(a, z ∈ a /\ a ∈ y)) by
Tautology.from(lastStep, ⋃.definition of (x := x, y := b, z := z))
have(z ∈ ⋃(x) |- (x ⊆ y) ==> z ∈ ⋃(y)) by
Tautology.from(lastStep, ⋃.definition of (x := y, y := b, z := z))
have(x ⊆ y |- z ∈ ⋃(x) ==> z ∈ ⋃(y)) by Tautology.from(lastStep)
thenHave(x ⊆ y |- forall(z, z ∈ ⋃(x) ==> z ∈ ⋃(y))) by RightForall
have(thesis) by Tautology.from(lastStep, Subset.definition of (x := ⋃(x), y := ⋃(y)))
}

/**
* Theorem --- `∅` is a left identity for `∪`: `∅ ∪ x === x`.
*/
val leftNeutral = Theorem(∅ ∪ x === x) {
have(∅ ⊆ x) by Tautology.from(Subset.leftEmpty of (x := x))
val incl1 = have(∅ ∪ x ⊆ x) by Tautology.from(
lastStep,
Subset.reflexivity of (x := x),
leftUnionSubset of (x := ∅, y := x, z := x)
)

have(x ⊆ (∅ ∪ x)) by Tautology.from(rightSubset of (x := ∅, y := x))

have(thesis) by Tautology.from(incl1, lastStep, Subset.antisymmetry of (x := ∅ ∪ x, y := x))
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package lisa.maths.SetTheory.Cardinal

import lisa.maths.SetTheory.Ordinals.Ordinal._
import lisa.maths.Quantifiers.existsEpsilon
import lisa.maths.SetTheory.Base.Predef.{_, given}
import lisa.maths.SetTheory.Order.Predef._
import lisa.maths.SetTheory.Order.WellOrders.WellOrder
import lisa.maths.SetTheory.Ordinals.Ordinal
import lisa.maths.SetTheory.Ordinals.Ordinal.comparability
import lisa.maths.SetTheory.Ordinals.Ordinal.elementOfOrdinal
import lisa.maths.SetTheory.Ordinals.Ordinal.ordinal
import lisa.maths.SetTheory.Ordinals.TransitiveSet
import lisa.maths.SetTheory.Relations.BasicTheorems
import lisa.maths.SetTheory.Relations.Examples.MembershipRelation
import lisa.maths.SetTheory.Relations.Predef._

import Universe._

object UniverseRank extends lisa.Main:
private val x, k = variable[Ind]
private val x, y, z, k, a, S = variable[Ind]
private val α, β, γ = variable[Ind]
private val U, U1, U2 = variable[Ind]

/**
Expand All @@ -22,12 +34,112 @@ object UniverseRank extends lisa.Main:
val level = DEF(λ(U, ε(k, ∀(x, x ∈ k <=> (x ∈ U /\ ordinal(x))))))

/**
* The level of a Universe is an Ordinal (actually an Inaccessible Cardinal).
* Membership in the set of ordinals contained in U.
*/
val levelMembership = Theorem(
x ∈ level(U) <=> (x ∈ U /\ ordinal(x))
) {
def property(k: Expr[Ind]) = ∀(x, x ∈ k <=> (x ∈ U /\ ordinal(x)))
have(∃(k, property(k))) by Restate.from(comprehensionSchema of (y := U, φ := λ(x, ordinal(x))))
thenHave(property(ε(k, property(k)))) by Tautology.fromLastStep(existsEpsilon of (x := k, P := λ(k, property(k))))
thenHave(∀(x, x ∈ level(U) <=> (x ∈ U /\ ordinal(x)))) by Substitute(level.definition)
thenHave(thesis) by InstantiateForall(x)
}

/**
* The ordinals contained in a universe form an ordinal.
*/
val levelIsOrdinal = Theorem(
isUniverse(U) ==> ordinal(level(U))
) {
sorry
assume(isUniverse(U))
val L = level(U)
val r = MembershipRelation.membershipRelation(L)

val transitiveLevel = have(TransitiveSet.transitiveSet(L)) subproof {
have((x ∈ y, y ∈ L) |- x ∈ L) subproof {
assume(x ∈ y)
assume(y ∈ L)
val yOrdinal = have(ordinal(y)) by Tautology.from(levelMembership of (x := y))
val yInU = have(y ∈ U) by Tautology.from(levelMembership of (x := y))
val ySubsetU = have(y ⊆ U) by Tautology.from(yInU, universeTransitivity of (x := y))
val xInU = have(x ∈ U) by Tautology.from(ySubsetU, Subset.membership of (x := y, y := U, z := x))
val xOrdinal = have(ordinal(x)) by Tautology.from(yOrdinal, elementOfOrdinal of (α := y, β := x))
have(thesis) by Tautology.from(xInU, xOrdinal, levelMembership)
}
thenHave(x ∈ y /\ (y ∈ L) ==> x ∈ L) by Restate
thenHave(∀(x, ∀(y, x ∈ y /\ (y ∈ L) ==> x ∈ L))) by Generalize
thenHave(thesis) by Substitute(TransitiveSet.transitiveSet.definition of (A := L))
}

val transitiveRelation = have(transitive(r)(L)) subproof {
have((x ∈ L, y ∈ L, z ∈ L) |- ((x, y) ∈ r) /\ ((y, z) ∈ r) ==> ((x, z) ∈ r)) by Tautology.from(
levelMembership of (x := z),
Ordinal.transitivity of (α := x, β := y, γ := z),
MembershipRelation.membership of (A := L),
MembershipRelation.membership of (x := y, y := z, A := L),
MembershipRelation.membership of (x := x, y := z, A := L)
)
val implication = (x ∈ L) ==> ((y ∈ L) ==> ((z ∈ L) ==> (((x, y) ∈ r) /\ ((y, z) ∈ r) ==> ((x, z) ∈ r))))
thenHave(implication) by Restate
thenHave(∀(x, ∀(y, ∀(z, implication)))) by Generalize
thenHave(∀(x ∈ L, ∀(y ∈ L, ∀(z ∈ L, ((x, y) ∈ r) /\ ((y, z) ∈ r) ==> ((x, z) ∈ r))))) by Tableau
thenHave(thesis) by Substitute(transitive.definition of (R := r, X := L))
}

val totalRelation = have(total(r)(L)) subproof {
have((x ∈ L, y ∈ L) |- ((x, y) ∈ r) \/ ((y, x) ∈ r) \/ (x === y)) by Tautology.from(
levelMembership,
levelMembership of (x := y),
comparability of (α := x, β := y),
MembershipRelation.membership of (A := L),
MembershipRelation.membership of (x := y, y := x, A := L)
)
val implication = (x ∈ L) ==> ((y ∈ L) ==> (((x, y) ∈ r) \/ ((y, x) ∈ r) \/ (x === y)))
thenHave(implication) by Restate
thenHave(∀(x, ∀(y, implication))) by Generalize
thenHave(∀(x ∈ L, ∀(y ∈ L, ((x, y) ∈ r) \/ ((y, x) ∈ r) \/ (x === y)))) by Tableau
thenHave(thesis) by Substitute(total.definition of (R := r, X := L))
}

val foundedRelation = have(wellFounded(r)(L)) subproof {
have((S ⊆ L, S ≠ ∅) |- ∃(a, minimal(a)(S)(r))) subproof {
assume(S ⊆ L)
assume(S ≠ ∅)
val foundation = have(∃(a, a ∈ S /\ ∀(x, x ∈ S ==> x ∉ a))) by Tautology.from(axiomOfFoundation of (x := S))
have((a ∈ S, ∀(x, x ∈ S ==> x ∉ a)) |- minimal(a)(S)(r)) subproof {
assume(a ∈ S)
assume(∀(x, x ∈ S ==> x ∉ a))
thenHave(x ∈ S ==> x ∉ a) by InstantiateForall(x)
thenHave(x ∈ S ==> (x, a) ∉ r) by Tautology.fromLastStep(MembershipRelation.membership of (x := x, y := a, A := L))
thenHave(∀(x, x ∈ S ==> (x, a) ∉ r)) by RightForall
thenHave(a ∈ S /\ ∀(x, x ∈ S ==> (x, a) ∉ r)) by Tautology
thenHave(thesis) by Substitute(minimal.definition of (a := a, A := S, < := r))
}
thenHave((a ∈ S, ∀(x, x ∈ S ==> x ∉ a)) |- ∃(a, minimal(a)(S)(r))) by RightExists
thenHave(a ∈ S /\ ∀(x, x ∈ S ==> x ∉ a) |- ∃(a, minimal(a)(S)(r))) by Restate
thenHave(∃(a, a ∈ S /\ ∀(x, x ∈ S ==> x ∉ a)) |- ∃(a, minimal(a)(S)(r))) by LeftExists
have(thesis) by Cut(foundation, lastStep)
}
thenHave((S ⊆ L) /\ (S ≠ ∅) ==> ∃(a, minimal(a)(S)(r))) by Restate
thenHave(∀(S, (S ⊆ L) /\ (S ≠ ∅) ==> ∃(a, minimal(a)(S)(r)))) by Generalize
thenHave(thesis) by Substitute(wellFounded.definition of (R := r, X := L))
}

have(ordinal(L)) by Tautology.from(
transitiveLevel,
transitiveRelation,
totalRelation,
foundedRelation,
MembershipRelation.irreflexivity of (A := L),
MembershipRelation.isRelation of (A := L),
BasicTheorems.relationOnIsRelation of (R := r, X := L),
strictPartialOrder.definition of (A := L, < := r),
strictTotalOrder.definition of (A := L, < := r),
WellOrder.wellOrder.definition of (A := L, < := r),
ordinal.definition of (α := L)
)
thenHave(thesis) by Restate
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1046,4 +1046,15 @@ object BasicTheorems extends lisa.Main {
have(thesis) by Tautology.from(`==>`, `<==`)
}

/**
* Theorem --- A function with a non-empty domain is non-empty.
*/
val nonEmptyDomain = Theorem(!(dom(h) === ∅) |- !(h === ∅)) {
val domEmpty = have(dom(∅) === ∅) by Restate.from(
lisa.maths.SetTheory.Relations.Examples.EmptyRelation.emptyDomain
)
have(h === ∅ |- dom(h) === ∅) by Congruence.from(domEmpty)
have(thesis) by Tautology.from(lastStep)
}

}
Loading