Skip to content

Commit

Permalink
fix funtor/3 wrt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Aug 31, 2020
1 parent 3a24578 commit 9ed465d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ class TestClassicFunctor : TestFunctor, SolverFactory by ClassicSolverFactory {
}

@Test
@Ignore
override fun testFunXNameYArity() {
prototype.testFunXNameYArity()
}

@Test
@Ignore
override fun testFunDecNum() {
prototype.testFunDecNum()
}
Expand Down Expand Up @@ -92,7 +90,6 @@ class TestClassicFunctor : TestFunctor, SolverFactory by ClassicSolverFactory {
}

@Test
@Ignore
override fun testFunNegativeArity() {
prototype.testFunNegativeArity()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ abstract class PrimitiveWrapper<C : ExecutionContext> : AbstractWrapper<Primitiv
}

fun <C : ExecutionContext> Solve.Request<C>.ensuringArgumentIsNonNegativeInteger(index: Int): Solve.Request<C> =
arguments[index].let { arg ->
ensuringArgumentIsInteger(index)
.arguments[index].let { arg ->
when {
arg !is Integer || arg.intValue < BigInteger.ZERO -> throw DomainError.forArgument(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.exception.error.InstantiationError
import it.unibo.tuprolog.solve.exception.error.TypeError
import it.unibo.tuprolog.solve.primitive.TernaryRelation
import it.unibo.tuprolog.unify.Unificator.Companion.mguWith
import org.gciatto.kt.math.BigInteger

/**
* Implementation of 'functor'/3 predicate
Expand All @@ -20,74 +22,38 @@ object Functor : TernaryRelation.Functional<ExecutionContext>("functor") {
third: Term
): Substitution = when (first) {
is Struct -> {
when (second) {
is Atom -> {
when (third) {
is Numeric -> {
if (second.value == first.functor && third.intValue.toInt() == first.arity)
Substitution.empty()
else
Substitution.failed()
}
is Var -> {
if (first.functor == second.value)
Substitution.of(third to Integer.of(first.arity))
else
Substitution.failed()
}
else -> {
// TODO expected here should be INTEGER | VARIABLE
throw TypeError.forArgument(context, signature, TypeError.Expected.INTEGER, third, 2)
}
}
}
is Var -> {
when (third) {
is Numeric -> {
if (first.arity == third.intValue.toInt())
Substitution.of(second to Atom.of(first.functor))
else
Substitution.failed()
}
is Var -> {
Substitution.of(
second to Atom.of(first.functor),
third to Integer.of(first.arity)
)
}
else -> {
// TODO expected here should be INTEGER | VARIABLE
throw TypeError.forArgument(context, signature, TypeError.Expected.INTEGER, third, 2)
}
}
}
else -> {
// TODO expected here should be ATOM | VARIABLE
throw TypeError.forArgument(context, signature, TypeError.Expected.ATOM, second, 1)
}
if (third !is Var) {
ensuringArgumentIsNonNegativeInteger(2)
}
(second mguWith Atom.of(first.functor)) + (third mguWith Integer.of(first.arity))
}
is Numeric -> {
if (third !is Var) {
ensuringArgumentIsNonNegativeInteger(2)
}
(first mguWith second) + (third mguWith Integer.of(0))
}
is Var -> {
when (second) {
is Atom -> {
when (third) {
is Numeric -> {
Substitution.of(first to Struct.template(second.value, third.intValue.toInt()))
}
is Var -> {
throw InstantiationError.forArgument(context, signature, 2, third)
}
else -> {
// TODO expected here should be INTEGER | VARIABLE
throw TypeError.forArgument(context, signature, TypeError.Expected.INTEGER, third, 2)
}
ensuringArgumentIsInstantiated(2)
ensuringArgumentIsNonNegativeInteger(2)
Substitution.of(first to Struct.template(second.value, (third as Integer).intValue.toInt()))
}
is Numeric -> {
ensuringArgumentIsInstantiated(2)
ensuringArgumentIsNonNegativeInteger(2)

if ((third as Integer).intValue == BigInteger.ZERO) {
Substitution.of(first to second) + (third mguWith Integer.of(0))
} else {
throw TypeError.forArgument(context, signature, TypeError.Expected.ATOM, second, 1)
}
}
is Var -> {
throw InstantiationError.forArgument(context, signature, 1, second)
}
else -> {
// TODO expected here should be ATOM | VARIABLE
throw TypeError.forArgument(context, signature, TypeError.Expected.ATOM, second, 1)
}
}
Expand Down

0 comments on commit 9ed465d

Please sign in to comment.