From eed875a06b0b25d4a519dad975761fac8308702c Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Tue, 30 Jun 2020 16:53:29 +0200 Subject: [PATCH] edit common primitives to exploit the side effects builder --- .../kotlin/it/unibo/tuprolog/solve/SideEffect.kt | 12 ++++++++++-- .../it/unibo/tuprolog/solve/SideEffectsBuilder.kt | 12 ++++++++---- .../solve/stdlib/primitive/AbstractAssert.kt | 9 +++------ .../unibo/tuprolog/solve/stdlib/primitive/Retract.kt | 5 +++-- .../tuprolog/solve/stdlib/primitive/RetractAll.kt | 6 +++--- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt index a2220525e..6ef20d6ed 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt @@ -20,7 +20,11 @@ sealed class SideEffect { : this(clauses.asIterable()) val theory by lazy { - Theory.indexedOf(clauses) + if (clauses is Theory) { + clauses + } else { + Theory.indexedOf(clauses) + } } } @@ -48,7 +52,11 @@ sealed class SideEffect { : this(clauses.asIterable()) val theory by lazy { - Theory.indexedOf(clauses) + if (clauses is Theory) { + clauses + } else { + Theory.indexedOf(clauses) + } } } diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffectsBuilder.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffectsBuilder.kt index db99da43f..d93e74be3 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffectsBuilder.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffectsBuilder.kt @@ -1,36 +1,40 @@ package it.unibo.tuprolog.solve import it.unibo.tuprolog.solve.impl.SideEffectsBuilderImpl +import it.unibo.tuprolog.utils.dequeOf import kotlin.js.JsName import kotlin.jvm.JvmStatic interface SideEffectsBuilder : SideEffectFactory { + @JsName("sideEffects") val sideEffects: MutableList + @JsName("buildArray") fun buildArray(): Array = sideEffects.toTypedArray() + @JsName("build") fun build(): List = sideEffects.toList() companion object { @JvmStatic @JsName("empty") fun empty(): SideEffectsBuilder = - SideEffectsBuilderImpl(mutableListOf()) + SideEffectsBuilderImpl(dequeOf()) @JvmStatic @JsName("ofIterable") fun of(sideEffects: Iterable): SideEffectsBuilder = - SideEffectsBuilderImpl(sideEffects.toMutableList()) + SideEffectsBuilderImpl(dequeOf(sideEffects)) @JvmStatic @JsName("ofSequence") fun of(sideEffects: Sequence): SideEffectsBuilder = - SideEffectsBuilderImpl(sideEffects.toMutableList()) + SideEffectsBuilderImpl(dequeOf(sideEffects)) @JvmStatic @JsName("of") fun of(vararg sideEffects: SideEffect): SideEffectsBuilder = - SideEffectsBuilderImpl(mutableListOf(*sideEffects)) + SideEffectsBuilderImpl(dequeOf(*sideEffects)) } } \ No newline at end of file diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/AbstractAssert.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/AbstractAssert.kt index 1b35804a4..08b0c681f 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/AbstractAssert.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/AbstractAssert.kt @@ -2,7 +2,6 @@ package it.unibo.tuprolog.solve.stdlib.primitive import it.unibo.tuprolog.core.* import it.unibo.tuprolog.solve.ExecutionContext -import it.unibo.tuprolog.solve.SideEffect import it.unibo.tuprolog.solve.primitive.Solve import it.unibo.tuprolog.solve.primitive.UnaryPredicate @@ -14,10 +13,8 @@ abstract class AbstractAssert( override fun Solve.Request.computeOne(first: Term): Solve.Response { ensuringArgumentIsStruct(0) val clause = if (first is Clause) first else Fact.of(first as Struct) - return replySuccess( - Substitution.empty(), - null, - SideEffect.AddDynamicClauses(clause, onTop = before) - ) + return replySuccess { + addDynamicClauses(clause, onTop = before) + } } } \ No newline at end of file diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Retract.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Retract.kt index 60bc5a9c5..d95cf47de 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Retract.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Retract.kt @@ -3,7 +3,6 @@ package it.unibo.tuprolog.solve.stdlib.primitive import it.unibo.tuprolog.core.* import it.unibo.tuprolog.core.Var import it.unibo.tuprolog.solve.ExecutionContext -import it.unibo.tuprolog.solve.SideEffect import it.unibo.tuprolog.solve.primitive.Solve import it.unibo.tuprolog.solve.primitive.UnaryPredicate import it.unibo.tuprolog.unify.Unificator.Companion.mguWith @@ -18,7 +17,9 @@ object Retract : UnaryPredicate("retract") { is Clause -> (first mguWith it) as Substitution.Unifier else -> (first mguWith it.head!!) as Substitution.Unifier } - replySuccess(substitution, null, SideEffect.RemoveDynamicClauses(it)) + replySuccess(substitution) { + removeDynamicClauses(it) + } } } } \ No newline at end of file diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/RetractAll.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/RetractAll.kt index 0fe0b85a5..4efd7e0c4 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/RetractAll.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/RetractAll.kt @@ -15,9 +15,9 @@ object RetractAll : UnaryPredicate.NonBacktrackable("retractal val dynamicKb = context.dynamicKb return when (val result = dynamicKb.retractAll(clause)) { is RetractResult.Success -> { - replySuccess( - sideEffects = *arrayOf(SideEffect.RemoveDynamicClauses(result.clauses)) - ) + replySuccess { + resetDynamicKb(result.theory) + } } else -> { replySuccess()