rubylog 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/Gemfile.lock +10 -0
- data/README.rdoc +1 -1
- data/RELEASE_NOTES.rdoc +7 -4
- data/VERSION +1 -1
- data/{examples → benchmark}/benchmark.rb +1 -0
- data/{examples → benchmark}/benchmark/compiled_not_indexed.rb +0 -0
- data/{examples → benchmark}/benchmark/compiled_sequence_indexed.rb +0 -0
- data/{examples → benchmark}/benchmark/indexed_procedure.rb +0 -0
- data/{examples → benchmark}/benchmark/prolog.rb +0 -0
- data/benchmark/benchmark/pure.rb +28 -0
- data/bin/rubylog +14 -0
- data/examples/a_plus_b.rb +2 -2
- data/examples/dcg.rb +22 -26
- data/examples/dcg2.rb +25 -30
- data/examples/divisors.rb +1 -3
- data/examples/factorial.rb +8 -15
- data/examples/file_search.rb +14 -13
- data/examples/hanoi.rb +1 -3
- data/examples/hu/csaladfa.rb +0 -4
- data/examples/n_queens.rb +17 -22
- data/examples/palindrome_detection.rb +1 -2
- data/examples/parsing.rb +19 -23
- data/examples/permutation.rb +1 -3
- data/examples/primality_by_division.rb +2 -2
- data/examples/sieve_of_eratosthenes.rb +2 -2
- data/examples/string_interpolation.rb +0 -3
- data/examples/tracing.rb +0 -4
- data/lib/rubylog/builtins/assumption.rb +2 -1
- data/lib/rubylog/builtins/file_system.rb +1 -1
- data/lib/rubylog/default_context.rb +3 -5
- data/lib/rubylog/mixins/kernel.rb +9 -1
- data/lib/rubylog/rubylog_files.rb +7 -0
- data/rubylog.gemspec +17 -22
- data/spec/inriasuite_spec.rb +851 -847
- data/spec/integration/dsl_spec.rb +32 -29
- data/spec/rspec/rubylog_spec.rb +46 -52
- data/spec/rubylog/assertable_spec.rb +92 -90
- data/spec/rubylog/builtins/arithmetics_spec.rb +92 -90
- data/spec/rubylog/builtins/assumption_spec.rb +59 -57
- data/spec/rubylog/builtins/ensure_spec.rb +6 -4
- data/spec/rubylog/builtins/file_system_spec.rb +41 -39
- data/spec/rubylog/builtins/logic_spec.rb +308 -306
- data/spec/rubylog/builtins/reflection_spec.rb +31 -29
- data/spec/rubylog/builtins/term_spec.rb +62 -60
- data/spec/rubylog/context_modules/demonstration_spec.rb +108 -106
- data/spec/rubylog/context_modules/predicates_spec.rb +29 -27
- data/spec/rubylog/context_modules/thats_spec.rb +77 -75
- data/spec/rubylog/dsl/array_splat_spec.rb +11 -9
- data/spec/rubylog/dsl/indicators_spec.rb +23 -21
- data/spec/rubylog/dsl/primitives_spec.rb +30 -28
- data/spec/rubylog/errors_spec.rb +13 -11
- data/spec/rubylog/interfaces/term_spec.rb +78 -76
- data/spec/rubylog/mixins/array_spec.rb +60 -58
- data/spec/rubylog/mixins/composite_term_spec.rb +55 -53
- data/spec/rubylog/mixins/proc_spec.rb +48 -46
- data/spec/rubylog/mixins/string_spec.rb +45 -43
- data/spec/rubylog/mixins/symbol_spec.rb +7 -5
- data/spec/rubylog/procedure_spec.rb +8 -6
- data/spec/rubylog/rule_spec.rb +10 -8
- data/spec/rubylog/structure_spec.rb +73 -71
- data/spec/rubylog/term_spec.rb +5 -3
- data/spec/rubylog/tracing_spec.rb +35 -33
- data/spec/rubylog/variable_spec.rb +249 -247
- data/spec/spec_helper.rb +4 -0
- metadata +54 -43
- data/examples/benchmark/pure.rb +0 -26
- data/examples/checkmate.rb +0 -88
- data/examples/combination.rb +0 -17
- data/examples/directory_structure_logic.rb +0 -17
- data/examples/dirlist.rb +0 -4
- data/examples/enumerators.rb +0 -30
- data/examples/hello.rb +0 -17
- data/examples/mice.rb +0 -92
- data/examples/mice2.rb +0 -37
- data/examples/object_oriented.rb +0 -14
- data/examples/prefix.rb +0 -13
- data/examples/primitives.rb +0 -26
- data/examples/sudoku.rb +0 -17
- data/spec/integration/theory_as_module_spec.rb +0 -20
- data/spec/integration/theory_as_module_with_include_spec.rb +0 -14
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubylog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,36 +9,36 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ruby-prof
|
16
|
-
requirement: &
|
16
|
+
requirement: &1 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *1
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &2 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - <
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '3'
|
33
|
-
- -
|
33
|
+
- - '>='
|
34
34
|
- !ruby/object:Gem::Version
|
35
35
|
version: 2.8.0
|
36
36
|
type: :development
|
37
37
|
prerelease: false
|
38
|
-
version_requirements: *
|
38
|
+
version_requirements: *2
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: yard
|
41
|
-
requirement: &
|
41
|
+
requirement: &3 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
44
|
- - ~>
|
@@ -46,43 +46,66 @@ dependencies:
|
|
46
46
|
version: '0.7'
|
47
47
|
type: :development
|
48
48
|
prerelease: false
|
49
|
-
version_requirements: *
|
49
|
+
version_requirements: *3
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
51
|
name: bundler
|
52
|
-
requirement: &
|
52
|
+
requirement: &4 !ruby/object:Gem::Requirement
|
53
53
|
none: false
|
54
54
|
requirements:
|
55
|
-
- -
|
55
|
+
- - '>='
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: 1.0.0
|
58
58
|
type: :development
|
59
59
|
prerelease: false
|
60
|
-
version_requirements: *
|
60
|
+
version_requirements: *4
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: jeweler
|
63
|
-
requirement: &
|
63
|
+
requirement: &5 !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 1.8.3
|
69
69
|
type: :development
|
70
70
|
prerelease: false
|
71
|
-
version_requirements: *
|
71
|
+
version_requirements: *5
|
72
72
|
- !ruby/object:Gem::Dependency
|
73
73
|
name: simplecov
|
74
|
-
requirement: &
|
74
|
+
requirement: &6 !ruby/object:Gem::Requirement
|
75
75
|
none: false
|
76
76
|
requirements:
|
77
|
-
- -
|
77
|
+
- - '>='
|
78
78
|
- !ruby/object:Gem::Version
|
79
79
|
version: '0'
|
80
80
|
type: :development
|
81
81
|
prerelease: false
|
82
|
-
version_requirements: *
|
82
|
+
version_requirements: *6
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard
|
85
|
+
requirement: &7 !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: *7
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: guard-rspec
|
96
|
+
requirement: &8 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: *8
|
83
105
|
description: Rubylog is a Prolog-like DSL for Ruby.
|
84
106
|
email: kallo.bernat@gmail.com
|
85
|
-
executables:
|
107
|
+
executables:
|
108
|
+
- rubylog
|
86
109
|
extensions: []
|
87
110
|
extra_rdoc_files:
|
88
111
|
- LICENSE.txt
|
@@ -98,39 +121,28 @@ files:
|
|
98
121
|
- Rakefile
|
99
122
|
- TODO.txt
|
100
123
|
- VERSION
|
124
|
+
- benchmark/benchmark.rb
|
125
|
+
- benchmark/benchmark/compiled_not_indexed.rb
|
126
|
+
- benchmark/benchmark/compiled_sequence_indexed.rb
|
127
|
+
- benchmark/benchmark/indexed_procedure.rb
|
128
|
+
- benchmark/benchmark/prolog.rb
|
129
|
+
- benchmark/benchmark/pure.rb
|
130
|
+
- bin/rubylog
|
101
131
|
- examples/a_plus_b.rb
|
102
|
-
- examples/benchmark.rb
|
103
|
-
- examples/benchmark/compiled_not_indexed.rb
|
104
|
-
- examples/benchmark/compiled_sequence_indexed.rb
|
105
|
-
- examples/benchmark/indexed_procedure.rb
|
106
|
-
- examples/benchmark/prolog.rb
|
107
|
-
- examples/benchmark/pure.rb
|
108
|
-
- examples/checkmate.rb
|
109
|
-
- examples/combination.rb
|
110
132
|
- examples/dcg.rb
|
111
133
|
- examples/dcg2.rb
|
112
|
-
- examples/directory_structure_logic.rb
|
113
|
-
- examples/dirlist.rb
|
114
134
|
- examples/divisors.rb
|
115
|
-
- examples/enumerators.rb
|
116
135
|
- examples/factorial.rb
|
117
136
|
- examples/file_search.rb
|
118
137
|
- examples/hanoi.rb
|
119
|
-
- examples/hello.rb
|
120
138
|
- examples/hu/csaladfa.rb
|
121
|
-
- examples/mice.rb
|
122
|
-
- examples/mice2.rb
|
123
139
|
- examples/n_queens.rb
|
124
|
-
- examples/object_oriented.rb
|
125
140
|
- examples/palindrome_detection.rb
|
126
141
|
- examples/parsing.rb
|
127
142
|
- examples/permutation.rb
|
128
|
-
- examples/prefix.rb
|
129
143
|
- examples/primality_by_division.rb
|
130
|
-
- examples/primitives.rb
|
131
144
|
- examples/sieve_of_eratosthenes.rb
|
132
145
|
- examples/string_interpolation.rb
|
133
|
-
- examples/sudoku.rb
|
134
146
|
- examples/tracing.rb
|
135
147
|
- lib/rspec/rubylog.rb
|
136
148
|
- lib/rubylog.rb
|
@@ -171,6 +183,7 @@ files:
|
|
171
183
|
- lib/rubylog/predicate.rb
|
172
184
|
- lib/rubylog/primitive.rb
|
173
185
|
- lib/rubylog/procedure.rb
|
186
|
+
- lib/rubylog/rubylog_files.rb
|
174
187
|
- lib/rubylog/rule.rb
|
175
188
|
- lib/rubylog/structure.rb
|
176
189
|
- lib/rubylog/term.rb
|
@@ -247,8 +260,6 @@ files:
|
|
247
260
|
- spec/inriasuite_spec.rb
|
248
261
|
- spec/integration/custom_classes_spec.rb
|
249
262
|
- spec/integration/dsl_spec.rb
|
250
|
-
- spec/integration/theory_as_module_spec.rb
|
251
|
-
- spec/integration/theory_as_module_with_include_spec.rb
|
252
263
|
- spec/rspec/rubylog_spec.rb
|
253
264
|
- spec/rubylog/assertable_spec.rb
|
254
265
|
- spec/rubylog/builtins/arithmetics_spec.rb
|
@@ -290,16 +301,16 @@ require_paths:
|
|
290
301
|
required_ruby_version: !ruby/object:Gem::Requirement
|
291
302
|
none: false
|
292
303
|
requirements:
|
293
|
-
- -
|
304
|
+
- - '>='
|
294
305
|
- !ruby/object:Gem::Version
|
295
306
|
version: '0'
|
296
307
|
segments:
|
297
308
|
- 0
|
298
|
-
hash:
|
309
|
+
hash: -992188621
|
299
310
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
300
311
|
none: false
|
301
312
|
requirements:
|
302
|
-
- -
|
313
|
+
- - '>='
|
303
314
|
- !ruby/object:Gem::Version
|
304
315
|
version: '0'
|
305
316
|
requirements: []
|
data/examples/benchmark/pure.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
require "rubylog"
|
3
|
-
extend Rubylog::Context
|
4
|
-
|
5
|
-
predicate_for $person_class, ".parent_of() .grandparent_of()"
|
6
|
-
|
7
|
-
def make_tree(parent, levels)
|
8
|
-
return if levels.zero?
|
9
|
-
|
10
|
-
children = (1..DEGREES).map{random_person}
|
11
|
-
|
12
|
-
children.each do |child|
|
13
|
-
# add relationship
|
14
|
-
parent.parent_of!(child)
|
15
|
-
end
|
16
|
-
|
17
|
-
children.each do |child|
|
18
|
-
# make sub-tree
|
19
|
-
make_tree(child, levels-1)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
make_tree(random_person, LEVELS)
|
24
|
-
|
25
|
-
A.grandparent_of(B).if A.parent_of(X).and X.parent_of(B)
|
26
|
-
|
data/examples/checkmate.rb
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
require "rubylog"
|
2
|
-
|
3
|
-
|
4
|
-
rubylog do
|
5
|
-
predicate_for Array, ".on()"
|
6
|
-
|
7
|
-
black, white = 0, 1
|
8
|
-
pawn,rook,knight,bishop,queen,king = :pawn, :rook, :knight, :bishop, :queen, :king
|
9
|
-
|
10
|
-
mark = :mark
|
11
|
-
|
12
|
-
signs = {pawn: "p", rook:"r", knight:"n", bishop:"b", queen:"q", king:"k", mark:"x"}
|
13
|
-
|
14
|
-
[black,pawn] .on! [5,7]
|
15
|
-
[black,bishop].on! [8,1]
|
16
|
-
[black,pawn] .on! [8,5]
|
17
|
-
[black,king] .on! [8,6]
|
18
|
-
|
19
|
-
[white,pawn] .on! [1,7]
|
20
|
-
[white,king] .on! [1,8]
|
21
|
-
[white,bishop].on! [2,8]
|
22
|
-
[white,pawn] .on! [3,7]
|
23
|
-
[white,pawn] .on! [5,5]
|
24
|
-
[white,pawn] .on! [6,6]
|
25
|
-
[white,knight].on! [6,8]
|
26
|
-
[white,bishop].on! [7,2]
|
27
|
-
[white,pawn] .on! [7,7]
|
28
|
-
[white,pawn] .on! [8,4]
|
29
|
-
|
30
|
-
predicate ":show"
|
31
|
-
|
32
|
-
:show.if do
|
33
|
-
(R_.in(1..8).and(R.is{9-R_})).each do
|
34
|
-
F.in(1..8).each do
|
35
|
-
print '.' unless prove [C,P].on([F,R]).and :cut!.and { print C==white ? signs[P].upcase : signs[P]; true }
|
36
|
-
end
|
37
|
-
puts
|
38
|
-
end
|
39
|
-
puts
|
40
|
-
true
|
41
|
-
end
|
42
|
-
|
43
|
-
functor_for Rubylog::Structure, :attacks, :can_move_to
|
44
|
-
functor_for Integer, :rook_move, :bishop_move, :squares, :squares_forward
|
45
|
-
prefix_functor :knight_move, :pawn_attack, :pawn_move
|
46
|
-
|
47
|
-
|
48
|
-
[C,king ].on(S).can_move_to(S1).if 1. rook_move(S,S1).or 1.bishop_move(S,S1)
|
49
|
-
[C,queen ].on(S).can_move_to(S1).if ANY. rook_move(S,S1).or ANY.bishop_move(S,S1)
|
50
|
-
[C,bishop ].on(S).can_move_to(S1).if ANY.bishop_move(S,S1)
|
51
|
-
[C,knight ].on(S).can_move_to(S1).if knight_move(S,S1)
|
52
|
-
[C,rook ].on(S).can_move_to(S1).if ANY. rook_move(S,S1)
|
53
|
-
|
54
|
-
[white,pawn].on(S).can_move_to(S1).if pawn_move(S,S1)
|
55
|
-
[black,pawn].on(S).can_move_to(S1).if pawn_move(S1,S)
|
56
|
-
[white,pawn].on([F,2]).can_move_to!([F,4])
|
57
|
-
[black,pawn].on([F,7]).can_move_to!([F,5])
|
58
|
-
|
59
|
-
[white,pawn].on(S).attacks(S1).if! pawn_attack(S,S1)
|
60
|
-
[black,pawn].on(S).attacks(S1).if! pawn_attack(S1,S)
|
61
|
-
X.attacks(S1).if X.can_move_to(S1)
|
62
|
-
|
63
|
-
|
64
|
-
N.squares_forward(A[1..8],B[1..8]).if B.is(A,:+,N)
|
65
|
-
N.squares(A,B).if A.in(1..8).and B.in(1..8).and B.is(A,:+,N).or A.is(B,:+,N)
|
66
|
-
|
67
|
-
N. rook_move([F ,R1],[F ,R2]).if N.squares(R1,R2)
|
68
|
-
N. rook_move([F1,R ],[F2,R ]).if N.squares(F1,F2)
|
69
|
-
N.bishop_move([F1,R1],[F2,R2]).if N.squares(R1,R2).and N.squares(F1,F2)
|
70
|
-
|
71
|
-
knight_move([F1,R1],[F2,R2]).if((1.squares(F1,F2).and 2.squares(R1,R2))
|
72
|
-
.or(1.squares(R1,R2).and 2.squares(F1,F2)))
|
73
|
-
pawn_move( [F ,R1],[F ,R2]).if 1.squares_forward(R1,R2)
|
74
|
-
pawn_attack([F1,R1],[F2,R2]).if R2.is(R1,:+,1).and 1.squares(F1,F2)
|
75
|
-
|
76
|
-
|
77
|
-
functor_for Rubylog::Structure, :moved_to
|
78
|
-
|
79
|
-
[C,P].on(S).moved_to(S1).if [C,P].on(S).and [C,P].on(S).can_move_to(S1).and [C,P].on(S).revoked.and [C,P].on(S1).assumed
|
80
|
-
|
81
|
-
solve :show.and [C,P].on(S).moved_to(S1).and{ puts "#{C} #{P}: #{S} -> #{S1}"; true}.and :show
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
end
|
data/examples/combination.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require "rubylog"
|
2
|
-
extend Rubylog::Context
|
3
|
-
|
4
|
-
predicate_for Array, ".choose(,,)"
|
5
|
-
predicate_for Integer, ".one_less_than()"
|
6
|
-
|
7
|
-
N.one_less_than(M).if M.sum_of(N,1)
|
8
|
-
|
9
|
-
L.choose!(0,[],L)
|
10
|
-
[A,*REST].choose(N[thats>0],CHOSEN,[A,*REM]).if REST.choose(N,CHOSEN,REM)
|
11
|
-
[A,*REST].choose(N[thats>0],[A,*CHOSEN],REM).if N1.one_less_than(N).and REST.choose(N1,CHOSEN,REM)
|
12
|
-
|
13
|
-
[1,2,3,4,5].choose(3,L,ANY).each do
|
14
|
-
L.choose(1,[A],REM).each do
|
15
|
-
puts "#{A}: #{REM}"
|
16
|
-
end
|
17
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require "rubylog"
|
2
|
-
|
3
|
-
rubylog do
|
4
|
-
predicate_for String, ".dir", ".contains()"
|
5
|
-
|
6
|
-
".git".dir!.contains! "Git repository"
|
7
|
-
"bin".dir!.contains! "executables"
|
8
|
-
"examples".dir!.contains! "examples and ideas for different applications of Rubylog"
|
9
|
-
"lib".dir!.contains! "source code of Rubylog"
|
10
|
-
"spec".dir!.contains! "functional tests written in RSpec"
|
11
|
-
"logic".dir!.contains! "integration tests written in Rubylog"
|
12
|
-
|
13
|
-
gitignore = File.readlines(".gitignore").map{|l| l.chop}
|
14
|
-
check X.in(Y.dir.map{Y}).iff(X.in(Y.dirname_in(".").and(Y.not_in(gitignore)).map{Y}))
|
15
|
-
|
16
|
-
end
|
17
|
-
|
data/examples/dirlist.rb
DELETED
data/examples/enumerators.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
$:.unshift File.dirname(__FILE__)+"/../lib"
|
2
|
-
require 'rubylog'
|
3
|
-
|
4
|
-
rubylog do
|
5
|
-
self.default_subject = Symbol
|
6
|
-
predicate ".likes()"
|
7
|
-
|
8
|
-
:john.likes! :beer
|
9
|
-
:john.likes! :milk
|
10
|
-
:john.likes! :water
|
11
|
-
|
12
|
-
:jane.likes! :milk
|
13
|
-
:jane.likes! :water
|
14
|
-
|
15
|
-
:jeff.likes! :water
|
16
|
-
:jeff.likes! :milk
|
17
|
-
:jeff.likes! :juice
|
18
|
-
|
19
|
-
check A.likes(:beer).all A.likes(:water)
|
20
|
-
check A.likes(:beer).any A.likes(B).and B.is_not :water
|
21
|
-
check A.likes(:milk).iff A.likes(:water)
|
22
|
-
check A.likes(:beer).iff A.is(:john)
|
23
|
-
|
24
|
-
check all(X.likes(:juice), X.likes(:beer).false)
|
25
|
-
check any(X.likes(:water), X.likes(:juice))
|
26
|
-
check one(X.likes :juice)
|
27
|
-
check one(X.likes :milk).false
|
28
|
-
check none(X.likes :palinka)
|
29
|
-
check iff A.likes(:milk), A.likes(:water)
|
30
|
-
end
|
data/examples/hello.rb
DELETED
data/examples/mice.rb
DELETED
@@ -1,92 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
$:.unshift File.dirname(__FILE__)+"/../lib"
|
3
|
-
# This is a solution for a math problem.
|
4
|
-
#
|
5
|
-
# Mafia the black cat plays a game. She has N cups with 0 or 1 mouse under each.
|
6
|
-
# She peeks under some cups one by one to see if there is a mouse under them.
|
7
|
-
# What she wants to find out is whether there are two cups next to each other
|
8
|
-
# which both have a mouse. She found that for some number of cups she has to
|
9
|
-
# peek under every cup to decide if there are two adjacent ones with a mouse.
|
10
|
-
# In this case she considers the task hard. For which N's does she consider it
|
11
|
-
# hard (N<=2013)?
|
12
|
-
|
13
|
-
require "rubylog"
|
14
|
-
require "rubylog/builtins/assumption"
|
15
|
-
extend Rubylog::Context
|
16
|
-
|
17
|
-
|
18
|
-
class Cup < Struct.new :i
|
19
|
-
extend Rubylog::Context
|
20
|
-
|
21
|
-
def inspect
|
22
|
-
"##{i}"
|
23
|
-
end
|
24
|
-
|
25
|
-
# A cup can be peeked: it has mouse or not
|
26
|
-
predicate %w(.peeked .has_mouse .seen )
|
27
|
-
C.peeked.if C.has_mouse.assumed.or(:true).and C.seen.assumed
|
28
|
-
|
29
|
-
# A cup can be guessed: it has mouse or not
|
30
|
-
predicate %w(.guessed)
|
31
|
-
C.guessed.if C.has_mouse.assumed.or(:true)
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
class CupSet
|
37
|
-
extend Rubylog::Context
|
38
|
-
|
39
|
-
def initialize n
|
40
|
-
@cups = (1..n).map {|i| Cup.new i }
|
41
|
-
end
|
42
|
-
|
43
|
-
def each &block
|
44
|
-
@cups.each &block
|
45
|
-
end
|
46
|
-
|
47
|
-
def [] index
|
48
|
-
@cups[index]
|
49
|
-
end
|
50
|
-
|
51
|
-
# A set has neighbors if
|
52
|
-
predicate %w(.has_neighbors)
|
53
|
-
CS.has_neighbors.if [C,D].in{CS[0..-2].zip(CS[1..-1] || [])}.and C.has_mouse.and D.has_mouse
|
54
|
-
|
55
|
-
# A predicate definitely solves a set if there is no ambiguity
|
56
|
-
predicate_for Rubylog::Goal, %w(.definitely_solves())
|
57
|
-
T.definitely_solves(CS).if T.any(CS.has_neighbors).and(T.any(CS.has_neighbors.false)).false
|
58
|
-
|
59
|
-
# A trial consist of peeking some cups
|
60
|
-
predicate_for Rubylog::Goal, %w(.trial_for())
|
61
|
-
T.trial_for(CS).if T.is{C.in(CS).map{C.peeked.or :true}.inject(:true,&:and)}.and T
|
62
|
-
|
63
|
-
# A set is easy if can be definitely solved by a trial that has not seen all
|
64
|
-
# cups. A set is had if it cannot.
|
65
|
-
predicate %w(.easy() .hard)
|
66
|
-
CS.easy(Peeks).if any T.trial_for(CS).and(C.in(CS).all(C.seen).false).definitely_solves(CS).and(Peeks.is{D.in(CS).and(D.peeked).map{D}})
|
67
|
-
CS.hard.if CS.easy.false
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
|
72
|
-
N.in(0..4).each do
|
73
|
-
puts "#{N}:"
|
74
|
-
CS.is{CupSet.new(N)}.each do
|
75
|
-
|
76
|
-
C.in{CS}.each { p C }
|
77
|
-
T.trial_for(CS).each { p T }
|
78
|
-
|
79
|
-
easy = false
|
80
|
-
CS.easy(Peeks).each do
|
81
|
-
puts "easy: #{Peeks.inspect}"
|
82
|
-
easy = true
|
83
|
-
end
|
84
|
-
puts "hard" if not easy
|
85
|
-
end
|
86
|
-
puts
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|