Checkpoint

This commit is contained in:
Tibo De Peuter 2025-04-11 19:27:01 +02:00
parent e3c84e1761
commit e73e5cbfc8
32 changed files with 1354 additions and 92 deletions

View file

@ -9,6 +9,11 @@ open class Atom(val name: String) : Goal(), Head, Body, Resolvent {
override fun solve(goal: Goal, subs: Substituted): Sequence<Substituted> = unifyLazy(goal, this, subs)
override fun evaluate(subs: Substituted): Pair<Term, Substituted> = Pair(this, emptyMap())
/**
* See also [SWI Prolog Standard Order of Terms](https://www.swi-prolog.org/pldoc/man?section=standardorder)
*/
override fun compareTo(other: Term): Int {
return when (other) {
is Variable -> 1

View file

@ -1,7 +1,7 @@
package prolog.ast.terms
import prolog.Program
import prolog.ast.logic.Provable
import prolog.ast.logic.LogicOperand
import prolog.logic.Substituted
/**
@ -11,7 +11,7 @@ import prolog.logic.Substituted
* A goal either [succeeds][prolog.builtins.True], in which case the variables in the compound terms have a binding,
* or it fails if Prolog fails to prove it.
*/
abstract class Goal : Term, Provable {
abstract class Goal : LogicOperand(), Term {
abstract val functor: Functor
override fun prove(subs: Substituted): Sequence<Substituted> = Program.solve(this, subs)

View file

@ -0,0 +1,32 @@
package prolog.ast.terms
import prolog.ast.arithmetic.Expression
import prolog.logic.Substituted
data class Integer(val value: Int): Term, Expression {
/**
* See also [SWI Prolog Standard Order of Terms](https://www.swi-prolog.org/pldoc/man?section=standardorder)
*/
override fun compareTo(other: Term): Int {
return when (other) {
is Variable -> 1
is Integer -> value.compareTo(other.value)
else -> -1
}
}
// Integers are already evaluated
override fun evaluate(subs: Substituted): Pair<Term, Substituted> = Pair(this, emptyMap())
override fun toString(): String {
return value.toString()
}
operator fun plus(other: Integer): Integer {
return Integer(value + other.value)
}
operator fun minus(other: Integer): Integer {
return Integer(value - other.value)
}
}

View file

@ -1,21 +1,16 @@
package prolog.ast.terms
import prolog.ast.logic.Provable
import prolog.logic.Substituted
typealias Operand = Term
abstract class Operator(
private val symbol: Atom,
val leftOperand: Operand? = null,
val rightOperand: Operand
) : CompoundTerm(symbol, listOfNotNull(leftOperand, rightOperand)), Provable {
abstract override fun prove(subs: Substituted): Sequence<Substituted>
private val leftOperand: Operand? = null,
private val rightOperand: Operand
) : CompoundTerm(symbol, listOfNotNull(leftOperand, rightOperand)) {
override fun toString(): String {
return when (leftOperand) {
null -> "${symbol.name} $rightOperand"
else -> "$leftOperand ${symbol.name} $rightOperand"
else -> "($leftOperand ${symbol.name} $rightOperand)"
}
}
}
typealias Operand = Goal

View file

@ -1,7 +1,7 @@
package prolog.ast.terms
import prolog.ast.logic.Resolvent
import prolog.builtins.equivalent
import prolog.logic.equivalent
import prolog.logic.Substituted
import prolog.logic.unifyLazy
@ -9,13 +9,19 @@ typealias Argument = Term
typealias CompoundTerm = Structure
open class Structure(val name: Atom, val arguments: List<Argument>) : Goal(), Head, Body, Resolvent {
open class Structure(val name: Atom, var arguments: List<Argument>) : Goal(), Head, Body, Resolvent {
override val functor: Functor = "${name.name}/${arguments.size}"
override fun solve(goal: Goal, subs: Substituted): Sequence<Substituted> {
return unifyLazy(goal, this, subs)
}
// A structure does not need to be evaluated, so return an empty sequence.
override fun evaluate(subs: Substituted): Pair<Term, Substituted> = Pair(this, emptyMap())
/**
* See also [SWI Prolog Standard Order of Terms](https://www.swi-prolog.org/pldoc/man?section=standardorder)
*/
override fun compareTo(other: Term): Int {
when (other) {
is Structure -> {

View file

@ -1,9 +1,16 @@
package prolog.ast.terms
import prolog.logic.Substituted
/**
* Value in Prolog.
*
* A [Term] is either a [Variable], [Atom], integer, float or [CompoundTerm].
* In addition, SWI-Prolog also defines the type string.
*/
interface Term : Comparable<Term>
interface Term : Comparable<Term> {
/**
* Returns the term that this expression evaluates to. (All the way down.)
*/
fun evaluate(subs: Substituted): Pair<Term, Substituted>
}

View file

@ -1,8 +1,10 @@
package prolog.ast.terms
import prolog.ast.arithmetic.Expression
import prolog.logic.Substituted
import java.util.*
data class Variable(val name: String) : Term {
data class Variable(val name: String) : Term, Expression {
private var alias: Optional<Term> = Optional.empty()
fun alias(): Optional<Term> {
@ -21,6 +23,16 @@ data class Variable(val name: String) : Term {
alias = Optional.empty()
}
override fun evaluate(subs: Substituted): Pair<Term, Substituted> {
// If the variable is bound, return the value of the binding
// If the variable is not bound, return the variable itself
return if (alias.isPresent) {
alias.get().evaluate(subs)
} else {
Pair(this, emptyMap())
}
}
override fun compareTo(other: Term): Int {
return when (other) {
is Variable -> name.compareTo(other.name)