Checkpoint
This commit is contained in:
parent
39c3af4ba5
commit
da21d890fb
39 changed files with 1166 additions and 48 deletions
|
@ -1,5 +0,0 @@
|
|||
package prolog
|
||||
|
||||
import prolog.terms.Variable
|
||||
|
||||
data class Result(val success: Boolean, val substitutions: List<Variable>)
|
20
src/prolog/builtins/control.kt
Normal file
20
src/prolog/builtins/control.kt
Normal file
|
@ -0,0 +1,20 @@
|
|||
package prolog.builtins
|
||||
|
||||
import prolog.components.terms.Atom
|
||||
|
||||
/**
|
||||
* Always fail.
|
||||
*/
|
||||
class Fail: Atom("fail")
|
||||
|
||||
/**
|
||||
* Same as fail, but the name has a more declarative connotation.
|
||||
*/
|
||||
typealias False = Fail
|
||||
|
||||
/**
|
||||
* Always succeed.
|
||||
*/
|
||||
class True: Atom("true")
|
||||
|
||||
// TODO Repeat/0
|
22
src/prolog/builtins/terms.kt
Normal file
22
src/prolog/builtins/terms.kt
Normal file
|
@ -0,0 +1,22 @@
|
|||
package prolog.builtins
|
||||
|
||||
import prolog.components.terms.Atom
|
||||
import prolog.components.terms.Term
|
||||
|
||||
/**
|
||||
* True when Term is a term with functor Name/Arity. If Term is a variable it is unified with a new term whose
|
||||
* arguments are all different variables (such a term is called a skeleton). If Term is atomic, Arity will be unified
|
||||
* with the integer 0, and Name will be unified with Term. Raises instantiation_error() if Term is unbound and
|
||||
* Name/Arity is insufficiently instantiated.
|
||||
*
|
||||
* SWI-Prolog also supports terms with arity 0, as in a() (see
|
||||
* [section 5](https://www.swi-prolog.org/pldoc/man?section=extensions)). Such terms must be processed using functor/4
|
||||
* or compound_name_arity/3. The predicate functor/3 and =../2 raise a domain_error when faced with these terms.
|
||||
* Without this precaution a round trip of a term with arity 0 over functor/3 would create an atom.
|
||||
*
|
||||
* Source: [SWI-Prolog Predicate functor/3](https://www.swi-prolog.org/pldoc/doc_for?object=functor/3)
|
||||
*/
|
||||
fun functor(term: Term, name: Atom, arity: Int): Boolean {
|
||||
// TODO Implement
|
||||
return true
|
||||
}
|
41
src/prolog/builtins/verification.kt
Normal file
41
src/prolog/builtins/verification.kt
Normal file
|
@ -0,0 +1,41 @@
|
|||
package prolog.builtins
|
||||
|
||||
import prolog.components.terms.CompoundTerm
|
||||
import prolog.components.terms.Term
|
||||
import prolog.components.terms.Variable
|
||||
|
||||
/**
|
||||
* True if [Term] is bound (i.e., not a variable) and is not compound.
|
||||
* Thus, atomic acts as if defined by:
|
||||
*
|
||||
* atomic(Term) :-
|
||||
* nonvar(Term),
|
||||
* \+ compound(Term).
|
||||
*/
|
||||
fun atomic(term: Term): Boolean = nonvariable(term) && !compound(term)
|
||||
|
||||
/**
|
||||
* True if [Term] is bound to a compound term.
|
||||
* See also functor/3 =../2, compound_name_arity/3 and compound_name_arguments/3.
|
||||
*/
|
||||
fun compound(term: Term): Boolean {
|
||||
val isCompound = term is CompoundTerm
|
||||
val isVariableCompound = term is Variable && term.alias().isPresent && compound(term.alias().get())
|
||||
return isCompound || isVariableCompound
|
||||
}
|
||||
|
||||
/**
|
||||
* True if [Term] currently is not a free variable.
|
||||
*/
|
||||
fun nonvariable(term: Term): Boolean = !variable(term)
|
||||
|
||||
/**
|
||||
* True if [Term] currently is a free variable.
|
||||
*/
|
||||
fun variable(term: Term): Boolean {
|
||||
if (term is Variable) {
|
||||
return term.alias().isEmpty || term.alias().get() === term || variable(term.alias().get())
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
10
src/prolog/builtins2/control/Conjunction.kt
Normal file
10
src/prolog/builtins2/control/Conjunction.kt
Normal file
|
@ -0,0 +1,10 @@
|
|||
package prolog.builtins2.control
|
||||
|
||||
import prolog.components.terms.Atom
|
||||
import prolog.components.Operand
|
||||
import prolog.components.Operator
|
||||
|
||||
/**
|
||||
* Conjunction (and). True if both Goal1 and Goal2 are true.
|
||||
*/
|
||||
class Conjunction(leftOperand: Operand, rightOperand: Operand) : Operator(Atom(","), leftOperand, rightOperand)
|
10
src/prolog/builtins2/control/Disjunction.kt
Normal file
10
src/prolog/builtins2/control/Disjunction.kt
Normal file
|
@ -0,0 +1,10 @@
|
|||
package prolog.builtins2.control
|
||||
|
||||
import prolog.components.terms.Atom
|
||||
import prolog.components.Operand
|
||||
import prolog.components.Operator
|
||||
|
||||
/**
|
||||
* Disjunction (or). True if either Goal1 or Goal2 succeeds.
|
||||
*/
|
||||
class Disjunction(leftOperand: Operand, rightOperand: Operand) : Operator(Atom(";"), leftOperand, rightOperand)
|
9
src/prolog/components/Functor.kt
Normal file
9
src/prolog/components/Functor.kt
Normal file
|
@ -0,0 +1,9 @@
|
|||
package prolog.components
|
||||
|
||||
import prolog.components.terms.Atom
|
||||
|
||||
data class Functor(val name: Atom, val arity: Int) {
|
||||
override fun toString(): String {
|
||||
return "${name.name}/${arity}"
|
||||
}
|
||||
}
|
18
src/prolog/components/Goal.kt
Normal file
18
src/prolog/components/Goal.kt
Normal file
|
@ -0,0 +1,18 @@
|
|||
package prolog.components
|
||||
|
||||
import prolog.components.terms.Term
|
||||
|
||||
/**
|
||||
* Question stated to the Prolog engine.
|
||||
*
|
||||
* A goal is either an [atom][prolog.components.terms.Atom] or a [compound term][prolog.components.terms.CompoundTerm].
|
||||
* A goal either [succeeds][prolog.builtins.True], in which case the variables in the compound terms have a binding,
|
||||
* or it fails if Prolog fails to prove it.
|
||||
*/
|
||||
interface Goal: Term {
|
||||
val functor: Functor
|
||||
|
||||
fun prove(): Boolean {
|
||||
return Program.query(this)
|
||||
}
|
||||
}
|
19
src/prolog/components/Operator.kt
Normal file
19
src/prolog/components/Operator.kt
Normal file
|
@ -0,0 +1,19 @@
|
|||
package prolog.components
|
||||
|
||||
import prolog.components.terms.Argument
|
||||
import prolog.components.terms.Atom
|
||||
|
||||
open class Operator(
|
||||
val symbol: Atom,
|
||||
val leftOperand: Operand? = null,
|
||||
val rightOperand: Operand
|
||||
) {
|
||||
override fun toString(): String {
|
||||
return when (leftOperand) {
|
||||
null -> "${symbol.name} $rightOperand"
|
||||
else -> "$leftOperand ${symbol.name} $rightOperand"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typealias Operand = Argument
|
62
src/prolog/components/Program.kt
Normal file
62
src/prolog/components/Program.kt
Normal file
|
@ -0,0 +1,62 @@
|
|||
package prolog.components
|
||||
|
||||
import prolog.builtins.True
|
||||
import prolog.components.expressions.Clause
|
||||
import prolog.components.expressions.Fact
|
||||
import prolog.components.expressions.Predicate
|
||||
|
||||
/**
|
||||
* Prolog Program or database.
|
||||
*/
|
||||
object Program {
|
||||
private var predicates: Map<Functor, Predicate> = emptyMap()
|
||||
|
||||
init {
|
||||
// Initialize the program with built-in predicates
|
||||
load(listOf(
|
||||
Fact(True())
|
||||
))
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a list of clauses into the program.
|
||||
*/
|
||||
fun load(clauses: List<Clause>) {
|
||||
for (clause in clauses) {
|
||||
val functor = clause.functor
|
||||
val predicate = predicates[functor]
|
||||
|
||||
if (predicate != null) {
|
||||
// If the predicate already exists, add the clause to it
|
||||
predicate.add(clause)
|
||||
} else {
|
||||
// If the predicate does not exist, create a new one
|
||||
predicates += Pair(functor, Predicate(listOf(clause)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun load(predicate: Predicate) {
|
||||
val functor = predicate.functor
|
||||
val existingPredicate = predicates[functor]
|
||||
|
||||
if (existingPredicate != null) {
|
||||
// If the predicate already exists, add the clauses to it
|
||||
existingPredicate.addAll(predicate.clauses)
|
||||
} else {
|
||||
// If the predicate does not exist, create a new one
|
||||
predicates += Pair(functor, predicate)
|
||||
}
|
||||
}
|
||||
|
||||
fun query(goal: Goal): Boolean {
|
||||
val functor = goal.functor
|
||||
val predicate = predicates[functor]
|
||||
?: // If the predicate does not exist, return false
|
||||
return false
|
||||
// If the predicate exists, evaluate the goal against it
|
||||
return predicate.evaluate(goal)
|
||||
}
|
||||
}
|
||||
|
||||
typealias Database = Program
|
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(", ")}"
|
||||
}
|
||||
}
|
||||
}
|
7
src/prolog/components/expressions/Expression.kt
Normal file
7
src/prolog/components/expressions/Expression.kt
Normal file
|
@ -0,0 +1,7 @@
|
|||
package prolog.components.expressions
|
||||
|
||||
import prolog.components.Goal
|
||||
|
||||
interface Expression {
|
||||
fun evaluate(goal: Goal): Boolean
|
||||
}
|
6
src/prolog/components/expressions/Fact.kt
Normal file
6
src/prolog/components/expressions/Fact.kt
Normal file
|
@ -0,0 +1,6 @@
|
|||
package prolog.components.expressions
|
||||
|
||||
import prolog.builtins.True
|
||||
import prolog.components.terms.Head
|
||||
|
||||
class Fact(head: Head) : Clause(head, listOf(True()))
|
54
src/prolog/components/expressions/Predicate.kt
Normal file
54
src/prolog/components/expressions/Predicate.kt
Normal file
|
@ -0,0 +1,54 @@
|
|||
package prolog.components.expressions
|
||||
|
||||
import prolog.components.Functor
|
||||
import prolog.components.Goal
|
||||
|
||||
/**
|
||||
* Collection of [Clause]s with the same [Functor].
|
||||
*
|
||||
* 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 : Expression {
|
||||
val functor: Functor
|
||||
val clauses: MutableList<Clause>
|
||||
|
||||
/**
|
||||
* Creates a predicate with the given functor and an empty list of clauses.
|
||||
*/
|
||||
constructor(functor: Functor) {
|
||||
this.functor = functor
|
||||
this.clauses = mutableListOf()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a predicate with the given clauses.
|
||||
*/
|
||||
constructor(clauses: List<Clause>) {
|
||||
this.functor = clauses.first().functor
|
||||
|
||||
require(clauses.all { it.functor == functor }) { "All clauses must have the same functor" }
|
||||
this.clauses = clauses.toMutableList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a clause to the predicate.
|
||||
*/
|
||||
fun add(clause: Clause) {
|
||||
require (clause.functor == functor) { "Clause functor does not match predicate functor" }
|
||||
clauses.add(clause)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a list of clauses to the predicate.
|
||||
*/
|
||||
fun addAll(clauses: List<Clause>) {
|
||||
require(clauses.all { it.functor == functor }) { "All clauses must have the same functor" }
|
||||
this.clauses.addAll(clauses)
|
||||
}
|
||||
|
||||
override fun evaluate(goal: Goal): Boolean {
|
||||
require(goal.functor == functor) { "Goal functor does not match predicate functor" }
|
||||
return clauses.any { it.evaluate(goal) }
|
||||
}
|
||||
}
|
5
src/prolog/components/expressions/Rule.kt
Normal file
5
src/prolog/components/expressions/Rule.kt
Normal file
|
@ -0,0 +1,5 @@
|
|||
package prolog.components.expressions
|
||||
|
||||
import prolog.components.terms.Head
|
||||
|
||||
class Rule(head: Head, body: Body): Clause(head, body)
|
12
src/prolog/components/terms/Atom.kt
Normal file
12
src/prolog/components/terms/Atom.kt
Normal file
|
@ -0,0 +1,12 @@
|
|||
package prolog.components.terms
|
||||
|
||||
import prolog.components.Functor
|
||||
import prolog.components.Goal
|
||||
|
||||
open class Atom(val name: String): Head(), Term, Goal {
|
||||
override val functor: Functor = Functor(this, 0)
|
||||
|
||||
override fun toString(): String {
|
||||
return name
|
||||
}
|
||||
}
|
13
src/prolog/components/terms/Head.kt
Normal file
13
src/prolog/components/terms/Head.kt
Normal file
|
@ -0,0 +1,13 @@
|
|||
package prolog.components.terms
|
||||
|
||||
import prolog.components.Functor
|
||||
|
||||
/**
|
||||
* Part of a [Clause][prolog.components.expressions.Clause] before the [neck][prolog.terms.Neck] operator.
|
||||
*
|
||||
* @see [Atom]
|
||||
* @see [CompoundTerm]
|
||||
*/
|
||||
abstract class Head: Term {
|
||||
abstract val functor: Functor
|
||||
}
|
17
src/prolog/components/terms/Structure.kt
Normal file
17
src/prolog/components/terms/Structure.kt
Normal file
|
@ -0,0 +1,17 @@
|
|||
package prolog.components.terms
|
||||
|
||||
import prolog.components.Functor
|
||||
import prolog.components.Goal
|
||||
|
||||
open class Structure(val name: Atom, val arguments: List<Argument>): Head(), Term, Goal {
|
||||
override val functor: Functor = Functor(name, arguments.size)
|
||||
|
||||
override fun toString(): String {
|
||||
return when {
|
||||
arguments.isEmpty() -> name.name
|
||||
else -> "${name.name}(${arguments.joinToString(", ")})"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typealias CompoundTerm = Structure
|
32
src/prolog/components/terms/Term.kt
Normal file
32
src/prolog/components/terms/Term.kt
Normal file
|
@ -0,0 +1,32 @@
|
|||
package prolog.components.terms
|
||||
|
||||
/**
|
||||
* Value in Prolog.
|
||||
*
|
||||
* A [Term] is either a [Variable], [Atom], integer, float or [CompoundTerm].
|
||||
* In addition, SWI-Prolog also defines the type string.
|
||||
*/
|
||||
interface Term
|
||||
|
||||
typealias Argument = Term
|
||||
|
||||
/*
|
||||
<program> ::= <clause list> <query> | <query>
|
||||
<clause list> ::= <clause> | <clause list> <clause>
|
||||
<clause> ::= <predicate> . | <predicate> :- <predicate list>.
|
||||
<predicate list> ::= <predicate> | <predicate list> , <predicate>
|
||||
<predicate> ::= <atom> | <atom> ( <term list> )
|
||||
<term list> ::= <term> | <term list> , <term>
|
||||
<term> ::= <numeral> | <atom> | <variable> | <structure>
|
||||
<structure> ::= <atom> ( <term list> )
|
||||
<query> ::= ?- <predicate list>.
|
||||
<small atom> ::= <lowercase letter> | <small atom> <character>
|
||||
<variable> ::= <uppercase letter> | <variable> <character>
|
||||
<lowercase letter> ::= a | b | c | ... | x | y | z
|
||||
<uppercase letter> ::= A | B | C | ... | X | Y | Z | _
|
||||
<numeral> ::= <digit> | <numeral> <digit>
|
||||
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
||||
<character> ::= <lowercase letter> | <uppercase letter> | <digit> | <special>
|
||||
<special> ::= + | - | * | / | \ | ^ | ~ | : | . | ? | | # | $ | &
|
||||
<string> ::= <character> | <string> <character>
|
||||
*/
|
30
src/prolog/components/terms/Variable.kt
Normal file
30
src/prolog/components/terms/Variable.kt
Normal file
|
@ -0,0 +1,30 @@
|
|||
package prolog.components.terms
|
||||
|
||||
import java.util.Optional
|
||||
|
||||
data class Variable(val name: String): Term {
|
||||
private var alias: Optional<Term> = Optional.empty()
|
||||
|
||||
fun alias(): Optional<Term> {
|
||||
return alias
|
||||
}
|
||||
|
||||
fun bind(term: Term): Optional<Term> {
|
||||
if (alias.isEmpty) {
|
||||
alias = Optional.of(term)
|
||||
}
|
||||
|
||||
return alias
|
||||
}
|
||||
|
||||
fun unbind() {
|
||||
alias = Optional.empty()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return when {
|
||||
alias.isPresent -> "$name = ${alias.get()}"
|
||||
else -> name
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
package prolog.terms
|
||||
|
||||
data class Atom(val name: String): Term()
|
|
@ -1,3 +0,0 @@
|
|||
package prolog.terms
|
||||
|
||||
sealed class Term
|
9
src/prolog/terms/operators.kt
Normal file
9
src/prolog/terms/operators.kt
Normal file
|
@ -0,0 +1,9 @@
|
|||
package prolog.terms
|
||||
|
||||
import prolog.components.Operand
|
||||
import prolog.components.Operator
|
||||
import prolog.components.terms.Atom
|
||||
|
||||
class Neck(leftOperand: Operand, rightOperand: Operand) : Operator(Atom(":-"), leftOperand, rightOperand)
|
||||
|
||||
class Query(rightOperand: Operand): Operator(Atom("?-"), null, rightOperand)
|
|
@ -1,7 +1,83 @@
|
|||
package prolog
|
||||
|
||||
import prolog.terms.Term
|
||||
import prolog.builtins.atomic
|
||||
import prolog.builtins.compound
|
||||
import prolog.builtins.variable
|
||||
import prolog.components.Goal
|
||||
import prolog.components.terms.Term
|
||||
import prolog.components.terms.Variable
|
||||
import prolog.components.terms.Structure
|
||||
import java.util.*
|
||||
|
||||
fun unify(term1: Term, term2: Term): Result {
|
||||
return Result(term1 == term2, emptyList())
|
||||
typealias Substitution = Map<Variable, Term>
|
||||
|
||||
// Apply substitutions to a term
|
||||
fun applySubstitution(term: Term, substitution: Substitution): Term = when {
|
||||
variable(term) -> (term as Variable).alias().map { applySubstitution(it, substitution) }.orElse(term)
|
||||
atomic(term) -> term
|
||||
compound(term) -> {
|
||||
val structure = term as Structure
|
||||
Structure(structure.name, structure.arguments.map { applySubstitution(it, substitution) })
|
||||
}
|
||||
else -> term
|
||||
}
|
||||
|
||||
// Check if a variable occurs in a term
|
||||
fun occurs(variable: Variable, term: Term): Boolean = when {
|
||||
variable(term) -> term == variable
|
||||
atomic(term) -> false
|
||||
compound(term) -> {
|
||||
val structure = term as Structure
|
||||
structure.arguments.any { occurs(variable, it) }
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
|
||||
// Generate possible substitutions
|
||||
fun generateSubstitutions(term1: Term, term2: Term, substitution: Substitution): Sequence<Substitution> = sequence {
|
||||
val t1 = applySubstitution(term1, substitution)
|
||||
val t2 = applySubstitution(term2, substitution)
|
||||
|
||||
when {
|
||||
t1 == t2 -> yield(substitution)
|
||||
variable(t1) -> {
|
||||
val variable = t1 as Variable
|
||||
if (!occurs(variable, t2)) {
|
||||
variable.bind(t2)
|
||||
yield(substitution + (variable to t2))
|
||||
}
|
||||
}
|
||||
variable(t2) -> {
|
||||
val variable = t2 as Variable
|
||||
if (!occurs(variable, t1)) {
|
||||
variable.bind(t1)
|
||||
yield(substitution + (variable to t1))
|
||||
}
|
||||
}
|
||||
compound(t1) && compound(t2) -> {
|
||||
val structure1 = t1 as Structure
|
||||
val structure2 = t2 as Structure
|
||||
if (structure1.functor == structure2.functor) {
|
||||
val newSubstitution = structure1.arguments.zip(structure2.arguments).fold(substitution) { acc, (arg1, arg2) ->
|
||||
generateSubstitutions(arg1, arg2, acc).firstOrNull() ?: return@sequence
|
||||
}
|
||||
yield(newSubstitution)
|
||||
}
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
// Unify two terms with backtracking and lazy evaluation
|
||||
fun unifyLazy(term1: Term, term2: Term, substitution: Substitution = emptyMap()): Sequence<Substitution> {
|
||||
return generateSubstitutions(term1, term2, substitution)
|
||||
}
|
||||
|
||||
fun unify(term1: Term, term2: Term): Optional<Substitution> {
|
||||
val substitutions = unifyLazy(term1, term2).toList()
|
||||
return if (substitutions.isEmpty()) {
|
||||
Optional.empty()
|
||||
} else {
|
||||
Optional.of(substitutions.first())
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue