REPL checkpoint

This commit is contained in:
Tibo De Peuter 2025-04-18 20:36:11 +02:00
parent 69c156024a
commit 1b3280a947
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
21 changed files with 503 additions and 34 deletions

View file

@ -1,5 +1,6 @@
package prolog
import Debug
import prolog.ast.logic.Clause
import prolog.ast.logic.Predicate
import prolog.ast.logic.Resolvent
@ -12,13 +13,16 @@ typealias Database = Program
* Prolog Program or database.
*/
object Program: Resolvent {
private var predicates: Map<Functor, Predicate> = emptyMap()
var predicates: Map<Functor, Predicate> = emptyMap()
init {
setup()
}
private fun setup() {
if (Debug.on) {
println("Setting up Prolog program...")
}
// Initialize the program with built-in predicates
load(listOf(
))

View file

@ -18,7 +18,7 @@ import prolog.logic.unifyLazy
* @see [prolog.ast.terms.Variable]
* @see [Predicate]
*/
abstract class Clause(private val head: Head, private val body: Body) : Resolvent {
abstract class Clause(val head: Head, val body: Body) : Resolvent {
val functor: Functor = head.functor
override fun solve (goal: Goal, subs: Substitutions): Answers = sequence {

View file

@ -39,6 +39,11 @@ class Predicate : Resolvent {
*/
fun add(clause: Clause) {
require(clause.functor == functor) { "Clause functor does not match predicate functor" }
if (Debug.on) {
println("Adding clause $clause to predicate $functor")
}
clauses.add(clause)
}

View file

@ -1,7 +1,5 @@
package prolog.ast.terms
import prolog.ast.arithmetic.Expression
typealias Operand = Term
abstract class Operator(

View file

@ -39,7 +39,7 @@ class Cut() : Atom("!") {
/**
* Conjunction (and). True if both Goal1 and Goal2 are true.
*/
class Conjunction(private val left: LogicOperand, private val right: LogicOperand) :
class Conjunction(val left: LogicOperand, private val right: LogicOperand) :
LogicOperator(Atom(","), left, right) {
override fun satisfy(subs: Substitutions): Answers = sequence {
// Satisfy the left part first, which either succeeds or fails