Cleanup 1

This commit is contained in:
Tibo De Peuter 2025-05-09 14:43:20 +02:00
parent 026218ddbd
commit 2089e20da5
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
4 changed files with 88 additions and 97 deletions

View file

@ -18,7 +18,7 @@ import prolog.logic.numbervars
*/
object Fail : Atom("fail"), Body {
override fun satisfy(subs: Substitutions): Answers = emptySequence()
override fun applySubstitution(subs: Substitutions): Fail = Fail
override fun applySubstitution(subs: Substitutions): Fail = this
}
/**
@ -31,7 +31,7 @@ typealias False = Fail
*/
object True : Atom("true"), Body {
override fun satisfy(subs: Substitutions): Answers = sequenceOf(Result.success(emptyMap()))
override fun applySubstitution(subs: Substitutions): True = True
override fun applySubstitution(subs: Substitutions): True = this
}
// TODO Repeat/0
@ -41,7 +41,7 @@ class Cut() : Atom("!") {
return sequenceOf(Result.failure(AppliedCut(emptyMap())))
}
override fun applySubstitution(subs: Substitutions): Cut = Cut()
override fun applySubstitution(subs: Substitutions): Cut = this
}
/**
@ -50,83 +50,63 @@ class Cut() : Atom("!") {
open class Conjunction(val left: LogicOperand, private val right: LogicOperand) :
LogicOperator(Atom(","), left, right) {
override fun satisfy(subs: Substitutions): Answers = sequence {
fun satisfyRight(leftSubs: Substitutions): Answers = sequence {
right.satisfy(subs + leftSubs).forEach { right ->
right.fold(
// If the right part succeeds, yield the result with the left substitutions
onSuccess = { rightSubs ->
yield(Result.success(leftSubs + rightSubs))
},
onFailure = { exception ->
// If the right part fails, check if it's a cut
if (exception is AppliedCut) {
// If it's a cut, yield the result with the left substitutions
val newSubs = if (exception.subs != null) leftSubs + exception.subs else null
yield(Result.failure(AppliedCut(newSubs)))
return@sequence
}
// If it's not a cut, yield the failure
yield(Result.failure(exception))
}
)
}
}
fun findNextCutSolution(appliedCut: AppliedCut): Answers = sequence {
val leftSubs = appliedCut.subs
right.satisfy(subs + (appliedCut.subs!!)).firstOrNull()?.map { rightSubs ->
// If the right part succeeds, yield the result with the left substitutions
yield(Result.success(leftSubs + rightSubs))
return@sequence
} ?: yield(Result.failure(AppliedCut()))
}
// Satisfy the left part first, which either succeeds or fails
left.satisfy(subs).forEach { left ->
left.fold(
// If it succeeds, satisfy the right part with the updated substitutions and return all results
onSuccess = { leftSubs ->
right.satisfy(subs + leftSubs).forEach { right ->
right.fold(
// If the right part succeeds, yield the result with the left substitutions
onSuccess = { rightSubs ->
yield(Result.success(leftSubs + rightSubs))
},
// If the right part fails, check if it's a cut
onFailure = { exception ->
if (exception is AppliedCut) {
if (exception.subs != null) {
// If it's a cut, yield the result with the left substitutions
yield(Result.failure(AppliedCut(leftSubs + exception.subs)))
} else {
yield(Result.failure(AppliedCut()))
}
return@sequence
}
// If it's not a cut, yield the failure
yield(Result.failure(exception))
}
)
}
},
onSuccess = { leftSubs -> yieldAll(satisfyRight(leftSubs)) },
// If it fails, check these conditions:
onFailure = { exception ->
// 1. If the left part is a cut, satisfy the right part ONCE, and stop searching for more solutions
if (exception is AppliedCut) {
right.satisfy(subs + (exception.subs!!)).firstOrNull()?.fold(
onSuccess = {
// If the right part succeeds, yield the result with the left substitutions
yield(Result.success(exception.subs + it))
return@sequence
},
onFailure = {
// If the right part fails, yield the failure
yield(Result.failure(it))
}
) ?: yield(Result.failure(AppliedCut()))
} else if (exception is AppliedShift) {
if (exception.cont == null) {
// Goal should be our right operand
yield(Result.failure(AppliedShift(exception.subs, exception.ball, right as Goal)))
} else {
val leftSubs = exception.subs
right.satisfy(subs + leftSubs).forEach { right ->
right.fold(
// If the right part succeeds, yield the result with the left substitutions
onSuccess = { rightSubs ->
yield(Result.success(leftSubs + rightSubs))
},
// If the right part fails, check if it's a cut
onFailure = { exception ->
if (exception is AppliedCut) {
if (exception.subs != null) {
// If it's a cut, yield the result with the left substitutions
yield(Result.failure(AppliedCut(leftSubs + exception.subs)))
} else {
yield(Result.failure(AppliedCut()))
}
return@sequence
}
when (exception) {
// 1. If the left part is a cut, satisfy the right part ONCE, and stop searching for more solutions
is AppliedCut -> yieldAll(findNextCutSolution(exception))
// If it's not a cut, yield the failure
yield(Result.failure(exception))
}
)
// 2. If the left part is a shift, we need to check if the right part can be satisfied
is AppliedShift -> {
if (exception.cont == null) {
// Pass the right of our disjunction as the continuation
val newShift = AppliedShift(exception.subs, exception.ball, right as Goal)
yield(Result.failure(newShift))
} else {
// Satisfy the right part with the updated substitutions
yieldAll(satisfyRight(exception.subs))
}
}
} else {
// 2. Any other failure should be returned as is
yield(Result.failure(exception))
// 3. Any other failure should be returned as is
else -> yield(Result.failure(exception))
}
}
)