Skip to content

Commit 09d4b94

Browse files
committed
docs: add some scaladoc
1 parent ea97e27 commit 09d4b94

14 files changed

+472
-29
lines changed

src/main/scala/io/github/kelvindev15/prolog/PrologProgram.scala

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,53 @@ package io.github.kelvindev15.prolog
33
import io.github.kelvindev15.prolog.core.Term
44
import io.github.kelvindev15.prolog.core.theory.Theory
55

6+
/** Represents an instance of a prolog program. */
67
trait PrologProgram:
7-
def staticTheory: Theory
8-
def dynamicTheory: Theory
9-
def goal: Option[Term]
8+
9+
/** The static [[Theory]] of the program. */
10+
val staticTheory: Theory
11+
12+
/** The dynamic [[Theory]] of the program. */
13+
val dynamicTheory: Theory
14+
15+
/** An [[Option]] that if filled with the goal set for the program. */
16+
val goal: Option[Term]
17+
18+
/** Returns this program with the new provided goal.
19+
*
20+
* @param goal a [[Term]] representing the goal to be set for this program.
21+
*/
1022
def withGoal(goal: Term): PrologProgram
23+
24+
/** Returns this program with the new provided static theory.
25+
*
26+
* @param theory the static [[Theory]] to be set for this program.
27+
* @return
28+
*/
1129
def setStaticTheory(theory: Theory): PrologProgram
30+
31+
/** Returns this program with the new provided dynamic theory
32+
*
33+
* @param theory the dynamic [[Theory]] to be set for this program.
34+
* @return
35+
*/
1236
def setDynamicTheory(theory: Theory): PrologProgram
1337

1438
object PrologProgram:
39+
40+
/** Returns an instance of a [[PrologProgram]].
41+
*
42+
* @param staticTheory the static [[Theory]] of the prolog program
43+
* @param dynamicTheory the dynamic [[Theory]] of the prolog program
44+
*/
1545
def apply(staticTheory: Theory = Theory(), dynamicTheory: Theory = Theory()): PrologProgram =
1646
PrologProgramImpl(staticTheory, dynamicTheory, None)
17-
47+
48+
/**
49+
*
50+
* @return an instance of a [[PrologProgram]] whose static and dynamic [[Theory]] are empty and
51+
* with no goal.
52+
*/
1853
def emptyTheory: PrologProgram = PrologProgramImpl(Theory.empty, Theory.empty, None)
1954

2055
private case class PrologProgramImpl(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import io.github.kelvindev15.prolog.core.Constant.Atom
44
import io.github.kelvindev15.prolog.core.Struct.Fact
55
import io.github.kelvindev15.prolog.core.{Constant, PrologList, Struct, Term}
66

7-
trait DSLConversions:
7+
protected trait DSLConversions:
88
dsl: PrologDSL =>
99
given Conversion[String, Atom] = Atom(_)
1010
given Conversion[AnyVal, Constant] = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import io.github.kelvindev15.prolog.core.{Goals, Struct, Term}
66

77
import scala.annotation.targetName
88

9-
trait DSLExtensions:
9+
protected trait DSLExtensions:
1010
dsl: PrologDSL =>
1111
extension (atom: Atom)
1212
def apply(terms: Term*): Struct = Struct(atom, terms *)

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

Lines changed: 129 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,187 @@ import io.github.kelvindev15.prolog.core.{Constant, Prolog, Struct, Term}
77

88
import scala.annotation.targetName
99

10-
enum AssociativitySpec extends Atom:
10+
protected enum AssociativitySpec extends Atom:
1111
case fx, fy, xf, xfx, xfy, yfx, yfy
1212
override val value: String = toString
1313
override val unquotedValue: String = value
1414

15-
trait DSLPrologBuiltins:
15+
protected trait DSLPrologBuiltins:
1616
dsl: PrologDSL =>
1717
export AssociativitySpec.*
1818

19+
/** Returns a [[Directive]]
20+
*
21+
* @param terms the arguments of the directive.
22+
*/
1923
@targetName("iff")
2024
def :-(terms: Term*): Directive = Directive(terms *)
2125

26+
/** Builds a var(X) [[Struct]].
27+
*
28+
* @param term the argument of the structure
29+
*/
2230
def `var`(term: Term): Struct = Struct(Atom("var"), term)
31+
32+
/** Builds a nonvar(X) [[Struct]].
33+
*
34+
* @param term the argument of the structure
35+
*/
2336
def nonvar(term: Term): Struct = Struct(Atom("nonvar"), term)
37+
38+
/** Builds a atom(X) [[Struct]].
39+
*
40+
* @param term the argument of the structure
41+
*/
2442
def atom(term: Term): Struct = Struct(Atom("atom"), term)
43+
44+
/** Builds a number(X) [[Struct]].
45+
*
46+
* @param term the argument of the structure
47+
*/
2548
def number(term: Term): Struct = Struct(Atom("number"), term)
49+
50+
/** Builds an atomic(X) [[Struct]].
51+
*
52+
* @param term the argument of the structure
53+
*/
2654
def atomic(term: Term): Struct = Struct(Atom("atomic"), term)
55+
56+
/** Builds a ground(X) [[Struct]].
57+
*
58+
* @param term the argument of the structure
59+
*/
2760
def ground(term: Term): Struct = Struct(Atom("ground"), term)
2861

62+
/** Returns a member(t, l) [[Struct]].
63+
*
64+
* @param term a term
65+
* @param l a term that should unify to a list.
66+
*/
2967
def member(term: Term, l: Term): Struct = Struct(Atom("member"), term, l)
68+
69+
/** Returns an append(l1, l2) [[Struct]].
70+
*
71+
* @param l1 a term that should unify to a list
72+
* @param l2 a term that should unify to a list.
73+
*/
3074
def append(l1: Term, l2: Term): Struct = Struct(Atom("append"), l1, l2)
3175

76+
/** Returns a dynamic(t1, t2, ...) [[Struct]].
77+
*
78+
* @param indicators the indicators of the predicates.
79+
*/
3280
def dynamic(indicators: Indicator*): Struct =
3381
Directive(Struct(Atom("dynamic"), Conjunction.wrapIfNecessary(indicators*)))
3482

3583
def clause(head: Term, body: Term): Struct = Struct(Atom("clause"), head, body)
84+
85+
/** Returns an asserta(X) [[Struct]].
86+
*
87+
* @param clause the clause to assert.
88+
*/
3689
def asserta(clause: Term): Struct = Struct(Atom("asserta"), clause)
90+
91+
/** Returns an assertz(X) [[Struct]].
92+
*
93+
* @param clause the clause to assert.
94+
*/
3795
def assertz(clause: Term): Struct = Struct(Atom("assertz"), clause)
96+
97+
/** Returns an retract(X) [[Struct]].
98+
*
99+
* @param clause the clause to retract.
100+
*/
101+
38102
def retract(clause: Term): Struct = Struct(Atom("retract"), clause)
39103

104+
/** Returns a functor(T, F, N) [[Struct]].
105+
*
106+
* @param term a term.
107+
* @param functor a term that should unify with the of the [[term]] functor.
108+
* @param arity a term that should unify with the arity of the [[term]].
109+
*/
40110
def functor(term: Term, functor: Term, arity: Term): Struct =
41111
Struct(Atom("functor"), term, functor, arity)
42112

113+
/** Returns an arg(N, T, L) [[Struct]].
114+
*
115+
* @param number a term that should unify with the position of the [[arg]] in [[term]].
116+
* @param term a term
117+
* @param arg a term that should unify the with [[number]]-th argument in [[term]].
118+
*/
43119
def arg(number: Term, term: Struct, arg: Term): Struct = Struct(Atom("arg"), number, term, arg)
44120

121+
/** Returns an atom_chars(A, L) [[Struct]].
122+
*
123+
* @param atom a term that should unify to an atom.
124+
* @param list a term that should unify to a list containing the characters of [[atom]].
125+
*/
45126
def atom_chars(atom: Term, list: Term): Struct = Struct(Atom("atom_chars"), atom, list)
127+
128+
/** Returns an number_chars(A, L) [[Struct]].
129+
*
130+
* @param atom a term that should unify to an numeric constant
131+
* @param list a term that should unify with a list containing the characters composing the constant.
132+
*/
46133
def number_chars(atom: Term, list: Term): Struct = Struct(Atom("number_chars"), atom, list)
47134

135+
/** The cut predicate */
48136
@targetName("cut")
49137
val `!`: Atom = Atom("!")
50138

139+
/** The repeat predicate */
51140
val repeat: Atom = Atom("repeat")
52141

142+
/** Builds a goal(G) [[Struct]].
143+
*
144+
* @param goal the argument of the structure
145+
*/
53146
def call(goal: Term): Struct = Struct(Atom("call"), goal)
147+
148+
/** Builds a once(G) [[Struct]].
149+
*
150+
* @param goal the argument of the structure
151+
*/
54152
def once(goal: Term): Struct = Struct(Atom("once"), goal)
153+
154+
/** Builds a not(G) [[Struct]].
155+
*
156+
* @param goal the argument of the structure
157+
*/
55158
def not(goal: Term): Struct = Struct(Atom("not"), goal)
56159

160+
/** Returns a findall(T, G, L) [[Struct]].
161+
*
162+
* @param res
163+
* @param goal
164+
* @param list a term that should unify to a list containing ???
165+
*/
57166
def findall(res: Term, goal: Term, list: Term): Struct = Struct(Atom("findall"), res, goal, list)
58-
167+
168+
/** Builds a \+(X) [[Struct]].
169+
*
170+
* @param goal the argument of the structure
171+
*/
59172
@targetName("naf")
60173
def !(goal: Term): Struct = Struct(Atom("\\+"), goal)
61174

175+
/** Returns an op(I, A, N) [[Struct]].
176+
*
177+
* @param precedence
178+
* @param associativity
179+
* @param name
180+
*/
62181
def op(precedence: Constant.Numeric, associativity: AssociativitySpec, name: Atom): Struct =
63182
Struct(Atom("op"), precedence, associativity, name)
64183

184+
/** Returns a length(T, L) [[Struct]].
185+
*
186+
* @param term
187+
* @param len
188+
*/
65189
def length(term: Term, len: Term): Struct = Struct(Atom("length"), term, len)
190+
191+
/** An empty list functor */
66192
val `[]`: Atom = Prolog.Functors.EMPTY_LIST
67193

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,102 @@ import io.github.kelvindev15.prolog.core.{Struct, Term}
77
import io.github.kelvindev15.prolog.dsl.DeclarativeDSL.{MutableDynamicTheoryWrapper, MutableTheoryWrapper}
88

99

10-
trait DeclarativeDSL:
10+
protected trait DeclarativeDSL:
1111
dsl: PrologDSL =>
1212
private var prologProgram: PrologProgram = PrologProgram()
1313

14+
/** Returns a new [[PrologProgram]].
15+
*
16+
* @param program a function that takes a [[PrologProgram]] as a context parameter and operates on it.
17+
*/
1418
def prolog(program: PrologProgram ?=> Unit): PrologProgram =
1519
given p: PrologProgram = prologProgram
1620
program
1721
prologProgram
1822

23+
/** Sets the static theory of a [[PrologProgram]].
24+
*
25+
* @param program the program whose static theory has to be set.
26+
* @param theory a function that takes a [[MutableTheoryWrapper]] as a context parameter and operates on it
27+
* e.g to add or remove clauses.
28+
*/
1929
def staticTheory(using program: PrologProgram)(theory: MutableTheoryWrapper ?=> Unit): Unit =
2030
given t: MutableTheoryWrapper = MutableDynamicTheoryWrapper()
2131
theory
2232
prologProgram = program.setStaticTheory(t.theory)
2333

34+
/** Sets the dynamic theory of a [[PrologProgram]].
35+
*
36+
* @param program the program whose dynamic theory has to be set.
37+
* @param theory a function that takes a [[MutableDynamicTheoryWrapper]] as a context parameter and operates on it
38+
* e.g to add or remove clauses.
39+
*/
2440
def dynamicTheory(using program: PrologProgram)(theory: MutableDynamicTheoryWrapper ?=> Unit): Unit =
2541
given t: MutableDynamicTheoryWrapper = MutableDynamicTheoryWrapper()
2642
theory
2743
prologProgram = prologProgram.setDynamicTheory(t.theory)
2844

45+
/** Add an assert(X) clause to the [[dynamicTheory]] provided as a context parameter.
46+
*
47+
* @param dynamicTheory the theory to which the clause will be added.
48+
* @param clause the clause to assert in the theory.
49+
*/
2950
def assert(using dynamicTheory: MutableDynamicTheoryWrapper)(clause: Clause): Unit =
3051
dynamicTheory add "assert"(clause)
3152

53+
/** Add an asserta(X) clause to the [[dynamicTheory]] provided as a context parameter.
54+
*
55+
* @param dynamicTheory the theory to which the clause will be added.
56+
* @param clause the clause to assert in the theory.
57+
*/
3258
def assertA(using dynamicTheory: MutableDynamicTheoryWrapper)(clause: Clause): Unit =
3359
dynamicTheory add "asserta"(clause)
3460

61+
/** Add an assertz(X) clause to the [[dynamicTheory]] provided as a context parameter.
62+
*
63+
* @param dynamicTheory the theory to which the clause will be added.
64+
* @param clause the clause to assert in the theory.
65+
*/
3566
def assertZ(using dynamicTheory: MutableDynamicTheoryWrapper)(clause: Clause): Unit =
3667
dynamicTheory add "assertz"(clause)
3768

69+
/** Add a rule to a [[MutableTheoryWrapper]] provided as a context parameter.
70+
*
71+
* @param theory the theory to which the rule will be added.
72+
* @param rule the rule to add to the [[theory]].
73+
*/
3874
def rule(using theory: MutableTheoryWrapper)(rule: Rule): Unit = theory add rule
75+
76+
/** Add a fact to a [[MutableTheoryWrapper]] provided as a context parameter.
77+
*
78+
* @param theory the theory to which the fact will be added.
79+
* @param fact the fact to add to the [[theory]].
80+
*/
3981
def fact(using theory: MutableTheoryWrapper)(fact: Fact): Unit = theory add fact
82+
83+
/** Add a directive to a [[MutableTheoryWrapper]] provided as a context parameter.
84+
*
85+
* @param theory the theory to which the directive will be added.
86+
* @param directive the directive to add to the [[theory]].
87+
*/
4088
def directive(using theory: MutableTheoryWrapper)(directive: Directive): Unit = theory add directive
89+
90+
/** Add a clause (either a rule, a fact or a directive) to a [[MutableTheoryWrapper]] provided as a context parameter.
91+
*
92+
* @param theory the theory to which the clause will be added.
93+
* @param c the clause to add to the [[theory]].
94+
*/
4195
def clause(using theory: MutableTheoryWrapper)(c: Clause): Unit = theory add c
4296

97+
/** Sets the goal of a [[PrologProgram]] provided as a context parameter.
98+
*
99+
* @param program the program whose goal will be set.
100+
* @param g the goal to set in the [[program]].
101+
*/
43102
def goal(using program: PrologProgram)(g: Term): Unit =
44103
prologProgram = prologProgram withGoal g
45104

46-
object DeclarativeDSL:
105+
private object DeclarativeDSL:
47106
trait MutableTheoryWrapper:
48107
var theory: Theory = Theory()
49108
def add(clause: Clause): Theory = { theory = theory add clause; theory }

0 commit comments

Comments
 (0)