Skip to content

Commit

Permalink
edit common primitives to exploit the side effects builder
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto authored and siboxd committed Jul 1, 2020
1 parent bf19f50 commit eed875a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
12 changes: 10 additions & 2 deletions solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<SideEffect>

@JsName("buildArray")
fun buildArray(): Array<SideEffect> = sideEffects.toTypedArray()

@JsName("build")
fun build(): List<SideEffect> = sideEffects.toList()

companion object {
@JvmStatic
@JsName("empty")
fun empty(): SideEffectsBuilder =
SideEffectsBuilderImpl(mutableListOf())
SideEffectsBuilderImpl(dequeOf())

@JvmStatic
@JsName("ofIterable")
fun of(sideEffects: Iterable<SideEffect>): SideEffectsBuilder =
SideEffectsBuilderImpl(sideEffects.toMutableList())
SideEffectsBuilderImpl(dequeOf(sideEffects))

@JvmStatic
@JsName("ofSequence")
fun of(sideEffects: Sequence<SideEffect>): SideEffectsBuilder =
SideEffectsBuilderImpl(sideEffects.toMutableList())
SideEffectsBuilderImpl(dequeOf(sideEffects))

@JvmStatic
@JsName("of")
fun of(vararg sideEffects: SideEffect): SideEffectsBuilder =
SideEffectsBuilderImpl(mutableListOf(*sideEffects))
SideEffectsBuilderImpl(dequeOf(*sideEffects))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -14,10 +13,8 @@ abstract class AbstractAssert(
override fun Solve.Request<ExecutionContext>.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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +17,9 @@ object Retract : UnaryPredicate<ExecutionContext>("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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ object RetractAll : UnaryPredicate.NonBacktrackable<ExecutionContext>("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()
Expand Down

0 comments on commit eed875a

Please sign in to comment.