commonMain.it.unibo.tuprolog.solve.primitive.Primitive.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solve-jvm Show documentation
Show all versions of solve-jvm Show documentation
Resolution-agnostic API for logic solvers
package it.unibo.tuprolog.solve.primitive
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Signature
import kotlin.js.JsName
import kotlin.jvm.JvmStatic
/** A typealias for a primitive function that accepts a [Solve.Request] and returns a Sequence of [Solve.Response]s */
fun interface Primitive {
@JsName("solve")
fun solve(request: Solve.Request): Sequence
companion object {
@JsName("of")
@JvmStatic
fun of(function: (Solve.Request) -> Sequence): Primitive = Primitive(function)
/**
* Creates a new [Primitive], behaving exactly as given [uncheckedPrimitive], but accepting only provided [supportedSignature]
* as [Solve.Request] signature, throwing [IllegalArgumentException] otherwise
*/
@JsName("enforcingSignature")
@JvmStatic
fun enforcingSignature(
supportedSignature: Signature,
uncheckedPrimitive: (Solve.Request) -> Sequence,
): Primitive =
Primitive {
// TODO see TODO in "Signature"; here should be called that method to check if primitive could execute
@Suppress("UNCHECKED_CAST")
when (it.signature) {
supportedSignature -> uncheckedPrimitive(it as Solve.Request)
else -> throw IllegalArgumentException(
"This primitive supports only this signature `$supportedSignature`",
)
}
}
@JsName("enforcingSignatureForPrimitive")
@JvmStatic
fun enforcingSignature(
supportedSignature: Signature,
uncheckedPrimitive: Primitive,
): Primitive = enforcingSignature(supportedSignature, uncheckedPrimitive::solve)
}
}