rubylog 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +38 -33
- data/VERSION +1 -1
- data/rubylog.gemspec +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -7,32 +7,35 @@ Rubylog is an implementation of (part of) the Prolog language as a Ruby DSL. The
|
|
7
7
|
|
8
8
|
== Using
|
9
9
|
|
10
|
-
To get started you have to do three things: require rubylog, include
|
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
11
|
require 'rubylog'
|
12
12
|
include Rubylog::DSL::Constants
|
13
13
|
Symbol.rubylog_functor :likes, :drinks, :has
|
14
14
|
|
15
15
|
A Rubylog clause can be created by sending the functor to the first argument, passing the other arguments:
|
16
|
-
:
|
16
|
+
:john.likes :beer # likes(john,beer) in Prolog
|
17
17
|
|
18
18
|
A variable is an undefined constant. Don't-care variables start with ANY...
|
19
19
|
A, B, Cat # the same in Prolog
|
20
20
|
ANY, ANYONE, ANYTHING # _ in Prolog
|
21
21
|
|
22
22
|
Now you can start writing Rubylog predicates. Facts can be asserted with a bang:
|
23
|
-
:
|
24
|
-
:
|
23
|
+
:john.likes! :beer # likes(john, beer). in Prolog
|
24
|
+
:john.has! :beer
|
25
|
+
|
26
|
+
Fact assertions return the first argument, so they can be chained:
|
27
|
+
:john.likes!(:beer).has!(:beer)
|
25
28
|
|
26
29
|
Rules can be asserted with +if+ and +unless+:
|
27
30
|
X.drinks(Y).if X.has(Y).and X.likes(Y) # drinks(X,Y) :- has(X,Y), likes(X,Y). in Prolog
|
28
31
|
|
29
32
|
Queries can be proved with a question mark or with +true?+:
|
30
|
-
|
31
|
-
(
|
33
|
+
:john.drinks? :beer # => true
|
34
|
+
(:john.drinks :beer).true? # => true
|
32
35
|
|
33
36
|
Or solutions can be enumerated (clauses include Enumerable)
|
34
|
-
(
|
35
|
-
(
|
37
|
+
(:john.drinks X).each {|x| p x} # outputs :beer
|
38
|
+
(:john.drinks X).to_a # => [:beer]
|
36
39
|
|
37
40
|
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
41
|
X.drinks(Y).if proc{|x,y| y.to_s =~ /^Z/ }
|
@@ -44,7 +47,7 @@ At most places you can mix native Ruby with Rubylog by using a proc instead of a
|
|
44
47
|
Variable values are passed to the proc, in the order of appearance.
|
45
48
|
|
46
49
|
Nullary predicates are just symbols:
|
47
|
-
|
50
|
+
:john.drinks(:beer).if :true.or :false
|
48
51
|
|
49
52
|
For predicates requiring a simple value as an argument, you can pass a proc that returns that value
|
50
53
|
X.drinks(Y).if Y.is proc{|x| $favorites[x] }
|
@@ -53,34 +56,36 @@ For predicates requiring a simple value as an argument, you can pass a proc that
|
|
53
56
|
|
54
57
|
The Prolog built-in predicates and the Rubylog equivalents (see +Rubylog::Builtins+ for details):
|
55
58
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
59
|
+
Prolog Rubylog
|
60
|
+
----------------------------------------
|
61
|
+
true true
|
62
|
+
fail fail
|
63
|
+
, and, &
|
64
|
+
; or, |
|
65
|
+
! :cut
|
66
|
+
-> then
|
67
|
+
\+ is_false, fails, not, ~
|
68
|
+
repeat repeat
|
69
|
+
A=B A.is B
|
70
|
+
X is Y*5 X.is{|y|y*5}
|
71
|
+
proc{|x,y|y===x} or X.matches Y
|
72
|
+
L=[H|T] L.splits_to(H,T)
|
73
|
+
member(A,L) A.in L (L can be any enumerable)
|
74
|
+
write(A) A._puts, A._print, A._p
|
75
|
+
nl nl
|
76
|
+
\+((A, \+ B)) A.all B
|
77
|
+
(A,B)->true A.any B
|
78
|
+
A.one B
|
79
|
+
\+((A,B)) A.none B
|
80
|
+
A,fail;true A.all
|
81
|
+
A->true A.any
|
82
|
+
A.one
|
83
|
+
\+(A) A.none
|
84
|
+
|
79
85
|
|
80
86
|
|
81
87
|
|
82
88
|
== Contributing to rubylog
|
83
|
-
|
84
89
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
85
90
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
86
91
|
* Fork the project.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/rubylog.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubylog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
9
|
+
- 1
|
10
10
|
segments_generated: true
|
11
|
-
version: 0.0.
|
11
|
+
version: 0.0.1
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- "Bern\xC3\xA1t Kall\xC3\xB3"
|