Checkpoint

This commit is contained in:
Tibo De Peuter 2025-04-06 12:14:18 +02:00
parent 438af6c053
commit 6eca9dfcb7
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
25 changed files with 309 additions and 113 deletions

View file

@ -1,15 +1,9 @@
package prolog.components.expressions
import prolog.components.terms.Functor
import prolog.components.terms.Goal
import prolog.components.terms.Head
import prolog.components.terms.Term
import prolog.components.Resolvent
import prolog.components.terms.*
import prolog.unify
// TODO Change this to the right type, supporting operators and normal Clauses
// Probably needs a new interface or abstract class (?)
typealias Body = List<Term>
/**
* Sentence of a Prolog program.
*
@ -18,18 +12,29 @@ typealias Body = List<Term>
* @see [prolog.components.terms.Variable]
* @see [Predicate]
*/
abstract class Clause(val head: Head, val body: Body = emptyList()) : Expression {
abstract class Clause(private val head: Head, private val body: Body? = null) : Resolvent {
val functor: Functor = head.functor
override fun evaluate(goal: Goal): Boolean {
val result = unify(goal, head)
// TODO Evaluate the body
return result.isEmpty
override fun solve(goal: Goal): Boolean {
val unification = unify(goal, head)
if (unification.isEmpty) {
return false
}
val substitutions = unification.get()
val proven = body == null || body.prove()
substitutions.forEach { (variable, _) -> variable.unbind() }
return proven
}
override fun toString(): String {
return when {
body.isEmpty() -> head.toString()
else -> "$head :- ${body.joinToString(", ")}"
body == null -> head.toString()
else -> "$head :- $body"
}
}
}