From cdf2513e968f8fd4df071e63a3286537da7c1765 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Mon, 5 May 2025 22:14:10 +0200 Subject: [PATCH] NotEquivalent --- src/interpreter/Preprocessor.kt | 1 + src/parser/grammars/TermsGrammar.kt | 2 +- src/parser/grammars/Tokens.kt | 1 + src/prolog/builtins/unificationOperators.kt | 9 +++++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/interpreter/Preprocessor.kt b/src/interpreter/Preprocessor.kt index 242a6ac..523598a 100644 --- a/src/interpreter/Preprocessor.kt +++ b/src/interpreter/Preprocessor.kt @@ -75,6 +75,7 @@ open class Preprocessor { term.functor == ",/2" -> Conjunction(args[0] as LogicOperand, args[1] as LogicOperand) term.functor == ";/2" -> Disjunction(args[0] as LogicOperand, args[1] as LogicOperand) term.functor == "\\+/1" -> Not(args[0] as Goal) + term.functor == "\\==/2" -> NotEquivalent(args[0], args[1]) term.functor == "==/2" -> Equivalent(args[0], args[1]) term.functor == "=\\=/2" && args.all { it is Expression } -> EvaluatesToDifferent(args[0] as Expression, args[1] as Expression) diff --git a/src/parser/grammars/TermsGrammar.kt b/src/parser/grammars/TermsGrammar.kt index 3fb9e48..0473200 100644 --- a/src/parser/grammars/TermsGrammar.kt +++ b/src/parser/grammars/TermsGrammar.kt @@ -87,7 +87,7 @@ open class TermsGrammar : Tokens() { t2.fold(t1) { acc, (op, term) -> CompoundTerm(Atom(op), listOf(acc, term)) } } - protected val op700: Parser by (equivalent or equals or notEquals or isOp) use { text } + protected val op700: Parser by (equivalent or notEquivalent or equals or notEquals or isOp) use { text } protected val term700: Parser by (term500 * optional(op700 * term500)) use { if (t2 == null) t1 else CompoundTerm(Atom(t2!!.t1), listOf(t1, t2!!.t2)) } diff --git a/src/parser/grammars/Tokens.kt b/src/parser/grammars/Tokens.kt index ac8c36f..018417f 100644 --- a/src/parser/grammars/Tokens.kt +++ b/src/parser/grammars/Tokens.kt @@ -20,6 +20,7 @@ abstract class Tokens : Grammar() { // 1000 protected val comma: Token by literalToken(",") // 700 + protected val notEquivalent: Token by literalToken("\\==") protected val equivalent: Token by literalToken("==") protected val equals: Token by literalToken("=") protected val isOp: Token by literalToken("is") diff --git a/src/prolog/builtins/unificationOperators.kt b/src/prolog/builtins/unificationOperators.kt index 6019b18..ad55acf 100644 --- a/src/prolog/builtins/unificationOperators.kt +++ b/src/prolog/builtins/unificationOperators.kt @@ -55,3 +55,12 @@ class Equivalent(private val term1: Term, private val term2: Term) : Operator(At applySubstitution(term2, subs) ) } + +class NotEquivalent(private val term1: Term, private val term2: Term) : Operator(Atom("\\=="), term1, term2) { + private val not = Not(Equivalent(term1, term2)) + override fun satisfy(subs: Substitutions): Answers = not.satisfy(subs) + override fun applySubstitution(subs: Substitutions): NotEquivalent = NotEquivalent( + applySubstitution(term1, subs), + applySubstitution(term2, subs) + ) +}