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

commonMain.it.unibo.tuprolog.solve.stdlib.primitive.CharCode.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.Atom
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.BinaryRelation
import it.unibo.tuprolog.solve.primitive.Solve

object CharCode : BinaryRelation.Functional("char_code") {
    override fun Solve.Request.computeOneSubstitution(
        first: Term,
        second: Term,
    ): Substitution {
        return when {
            first is Var -> {
                ensuringArgumentIsInstantiated(1)
                ensuringArgumentIsCharCode(1)
                val atom: Atom =
                    Atom.of(
                        charArrayOf((second as Integer).intValue.toChar()).concatToString(),
                    )
                Substitution.of(first, atom)
            }
            else -> {
                ensuringArgumentIsInstantiated(0)
                ensuringArgumentIsChar(0)
                val result = (first as Atom).value[0].code
                mgu(second, Integer.of(result))
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy