commonMain.it.unibo.tuprolog.solve.stdlib.primitive.Functor.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.stdlib.primitive
import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Integer
import it.unibo.tuprolog.core.Numeric
import it.unibo.tuprolog.core.Struct
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.exception.error.InstantiationError
import it.unibo.tuprolog.solve.exception.error.TypeError
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.primitive.TernaryRelation
import org.gciatto.kt.math.BigInteger
/**
* Implementation of 'functor'/3 predicate
*/
object Functor : TernaryRelation.Functional("functor") {
override fun Solve.Request.computeOneSubstitution(
first: Term,
second: Term,
third: Term,
): Substitution =
when (first) {
is Struct -> {
if (third !is Var) {
ensuringArgumentIsArity(2)
}
mgu(second, Atom.of(first.functor)) + mgu(third, Integer.of(first.arity))
}
is Numeric -> {
if (third !is Var) {
ensuringArgumentIsArity(2)
}
mgu(first, second) + mgu(third, Integer.of(0))
}
is Var -> {
when (second) {
is Atom -> {
ensuringArgumentIsInstantiated(2)
ensuringArgumentIsArity(2)
Substitution.of(first to Struct.template(second.value, (third as Integer).intValue.toInt()))
}
is Numeric -> {
ensuringArgumentIsInstantiated(2)
ensuringArgumentIsArity(2)
val arity = (third as Integer)
if (arity.intValue == BigInteger.ZERO) {
Substitution.of(first to second) + mgu(third, Integer.of(0))
} else {
throw TypeError.forArgument(context, signature, TypeError.Expected.ATOM, second, 1)
}
}
is Var -> {
throw InstantiationError.forArgument(context, signature, second, 1)
}
else -> {
throw TypeError.forArgument(context, signature, TypeError.Expected.ATOMIC, second, 1)
}
}
}
else -> {
throw TypeError.forArgument(context, signature, TypeError.Expected.CALLABLE, first, 0)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy