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

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

There is a newer version: 1.0.4
Show newest version
package it.unibo.tuprolog.solve.primitive

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

abstract class QuinaryRelation(operator: String) : PrimitiveWrapper(operator, 5) {
    /** Template method aimed at computing the application of this relation to three [Term]s */
    protected abstract fun Solve.Request.computeAll(
        first: Term,
        second: Term,
        third: Term,
        fourth: Term,
        fifth: Term,
    ): Sequence

    final override fun uncheckedImplementation(request: Solve.Request): Sequence {
        return request.computeAll(
            request.arguments[0],
            request.arguments[1],
            request.arguments[2],
            request.arguments[3],
            request.arguments[4],
        )
    }

    abstract class WithoutSideEffects(operator: String) : QuinaryRelation(operator) {
        protected abstract fun Solve.Request.computeAllSubstitutions(
            first: Term,
            second: Term,
            third: Term,
            fourth: Term,
            fifth: Term,
        ): Sequence

        final override fun Solve.Request.computeAll(
            first: Term,
            second: Term,
            third: Term,
            fourth: Term,
            fifth: Term,
        ): Sequence {
            return computeAllSubstitutions(first, second, third, fourth, fifth).map { replyWith(it) }
        }
    }

    abstract class NonBacktrackable(operator: String) : QuinaryRelation(operator) {
        protected abstract fun Solve.Request.computeOne(
            first: Term,
            second: Term,
            third: Term,
            fourth: Term,
            fifth: Term,
        ): Solve.Response

        final override fun Solve.Request.computeAll(
            first: Term,
            second: Term,
            third: Term,
            fourth: Term,
            fifth: Term,
        ): Sequence {
            return sequenceOf(computeOne(first, second, third, fourth, fifth))
        }
    }

    abstract class Functional(operator: String) : NonBacktrackable(operator) {
        protected abstract fun Solve.Request.computeOneSubstitution(
            first: Term,
            second: Term,
            third: Term,
            fourth: Term,
            fifth: Term,
        ): Substitution

        final override fun Solve.Request.computeOne(
            first: Term,
            second: Term,
            third: Term,
            fourth: Term,
            fifth: Term,
        ): Solve.Response {
            return replyWith(computeOneSubstitution(first, second, third, fourth, fifth))
        }
    }

    abstract class Predicative(operator: String) : NonBacktrackable(operator) {
        protected abstract fun Solve.Request.compute(
            first: Term,
            second: Term,
            third: Term,
            fourth: Term,
            fifth: Term,
        ): Boolean

        final override fun Solve.Request.computeOne(
            first: Term,
            second: Term,
            third: Term,
            fourth: Term,
            fifth: Term,
        ): Solve.Response {
            return if (compute(first, second, third, fourth, fifth)) replySuccess() else replyFail()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy