File tree Expand file tree Collapse file tree 3 files changed +29
-3
lines changed
main/scala/io/github/kelvindev15/prolog/engine
test/scala/io/github/kelvindev15/engine Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ trait Solver:
14
14
15
15
object Solver :
16
16
type Substitution = Map [Variable , Term ]
17
+ object Substitution :
18
+ def apply (substitutions : (Variable , Term )* ): Substitution = Map (substitutions* )
17
19
enum Solution :
18
20
case Yes (query : Struct , substitution : Substitution )
19
21
case No (query : Struct )
@@ -23,3 +25,6 @@ object Solver:
23
25
24
26
def solve (using solver : Solver = tuPrologSolver())(prologProgram : PrologProgram ): Iterator [Solution ] =
25
27
solver solve prologProgram
28
+
29
+ def lazySolve (using solver : Solver = tuPrologSolver())(prologProgram : PrologProgram ): LazyList [Solution ] =
30
+ solver lazySolve prologProgram
Original file line number Diff line number Diff line change @@ -42,4 +42,16 @@ class TestPrologEngine extends AnyFlatSpec with Matchers with TestUtils with Pro
42
42
43
43
" The goal 'X = 2, Y = 3, 5 is X + Y'" should " give a Yes solution" in :
44
44
expectOne[Solution .Yes ]:
45
- Solver solve (PrologProgram .emptyTheory withGoal && (X `=` 2 , Y `=` 3 , 5 is X + Y ))
45
+ Solver solve (PrologProgram .emptyTheory withGoal && (X `=` 2 , Y `=` 3 , 5 is X + Y ))
46
+
47
+ " All solution" should " be returned by the engine" in :
48
+ val fruit = " fruit"
49
+ val fruits = Seq (" banana" , " orange" , " apple" )
50
+ val query = fruit(X )
51
+ Solver lazySolve {
52
+ prolog :
53
+ staticTheory :
54
+ for f <- fruits do clause(fruit(f))
55
+ goal :
56
+ query
57
+ } expectSolutionsIn fruits.map(e => query.yes(X -> e))
Original file line number Diff line number Diff line change 1
1
package io .github .kelvindev15 .engine .utils
2
2
3
- import io .github .kelvindev15 .prolog .engine .Solver .Solution
3
+ import io .github .kelvindev15 .prolog .core .{Struct , Term , Variable }
4
+ import io .github .kelvindev15 .prolog .engine .Solver .Solution .Yes
5
+ import io .github .kelvindev15 .prolog .engine .Solver .{Solution , Substitution }
4
6
import org .scalatest .matchers .should .Matchers
5
7
6
8
import scala .reflect .ClassTag
@@ -14,4 +16,11 @@ trait TestUtils:
14
16
def expectOne [T <: Solution ](using tag : ClassTag [T ])(solution : Iterator [Solution ]): Unit =
15
17
expect[T ](solution)
16
18
solution.hasNext shouldBe false
17
-
19
+
20
+ extension(solutions : Iterable [Solution ])
21
+ def expectSolutionsIn (expectedSolutions : Iterable [Solution ]): Unit =
22
+ solutions should contain allElementsOf expectedSolutions.to(LazyList )
23
+
24
+ extension(query : Struct )
25
+ def yes (substitutions : (Variable , Term )* ): Yes = Yes (query, Substitution (substitutions* ))
26
+ def no : Solution .No = Solution .No (query)
You can’t perform that action at this time.
0 commit comments