Cleanup 2

This commit is contained in:
Tibo De Peuter 2025-05-09 18:30:18 +02:00
parent a9bb6e0338
commit 3c938749d0
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
22 changed files with 299 additions and 110 deletions

View file

@ -21,7 +21,7 @@ import prolog.ast.lists.List.Cons
* Source: [SWI-Prolog Predicate functor/3](https://www.swi-prolog.org/pldoc/doc_for?object=functor/3)
*/
class FunctorOp(private val term: Term, private val functorName: Term, private val functorArity: Term) :
Structure(Atom("functor"), listOf(term, functorName, functorArity)) {
Structure("functor", term, functorName, functorArity) {
override fun satisfy(subs: Substitutions): Answers {
return when {
nonvariable(term, subs) -> {
@ -57,7 +57,7 @@ class FunctorOp(private val term: Term, private val functorName: Term, private v
}
class Arg(private val arg: Term, private val term: Term, private val value: Term) :
Structure(Atom("arg"), listOf(arg, term, value)) {
Structure("arg", arg, term, value) {
override fun satisfy(subs: Substitutions): Answers = sequence {
require(nonvariable(term, subs)) { "Arguments are not sufficiently instantiated" }
require(compound(term, subs)) {
@ -118,7 +118,7 @@ class Arg(private val arg: Term, private val term: Term, private val value: Term
* [SWI-Prolog Operator clause/2](https://www.swi-prolog.org/pldoc/doc_for?object=clause/2)
*/
class ClauseOp(private val head: Head, private val body: Body) :
Structure(Atom("clause"), listOf(head, body)) {
Structure("clause", head, body) {
override fun satisfy(subs: Substitutions): Answers = sequence {
require(nonvariable(head, subs)) { "Arguments are not sufficiently instantiated" }
@ -157,7 +157,7 @@ class ClauseOp(private val head: Head, private val body: Body) :
)
}
open class Univ(private val term: Term, private val list: Term) : Operator(Atom("=.."), term, list) {
open class Univ(private val term: Term, private val list: Term) : Operator("=..", term, list) {
override fun satisfy(subs: Substitutions): Answers {
return when {
nonvariable(term, subs) && nonvariable(list, subs) -> {
@ -189,11 +189,14 @@ open class Univ(private val term: Term, private val list: Term) : Operator(Atom(
list.size.value > 1 -> {
val head = list.head
val arguments = mutableListOf<Term>()
var tail: List? = list.tail
while (tail != null && tail !is Empty) {
var tail: Term? = list.tail
while (tail != null && tail is Cons) {
arguments.add(tail.head)
tail = tail.tail
}
if (tail != null && tail !is Empty) {
arguments.add(tail)
}
Structure(head as Atom, arguments)
}
@ -227,7 +230,7 @@ open class Univ(private val term: Term, private val list: Term) : Operator(Atom(
}
class NumberVars(private val term: Term, private val start: Integer, private val end: Term) :
Structure(Atom("numbervars"), listOf(term, start, end)) {
Structure("numbervars", term, start, end) {
private var yieldEnd: Boolean = true
constructor(term: Term) : this(term, Integer(0), AnonymousVariable.create()) {