From dff53b4e68333ce676f2ce1c970286921ca22e53 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Tue, 6 May 2025 21:10:44 +0200 Subject: [PATCH] fix: Groene vinkjes --- src/prolog/ast/logic/Clause.kt | 8 +++++--- tests/e2e/Examples.kt | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/prolog/ast/logic/Clause.kt b/src/prolog/ast/logic/Clause.kt index 9955539..1080a9f 100644 --- a/src/prolog/ast/logic/Clause.kt +++ b/src/prolog/ast/logic/Clause.kt @@ -44,14 +44,16 @@ abstract class Clause(var head: Head, var body: Body) : Term, Resolvent { bodyAnswer.fold( onSuccess = { bodySubs -> // If the body can be proven, yield the (combined) substitutions - var result = goalToHeadSubs.mapValues { applySubstitution(it.value, bodySubs) } - yield(Result.success(result)) + val goalToHeadResult = goalToHeadSubs.mapValues { applySubstitution(it.value, bodySubs) } + val headResult = headToGoalSubs.filterKeys { key -> goalToHeadSubs.any { occurs(key as Variable, it.value) } } + yield(Result.success(goalToHeadResult + headResult)) }, onFailure = { error -> if (error is AppliedCut) { // Find single solution and return immediately 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 { yield(Result.failure(AppliedCut())) } diff --git a/tests/e2e/Examples.kt b/tests/e2e/Examples.kt index f93fc35..bafef47 100644 --- a/tests/e2e/Examples.kt +++ b/tests/e2e/Examples.kt @@ -27,7 +27,7 @@ class Examples { @Test fun debugHelper() { - loader.load("examples/basics/backtracking.pl") + loader.load("examples/basics/arithmetics.pl") } @ParameterizedTest @@ -45,7 +45,7 @@ class Examples { } 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("cut.pl", "0\n"), Arguments.of("disjunction.pl", "Alice likes Italian food.\nBob likes Italian food.\n"),