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.
- [list]
This is probably the most frequently used command at the start of every interactive session.
It simply shows you what Prolog modules (libraries) are loaded.
Most likely you will see at least user! and studio.
- [list "module_name"]
This one shows you what symbold are defined inside one specific module. Please note that this time you pass a text parameter indicating the module name.
- [list command]
This one displays how the specified command is defined.
The command here indicates a specific instruction's name.
You can try listing different commands. There are many commands defined internally at a very low level, such as mathematical operations and many others.
Therefore [list cos] will tell you only that cosine operation is defined at the machine level.
List itself is defined at the machine level. You can try [list list] and you will see a similar result.
However, proper Prolog definitions are visible. Try for example [list show] or [list command] to see some of them.
- [exit]
This command ends the interactive session.
- [load "module_name.prc"]
[import "module_name.prc"]
[consult "module_name.prc"]
These 3 commands load a new module into the Prolog interpreter.
The import command does not load the module if it is already loaded.
The load command re-loads the module if it is already loaded.
The consult command works exactly the same way as load but it also shows on the screen what is being loaded.
Prolog modules are normally stored in separate files with a dot-prc extension.
However, there are several factory-made preset modules stored internally as a resource.
Therefore you can load them even if there is no physical file in the file system.
For example: [load "help.prc"] loads a help module, where every command has got its explanation.
- [batch "script_name.prb"]
This command interprets a batch script.
A batch script is exactly the same things as recorded interactive session.
Commands, which you would have to type from the keyboard, can be stored in a batch script and executed.
The last instruction inside batch script should be [exit]
- [load "help.prc"]
This command is your friend. It contains help for all the predefined commands.
Once the help module is loaded you can use the help command.
For example, to get some help on the list command, you type [help list] and help appears on the screen.
- [help]
[help something]
Help command offers you assistance. Before using help you must load the help module [load "help.prc"].
You can ask for help for almost any predefined command.
However, is you just type [help], it will help you with help itself.
- [computer-do-this .......]
[res .....]
This is quite an important command. Everytime you ask a qustion, you will probably want some answers.
The res command allows you to combine several commands into one, nested, compound command.
The computer-do-this command is exactly the same one and it is used for demonstration purposes to show pepole how Prolog works.
Computer-do-this is less enigmatic than res.
Res is basically short version of resolution, an internal Prolog mechanism of resolving queries.
So, let's say, you want to add some numbers and see the result. Here is what you write: [res [add 1 2 3 *x] [show *x]].
Two commands (add and show) combined into one sentece. Nothing more, nothing less. In fact, you can write [res [list]]
or [res [computer-do-this [list res]]] with exactly the same result. However, you can't write
[add 1 2 3 *x] [show *x]. This wouldn't give you any meaningful answer, because add and show don't share the same context.
... 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]].