refactor: Herstructurering
This commit is contained in:
parent
1acd1cfb67
commit
e3c84e1761
33 changed files with 290 additions and 178 deletions
44
src/prolog/ast/logic/Clause.kt
Normal file
44
src/prolog/ast/logic/Clause.kt
Normal file
|
@ -0,0 +1,44 @@
|
|||
package prolog.ast.logic
|
||||
|
||||
import prolog.logic.Substituted
|
||||
import prolog.ast.terms.Body
|
||||
import prolog.ast.terms.Functor
|
||||
import prolog.ast.terms.Goal
|
||||
import prolog.ast.terms.Head
|
||||
import prolog.builtins.True
|
||||
import prolog.logic.unifyLazy
|
||||
|
||||
/**
|
||||
* ‘Sentence’ of a Prolog program.
|
||||
*
|
||||
* A clause consists of a [Head] and body separated by the neck operator, or it is a [Fact].
|
||||
*
|
||||
* @see [prolog.ast.terms.Variable]
|
||||
* @see [Predicate]
|
||||
*/
|
||||
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 {
|
||||
// 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 == True() -> head.toString()
|
||||
else -> "$head :- $body"
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue