Frequently Used Commands

For instructions how to do the same thing in Ruby, click here.

First, you have to start the interpreter. Open your terminal window and type-in studio. It doesn't display any welcome message, so you will have the impression that nothing really happened. This means you are in and ready to talk to the interpreter.

You will always being your instruction with the left square bracket and finish it with the right square bracket. For example: [add 1 2 3 *x] or [show "Hello World"]. So, again, every sentence is enclosed by square brackets. All Prolog instructions/commands will either work or fail. If an instruction fails, you will see a "Doesn't work" message.

During the interactive session you will be entering some commands. Simply start with the square brackets, type the instruction and finish with the square bracket. Below are the most frequently used ones.

... in Ruby

In Ruby you will need to load the Prolog interpreter engine gem first and instantiate an object with it. Once the Prolog engine object is instantiated you will be able to communicate with it. You will be able to pass questions, get answers and modify the content. Everything can be done in Ruby fashion (using Ruby syntax and objects). Alternatively you can enter interactive session. Here is what you have to do:
  • require 'HERCs'
    p = HERCs::Prolog.new

    The instructions above load the Prolog interpreter gem and instantiate the Prolog engine object.
  • p.cafe enters an interactive session. You can issue prolog commands and queries here and finish by typing [exit].
  • p.list This method shows you what prolog modules are loaded into the Prolog engine object.
  • p.load "module_name.prc" This method allows you to load a new module into the Prolog engine object. Please note, some factory-made predefined modules are stored internally within the gem itself. Therefore p.load "help.prc" does not require an external help.prc file to be present.
  • p.res [.......] This method allows you to issue prolog commands and queries to the Prolog engine object. This is done using Ruby's native syntax and objects. For example: p.res [:list] will have exactly the same result as p.list. To get some results you will need to pass some Prolog variables. Unfortunately, Ruby hasn't got any entity similar to Prolog's variables, therefore you will need to use a convention. All Prolog variables are denoted by Ruby's symbols beginning with x, therefore you will have: :x0, :x1, :x2 .... and so on. For example: p.res [:add, 1, 2, 3, :x0] will return 6 to the Ruby layer. Of course, you can pass entire compounded commands/queries as well. Try this: p.res [:computer-do-this, [:add, 1, 2, 3, :x0], [:show, :x0]].