Skip to content

Commit 7939e2c

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

File tree

18 files changed

+264
-43
lines changed

18 files changed

+264
-43
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,62 @@ package io.github.kelvindev15.prolog.core
33
import io.github.kelvindev15.prolog.core.Struct.quoteIfFunctorIsMalformed
44
import io.github.kelvindev15.prolog.utils.TermVisitor
55

6+
/** A Prolog Constant, either numeric or an atom */
67
trait Constant extends Term:
8+
/** The value of the constant. */
79
val value: Any
10+
811
override def variables: Seq[Variable] = Seq()
12+
913
override def isGround: Boolean = true
14+
1015
override def accept[T](visitor: TermVisitor[T]): T = visitor.visit(this)
16+
1117
override def asTerm: Term = this
1218

1319
object Constant:
20+
/** Returns a Prolog [[Constant]].
21+
*
22+
* @param value the value of the constant.
23+
* @throws IllegalArgumentException if the value cannot be used for an [[Atom]] or a [[Numeric]].
24+
*/
1425
def apply(value: Any): Constant = value match
1526
case n: (Int | Double) => Numeric(n)
1627
case s: String => Atom(s)
1728
case _ => throw IllegalArgumentException("Cannot create a constant with the provided argument")
29+
30+
/** A Prolog atom. */
1831
trait Atom extends Constant with Struct:
1932
override val value: String
33+
34+
/** The unquoted value of the atom. */
2035
val unquotedValue: String
36+
2137
override val arity: Int = 0
38+
2239
override val arguments: Seq[Term] = Seq()
40+
2341
override val functor: Atom = this
2442

2543
object Atom:
2644
private def removeQuotes(value: String) = value.replaceAll("^'+|'+$", "")
45+
46+
/** Returns an instance of [[Atom]].
47+
*
48+
* @param value the value of the atom.
49+
*/
2750
def apply(value: String): Atom = AtomImpl(removeQuotes(value))
51+
2852
private case class AtomImpl(private val _value: String) extends Atom:
2953
override val value: String = quoteIfFunctorIsMalformed(_value)
3054
override val unquotedValue: String = _value
3155

56+
/** A Prolog numeric constant. */
3257
trait Numeric extends Constant:
3358
override val value: AnyVal
3459

3560
object Numeric:
61+
/** Returns an instance of [[Numeric]] */
3662
def apply(value: AnyVal): Numeric = NumericImpl(value)
63+
3764
private case class NumericImpl(value: AnyVal) extends Numeric

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,63 @@ import io.github.kelvindev15.prolog.core.RecursiveStruct.BinaryRecursiveStruct
66
import io.github.kelvindev15.prolog.utils.TermVisitor
77

88
object Goals:
9+
/** Represents a conjunction of goals. */
910
trait Conjunction extends BinaryRecursiveStruct:
1011
override val functor: Constant.Atom = Functors.GOAL_CONJUNCTION
1112
override def accept[T](visitor: TermVisitor[T]): T = visitor.visit(this)
1213

1314
object Conjunction:
15+
/** Returns a [[Conjunction]] if more to arguments are provided,
16+
* otherwise return the provided term.
17+
*
18+
* @param args the arguments to put in conjunction.
19+
* @throws IllegalArgumentException if no arguments is provided.
20+
*/
1421
def wrapIfNecessary(args: Term*): Term = BinaryRecursiveStruct.wrapIfNecessary(Conjunction.apply)(args*)
22+
23+
/** Returns an instance of [[Conjunction]].
24+
*
25+
* @param args the arguments to put in conjunction.
26+
* @throws IllegalArgumentException if less of two arguments are provided.
27+
*/
1528
def apply(args: Term*): Conjunction = BinaryRecursiveStruct.fold(ConjunctionImpl.apply)(args*)
29+
30+
/** Deconstructs the Conjuction in its arguments.
31+
*
32+
* @param conjunction the conjunction to deconstruct.
33+
*/
1634
def unapply(conjunction: Conjunction): Option[(Term, Term)] = Option((conjunction.first, conjunction.second))
35+
1736
private case class ConjunctionImpl(left: Term, right: Term) extends Conjunction
1837

38+
/** Represents a disjunction of goals. */
1939
trait Disjunction extends BinaryRecursiveStruct:
2040
override val functor: Constant.Atom = Functors.GOAL_DISJUNCTION
2141
override def accept[T](visitor: TermVisitor[T]): T = visitor.visit(this)
2242

2343
object Disjunction:
44+
/** Returns a [[Disjunction]] if more to arguments are provided,
45+
* otherwise return the provided term.
46+
*
47+
* @param args the arguments to put in disjuction.
48+
* @throws IllegalArgumentException if no arguments is provided.
49+
*/
2450
def wrapIfNecessary(args: Term*): Term = BinaryRecursiveStruct.wrapIfNecessary(Disjunction.apply)(args*)
51+
52+
/** Returns an instance of [[Conjunction]].
53+
*
54+
* @param args the arguments to put in disjunction.
55+
* @throws IllegalArgumentException if less of two arguments are provided.
56+
*/
57+
2558
def apply(args: Term*): Disjunction = BinaryRecursiveStruct.fold(DisjunctionImpl.apply)(args*)
59+
60+
/** Deconstructs the Disjunction in its arguments.
61+
*
62+
* @param disjunction the disjunction to deconstruct.
63+
*/
2664
def unapply(disjunction: Disjunction): Option[(Term, Term)] = Option((disjunction.first, disjunction.second))
65+
2766
private case class DisjunctionImpl(left: Term, right: Term) extends Disjunction
2867

2968

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import scala.util.matching.Regex
66

77
object Prolog:
88
object Syntax:
9+
/** Prolog's atom syntax regex */
910
val AtomRegex: Regex = "^[a-z][a-zA-Z_0-9]*$".r
11+
12+
/** Prolog's variable syntax regex */
1013
val VariableRegex: Regex = "[A-Z_][a-zA-Z_0-9]*".r
1114

1215
object Functors:

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,53 @@ package io.github.kelvindev15.prolog.core
33
import io.github.kelvindev15.prolog.core.Constant.Atom
44
import io.github.kelvindev15.prolog.core.Prolog.Functors
55

6+
/** A Prolog list */
67
trait PrologList extends RecursiveStruct:
8+
/** The number of elements in the list. */
79
val size: Int
810

911
object PrologList:
1012
extension (listTerminator: (PrologList | Variable))
13+
/** Returns all arguments of the list in a sequence. */
1114
def linearizedArguments: Seq[Term] = listTerminator match
1215
case l: PrologList => l.linearizedArguments
1316
case v: Variable => Seq(v)
1417

1518
given Conversion[(PrologList | Variable), Term] = _.asInstanceOf[Term]
19+
20+
/** Returns an instance of [[PrologList]].
21+
*
22+
* @param elements the elements to include in the list.
23+
*/
24+
def apply(elements: Term*): PrologList = elements.size match
25+
case 0 => Nil
26+
case 1 => Cons(elements.head, Nil)
27+
case _ => Cons(elements.head, apply(elements.tail *))
1628

29+
/** A Prolog list, comprises a head and a tail. */
1730
trait Cons extends PrologList:
31+
/** The head of the list */
1832
val head: Term
33+
34+
/** The tail of the list. */
1935
val tail: (PrologList | Variable)
2036

2137
override def linearizedArguments: Seq[Term] = Seq(head) ++ tail.linearizedArguments
38+
2239
override val arity: Int = 2
40+
2341
override val arguments: Seq[Term] = Seq(head, tail)
42+
2443
override val size: Int = linearizedArguments.size
25-
override val functor: Constant.Atom = Functors.CONS
2644

27-
def apply(elements: Term*): PrologList = elements.size match
28-
case 0 => Nil
29-
case 1 => Cons(elements.head, Nil)
30-
case _ => Cons(elements.head, apply(elements.tail *))
45+
override val functor: Constant.Atom = Functors.CONS
3146

3247
object Cons:
48+
/** Returns an instance of [[Cons]].
49+
*
50+
* @param head the head of the list.
51+
* @param tail the tail of the list.
52+
*/
3353
def apply(head: Term, tail: (PrologList | Variable)): PrologList = ConsImpl(head, tail)
3454

3555
object Nil extends PrologList:
@@ -38,7 +58,6 @@ object PrologList:
3858
override val arguments: Seq[Term] = Seq()
3959
override val size: Int = 0
4060
override def asTerm: Term = Functors.EMPTY_LIST
41-
4261
override val functor: Constant.Atom = Functors.EMPTY_LIST
4362

4463
private case class ConsImpl(head: Term, tail: (PrologList | Variable)) extends Cons:

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,61 @@ package io.github.kelvindev15.prolog.core
22

33
import io.github.kelvindev15.prolog.utils.BinaryToFlatVisitor
44

5+
/** A Prolog [[Struct]] that is also recursive. */
56
trait RecursiveStruct extends Struct:
7+
/** All arguments of the struct linearized in a list. */
68
def linearizedArguments: Seq[Term]
9+
710
override def foreach(action: Term => Unit): Unit = linearizedArguments.foreach(action)
811

912
object RecursiveStruct:
13+
/** A [[RecursiveStruct]] that is also binary. */
1014
trait BinaryRecursiveStruct extends RecursiveStruct:
1115
final override val arity: Int = 2
16+
/** The first argument of the struct. */
1217
def first: Term = left
18+
19+
/** The second argument of the struct. */
1320
def second: Term = right
21+
22+
/** Alias of [[first]]. */
1423
def left: Term
24+
25+
/** Alias of [[second]]. */
1526
def right: Term
27+
1628
final override val arguments: Seq[Term] = Seq(left, right)
29+
1730
override def linearizedArguments: Seq[Term] = accept(BinaryToFlatVisitor())
31+
1832
final override def asTerm: Term = Struct(functor, left.asTerm, right.asTerm)
1933

2034
object BinaryRecursiveStruct:
21-
def wrapIfNecessary(struct: Seq[Term] => BinaryRecursiveStruct)(args: Term*): Term = args.size match
35+
/** Binarize a list of [[Term]]s using a strategy.
36+
*
37+
* @param strategy the binarization strategy.
38+
* @param args the arguments to binarize.
39+
*
40+
* @throws IllegalArgumentException if the are no [[args]].
41+
*/
42+
def wrapIfNecessary(strategy: Seq[Term] => BinaryRecursiveStruct)(args: Term*): Term = args.size match
2243
case 0 => throw IllegalArgumentException("Cannot create a goal from an empty sequence")
2344
case 1 => args.head
24-
case _ => struct(args)
45+
case _ => strategy(args)
2546

47+
/** A destructuring object for [[BinaryRecursiveStruct]]s. */
2648
object Tuple:
2749
def unapply(tuple: BinaryRecursiveStruct): Option[(Term, Term)] =
2850
Option((tuple.left, tuple.right))
2951

30-
def fold[T <: BinaryRecursiveStruct](struct: (Term, Term) => T)(args: Term*): T = args.size match
52+
/** Folds a sequence of arguments into a binarized struct using a strategy.
53+
*
54+
* @param strategy the strategy for binarization.
55+
* @param args the sequence of terms.
56+
* @tparam T the type of [[BinaryRecursiveStruct]].
57+
*/
58+
def fold[T <: BinaryRecursiveStruct](strategy: (Term, Term) => T)(args: Term*): T = args.size match
3159
case n if n < 2 => throw IllegalArgumentException("There must be at least two arguments")
32-
case 2 => struct(args.head, args.tail.head)
33-
case _ => struct(args.head, fold(struct)(args.tail *))
60+
case 2 => strategy(args.head, args.tail.head)
61+
case _ => strategy(args.head, fold(strategy)(args.tail *))
3462

0 commit comments

Comments
 (0)