For a successful programming in Logtalk, you need a good working knowledge of Prolog and an understanding of the principles of object-oriented programming. All guidelines for writing good Prolog code apply as well to Logtalk programming. To those guidelines, you should add the basics of good object-oriented design.
One of the advantages of a system like Logtalk is that it enable us to use the currently available object-oriented methodologies, tools, and metrics [Champaux 92] in Prolog programming. That said, writing programs in Logtalk is similar to writing programs in Prolog: we define new predicates describing what is true about our domain objects, about our problem solution. We encapsulate our predicate directives and definitions inside new objects, categories and protocols that we create by hand with a text editor or by using the Logtalk built-in predicates. Some of the information collected during the analysis and design phases can be integrated in the objects, categories and protocols that we define by using the available entity and predicate documenting directives.
A Logtalk source file must contain only one entity, either an object, a category, or a protocol. It is recommended that each source file be named after the entity identifier. For parametric objects, the identifier arity can be appended to the identifier functor. By default, all Logtalk source files use the extension .lgt
but this is optional and can be set in the configuration files. Compiled source files (by the Logtalk preprocessor) have, by default, a .pl
extension. Again, this can be set to match the needs of a particular Prolog compiler in the corresponding configuration file. For example, we may define an object named vehicle
and save it in a vehicle.lgt
source file that will be compiled to a vehicle.pl
Prolog file. If we have a sort(_)
parametric object we can save it on a sort1.lgt
source file that will be compiled to a sort1.pl
Prolog file. This name scheme helps avoid file name conflicts (remember that all Logtalk entities share the same name space).
Any Logtalk source file can contain arbitrary directives and clauses before the opening entity directive. These directives and clauses will not be compiled by the Logtalk preprocessor and will be copied unchanged to the beginning of the corresponding Prolog output file. This feature is included to help the integration of Logtalk with other Prolog extensions such as, for example, constraint programming extensions.
Sometimes is useful to be able to define several entities in the same source file. This can be accomplished by using Logtalk source metafiles. Logtalk source metafiles are identified by the extension .mlgt
. A metafile can be interpreted as the concatenation of source files. Logtalk compiles a source metafile by automatically splitting it into individual entity source files. In addition, two helper files for compiling and loading the extracted entity files are generated. The names of these helper files are the concatenation of the metafile name with the sufixes _load
and _compile
. Both helper files use the extension .lgt
. For example, splitting a metafile named my_program.mlgt
results in helper files named my_program_compile.mlgt
and my_program_load.lgt
.
Source metafiles may be compiled and loaded in the same way as single-entity source files by using the Logtalk built-in predicates logtalk_compile/1-2
and logtalk_load/1-2
(omitting the .mlgt
file name extension). For example, assuming a metafile named my_program.mlgt
, the following call:
:- logtalk_compile(my_program).
results in the compilation of all extracted entity files. Likewise, the call:
:- logtalk_load(my_program).
results in the compilation and loading of all extracted entity files. If compilation options are given (through the use of the predicates logtalk_compile/2
or logtalk_load/2
), these will be used in the compilation and loading of each extracted entity file.
Currently, only limited error checking is performed during extraction of individual source files from a source metafile. The extraction process is guided by the occurrence of opening and closing entity directives, with all terms being copied to the generated files without further analysis.
As with source files, clauses and directives occurring before an entity opening directive are copied unchanged to the extracted entity source file. In the unlikely event that a source metafile does not contain any entity definitions, any predicate clauses and directives it might contain are copied, unchanged, to the generated loading helper file (but not to the generated compiling helper file).
Most examples directories contain a Logtalk utility file that can be used to load all included source files. These loader utility files are usually named loader.lgt
or contain the word "loader" in their name. Loader files are compiled and loaded like any ordinary Logtalk source file. For an example loader file named loader.lgt
we would type:
| ?- logtalk_load(loader).
Usually these files contain a call to the Logtalk built-in predicates logtalk_load/1
or logtalk_load/2
, wrapped inside an initialization/1
directive. For instance, if your code is split in three Logtalk source files named source1.lgt
, source2.lgt
, and source3.lgt
, then the contents of your loader file could be:
:- initialization( logtalk_load([ source1, source2, source3])).
Another example of directives that are often used in a loader file would be op/3
directives declaring global operators needed by your application.
To take the best advantage of loader files, assert a clause to the dynamic predicate logtalk_library_path/2
for the directory containing your source files, as explained in the next section.
Logtalk defines a library simply as a directory containg source files. Library locations can be specified by asserting clauses to the dynamic predicate logtalk_library_path/2
. For example:
| ?- assertz(logtalk_library_path(shapes, '$LOGTALKHOME/examples/shapes/')).
This allows us to load a library source file without the need to first change the current working directory to the library directory and then back to the original directory. For example, in order to load a loader.lgt
file, contained in a library named shapes
, we just need to type:
| ?- logtalk_load(shapes(loader)).
The best way to take advantage of this feature is to load at startup a source file containing an initialization/1
directive which asserts all the logtalk_library_path/2
clauses needed for all available libraries. This allows us to load library source files or entire libraries without worrying about libraries paths, improving code portability.
Unfortunately, a few Prolog compilers do not support the <library>(<entity>)
notation. In this case, you will need to set the working directory to be the one that contains the entity file in order to load it.
Logtalk is compatible with almost all modern Prolog compilers. However, this does not necessarily imply that your Logtalk programs will have the same level of portability. If possible, you should only use in your programs Logtalk built-in predicates and ISO Prolog defined built-in predicates. If you need to use built-in predicates that may not be available in other Prolog compilers, you should try to encapsulate the non-portable code in a minimum of objects and provide a portable interface for that code through the use of Logtalk protocols. An example will be code that access operating-system specific features. The Logtalk compiler can warn you of the use of non-ISO defined built-in predicates by using the portability/1
compiler flag.
Try to write objects and protocol documentation before writing any other code; if you are having trouble documenting a predicate perhaps we need to go back to the design stage.
Try to avoid lengthy hierarchies. Besides performance penalties, composition is often a better choice over inheritance for defining new objects (Logtalk supports component-based programming through the use of categories). In addition, prototype-based hierarchies are conceptually simpler and more efficient than class-based hierarchies.
Dynamic predicates or dynamic entities are sometimes needed, but we should always try to minimize the use of non-logical features like destructive assignment (asserts and retracts).
Since each Logtalk entity is independently compiled, if an object inherits a dynamic or a metapredicate predicate, then we must repeat the respective directives in order to ensure a correct compilation.
In general, Logtalk does not verify if a user predicate call/return arguments comply with the declared modes. On the other hand, Logtalk built-in predicates, built-in methods and message sending control structures are carefully checked for calling mode errors.
Logtalk error handling strongly depends on the ISO compliance of the chosen Prolog compiler. For instance, the error terms that are generated by some Logtalk built-in predicates assume that the Prolog built-ins behave as defined in the ISO standard regarding error conditions. In particular, if your Prolog compiler does not support a read_term/3
built-in predicate compliant with the ISO Prolog Standard definition, then the current version of the Logtalk preprocessor will not be able to detect misspell variables in your source code.
Logtalk, as an object-oriented extension to Prolog, shares with it the same preferred areas of application but also extends them with those areas where object-oriented features provide an advantage compared to plain Prolog. Among these areas we have: