fix: Groene vinkjes

This commit is contained in:
Tibo De Peuter 2025-05-06 21:10:44 +02:00
parent 4810b91628
commit dff53b4e68
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
2 changed files with 7 additions and 5 deletions

View file

@ -44,14 +44,16 @@ abstract class Clause(var head: Head, var body: Body) : Term, Resolvent {
bodyAnswer.fold( bodyAnswer.fold(
onSuccess = { bodySubs -> onSuccess = { bodySubs ->
// If the body can be proven, yield the (combined) substitutions // If the body can be proven, yield the (combined) substitutions
var result = goalToHeadSubs.mapValues { applySubstitution(it.value, bodySubs) } val goalToHeadResult = goalToHeadSubs.mapValues { applySubstitution(it.value, bodySubs) }
yield(Result.success(result)) val headResult = headToGoalSubs.filterKeys { key -> goalToHeadSubs.any { occurs(key as Variable, it.value) } }
yield(Result.success(goalToHeadResult + headResult))
}, },
onFailure = { error -> onFailure = { error ->
if (error is AppliedCut) { if (error is AppliedCut) {
// Find single solution and return immediately // Find single solution and return immediately
if (error.subs != null) { if (error.subs != null) {
yield(Result.failure(AppliedCut(headAndGoalSubs + error.subs))) var result = goalToHeadSubs.mapValues { applySubstitution(it.value, error.subs) }
yield(Result.failure(AppliedCut(result)))
} else { } else {
yield(Result.failure(AppliedCut())) yield(Result.failure(AppliedCut()))
} }

View file

@ -27,7 +27,7 @@ class Examples {
@Test @Test
fun debugHelper() { fun debugHelper() {
loader.load("examples/basics/backtracking.pl") loader.load("examples/basics/arithmetics.pl")
} }
@ParameterizedTest @ParameterizedTest
@ -45,7 +45,7 @@ class Examples {
} }
fun basics() = listOf( fun basics() = listOf(
Arguments.of("arithmetics.pl", "gimli is a level 4 fighter with 35 hitpoints.\nlegolas is a level 5 ranger with 30 hitpoints.\ngandalf is a level 10 wizard with 25 hitpoints.\nfrodo is a level 2 rogue with 20 hitpoints.\nlegolas threw gimli, and gimli took 5 damage.\ngimli is a level 4 fighter with 30 hitpoints.\ngandalf casts aid.\ngimli is a level 4 fighter with 35 hitpoints.\nlegolas leveled up.\nlegolas is a level 6 ranger with 30 hitpoints"), Arguments.of("arithmetics.pl", "gimli is a level 4 fighter with 35 hitpoints.\nlegolas is a level 5 ranger with 30 hitpoints.\ngandalf is a level 10 wizard with 25 hitpoints.\nfrodo is a level 2 rogue with 20 hitpoints.\nlegolas threw gimli, and gimli took 5 damage.\ngimli is a level 4 fighter with 30 hitpoints.\ngandalf casts aid.\ngimli is a level 4 fighter with 35 hitpoints.\nlegolas leveled up.\nlegolas is a level 6 ranger with 30 hitpoints.\n"),
Arguments.of("backtracking.pl", "0\ns(0)\ns(s(0))\ns(s(s(0)))\n"), Arguments.of("backtracking.pl", "0\ns(0)\ns(s(0))\ns(s(s(0)))\n"),
Arguments.of("cut.pl", "0\n"), Arguments.of("cut.pl", "0\n"),
Arguments.of("disjunction.pl", "Alice likes Italian food.\nBob likes Italian food.\n"), Arguments.of("disjunction.pl", "Alice likes Italian food.\nBob likes Italian food.\n"),