io.github.pirocks.nd.Builder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simple-logic-lib Show documentation
Show all versions of simple-logic-lib Show documentation
A simple library for working with first order logic and natural deduction proofs
The newest version!
package io.github.pirocks.nd
import io.github.pirocks.logic.FOLFormula
import io.github.pirocks.logic.VariableName
fun proof(result: FOLFormula, givens: Set = emptySet(), verify: Boolean = false, init: MutableList.() -> Unit): NDProof {
val statements = mutableListOf()
statements.init()
val res = NDProof(statements, givens.map { GivenStatement(it) }.toMutableSet(), result)
if (verify && !res.verify()) {
throw MalformedProofException("Proof failed to verify")
}
return res
}
fun MutableList.implies(assumptionStatement: AssumptionStatement, init: MutableList.(assumption: NDStatement) -> Unit): ImpliesIntroduction {
val statements = mutableListOf()
statements.init(assumptionStatement)
assert(statements.isNotEmpty())
val implication = ImpliesIntroduction(assumptionStatement, statements)
add(implication)
return implication
}
fun MutableList.trueIntro(): TruthIntroduction = TruthIntroduction().also { add(it) }
fun MutableList.andIntro(left: NDStatement, right: NDStatement): AndIntroduction = AndIntroduction(left, right).also { add(it) }
fun MutableList.andElimLeft(target: NDStatement): AndEliminationLeft = AndEliminationLeft(target).also { add(it) }
fun MutableList.andElimRight(target: NDStatement): AndEliminationRight = AndEliminationRight(target).also { add(it) }
fun MutableList.orElim(target: NDStatement, left: NDStatement, right: NDStatement): OrElimination = OrElimination(target, left, right).also { add(it) }
fun MutableList.impliesElim(eliminationTarget: NDStatement, impliesStatement: NDStatement): ImpliesElimination = ImpliesElimination(eliminationTarget, impliesStatement).also { add(it) }
fun MutableList.iffElimLeft(eliminationTarget: NDStatement, iffStatement: NDStatement): IFFEliminationLeft = IFFEliminationLeft(eliminationTarget, iffStatement).also { add(it) }
fun MutableList.iffElimRight(eliminationTarget: NDStatement, iffStatement: NDStatement): IFFEliminationRight = IFFEliminationRight(eliminationTarget, iffStatement).also { add(it) }
fun MutableList.doubleNegElim(eliminationTarget: NDStatement): DoubleNegationElimination = DoubleNegationElimination(eliminationTarget).also { add(it) }
fun MutableList.falsityElim(eliminationTarget: NDStatement, to: FOLFormula): FalsityElimination = FalsityElimination(eliminationTarget, to).also { add(it) }
fun MutableList.orIntro(left: FOLFormula, right: NDStatement): OrIntroductionLeft = OrIntroductionLeft(left, right).also { add(it) }
fun MutableList.orIntro(left: NDStatement, right: FOLFormula): OrIntroductionRight = OrIntroductionRight(left, right).also { add(it) }
fun MutableList.iFFIntro(leftImplication: NDStatement, rightImplication: NDStatement): IFFIntroduction = IFFIntroduction(leftImplication, rightImplication).also { add(it) }
fun MutableList.negationIntro(assumptionStatement: AssumptionStatement, init: MutableList.(assumption: NDStatement) -> Unit): NegationIntroduction {
val statements = mutableListOf()
statements.init(assumptionStatement)
val implication = NegationIntroduction(assumptionStatement, statements)
add(implication)
return implication
}
fun MutableList.falseIntro(contradictoryLeft: NDStatement, contradictoryRight: NDStatement): FalseIntroduction =
FalseIntroduction(contradictoryLeft, contradictoryRight).also { add(it) }
fun MutableList.idIntro(toCopy: NDStatement): IDIntroduction = IDIntroduction(toCopy).also { add(it) }
fun assume(formula: FOLFormula): AssumptionStatement {
return AssumptionStatement(formula)
}
fun MutableList.forAllIntro(init: MutableList.(forAllConst: VariableName) -> Unit): ForAllIntroduction {
val statements = mutableListOf()
val forAllConst = VariableName()
statements.init(forAllConst)
val elem = ForAllIntroduction(forAllConst, statements)
add(elem)
return elem
}
fun MutableList.existsIntro(example: NDStatement, varToTarget: VariableName): ExistsIntroduction =
ExistsIntroduction(example, varToTarget).also { add(it) }
fun MutableList.existsElim(target: NDStatement, init: MutableList.(skolemCons: VariableName, skolemConstExpr: AssumptionStatement) -> Unit): ExistsElimination {
val statements = mutableListOf()
val skolemConstant = VariableName()
val skolemConstantExpression = ExistsElimination.calcSkolemConstantExpression(target, skolemConstant)
statements.init(skolemConstant, skolemConstantExpression)
val element = ExistsElimination(target, statements, skolemConstant, skolemConstantExpression)
add(element)
return element
}
fun MutableList.forAllElim(target: NDStatement, to: VariableName): ForAllElimination =
ForAllElimination(target, to).also { add(it) }
fun given(folFormula: FOLFormula): GivenStatement {
return GivenStatement(folFormula)
}