Skip to content

Commit 0bc416b

Browse files
committed
feat(declarativeDSL): programTheory is now an alias of dynamicTheory
1 parent 10829bb commit 0bc416b

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/main/scala/io/github/kelvindev15/prolog/core/Struct.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,6 @@ object Struct:
162162

163163
private case class FactImpl(_head: Struct) extends Fact:
164164
override val head: Option[Struct] = Option(_head)
165-
override val arguments: Seq[Term] = Seq(head.get)
165+
override val arguments: Seq[Term] = Seq(head.get, body)
166166
override def isGround: Boolean = head.get.isGround
167167
override def variables: Seq[Variable] = head.get.variables

src/main/scala/io/github/kelvindev15/prolog/dsl/DeclarativeProlog.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ trait DeclarativeProlog:
2525
prologProgram
2626

2727
/** Sets the static theory of a [[PrologProgram]].
28+
* Warning: it overrides any previously static theory set in the program.
2829
*
2930
* @param program
3031
* the program whose static theory has to be set.
@@ -40,6 +41,7 @@ trait DeclarativeProlog:
4041
prologProgram = program.setStaticTheory(t.theory)
4142

4243
/** Sets the dynamic theory of a [[PrologProgram]].
44+
* Warning: it overrides any previously dynamic theory set in the program.
4345
*
4446
* @param program
4547
* the program whose dynamic theory has to be set.
@@ -54,6 +56,22 @@ trait DeclarativeProlog:
5456
theory
5557
prologProgram = prologProgram.setDynamicTheory(t.theory)
5658

59+
/** Alias of [[dynamicTheory]]. Sets the dynamic theory of a [[PrologProgram]].
60+
* Warning: it overrides any previously dynamic theory set in the program.
61+
*
62+
* @param program
63+
* the program whose dynamic theory has to be set.
64+
* @param theory
65+
* a function that takes a [[MutableDynamicTheoryWrapper]] as a context
66+
* parameter and operates on it e.g to add or remove clauses.
67+
*/
68+
def programTheory(using program: PrologProgram)(
69+
theory: MutableDynamicTheoryWrapper ?=> Unit
70+
): Unit =
71+
given t: MutableDynamicTheoryWrapper = MutableDynamicTheoryWrapper()
72+
theory
73+
prologProgram = prologProgram.setDynamicTheory(t.theory)
74+
5775
/** Add an assert(X) clause to the [[dynamicTheory]] provided as a context
5876
* parameter.
5977
*

src/test/scala/io/github/kelvindev15/prolog/dsl/TestDeclarativePrologDSL.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,9 @@ class TestDeclarativePrologDSL
104104
Struct(Atom("="), Cons(Variable("H"), Variable("T")), Variable("S"))
105105
)
106106
)
107+
108+
test("Using programTheory as dynamic theory alias"):
109+
prolog {
110+
programTheory:
111+
fact { "likes"(X) }
112+
}.dynamicTheory shouldBe Seq(Fact(Struct(Atom("likes"), Variable("X"))))

0 commit comments

Comments
 (0)