refactor: Herstructurering
This commit is contained in:
parent
1acd1cfb67
commit
e3c84e1761
33 changed files with 290 additions and 178 deletions
43
src/prolog/ast/terms/Structure.kt
Normal file
43
src/prolog/ast/terms/Structure.kt
Normal file
|
@ -0,0 +1,43 @@
|
|||
package prolog.ast.terms
|
||||
|
||||
import prolog.ast.logic.Resolvent
|
||||
import prolog.builtins.equivalent
|
||||
import prolog.logic.Substituted
|
||||
import prolog.logic.unifyLazy
|
||||
|
||||
typealias Argument = Term
|
||||
|
||||
typealias CompoundTerm = Structure
|
||||
|
||||
open class Structure(val name: Atom, val arguments: List<Argument>) : Goal(), Head, Body, Resolvent {
|
||||
override val functor: Functor = "${name.name}/${arguments.size}"
|
||||
|
||||
override fun solve(goal: Goal, subs: Substituted): Sequence<Substituted> {
|
||||
return unifyLazy(goal, this, subs)
|
||||
}
|
||||
|
||||
override fun compareTo(other: Term): Int {
|
||||
when (other) {
|
||||
is Structure -> {
|
||||
val arityComparison = arguments.size.compareTo(other.arguments.size)
|
||||
if (arityComparison != 0) return arityComparison
|
||||
val nameComparison = name.compareTo(other.name)
|
||||
if (nameComparison != 0) return nameComparison
|
||||
arguments.zip(other.arguments).forEach { (arg1, arg2) ->
|
||||
val argsComparison = equivalent(arg1, arg2)
|
||||
if (!argsComparison) return arg1.compareTo(arg2)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
// Structure is always greater than other terms
|
||||
else -> return 1
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return when {
|
||||
arguments.isEmpty() -> name.name
|
||||
else -> "${name.name}(${arguments.joinToString(", ")})"
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue