Skip to content
cie edited this page Mar 5, 2013 · 23 revisions

Installing

First, install the gem

$ gem install rubylog

or, if you use +bundler+, add this line to your +Gemfile+:

gem 'rubylog', '~>1.0.0'

Creating a theory

In Rubylog, all logical predicates are part of a theory. You can create a theory with the theory method:

theory do
  # your code here
end

Creating structures

The basic building element of a Rubylog program is a structure. It consists of a functor (which is a symbol) and some arguments (which can be any object), and it represent a logical statement. For example, in the statement "John likes beer", "likes" would be the functor and "John" and "beer" are the arguments.

While in Prolog, structures are written in prefix notation likes('John','beer'), in Rubylog they are written in infix notation 'John'.likes('beer'), with the functor between the first and the second argument. (Syntactically it is a method call with 'John' as a receiver.)

To be able to use a functor you have to declare it, for the first argument's class.

theory do
  functor_for String, :likes
  p 'John'.likes('beer')   # outputs "John".likes("beer")
end

As you can see, structures have a programmer-readable string representation.

Asserting facts and rules

The theory has an internal database of known true statements (which are called predicates). There are two type of predicates: facts and rules. A fact states that something is true, a rule states that something is true if some condition holds.

You can assert facts with the bang version of the functor or with assert:

theory do
  functor_for String, :likes
  'John'.likes!('beer')
  assert 'John'.likes('beer')
end

You can assert a fact with the if method of a structure, or with the assert method.

theory do
  functor_for String, :likes, :drinks
  'John'.drinks('beer').if 'John'.likes('beer')
  assert 'John'.drinks('beer'), 'John'.likes('beer')
end
Clone this wiki locally