Continuations

This commit is contained in:
Tibo De Peuter 2025-05-09 14:02:03 +02:00
parent 88c90220fe
commit 026218ddbd
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
13 changed files with 255 additions and 15 deletions

View file

@ -9,6 +9,7 @@ import prolog.ast.terms.Atom
import prolog.ast.terms.Body
import prolog.ast.terms.Goal
import prolog.flags.AppliedCut
import prolog.flags.AppliedShift
import prolog.logic.applySubstitution
import prolog.logic.numbervars
@ -46,7 +47,7 @@ class Cut() : Atom("!") {
/**
* Conjunction (and). True if both Goal1 and Goal2 are true.
*/
class Conjunction(val left: LogicOperand, private val right: LogicOperand) :
open class Conjunction(val left: LogicOperand, private val right: LogicOperand) :
LogicOperator(Atom(","), left, right) {
override fun satisfy(subs: Substitutions): Answers = sequence {
// Satisfy the left part first, which either succeeds or fails
@ -93,6 +94,36 @@ class Conjunction(val left: LogicOperand, private val right: LogicOperand) :
yield(Result.failure(it))
}
) ?: yield(Result.failure(AppliedCut()))
} else if (exception is AppliedShift) {
if (exception.cont == null) {
// Goal should be our right operand
yield(Result.failure(AppliedShift(exception.subs, exception.ball, right as Goal)))
} else {
val leftSubs = exception.subs
right.satisfy(subs + leftSubs).forEach { right ->
right.fold(
// If the right part succeeds, yield the result with the left substitutions
onSuccess = { rightSubs ->
yield(Result.success(leftSubs + rightSubs))
},
// If the right part fails, check if it's a cut
onFailure = { exception ->
if (exception is AppliedCut) {
if (exception.subs != null) {
// If it's a cut, yield the result with the left substitutions
yield(Result.failure(AppliedCut(leftSubs + exception.subs)))
} else {
yield(Result.failure(AppliedCut()))
}
return@sequence
}
// If it's not a cut, yield the failure
yield(Result.failure(exception))
}
)
}
}
} else {
// 2. Any other failure should be returned as is
yield(Result.failure(exception))

View file

@ -0,0 +1,66 @@
package prolog.builtins
import prolog.Answers
import prolog.Substitutions
import prolog.ast.arithmetic.Integer
import prolog.ast.terms.Atom
import prolog.ast.terms.CompoundTerm
import prolog.ast.terms.Goal
import prolog.ast.terms.Structure
import prolog.ast.terms.Term
import prolog.flags.AppliedShift
import prolog.logic.unifyLazy
class Reset(private val goal: Goal, private val term1: Term, private val cont: Term) :
Structure(Atom("reset"), listOf(goal, term1, cont)) {
override fun satisfy(subs: Substitutions): Answers = sequence {
goal.satisfy(subs).forEach { goalResult ->
goalResult.fold(
// If Goal succeeds, then reset/3 also succeeds and binds Cont and Term1 to 0.
onSuccess = { goalSubs ->
// Unify Cont with 0
unifyLazy(cont, Integer(0), goalSubs).forEach { contResult ->
contResult.map { contSubs ->
yield(Result.success(goalSubs + contSubs))
// // Unify Term1 with 0
// unifyLazy(term1, Integer(0), goalSubs + contSubs).forEach { termResult ->
// termResult.map { termSubs ->
// yield(Result.success(goalSubs + termSubs + contSubs))
// }
// }
}
}
// TODO
// val conjunction = Conjunction(Unify(term1, Integer(0)), Unify(cont, Integer(0)))
// yieldAll(conjunction.satisfy(goalSubs))
},
onFailure = { failure ->
// If at some point Goal calls shift(Term2), then its further execution is suspended.
// Reset/3 succeeds immediately, binding Term1 to Term2 and Cont to the remainder of Goal.
// If Goal fails, then reset also fails.
// yield(Result.failure(failure))
// Unify Term1 with the ball
if (failure is AppliedShift) {
require(failure.cont != null) { "Shift must have a continuation" }
val newSubs: Substitutions = mapOf(
term1 to failure.ball,
cont to failure.cont
)
yield(Result.success(failure.subs + newSubs))
} else {
yield(Result.failure(failure))
}
}
)
}
}
}
/**
* Variables that have been bound during the procedure called by reset/3 stay bound after a shift/1:
*/
class Shift(private val ball: Term) : Structure(Atom("shift"), listOf(ball)) {
override fun satisfy(subs: Substitutions): Answers = sequenceOf(Result.failure(AppliedShift(subs, ball)))
}

View file

@ -50,6 +50,7 @@ object Nl : Atom("nl"), Satisfiable {
class WriteLn(private val term: Term) : Conjunction(Write(term), Nl) {
constructor(message: String) : this(Atom(message))
override fun applySubstitution(subs: Substitutions): WriteLn = WriteLn(applySubstitution(term, subs))
override fun toString(): String = "writeln($term)"
}
/**

View file

@ -7,7 +7,9 @@ import prolog.ast.lists.List.Cons
import prolog.ast.lists.List.Empty
import prolog.ast.terms.Atom
import prolog.ast.terms.Operator
import prolog.ast.terms.Structure
import prolog.ast.terms.Term
import prolog.logic.applySubstitution
class Member(private val element: Term, private val list: List) : Operator(Atom("member"), element, list) {
private var solution: Operator = when (list) {
@ -20,5 +22,10 @@ class Member(private val element: Term, private val list: List) : Operator(Atom(
override fun satisfy(subs: Substitutions): Answers = solution.satisfy(subs)
override fun applySubstitution(subs: Substitutions): Member = Member(
applySubstitution(element, subs),
applySubstitution(list, subs) as List
)
override fun toString(): String = "$element$list"
}

View file

@ -5,10 +5,15 @@ import prolog.Substitutions
import prolog.ast.terms.Atom
import prolog.ast.terms.Goal
import prolog.ast.terms.Operator
import prolog.ast.terms.Term
import prolog.flags.AppliedCut
import prolog.logic.applySubstitution
class Call(private val goal: Goal) : Operator(Atom("call"), null, goal) {
override fun satisfy(subs: Substitutions): Answers = goal.satisfy(subs)
class Call(private val goal: Term) : Operator(Atom("call"), null, goal) {
override fun satisfy(subs: Substitutions): Answers {
val appliedGoal = applySubstitution(goal, subs) as Goal
return appliedGoal.satisfy(subs)
}
}
/**

View file

@ -0,0 +1,14 @@
package prolog.flags
import prolog.Substitutions
import prolog.ast.terms.Goal
import prolog.ast.terms.Term
/**
* An exception that indicates that a shift has been applied in the Prolog engine.
*/
data class AppliedShift(
val subs: Substitutions,
val ball: Term,
val cont: Term? = null,
) : Throwable()