This commit is contained in:
Tibo De Peuter 2025-05-15 21:29:02 +02:00
parent 28168cb0f1
commit e5c61c89da
5 changed files with 28 additions and 23 deletions

View file

@ -3,8 +3,9 @@ package prolog.ast.arithmetic
import prolog.Answers import prolog.Answers
import prolog.Substitutions import prolog.Substitutions
import prolog.ast.logic.LogicOperand import prolog.ast.logic.LogicOperand
import prolog.ast.terms.Body
data class Integer(override val value: Int) : Number, LogicOperand() { data class Integer(override val value: Int) : Number, Body, LogicOperand() {
// Integers are already evaluated // Integers are already evaluated
override fun simplify(subs: Substitutions): Simplification = Simplification(this, this) override fun simplify(subs: Substitutions): Simplification = Simplification(this, this)

View file

@ -1,5 +1,8 @@
package prolog.ast.terms package prolog.ast.terms
import prolog.Substitutions
import prolog.ast.logic.Satisfiable import prolog.ast.logic.Satisfiable
interface Body : Term, Satisfiable interface Body : Term, Satisfiable {
override fun applySubstitution(subs: Substitutions): Body
}

View file

@ -40,8 +40,8 @@ class EvaluatesToDifferent(private val left: Expression, private val right: Expr
} }
override fun applySubstitution(subs: Substitutions): EvaluatesToDifferent = EvaluatesToDifferent( override fun applySubstitution(subs: Substitutions): EvaluatesToDifferent = EvaluatesToDifferent(
left.applySubstitution(subs) as Expression, applySubstitution(left, subs),
right.applySubstitution(subs) as Expression applySubstitution(right, subs)
) )
} }
@ -63,8 +63,8 @@ class EvaluatesTo(private val left: Expression, private val right: Expression) :
} }
override fun applySubstitution(subs: Substitutions): EvaluatesTo = EvaluatesTo( override fun applySubstitution(subs: Substitutions): EvaluatesTo = EvaluatesTo(
left.applySubstitution(subs) as Expression, applySubstitution(left, subs),
right.applySubstitution(subs) as Expression applySubstitution(right, subs)
) )
} }
@ -89,8 +89,8 @@ class Is(val number: Expression, val expr: Expression) :
} }
override fun applySubstitution(subs: Substitutions): Is = Is( override fun applySubstitution(subs: Substitutions): Is = Is(
number.applySubstitution(subs) as Expression, applySubstitution(number, subs),
expr.applySubstitution(subs) as Expression applySubstitution(expr, subs)
) )
} }
@ -117,8 +117,8 @@ open class Add(private val expr1: Expression, private val expr2: Expression) :
} }
override fun applySubstitution(subs: Substitutions): Add = Add( override fun applySubstitution(subs: Substitutions): Add = Add(
expr1.applySubstitution(subs) as Expression, applySubstitution(expr1, subs),
expr2.applySubstitution(subs) as Expression applySubstitution(expr2, subs)
) )
} }
@ -140,8 +140,8 @@ open class Subtract(private val expr1: Expression, private val expr2: Expression
} }
override fun applySubstitution(subs: Substitutions): Subtract = Subtract( override fun applySubstitution(subs: Substitutions): Subtract = Subtract(
expr1.applySubstitution(subs) as Expression, applySubstitution(expr1, subs),
expr2.applySubstitution(subs) as Expression applySubstitution(expr2, subs)
) )
} }
@ -158,8 +158,8 @@ class Multiply(val expr1: Expression, val expr2: Expression) :
} }
override fun applySubstitution(subs: Substitutions): Multiply = Multiply( override fun applySubstitution(subs: Substitutions): Multiply = Multiply(
expr1.applySubstitution(subs) as Expression, applySubstitution(expr1, subs),
expr2.applySubstitution(subs) as Expression applySubstitution(expr2, subs)
) )
} }
@ -173,8 +173,8 @@ class Divide(private val expr1: Expression, private val expr2: Expression) :
} }
override fun applySubstitution(subs: Substitutions): Divide = Divide( override fun applySubstitution(subs: Substitutions): Divide = Divide(
expr1.applySubstitution(subs) as Expression, applySubstitution(expr1, subs),
expr2.applySubstitution(subs) as Expression applySubstitution(expr2, subs)
) )
} }
@ -206,9 +206,9 @@ class Between(private val expr1: Expression, private val expr2: Expression, priv
} }
override fun applySubstitution(subs: Substitutions): Between = Between( override fun applySubstitution(subs: Substitutions): Between = Between(
expr1.applySubstitution(subs) as Expression, applySubstitution(expr1, subs),
expr2.applySubstitution(subs) as Expression, applySubstitution(expr2, subs),
expr3.applySubstitution(subs) as Expression applySubstitution(expr3, subs)
) )
override fun toString(): String = "$expr1..$expr3..$expr2" override fun toString(): String = "$expr1..$expr3..$expr2"

View file

@ -55,7 +55,7 @@ open class Conjunction(val left: LogicOperand, private val right: LogicOperand)
right.fold( right.fold(
// If the right part succeeds, yield the result with the left substitutions // If the right part succeeds, yield the result with the left substitutions
onSuccess = { rightSubs -> onSuccess = { rightSubs ->
yield(Result.success(leftSubs + rightSubs)) yield(Result.success(rightSubs + leftSubs))
}, },
onFailure = { exception -> onFailure = { exception ->
// If the right part fails, check if it's a cut // If the right part fails, check if it's a cut
@ -74,8 +74,8 @@ open class Conjunction(val left: LogicOperand, private val right: LogicOperand)
} }
fun findNextCutSolution(appliedCut: AppliedCut): Answers = sequence { fun findNextCutSolution(appliedCut: AppliedCut): Answers = sequence {
val leftSubs = appliedCut.subs val leftSubs = appliedCut.subs ?: emptyMap()
right.satisfy(subs + (appliedCut.subs!!)).firstOrNull()?.map { rightSubs -> right.satisfy(subs + leftSubs).firstOrNull()?.map { rightSubs ->
// If the right part succeeds, yield the result with the left substitutions // If the right part succeeds, yield the result with the left substitutions
yield(Result.success(leftSubs + rightSubs)) yield(Result.success(leftSubs + rightSubs))
return@sequence return@sequence

View file

@ -27,7 +27,7 @@ class Examples {
@Test @Test
fun debugHelper() { fun debugHelper() {
loader.load("examples/meta/continuations.pl") loader.load("examples/basics/summer.pl")
} }
@ParameterizedTest @ParameterizedTest
@ -60,6 +60,7 @@ class Examples {
Arguments.of("forall.pl", "Only alice likes pizza.\n"), Arguments.of("forall.pl", "Only alice likes pizza.\n"),
Arguments.of("fraternity.pl", "Citizen robespierre is eligible for the event.\nCitizen danton is eligible for the event.\nCitizen camus is eligible for the event.\n"), Arguments.of("fraternity.pl", "Citizen robespierre is eligible for the event.\nCitizen danton is eligible for the event.\nCitizen camus is eligible for the event.\n"),
Arguments.of("liberty.pl", "Give me Liberty, or give me Death!\nI disapprove of what you say, but I will defend to the death your right to say it.\nThe revolution devours its own children.\nSo this is how liberty dies, with thunderous applause.\n"), Arguments.of("liberty.pl", "Give me Liberty, or give me Death!\nI disapprove of what you say, but I will defend to the death your right to say it.\nThe revolution devours its own children.\nSo this is how liberty dies, with thunderous applause.\n"),
Arguments.of("summer.pl", "rest(0)\nsum(4)\nrest(4)\nsum(7)\nrest(7)\nsum(9)\nrest(9)\nsum(10)\n"),
Arguments.of("unification.pl", "While alice got an A, carol got an A, but bob did not get an A, dave did not get an A, unfortunately.\n"), Arguments.of("unification.pl", "While alice got an A, carol got an A, but bob did not get an A, dave did not get an A, unfortunately.\n"),
Arguments.of("write.pl", "gpl zegt: dag(wereld)\n"), Arguments.of("write.pl", "gpl zegt: dag(wereld)\n"),
) )