rubylog 1.0.0 → 2.0pre1
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -0,0 +1,61 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "custom classes" do
|
4
|
+
before do
|
5
|
+
class User
|
6
|
+
extend Rubylog::Context
|
7
|
+
predicate ".girl .boy"
|
8
|
+
|
9
|
+
attr_reader :name
|
10
|
+
def initialize name
|
11
|
+
@name = name
|
12
|
+
end
|
13
|
+
|
14
|
+
U.girl.if { U.name =~ /[aeiouh]$/ }
|
15
|
+
U.boy.unless U.girl
|
16
|
+
|
17
|
+
def long_hair?
|
18
|
+
girl?
|
19
|
+
end
|
20
|
+
|
21
|
+
def hello
|
22
|
+
"Hello #{U.is(self).and(U.girl).true? ? "Ms." : "Mr."} #{@name}!"
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can have ruby predicates" do
|
30
|
+
john = User.new "John"
|
31
|
+
john.girl?.should be_false
|
32
|
+
john.boy?.should be_true
|
33
|
+
john.long_hair?.should be_false
|
34
|
+
|
35
|
+
jane = User.new "Jane"
|
36
|
+
jane.girl?.should be_true
|
37
|
+
jane.boy?.should be_false
|
38
|
+
jane.long_hair?.should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "can be used in assertions" do
|
42
|
+
pete = User.new "Pete"
|
43
|
+
pete.boy?.should be_false
|
44
|
+
pete.boy!
|
45
|
+
pete.boy?.should be_true
|
46
|
+
|
47
|
+
janet = User.new "Janet"
|
48
|
+
janet.girl?.should be_false
|
49
|
+
janet.girl!
|
50
|
+
janet.girl?.should be_true
|
51
|
+
end
|
52
|
+
|
53
|
+
it "can use variables in instance methods" do
|
54
|
+
User.new("John").hello.should == "Hello Mr. John!"
|
55
|
+
User.new("Jane").hello.should == "Hello Ms. Jane!"
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Rubylog dsl", :rubylog=>true do
|
4
|
+
|
5
|
+
describe "prefix functors" do
|
6
|
+
before do
|
7
|
+
predicate_for singleton_class, ".we_have()"
|
8
|
+
we_have! :weapons
|
9
|
+
we_have! :sunglasses
|
10
|
+
we_have! :rustling_leather_coats
|
11
|
+
end
|
12
|
+
|
13
|
+
it "works" do
|
14
|
+
check we_have :sunglasses
|
15
|
+
end
|
16
|
+
|
17
|
+
it "works with qmarks" do
|
18
|
+
check { we_have? :rustling_leather_coats }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
describe "built-in prefix functors" do
|
24
|
+
check all X.is(4).and(Y.is(X)), Y.is(4)
|
25
|
+
check any X.is(4)
|
26
|
+
check one(X.in([1,2,3])) { X % 2 == 0 }
|
27
|
+
check none X.in([1,2,3]), X.is(5)
|
28
|
+
check { all?(X.is(4)) { X < 5 } }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "variables" do
|
32
|
+
check A.is(ANY).and{ A == Rubylog::Variable.new(:A) }
|
33
|
+
check A.is(4) .and{ A == 4 }
|
34
|
+
check B.is(A) .and{ A == Rubylog::Variable.new(:B) and B == Rubylog::Variable.new(:B) }
|
35
|
+
check B.is(ANY).and{ A == Rubylog::Variable.new(:A) }
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Recursion", :rubylog=>true do
|
4
|
+
predicate_for Integer, ".factorial()"
|
5
|
+
0.factorial! 1
|
6
|
+
N[thats > 0].factorial(K).if N1.is {N-1} .and N1.factorial(K1).and K.is{ N*K1 }
|
7
|
+
|
8
|
+
check 0.factorial 1
|
9
|
+
check 1.factorial 1
|
10
|
+
check 2.factorial 2
|
11
|
+
check 3.factorial 6
|
12
|
+
check 4.factorial 24
|
13
|
+
check 7.factorial 5040
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
extend Rubylog::Context
|
4
|
+
|
5
|
+
predicate_for Integer, %w(.divides())
|
6
|
+
|
7
|
+
(A[thats > 0].divides B[thats > 0]).if { B%A == 0 }
|
8
|
+
|
9
|
+
(N.is 210).solve do
|
10
|
+
|
11
|
+
(I.in [1, 2, 3, 5, 6, 7, 10, 14, 15, 21, 30, 35, 42, 70, 105, 210]).each do
|
12
|
+
check(I.divides N)
|
13
|
+
end
|
14
|
+
|
15
|
+
(I.in [4, 8, 9, 11, 12, 20, 31]).each do
|
16
|
+
check( (I.divides N).false )
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Rubylog", :rubylog => true do
|
4
|
+
|
5
|
+
predicate_for Integer, ".even"
|
6
|
+
|
7
|
+
N.even.if { N%2 == 0 }
|
8
|
+
|
9
|
+
it "checks for true statements" do
|
10
|
+
check :true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "raises an exception for false statements" do
|
14
|
+
proc{check :fail}.should raise_exception(Rubylog::CheckFailed)
|
15
|
+
|
16
|
+
self.default_subject = Integer
|
17
|
+
proc{check 4.is 5}.should raise_exception(Rubylog::CheckFailed)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "can use variables" do
|
21
|
+
check A.is 3
|
22
|
+
end
|
23
|
+
|
24
|
+
it "can use predicates" do
|
25
|
+
check((A.in [1,2]).and(B.in([2,3])).and(A.is 1).and(B.is 3))
|
26
|
+
end
|
27
|
+
|
28
|
+
it "can assert predicates" do
|
29
|
+
predicate_for Integer, ".divides()"
|
30
|
+
(A.divides B).if {B%A == 0}
|
31
|
+
check(4.divides 12)
|
32
|
+
check((4.divides 30).false)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "can use predicates defined outside" do
|
36
|
+
check 1.even.false
|
37
|
+
check 2.even
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "nested check" do
|
41
|
+
it "works" do
|
42
|
+
check 9.even.false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# inline check
|
47
|
+
check 0.even
|
48
|
+
|
49
|
+
describe "nested inline check" do
|
50
|
+
check 3.even.false
|
51
|
+
check 4.even
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "Rubylog" do
|
57
|
+
describe "when not included" do
|
58
|
+
|
59
|
+
it "does not add methods" do
|
60
|
+
proc{check :true}.should raise_exception NameError
|
61
|
+
proc{trace}.should raise_exception NameError
|
62
|
+
end
|
63
|
+
|
64
|
+
it "does not add constants" do
|
65
|
+
proc{A}.should raise_exception NameError
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
#require "rspec/core/formatters/progress_formatter"
|
74
|
+
#require "rspec/core/formatters/documentation_formatter"
|
75
|
+
#EG.run RSpec::Core::Reporter.new RSpec::Core::Formatters::DocumentationFormatter.new(STDOUT)
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "rules", :rubylog=>true do
|
4
|
+
before do
|
5
|
+
predicate_for Symbol, ".likes(Drink) .is_happy .has()"
|
6
|
+
predicate_for_context ".we_have()"
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "with prolog body" do
|
10
|
+
it "cannot be asserted in a builtin's desc" do
|
11
|
+
lambda {
|
12
|
+
:john.likes(:beer).and! :jane.likes(:milk)
|
13
|
+
}.should raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it "can be asserted with if" do
|
17
|
+
:john.is_happy.if we_have(:beer)
|
18
|
+
:john.is_happy?.should be_false
|
19
|
+
we_have!(:beer)
|
20
|
+
:john.is_happy?.should be_true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "can be asserted with unless" do
|
24
|
+
:john.is_happy.unless we_have(:problem)
|
25
|
+
:john.is_happy?.should be_true
|
26
|
+
we_have!(:problem)
|
27
|
+
:john.is_happy?.should be_false
|
28
|
+
end
|
29
|
+
|
30
|
+
it "can do simple general implications" do
|
31
|
+
X.is_happy.if X.likes(Y).and X.has(Y)
|
32
|
+
:john.likes! :milk
|
33
|
+
:john.is_happy?.should be_false
|
34
|
+
:john.has! :beer
|
35
|
+
:john.is_happy?.should be_false
|
36
|
+
:john.likes! :beer
|
37
|
+
:john.is_happy?.should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "can yield implied solutions" do
|
41
|
+
predicate_for Symbol, ".brother() .father() .uncle()"
|
42
|
+
X.brother(Y).if X.father(Z).and Y.father(Z).and {X != Y}
|
43
|
+
X.uncle(Y).if X.father(Z).and Z.brother(Y)
|
44
|
+
|
45
|
+
:john.father! :dad
|
46
|
+
:jack.father! :dad
|
47
|
+
:dad.father! :grandpa
|
48
|
+
:jim.father! :grandpa
|
49
|
+
|
50
|
+
(:john.brother X).map{X}.should == [:jack]
|
51
|
+
(:john.father X).map{X}.should == [:dad]
|
52
|
+
(X.father :dad).map{X}.should == [:john, :jack]
|
53
|
+
(ANY.father X).map{X}.should == [:dad, :dad, :grandpa, :grandpa]
|
54
|
+
(:john.uncle X).map{X}.should == [:jim]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "with ruby body" do
|
59
|
+
it "can be asserted (true)" do
|
60
|
+
:john.is_happy.if proc{ true }
|
61
|
+
:john.is_happy?.should be_true
|
62
|
+
end
|
63
|
+
|
64
|
+
it "can be asserted (false)" do
|
65
|
+
:john.is_happy.if proc{ false }
|
66
|
+
:john.is_happy?.should be_false
|
67
|
+
end
|
68
|
+
|
69
|
+
it "can be asserted implicitly (true)" do
|
70
|
+
:john.is_happy.if { true }
|
71
|
+
:john.is_happy?.should be_true
|
72
|
+
end
|
73
|
+
|
74
|
+
it "can be asserted implicitly (false)" do
|
75
|
+
:john.is_happy.if { false }
|
76
|
+
:john.is_happy?.should be_false
|
77
|
+
end
|
78
|
+
|
79
|
+
it "run the body during every query" do
|
80
|
+
count = 0
|
81
|
+
:john.is_happy.if proc{ count += 1 }
|
82
|
+
count.should == 0
|
83
|
+
:john.is_happy?
|
84
|
+
count.should == 1
|
85
|
+
:john.is_happy?
|
86
|
+
count.should == 2
|
87
|
+
end
|
88
|
+
|
89
|
+
it "can take arguments" do
|
90
|
+
predicate_for Integer, ".divides()"
|
91
|
+
(A.divides B).if proc{B % A == 0}
|
92
|
+
(4.divides? 16).should be_true
|
93
|
+
(4.divides? 17).should be_false
|
94
|
+
(4.divides? 18).should be_false
|
95
|
+
(3.divides? 3).should be_true
|
96
|
+
(3.divides? 4).should be_false
|
97
|
+
(3.divides? 5).should be_false
|
98
|
+
(3.divides? 6).should be_true
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "#if!" do
|
103
|
+
it "can be used to cut off branches" do
|
104
|
+
predicate_for Integer, ".divides()"
|
105
|
+
1.divides(X).if! :true
|
106
|
+
(A.divides B).if proc{B % A == 0}
|
107
|
+
1.divides(4).map{}.should == [nil]
|
108
|
+
2.divides(4).map{}.should == [nil]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Arithmetics builtins", :rubylog=>true do
|
4
|
+
check 5.sum_of 2, 3
|
5
|
+
|
6
|
+
check((5.sum_of 2, 6).false)
|
7
|
+
|
8
|
+
check { 5.sum_of(3, A).map{A}.eql? [2] }
|
9
|
+
check { 5.sum_of(A, 2).map{A}.eql? [3] }
|
10
|
+
check { A.sum_of(3, 4).map{A}.eql? [7] }
|
11
|
+
|
12
|
+
check A.is(5).and B.is(3).and C.is(2).and A.sum_of(C, B)
|
13
|
+
|
14
|
+
it "works with functions" do
|
15
|
+
check A.is(5).and 5.sum_of(proc{2}, proc{A-2})
|
16
|
+
check A.is(5).and 5.sum_of(proc{3}, proc{A-2}).false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "checks types" do
|
20
|
+
check 4.sum_of(2,2.0).false
|
21
|
+
check 4.0.sum_of(2.0,2.0)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "can add strings" do
|
25
|
+
A.sum_of(B,C).predicate.add_functor_to String
|
26
|
+
check "hello".sum_of("he", "llo")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can add arrays" do
|
30
|
+
A.sum_of(B,C).predicate.add_functor_to Array
|
31
|
+
check [1,2,3].sum_of([1,2], [3])
|
32
|
+
end
|
33
|
+
|
34
|
+
it "can subtract arrays" do
|
35
|
+
A.sum_of(B,C).predicate.add_functor_to Array
|
36
|
+
[1,2,3].sum_of(X, [3]).map{X}.should be_eql [[1,2]]
|
37
|
+
end
|
38
|
+
|
39
|
+
it "can multiply integers" do
|
40
|
+
A.product_of(3,2).map{A}.should be_eql [6]
|
41
|
+
end
|
42
|
+
|
43
|
+
it "can divide integers" do
|
44
|
+
6.product_of(A,2).map{A}.should be_eql [3]
|
45
|
+
end
|
46
|
+
|
47
|
+
it "can divide integers" do
|
48
|
+
6.product_of(3,A).map{A}.should be_eql [2]
|
49
|
+
end
|
50
|
+
|
51
|
+
it "can divide floats" do
|
52
|
+
6.0.product_of(A,2.0).map{A}.should be_eql [3.0]
|
53
|
+
end
|
54
|
+
|
55
|
+
it "can divide floats" do
|
56
|
+
6.0.product_of(3.0,A).map{A}.should be_eql [2.0]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "checks non-divisibility of integers" do
|
60
|
+
6.product_of(5,A).map{A}.should be_eql []
|
61
|
+
end
|
62
|
+
|
63
|
+
it "checks non-divisibility of integers" do
|
64
|
+
6.product_of(A,4).map{A}.should be_eql []
|
65
|
+
end
|
66
|
+
|
67
|
+
it "casts types" do
|
68
|
+
6.0.product_of(A,2).map{A}.should be_eql [3.0]
|
69
|
+
end
|
70
|
+
|
71
|
+
it "casts types" do
|
72
|
+
6.0.product_of(3,A).map{A}.should be_eql [2.0]
|
73
|
+
end
|
74
|
+
|
75
|
+
it "casts types" do
|
76
|
+
A.product_of(3,2.0).map{A}.should be_eql [6.0]
|
77
|
+
end
|
78
|
+
|
79
|
+
it "checks instatiation" do
|
80
|
+
expect { 8.product_of(B,C).map{[B,C]} }.to raise_error Rubylog::InstantiationError
|
81
|
+
end
|
82
|
+
|
83
|
+
it "checks instatiation" do
|
84
|
+
expect { 8.sum_of(B,C).map{[B,C]} }.to raise_error Rubylog::InstantiationError
|
85
|
+
end
|
86
|
+
|
87
|
+
it "checks instatiation" do
|
88
|
+
expect { A.sum_of(B,2).map{[B,C]} }.to raise_error Rubylog::InstantiationError
|
89
|
+
end
|
90
|
+
|
91
|
+
it "checks instatiation" do
|
92
|
+
expect { A.product_of(B,2).map{[B,C]} }.to raise_error Rubylog::InstantiationError
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Assumption builtins", :rubylog=>true do
|
4
|
+
before do
|
5
|
+
predicate_for Integer, ".divides() .perfect"
|
6
|
+
A.divides(B).if { B%A == 0 }
|
7
|
+
N.perfect.if { (A.in{1...N}.and A.divides(N)).map{A}.inject{|a,b|a+b} == N }
|
8
|
+
end
|
9
|
+
|
10
|
+
specify do
|
11
|
+
check 4.perfect.false
|
12
|
+
check 5.perfect.false
|
13
|
+
check 6.perfect
|
14
|
+
check 7.perfect.false
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "assumed" do
|
18
|
+
specify do
|
19
|
+
check 4.divides(6).assumed.and 6.perfect.false
|
20
|
+
check 2.divides(3).assumed.and 3.perfect
|
21
|
+
check 4.perfect.assumed.and 4.perfect
|
22
|
+
check ANY.perfect.assumed.and 4.perfect
|
23
|
+
check ANY.perfect.assumed.and 17.perfect
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "rejected" do
|
28
|
+
specify do
|
29
|
+
check 4.divides(6).rejected.and 6.perfect
|
30
|
+
check 3.divides(6).rejected.and 6.perfect.false
|
31
|
+
check 2.divides(20).rejected.and 20.perfect
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "revoked" do
|
36
|
+
before do
|
37
|
+
predicate_for String, ".likes()"
|
38
|
+
'John'.likes! 'milk'
|
39
|
+
'John'.likes! 'beer'
|
40
|
+
end
|
41
|
+
|
42
|
+
specify { 'John'.likes(A).map{A}.should == ['milk','beer'] }
|
43
|
+
specify {
|
44
|
+
'John'.likes('milk').revoked.and('John'.likes(A)) \
|
45
|
+
.map{A}.should == ['beer'] }
|
46
|
+
specify {
|
47
|
+
'John'.likes('beer').revoked.and('John'.likes(A)) \
|
48
|
+
.map{A}.should == ['milk'] }
|
49
|
+
specify {
|
50
|
+
'John'.likes('water').revoked.true?.should be_false }
|
51
|
+
specify {
|
52
|
+
'John'.likes(X).revoked.map{X}.should == ['milk','beer'] }
|
53
|
+
specify { 'John'.likes(A).map{A}.should == ['milk','beer'] }
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "assumed unless" do
|
57
|
+
xspecify
|
58
|
+
end
|
59
|
+
describe "rejected" do
|
60
|
+
xspecify
|
61
|
+
end
|
62
|
+
describe "rejected_if" do
|
63
|
+
xspecify
|
64
|
+
end
|
65
|
+
describe "rejected_unless" do
|
66
|
+
xspecify
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "file system builtins", :rubylog=>true do
|
4
|
+
specify "directories are files" do
|
5
|
+
check "./lib".file_in(".")
|
6
|
+
end
|
7
|
+
|
8
|
+
specify ": regular files are files" do
|
9
|
+
check "./README.rdoc".file_in(".")
|
10
|
+
end
|
11
|
+
|
12
|
+
specify ": regular files are not directories" do
|
13
|
+
check "./README.rdoc".dir_in(".").false
|
14
|
+
end
|
15
|
+
|
16
|
+
specify "#filename_in" do
|
17
|
+
check "rubylog.rb".filename_in("lib")
|
18
|
+
end
|
19
|
+
|
20
|
+
specify "#dirname_in" do
|
21
|
+
check "rubylog".dirname_in("lib")
|
22
|
+
end
|
23
|
+
|
24
|
+
specify ": works with variable filename" do
|
25
|
+
A.dir_in(".").map{A}.should include "./lib"
|
26
|
+
end
|
27
|
+
|
28
|
+
specify ": works with variable dir" do
|
29
|
+
"./lib/rubylog.rb".file_in(D).map{D}.should == ["./lib"]
|
30
|
+
end
|
31
|
+
|
32
|
+
specify ": works with function dir" do
|
33
|
+
FN.file_in{"./lib"}.map{FN}.should include "./lib/rubylog.rb"
|
34
|
+
end
|
35
|
+
|
36
|
+
specify ": works with partial strings" do
|
37
|
+
"./lib/#{A}.rb".file_in("./lib").map{A}.should == ["rubylog"]
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|