rubylog 1.0.0 → 2.0pre1
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.
- data/Gemfile +3 -12
- data/Gemfile.lock +22 -48
- data/README.rdoc +38 -38
- data/README.rdoc.orig +284 -0
- data/RELEASE_NOTES.rdoc +51 -0
- data/Rakefile +14 -18
- data/TODO.txt +0 -0
- data/VERSION +1 -1
- data/examples/a_plus_b.rb +6 -0
- data/examples/checkmate.rb +88 -0
- data/examples/combination.rb +17 -0
- data/examples/dcg.rb +3 -2
- data/examples/dcg2.rb +2 -2
- data/{logic → examples}/directory_structure_logic.rb +3 -5
- data/examples/dirlist.rb +4 -0
- data/examples/divisors.rb +6 -0
- data/examples/enumerators.rb +3 -3
- data/examples/factorial.rb +2 -3
- data/examples/file_search.rb +14 -0
- data/examples/hanoi.rb +4 -5
- data/examples/hello.rb +6 -4
- data/examples/mice.rb +92 -0
- data/examples/mice2.rb +19 -0
- data/examples/n_queens.rb +32 -0
- data/examples/object_oriented.rb +14 -0
- data/examples/palindrome_detection.rb +18 -0
- data/examples/parsing.rb +6 -4
- data/examples/permutation.rb +12 -0
- data/examples/prefix.rb +13 -0
- data/examples/primality_by_division.rb +22 -0
- data/examples/primitives.rb +10 -8
- data/examples/sieve_of_eratosthenes.rb +14 -0
- data/examples/string_interpolation.rb +4 -0
- data/examples/sudoku.rb +52 -0
- data/examples/tracing.rb +19 -0
- data/lib/rspec/rubylog.rb +29 -0
- data/lib/rubylog/assertable.rb +24 -0
- data/lib/rubylog/builtins/arithmetics.rb +63 -0
- data/lib/rubylog/builtins/assumption.rb +71 -0
- data/lib/rubylog/builtins/ensure.rb +13 -0
- data/lib/rubylog/builtins/file_system.rb +30 -8
- data/lib/rubylog/builtins/logic.rb +69 -38
- data/lib/rubylog/builtins/reflection.rb +35 -50
- data/lib/rubylog/builtins/term.rb +15 -17
- data/lib/rubylog/builtins.rb +11 -0
- data/lib/rubylog/clause.rb +19 -0
- data/lib/rubylog/{interfaces/composite_term.rb → compound_term.rb} +3 -3
- data/lib/rubylog/context.rb +24 -0
- data/lib/rubylog/context_creation.rb +71 -0
- data/lib/rubylog/context_modules/checks.rb +35 -0
- data/lib/rubylog/context_modules/demonstration.rb +16 -0
- data/lib/rubylog/context_modules/predicates.rb +86 -0
- data/lib/rubylog/context_modules/primitives.rb +18 -0
- data/lib/rubylog/context_modules/thats.rb +13 -0
- data/lib/rubylog/default_context.rb +9 -0
- data/lib/rubylog/dsl/array_splat.rb +11 -3
- data/lib/rubylog/dsl/primitives.rb +24 -12
- data/lib/rubylog/dsl/thats.rb +6 -0
- data/lib/rubylog/dsl/variables.rb +56 -21
- data/lib/rubylog/errors.rb +26 -15
- data/lib/rubylog/mixins/array.rb +95 -62
- data/lib/rubylog/mixins/kernel.rb +3 -2
- data/lib/rubylog/mixins/method.rb +0 -1
- data/lib/rubylog/mixins/object.rb +2 -1
- data/lib/rubylog/mixins/proc.rb +9 -12
- data/lib/rubylog/mixins/string.rb +15 -23
- data/lib/rubylog/mixins/symbol.rb +7 -24
- data/lib/rubylog/nullary_predicates.rb +3 -0
- data/lib/rubylog/predicate.rb +53 -0
- data/lib/rubylog/primitive.rb +15 -0
- data/lib/rubylog/procedure.rb +42 -0
- data/lib/rubylog/rule.rb +24 -0
- data/lib/rubylog/structure.rb +19 -38
- data/lib/rubylog/{interfaces/term.rb → term.rb} +2 -7
- data/lib/rubylog/tracing.rb +75 -0
- data/lib/rubylog/variable.rb +31 -12
- data/lib/rubylog.rb +36 -32
- data/rubylog.gemspec +92 -84
- data/spec/inriasuite_spec.rb +906 -9
- data/spec/integration/custom_classes_spec.rb +61 -0
- data/spec/integration/dsl_spec.rb +38 -0
- data/spec/integration/recursion_spec.rb +14 -0
- data/spec/integration/theory_as_module_spec.rb +20 -0
- data/spec/integration/theory_as_module_with_include_spec.rb +14 -0
- data/spec/rspec/rubylog_spec.rb +75 -0
- data/spec/rubylog/assertable_spec.rb +111 -0
- data/spec/rubylog/builtins/arithmetics_spec.rb +94 -0
- data/spec/rubylog/builtins/assumption_spec.rb +70 -0
- data/spec/rubylog/builtins/ensure_spec.rb +8 -0
- data/spec/rubylog/builtins/file_system_spec.rb +40 -0
- data/spec/rubylog/builtins/logic_spec.rb +340 -0
- data/spec/rubylog/builtins/reflection_spec.rb +43 -0
- data/spec/rubylog/builtins/term_spec.rb +85 -0
- data/spec/rubylog/context_modules/demonstration_spec.rb +132 -0
- data/spec/rubylog/context_modules/predicates_spec.rb +57 -0
- data/spec/rubylog/context_modules/thats_spec.rb +94 -0
- data/spec/rubylog/dsl/array_splat_spec.rb +15 -0
- data/spec/rubylog/dsl/primitives_spec.rb +43 -0
- data/spec/rubylog/errors_spec.rb +18 -0
- data/spec/{unification_spec.rb → rubylog/interfaces/term_spec.rb} +8 -9
- data/spec/rubylog/mixins/array_spec.rb +80 -0
- data/spec/rubylog/mixins/composite_term_spec.rb +66 -0
- data/spec/rubylog/mixins/proc_spec.rb +59 -0
- data/spec/rubylog/mixins/string_spec.rb +48 -0
- data/spec/rubylog/mixins/symbol_spec.rb +9 -0
- data/spec/{clause_spec.rb → rubylog/structure_spec.rb} +16 -15
- data/spec/rubylog/term_spec.rb +7 -0
- data/spec/rubylog/tracing_spec.input +27 -0
- data/spec/rubylog/tracing_spec.rb +44 -0
- data/spec/rubylog/variable_spec.rb +279 -0
- data/spec/spec_helper.rb +1 -0
- data/vimrc +11 -0
- metadata +103 -123
- data/README.hu.rb +0 -58
- data/bin/rubylog +0 -18
- data/examples/theory.rb +0 -32
- data/lib/rubylog/builtins/default.rb +0 -10
- data/lib/rubylog/dsl.rb +0 -70
- data/lib/rubylog/interfaces/assertable.rb +0 -16
- data/lib/rubylog/interfaces/callable.rb +0 -18
- data/lib/rubylog/interfaces/predicate.rb +0 -8
- data/lib/rubylog/interfaces/procedure.rb +0 -60
- data/lib/rubylog/mixins/class.rb +0 -11
- data/lib/rubylog/simple_procedure.rb +0 -8
- data/lib/rubylog/theory.rb +0 -422
- data/logic/builtins/file_system_logic.rb +0 -23
- data/logic/builtins/reflection_logic.rb +0 -40
- data/logic/dereference_logic.rb +0 -23
- data/logic/dsl_logic.rb +0 -29
- data/logic/errors_logic.rb +0 -9
- data/logic/guard_logic.rb +0 -115
- data/logic/list_logic.rb +0 -55
- data/logic/map_logic.rb +0 -15
- data/logic/multitheory.rb +0 -23
- data/logic/recursion_logic.rb +0 -12
- data/logic/string_logic.rb +0 -41
- data/logic/thats_logic.rb +0 -51
- data/logic/variable_logic.rb +0 -24
- data/spec/bartak_guide_spec.rb +0 -86
- data/spec/builtins/all_spec.rb +0 -99
- data/spec/builtins/and_spec.rb +0 -22
- data/spec/builtins/array_spec.rb +0 -16
- data/spec/builtins/branch_or_spec.rb +0 -27
- data/spec/builtins/cut_spec.rb +0 -44
- data/spec/builtins/fail_spec.rb +0 -5
- data/spec/builtins/false_spec.rb +0 -5
- data/spec/builtins/in_spec.rb +0 -38
- data/spec/builtins/is_false_spec.rb +0 -12
- data/spec/builtins/is_spec.rb +0 -26
- data/spec/builtins/matches_spec.rb +0 -23
- data/spec/builtins/or_spec.rb +0 -22
- data/spec/builtins/splits_to.rb +0 -18
- data/spec/builtins/then_spec.rb +0 -27
- data/spec/builtins/true_spec.rb +0 -5
- data/spec/compilation_spec.rb +0 -61
- data/spec/custom_classes_spec.rb +0 -43
- data/spec/dereference.rb +0 -10
- data/spec/queries_spec.rb +0 -150
- data/spec/recursion_spec.rb +0 -18
- data/spec/ruby_code_spec.rb +0 -52
- data/spec/rules_spec.rb +0 -97
- data/spec/theory_spec.rb +0 -29
- data/spec/variable_spec.rb +0 -26
@@ -1,40 +0,0 @@
|
|
1
|
-
require "./lib/rubylog/builtins/reflection.rb"
|
2
|
-
|
3
|
-
theory do
|
4
|
-
functor_for String, :likes, :drinks
|
5
|
-
|
6
|
-
# fact
|
7
|
-
"John".likes! "beer"
|
8
|
-
check "John".likes("beer").fact
|
9
|
-
check { A.likes(B).fact.map{A.likes(B)} == ["John".likes("beer")] }
|
10
|
-
|
11
|
-
# follows_from
|
12
|
-
A.drinks(B).if A.likes(B)
|
13
|
-
check A.drinks(B).follows_from A.likes(B)
|
14
|
-
check {A.drinks(B).follows_from(K).map{K} == [A.likes(B)] }
|
15
|
-
|
16
|
-
# structure
|
17
|
-
check A.likes(B).structure(:likes, [A,B])
|
18
|
-
check { A.likes(B).structure(X,Y).map{[X,Y]} == [[:likes, [A,B]]] }
|
19
|
-
|
20
|
-
# structures with variable functor and partial argument list
|
21
|
-
check { K.structure(:drinks, ["John", "beer"]).map{K} == ["John".drinks("beer")] }
|
22
|
-
check { K.structure(A,[*B]).
|
23
|
-
and(A.is(:drinks)).
|
24
|
-
and(B.is(["John","beer"])).
|
25
|
-
map{K} == ["John".drinks("beer")] }
|
26
|
-
|
27
|
-
# variable
|
28
|
-
# Removed because of the "every built-in prediate is pure logical" principle
|
29
|
-
#check A.variable("A")
|
30
|
-
#check A.variable("B").false
|
31
|
-
#check { A.variable(B).map{B} == ["A"] }
|
32
|
-
# You can use the fact that automatic variable resolution yields nil if the
|
33
|
-
# variable is undefined:
|
34
|
-
#
|
35
|
-
# check A.is(ANY).and{ A }.false
|
36
|
-
# check A.is(ANY).and{ not A }
|
37
|
-
#
|
38
|
-
|
39
|
-
|
40
|
-
end
|
data/logic/dereference_logic.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
theory do
|
2
|
-
functor_for Integer, :divides
|
3
|
-
|
4
|
-
check { A.is_a? Rubylog::Variable }
|
5
|
-
check { A.rubylog_deep_dereference == A }
|
6
|
-
|
7
|
-
a = A
|
8
|
-
check { a.rubylog_deep_dereference.equal? a }
|
9
|
-
|
10
|
-
a.rubylog_unify(4) do
|
11
|
-
check { a.rubylog_deep_dereference == 4 }
|
12
|
-
check { [1,a].rubylog_deep_dereference == [1,4] }
|
13
|
-
check { a.divides(16).rubylog_deep_dereference == 4.divides(16) }
|
14
|
-
end
|
15
|
-
|
16
|
-
b = []
|
17
|
-
check { not b.rubylog_deep_dereference.equal? b }
|
18
|
-
|
19
|
-
c = Object.new
|
20
|
-
check { c.rubylog_deep_dereference.equal? c }
|
21
|
-
end
|
22
|
-
|
23
|
-
|
data/logic/dsl_logic.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
theory do
|
2
|
-
|
3
|
-
# prefix functors
|
4
|
-
|
5
|
-
prefix_functor :we_have
|
6
|
-
we_have! :weapons
|
7
|
-
we_have! :sunglasses
|
8
|
-
we_have! :rustling_leather_coats
|
9
|
-
|
10
|
-
check we_have :sunglasses
|
11
|
-
check { we_have? :rustling_leather_coats }
|
12
|
-
|
13
|
-
|
14
|
-
# built-in prefix functors
|
15
|
-
|
16
|
-
check all X.is(4).and(Y.is(X)), Y.is(4)
|
17
|
-
check any X.is(4)
|
18
|
-
check one(X.in([1,2,3])) { X % 2 == 0 }
|
19
|
-
check none X.in([1,2,3]), X.is(5)
|
20
|
-
|
21
|
-
check { all?(X.is(4)) { X < 5 } }
|
22
|
-
|
23
|
-
# variables
|
24
|
-
check A.is(ANY).and{ A == nil }
|
25
|
-
check A.is(4) .and{ A == 4 }
|
26
|
-
check B.is(4) .and{ A.is_a? Rubylog::Variable }
|
27
|
-
check B.is(ANY).and{ A.is_a? Rubylog::Variable }
|
28
|
-
|
29
|
-
end
|
data/logic/errors_logic.rb
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
theory do
|
2
|
-
functor_for String, :happy, :has
|
3
|
-
|
4
|
-
# syntax error
|
5
|
-
check { begin "John".happy.if; rescue Rubylog::SyntaxError; true else false end }
|
6
|
-
check { begin "John".happy.if!; rescue Rubylog::SyntaxError; true else false end }
|
7
|
-
check { begin "John".happy.unless; rescue Rubylog::SyntaxError; true else false end }
|
8
|
-
|
9
|
-
end
|
data/logic/guard_logic.rb
DELETED
@@ -1,115 +0,0 @@
|
|
1
|
-
theory do
|
2
|
-
|
3
|
-
# class guard
|
4
|
-
functor_for Numeric, :divides
|
5
|
-
A[Integer].divides(B[Integer]).if { B % A == 0 }
|
6
|
-
A[Float].divides!(B[Float])
|
7
|
-
check 2.divides(10)
|
8
|
-
check 2.divides(9).false
|
9
|
-
check 2.divides(1).false
|
10
|
-
check 2.divides(0)
|
11
|
-
check 2.12.divides(4.5)
|
12
|
-
check -0.31.divides(-1.5)
|
13
|
-
check 0.3.divides(3).false
|
14
|
-
check 2.0.divides(10).false
|
15
|
-
check 2.divides(10.0).false
|
16
|
-
check 2.divides(9.0).false
|
17
|
-
|
18
|
-
# union of guards at compile
|
19
|
-
functor_for Numeric, :small
|
20
|
-
A[0...100].small.if ANY.is A[Integer]
|
21
|
-
check 0.small
|
22
|
-
check 10.small
|
23
|
-
check -1.small.false
|
24
|
-
check 0.0.small.false
|
25
|
-
check 99.small
|
26
|
-
check 99.0.small.false
|
27
|
-
check 99.9.small.false
|
28
|
-
check 100.small.false
|
29
|
-
check 100.0.small.false
|
30
|
-
|
31
|
-
# union of guards at compile (dont-care)
|
32
|
-
functor_for Numeric, :small
|
33
|
-
A[0...100].small.if A.is ANY[Integer]
|
34
|
-
check 0.small
|
35
|
-
check 10.small
|
36
|
-
check -1.small.false
|
37
|
-
check 0.0.small.false
|
38
|
-
check 99.small
|
39
|
-
check 99.0.small.false
|
40
|
-
check 99.9.small.false
|
41
|
-
check 100.small.false
|
42
|
-
check 100.0.small.false
|
43
|
-
|
44
|
-
# union of guards at unification
|
45
|
-
self[[:small,1]].clear
|
46
|
-
functor_for Numeric, :small
|
47
|
-
A[0...100].small.if A.is B[Integer]
|
48
|
-
check 0.small
|
49
|
-
check 10.small
|
50
|
-
check -1.small.false
|
51
|
-
check 0.0.small.false
|
52
|
-
check 99.small
|
53
|
-
check 99.0.small.false
|
54
|
-
check 99.9.small.false
|
55
|
-
check 100.small.false
|
56
|
-
check 100.0.small.false
|
57
|
-
|
58
|
-
# union of guards at unification (reversed)
|
59
|
-
self[[:small,1]].clear
|
60
|
-
functor_for Numeric, :small
|
61
|
-
A[0...100].small.if B[Integer].is A
|
62
|
-
check 0.small
|
63
|
-
check 10.small
|
64
|
-
check -1.small.false
|
65
|
-
check 0.0.small.false
|
66
|
-
check 99.small
|
67
|
-
check 99.0.small.false
|
68
|
-
check 99.9.small.false
|
69
|
-
check 100.small.false
|
70
|
-
check 100.0.small.false
|
71
|
-
|
72
|
-
|
73
|
-
# proc guards
|
74
|
-
functor_for Numeric, :big
|
75
|
-
A[proc{|a|a > 20}].big!
|
76
|
-
check -100.big.false
|
77
|
-
check 0.big.false
|
78
|
-
check 10.big.false
|
79
|
-
check 20.big.false
|
80
|
-
check 21.big
|
81
|
-
check 200.big
|
82
|
-
|
83
|
-
# hash guards
|
84
|
-
functor_for String, :char
|
85
|
-
S[length: 1].char!
|
86
|
-
check "a".char
|
87
|
-
check " ".char
|
88
|
-
check "".char.false
|
89
|
-
check "af".char.false
|
90
|
-
check "Hello".char.false
|
91
|
-
|
92
|
-
# list hash guards
|
93
|
-
functor_for String, :capitalized
|
94
|
-
S[[:[],0] => /[A-Z]/].capitalized!
|
95
|
-
check "a".capitalized.false
|
96
|
-
check " ".capitalized.false
|
97
|
-
check "".capitalized.false
|
98
|
-
check "af".capitalized.false
|
99
|
-
check "A".capitalized
|
100
|
-
check "Hello".capitalized
|
101
|
-
|
102
|
-
|
103
|
-
# chained hash guards
|
104
|
-
functor_for String, :funny
|
105
|
-
S[upcase: {[:[],1..-2] => "ELL"}].funny!
|
106
|
-
check "hello".funny
|
107
|
-
check "Bell!".funny
|
108
|
-
check "BELL!".funny
|
109
|
-
check "DELL".funny.false
|
110
|
-
check "help!".funny.false
|
111
|
-
check "hello!".funny.false
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
end
|
data/logic/list_logic.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
theory do
|
2
|
-
check [1,2,3].is [1,2,3]
|
3
|
-
check {[1,2,3].is([A,B,C]).map{[A,B,C]} == [[1,2,3]]}
|
4
|
-
check [].is []
|
5
|
-
|
6
|
-
check [1,2,3].is_not [1,2,4]
|
7
|
-
check [1,2,3].is_not [1,2]
|
8
|
-
check [1,2,3].is_not [1,ANY]
|
9
|
-
check [1,2,3].is [1,2,ANY]
|
10
|
-
|
11
|
-
# splats
|
12
|
-
check { [1,2,3].is([1,*A]).map{A} == [[2,3]] }
|
13
|
-
check [].is_not [A,*ANY]
|
14
|
-
check [].is [*ANY]
|
15
|
-
check [2].is [2, *ANY]
|
16
|
-
check [2, *ANY].is [2]
|
17
|
-
check [1,2].is([A,*B]).and A.is(1).and B.is([2])
|
18
|
-
|
19
|
-
# [*A] = [*B]
|
20
|
-
check {[*A].is([*B]).and A.is([1,2]).map{B} == [[1,2]]}
|
21
|
-
check [*A].is [*B]
|
22
|
-
check { [*A].is([*B]).each { } } # no infinite recursion
|
23
|
-
|
24
|
-
|
25
|
-
# append
|
26
|
-
check {[1,2,3].is([*A,*B]).map{[A,B]} == [[[],[1,2,3]],[[1],[2,3]],[[1,2],[3]],[[1,2,3],[]]] }
|
27
|
-
check [1,2,3].is [Rubylog::DSL::ArraySplat.new([1,2]),*ANY]
|
28
|
-
check [1,2,3].is [Rubylog::DSL::ArraySplat.new([1,2,3]),*ANY]
|
29
|
-
check [1,2,3].is_not [Rubylog::DSL::ArraySplat.new([1,2,3,B]),*ANY]
|
30
|
-
|
31
|
-
# member
|
32
|
-
check [1,2,3].is [*ANY,2,*ANY]
|
33
|
-
check X.is([1,2,3]).and A.in(X).all X.is [*ANY,A,*ANY]
|
34
|
-
|
35
|
-
# palindromes
|
36
|
-
functor_for Array, :palindrome
|
37
|
-
[].palindrome!
|
38
|
-
[ANY].palindrome!
|
39
|
-
[A,*X,A].palindrome.if X.palindrome
|
40
|
-
check X.in([[], [1], [1,1], [1,2,1], [1,2,2,1], [1,2,3,2,1], [1,2,3,3,2,1]]).all X.palindrome
|
41
|
-
check X.in([[1,2], [1,3,4,1], [2,2,5]]).none X.palindrome
|
42
|
-
|
43
|
-
# legacy syntax
|
44
|
-
check { [1,2,3].is([1]+A).map{A} == [[2,3]] }
|
45
|
-
check [].is_not [A]+ANY
|
46
|
-
check [].is []+ANY
|
47
|
-
check [2].is [2]+ANY
|
48
|
-
check(([2]+ANY).is [2])
|
49
|
-
check [1,2].is([A]+B).and A.is(1).and B.is([2])
|
50
|
-
check {([]+A).is([]+B).and A.is([1,2]).map{B} == [[1,2]]}
|
51
|
-
check(([]+A).is([]+B))
|
52
|
-
check {([]+A).is([]+B).each { } }
|
53
|
-
check {[1,2,3].is([]+A+B).map{[A,B]} == [[[],[1,2,3]],[[1],[2,3]],[[1,2],[3]],[[1,2,3],[]]] }
|
54
|
-
check X.is([1,2,3]).and A.in(X).all X.is []+ANY+[A]+ANY
|
55
|
-
end
|
data/logic/map_logic.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
theory "Rubylog::MapLogic" do
|
2
|
-
functor :likes
|
3
|
-
subject Symbol
|
4
|
-
|
5
|
-
# inriausite^findall
|
6
|
-
check S.is{X.is(1).or(X.is(2)).map{X}}.and{S == [1,2]}
|
7
|
-
check S.is{X.is(:john).map{X.likes(Y)}}.and{S == [:john.likes(Y)]}
|
8
|
-
check G.is(:fail.or :fail).and L.is{G.map{X}}.and{L == []}
|
9
|
-
check S.is{X.is(1).or(X.is(1)).map{X}}.and{S == [1,1]}
|
10
|
-
check [1,2].is{X.is(2).or(X.is(1)).map{X}}.false
|
11
|
-
check [X,Y].is{A.is(1).or(A.is(2)).map{A}}.and{X == 1}.and{Y == 2}
|
12
|
-
(S.is {Goal.map{X}}).solve and check :fail rescue NoMethodError
|
13
|
-
(S.is {4.map{X}}).solve and check :fail rescue NoMethodError
|
14
|
-
end
|
15
|
-
|
data/logic/multitheory.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
load "lib/rubylog/theory.rb"
|
2
|
-
A = theory do
|
3
|
-
functor_for Symbol, :good
|
4
|
-
|
5
|
-
:x.good!
|
6
|
-
end
|
7
|
-
|
8
|
-
B = theory do
|
9
|
-
:y.good!
|
10
|
-
end
|
11
|
-
|
12
|
-
theory do
|
13
|
-
multitheory [:good,1]
|
14
|
-
|
15
|
-
check { self[[:good,1]].multitheory? }
|
16
|
-
|
17
|
-
include A
|
18
|
-
include B
|
19
|
-
|
20
|
-
check { self[[:good,1]].multitheory? }
|
21
|
-
|
22
|
-
check {X.good.map{X} == [:x,:y]}
|
23
|
-
end
|
data/logic/recursion_logic.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
theory do
|
2
|
-
functor_for Integer, :factorial
|
3
|
-
0.factorial! 1
|
4
|
-
N.factorial(K).if proc{N > 0}.and N1.is {N-1} .and N1.factorial(K1).and K.is{ N*K1 }
|
5
|
-
|
6
|
-
check 0.factorial 1
|
7
|
-
check 1.factorial 1
|
8
|
-
check 2.factorial 2
|
9
|
-
check 3.factorial 6
|
10
|
-
check 4.factorial 24
|
11
|
-
check 7.factorial 5040
|
12
|
-
end
|
data/logic/string_logic.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
theory do
|
2
|
-
check "asdf".is "asdf"
|
3
|
-
check { "abc#{S}def" =~ /abc.S\[\].def/ }
|
4
|
-
check { "abc#{S[length: 1]}def" =~ /abc.S\[\d+\].def/ }
|
5
|
-
check { "h#{S}o".is("hello").map{S} == ["ell"] }
|
6
|
-
check { "#{Base}.#{Ext}".is("hello.rb").map{Base} == ["hello"] }
|
7
|
-
|
8
|
-
check { "#{Base}.#{Ext}".is("hello.rb").map{Ext} == ["rb"] }
|
9
|
-
check { "h#{S}o".is("auto").map{S} == [] }
|
10
|
-
|
11
|
-
# backtracked matches
|
12
|
-
check { "abc".is("#{A}#{B}").map{"#{A}:#{B}"} == [":abc","a:bc","ab:c","abc:"] }
|
13
|
-
check { "www.google.com".is("#{A}.#{B}").map{[A,B]} == [["www", "google.com"],["www.google", "com"]] }
|
14
|
-
check { "a".is("#{A}#{B}#{C}").map{"#{A}:#{B}:#{C}"} == ["::a",":a:","a::"] }
|
15
|
-
check { "ab".is("#{A}#{B}#{C}").map{"#{A}:#{B}:#{C}"} == %w(::ab :a:b :ab: a::b a:b: ab::) }
|
16
|
-
|
17
|
-
# guards
|
18
|
-
check { "abc".is("#{A[/\A.\z/]}#{B}").map{"#{A}:#{B}"} == ["a:bc"] }
|
19
|
-
check { "abc".is("#{A[length: 1]}#{B}").map{"#{A}:#{B}"} == ["a:bc"] }
|
20
|
-
|
21
|
-
|
22
|
-
# palindromes
|
23
|
-
functor_for String, :palindrome
|
24
|
-
|
25
|
-
S[empty?: true].palindrome!
|
26
|
-
S[length: 1].palindrome!
|
27
|
-
S[lambda{|s|s.length > 1}].palindrome.if S.is("#{A[length: 1]}#{B}#{A}").and B.palindrome
|
28
|
-
|
29
|
-
check "".palindrome
|
30
|
-
check "dd".palindrome
|
31
|
-
check "aba".palindrome
|
32
|
-
check "faaf".palindrome
|
33
|
-
check "ffaaff".palindrome
|
34
|
-
check "rererer".palindrome
|
35
|
-
check "lol".palindrome
|
36
|
-
check "ji".palindrome.false
|
37
|
-
check "doo".palindrome.false
|
38
|
-
check "taaaz".palindrome.false
|
39
|
-
check "faad".palindrome.false
|
40
|
-
check "rerere".palindrome.false
|
41
|
-
end
|
data/logic/thats_logic.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
require "./lib/rubylog/builtins/file_system"
|
2
|
-
|
3
|
-
theory do
|
4
|
-
functor_for Integer, :factorial
|
5
|
-
|
6
|
-
# one level
|
7
|
-
check 4.is(ANY[Integer,thats < 10])
|
8
|
-
check 4.is(ANY[Integer,thats < 5])
|
9
|
-
check 4.is(ANY[Integer,thats < 4]).false
|
10
|
-
check 4.is(ANY[Integer,thats < 2]).false
|
11
|
-
|
12
|
-
# question mark
|
13
|
-
check "".is(ANY[thats.empty?])
|
14
|
-
check "a".is(ANY[thats.empty?]).false
|
15
|
-
|
16
|
-
# two levels
|
17
|
-
check "hello".is(ANYTHING[thats.reverse == "olleh"])
|
18
|
-
check "hello".is(ANYTHING[thats.reverse == "olle"]).false
|
19
|
-
|
20
|
-
# four levels
|
21
|
-
check "hello".is(ANY[thats.upcase.partition("E")[0] == "H"])
|
22
|
-
check "hello".is(ANY[thats.upcase.partition("E")[1] == "E"])
|
23
|
-
check "hello".is(ANY[thats.upcase.partition("E")[2] == "LLO"])
|
24
|
-
check "hello".is(ANY[thats.upcase.partition("E")[1] == "H"]).false
|
25
|
-
check "hello".is(ANY[thats.upcase.partition("E")[0] == "h"]).false
|
26
|
-
check "hello".is(ANY[thats.upcase.partition("L")[0] == "H"]).false
|
27
|
-
check "hello".is(ANY[thats.upcase.partition("e")[0] == "H"]).false
|
28
|
-
|
29
|
-
# files
|
30
|
-
check { "#{S[String, thats.start_with?(".")]}".filename_in(".").map{S}.include? ".gitignore" }
|
31
|
-
check { not "#{S[String, thats.start_with?(".")]}".filename_in(".").map{S}.include? "lib" }
|
32
|
-
check { "#{S[/\A(.*)\.rb/]}".filename_in("lib").map{S} == ["rubylog.rb"] }
|
33
|
-
|
34
|
-
# factorial
|
35
|
-
0.factorial! 1
|
36
|
-
K[thats > 0].factorial(N).if K0.is{K-1}.and K0.factorial(N0).and N.is{N0*K}
|
37
|
-
|
38
|
-
# palindrome
|
39
|
-
functor_for String, :palindrome
|
40
|
-
S[String, thats.length <= 1].palindrome!
|
41
|
-
"#{A[thats.length == 1]}#{B}#{A}".palindrome.if B.palindrome
|
42
|
-
|
43
|
-
check "a".palindrome
|
44
|
-
check "aa".palindrome
|
45
|
-
check "aga".palindrome
|
46
|
-
check "aaa".palindrome
|
47
|
-
check "avava".palindrome
|
48
|
-
check "ab".palindrome.false
|
49
|
-
check "abb".palindrome.false
|
50
|
-
check "abab".palindrome.false
|
51
|
-
end
|
data/logic/variable_logic.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
theory do
|
2
|
-
# dont-care variables case insensitive ANY* and _*
|
3
|
-
check 3.is(ANY).and 3.is(ANY)
|
4
|
-
check 3.is(ANY).and 4.is(ANY)
|
5
|
-
check 3.is(ANYTHING).and 3.is(ANYTHING)
|
6
|
-
check 3.is(ANYTHING).and 4.is(ANYTHING)
|
7
|
-
check 3.is(Anything).and 3.is(Anything)
|
8
|
-
check 3.is(Anything).and 4.is(Anything)
|
9
|
-
check 3.is(Rubylog::Variable.new(:_var1)).and(3.is(Rubylog::Variable.new(:_var1)))
|
10
|
-
check 3.is(Rubylog::Variable.new(:_var1)).and(4.is(Rubylog::Variable.new(:_var1)))
|
11
|
-
check 3.is(Rubylog::Variable.new(:var1 )).and(3.is(Rubylog::Variable.new(:var1 )))
|
12
|
-
check 3.is(Rubylog::Variable.new(:var1 )).and(4.is(Rubylog::Variable.new(:var1 ))).false
|
13
|
-
|
14
|
-
# dont-care variables support recursion
|
15
|
-
functor_for Integer, :factorial
|
16
|
-
0.factorial! 1
|
17
|
-
N.factorial(K).if proc{N > 0}.and N1.is {N-1} .and N1.factorial(K1).and K.is{ N*K1 }.and K.is(ANY1).and N.is(ANY2)
|
18
|
-
check 0.factorial 1
|
19
|
-
check 1.factorial 1
|
20
|
-
check 2.factorial 2
|
21
|
-
check 3.factorial 6
|
22
|
-
check 4.factorial 24
|
23
|
-
check 7.factorial 5040
|
24
|
-
end
|
data/spec/bartak_guide_spec.rb
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# These are the rubylog transcripts of prolog codes from Roman Barták's Prolog
|
3
|
-
# guide: http://kti.mff.cuni.cz/~bartak/prolog/
|
4
|
-
|
5
|
-
require 'rubylog'
|
6
|
-
|
7
|
-
|
8
|
-
theory "Genealogy" do
|
9
|
-
subject Symbol
|
10
|
-
functor :man, :woman
|
11
|
-
|
12
|
-
:adam.man!
|
13
|
-
:peter.man!
|
14
|
-
:paul.man!
|
15
|
-
|
16
|
-
:marry.woman!
|
17
|
-
:eve.woman!
|
18
|
-
end
|
19
|
-
|
20
|
-
__END__
|
21
|
-
theory "Lists" do
|
22
|
-
functor_for Object, :head_of
|
23
|
-
functor_for Array, :tail_of
|
24
|
-
|
25
|
-
H.head_of(A).if A.splits_to(H,ANY)
|
26
|
-
T.tail_of(A).if A.splits_to(ANY,T)
|
27
|
-
|
28
|
-
2.should be_head_of [2,3,4]
|
29
|
-
2.should be_head_of [2]
|
30
|
-
2.should_not be_head_of [1]
|
31
|
-
2.should_not be_head_of []
|
32
|
-
|
33
|
-
[3,4].should be_tail_of [2,3,4]
|
34
|
-
[]. should be_tail_of [1]
|
35
|
-
|
36
|
-
Object.rubylog_functor :member_of
|
37
|
-
|
38
|
-
M.member_of(A).if A.splits_to(M,ANY)
|
39
|
-
M.member_of(A).if A.splits_to(ANY,T).and M.member_of(T)
|
40
|
-
|
41
|
-
0.should_not be_member_of [1,2,3]
|
42
|
-
1.should be_member_of [1,2,3]
|
43
|
-
2.should be_member_of [1,2,3]
|
44
|
-
3.should be_member_of [1,2,3]
|
45
|
-
4.should_not be_member_of [1,2,3]
|
46
|
-
|
47
|
-
Object.rubylog_functor :first_of, :last_of
|
48
|
-
|
49
|
-
F.first_of(A).if A.splits_to(F,ANY)
|
50
|
-
L.last_of(A).if A.splits_to(L,[])
|
51
|
-
L.last_of(A).if A.splits_to(H,T).and L.last_of(T)
|
52
|
-
|
53
|
-
1.should be_first_of [1,2,3]
|
54
|
-
3.should_not be_last_of []
|
55
|
-
3.should be_last_of [3]
|
56
|
-
3.should be_last_of [2,3]
|
57
|
-
3.should be_last_of [1,2,3]
|
58
|
-
1.should_not be_last_of [1,2,3]
|
59
|
-
|
60
|
-
Array.rubylog_functor :prefix_of
|
61
|
-
|
62
|
-
[].prefix_of! ANY_LIST
|
63
|
-
A.prefix_of(B).if A.splits_to(H,TA).and B.splits_to(H,TB).and TA.prefix_of(TB)
|
64
|
-
|
65
|
-
[1,2,3].should be_prefix_of [1,2,3]
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
describe "How does it work?" do
|
70
|
-
|
71
|
-
specify "declarative character" do
|
72
|
-
(1.in? [1,2,3]).should be_true
|
73
|
-
(X.in [1,2,3]).to_a.should == [1,2,3]
|
74
|
-
(1.in [X,Y]).to_a.should == [[1,nil], [nil,1]]
|
75
|
-
lists = []
|
76
|
-
(1.in L).each do |l|
|
77
|
-
lists << l
|
78
|
-
break if lists.size == 3
|
79
|
-
end
|
80
|
-
#lists.should == [[1], [nil,1], [nil,nil,1]]
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
data/spec/builtins/all_spec.rb
DELETED
@@ -1,99 +0,0 @@
|
|
1
|
-
|
2
|
-
describe "all,any,one,none" do
|
3
|
-
before do
|
4
|
-
:john.likes! :water
|
5
|
-
:john.likes! :beer
|
6
|
-
|
7
|
-
:jane.likes! :water
|
8
|
-
:jane.likes! :milk
|
9
|
-
:jane.likes! :beer
|
10
|
-
|
11
|
-
:jeff.likes! :water
|
12
|
-
:jeff.likes! :absinth
|
13
|
-
|
14
|
-
:todd.likes! :milk
|
15
|
-
|
16
|
-
@predicates = [:all, :any, :one, :none]
|
17
|
-
@names = :john, :jane, :jeff, :todd
|
18
|
-
@good =
|
19
|
-
[
|
20
|
-
[[1,1,0,0], [0,1,0,0], [0,0,1,0], [0,1,0,1]], # all
|
21
|
-
[[1,1,1,0], [1,1,1,1], [1,1,1,0], [0,1,0,1]], # any
|
22
|
-
[[0,0,1,0], [0,0,1,1], [1,1,0,0], [0,1,0,1]], # one
|
23
|
-
[[0,0,0,1], [0,0,0,0], [0,0,0,1], [1,0,1,0]] # none
|
24
|
-
]
|
25
|
-
end
|
26
|
-
|
27
|
-
it "work" do
|
28
|
-
@predicates.map{|p| @names.map{|n| @names.map{|m|
|
29
|
-
(n.likes(K).send p, m.likes(K)).true? ? 1 : 0
|
30
|
-
}}}.should == @good
|
31
|
-
end
|
32
|
-
|
33
|
-
it "mimic well enumerators' predicates" do
|
34
|
-
@predicates.map{|p| @names.map{|n| @names.map{|m|
|
35
|
-
n.likes(K).to_a.send(:"#{p}?"){|x| m.likes?(x) } ? 1 : 0
|
36
|
-
}}}.should == @good
|
37
|
-
end
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
it "all works" do
|
42
|
-
(:john.likes(X).all(:john.likes(X))).should stand
|
43
|
-
(:john.likes(X).all(:jane.likes(X))).should stand
|
44
|
-
(:john.likes(X).all(:jeff.likes(X))).should_not stand
|
45
|
-
(:john.likes(X).all(:todd.likes(X))).should_not stand
|
46
|
-
|
47
|
-
(:jane.likes(X).all(:john.likes(X))).should_not stand
|
48
|
-
(:jane.likes(X).all(:jane.likes(X))).should stand
|
49
|
-
(:jane.likes(X).all(:jeff.likes(X))).should_not stand
|
50
|
-
(:jane.likes(X).all(:todd.likes(X))).should_not stand
|
51
|
-
|
52
|
-
(:jeff.likes(X).all(:john.likes(X))).should_not stand
|
53
|
-
(:jeff.likes(X).all(:jane.likes(X))).should_not stand
|
54
|
-
(:jeff.likes(X).all(:jeff.likes(X))).should stand
|
55
|
-
(:jeff.likes(X).all(:todd.likes(X))).should_not stand
|
56
|
-
|
57
|
-
(:todd.likes(X).all(:john.likes(X))).should_not stand
|
58
|
-
(:todd.likes(X).all(:jane.likes(X))).should stand
|
59
|
-
(:todd.likes(X).all(:jeff.likes(X))).should_not stand
|
60
|
-
(:todd.likes(X).all(:todd.likes(X))).should stand
|
61
|
-
end
|
62
|
-
|
63
|
-
it "can be called with global functor syntax" do
|
64
|
-
extend Rubylog::DSL::GlobalFunctors
|
65
|
-
all(:john.likes(X), :jane.likes(X)).should stand
|
66
|
-
all(:jane.likes(X), :john.likes(X)).should_not stand
|
67
|
-
any(:jane.likes(X), :todd.likes(X)).should stand
|
68
|
-
any(:john.likes(X), :todd.likes(X)).should_not stand
|
69
|
-
end
|
70
|
-
|
71
|
-
it "can be called unarily" do
|
72
|
-
extend Rubylog::DSL::GlobalFunctors
|
73
|
-
one(:john.likes(X)).should_not stand
|
74
|
-
one(:jane.likes(X)).should_not stand
|
75
|
-
one(:jeff.likes(X)).should_not stand
|
76
|
-
one(:todd.likes(X)).should stand
|
77
|
-
one(:jim.likes(X)).should_not stand
|
78
|
-
|
79
|
-
any(:john.likes(X)).should stand
|
80
|
-
any(:jane.likes(X)).should stand
|
81
|
-
any(:jeff.likes(X)).should stand
|
82
|
-
any(:todd.likes(X)).should stand
|
83
|
-
any(:jim.likes(X)).should_not stand
|
84
|
-
|
85
|
-
all(:john.likes(X)).should stand
|
86
|
-
all(:jane.likes(X)).should stand
|
87
|
-
all(:jeff.likes(X)).should stand
|
88
|
-
all(:todd.likes(X)).should stand
|
89
|
-
all(:jim.likes(X)).should stand
|
90
|
-
|
91
|
-
none(:john.likes(X)).should_not stand
|
92
|
-
none(:jane.likes(X)).should_not stand
|
93
|
-
none(:jeff.likes(X)).should_not stand
|
94
|
-
none(:todd.likes(X)).should_not stand
|
95
|
-
none(:jim.likes(X)).should stand
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
data/spec/builtins/and_spec.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
|
2
|
-
describe "and" do
|
3
|
-
it "works 1" do
|
4
|
-
:john.happy.if :fail.and :true
|
5
|
-
:john.should_not be_happy
|
6
|
-
end
|
7
|
-
|
8
|
-
it "works 2" do
|
9
|
-
:john.happy.if :true.and :fail
|
10
|
-
:john.should_not be_happy
|
11
|
-
end
|
12
|
-
|
13
|
-
it "works 3" do
|
14
|
-
:john.happy.if :fail.and :fail
|
15
|
-
:john.should_not be_happy
|
16
|
-
end
|
17
|
-
|
18
|
-
it "works 4" do
|
19
|
-
:john.happy.if :true.and :true
|
20
|
-
:john.should be_happy
|
21
|
-
end
|
22
|
-
end
|
data/spec/builtins/array_spec.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
|
2
|
-
describe "Array" do
|
3
|
-
it "can be unified" do
|
4
|
-
result = false
|
5
|
-
[A,B].rubylog_unify(12) { result = true }
|
6
|
-
result.should == false
|
7
|
-
|
8
|
-
result = false
|
9
|
-
[A,B].rubylog_unify([12,13]) { result = true }
|
10
|
-
result.should == true
|
11
|
-
|
12
|
-
result = false
|
13
|
-
[14,B].rubylog_unify([12,13]) { result = true }
|
14
|
-
result.should == false
|
15
|
-
end
|
16
|
-
end
|