refactor: Herstructurering

This commit is contained in:
Tibo De Peuter 2025-04-06 19:16:50 +02:00
parent 1acd1cfb67
commit e3c84e1761
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
33 changed files with 290 additions and 178 deletions

View file

@ -0,0 +1,24 @@
package prolog.ast.terms
import prolog.ast.logic.Resolvent
import prolog.logic.Substituted
import prolog.logic.unifyLazy
open class Atom(val name: String) : Goal(), Head, Body, Resolvent {
override val functor: Functor = "$name/_"
override fun solve(goal: Goal, subs: Substituted): Sequence<Substituted> = unifyLazy(goal, this, subs)
override fun compareTo(other: Term): Int {
return when (other) {
is Variable -> 1
is Atom -> name.compareTo(other.name)
is Structure -> -1
else -> throw IllegalArgumentException("Cannot compare $this with $other")
}
}
override fun toString(): String {
return name
}
}

View file

@ -0,0 +1,5 @@
package prolog.ast.terms
import prolog.ast.logic.Provable
interface Body : Provable

View file

@ -0,0 +1,18 @@
package prolog.ast.terms
import prolog.Program
import prolog.ast.logic.Provable
import prolog.logic.Substituted
/**
* Ask the Prolog engine.
*
* A goal is either an [Atom] or a [CompoundTerm].
* 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 val functor: Functor
override fun prove(subs: Substituted): Sequence<Substituted> = Program.solve(this, subs)
}

View file

@ -0,0 +1,10 @@
package prolog.ast.terms
/**
* Part of a [Clause][prolog.ast.logic.Clause] before the neck operator.
*/
interface Head : Term {
val functor: Functor
}
typealias Functor = String

View file

@ -0,0 +1,21 @@
package prolog.ast.terms
import prolog.ast.logic.Provable
import prolog.logic.Substituted
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>
override fun toString(): String {
return when (leftOperand) {
null -> "${symbol.name} $rightOperand"
else -> "$leftOperand ${symbol.name} $rightOperand"
}
}
}
typealias Operand = Goal

View file

@ -0,0 +1,43 @@
package prolog.ast.terms
import prolog.ast.logic.Resolvent
import prolog.builtins.equivalent
import prolog.logic.Substituted
import prolog.logic.unifyLazy
typealias Argument = Term
typealias CompoundTerm = Structure
open class Structure(val name: Atom, val 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)
}
override fun compareTo(other: Term): Int {
when (other) {
is Structure -> {
val arityComparison = arguments.size.compareTo(other.arguments.size)
if (arityComparison != 0) return arityComparison
val nameComparison = name.compareTo(other.name)
if (nameComparison != 0) return nameComparison
arguments.zip(other.arguments).forEach { (arg1, arg2) ->
val argsComparison = equivalent(arg1, arg2)
if (!argsComparison) return arg1.compareTo(arg2)
}
return 0
}
// Structure is always greater than other terms
else -> return 1
}
}
override fun toString(): String {
return when {
arguments.isEmpty() -> name.name
else -> "${name.name}(${arguments.joinToString(", ")})"
}
}
}

View file

@ -0,0 +1,9 @@
package prolog.ast.terms
/**
* 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>

View file

@ -0,0 +1,38 @@
package prolog.ast.terms
import java.util.*
data class Variable(val name: String) : Term {
private var alias: Optional<Term> = Optional.empty()
fun alias(): Optional<Term> {
return alias
}
fun bind(term: Term): Optional<Term> {
if (alias.isEmpty) {
alias = Optional.of(term)
}
return alias
}
fun unbind() {
alias = Optional.empty()
}
override fun compareTo(other: Term): Int {
return when (other) {
is Variable -> name.compareTo(other.name)
// Variables are always less than atoms
else -> -1
}
}
override fun toString(): String {
return when {
alias.isPresent -> "$name: ${alias.get()}"
else -> name
}
}
}