From C++ to HERCs Prolog (Hello World example)
In this example we will translate the "Hello World" program from C++ to HERCs Prolog.
This tutorial is not designed for other dialects of Prolog.
Nevertheless, it may be helpful to understand the concept of a clause in general.
Let's assume you have a file called "helloworld.cpp" with the following content:
#include <stdio.h>
void main (int args, char * * argv)
{
printf ("Hello World.\n");
}
After comilation, you will have helloworld.exe executable file, which you will trigger by typing: helloworld.exe.
With HERCs Prolog you will need to translate helloworld.cpp into helloworld.prc and then type: studio helloworld.
As HERCs Prolog is an interpreter, the compilation step does not exist.
The first translation step is in this tutorial is to copy helloworld.cpp to helloworld.prc AS IS without any change.
Now you can open helloworld.prc in the simplest text editor possible (notepad or gedit) and start editing.
Let's quickly deal with the #include directive.
In C++ #include is necessary because the printf function is declared inside stdio.h header file.
In HERCs Prolog we also need to use printf equivalent, which is defined inside studio.prc module.
#include <stdio.h> will change here into import studio.
Also, we can remove the input parameters to the main function.
They are never used in the code and HERCs Prolog does not support passing parameters. (This feature may be enabled later.)
This is how the code will look after the first modification:
import studio
void main ()
{
printf ("Hello World.\n");
}
We can also remove the function's output return type, which is void.
Our main function is not going to return anything.
In fact, instructions in Prolog never return anything anyway.
Therefore we can omit the void.
import studio
main ()
{
printf ("Hello World.\n");
}
Our equivalent of printf function is write. pp or show are also good candidates.
The difference is mostly in how texts are displayed. write does not display quotation marks.
show does not display them either and also put a new line character at the end.
pp displays everything AS IS, i.e. it will display quotation marks around texts.
Our little sample program has now just become a bit smaller.
import studio
main ()
{
write ("Hello World.\n");
}
In the next step, we will have to transform our main function from M-Expression form into an S-Expression.
The difference is in where we put the left bracket.
In M-Expression form the identifier (main) is outside the bracket.
However, in S-Expression it will be INSIDE.
Therefore main () will become (main) and write (....) will become (write .....).
And yes, HERCs Prolog used white space as a separator, so there will be no comas.
For this reason we will remove the semicolons as well.
Look what we have now:
import studio
(main)
{
(write "Hello World.\n")
}
... but this is not the end of our M-Expression to S-Expression tranformation !!!
We still have to do something about the braces. They will also have to include the signature of the function.
In other words, they will have to be moved to enclose everything. This is what we will have now:
import studio
{(main)
(write "Hello World.\n")
}
Now, once we have everything in S-Expression form, we need to change all brackets into square ones.
Congratulations, you just created a clause.
import studio
[[main]
[write "Hello World.\n"]
]
At last, we need to tranform this script into a proper module.
It requires some extra code, such as module header and footer.
The empty square bracket indicates that the module does not export any symbols.
import studio
program helloworld []
[[main]
[write "Hello World.\n"]
]
end .
Also, the convention in C++ says that the main function is the beginning of the program.
It is the main starting point.
Here we have to write it explicitly, which we do just after the end but before the final dot.
import studio
program helloworld []
[[main]
[write "Hello World.\n"]
]
end := [ [main] ] .
Please note, that program will end after displaying the "Hello World" message.
You can (if you like) start the interactive session with the interpreter by extending the starting sequence.
The instruction that starts the interactive session is [command].
You will then be able to issue instructions from the keyboard.
To stop the interactive session type the [exit] command.
import studio
program helloworld []
[[main]
[write "Hello World.\n"]
]
end := [ [main] [command] ] .
Summary
- main is a symbols
- [[main] [write "Hello World.\n"]] is a clause (attached to the main symbol).
- write is a symbol too. It has got a machine clause attached, which display the parameters.
- write is defined in studio module. Therefore you need to import studio.
- Everything is written using S-Expressions, without separators, using square brackets.
- At the end of the script you can write the starting sequence.
- ... and don't forget about the dot at the end of the script.