commonMain.it.unibo.tuprolog.solve.classic.stdlib.primitive.Throw.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solve-classic-jvm Show documentation
Show all versions of solve-classic-jvm Show documentation
Stable, state-machine-based implementation of Prolog's SLDNF resolution principle
package it.unibo.tuprolog.solve.classic.stdlib.primitive
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.exception.LogicError
import it.unibo.tuprolog.solve.exception.ResolutionException
import it.unibo.tuprolog.solve.exception.error.MessageError
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.primitive.UnaryPredicate
object Throw : UnaryPredicate("throw") {
private fun handleError(
context: ExecutionContext,
error: Term,
): ResolutionException =
when {
error is Struct && error.functor == "error" && error.arity in 1..2 -> {
LogicError.of(
context = context,
type = error[0] as Struct,
extraData = if (error.arity > 1) error[1] else null,
)
}
else -> MessageError.of(error, context)
}
override fun Solve.Request.computeAll(first: Term): Sequence {
return sequenceOf(
ensuringAllArgumentsAreInstantiated()
.replyException(handleError(context, arguments[0])),
)
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy