35 lines
1 KiB
Kotlin
35 lines
1 KiB
Kotlin
package prolog.components.expressions
|
||
|
||
import prolog.components.terms.Functor
|
||
import prolog.components.terms.Goal
|
||
import prolog.components.terms.Head
|
||
import prolog.components.terms.Term
|
||
import prolog.unify
|
||
|
||
// TODO Change this to the right type, supporting operators and normal Clauses
|
||
// Probably needs a new interface or abstract class (?)
|
||
typealias Body = List<Term>
|
||
|
||
/**
|
||
* ‘Sentence’ of a Prolog program.
|
||
*
|
||
* A clause consists of a [Head] and body separated by the [neck][prolog.terms.Neck] operator, or it is a [Fact].
|
||
*
|
||
* @see [prolog.components.terms.Variable]
|
||
* @see [Predicate]
|
||
*/
|
||
abstract class Clause(val head: Head, val body: Body = emptyList()) : Expression {
|
||
val functor: Functor = head.functor
|
||
override fun evaluate(goal: Goal): Boolean {
|
||
val result = unify(goal, head)
|
||
// TODO Evaluate the body
|
||
return result.isEmpty
|
||
}
|
||
|
||
override fun toString(): String {
|
||
return when {
|
||
body.isEmpty() -> head.toString()
|
||
else -> "$head :- ${body.joinToString(", ")}"
|
||
}
|
||
}
|
||
}
|