Checkpoint
This commit is contained in:
parent
e3c84e1761
commit
e73e5cbfc8
32 changed files with 1354 additions and 92 deletions
|
@ -1,77 +0,0 @@
|
|||
package prolog.builtins
|
||||
|
||||
import prolog.ast.terms.*
|
||||
import prolog.logic.Substituted
|
||||
|
||||
/**
|
||||
* Always fail.
|
||||
*/
|
||||
class Fail : Atom("fail"), Body {
|
||||
override fun prove(subs: Substituted): Sequence<Substituted> = emptySequence()
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as fail, but the name has a more declarative connotation.
|
||||
*/
|
||||
typealias False = Fail
|
||||
|
||||
/**
|
||||
* Always succeed.
|
||||
*/
|
||||
class True : Atom("true"), Body {
|
||||
override fun prove(subs: Substituted): Sequence<Substituted> = sequenceOf(emptyMap())
|
||||
}
|
||||
|
||||
// TODO Repeat/0
|
||||
|
||||
// TODO !/0 (Cut)
|
||||
|
||||
/**
|
||||
* Conjunction (and). True if both Goal1 and Goal2 are true.
|
||||
*/
|
||||
class Conjunction(leftOperand: Operand, rightOperand: Operand) : Operator(Atom(","), leftOperand, rightOperand) {
|
||||
override fun prove(subs: Substituted): Sequence<Substituted> = sequence {
|
||||
if (leftOperand != null) {
|
||||
leftOperand.prove(subs).forEach { left ->
|
||||
rightOperand.prove(subs + left).forEach { right ->
|
||||
yield(left + right)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
yieldAll(rightOperand.prove(subs))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disjunction (or). True if either Goal1 or Goal2 succeeds.
|
||||
*/
|
||||
open class Disjunction(leftOperand: Operand, rightOperand: Operand) : Operator(Atom(";"), leftOperand, rightOperand) {
|
||||
override fun prove(subs: Substituted): Sequence<Substituted> = sequence {
|
||||
if (leftOperand != null) {
|
||||
yieldAll(leftOperand.prove(subs))
|
||||
}
|
||||
yieldAll(rightOperand.prove(subs))
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated("Use Disjunction instead")
|
||||
class Bar(leftOperand: Operand, rightOperand: Operand) : Disjunction(leftOperand, rightOperand)
|
||||
|
||||
// TODO ->
|
||||
|
||||
// TODO *->
|
||||
|
||||
/**
|
||||
* True if 'Goal' cannot be proven.
|
||||
*/
|
||||
class Not(goal: Goal) : Operator(Atom("\\+"), rightOperand = goal) {
|
||||
override fun prove(subs: Substituted): Sequence<Substituted> {
|
||||
// If the goal can be proven, return an empty sequence
|
||||
if (rightOperand.prove(subs).toList().isNotEmpty()) {
|
||||
return emptySequence()
|
||||
}
|
||||
// If the goal cannot be proven, return a sequence with an empty map
|
||||
return sequenceOf(emptyMap())
|
||||
}
|
||||
}
|
Reference in a new issue