From 48bbe1fee56c2bed15cf71bb15d9eeda3c170cfe Mon Sep 17 00:00:00 2001 From: Enrico Siboni Date: Wed, 1 Jul 2020 10:35:59 +0200 Subject: [PATCH] Added Giovanni's fix to Conjunction.kt --- .../tuprolog/solve/stdlib/primitive/Conjunction.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/solve-streams/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Conjunction.kt b/solve-streams/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Conjunction.kt index e5eaa6f9b..898506a2c 100644 --- a/solve-streams/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Conjunction.kt +++ b/solve-streams/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Conjunction.kt @@ -27,6 +27,7 @@ internal object Conjunction : PrimitiveWrapper(Tuple.FU subGoals, request.context, Substitution.empty(), + emptyList(), request.context.sideEffectManager, previousGoalsHadAlternatives = false ) @@ -48,6 +49,7 @@ internal object Conjunction : PrimitiveWrapper(Tuple.FU goals: Iterable, toPropagateContext: ExecutionContext, accumulatedSubstitutions: Substitution, + accumulatedSideEffects: List, previousResponseSideEffectManager: SideEffectManagerImpl?, previousGoalsHadAlternatives: Boolean ): Boolean { @@ -66,6 +68,8 @@ internal object Conjunction : PrimitiveWrapper(Tuple.FU if (Cut.functor == goal.functor || goalResponse.sideEffectManager?.shouldExecuteThrowCut() == true) cutExecuted = true + val allSideEffectsSoFar = accumulatedSideEffects + goalResponse.sideEffects + when { goalResponse.sideEffectManager?.shouldExecuteThrowCut() == false && goalResponse.solution is Solution.Yes && @@ -76,6 +80,7 @@ internal object Conjunction : PrimitiveWrapper(Tuple.FU goals.drop(1), goalFinalState.context, goalResponse.solution.substitution - mainRequest.context.substitution, + allSideEffectsSoFar, goalResponse.sideEffectManager as? SideEffectManagerImpl, previousGoalsHadAlternatives || currentHasAlternatives ) @@ -83,7 +88,11 @@ internal object Conjunction : PrimitiveWrapper(Tuple.FU else -> // yield only non-false responses or false responses when there are no open alternatives (because no more or cut) if (goalResponse.solution !is Solution.No || (!previousGoalsHadAlternatives && !currentHasAlternatives) || cutExecuted) - yield(mainRequest.replyWith(goalResponse)) + yield( + mainRequest.replyWith( + goalResponse.copy(sideEffects = allSideEffectsSoFar) + ) + ) } if (cutExecuted || goalResponse.solution is Solution.Halt) return true // cut other alternatives of current and previous goals