test: Cut not_equal

This commit is contained in:
Tibo De Peuter 2025-04-15 16:40:52 +02:00
parent 229a8bbc3c
commit 2fcab52f65
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
7 changed files with 75 additions and 18 deletions

View file

@ -7,7 +7,7 @@ import prolog.ast.terms.Atom
import prolog.ast.terms.Body
import prolog.ast.terms.Goal
import prolog.ast.logic.LogicOperator
import prolog.exceptions.AppliedCut
import prolog.flags.AppliedCut
/**
* Always fail.
@ -56,13 +56,17 @@ class Conjunction(private val left: LogicOperand, private val right: LogicOperan
// If the right part fails, check if it's a cut
onFailure = { exception ->
if (exception is AppliedCut) {
// If it's a cut, yield the result with the left substitutions
yield(Result.failure(AppliedCut(leftSubs + exception.subs)))
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
} else {
// If it's not a cut, yield the failure
yield(Result.failure(exception))
}
// If it's not a cut, yield the failure
yield(Result.failure(exception))
}
)
}
@ -71,7 +75,7 @@ class Conjunction(private val left: LogicOperand, private val right: LogicOperan
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).first().fold(
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))
@ -81,7 +85,7 @@ class Conjunction(private val left: LogicOperand, private val right: LogicOperan
// If the right part fails, yield the failure
yield(Result.failure(it))
}
)
) ?: yield(Result.failure(AppliedCut()))
} else {
// 2. Any other failure should be returned as is
yield(Result.failure(exception))