Checkpoint

This commit is contained in:
Tibo De Peuter 2025-04-06 14:42:57 +02:00
parent ef8b82457c
commit d702b9b081
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
13 changed files with 114 additions and 63 deletions

View file

@ -1,8 +1,9 @@
package prolog.components.expressions
import prolog.Substitution
import prolog.components.Resolvent
import prolog.components.terms.*
import prolog.unify
import prolog.unifyLazy
/**
* Sentence of a Prolog program.
@ -15,20 +16,22 @@ import prolog.unify
abstract class Clause(private val head: Head, private val body: Body? = null) : Resolvent {
val functor: Functor = head.functor
override fun solve(goal: Goal): Boolean {
val unification = unify(goal, head)
if (unification.isEmpty) {
return false
override fun solve(goal: Goal): Sequence<Substitution> = sequence {
if (body == null) {
// If the clause is a fact, unify the goal with the head, and return the substitutions.
// Do this in a lazy way.
yieldAll(unifyLazy(goal, head))
} else {
// If the clause is a rule, unify the goal with the head and then try to prove the body.
// Only if the body can be proven, the substitutions should be returned.
// Do this in a lazy way.
unifyLazy(goal, head).forEach { subs ->
// If the body can be proven, yield the (combined) substitutions
body.prove().forEach { bodySubs ->
yield(subs + bodySubs)
}
}
}
val substitutions = unification.get()
val proven = body == null || body.prove()
substitutions.forEach { (variable, _) -> variable.unbind() }
return proven
}
override fun toString(): String {