Backtracking fixed
This commit is contained in:
parent
a85169dced
commit
fd16c4cedc
18 changed files with 213 additions and 39 deletions
|
@ -27,27 +27,23 @@ abstract class Clause(var head: Head, var body: Body) : Term, Resolvent {
|
|||
// Only if the body can be proven, the substitutions should be returned.
|
||||
// Do this in a lazy way.
|
||||
|
||||
// Since we are only interested in substitutions in the goal (as opposed to the head of this clause),
|
||||
// we can use variable renaming and filter out the substitutions that are not in the goal.
|
||||
val (end, renamed: Substitutions) = numbervars(head, Program.variableRenamingStart, subs)
|
||||
val (headEnd, headRenaming) = numbervars(head, Program.variableRenamingStart, subs)
|
||||
Program.variableRenamingStart = headEnd
|
||||
|
||||
val reverse = renamed.entries.associate { (a, b) -> b to a }
|
||||
Program.variableRenamingStart = end
|
||||
val renamedHead = applySubstitution(head, subs + headRenaming)
|
||||
val renamedBody = applySubstitution(body, subs + headRenaming) as Body
|
||||
|
||||
var newSubs: Substitutions = subs + renamed
|
||||
unifyLazy(applySubstitution(goal, subs), head, newSubs).forEach { headAnswer ->
|
||||
unifyLazy(goal, renamedHead, subs).forEach { headAnswer ->
|
||||
headAnswer.map { headSubs ->
|
||||
// If the body can be proven, yield the (combined) substitutions
|
||||
newSubs = subs + renamed + headSubs
|
||||
body.satisfy(newSubs).forEach { bodyAnswer ->
|
||||
val subsNotInGoal = headSubs.filterNot { occurs(it.key as Variable, goal, emptyMap()) }
|
||||
renamedBody.satisfy(subsNotInGoal).forEach { bodyAnswer ->
|
||||
bodyAnswer.fold(
|
||||
// If the body can be proven, yield the (combined) substitutions
|
||||
onSuccess = { bodySubs ->
|
||||
var result = (headSubs + bodySubs)
|
||||
.mapKeys { applySubstitution(it.key, reverse)}
|
||||
.mapValues { applySubstitution(it.value, reverse) }
|
||||
result = result.map { it.key to applySubstitution(it.value, result) }
|
||||
.toMap()
|
||||
.filterNot { it.key in renamed.keys && !occurs(it.key as Variable, goal, emptyMap())}
|
||||
var result = headSubs + bodySubs
|
||||
result = result
|
||||
.mapValues { applySubstitution(it.value, result) }
|
||||
.filterKeys { it !is AnonymousVariable && occurs(it as Variable, goal, emptyMap()) }
|
||||
yield(Result.success(result))
|
||||
},
|
||||
onFailure = { error ->
|
||||
|
@ -81,7 +77,5 @@ abstract class Clause(var head: Head, var body: Body) : Term, Resolvent {
|
|||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return super.hashCode()
|
||||
}
|
||||
override fun hashCode(): Int = super.hashCode()
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package prolog.ast.logic
|
||||
|
||||
import prolog.Substitutions
|
||||
import prolog.ast.terms.Head
|
||||
import prolog.builtins.True
|
||||
import prolog.logic.applySubstitution
|
||||
|
||||
class Fact(head: Head) : Clause(head, True)
|
||||
class Fact(head: Head) : Clause(head, True) {
|
||||
override fun applySubstitution(subs: Substitutions): Fact = Fact(applySubstitution(head, subs) as Head)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
package prolog.ast.logic
|
||||
|
||||
import prolog.Substitutions
|
||||
import prolog.ast.terms.Body
|
||||
import prolog.ast.terms.Head
|
||||
import prolog.logic.applySubstitution
|
||||
|
||||
class Rule(head: Head, body: Body) : Clause(head, body)
|
||||
class Rule(head: Head, body: Body) : Clause(head, body) {
|
||||
override fun applySubstitution(subs: Substitutions): Rule = Rule(
|
||||
head = applySubstitution(head, subs) as Head,
|
||||
body = applySubstitution(body, subs) as Body
|
||||
)
|
||||
}
|
||||
|
|
Reference in a new issue