refactor: Rework
This commit is contained in:
parent
ac55ed4c64
commit
6469dd6ced
34 changed files with 593 additions and 552 deletions
|
@ -1,6 +1,7 @@
|
|||
package prolog.ast.logic
|
||||
|
||||
import prolog.logic.Substituted
|
||||
import prolog.Answers
|
||||
import prolog.Substitutions
|
||||
import prolog.ast.terms.Body
|
||||
import prolog.ast.terms.Functor
|
||||
import prolog.ast.terms.Goal
|
||||
|
@ -19,26 +20,26 @@ import prolog.logic.unifyLazy
|
|||
abstract class Clause(private val head: Head, private val body: Body) : Resolvent {
|
||||
val functor: Functor = head.functor
|
||||
|
||||
override fun solve(goal: Goal, subs: Substituted): Sequence<Substituted> = sequence {
|
||||
override fun solve (goal: Goal, subs: Substitutions): Answers = 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() }
|
||||
unifyLazy(goal, head, subs).forEach { headAnswer ->
|
||||
headAnswer.map { newHeadSubs ->
|
||||
// If the body can be proven, yield the (combined) substitutions
|
||||
body.satisfy(subs + newHeadSubs).forEach { bodyAnswer ->
|
||||
bodyAnswer.map { newBodySubs ->
|
||||
yield(Result.success(newHeadSubs + newBodySubs))
|
||||
}
|
||||
}
|
||||
}
|
||||
// Unbind the newly bound variables, so they can be reused.
|
||||
newHeadSubs.keys.forEach { it.unbind() }
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return when {
|
||||
body is True -> head.toString()
|
||||
else -> "$head :- $body"
|
||||
else -> "$head :- $body"
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue