This commit is contained in:
Tibo De Peuter 2025-05-07 20:22:14 +02:00
parent dff53b4e68
commit 65c4d925d3
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
15 changed files with 270 additions and 83 deletions

View file

@ -1,8 +1,8 @@
package prolog.ast.logic
import prolog.Answers
import prolog.ast.Database.Program
import prolog.Substitutions
import prolog.ast.Database.Program
import prolog.ast.terms.*
import prolog.builtins.True
import prolog.flags.AppliedCut
@ -20,7 +20,7 @@ import prolog.logic.unifyLazy
* @see [Predicate]
*/
abstract class Clause(var head: Head, var body: Body) : Term, Resolvent {
val functor: Functor = head.functor
val functor: FunctorInfo = head.functor
override fun solve(goal: Goal, subs: Substitutions): Answers = sequence {
// If the clause is a rule, unify the goal with the head and then try to prove the body.
@ -45,7 +45,14 @@ abstract class Clause(var head: Head, var body: Body) : Term, Resolvent {
onSuccess = { bodySubs ->
// If the body can be proven, yield the (combined) substitutions
val goalToHeadResult = goalToHeadSubs.mapValues { applySubstitution(it.value, bodySubs) }
val headResult = headToGoalSubs.filterKeys { key -> goalToHeadSubs.any { occurs(key as Variable, it.value) } }
val headResult = headToGoalSubs.filterKeys { key ->
goalToHeadSubs.any {
occurs(
key as Variable,
it.value
)
}
}
yield(Result.success(goalToHeadResult + headResult))
},
onFailure = { error ->

View file

@ -2,25 +2,25 @@ package prolog.ast.logic
import prolog.Answers
import prolog.Substitutions
import prolog.ast.terms.Functor
import prolog.ast.terms.FunctorInfo
import prolog.ast.terms.Goal
import prolog.flags.AppliedCut
/**
* Collection of [Clause]s with the same [Functor].
* Collection of [Clause]s with the same [FunctorInfo].
*
* If a goal is proved, the system looks for a predicate with the same functor, then uses indexing
* to select candidate clauses and then tries these clauses one-by-one.
*/
class Predicate : Resolvent {
val functor: Functor
val functor: FunctorInfo
val clauses: MutableList<Clause>
var dynamic = false
/**
* Creates a predicate with the given functor and an empty list of clauses.
*/
constructor(functor: Functor, dynamic: Boolean = false) {
constructor(functor: FunctorInfo, dynamic: Boolean = false) {
this.functor = functor
this.clauses = mutableListOf()
this.dynamic = dynamic