eyeling 1.34.2 → 1.34.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +7 -10
  2. package/docs/eyelang-guide.md +8 -30
  3. package/docs/eyelang-language-reference.md +32 -6
  4. package/examples/eyelang/basic-monadic.pl +16 -2
  5. package/examples/eyelang/bayes-therapy.pl +4 -4
  6. package/examples/eyelang/output/basic-monadic.pl +1314 -1314
  7. package/examples/eyelang/output/path-discovery.pl +3 -3
  8. package/examples/eyelang/output/reusable-builtins.pl +5 -0
  9. package/examples/eyelang/output/term-tools.pl +6 -0
  10. package/examples/eyelang/path-discovery.pl +22 -7
  11. package/examples/eyelang/reusable-builtins.pl +32 -0
  12. package/examples/eyelang/term-tools.pl +23 -0
  13. package/lib/eyelang/builtins/arithmetic.js +19 -6
  14. package/lib/eyelang/builtins/control.js +12 -0
  15. package/lib/eyelang/builtins/lists.js +146 -7
  16. package/lib/eyelang/builtins/registry.js +2 -4
  17. package/lib/eyelang/builtins/strings.js +165 -1
  18. package/lib/eyelang/builtins/terms.js +66 -0
  19. package/package.json +1 -1
  20. package/test/eyelang/conformance/README.md +1 -1
  21. package/test/eyelang/conformance/cases/extension/036_reusable_numeric_builtins.pl +10 -0
  22. package/test/eyelang/conformance/cases/extension/037_reusable_list_builtins.pl +11 -0
  23. package/test/eyelang/conformance/cases/extension/038_reusable_string_builtins.pl +12 -0
  24. package/test/eyelang/conformance/cases/extension/039_reusable_term_control_builtins.pl +11 -0
  25. package/test/eyelang/conformance/expected/extension/036_reusable_numeric_builtins.out +8 -0
  26. package/test/eyelang/conformance/expected/extension/037_reusable_list_builtins.out +9 -0
  27. package/test/eyelang/conformance/expected/extension/038_reusable_string_builtins.out +10 -0
  28. package/test/eyelang/conformance/expected/extension/039_reusable_term_control_builtins.out +6 -0
  29. package/examples/eyelang/collatz-1000.pl +0 -14
  30. package/examples/eyelang/complex-matrix-stability.pl +0 -45
  31. package/examples/eyelang/dense-hamiltonian-cycle.pl +0 -92
  32. package/examples/eyelang/gcd-bezout-identity.pl +0 -48
  33. package/examples/eyelang/goldbach-1000.pl +0 -185
  34. package/examples/eyelang/hamiltonian-cycle.pl +0 -55
  35. package/examples/eyelang/kaprekar.pl +0 -32
  36. package/examples/eyelang/matrix.pl +0 -296
  37. package/examples/eyelang/n-queens.pl +0 -23
  38. package/examples/eyelang/output/collatz-1000.pl +0 -1000
  39. package/examples/eyelang/output/complex-matrix-stability.pl +0 -5
  40. package/examples/eyelang/output/dense-hamiltonian-cycle.pl +0 -4
  41. package/examples/eyelang/output/gcd-bezout-identity.pl +0 -36
  42. package/examples/eyelang/output/goldbach-1000.pl +0 -667
  43. package/examples/eyelang/output/hamiltonian-cycle.pl +0 -4
  44. package/examples/eyelang/output/kaprekar.pl +0 -8
  45. package/examples/eyelang/output/matrix.pl +0 -10
  46. package/examples/eyelang/output/n-queens.pl +0 -93
  47. package/examples/eyelang/output/quine-mccluskey.pl +0 -3
  48. package/examples/eyelang/output/sat-dpll.pl +0 -5
  49. package/examples/eyelang/output/traveling-salesman.pl +0 -1
  50. package/examples/eyelang/quine-mccluskey.pl +0 -143
  51. package/examples/eyelang/sat-dpll.pl +0 -80
  52. package/examples/eyelang/traveling-salesman.pl +0 -64
  53. package/lib/eyelang/builtins/matrix.js +0 -226
  54. package/lib/eyelang/builtins/number-theory.js +0 -114
  55. package/lib/eyelang/builtins/search.js +0 -519
  56. package/test/eyelang/conformance/cases/extension/036_extended_gcd.pl +0 -3
  57. package/test/eyelang/conformance/cases/extension/037_collatz_trajectory.pl +0 -3
  58. package/test/eyelang/conformance/cases/extension/038_kaprekar_steps.pl +0 -3
  59. package/test/eyelang/conformance/cases/extension/039_goldbach_pair.pl +0 -3
  60. package/test/eyelang/conformance/cases/extension/040_matrix_operations.pl +0 -5
  61. package/test/eyelang/conformance/cases/extension/042_n_queens_small.pl +0 -3
  62. package/test/eyelang/conformance/cases/extension/043_cnf_model.pl +0 -3
  63. package/test/eyelang/conformance/expected/extension/036_extended_gcd.out +0 -1
  64. package/test/eyelang/conformance/expected/extension/037_collatz_trajectory.out +0 -1
  65. package/test/eyelang/conformance/expected/extension/038_kaprekar_steps.out +0 -1
  66. package/test/eyelang/conformance/expected/extension/039_goldbach_pair.out +0 -2
  67. package/test/eyelang/conformance/expected/extension/040_matrix_operations.out +0 -3
  68. package/test/eyelang/conformance/expected/extension/042_n_queens_small.out +0 -2
  69. package/test/eyelang/conformance/expected/extension/043_cnf_model.out +0 -1
@@ -1,10 +0,0 @@
1
- result(det3, determinant([[2, -1, 0], [-1, 2, -1], [0, -1, 2]], 4.0)).
2
- result(inv3, matrix_inversion([[2, -1, 0], [-1, 2, -1], [0, -1, 2]], [[0.74999999999999978, 0.5, 0.25], [0.5, 1.0000000000000004, 0.50000000000000022], [0.25, 0.50000000000000022, 0.75000000000000011]])).
3
- result(inv4, matrix_inversion([[18, 22, 54, 42], [22, 70, 86, 62], [54, 86, 174, 134], [42, 62, 134, 106]], [[2.5156250000000067, 0.48437500000000011, -1.2968749999999987, 0.35937499999999561], [0.48437500000000011, 0.14062499999999961, -0.32812499999999822, 0.14062499999999797], [-1.2968749999999987, -0.32812499999999822, 1.0156249999999925, -0.57812499999999201], [0.35937499999999561, 0.14062499999999797, -0.57812499999999201, 0.51562499999999278]])).
4
- result(invtri3, matrix_inv_triang([[2, 0, 0], [-1, 2, 0], [0, -1, 2]], [[0.5, 0, 0], [0.25, 0.5, 0], [0.125, 0.25, 0.5]])).
5
- result(mul_small, matrix_multiply([[[1, 2], [3, 4], [5, 6]], [[1, 1, 1], [1, 1, 1]]], [[3, 3, 3], [7, 7, 7], [11, 11, 11]])).
6
- result(mul_identity_check, matrix_multiply([[[18, 22, 54, 42], [22, 70, 86, 62], [54, 86, 174, 134], [42, 62, 134, 106]], [[2.515624999999984, 0.4843749999999933, -1.296874999999973, 0.3593749999999767], [0.4843749999999933, 0.1406249999999978, -0.3281249999999918, 0.1406249999999936], [-1.296874999999973, -0.3281249999999918, 1.015624999999971, -0.5781249999999781], [0.3593749999999767, 0.1406249999999936, -0.5781249999999781, 0.5156249999999853]]], [[1.0000000000000497, 5.3290705182007514e-15, 1.7763568394002505e-14, 4.4408920985006262e-15], [5.6843418860808015e-14, 1.0000000000000089, 2.8421709430404007e-14, 1.0658141036401503e-14], [1.7053025658242404e-13, 1.7763568394002505e-14, 1.0000000000000426, 3.907985046680551e-14], [8.5265128291212022e-14, 3.5527136788005009e-15, 7.1054273576010019e-14, 0.99999999999999467]])).
7
- result(sum_small, matrix_sum([[[1, 2], [3, 4], [5, 6]], [[1, 2], [3, 4], [5, 6]]], [[2, 4], [6, 8], [10, 12]])).
8
- result(chol3, cholesky_decomposition([[25, 15, -5], [15, 18, 0], [-5, 0, 11]], [[5.0, 0, 0], [3.0, 3.0, 0], [-1.0, 1.0, 3.0]])).
9
- result(chol4, cholesky_decomposition([[18, 22, 54, 42], [22, 70, 86, 62], [54, 86, 174, 134], [42, 62, 134, 106]], [[4.2426406871192848, 0, 0, 0], [5.1854497287013492, 6.5659052011974026, 0, 0], [12.727922061357857, 3.0460384954008553, 1.6497422479090704, 0], [9.8994949366116654, 1.6245538642137891, 1.8497110052313821, 1.3926212476455924]])).
10
- checksConsistentWithTreallaReference(matrix, true).
@@ -1,93 +0,0 @@
1
- solution(nQueens8, [1, 5, 8, 6, 3, 7, 2, 4]).
2
- solution(nQueens8, [1, 6, 8, 3, 7, 4, 2, 5]).
3
- solution(nQueens8, [1, 7, 4, 6, 8, 2, 5, 3]).
4
- solution(nQueens8, [1, 7, 5, 8, 2, 4, 6, 3]).
5
- solution(nQueens8, [2, 4, 6, 8, 3, 1, 7, 5]).
6
- solution(nQueens8, [2, 5, 7, 1, 3, 8, 6, 4]).
7
- solution(nQueens8, [2, 5, 7, 4, 1, 8, 6, 3]).
8
- solution(nQueens8, [2, 6, 1, 7, 4, 8, 3, 5]).
9
- solution(nQueens8, [2, 6, 8, 3, 1, 4, 7, 5]).
10
- solution(nQueens8, [2, 7, 3, 6, 8, 5, 1, 4]).
11
- solution(nQueens8, [2, 7, 5, 8, 1, 4, 6, 3]).
12
- solution(nQueens8, [2, 8, 6, 1, 3, 5, 7, 4]).
13
- solution(nQueens8, [3, 1, 7, 5, 8, 2, 4, 6]).
14
- solution(nQueens8, [3, 5, 2, 8, 1, 7, 4, 6]).
15
- solution(nQueens8, [3, 5, 2, 8, 6, 4, 7, 1]).
16
- solution(nQueens8, [3, 5, 7, 1, 4, 2, 8, 6]).
17
- solution(nQueens8, [3, 5, 8, 4, 1, 7, 2, 6]).
18
- solution(nQueens8, [3, 6, 2, 5, 8, 1, 7, 4]).
19
- solution(nQueens8, [3, 6, 2, 7, 1, 4, 8, 5]).
20
- solution(nQueens8, [3, 6, 2, 7, 5, 1, 8, 4]).
21
- solution(nQueens8, [3, 6, 4, 1, 8, 5, 7, 2]).
22
- solution(nQueens8, [3, 6, 4, 2, 8, 5, 7, 1]).
23
- solution(nQueens8, [3, 6, 8, 1, 4, 7, 5, 2]).
24
- solution(nQueens8, [3, 6, 8, 1, 5, 7, 2, 4]).
25
- solution(nQueens8, [3, 6, 8, 2, 4, 1, 7, 5]).
26
- solution(nQueens8, [3, 7, 2, 8, 5, 1, 4, 6]).
27
- solution(nQueens8, [3, 7, 2, 8, 6, 4, 1, 5]).
28
- solution(nQueens8, [3, 8, 4, 7, 1, 6, 2, 5]).
29
- solution(nQueens8, [4, 1, 5, 8, 2, 7, 3, 6]).
30
- solution(nQueens8, [4, 1, 5, 8, 6, 3, 7, 2]).
31
- solution(nQueens8, [4, 2, 5, 8, 6, 1, 3, 7]).
32
- solution(nQueens8, [4, 2, 7, 3, 6, 8, 1, 5]).
33
- solution(nQueens8, [4, 2, 7, 3, 6, 8, 5, 1]).
34
- solution(nQueens8, [4, 2, 7, 5, 1, 8, 6, 3]).
35
- solution(nQueens8, [4, 2, 8, 5, 7, 1, 3, 6]).
36
- solution(nQueens8, [4, 2, 8, 6, 1, 3, 5, 7]).
37
- solution(nQueens8, [4, 6, 1, 5, 2, 8, 3, 7]).
38
- solution(nQueens8, [4, 6, 8, 2, 7, 1, 3, 5]).
39
- solution(nQueens8, [4, 6, 8, 3, 1, 7, 5, 2]).
40
- solution(nQueens8, [4, 7, 1, 8, 5, 2, 6, 3]).
41
- solution(nQueens8, [4, 7, 3, 8, 2, 5, 1, 6]).
42
- solution(nQueens8, [4, 7, 5, 2, 6, 1, 3, 8]).
43
- solution(nQueens8, [4, 7, 5, 3, 1, 6, 8, 2]).
44
- solution(nQueens8, [4, 8, 1, 3, 6, 2, 7, 5]).
45
- solution(nQueens8, [4, 8, 1, 5, 7, 2, 6, 3]).
46
- solution(nQueens8, [4, 8, 5, 3, 1, 7, 2, 6]).
47
- solution(nQueens8, [5, 1, 4, 6, 8, 2, 7, 3]).
48
- solution(nQueens8, [5, 1, 8, 4, 2, 7, 3, 6]).
49
- solution(nQueens8, [5, 1, 8, 6, 3, 7, 2, 4]).
50
- solution(nQueens8, [5, 2, 4, 6, 8, 3, 1, 7]).
51
- solution(nQueens8, [5, 2, 4, 7, 3, 8, 6, 1]).
52
- solution(nQueens8, [5, 2, 6, 1, 7, 4, 8, 3]).
53
- solution(nQueens8, [5, 2, 8, 1, 4, 7, 3, 6]).
54
- solution(nQueens8, [5, 3, 1, 6, 8, 2, 4, 7]).
55
- solution(nQueens8, [5, 3, 1, 7, 2, 8, 6, 4]).
56
- solution(nQueens8, [5, 3, 8, 4, 7, 1, 6, 2]).
57
- solution(nQueens8, [5, 7, 1, 3, 8, 6, 4, 2]).
58
- solution(nQueens8, [5, 7, 1, 4, 2, 8, 6, 3]).
59
- solution(nQueens8, [5, 7, 2, 4, 8, 1, 3, 6]).
60
- solution(nQueens8, [5, 7, 2, 6, 3, 1, 4, 8]).
61
- solution(nQueens8, [5, 7, 2, 6, 3, 1, 8, 4]).
62
- solution(nQueens8, [5, 7, 4, 1, 3, 8, 6, 2]).
63
- solution(nQueens8, [5, 8, 4, 1, 3, 6, 2, 7]).
64
- solution(nQueens8, [5, 8, 4, 1, 7, 2, 6, 3]).
65
- solution(nQueens8, [6, 1, 5, 2, 8, 3, 7, 4]).
66
- solution(nQueens8, [6, 2, 7, 1, 3, 5, 8, 4]).
67
- solution(nQueens8, [6, 2, 7, 1, 4, 8, 5, 3]).
68
- solution(nQueens8, [6, 3, 1, 7, 5, 8, 2, 4]).
69
- solution(nQueens8, [6, 3, 1, 8, 4, 2, 7, 5]).
70
- solution(nQueens8, [6, 3, 1, 8, 5, 2, 4, 7]).
71
- solution(nQueens8, [6, 3, 5, 7, 1, 4, 2, 8]).
72
- solution(nQueens8, [6, 3, 5, 8, 1, 4, 2, 7]).
73
- solution(nQueens8, [6, 3, 7, 2, 4, 8, 1, 5]).
74
- solution(nQueens8, [6, 3, 7, 2, 8, 5, 1, 4]).
75
- solution(nQueens8, [6, 3, 7, 4, 1, 8, 2, 5]).
76
- solution(nQueens8, [6, 4, 1, 5, 8, 2, 7, 3]).
77
- solution(nQueens8, [6, 4, 2, 8, 5, 7, 1, 3]).
78
- solution(nQueens8, [6, 4, 7, 1, 3, 5, 2, 8]).
79
- solution(nQueens8, [6, 4, 7, 1, 8, 2, 5, 3]).
80
- solution(nQueens8, [6, 8, 2, 4, 1, 7, 5, 3]).
81
- solution(nQueens8, [7, 1, 3, 8, 6, 4, 2, 5]).
82
- solution(nQueens8, [7, 2, 4, 1, 8, 5, 3, 6]).
83
- solution(nQueens8, [7, 2, 6, 3, 1, 4, 8, 5]).
84
- solution(nQueens8, [7, 3, 1, 6, 8, 5, 2, 4]).
85
- solution(nQueens8, [7, 3, 8, 2, 5, 1, 6, 4]).
86
- solution(nQueens8, [7, 4, 2, 5, 8, 1, 3, 6]).
87
- solution(nQueens8, [7, 4, 2, 8, 6, 1, 3, 5]).
88
- solution(nQueens8, [7, 5, 3, 1, 6, 8, 2, 4]).
89
- solution(nQueens8, [8, 2, 4, 1, 7, 5, 3, 6]).
90
- solution(nQueens8, [8, 2, 5, 3, 1, 7, 4, 6]).
91
- solution(nQueens8, [8, 3, 1, 6, 2, 5, 7, 4]).
92
- solution(nQueens8, [8, 4, 1, 3, 6, 2, 7, 5]).
93
- solved(nQueens8, true).
@@ -1,3 +0,0 @@
1
- primeImplicants(quine_mccluskey, [[0, 0, x, x], [0, x, x, 1], [x, x, 1, 1]]).
2
- minimalCover(quine_mccluskey, [[0, 0, x, x], [x, x, 1, 1]]).
3
- equation(quine_mccluskey, "f = ~A~B + CD").
@@ -1,5 +0,0 @@
1
- status(sat_instance, satisfiable).
2
- witness(sat_instance, [value(a, false), value(b, true), value(c, false), value(d, true), value(e, false), value(f, true), value(g, false), value(h, true)]).
3
- modelCount(sat_instance, 18).
4
- variableCount(sat_instance, 8).
5
- clauseCount(sat_instance, 12).
@@ -1 +0,0 @@
1
- optimalTour([aaa, bbb, ccc, ddd, eee, fff, ggg], [[aaa, bbb, fff, ccc, eee, ddd, ggg], 75]).
@@ -1,143 +0,0 @@
1
- % Eyelet-inspired Quine-McCluskey minimization using findall/3 and sort/2.
2
- % Problem: f(A,B,C,D) = Sigma m(1,3,7,11,15) + d(0,2,5).
3
-
4
- % Output declarations: materialize/2 selects the relations written to this example's golden output.
5
- materialize(primeImplicants, 2).
6
- materialize(minimalCover, 2).
7
- materialize(equation, 2).
8
- materialize(reason, 2).
9
-
10
- % Program structure: facts set up the scenario, and rules derive the materialized conclusions.
11
- minterm(1).
12
- minterm(3).
13
- minterm(7).
14
- minterm(11).
15
- minterm(15).
16
-
17
- dont_care(0).
18
- dont_care(2).
19
- dont_care(5).
20
-
21
- bits(0, [0, 0, 0, 0]).
22
- bits(1, [0, 0, 0, 1]).
23
- bits(2, [0, 0, 1, 0]).
24
- bits(3, [0, 0, 1, 1]).
25
- bits(4, [0, 1, 0, 0]).
26
- bits(5, [0, 1, 0, 1]).
27
- bits(6, [0, 1, 1, 0]).
28
- bits(7, [0, 1, 1, 1]).
29
- bits(8, [1, 0, 0, 0]).
30
- bits(9, [1, 0, 0, 1]).
31
- bits(10, [1, 0, 1, 0]).
32
- bits(11, [1, 0, 1, 1]).
33
- bits(12, [1, 1, 0, 0]).
34
- bits(13, [1, 1, 0, 1]).
35
- bits(14, [1, 1, 1, 0]).
36
- bits(15, [1, 1, 1, 1]).
37
-
38
- % Derivation rules: each rule below contributes one logical step toward the displayed results.
39
- initial_pattern(P) :- minterm(M), bits(M, P).
40
- initial_pattern(P) :- dont_care(D), bits(D, P).
41
-
42
- combine(A, B, R) :-
43
- diff_count(A, B, 0, 1, [], Rev),
44
- reverse(Rev, R).
45
-
46
- diff_count([], [], Count, Count, Acc, Acc).
47
- diff_count([X | A], [X | B], Count, Max, Acc, R) :-
48
- diff_count(A, B, Count, Max, [X | Acc], R).
49
- diff_count([X | A], [Y | B], Count, Max, Acc, R) :-
50
- neq(X, Y),
51
- add(Count, 1, Next),
52
- le(Next, Max),
53
- diff_count(A, B, Next, Max, [x | Acc], R).
54
-
55
- combined_once(P) :-
56
- initial_pattern(A),
57
- initial_pattern(B),
58
- combine(A, B, P).
59
-
60
- combined_twice(P) :-
61
- combined_once(A),
62
- combined_once(B),
63
- combine(A, B, P).
64
-
65
- used_initial(P) :-
66
- initial_pattern(Q),
67
- combine(P, Q, _R).
68
-
69
- used_once(P) :-
70
- combined_once(Q),
71
- combine(P, Q, _R).
72
-
73
- prime(P) :-
74
- initial_pattern(P),
75
- not(used_initial(P)).
76
- prime(P) :-
77
- combined_once(P),
78
- not(used_once(P)).
79
- prime(P) :-
80
- combined_twice(P).
81
-
82
- prime_implicants(Primes) :-
83
- minterms(Minterms),
84
- dont_cares(DontCares),
85
- bit_table(Bits),
86
- qm_prime_implicants(Minterms, DontCares, Bits, Primes).
87
-
88
- covers([], []).
89
- covers([x | Pattern], [_Bit | Bits]) :-
90
- covers(Pattern, Bits).
91
- covers([Bit | Pattern], [Bit | Bits]) :-
92
- neq(Bit, x),
93
- covers(Pattern, Bits).
94
-
95
- covers_int(Pattern, Int) :-
96
- bits(Int, Bits),
97
- covers(Pattern, Bits).
98
-
99
- covers_all(_Cover, []).
100
- covers_all(Cover, [M | Minterms]) :-
101
- member(P, Cover),
102
- covers_int(P, M),
103
- covers_all(Cover, Minterms).
104
-
105
- minterms(Minterms) :-
106
- findall(M, minterm(M), Raw),
107
- sort(Raw, Minterms).
108
-
109
- dont_cares(DontCares) :-
110
- findall(D, dont_care(D), Raw),
111
- sort(Raw, DontCares).
112
-
113
- bit_table(Bits) :-
114
- findall(bit(N, Pattern), bits(N, Pattern), Bits).
115
-
116
- pair_from([A | Rest], A, B) :-
117
- member(B, Rest).
118
- pair_from([_Head | Rest], A, B) :-
119
- pair_from(Rest, A, B).
120
-
121
- cover_candidate(Cover) :-
122
- prime_implicants(Primes),
123
- pair_from(Primes, A, B),
124
- sort([A, B], Cover),
125
- minterms(Minterms),
126
- covers_all(Cover, Minterms).
127
-
128
- minimal_cover(Cover) :-
129
- minterms(Minterms),
130
- dont_cares(DontCares),
131
- bit_table(Bits),
132
- qm_minimal_cover(Minterms, DontCares, Bits, Cover).
133
-
134
- primeImplicants(quine_mccluskey, Primes) :-
135
- prime_implicants(Primes).
136
-
137
- minimalCover(quine_mccluskey, Cover) :-
138
- minimal_cover(Cover).
139
-
140
- equation(quine_mccluskey, "f = ~A~B + CD") :-
141
- minimal_cover([[0, 0, x, x], [x, x, 1, 1]]).
142
-
143
- reason(quine_mccluskey, "findall builds implicant sets and sort removes duplicates before cover selection").
@@ -1,80 +0,0 @@
1
- % SAT solving without cut.
2
- %
3
- % A compact DPLL-style example for a finite CNF formula. The program first
4
- % generates truth assignments, then checks every clause declaratively. This
5
- % larger instance has eight variables and twelve clauses, so it is still small
6
- % enough for full model collection but no longer a toy four-variable search.
7
-
8
- % Output declarations: materialize/2 selects the relations written to this example's golden output.
9
- materialize(status, 2).
10
- materialize(witness, 2).
11
- materialize(modelCount, 2).
12
- materialize(variableCount, 2).
13
- materialize(clauseCount, 2).
14
- materialize(reason, 2).
15
-
16
- % Program structure: facts set up the scenario, and rules derive the materialized conclusions.
17
- problem(sat_instance,
18
- [a, b, c, d, e, f, g, h],
19
- [
20
- [pos(a), pos(b)],
21
- [neg(a), pos(c)],
22
- [neg(b), pos(d)],
23
- [pos(c), pos(d)],
24
- [neg(c), pos(e)],
25
- [neg(d), pos(f)],
26
- [pos(e), pos(f)],
27
- [neg(e), pos(g)],
28
- [neg(f), pos(h)],
29
- [pos(g), pos(h)],
30
- [neg(g), pos(a)],
31
- [neg(h), pos(b)]
32
- ]).
33
-
34
- truth(false).
35
- truth(true).
36
-
37
- assignment([], []).
38
- % Derivation rules: each rule below contributes one logical step toward the displayed results.
39
- assignment([Var|Vars], [value(Var, Truth)|Rest]) :-
40
- truth(Truth),
41
- assignment(Vars, Rest).
42
-
43
- literal_true(pos(Var), Assignment) :-
44
- member(value(Var, true), Assignment).
45
- literal_true(neg(Var), Assignment) :-
46
- member(value(Var, false), Assignment).
47
-
48
- clause_true([Literal|_Rest], Assignment) :-
49
- literal_true(Literal, Assignment).
50
- clause_true([_Literal|Rest], Assignment) :-
51
- clause_true(Rest, Assignment).
52
-
53
- cnf_true([], _Assignment).
54
- cnf_true([Clause|Clauses], Assignment) :-
55
- clause_true(Clause, Assignment),
56
- cnf_true(Clauses, Assignment).
57
-
58
- model(Name, Assignment) :-
59
- problem(Name, Variables, Clauses),
60
- cnf_model(Variables, Clauses, Assignment).
61
-
62
- status(sat_instance, satisfiable) :-
63
- once(model(sat_instance, _Assignment)).
64
-
65
- witness(sat_instance, Assignment) :-
66
- once(model(sat_instance, Assignment)).
67
-
68
- modelCount(sat_instance, Count) :-
69
- findall(Assignment, model(sat_instance, Assignment), Models),
70
- length(Models, Count).
71
-
72
- variableCount(sat_instance, Count) :-
73
- problem(sat_instance, Variables, _Clauses),
74
- length(Variables, Count).
75
-
76
- clauseCount(sat_instance, Count) :-
77
- problem(sat_instance, _Variables, Clauses),
78
- length(Clauses, Count).
79
-
80
- reason(sat_instance, "finite 8-variable CNF search succeeds without cut").
@@ -1,64 +0,0 @@
1
- % Traveling salesman problem, adapted from Eyelet input/traveling-salesman.pl.
2
- % The optimal tour matches Eyelet output-swipl/traveling-salesman.pl.
3
-
4
- % Output declarations: materialize/2 selects the relations written to this example's golden output.
5
- materialize(optimalTour, 2).
6
-
7
- % Program structure: facts set up the scenario, and rules derive the materialized conclusions.
8
- % The target list fixes the city set; weighted_hamiltonian_path/4 searches tours.
9
- target_cities([aaa, bbb, ccc, ddd, eee, fff, ggg]).
10
-
11
- distance(aaa, bbb, 10).
12
- distance(aaa, ccc, 15).
13
- distance(aaa, ddd, 20).
14
- distance(aaa, eee, 25).
15
- distance(aaa, fff, 30).
16
- distance(bbb, ccc, 35).
17
- distance(bbb, ddd, 40).
18
- distance(bbb, eee, 20).
19
- distance(bbb, fff, 25).
20
- distance(ccc, ddd, 30).
21
- distance(ccc, eee, 15).
22
- distance(ccc, fff, 10).
23
- distance(ddd, eee, 10).
24
- distance(ddd, fff, 35).
25
- distance(ddd, ggg, 5).
26
- distance(eee, fff, 20).
27
-
28
- % Distances are stored once; distance2/3 makes the graph undirected.
29
- % Derivation rules: each rule below contributes one logical step toward the displayed results.
30
- distance2(A, B, D) :-
31
- distance(A, B, D).
32
- distance2(A, B, D) :-
33
- distance(B, A, D).
34
-
35
- permutation([], []).
36
- permutation(List, [X|Perm]) :-
37
- select(X, List, Rest),
38
- permutation(Rest, Perm).
39
-
40
- total_distance([_Last], 0).
41
- total_distance([X, Y|Tail], D) :-
42
- distance2(X, Y, D1),
43
- total_distance([Y|Tail], D2),
44
- add(D1, D2, D).
45
-
46
- tsp(Cities, Tour, Distance) :-
47
- weighted_hamiltonian_path(distance, Cities, Tour, Distance).
48
-
49
- % Materialized output keeps only the minimum-distance candidate.
50
- optimalTour(Cities, Optimal) :-
51
- target_cities(Cities),
52
- findall([T, D], tsp(Cities, T, D), Solutions),
53
- min_distance(Solutions, Optimal).
54
-
55
- min_distance([Sol|Sols], Optimal) :-
56
- min_distance_acc(Sols, Sol, Optimal).
57
-
58
- min_distance_acc([], Best, Best).
59
- min_distance_acc([[_T, D]|Rest], [_BestTour, BestDistance], Optimal) :-
60
- lt(D, BestDistance),
61
- min_distance_acc(Rest, [_T, D], Optimal).
62
- min_distance_acc([[_T, D]|Rest], [BestTour, BestDistance], Optimal) :-
63
- ge(D, BestDistance),
64
- min_distance_acc(Rest, [BestTour, BestDistance], Optimal).
@@ -1,226 +0,0 @@
1
- // Small dense-matrix arithmetic builtins.
2
- // These are reusable numeric kernels for ground matrices; when arguments are not
3
- // ground proper numeric lists, user-defined matrix predicates remain available.
4
- import { deref, isDecimalInteger, listFromItems, numberTerm, numberTextFromDouble, parseFiniteNumber, properListItems, unify } from '../term.js';
5
-
6
- export const matrixBuiltins = {
7
- register(registry) {
8
- registry.add('matrix_sum', 2, matrixSum, { deterministic: true, fallbackWhenNotReady: true, ready: matrixPairReady });
9
- registry.add('matrix_multiply', 2, matrixMultiply, { deterministic: true, fallbackWhenNotReady: true, ready: matrixPairReady });
10
- registry.add('cholesky_decomposition', 2, choleskyDecomposition, { deterministic: true, fallbackWhenNotReady: true, ready: firstMatrixReady });
11
- registry.add('determinant', 2, determinant, { deterministic: true, fallbackWhenNotReady: true, ready: firstMatrixReady });
12
- registry.add('matrix_inv_triang', 2, matrixInvTriang, { deterministic: true, fallbackWhenNotReady: true, ready: firstMatrixReady });
13
- registry.add('matrix_inversion', 2, matrixInversion, { deterministic: true, fallbackWhenNotReady: true, ready: firstMatrixReady });
14
- }
15
- };
16
-
17
- function firstMatrixReady(goal, env) { return parseMatrix(goal.args[0], env) !== null; }
18
- function matrixPairReady(goal, env) { return parseMatrixPair(goal.args[0], env) !== null; }
19
-
20
- function* matrixSum({ goal, env }) {
21
- const pair = parseMatrixPair(goal.args[0], env);
22
- if (!pair) return;
23
- const [a, b] = pair;
24
- if (!sameShape(a, b)) return;
25
- const out = a.map((row, i) => row.map((cell, j) => addNum(cell, b[i][j])));
26
- yield* unifyMatrix(goal.args[1], out, env);
27
- }
28
-
29
- function* matrixMultiply({ goal, env }) {
30
- const pair = parseMatrixPair(goal.args[0], env);
31
- if (!pair) return;
32
- const out = multiplyMatrices(pair[0], pair[1]);
33
- if (!out) return;
34
- yield* unifyMatrix(goal.args[1], out, env);
35
- }
36
-
37
- function* choleskyDecomposition({ goal, env }) {
38
- const matrix = parseMatrix(goal.args[0], env);
39
- const out = cholesky(matrix);
40
- if (!out) return;
41
- yield* unifyMatrix(goal.args[1], out, env);
42
- }
43
-
44
- function* determinant({ goal, env }) {
45
- const matrix = parseMatrix(goal.args[0], env);
46
- const l = cholesky(matrix);
47
- if (!l) return;
48
- let prod = num(1n);
49
- for (let i = l.length - 1; i >= 0; i--) prod = mulNum(l[i][i], prod);
50
- const det = mulNum(prod, prod);
51
- const next = env.clone();
52
- if (unify(goal.args[1], numTerm(det), next)) yield next;
53
- }
54
-
55
- function* matrixInvTriang({ goal, env }) {
56
- const matrix = parseMatrix(goal.args[0], env);
57
- const out = invertLowerTriangular(matrix);
58
- if (!out) return;
59
- yield* unifyMatrix(goal.args[1], out, env);
60
- }
61
-
62
- function* matrixInversion({ goal, env }) {
63
- const matrix = parseMatrix(goal.args[0], env);
64
- const l = cholesky(matrix);
65
- if (!l) return;
66
- const li = invertLowerTriangular(l);
67
- if (!li) return;
68
- const lit = transpose(li);
69
- const out = multiplyMatrices(lit, li);
70
- if (!out) return;
71
- yield* unifyMatrix(goal.args[1], out, env);
72
- }
73
-
74
- function cholesky(a) {
75
- if (!isSquare(a)) return null;
76
- const n = a.length;
77
- const l = Array.from({ length: n }, () => Array.from({ length: n }, () => num(0n)));
78
- for (let i = 0; i < n; i++) {
79
- for (let j = 0; j <= i; j++) {
80
- let sum = num(0n);
81
- for (let k = 0; k < j; k++) sum = addNum(sum, mulNum(l[i][k], l[j][k]));
82
- if (j === i) {
83
- const v2 = subNum(a[i][i], sum);
84
- const v = Math.sqrt(toNumber(v2));
85
- if (!Number.isFinite(v)) return null;
86
- l[i][j] = num(v);
87
- } else {
88
- const numerator = subNum(a[i][j], sum);
89
- l[i][j] = divNum(numerator, l[j][j]);
90
- }
91
- }
92
- }
93
- return l;
94
- }
95
-
96
- function invertLowerTriangular(l) {
97
- if (!isSquare(l)) return null;
98
- const n = l.length;
99
- const previous = [];
100
- const out = [];
101
- for (let i = 0; i < n; i++) {
102
- const row = [];
103
- for (let j = 0; j < n; j++) {
104
- if (j > i) row.push(num(0n));
105
- else if (i === j) row.push(divNum(num(1), l[i][i]));
106
- else {
107
- let sum = num(0n);
108
- for (let k = j; k < i; k++) sum = addNum(sum, mulNum(l[i][k], previous[k][j]));
109
- row.push(divNum(negNum(sum), l[i][i]));
110
- }
111
- }
112
- previous.push(row);
113
- out.push(row);
114
- }
115
- return out;
116
- }
117
-
118
- function multiplyMatrices(a, b) {
119
- if (!a.length || !b.length || !a[0].length || !b[0].length) return null;
120
- const rows = a.length, inner = a[0].length, cols = b[0].length;
121
- if (b.length !== inner || a.some((row) => row.length !== inner) || b.some((row) => row.length !== cols)) return null;
122
- const out = [];
123
- for (let i = 0; i < rows; i++) {
124
- const row = [];
125
- for (let j = 0; j < cols; j++) {
126
- let total = num(0n);
127
- for (let k = inner - 1; k >= 0; k--) total = addNum(mulNum(a[i][k], b[k][j]), total);
128
- row.push(total);
129
- }
130
- out.push(row);
131
- }
132
- return out;
133
- }
134
-
135
- function transpose(a) {
136
- if (!a.length) return [];
137
- return a[0].map((_, i) => a.map((row) => row[i]));
138
- }
139
-
140
- function sameShape(a, b) {
141
- return a.length === b.length && a.every((row, i) => row.length === b[i].length);
142
- }
143
-
144
- function isSquare(a) {
145
- return Array.isArray(a) && a.length > 0 && a.every((row) => row.length === a.length);
146
- }
147
-
148
- function parseMatrixPair(term, env) {
149
- const pair = properListItems(term, env);
150
- if (!pair || pair.length !== 2) return null;
151
- const a = parseMatrix(pair[0], env), b = parseMatrix(pair[1], env);
152
- return a && b ? [a, b] : null;
153
- }
154
-
155
- function parseMatrix(term, env) {
156
- const rows = properListItems(term, env);
157
- if (!rows) return null;
158
- const matrix = [];
159
- let width = null;
160
- for (const rowTerm of rows) {
161
- const rowItems = properListItems(rowTerm, env);
162
- if (!rowItems) return null;
163
- if (width == null) width = rowItems.length;
164
- else if (width !== rowItems.length) return null;
165
- const row = [];
166
- for (const item of rowItems) {
167
- const n = parseNum(item, env);
168
- if (!n) return null;
169
- row.push(n);
170
- }
171
- matrix.push(row);
172
- }
173
- return matrix;
174
- }
175
-
176
- function parseNum(term, env) {
177
- const value = deref(term, env);
178
- if (value.type !== 'number') return null;
179
- if (isDecimalInteger(value.name)) return num(BigInt(value.name));
180
- const f = parseFiniteNumber(value.name);
181
- return f == null ? null : num(f);
182
- }
183
-
184
- function* unifyMatrix(target, matrix, env) {
185
- const next = env.clone();
186
- if (unify(target, matrixTerm(matrix), next)) yield next;
187
- }
188
-
189
- function matrixTerm(matrix) {
190
- return listFromItems(matrix.map((row) => listFromItems(row.map(numTerm))));
191
- }
192
-
193
- function numTerm(value) {
194
- if (value.kind === 'int') return numberTerm(value.value.toString());
195
- return numberTerm(numberTextFromDouble(value.value));
196
- }
197
-
198
- function num(value) {
199
- return typeof value === 'bigint' ? { kind: 'int', value } : { kind: 'float', value: Number(value) };
200
- }
201
-
202
- function toNumber(value) {
203
- return value.kind === 'int' ? Number(value.value) : value.value;
204
- }
205
-
206
- function addNum(a, b) {
207
- if (a.kind === 'int' && b.kind === 'int') return num(a.value + b.value);
208
- return num(toNumber(a) + toNumber(b));
209
- }
210
- function subNum(a, b) {
211
- if (a.kind === 'int' && b.kind === 'int') return num(a.value - b.value);
212
- return num(toNumber(a) - toNumber(b));
213
- }
214
- function mulNum(a, b) {
215
- if (a.kind === 'int' && b.kind === 'int') return num(a.value * b.value);
216
- return num(toNumber(a) * toNumber(b));
217
- }
218
- function divNum(a, b) {
219
- if (b.kind === 'int' && b.value === 0n) return num(Number.NaN);
220
- if (b.kind === 'float' && b.value === 0) return num(Number.NaN);
221
- if (a.kind === 'int' && b.kind === 'int' && a.value % b.value === 0n) return num(a.value / b.value);
222
- return num(toNumber(a) / toNumber(b));
223
- }
224
- function negNum(a) {
225
- return a.kind === 'int' ? num(-a.value) : num(-a.value);
226
- }