Skip to content

Commit

Permalink
fix primitive wrappers hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Jun 11, 2020
1 parent b8b1f42 commit 86a6589
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 197 deletions.
29 changes: 29 additions & 0 deletions solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/Solve.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ sealed class Solve {

/** Creates a new [Response] to this Request */
@JsName("replyWith")
fun replyWith(
substitution: Substitution,
libraries: Libraries? = null,
flags: PrologFlags? = null,
staticKb: Theory? = null,
dynamicKb: Theory? = null,
sideEffectManager: SideEffectManager? = null,
inputChannels: PrologInputChannels<*>? = null,
outputChannels: PrologOutputChannels<*>? = null
) = when (substitution) {
is Substitution.Unifier -> {
replySuccess(
substitution,
libraries,
flags,
staticKb,
dynamicKb,
sideEffectManager,
inputChannels,
outputChannels
)
}
else -> {
replyFail(libraries, flags, staticKb, dynamicKb, sideEffectManager, inputChannels, outputChannels)
}
}

/** Creates a new [Response] to this Request */
@JsName("replyWithSolution")
fun replyWith(
solution: Solution,
libraries: Libraries? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,15 @@ import it.unibo.tuprolog.core.Numeric
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solve
import it.unibo.tuprolog.solve.function.ArithmeticEvaluator

/** Base class for implementing arithmetic relation between [Numeric] terms */
abstract class ArithmeticRelation<E : ExecutionContext>(operator: String) : BinaryRelation<E>(operator) {
abstract class ArithmeticRelation<E : ExecutionContext>(operator: String) : BinaryRelation.Predicative<E>(operator) {

override fun uncheckedImplementation(request: Solve.Request<E>): Sequence<Solve.Response> =
sequenceOf(
request.ensuringAllArgumentsAreInstantiated()
.computeSingleResponse()
)
override fun Solve.Request<E>.compute(first: Term, second: Term): Boolean {
ensuringArgumentIsNumeric(1)
ensuringArgumentIsNumeric(2)
return arithmeticRelation(first as Numeric, second as Numeric)
}

override fun Solve.Request<E>.computeSingleResponse(): Solve.Response =
ArithmeticEvaluator(context).let {
replyWith(relationWithoutSideEffects(arguments[0].accept(it), arguments[1].accept(it)))
}

override fun relationWithoutSideEffects(x: Term, y: Term): Boolean =
arithmeticRelation(x as Numeric, y as Numeric)

/** Template method that should implement the arithmetic relation between [x] and [y] */
protected abstract fun arithmeticRelation(x: Numeric, y: Numeric): Boolean
abstract fun arithmeticRelation(x: Numeric, y: Numeric): Boolean
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,58 @@ import it.unibo.tuprolog.solve.Solve
/** Base class to implement primitives that relate two [Term]s and provide a single response */
abstract class BinaryRelation<E : ExecutionContext>(operator: String) : PrimitiveWrapper<E>(operator, 2) {

/** Template method that should compute the response of the [Term] relation application */
protected abstract fun Solve.Request<E>.computeSingleResponse(): Solve.Response
/** Template method aimed at computing the application of this relation to three [Term]s */
protected abstract fun Solve.Request<E>.computeAll(
first: Term,
second: Term
): Sequence<Solve.Response>

/** Template method that should be implemented and called in BinaryRelations without side effects */
protected open fun relationWithoutSideEffects(x: Term, y: Term): Boolean = throw NotImplementedError()
final override fun uncheckedImplementation(request: Solve.Request<E>): Sequence<Solve.Response> {
return request.computeAll(request.arguments[0], request.arguments[1])
}

/** Template method that should be implemented and called in BinaryRelations with side effects */
protected open fun relationWithSideEffects(x: Term, y: Term): Substitution = throw NotImplementedError()
abstract class WithoutSideEffects<E : ExecutionContext>(operator: String) : BinaryRelation<E>(operator) {
protected abstract fun Solve.Request<E>.computeAllSubstitutions(
first: Term,
second: Term
): Sequence<Substitution>

final override fun Solve.Request<E>.computeAll(first: Term, second: Term): Sequence<Solve.Response> {
return computeAllSubstitutions(first, second).map { replyWith(it) }
}
}

abstract class NonBacktrackable<E : ExecutionContext>(operator: String) : BinaryRelation<E>(operator) {
protected abstract fun Solve.Request<E>.computeOne(
first: Term,
second: Term
): Solve.Response

final override fun Solve.Request<E>.computeAll(first: Term, second: Term): Sequence<Solve.Response> {
return sequenceOf(computeOne(first, second))
}

}

abstract class Functional<E : ExecutionContext>(operator: String) : NonBacktrackable<E>(operator) {
protected abstract fun Solve.Request<E>.computeOneSubstitution(
first: Term,
second: Term
): Substitution

final override fun Solve.Request<E>.computeOne(first: Term, second: Term): Solve.Response {
return replyWith(computeOneSubstitution(first, second))
}
}

abstract class Predicative<E : ExecutionContext>(operator: String) : NonBacktrackable<E>(operator) {
protected abstract fun Solve.Request<E>.compute(
first: Term,
second: Term
): Boolean

final override fun Solve.Request<E>.computeOne(first: Term, second: Term): Solve.Response {
return if (compute(first, second)) replySuccess() else replyFail()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
package it.unibo.tuprolog.solve.primitive

import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solve

/** A base class to implement predicates with zero argument */
abstract class PredicateWithoutArguments<E : ExecutionContext>(operator: String) : PrimitiveWrapper<E>(operator, 0)
abstract class PredicateWithoutArguments<E : ExecutionContext>(operator: String) : PrimitiveWrapper<E>(operator, 0) {
/** Template method that should compute the response of the application of this predicate to a term [Term] */
protected abstract fun Solve.Request<E>.computeAll(): Sequence<Solve.Response>

final override fun uncheckedImplementation(request: Solve.Request<E>): Sequence<Solve.Response> {
return request.computeAll()
}

abstract class WithoutSideEffects<E : ExecutionContext>(operator: String) : PredicateWithoutArguments<E>(operator) {
protected abstract fun Solve.Request<E>.computeAllSubstitutions(): Sequence<Substitution>

final override fun Solve.Request<E>.computeAll(): Sequence<Solve.Response> {
return computeAllSubstitutions().map { replyWith(it) }
}
}

abstract class NonBacktrackable<E : ExecutionContext>(operator: String) : PredicateWithoutArguments<E>(operator) {
protected abstract fun Solve.Request<E>.computeOne(): Solve.Response

final override fun Solve.Request<E>.computeAll(): Sequence<Solve.Response> {
return sequenceOf(computeOne())
}
}

abstract class Functional<E : ExecutionContext>(operator: String) : NonBacktrackable<E>(operator) {
protected abstract fun Solve.Request<E>.computeOneSubstitution(): Substitution

final override fun Solve.Request<E>.computeOne(): Solve.Response {
return replyWith(computeOneSubstitution())
}
}

abstract class Predicative<E : ExecutionContext>(operator: String) : NonBacktrackable<E>(operator) {
protected abstract fun Solve.Request<E>.compute(): Boolean

final override fun Solve.Request<E>.computeOne(): Solve.Response {
return if (compute()) replySuccess() else replyFail()
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 86a6589

Please sign in to comment.