fix: Evaluation

This commit is contained in:
Tibo De Peuter 2025-04-06 17:29:23 +02:00
parent d702b9b081
commit 1acd1cfb67
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
17 changed files with 189 additions and 120 deletions

View file

@ -1,18 +1,16 @@
package prolog.builtins
import prolog.Substitution
import prolog.Substituted
import prolog.components.expressions.Operand
import prolog.components.expressions.Operator
import prolog.components.terms.Atom
import prolog.components.terms.Body
import prolog.components.terms.Term
import prolog.components.terms.Variable
/**
* Always fail.
*/
class Fail: Atom("fail"), Body {
override fun prove(): Sequence<Substitution> = emptySequence()
override fun prove(subs: Substituted): Sequence<Substituted> = emptySequence()
}
/**
@ -24,7 +22,7 @@ typealias False = Fail
* Always succeed.
*/
class True: Atom("true"), Body {
override fun prove(): Sequence<Substitution> = sequenceOf(emptyMap())
override fun prove(subs: Substituted): Sequence<Substituted> = sequenceOf(emptyMap())
}
// TODO Repeat/0
@ -35,15 +33,15 @@ class True: Atom("true"), Body {
* Conjunction (and). True if both Goal1 and Goal2 are true.
*/
class Conjunction(leftOperand: Operand, rightOperand: Operand) : Operator(Atom(","), leftOperand, rightOperand) {
override fun prove(): Sequence<Substitution> = sequence {
override fun prove(subs: Substituted): Sequence<Substituted> = sequence {
if (leftOperand != null) {
leftOperand.prove().forEach { left ->
rightOperand.prove().forEach { right ->
leftOperand.prove(subs).forEach { left ->
rightOperand.prove(subs + left).forEach { right ->
yield(left + right)
}
}
} else {
yieldAll(rightOperand.prove())
yieldAll(rightOperand.prove(subs))
}
}
}
@ -52,10 +50,10 @@ class Conjunction(leftOperand: Operand, rightOperand: Operand) : Operator(Atom("
* Disjunction (or). True if either Goal1 or Goal2 succeeds.
*/
class Disjunction(leftOperand: Operand, rightOperand: Operand) : Operator(Atom(";"), leftOperand, rightOperand) {
override fun prove(): Sequence<Substitution> = sequence {
override fun prove(subs: Substituted): Sequence<Substituted> = sequence {
if (leftOperand != null) {
yieldAll(leftOperand.prove())
yieldAll(leftOperand.prove(subs))
}
yieldAll(rightOperand.prove())
yieldAll(rightOperand.prove(subs))
}
}