commonMain.it.unibo.tuprolog.solve.primitive.UnaryPredicate.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solve-jvm Show documentation
Show all versions of solve-jvm Show documentation
Resolution-agnostic API for logic solvers
package it.unibo.tuprolog.solve.primitive
import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
/** A base class to implement predicates with one argument */
abstract class UnaryPredicate(operator: String) : PrimitiveWrapper(operator, 1) {
/** Template method aimed at computing the application of this predicate to a [Term] */
protected abstract fun Solve.Request.computeAll(first: Term): Sequence
final override fun uncheckedImplementation(request: Solve.Request): Sequence {
return request.computeAll(request.arguments[0])
}
abstract class WithoutSideEffects(operator: String) : UnaryPredicate(operator) {
protected abstract fun Solve.Request.computeAllSubstitutions(first: Term): Sequence
final override fun Solve.Request.computeAll(first: Term): Sequence {
return computeAllSubstitutions(first).map { replyWith(it) }
}
}
abstract class NonBacktrackable(operator: String) : UnaryPredicate(operator) {
protected abstract fun Solve.Request.computeOne(first: Term): Solve.Response
final override fun Solve.Request.computeAll(first: Term): Sequence {
return sequenceOf(computeOne(first))
}
}
abstract class Functional(operator: String) : NonBacktrackable(operator) {
protected abstract fun Solve.Request.computeOneSubstitution(first: Term): Substitution
final override fun Solve.Request.computeOne(first: Term): Solve.Response {
return replyWith(computeOneSubstitution(first))
}
}
abstract class Predicative(operator: String) : NonBacktrackable(operator) {
protected abstract fun Solve.Request.compute(first: Term): Boolean
final override fun Solve.Request.computeOne(first: Term): Solve.Response {
return if (compute(first)) replySuccess() else replyFail()
}
}
}