Checkpoint
This commit is contained in:
parent
39c3af4ba5
commit
da21d890fb
39 changed files with 1166 additions and 48 deletions
35
src/prolog/components/expressions/Clause.kt
Normal file
35
src/prolog/components/expressions/Clause.kt
Normal file
|
@ -0,0 +1,35 @@
|
|||
package prolog.components.expressions
|
||||
|
||||
import prolog.components.Functor
|
||||
import prolog.components.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(", ")}"
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue