Created
April 14, 2020 15:19
-
-
Save ryohji/fe808fc25af59d925d3384b6380d7dae to your computer and use it in GitHub Desktop.
等式論理
This file contains hidden or 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
interface Substitution { | |
fun with(vararg ps: Predicate): Predicate | |
} | |
interface Predicate { | |
fun substitute(vararg xs: Symbol): Substitution | |
} | |
data class Symbol(val name: String) : Predicate { | |
override fun toString(): String = name | |
override fun substitute(vararg xs: Symbol): Substitution = | |
object: Substitution { | |
override fun with(vararg ps: Predicate): Predicate = | |
xs.zip(ps).find { (x, _) -> x.name == name }?.second ?: this@Symbol | |
} | |
} | |
data class Equation(val left: Predicate, val right: Predicate) : Predicate { | |
override fun toString(): String = "($left = $right)" | |
override fun substitute(vararg xs: Symbol): Substitution = | |
object: Substitution { | |
override fun with(vararg ps: Predicate): Predicate = | |
Equation(left.substitute(*xs).with(*ps), right.substitute(*xs).with(*ps)) | |
} | |
} | |
infix fun Equation.leibniz(e: Substitution): Equation = | |
Equation(e.with(left), e.with(right)) | |
infix fun Equation.transitivity(e: Equation): Equation = | |
if (right == e.left) { | |
Equation(left, e.right) | |
} else { | |
throw IllegalArgumentException("The right hand side of $this does not match the left hand side of $e.") | |
} | |
infix fun Predicate.equanimity(e: Equation): Predicate = | |
if (this == e.left) { | |
e.right | |
} else { | |
throw IllegalArgumentException("$this does not match the left hand side of equation $e.") | |
} |
Transitivity:
// P=Q Q=R / P=R
Equation(Symbol("P"), Symbol("Q"))
.transitivity(Equation(Symbol("Q"), Symbol("R")))
および Equanimity:
// P P=Q / Q
Symbol("P")
.equanimity(Equation(Symbol("P"), Symbol("Q")))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leibniz 則は
Equation
に生やしたleibniz
によって起動する。