Cleanup 3

This commit is contained in:
Tibo De Peuter 2025-05-09 19:49:37 +02:00
parent 3c938749d0
commit a5bd38ef01
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
6 changed files with 62 additions and 9 deletions

View file

@ -2,7 +2,6 @@ package prolog.builtins
import prolog.Answers
import prolog.Substitutions
import prolog.ast.terms.Atom
import prolog.ast.terms.Goal
import prolog.ast.terms.Operator
import prolog.ast.terms.Term
@ -16,10 +15,18 @@ class Call(private val goal: Term) : Operator("call", rightOperand = goal) {
}
}
/**
* Make a possibly nondeterministic [Goal] semideterministic, i.e. succeed at most once.
*/
class Once(private val goal: Term) : Operator("once", rightOperand = goal) {
private val conjunction = Conjunction(Call(goal), Cut())
override fun satisfy(subs: Substitutions): Answers = conjunction.satisfy(subs).take(1)
}
/**
* Calls [Goal] once, but succeeds, regardless of whether Goal succeeded or not.
*/
class Ignore(goal: Goal) : Operator("ignore", rightOperand = goal) {
class Ignore(goal: Term) : Operator("ignore", rightOperand = goal) {
private val disjunction = Disjunction(
Conjunction(Call(goal), Cut()),
True