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

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

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

import it.unibo.tuprolog.core.Integer
import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.primitive.UnaryPredicate
import org.gciatto.kt.math.BigInteger

object Natural : UnaryPredicate("natural") {
    override fun Solve.Request.computeAll(first: Term): Sequence =
        when (first) {
            is Var -> generateValues().map { replySuccess(Substitution.of(first, it)) }
            is Integer -> sequenceOf(replyWith(checkValue(first)))
            else -> sequenceOf(replyFail())
        }

    private fun generateValues(): Sequence =
        generateSequence(BigInteger.ZERO) {
            it + BigInteger.ONE
        }.map { Integer.of(it) }

    private fun checkValue(value: Integer): Boolean = value.intValue.signum >= 0
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy