This commit is contained in:
Tibo De Peuter 2025-05-05 00:18:38 +02:00
parent a85169dced
commit a937b1bc44
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
15 changed files with 93 additions and 30 deletions

View file

@ -1,8 +1,9 @@
package prolog.ast.terms
import io.Logger
import prolog.Substitutions
class AnonymousVariable(id: Int) : Variable("_$id") {
class AnonymousVariable(private val id: Int) : Variable("_$id") {
companion object {
private var counter = 0
fun create(): AnonymousVariable {
@ -14,4 +15,7 @@ class AnonymousVariable(id: Int) : Variable("_$id") {
}
override fun toString(): String = "_"
override fun clone(): AnonymousVariable = AnonymousVariable(id)
override fun applySubstitution(subs: Substitutions): AnonymousVariable = this
}

View file

@ -21,4 +21,7 @@ open class Atom(val name: String) : Goal(), Head, Body, Resolvent {
override fun hashCode(): Int {
return javaClass.hashCode()
}
override fun clone(): Atom = Atom(name)
override fun applySubstitution(subs: Substitutions): Atom = Atom(name)
}

View file

@ -2,4 +2,4 @@ package prolog.ast.terms
import prolog.ast.logic.Satisfiable
interface Body : Satisfiable
interface Body : Term, Satisfiable

View file

@ -3,6 +3,7 @@ package prolog.ast.terms
import prolog.Answers
import prolog.Substitutions
import prolog.ast.logic.Resolvent
import prolog.logic.applySubstitution
import prolog.logic.unifyLazy
typealias Argument = Term
@ -33,4 +34,10 @@ open class Structure(val name: Atom, var arguments: List<Argument>) : Goal(), He
override fun hashCode(): Int {
return javaClass.hashCode()
}
override fun clone(): Structure = Structure(name, arguments)
override fun applySubstitution(subs: Substitutions): Structure = Structure(
name,
arguments.map { applySubstitution(it, subs) }
)
}

View file

@ -1,14 +1,19 @@
package prolog.ast.terms
import prolog.Substitutions
import prolog.logic.compare
import prolog.ast.arithmetic.Integer
import prolog.ast.arithmetic.Float
/**
* Value in Prolog.
*
* A [Term] is either a [Variable], [Atom], [Integer][prolog.ast.arithmetic.Integer],
* [Float][prolog.ast.arithmetic.Float] or [CompoundTerm].
* A [Term] is either a [Variable], [Atom], [Integer],
* [Float] or [CompoundTerm].
* In addition, SWI-Prolog also defines the type TODO string.
*/
interface Term : Comparable<Term> {
interface Term : Comparable<Term>, Cloneable {
override fun compareTo(other: Term): Int = compare(this, other, emptyMap())
fun applySubstitution(subs: Substitutions): Term
public override fun clone(): Term
}

View file

@ -39,4 +39,7 @@ open class Variable(val name: String) : Term, Body, Expression, LogicOperand() {
}
override fun toString(): String = name
override fun clone(): Variable = Variable(name)
override fun applySubstitution(subs: Substitutions): Variable = this
}