All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.tuprolog.solve.primitive.PredicateWithoutArguments.kt Maven / Gradle / Ivy

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 zero argument */
abstract class PredicateWithoutArguments(operator: String) : PrimitiveWrapper(operator, 0) {
    /** Template method that should compute the response of the application of this predicate to a term [Term] */
    protected abstract fun Solve.Request.computeAll(): Sequence

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

    abstract class WithoutSideEffects(operator: String) : PredicateWithoutArguments(operator) {
        protected abstract fun Solve.Request.computeAllSubstitutions(): Sequence

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

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

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

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

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy