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

commonMain.it.unibo.tuprolog.solve.streams.stdlib.primitive.Not.kt Maven / Gradle / Ivy

Go to download

Experimental, functional-programming-based implementation of Prolog's SLDNF resolution principle

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

import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.solve.Solution
import it.unibo.tuprolog.solve.primitive.PrimitiveWrapper
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.streams.StreamsSolver
import it.unibo.tuprolog.solve.streams.solver.StreamsExecutionContext
import it.unibo.tuprolog.solve.streams.solver.newSolveRequest
import it.unibo.tuprolog.solve.streams.solver.replyWith

/**
 * Implementation of primitive handling `'\+'/1` behaviour
 *
 * @author Enrico
 */
internal object Not : PrimitiveWrapper("\\+", 1) {
    override fun uncheckedImplementation(request: Solve.Request): Sequence =
        sequence {
            val goalArgument = request.arguments.single()

            StreamsSolver.solveToResponses(
                request.newSolveRequest(Struct.of(Call.functor, goalArgument)),
            ).forEach { goalResponse ->
                when (goalResponse.solution) {
                    is Solution.Yes -> {
                        yield(request.replyFail())
                        return@sequence
                    }

                    is Solution.No -> yield(request.replySuccess(request.context.substitution))

                    else -> yield(request.replyWith(goalResponse))
                }
            }
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy