rubylog 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/Gemfile +2 -0
  2. data/Gemfile.lock +10 -0
  3. data/README.rdoc +1 -1
  4. data/RELEASE_NOTES.rdoc +7 -4
  5. data/VERSION +1 -1
  6. data/{examples → benchmark}/benchmark.rb +1 -0
  7. data/{examples → benchmark}/benchmark/compiled_not_indexed.rb +0 -0
  8. data/{examples → benchmark}/benchmark/compiled_sequence_indexed.rb +0 -0
  9. data/{examples → benchmark}/benchmark/indexed_procedure.rb +0 -0
  10. data/{examples → benchmark}/benchmark/prolog.rb +0 -0
  11. data/benchmark/benchmark/pure.rb +28 -0
  12. data/bin/rubylog +14 -0
  13. data/examples/a_plus_b.rb +2 -2
  14. data/examples/dcg.rb +22 -26
  15. data/examples/dcg2.rb +25 -30
  16. data/examples/divisors.rb +1 -3
  17. data/examples/factorial.rb +8 -15
  18. data/examples/file_search.rb +14 -13
  19. data/examples/hanoi.rb +1 -3
  20. data/examples/hu/csaladfa.rb +0 -4
  21. data/examples/n_queens.rb +17 -22
  22. data/examples/palindrome_detection.rb +1 -2
  23. data/examples/parsing.rb +19 -23
  24. data/examples/permutation.rb +1 -3
  25. data/examples/primality_by_division.rb +2 -2
  26. data/examples/sieve_of_eratosthenes.rb +2 -2
  27. data/examples/string_interpolation.rb +0 -3
  28. data/examples/tracing.rb +0 -4
  29. data/lib/rubylog/builtins/assumption.rb +2 -1
  30. data/lib/rubylog/builtins/file_system.rb +1 -1
  31. data/lib/rubylog/default_context.rb +3 -5
  32. data/lib/rubylog/mixins/kernel.rb +9 -1
  33. data/lib/rubylog/rubylog_files.rb +7 -0
  34. data/rubylog.gemspec +17 -22
  35. data/spec/inriasuite_spec.rb +851 -847
  36. data/spec/integration/dsl_spec.rb +32 -29
  37. data/spec/rspec/rubylog_spec.rb +46 -52
  38. data/spec/rubylog/assertable_spec.rb +92 -90
  39. data/spec/rubylog/builtins/arithmetics_spec.rb +92 -90
  40. data/spec/rubylog/builtins/assumption_spec.rb +59 -57
  41. data/spec/rubylog/builtins/ensure_spec.rb +6 -4
  42. data/spec/rubylog/builtins/file_system_spec.rb +41 -39
  43. data/spec/rubylog/builtins/logic_spec.rb +308 -306
  44. data/spec/rubylog/builtins/reflection_spec.rb +31 -29
  45. data/spec/rubylog/builtins/term_spec.rb +62 -60
  46. data/spec/rubylog/context_modules/demonstration_spec.rb +108 -106
  47. data/spec/rubylog/context_modules/predicates_spec.rb +29 -27
  48. data/spec/rubylog/context_modules/thats_spec.rb +77 -75
  49. data/spec/rubylog/dsl/array_splat_spec.rb +11 -9
  50. data/spec/rubylog/dsl/indicators_spec.rb +23 -21
  51. data/spec/rubylog/dsl/primitives_spec.rb +30 -28
  52. data/spec/rubylog/errors_spec.rb +13 -11
  53. data/spec/rubylog/interfaces/term_spec.rb +78 -76
  54. data/spec/rubylog/mixins/array_spec.rb +60 -58
  55. data/spec/rubylog/mixins/composite_term_spec.rb +55 -53
  56. data/spec/rubylog/mixins/proc_spec.rb +48 -46
  57. data/spec/rubylog/mixins/string_spec.rb +45 -43
  58. data/spec/rubylog/mixins/symbol_spec.rb +7 -5
  59. data/spec/rubylog/procedure_spec.rb +8 -6
  60. data/spec/rubylog/rule_spec.rb +10 -8
  61. data/spec/rubylog/structure_spec.rb +73 -71
  62. data/spec/rubylog/term_spec.rb +5 -3
  63. data/spec/rubylog/tracing_spec.rb +35 -33
  64. data/spec/rubylog/variable_spec.rb +249 -247
  65. data/spec/spec_helper.rb +4 -0
  66. metadata +54 -43
  67. data/examples/benchmark/pure.rb +0 -26
  68. data/examples/checkmate.rb +0 -88
  69. data/examples/combination.rb +0 -17
  70. data/examples/directory_structure_logic.rb +0 -17
  71. data/examples/dirlist.rb +0 -4
  72. data/examples/enumerators.rb +0 -30
  73. data/examples/hello.rb +0 -17
  74. data/examples/mice.rb +0 -92
  75. data/examples/mice2.rb +0 -37
  76. data/examples/object_oriented.rb +0 -14
  77. data/examples/prefix.rb +0 -13
  78. data/examples/primitives.rb +0 -26
  79. data/examples/sudoku.rb +0 -17
  80. data/spec/integration/theory_as_module_spec.rb +0 -20
  81. data/spec/integration/theory_as_module_with_include_spec.rb +0 -14
@@ -1,70 +1,72 @@
1
1
  require "spec_helper"
2
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
3
+ module RubylogSpec
4
+ describe "Assumption builtins", :rubylog=>true do
5
+ before do
6
+ predicate_for Integer, ".divides() .perfect"
7
+ A.divides(B).if { B%A == 0 }
8
+ N.perfect.if { (A.in{1...N}.and A.divides(N)).map{A}.inject{|a,b|a+b} == N }
9
+ end
16
10
 
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
11
+ specify do
12
+ check 4.perfect.false
13
+ check 5.perfect.false
14
+ check 6.perfect
15
+ check 7.perfect.false
24
16
  end
25
- end
26
17
 
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
18
+ describe "assumed" do
19
+ specify do
20
+ check 4.divides(6).assumed.and 6.perfect.false
21
+ check 2.divides(3).assumed.and 3.perfect
22
+ check 4.perfect.assumed.and 4.perfect
23
+ check ANY.perfect.assumed.and 4.perfect
24
+ check ANY.perfect.assumed.and 17.perfect
25
+ end
32
26
  end
33
- end
34
27
 
35
- describe "revoked" do
36
- before do
37
- predicate_for String, ".likes()"
38
- 'John'.likes! 'milk'
39
- 'John'.likes! 'beer'
28
+ describe "rejected" do
29
+ specify do
30
+ check 4.divides(6).rejected.and 6.perfect
31
+ check 3.divides(6).rejected.and 6.perfect.false
32
+ check 2.divides(20).rejected.and 20.perfect
33
+ end
40
34
  end
41
35
 
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
36
+ describe "revoked" do
37
+ before do
38
+ predicate_for String, ".likes()"
39
+ 'John'.likes! 'milk'
40
+ 'John'.likes! 'beer'
41
+ end
55
42
 
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
43
+ specify { 'John'.likes(A).map{A}.should == ['milk','beer'] }
44
+ specify {
45
+ 'John'.likes('milk').revoked.and('John'.likes(A)) \
46
+ .map{A}.should == ['beer'] }
47
+ specify {
48
+ 'John'.likes('beer').revoked.and('John'.likes(A)) \
49
+ .map{A}.should == ['milk'] }
50
+ specify {
51
+ 'John'.likes('water').revoked.true?.should be_false }
52
+ specify {
53
+ 'John'.likes(X).revoked.map{X}.should == ['milk','beer'] }
54
+ specify { 'John'.likes(A).map{A}.should == ['milk','beer'] }
55
+ end
56
+
57
+ describe "assumed unless" do
58
+ xspecify
59
+ end
60
+ describe "rejected" do
61
+ xspecify
62
+ end
63
+ describe "rejected_if" do
64
+ xspecify
65
+ end
66
+ describe "rejected_unless" do
67
+ xspecify
68
+ end
67
69
  end
68
- end
69
70
 
70
71
 
72
+ end
@@ -1,8 +1,10 @@
1
1
  require "spec_helper"
2
2
 
3
- describe "ensure builtin", :rubylog=>true do
4
- k = 6
5
- check proc{k=5}.ensure{k=6}.and{k.eql? 5}
6
- check {k.eql? 6}
3
+ module RubylogSpec
4
+ describe "ensure builtin", :rubylog=>true do
5
+ k = 6
6
+ check proc{k=5}.ensure{k=6}.and{k.eql? 5}
7
+ check {k.eql? 6}
8
+ end
7
9
  end
8
10
 
@@ -1,43 +1,45 @@
1
1
  require "spec_helper"
2
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
- specify "works with nonvars" do
41
- "./lib/rubylog.rb".file_in("./lib").to_a.should == [nil]
3
+ module RubylogSpec
4
+ describe "file system builtins", :rubylog=>true do
5
+ specify "directories are files" do
6
+ check "./lib".file_in(".")
7
+ end
8
+
9
+ specify ": regular files are files" do
10
+ check "./README.rdoc".file_in(".")
11
+ end
12
+
13
+ specify ": regular files are not directories" do
14
+ check "./README.rdoc".dir_in(".").false
15
+ end
16
+
17
+ specify "#filename_in" do
18
+ check "rubylog.rb".filename_in("lib")
19
+ end
20
+
21
+ specify "#dirname_in" do
22
+ check "rubylog".dirname_in("lib")
23
+ end
24
+
25
+ specify ": works with variable filename" do
26
+ A.dir_in(".").map{A}.should include "./lib"
27
+ end
28
+
29
+ specify ": works with variable dir" do
30
+ "./lib/rubylog.rb".file_in(D).map{D}.should == ["./lib"]
31
+ end
32
+
33
+ specify ": works with function dir" do
34
+ FN.file_in{"./lib"}.map{FN}.should include "./lib/rubylog.rb"
35
+ end
36
+
37
+ specify ": works with partial strings" do
38
+ "./lib/#{A}.rb".file_in("./lib").map{A}.should == ["rubylog"]
39
+ end
40
+
41
+ specify "works with nonvars" do
42
+ "./lib/rubylog.rb".file_in("./lib").to_a.should == [nil]
43
+ end
42
44
  end
43
45
  end
@@ -1,382 +1,384 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "logic builtins", :rubylog => true do
4
- before do
5
- predicate_for Symbol, ".likes() .happy"
6
- end
7
-
8
- specify "true" do
9
- :john.happy.if :true
10
- :john.should be_happy
11
- end
12
-
13
- specify "fail" do
14
- :john.happy.if :fail
15
- :john.should_not be_happy
16
- end
17
-
18
- describe "false" do
19
- specify do
20
- :john.happy.if :true.false
21
- :john.should_not be_happy
3
+ module RubylogSpec
4
+ describe "logic builtins", :rubylog => true do
5
+ before do
6
+ predicate_for Symbol, ".likes() .happy"
22
7
  end
23
8
 
24
- specify do
25
- :john.happy.if :fail.false
9
+ specify "true" do
10
+ :john.happy.if :true
26
11
  :john.should be_happy
27
12
  end
28
- end
29
13
 
30
- describe "and" do
31
- it "works 1" do
32
- :john.happy.if :fail.and :true
14
+ specify "fail" do
15
+ :john.happy.if :fail
33
16
  :john.should_not be_happy
34
17
  end
35
18
 
36
- it "works 2" do
37
- :john.happy.if :true.and :fail
38
- :john.should_not be_happy
39
- end
19
+ describe "false" do
20
+ specify do
21
+ :john.happy.if :true.false
22
+ :john.should_not be_happy
23
+ end
40
24
 
41
- it "works 3" do
42
- :john.happy.if :fail.and :fail
43
- :john.should_not be_happy
25
+ specify do
26
+ :john.happy.if :fail.false
27
+ :john.should be_happy
28
+ end
44
29
  end
45
30
 
46
- it "works 4" do
47
- :john.happy.if :true.and :true
48
- :john.should be_happy
49
- end
50
- end
31
+ describe "and" do
32
+ it "works 1" do
33
+ :john.happy.if :fail.and :true
34
+ :john.should_not be_happy
35
+ end
51
36
 
37
+ it "works 2" do
38
+ :john.happy.if :true.and :fail
39
+ :john.should_not be_happy
40
+ end
52
41
 
53
- describe "or" do
54
- it "works 1" do
55
- :john.happy.if :fail.or :true
56
- :john.should be_happy
57
- end
42
+ it "works 3" do
43
+ :john.happy.if :fail.and :fail
44
+ :john.should_not be_happy
45
+ end
58
46
 
59
- it "works 2" do
60
- :john.happy.if :true.or :fail
61
- :john.should be_happy
47
+ it "works 4" do
48
+ :john.happy.if :true.and :true
49
+ :john.should be_happy
50
+ end
62
51
  end
63
52
 
64
- it "works 3" do
65
- :john.happy.if :fail.or :fail
66
- :john.should_not be_happy
67
- end
68
53
 
69
- it "works 4" do
70
- :john.happy.if :true.or :true
71
- :john.should be_happy
72
- end
73
- end
54
+ describe "or" do
55
+ it "works 1" do
56
+ :john.happy.if :fail.or :true
57
+ :john.should be_happy
58
+ end
74
59
 
60
+ it "works 2" do
61
+ :john.happy.if :true.or :fail
62
+ :john.should be_happy
63
+ end
75
64
 
76
- describe "branch or" do
77
- it "works 1" do
78
- :john.happy.if :fail
79
- :john.happy.if :true
80
- :john.should be_happy
81
- end
65
+ it "works 3" do
66
+ :john.happy.if :fail.or :fail
67
+ :john.should_not be_happy
68
+ end
82
69
 
83
- it "works 2" do
84
- :john.happy.if :true
85
- :john.happy.if :fail
86
- :john.should be_happy
70
+ it "works 4" do
71
+ :john.happy.if :true.or :true
72
+ :john.should be_happy
73
+ end
87
74
  end
88
75
 
89
- it "works 3" do
90
- :john.happy.if :fail
91
- :john.happy.if :fail
92
- :john.should_not be_happy
93
- end
94
76
 
95
- it "works 4" do
96
- :john.happy.if :true
97
- :john.happy.if :true
98
- :john.should be_happy
99
- end
100
- end
77
+ describe "branch or" do
78
+ it "works 1" do
79
+ :john.happy.if :fail
80
+ :john.happy.if :true
81
+ :john.should be_happy
82
+ end
101
83
 
102
- describe "iff" do
103
- it "works 1" do
104
- :john.happy.if :fail.iff :true
105
- :john.should_not be_happy
106
- end
84
+ it "works 2" do
85
+ :john.happy.if :true
86
+ :john.happy.if :fail
87
+ :john.should be_happy
88
+ end
107
89
 
108
- it "works 2" do
109
- :john.happy.if :true.iff :fail
110
- :john.should_not be_happy
111
- end
90
+ it "works 3" do
91
+ :john.happy.if :fail
92
+ :john.happy.if :fail
93
+ :john.should_not be_happy
94
+ end
112
95
 
113
- it "works 3" do
114
- :john.happy.if :fail.iff :fail
115
- :john.should be_happy
96
+ it "works 4" do
97
+ :john.happy.if :true
98
+ :john.happy.if :true
99
+ :john.should be_happy
100
+ end
116
101
  end
117
102
 
118
- it "works 4" do
119
- :john.happy.if :true.iff :true
120
- :john.should be_happy
121
- end
122
- end
103
+ describe "iff" do
104
+ it "works 1" do
105
+ :john.happy.if :fail.iff :true
106
+ :john.should_not be_happy
107
+ end
123
108
 
124
- describe "cut" do
125
- it "works with branch or" do
126
- :john.happy.if :true.and :cut!.and :fail
127
- :john.happy.if :true
128
- :john.should_not be_happy
129
- end
130
- it "works with branch or (control)" do
131
- :john.happy.if :true.and :fail
132
- :john.happy.if :true
133
- :john.should be_happy
134
- end
109
+ it "works 2" do
110
+ :john.happy.if :true.iff :fail
111
+ :john.should_not be_happy
112
+ end
135
113
 
136
- it "works with or" do
137
- :john.happy.if((:true.and :cut!.and :fail).or :true)
138
- :john.should_not be_happy
139
- end
114
+ it "works 3" do
115
+ :john.happy.if :fail.iff :fail
116
+ :john.should be_happy
117
+ end
140
118
 
141
- it "works with or (control)" do
142
- :john.happy.if((:true.and :fail).or :true)
143
- :john.should be_happy
119
+ it "works 4" do
120
+ :john.happy.if :true.iff :true
121
+ :john.should be_happy
122
+ end
144
123
  end
145
124
 
146
- it "returns true with branch or" do
147
- :john.happy.if :true.and :cut!.and :true
148
- :john.happy.if :true
149
- :john.should be_happy
150
- end
151
- it "returns true with branch or (control)" do
152
- :john.happy.if :true.and :true
153
- :john.happy.if :true
154
- :john.should be_happy
155
- end
125
+ describe "cut" do
126
+ it "works with branch or" do
127
+ :john.happy.if :true.and :cut!.and :fail
128
+ :john.happy.if :true
129
+ :john.should_not be_happy
130
+ end
131
+ it "works with branch or (control)" do
132
+ :john.happy.if :true.and :fail
133
+ :john.happy.if :true
134
+ :john.should be_happy
135
+ end
156
136
 
157
- it "returns true with or" do
158
- :john.happy.if((:true.and :cut!.and :true).or :true)
159
- :john.should be_happy
160
- end
137
+ it "works with or" do
138
+ :john.happy.if((:true.and :cut!.and :fail).or :true)
139
+ :john.should_not be_happy
140
+ end
161
141
 
162
- it "returns true with or (control)" do
163
- :john.happy.if((:true.and :true).or :true)
164
- :john.should be_happy
165
- end
142
+ it "works with or (control)" do
143
+ :john.happy.if((:true.and :fail).or :true)
144
+ :john.should be_happy
145
+ end
166
146
 
167
- it "works after variable calls" do
168
- (A.is(:true).and A.and :cut!.and :fail).or(:true).true?.should be_false
169
- end
147
+ it "returns true with branch or" do
148
+ :john.happy.if :true.and :cut!.and :true
149
+ :john.happy.if :true
150
+ :john.should be_happy
151
+ end
152
+ it "returns true with branch or (control)" do
153
+ :john.happy.if :true.and :true
154
+ :john.happy.if :true
155
+ :john.should be_happy
156
+ end
170
157
 
171
- it "works after variable calls (control)" do
172
- (A.is(:true).and A.and :fail).or(:true).true?.should be_true
173
- end
158
+ it "returns true with or" do
159
+ :john.happy.if((:true.and :cut!.and :true).or :true)
160
+ :john.should be_happy
161
+ end
174
162
 
175
- it "works after variable calls (branch or)" do
176
- :john.happy.if A.is(:true).and A.and :cut!.and :fail
177
- :john.happy.if :true
178
- :john.should_not be_happy
179
- end
163
+ it "returns true with or (control)" do
164
+ :john.happy.if((:true.and :true).or :true)
165
+ :john.should be_happy
166
+ end
180
167
 
181
- it "works after variable calls with multiple solutions of the variable" do
182
- (A.is(B.is(4).or(B.is(6))).and A.and :cut!).map{B}.should == [4]
183
- end
168
+ it "works after variable calls" do
169
+ (A.is(:true).and A.and :cut!.and :fail).or(:true).true?.should be_false
170
+ end
184
171
 
185
- it "works after variable calls with multiple solutions of the variable (control)" do
186
- (A.is(B.is(4).or(B.is(6))).and A).map{B}.should == [4,6]
187
- end
172
+ it "works after variable calls (control)" do
173
+ (A.is(:true).and A.and :fail).or(:true).true?.should be_true
174
+ end
188
175
 
189
- end
176
+ it "works after variable calls (branch or)" do
177
+ :john.happy.if A.is(:true).and A.and :cut!.and :fail
178
+ :john.happy.if :true
179
+ :john.should_not be_happy
180
+ end
190
181
 
191
- describe "all,any,one,none,every" do
192
- before do
193
- :john.likes! :water
194
- :john.likes! :beer
195
-
196
- :jane.likes! :water
197
- :jane.likes! :milk
198
- :jane.likes! :beer
199
-
200
- :jeff.likes! :water
201
- :jeff.likes! :absinth
202
-
203
- :todd.likes! :milk
204
-
205
- @predicates = [:all, :any, :one, :none]
206
- @names = :john, :jane, :jeff, :todd
207
- @good =
208
- [
209
- [[1,1,0,0], [0,1,0,0], [0,0,1,0], [0,1,0,1]], # all
210
- [[1,1,1,0], [1,1,1,1], [1,1,1,0], [0,1,0,1]], # any
211
- [[0,0,1,0], [0,0,1,1], [1,1,0,0], [0,1,0,1]], # one
212
- [[0,0,0,1], [0,0,0,0], [0,0,0,1], [1,0,1,0]] # none
213
- ]
214
- end
182
+ it "works after variable calls with multiple solutions of the variable" do
183
+ (A.is(B.is(4).or(B.is(6))).and A.and :cut!).map{B}.should == [4]
184
+ end
215
185
 
216
- it "work" do
217
- @predicates.map{|p| @names.map{|n| @names.map{|m|
218
- (n.likes(K).send p, m.likes(K)).true? ? 1 : 0
219
- }}}.should == @good
220
- end
186
+ it "works after variable calls with multiple solutions of the variable (control)" do
187
+ (A.is(B.is(4).or(B.is(6))).and A).map{B}.should == [4,6]
188
+ end
221
189
 
222
- it "mimic well enumerators' predicates" do
223
- @predicates.map{|p| @names.map{|n| @names.map{|m|
224
- n.likes(K).map{K}.send(:"#{p}?"){|x| m.likes?(x) } ? 1 : 0
225
- }}}.should == @good
226
190
  end
227
191
 
192
+ describe "all,any,one,none,every" do
193
+ before do
194
+ :john.likes! :water
195
+ :john.likes! :beer
228
196
 
197
+ :jane.likes! :water
198
+ :jane.likes! :milk
199
+ :jane.likes! :beer
229
200
 
230
- specify "all works" do
231
- (:john.likes(X).all(:john.likes(X))).true?.should be_true
232
- (:john.likes(X).all(:jane.likes(X))).true?.should be_true
233
- (:john.likes(X).all(:jeff.likes(X))).true?.should_not be_true
234
- (:john.likes(X).all(:todd.likes(X))).true?.should_not be_true
235
-
236
- (:jane.likes(X).all(:john.likes(X))).true?.should_not be_true
237
- (:jane.likes(X).all(:jane.likes(X))).true?.should be_true
238
- (:jane.likes(X).all(:jeff.likes(X))).true?.should_not be_true
239
- (:jane.likes(X).all(:todd.likes(X))).true?.should_not be_true
201
+ :jeff.likes! :water
202
+ :jeff.likes! :absinth
240
203
 
241
- (:jeff.likes(X).all(:john.likes(X))).true?.should_not be_true
242
- (:jeff.likes(X).all(:jane.likes(X))).true?.should_not be_true
243
- (:jeff.likes(X).all(:jeff.likes(X))).true?.should be_true
244
- (:jeff.likes(X).all(:todd.likes(X))).true?.should_not be_true
204
+ :todd.likes! :milk
245
205
 
246
- (:todd.likes(X).all(:john.likes(X))).true?.should_not be_true
247
- (:todd.likes(X).all(:jane.likes(X))).true?.should be_true
248
- (:todd.likes(X).all(:jeff.likes(X))).true?.should_not be_true
249
- (:todd.likes(X).all(:todd.likes(X))).true?.should be_true
250
- end
206
+ @predicates = [:all, :any, :one, :none]
207
+ @names = :john, :jane, :jeff, :todd
208
+ @good =
209
+ [
210
+ [[1,1,0,0], [0,1,0,0], [0,0,1,0], [0,1,0,1]], # all
211
+ [[1,1,1,0], [1,1,1,1], [1,1,1,0], [0,1,0,1]], # any
212
+ [[0,0,1,0], [0,0,1,1], [1,1,0,0], [0,1,0,1]], # one
213
+ [[0,0,0,1], [0,0,0,0], [0,0,0,1], [1,0,1,0]] # none
214
+ ]
215
+ end
251
216
 
252
- specify "all works with procs" do
253
- (:john.likes(X).all{:john.likes?(X)}).true?.should be_true
254
- (:john.likes(X).all{:jane.likes?(X)}).true?.should be_true
255
- (:john.likes(X).all{:jeff.likes?(X)}).true?.should_not be_true
256
- (:john.likes(X).all{:todd.likes?(X)}).true?.should_not be_true
257
-
258
- (:jane.likes(X).all{:john.likes?(X)}).true?.should_not be_true
259
- (:jane.likes(X).all{:jane.likes?(X)}).true?.should be_true
260
- (:jane.likes(X).all{:jeff.likes?(X)}).true?.should_not be_true
261
- (:jane.likes(X).all{:todd.likes?(X)}).true?.should_not be_true
262
-
263
- (:jeff.likes(X).all{:john.likes?(X)}).true?.should_not be_true
264
- (:jeff.likes(X).all{:jane.likes?(X)}).true?.should_not be_true
265
- (:jeff.likes(X).all{:jeff.likes?(X)}).true?.should be_true
266
- (:jeff.likes(X).all{:todd.likes?(X)}).true?.should_not be_true
267
-
268
- (:todd.likes(X).all{:john.likes?(X)}).true?.should_not be_true
269
- (:todd.likes(X).all{:jane.likes?(X)}).true?.should be_true
270
- (:todd.likes(X).all{:jeff.likes?(X)}).true?.should_not be_true
271
- (:todd.likes(X).all{:todd.likes?(X)}).true?.should be_true
272
- end
217
+ it "work" do
218
+ @predicates.map{|p| @names.map{|n| @names.map{|m|
219
+ (n.likes(K).send p, m.likes(K)).true? ? 1 : 0
220
+ }}}.should == @good
221
+ end
273
222
 
274
- it "can be called with global functor syntax" do
275
- all(:john.likes(X), :jane.likes(X)).true?.should be_true
276
- all(:jane.likes(X), :john.likes(X)).true?.should_not be_true
277
- any(:jane.likes(X), :todd.likes(X)).true?.should be_true
278
- any(:john.likes(X), :todd.likes(X)).true?.should_not be_true
279
- end
223
+ it "mimic well enumerators' predicates" do
224
+ @predicates.map{|p| @names.map{|n| @names.map{|m|
225
+ n.likes(K).map{K}.send(:"#{p}?"){|x| m.likes?(x) } ? 1 : 0
226
+ }}}.should == @good
227
+ end
280
228
 
281
- it "one, any, none can be called unarily" do
282
- one(:john.likes(X)).true?.should_not be_true
283
- one(:jane.likes(X)).true?.should_not be_true
284
- one(:jeff.likes(X)).true?.should_not be_true
285
- one(:todd.likes(X)).true?.should be_true
286
- one(:jim.likes(X)).true?.should_not be_true
287
-
288
- any(:john.likes(X)).true?.should be_true
289
- any(:jane.likes(X)).true?.should be_true
290
- any(:jeff.likes(X)).true?.should be_true
291
- any(:todd.likes(X)).true?.should be_true
292
- any(:jim.likes(X)).true?.should_not be_true
293
-
294
- none(:john.likes(X)).true?.should_not be_true
295
- none(:jane.likes(X)).true?.should_not be_true
296
- none(:jeff.likes(X)).true?.should_not be_true
297
- none(:todd.likes(X)).true?.should_not be_true
298
- none(:jim.likes(X)).true?.should be_true
299
- end
300
229
 
301
- it "does not hijack variables" do
302
- A.is(X.is(5)).and(A.all{X<10}).true?.should == true
303
- end
304
230
 
305
- describe "every" do
306
- specify "works as infix" do
307
- :john.likes(X).every(:john.likes(X)).true?.should be_true
308
- :john.likes(X).every(:jane.likes(X)).true?.should be_true
309
- :john.likes(X).every(:jeff.likes(X)).true?.should_not be_true
310
- :john.likes(X).every(:todd.likes(X)).true?.should_not be_true
311
- end
312
-
313
- specify "works like all" do
314
- every(:john.likes(X),:john.likes(X)).true?.should be_true
315
- every(:john.likes(X),:jane.likes(X)).true?.should be_true
316
- every(:john.likes(X),:jeff.likes(X)).true?.should_not be_true
317
- every(:john.likes(X),:todd.likes(X)).true?.should_not be_true
231
+ specify "all works" do
232
+ (:john.likes(X).all(:john.likes(X))).true?.should be_true
233
+ (:john.likes(X).all(:jane.likes(X))).true?.should be_true
234
+ (:john.likes(X).all(:jeff.likes(X))).true?.should_not be_true
235
+ (:john.likes(X).all(:todd.likes(X))).true?.should_not be_true
318
236
 
319
- every(:jane.likes(X),:john.likes(X)).true?.should_not be_true
320
- every(:jane.likes(X),:jane.likes(X)).true?.should be_true
321
- every(:jane.likes(X),:jeff.likes(X)).true?.should_not be_true
322
- every(:jane.likes(X),:todd.likes(X)).true?.should_not be_true
237
+ (:jane.likes(X).all(:john.likes(X))).true?.should_not be_true
238
+ (:jane.likes(X).all(:jane.likes(X))).true?.should be_true
239
+ (:jane.likes(X).all(:jeff.likes(X))).true?.should_not be_true
240
+ (:jane.likes(X).all(:todd.likes(X))).true?.should_not be_true
323
241
 
324
- every(:jeff.likes(X),:john.likes(X)).true?.should_not be_true
325
- every(:jeff.likes(X),:jane.likes(X)).true?.should_not be_true
326
- every(:jeff.likes(X),:jeff.likes(X)).true?.should be_true
327
- every(:jeff.likes(X),:todd.likes(X)).true?.should_not be_true
242
+ (:jeff.likes(X).all(:john.likes(X))).true?.should_not be_true
243
+ (:jeff.likes(X).all(:jane.likes(X))).true?.should_not be_true
244
+ (:jeff.likes(X).all(:jeff.likes(X))).true?.should be_true
245
+ (:jeff.likes(X).all(:todd.likes(X))).true?.should_not be_true
328
246
 
329
- every(:todd.likes(X),:john.likes(X)).true?.should_not be_true
330
- every(:todd.likes(X),:jane.likes(X)).true?.should be_true
331
- every(:todd.likes(X),:jeff.likes(X)).true?.should_not be_true
332
- every(:todd.likes(X),:todd.likes(X)).true?.should be_true
247
+ (:todd.likes(X).all(:john.likes(X))).true?.should_not be_true
248
+ (:todd.likes(X).all(:jane.likes(X))).true?.should be_true
249
+ (:todd.likes(X).all(:jeff.likes(X))).true?.should_not be_true
250
+ (:todd.likes(X).all(:todd.likes(X))).true?.should be_true
333
251
  end
334
252
 
335
- specify "works like all with procs" do
336
- every(:john.likes(X)){:john.likes?(X)}.true?.should be_true
337
- every(:john.likes(X)){:jane.likes?(X)}.true?.should be_true
338
- every(:john.likes(X)){:jeff.likes?(X)}.true?.should_not be_true
339
- every(:john.likes(X)){:todd.likes?(X)}.true?.should_not be_true
253
+ specify "all works with procs" do
254
+ (:john.likes(X).all{:john.likes?(X)}).true?.should be_true
255
+ (:john.likes(X).all{:jane.likes?(X)}).true?.should be_true
256
+ (:john.likes(X).all{:jeff.likes?(X)}).true?.should_not be_true
257
+ (:john.likes(X).all{:todd.likes?(X)}).true?.should_not be_true
258
+
259
+ (:jane.likes(X).all{:john.likes?(X)}).true?.should_not be_true
260
+ (:jane.likes(X).all{:jane.likes?(X)}).true?.should be_true
261
+ (:jane.likes(X).all{:jeff.likes?(X)}).true?.should_not be_true
262
+ (:jane.likes(X).all{:todd.likes?(X)}).true?.should_not be_true
263
+
264
+ (:jeff.likes(X).all{:john.likes?(X)}).true?.should_not be_true
265
+ (:jeff.likes(X).all{:jane.likes?(X)}).true?.should_not be_true
266
+ (:jeff.likes(X).all{:jeff.likes?(X)}).true?.should be_true
267
+ (:jeff.likes(X).all{:todd.likes?(X)}).true?.should_not be_true
268
+
269
+ (:todd.likes(X).all{:john.likes?(X)}).true?.should_not be_true
270
+ (:todd.likes(X).all{:jane.likes?(X)}).true?.should be_true
271
+ (:todd.likes(X).all{:jeff.likes?(X)}).true?.should_not be_true
272
+ (:todd.likes(X).all{:todd.likes?(X)}).true?.should be_true
273
+ end
340
274
 
341
- every(:jane.likes(X)){:john.likes?(X)}.true?.should_not be_true
342
- every(:jane.likes(X)){:jane.likes?(X)}.true?.should be_true
343
- every(:jane.likes(X)){:jeff.likes?(X)}.true?.should_not be_true
344
- every(:jane.likes(X)){:todd.likes?(X)}.true?.should_not be_true
275
+ it "can be called with global functor syntax" do
276
+ all(:john.likes(X), :jane.likes(X)).true?.should be_true
277
+ all(:jane.likes(X), :john.likes(X)).true?.should_not be_true
278
+ any(:jane.likes(X), :todd.likes(X)).true?.should be_true
279
+ any(:john.likes(X), :todd.likes(X)).true?.should_not be_true
280
+ end
345
281
 
346
- every(:jeff.likes(X)){:john.likes?(X)}.true?.should_not be_true
347
- every(:jeff.likes(X)){:jane.likes?(X)}.true?.should_not be_true
348
- every(:jeff.likes(X)){:jeff.likes?(X)}.true?.should be_true
349
- every(:jeff.likes(X)){:todd.likes?(X)}.true?.should_not be_true
282
+ it "one, any, none can be called unarily" do
283
+ one(:john.likes(X)).true?.should_not be_true
284
+ one(:jane.likes(X)).true?.should_not be_true
285
+ one(:jeff.likes(X)).true?.should_not be_true
286
+ one(:todd.likes(X)).true?.should be_true
287
+ one(:jim.likes(X)).true?.should_not be_true
288
+
289
+ any(:john.likes(X)).true?.should be_true
290
+ any(:jane.likes(X)).true?.should be_true
291
+ any(:jeff.likes(X)).true?.should be_true
292
+ any(:todd.likes(X)).true?.should be_true
293
+ any(:jim.likes(X)).true?.should_not be_true
294
+
295
+ none(:john.likes(X)).true?.should_not be_true
296
+ none(:jane.likes(X)).true?.should_not be_true
297
+ none(:jeff.likes(X)).true?.should_not be_true
298
+ none(:todd.likes(X)).true?.should_not be_true
299
+ none(:jim.likes(X)).true?.should be_true
300
+ end
350
301
 
351
- every(:todd.likes(X)){:john.likes?(X)}.true?.should_not be_true
352
- every(:todd.likes(X)){:jane.likes?(X)}.true?.should be_true
353
- every(:todd.likes(X)){:jeff.likes?(X)}.true?.should_not be_true
354
- every(:todd.likes(X)){:todd.likes?(X)}.true?.should be_true
302
+ it "does not hijack variables" do
303
+ A.is(X.is(5)).and(A.all{X<10}).true?.should == true
355
304
  end
356
305
 
306
+ describe "every" do
307
+ specify "works as infix" do
308
+ :john.likes(X).every(:john.likes(X)).true?.should be_true
309
+ :john.likes(X).every(:jane.likes(X)).true?.should be_true
310
+ :john.likes(X).every(:jeff.likes(X)).true?.should_not be_true
311
+ :john.likes(X).every(:todd.likes(X)).true?.should_not be_true
312
+ end
313
+
314
+ specify "works like all" do
315
+ every(:john.likes(X),:john.likes(X)).true?.should be_true
316
+ every(:john.likes(X),:jane.likes(X)).true?.should be_true
317
+ every(:john.likes(X),:jeff.likes(X)).true?.should_not be_true
318
+ every(:john.likes(X),:todd.likes(X)).true?.should_not be_true
319
+
320
+ every(:jane.likes(X),:john.likes(X)).true?.should_not be_true
321
+ every(:jane.likes(X),:jane.likes(X)).true?.should be_true
322
+ every(:jane.likes(X),:jeff.likes(X)).true?.should_not be_true
323
+ every(:jane.likes(X),:todd.likes(X)).true?.should_not be_true
324
+
325
+ every(:jeff.likes(X),:john.likes(X)).true?.should_not be_true
326
+ every(:jeff.likes(X),:jane.likes(X)).true?.should_not be_true
327
+ every(:jeff.likes(X),:jeff.likes(X)).true?.should be_true
328
+ every(:jeff.likes(X),:todd.likes(X)).true?.should_not be_true
329
+
330
+ every(:todd.likes(X),:john.likes(X)).true?.should_not be_true
331
+ every(:todd.likes(X),:jane.likes(X)).true?.should be_true
332
+ every(:todd.likes(X),:jeff.likes(X)).true?.should_not be_true
333
+ every(:todd.likes(X),:todd.likes(X)).true?.should be_true
334
+ end
335
+
336
+ specify "works like all with procs" do
337
+ every(:john.likes(X)){:john.likes?(X)}.true?.should be_true
338
+ every(:john.likes(X)){:jane.likes?(X)}.true?.should be_true
339
+ every(:john.likes(X)){:jeff.likes?(X)}.true?.should_not be_true
340
+ every(:john.likes(X)){:todd.likes?(X)}.true?.should_not be_true
341
+
342
+ every(:jane.likes(X)){:john.likes?(X)}.true?.should_not be_true
343
+ every(:jane.likes(X)){:jane.likes?(X)}.true?.should be_true
344
+ every(:jane.likes(X)){:jeff.likes?(X)}.true?.should_not be_true
345
+ every(:jane.likes(X)){:todd.likes?(X)}.true?.should_not be_true
346
+
347
+ every(:jeff.likes(X)){:john.likes?(X)}.true?.should_not be_true
348
+ every(:jeff.likes(X)){:jane.likes?(X)}.true?.should_not be_true
349
+ every(:jeff.likes(X)){:jeff.likes?(X)}.true?.should be_true
350
+ every(:jeff.likes(X)){:todd.likes?(X)}.true?.should_not be_true
351
+
352
+ every(:todd.likes(X)){:john.likes?(X)}.true?.should_not be_true
353
+ every(:todd.likes(X)){:jane.likes?(X)}.true?.should be_true
354
+ every(:todd.likes(X)){:jeff.likes?(X)}.true?.should_not be_true
355
+ every(:todd.likes(X)){:todd.likes?(X)}.true?.should be_true
356
+ end
357
+
358
+
359
+ specify "can be used for assumptions" do
360
+ predicate_for Symbol, ".good"
361
+ # assumptions reverse the order
362
+ every(:john.likes(X), X.good.assumed).and(Y.good).map{Y}.should == [:beer, :water]
363
+ end
364
+
365
+ specify "passes variables" do
366
+ solve N.is(5).and K.in{1..N}.every {N.should eql 5}
367
+ end
368
+
369
+ specify "passes variables" do
370
+ solve N.is(5).and K.in{1..N}.every L.in{1..N}
371
+ end
372
+
373
+ specify "passes variables if b contains the variable" do
374
+ solve N.is(5).and every I.in{2..N}, K.is{N/I}
375
+ end
357
376
 
358
- specify "can be used for assumptions" do
359
- predicate_for Symbol, ".good"
360
- # assumptions reverse the order
361
- every(:john.likes(X), X.good.assumed).and(Y.good).map{Y}.should == [:beer, :water]
362
377
  end
363
378
 
364
- specify "passes variables" do
365
- solve N.is(5).and K.in{1..N}.every {N.should eql 5}
366
- end
367
-
368
- specify "passes variables" do
369
- solve N.is(5).and K.in{1..N}.every L.in{1..N}
370
- end
371
379
 
372
- specify "passes variables if b contains the variable" do
373
- solve N.is(5).and every I.in{2..N}, K.is{N/I}
374
- end
375
380
 
376
381
  end
377
382
 
378
-
379
-
380
383
  end
381
-
382
384
  end