fix: Evaluation
This commit is contained in:
parent
d702b9b081
commit
1acd1cfb67
17 changed files with 189 additions and 120 deletions
|
@ -1,6 +1,8 @@
|
|||
package prolog.components.expressions
|
||||
|
||||
import prolog.Substitution
|
||||
import prolog.Substituted
|
||||
import prolog.builtins.True
|
||||
import prolog.builtins.equivalent
|
||||
import prolog.components.Resolvent
|
||||
import prolog.components.terms.*
|
||||
import prolog.unifyLazy
|
||||
|
@ -13,31 +15,29 @@ import prolog.unifyLazy
|
|||
* @see [prolog.components.terms.Variable]
|
||||
* @see [Predicate]
|
||||
*/
|
||||
abstract class Clause(private val head: Head, private val body: Body? = null) : Resolvent {
|
||||
abstract class Clause(private val head: Head, private val body: Body) : Resolvent {
|
||||
val functor: Functor = head.functor
|
||||
|
||||
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)
|
||||
}
|
||||
override fun solve(goal: Goal, subs: Substituted): Sequence<Substituted> = sequence {
|
||||
// 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, subs).forEach { newHeadSubs ->
|
||||
// If the body can be proven, yield the (combined) substitutions
|
||||
body.prove(subs + newHeadSubs).forEach { newBodySubs ->
|
||||
yield(newHeadSubs + newBodySubs)
|
||||
// Unbind the newly bound variables, so they can be reused.
|
||||
newBodySubs.keys.forEach { it.unbind() }
|
||||
}
|
||||
// Unbind the newly bound variables, so they can be reused.
|
||||
newHeadSubs.keys.forEach { it.unbind() }
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return when {
|
||||
body == null -> head.toString()
|
||||
else -> "$head :- $body"
|
||||
body == True() -> head.toString()
|
||||
else -> "$head :- $body"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue