rubylog 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +64 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +96 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/examples/4queens.rb +10 -0
- data/examples/calculation.rb +12 -0
- data/examples/concepts.rb +46 -0
- data/examples/factorial.rb +16 -0
- data/examples/fp.rb +56 -0
- data/examples/hello.rb +9 -0
- data/examples/historia_de_espana.rb +31 -0
- data/examples/idea.rb +143 -0
- data/examples/lists.rb +5 -0
- data/examples/mechanika.rb +409 -0
- data/examples/parse.rb +15 -0
- data/examples/theory.rb +20 -0
- data/lib/array.rb +24 -0
- data/lib/class.rb +11 -0
- data/lib/method.rb +4 -0
- data/lib/object.rb +5 -0
- data/lib/proc.rb +4 -0
- data/lib/rubylog/builtins.rb +193 -0
- data/lib/rubylog/callable.rb +20 -0
- data/lib/rubylog/clause.rb +113 -0
- data/lib/rubylog/composite_term.rb +38 -0
- data/lib/rubylog/dsl/constants.rb +15 -0
- data/lib/rubylog/dsl/first_order_functors.rb +9 -0
- data/lib/rubylog/dsl/global_functors.rb +3 -0
- data/lib/rubylog/dsl/second_order_functors.rb +8 -0
- data/lib/rubylog/dsl.rb +52 -0
- data/lib/rubylog/errors.rb +18 -0
- data/lib/rubylog/internal_helpers.rb +16 -0
- data/lib/rubylog/predicate.rb +34 -0
- data/lib/rubylog/proc_method_additions.rb +69 -0
- data/lib/rubylog/term.rb +20 -0
- data/lib/rubylog/theory.rb +133 -0
- data/lib/rubylog/unifiable.rb +19 -0
- data/lib/rubylog/variable.rb +97 -0
- data/lib/rubylog.rb +39 -0
- data/lib/symbol.rb +35 -0
- data/rubylog.gemspec +187 -0
- data/script/inriasuite2spec +0 -0
- data/script/inriasuite2spec.pl +22 -0
- data/spec/bartak_guide_spec.rb +91 -0
- data/spec/inriasuite/README +122 -0
- data/spec/inriasuite/abolish +18 -0
- data/spec/inriasuite/and +9 -0
- data/spec/inriasuite/arg +32 -0
- data/spec/inriasuite/arith_diff +10 -0
- data/spec/inriasuite/arith_eq +10 -0
- data/spec/inriasuite/arith_gt +10 -0
- data/spec/inriasuite/arith_gt= +10 -0
- data/spec/inriasuite/arith_lt +10 -0
- data/spec/inriasuite/arith_lt= +10 -0
- data/spec/inriasuite/asserta +18 -0
- data/spec/inriasuite/assertz +16 -0
- data/spec/inriasuite/atom +12 -0
- data/spec/inriasuite/atom_chars +19 -0
- data/spec/inriasuite/atom_codes +15 -0
- data/spec/inriasuite/atom_concat +19 -0
- data/spec/inriasuite/atom_length +12 -0
- data/spec/inriasuite/atomic +11 -0
- data/spec/inriasuite/bagof +31 -0
- data/spec/inriasuite/call +19 -0
- data/spec/inriasuite/catch-and-throw +16 -0
- data/spec/inriasuite/char_code +13 -0
- data/spec/inriasuite/clause +16 -0
- data/spec/inriasuite/compound +12 -0
- data/spec/inriasuite/copy_term +25 -0
- data/spec/inriasuite/current_input +5 -0
- data/spec/inriasuite/current_output +5 -0
- data/spec/inriasuite/current_predicate +16 -0
- data/spec/inriasuite/current_prolog_flag +12 -0
- data/spec/inriasuite/cut +9 -0
- data/spec/inriasuite/fail +15 -0
- data/spec/inriasuite/file_manip +8 -0
- data/spec/inriasuite/findall +22 -0
- data/spec/inriasuite/float +10 -0
- data/spec/inriasuite/functor +41 -0
- data/spec/inriasuite/functor-bis +41 -0
- data/spec/inriasuite/halt +7 -0
- data/spec/inriasuite/if-then +10 -0
- data/spec/inriasuite/if-then-else +12 -0
- data/spec/inriasuite/inriasuite.obp +0 -0
- data/spec/inriasuite/inriasuite.pl +836 -0
- data/spec/inriasuite/integer +10 -0
- data/spec/inriasuite/is +11 -0
- data/spec/inriasuite/junk +0 -0
- data/spec/inriasuite/nonvar +11 -0
- data/spec/inriasuite/not_provable +12 -0
- data/spec/inriasuite/not_unify +15 -0
- data/spec/inriasuite/number +10 -0
- data/spec/inriasuite/number_chars +22 -0
- data/spec/inriasuite/number_codes +19 -0
- data/spec/inriasuite/once +11 -0
- data/spec/inriasuite/or +9 -0
- data/spec/inriasuite/repeat +5 -0
- data/spec/inriasuite/retract +10 -0
- data/spec/inriasuite/set_prolog_flag +21 -0
- data/spec/inriasuite/setof +36 -0
- data/spec/inriasuite/sub_atom +30 -0
- data/spec/inriasuite/t +1 -0
- data/spec/inriasuite/t_foo.pl +4 -0
- data/spec/inriasuite/term_diff +13 -0
- data/spec/inriasuite/term_eq +12 -0
- data/spec/inriasuite/term_gt +12 -0
- data/spec/inriasuite/term_gt= +12 -0
- data/spec/inriasuite/term_lt +12 -0
- data/spec/inriasuite/term_lt= +12 -0
- data/spec/inriasuite/true +7 -0
- data/spec/inriasuite/unify +18 -0
- data/spec/inriasuite.rb +20 -0
- data/spec/recursion_spec.rb +18 -0
- data/spec/rubylog/builtins/splits_to.rb +18 -0
- data/spec/rubylog/clause_spec.rb +81 -0
- data/spec/rubylog/variable_spec.rb +25 -0
- data/spec/rubylog_spec.rb +914 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/theory_spec.rb +1 -0
- metadata +339 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.8.0"
|
10
|
+
gem "yard", "~> 0.7"
|
11
|
+
gem "rdoc", "~> 3.12"
|
12
|
+
gem "cucumber", ">= 0"
|
13
|
+
gem "bundler", "~> 1.0.0"
|
14
|
+
gem "jeweler", "~> 1.8.3"
|
15
|
+
gem "simplecov"
|
16
|
+
gem "reek", "~> 1.2.8"
|
17
|
+
gem "roodi", "~> 2.1.0"
|
18
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
builder (3.0.0)
|
5
|
+
cucumber (1.1.4)
|
6
|
+
builder (>= 2.1.2)
|
7
|
+
diff-lcs (>= 1.1.2)
|
8
|
+
gherkin (~> 2.7.1)
|
9
|
+
json (>= 1.4.6)
|
10
|
+
term-ansicolor (>= 1.0.6)
|
11
|
+
diff-lcs (1.1.3)
|
12
|
+
gherkin (2.7.6)
|
13
|
+
json (>= 1.4.6)
|
14
|
+
git (1.2.5)
|
15
|
+
jeweler (1.8.3)
|
16
|
+
bundler (~> 1.0)
|
17
|
+
git (>= 1.2.5)
|
18
|
+
rake
|
19
|
+
rdoc
|
20
|
+
json (1.6.5)
|
21
|
+
multi_json (1.0.4)
|
22
|
+
rake (0.9.2.2)
|
23
|
+
rdoc (3.12)
|
24
|
+
json (~> 1.4)
|
25
|
+
reek (1.2.8)
|
26
|
+
ruby2ruby (~> 1.2)
|
27
|
+
ruby_parser (~> 2.0)
|
28
|
+
sexp_processor (~> 3.0)
|
29
|
+
roodi (2.1.0)
|
30
|
+
ruby_parser
|
31
|
+
rspec (2.8.0)
|
32
|
+
rspec-core (~> 2.8.0)
|
33
|
+
rspec-expectations (~> 2.8.0)
|
34
|
+
rspec-mocks (~> 2.8.0)
|
35
|
+
rspec-core (2.8.0)
|
36
|
+
rspec-expectations (2.8.0)
|
37
|
+
diff-lcs (~> 1.1.2)
|
38
|
+
rspec-mocks (2.8.0)
|
39
|
+
ruby2ruby (1.3.1)
|
40
|
+
ruby_parser (~> 2.0)
|
41
|
+
sexp_processor (~> 3.0)
|
42
|
+
ruby_parser (2.3.1)
|
43
|
+
sexp_processor (~> 3.0)
|
44
|
+
sexp_processor (3.0.10)
|
45
|
+
simplecov (0.5.4)
|
46
|
+
multi_json (~> 1.0.3)
|
47
|
+
simplecov-html (~> 0.5.3)
|
48
|
+
simplecov-html (0.5.3)
|
49
|
+
term-ansicolor (1.0.7)
|
50
|
+
yard (0.7.5)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
bundler (~> 1.0.0)
|
57
|
+
cucumber
|
58
|
+
jeweler (~> 1.8.3)
|
59
|
+
rdoc (~> 3.12)
|
60
|
+
reek (~> 1.2.8)
|
61
|
+
roodi (~> 2.1.0)
|
62
|
+
rspec (~> 2.8.0)
|
63
|
+
simplecov
|
64
|
+
yard (~> 0.7)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Bernát Kalló
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
= Rubylog - Prolog interpreter for ruby
|
2
|
+
|
3
|
+
Rubylog is an implementation of (part of) the Prolog language as a Ruby DSL. The DSL is inspired by {Jamis Buck}[http://weblog.jamisbuck.org/2006/10/28/prolog-in-ruby], and the implementation is based on {Yield Prolog}[http://yieldprolog.sourceforge.net/].
|
4
|
+
|
5
|
+
== Installing
|
6
|
+
$ gem install rubylog
|
7
|
+
|
8
|
+
== Using
|
9
|
+
|
10
|
+
To get started you have to do three things: require rubylog, include +Rubylog::DSL::Constants+ and declare some functors on a class you want to use:
|
11
|
+
require 'rubylog'
|
12
|
+
include Rubylog::DSL::Constants
|
13
|
+
Symbol.rubylog_functor :likes, :drinks, :has
|
14
|
+
|
15
|
+
A Rubylog clause can be created by sending the functor to the first argument, passing the other arguments:
|
16
|
+
:John.likes :beer # likes(john,beer) in Prolog
|
17
|
+
|
18
|
+
A variable is an undefined constant. Don't-care variables start with ANY...
|
19
|
+
A, B, Cat # the same in Prolog
|
20
|
+
ANY, ANYONE, ANYTHING # _ in Prolog
|
21
|
+
|
22
|
+
Now you can start writing Rubylog predicates. Facts can be asserted with a bang:
|
23
|
+
:John.likes! :beer # likes(john, beer). in Prolog
|
24
|
+
:John.has! :beer # has(john, beer). in Prolog
|
25
|
+
|
26
|
+
Rules can be asserted with +if+ and +unless+:
|
27
|
+
X.drinks(Y).if X.has(Y).and X.likes(Y) # drinks(X,Y) :- has(X,Y), likes(X,Y). in Prolog
|
28
|
+
|
29
|
+
Queries can be proved with a question mark or with +true?+:
|
30
|
+
John.drinks? :beer # => true
|
31
|
+
(John.drinks :beer).true? # => true
|
32
|
+
|
33
|
+
Or solutions can be enumerated (clauses include Enumerable)
|
34
|
+
(John.drinks X).each {|x| p x} # outputs :beer
|
35
|
+
(John.drinks X).to_a # => [:beer]
|
36
|
+
|
37
|
+
At most places you can mix native Ruby with Rubylog by using a proc instead of a clause. Blocks are automatically converted to proc objects:
|
38
|
+
X.drinks(Y).if proc{|x,y| y.to_s =~ /^Z/ }
|
39
|
+
X.drinks(Y).if {|x,y| y.to_s =~ /^Z/ } # the same
|
40
|
+
|
41
|
+
X.drinks(Y).if X.likes(Y).and proc { Time.now.hour == 12 }
|
42
|
+
X.drinks(Y).if X.likes(Y).and { Time.now.hour == 12 } # the same
|
43
|
+
|
44
|
+
Variable values are passed to the proc, in the order of appearance.
|
45
|
+
|
46
|
+
Nullary predicates are just symbols:
|
47
|
+
John.drinks(:beer).if :true.or :false
|
48
|
+
|
49
|
+
For predicates requiring a simple value as an argument, you can pass a proc that returns that value
|
50
|
+
X.drinks(Y).if Y.is proc{|x| $favorites[x] }
|
51
|
+
X.drinks(Y).if Y.is {|x| $favorites[x] } # the same
|
52
|
+
|
53
|
+
|
54
|
+
The Prolog built-in predicates and the Rubylog equivalents (see +Rubylog::Builtins+ for details):
|
55
|
+
|
56
|
+
<table>
|
57
|
+
<tr><th>Prolog</th><th>Rubylog</th></tr>
|
58
|
+
<tr><td>+true+</td><td>+:true+</td></tr>
|
59
|
+
<tr><td>+fail+</td><td>+:fail+</td></tr>
|
60
|
+
<tr><td>+,+</td><td>+and+, +&+</td></tr>
|
61
|
+
<tr><td>+;+</td><td>+or+, +|+</td></tr>
|
62
|
+
<tr><td>+!+</td><td>+:cut+</td></tr>
|
63
|
+
<tr><td>+->+</td><td>+then+</td></tr>
|
64
|
+
<tr><td><tt>\\\+</tt></td><td>+is_false+, +fails+, +not+, +~+</td></tr>
|
65
|
+
<tr><td>+repeat+</td><td>+repeat+</td></tr>
|
66
|
+
<tr><td>+A=B+</td><td>+A.is B+</td></tr>
|
67
|
+
<tr><td><tt>X is Y*5</tt></td><td><tt>X.is{|y|y*5}</tt></td></tr>
|
68
|
+
<tr><td></td><td>+proc{|x,y|y===x}+ or +X.matches Y+</td></tr>
|
69
|
+
<tr><td>+L=[H|T]+</td><td>+L.splits_to(H,T)+</td></tr>
|
70
|
+
<tr><td>+member(A,L)+</td><td>+A.in L+ (+L+ can be any enumerable)</td></tr>
|
71
|
+
<tr><td>+write(A)+</td><td>+A._puts+, +A._print+, +A._p+</td></tr>
|
72
|
+
<tr><td>+nl+</td><td>+:nl+</td></tr>
|
73
|
+
<tr><td><tt>\\\+((A, \\\+ B))</tt></td><td>+A.all B+</td></tr>
|
74
|
+
<tr><td><tt>(A,B)->true</tt></td><td>+A.any B+</td></tr>
|
75
|
+
<tr><td><tt></tt></td><td>+A.one B+</td></tr>
|
76
|
+
<tr><td><tt>\\\+((A,B))</tt></td><td><tt>A.none B</tt></td></tr>
|
77
|
+
|
78
|
+
</table>
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
== Contributing to rubylog
|
83
|
+
|
84
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
85
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
86
|
+
* Fork the project.
|
87
|
+
* Start a feature/bugfix branch.
|
88
|
+
* Commit and push until you are happy with your contribution.
|
89
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
90
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
91
|
+
|
92
|
+
== Copyright
|
93
|
+
|
94
|
+
Copyright (c) 2012 Bernát Kalló. See LICENSE.txt for
|
95
|
+
further details.
|
96
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "rubylog"
|
18
|
+
gem.homepage = "https://github.com/cie/rubylog"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{An embedded Prolog interpreter}
|
21
|
+
gem.description = %Q{Rubylog is an embedded Prolog language and interpreter for Ruby.}
|
22
|
+
gem.email = "kallo.bernat@gmail.com"
|
23
|
+
gem.authors = ["Bernát Kalló"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'cucumber/rake/task'
|
35
|
+
Cucumber::Rake::Task.new(:features)
|
36
|
+
|
37
|
+
require 'reek/rake/task'
|
38
|
+
Reek::Rake::Task.new do |t|
|
39
|
+
t.fail_on_error = true
|
40
|
+
t.verbose = false
|
41
|
+
t.source_files = 'lib/**/*.rb'
|
42
|
+
end
|
43
|
+
|
44
|
+
require 'roodi'
|
45
|
+
require 'roodi_task'
|
46
|
+
RoodiTask.new do |t|
|
47
|
+
t.verbose = false
|
48
|
+
end
|
49
|
+
|
50
|
+
task :default => :spec
|
51
|
+
|
52
|
+
require 'yard'
|
53
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/examples/4queens.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
X.clause.if X.fact
|
3
|
+
X.clause.if X.rule
|
4
|
+
X.clause.if X.query
|
5
|
+
|
6
|
+
X.comment.if X.starts_with('%')
|
7
|
+
|
8
|
+
X.predicate.if X.procedure
|
9
|
+
|
10
|
+
X.procedure.if X.has_many(clause)
|
11
|
+
|
12
|
+
|
13
|
+
X.operator.if X.comparison_operator
|
14
|
+
X.operator.if X.arithmetic_operator
|
15
|
+
|
16
|
+
X.comparison_operator.if X.arithmetic_comparison_operator.or X.unification_style_operator
|
17
|
+
|
18
|
+
X.unification_style_operator.if X === :===
|
19
|
+
X.unification_style_operator.if X === :==
|
20
|
+
|
21
|
+
X.arithmetic_comparison_operator.if X === :<
|
22
|
+
X.arithmetic_comparison_operator.if X === :<=
|
23
|
+
X.arithmetic_comparison_operator.if X === :>
|
24
|
+
X.arithmetic_comparison_operator.if X === :>=
|
25
|
+
|
26
|
+
X.arithmetic_operator.if X === :+
|
27
|
+
X.arithmetic_operator.if X === :-
|
28
|
+
X.arithmetic_operator.if X === :-@
|
29
|
+
X.arithmetic_operator.if X === :*
|
30
|
+
X.arithmetic_operator.if X === :/
|
31
|
+
X.arithmetic_operator.if X === :%
|
32
|
+
X.arithmetic_operator.if X === :**
|
33
|
+
X.arithmetic_operator.if X === :<=>
|
34
|
+
X.arithmetic_operator.if X === :&
|
35
|
+
X.arithmetic_operator.if X === :|
|
36
|
+
X.arithmetic_operator.if X === :^
|
37
|
+
X.arithmetic_operator.if X === :~
|
38
|
+
X.arithmetic_operator.if X === :<<
|
39
|
+
X.arithmetic_operator.if X === :>>
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
X.builtin.if X === :arg
|
45
|
+
X.builtin.if X === :forget
|
46
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubylog'
|
2
|
+
|
3
|
+
class << FactorialTheory = Rubylog::Theory.new!
|
4
|
+
|
5
|
+
0.factorial! 1
|
6
|
+
N.factorial(K).if proc{|n|n>0}.and N1.is{|n|n-1}.and N1.factorial(K1).and K.is{|n,_,_,k1| n*k1}
|
7
|
+
|
8
|
+
[0,1,2,7].each do |n|
|
9
|
+
(n.factorial? K).solve{|k| puts "#{n} factorial is #{k}"}
|
10
|
+
end
|
11
|
+
|
12
|
+
Integer.include_theory self
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
|
data/examples/fp.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
This is a port of some little Haskell programs
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
zipPair :: ([a],[b]) -> [(a,b)]
|
10
|
+
-- zipPair = uncurry zip
|
11
|
+
zipPair (_,[]) = []
|
12
|
+
zipPair ([],_) = []
|
13
|
+
zipPair ((a:as), (b:bs)) = (a,b) : zipPair (as,bs)
|
14
|
+
|
15
|
+
=end
|
16
|
+
|
17
|
+
class << Rubylog::Theory.new!
|
18
|
+
|
19
|
+
rubylog_functor
|
20
|
+
|
21
|
+
# multiply 0 _ = []
|
22
|
+
# multiply n k = k : multiply (n-1) k
|
23
|
+
#
|
24
|
+
(N.times X, XS).if XS.is{|n,x| n*[x] }
|
25
|
+
|
26
|
+
|
27
|
+
# dividers n = filter (`divides` n) [1..n `div` 2]
|
28
|
+
# where a `divides` b = b `mod` a == 0
|
29
|
+
# isPerfectNumber n = n == (sum $ dividers n)
|
30
|
+
|
31
|
+
(A.divides B).if {|a,b| b % a == 0}
|
32
|
+
(A.divider_of B).if A.in{|a,b| (1..b%2)}.and A.divides B
|
33
|
+
(N.perfect_number).if DS.is{|n| (D.divider_of n).to_a }.and DS.sum N
|
34
|
+
(L.sum S).if S.is{|l| l.inject(0){|a,b|a+b}}
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
# mirrorList [] = []
|
39
|
+
# mirrorList (a:as) = mirrorList as ++ [a]
|
40
|
+
(L.mirrored M).if M.is{|l|l.reverse}
|
41
|
+
|
42
|
+
|
43
|
+
# intermix [] _ = []
|
44
|
+
# intermix [a] _ = [a]
|
45
|
+
# intermix (a:as) b = a:b:intermix as b
|
46
|
+
(L.intermix E, M).if M.is{|l,e|l[1..-1].inject [l[0]] {|a,b| a << e << b }}
|
47
|
+
# possible:
|
48
|
+
#([].intermix ANY, [])
|
49
|
+
#([A].intermix ANY, [A])
|
50
|
+
#((:|[A,AS]).intermix B, :|[A,B,L]).if (AS.intermix B, L)
|
51
|
+
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
data/examples/hello.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubylog'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
class << Rubylog::Theory.new!
|
5
|
+
Symbol.rubylog_functor :lugar_historico, :during
|
6
|
+
Integer.rubylog_functor :during
|
7
|
+
|
8
|
+
A.lugar_historico.if A.in [
|
9
|
+
:Hispania_prehistorica,
|
10
|
+
:Hispania_romana,
|
11
|
+
:Hispania_visigoda,
|
12
|
+
:Al_Andalus,
|
13
|
+
:Imperio_espanol,
|
14
|
+
:Reino_Constitucional_Espanol
|
15
|
+
]
|
16
|
+
|
17
|
+
:Hispania_prehistorica.during! [-800000,200]
|
18
|
+
A.during([X,Y]).if A.matches(Integer).and {|a,x,y| (x..y) === a}
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
def self.solve k
|
24
|
+
pp *k.solutions
|
25
|
+
end
|
26
|
+
|
27
|
+
puts
|
28
|
+
pp *(A.lugar_historico.and A.during(P).and 200.during(P)).variable_hashes
|
29
|
+
|
30
|
+
|
31
|
+
end
|
data/examples/idea.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
# ActiveSpec
|
5
|
+
user = User.new(:username => 'luke')
|
6
|
+
spec = SizeSpecification.new(6, :username)
|
7
|
+
spec.satisfied_by? user
|
8
|
+
|
9
|
+
spec = CompositeSpecification.new
|
10
|
+
spec.add_specification(SizeSpecification.new(6, :username))
|
11
|
+
spec.add_specification(CollectionSpecification.new(18..30, :age))
|
12
|
+
spec.add_specification(ConfirmationSpecification.new(:password))
|
13
|
+
spec.satisfied_by?(User.new)
|
14
|
+
#=> false
|
15
|
+
|
16
|
+
# Rubylog
|
17
|
+
user = User.new(:username => 'luke')
|
18
|
+
U.username_valid.if U.size_of(:username,6)
|
19
|
+
user.username_valid?
|
20
|
+
|
21
|
+
# ActiveSpec
|
22
|
+
spec = CollectionSpecification.new(1..10, :age)
|
23
|
+
# or validates_inclusion_of :age, :in => 1..10
|
24
|
+
spec_two = NotSpecification.new(CollectionSpecification.new(1..10, :age))
|
25
|
+
# or validates_exclusion_of :age, :in => 1..10
|
26
|
+
|
27
|
+
# Rubylog
|
28
|
+
U.age_valid.if ~ U.is_included(:age, 1..10)
|
29
|
+
|
30
|
+
# ActiveSpec
|
31
|
+
spec = ProcSpecification.new(proc{ |object|
|
32
|
+
# do something with object
|
33
|
+
# and return true or false
|
34
|
+
})
|
35
|
+
spec.satisfied_by? some_object
|
36
|
+
|
37
|
+
U.everything_valid.if {|u|
|
38
|
+
# do something w/ u and return true/false
|
39
|
+
}
|
40
|
+
|
41
|
+
# ActiveSpec
|
42
|
+
class UserSpecification < ActiveSpec::Base
|
43
|
+
requires_presence_of :username, :password
|
44
|
+
requires_size 6, :password
|
45
|
+
requires_confirmation_of :password
|
46
|
+
requires_inclusion_in 18..30, :age
|
47
|
+
end
|
48
|
+
UserSpecification.satisfied_by?(some_user)
|
49
|
+
|
50
|
+
# Rubylog
|
51
|
+
UserValidationTheory = Rubylog::Theory.new do
|
52
|
+
protected
|
53
|
+
U.invalid.if (U.username=N) & N.blank
|
54
|
+
U.invalid.if (U.password=P) & P.size=K & K.is_not 6
|
55
|
+
U.invalid.if (U.password_confirmation=C) & (U.password=P) & (K.is_not P)
|
56
|
+
U.invalid.if (U.age=A) & ~:include[18..30, A]
|
57
|
+
public
|
58
|
+
U.valid_user.if ~U.invalid
|
59
|
+
end
|
60
|
+
|
61
|
+
User.include_theory UserValidationTheory
|
62
|
+
some_user.valid_user?
|
63
|
+
|
64
|
+
|
65
|
+
# ActiveSpec
|
66
|
+
specification :valid_user do
|
67
|
+
requires_presence_of :username, :password
|
68
|
+
requires_confirmation_of :password
|
69
|
+
must_satisfy :another_specification
|
70
|
+
end
|
71
|
+
|
72
|
+
ValidUserSpecification.satisfied_by?(some_user)
|
73
|
+
|
74
|
+
# Rubylog
|
75
|
+
# app/theories/valid_user_theory.rb
|
76
|
+
theory :valid_user do
|
77
|
+
U.valid_user.if ~U.invalid
|
78
|
+
protected
|
79
|
+
U.invalid.if (U.username=N) & N.blank
|
80
|
+
U.invalid.if (U.password=P) & P.size=K & K.is_not 6
|
81
|
+
U.invalid.if (U.password_confirmation=C) & (U.password=P) & (K.is_not P)
|
82
|
+
U.invalid.if (U.age=A) & ~:include[18..30, A]
|
83
|
+
end
|
84
|
+
|
85
|
+
User.include_theory :valid_user
|
86
|
+
some_user.valid_user?
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
# ActiveSpec
|
93
|
+
class User
|
94
|
+
must_satisfy :valid_user_specification
|
95
|
+
|
96
|
+
# optional :if argument takes a symbol or a block
|
97
|
+
# and can be used for conditional evaluation of
|
98
|
+
# specifications
|
99
|
+
must_satisfy :activated_user_specification, :if => :activated?
|
100
|
+
end
|
101
|
+
|
102
|
+
user = User.new
|
103
|
+
user.satisfies_specs?
|
104
|
+
# evaluates ValidUserSpecification
|
105
|
+
user.activated = true
|
106
|
+
user.satisfies_specs?
|
107
|
+
# evaluates both specifications
|
108
|
+
|
109
|
+
|
110
|
+
# Rubylog
|
111
|
+
class User
|
112
|
+
include_theory :valid_user, :activated_user
|
113
|
+
def satisfies_specs?
|
114
|
+
!bad?
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
theory :valid_user do
|
119
|
+
U.bad.if ~U.valid_user
|
120
|
+
protected
|
121
|
+
#...
|
122
|
+
end
|
123
|
+
|
124
|
+
theory :activated_user do
|
125
|
+
U.bad.if U.activated.and ~U.valid_activated_user
|
126
|
+
protected
|
127
|
+
# ...
|
128
|
+
end
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|