Checkpoint
This commit is contained in:
parent
438af6c053
commit
6eca9dfcb7
25 changed files with 309 additions and 113 deletions
|
@ -1,15 +1,9 @@
|
|||
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.components.Resolvent
|
||||
import prolog.components.terms.*
|
||||
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.
|
||||
*
|
||||
|
@ -18,18 +12,29 @@ typealias Body = List<Term>
|
|||
* @see [prolog.components.terms.Variable]
|
||||
* @see [Predicate]
|
||||
*/
|
||||
abstract class Clause(val head: Head, val body: Body = emptyList()) : Expression {
|
||||
abstract class Clause(private val head: Head, private val body: Body? = null) : Resolvent {
|
||||
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 solve(goal: Goal): Boolean {
|
||||
val unification = unify(goal, head)
|
||||
|
||||
if (unification.isEmpty) {
|
||||
return false
|
||||
}
|
||||
|
||||
val substitutions = unification.get()
|
||||
|
||||
val proven = body == null || body.prove()
|
||||
|
||||
substitutions.forEach { (variable, _) -> variable.unbind() }
|
||||
|
||||
return proven
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return when {
|
||||
body.isEmpty() -> head.toString()
|
||||
else -> "$head :- ${body.joinToString(", ")}"
|
||||
body == null -> head.toString()
|
||||
else -> "$head :- $body"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue