This file is part of Logtalk https://logtalk.org/
SPDX-FileCopyrightText: 1998-2024 Paulo Moura <pmoura@logtalk.org>
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This folder contains several examples of Logtalk programs. A brief
description of each example is included below.
Each example folder contains a NOTES.md
file and a loader helper file
(usually named loader.lgt
) that can be used to compile and load the
example source code. Most examples also contain a SCRIPT.txt
file with
instructions on how to load the example and sample queries for you to try.
Most of these examples require objects, protocols, and categories that
are defined in the Logtalk standard library or in other examples. See
the NOTES.md
files inside the library folder, plus the NOTES.md
and SCRIPT.txt
files inside each example folder.
Some examples may redefine objects already loaded from other examples.
It might be necessary to restart Logtalk after playing with some examples.
Some examples have been adapted from public Prolog code or from Prolog
text book and tutorials and are copyrighted by the respective authors.
These are programming examples, meaning that you should study the source
files to fully understand them. Note, however, that some examples main
purpose is to illustrate general principles rather than being adequate,
efficient solutions for deployment code.
All examples are indented using tabs (a common setting is a tab width
equivalent to 4 spaces).
Most example folders contain two files, tests.lgt
and tester.lgt
. The
file tests.lgt
contains unit tests for the example. These unit tests
are based on the sample queries found on the example SCRIPT.txt
file.
The file tester.lgt
is a loader file that, when loaded, will automatically
run all the example unit tests. You can automate running all these tests by
calling the logtalk_tester
shell script from the command-line (see the
scripts/NOTES.md
file for details on how to use with POSIX and Windows
operating-systems). Type `man logtalk_tester or
logtalk_tester -h` for
usage details, including how to select the backend Prolog compiler.
Follows a short description of each included example (in alphabetical order):
- [97, 99, 107]
- implementation of the Ackermann arithmetic function
- [97, 100, 118, 101, 110, 116, 117, 114, 101]
- some simple examples of text adventures
- [97, 108, 105, 97, 115, 101, 115]
- example of using the alias/2 predicate directive to provide
alternative names to inherited predicates in order to improve
readability or to solve multi-inheritance conflicts
- [97, 114, 111, 117, 110, 100, 95, 109, 101, 116, 104, 111, 100, 115]
- example of defining a complementing category that uses the
experimental @/1 goal annotation to define an "around method"
- [97, 115, 112, 101, 99, 116, 115]
- example of defining aspects (as in Aspect-Oriented Programming)
- [97, 115, 115, 105, 103, 110, 95, 112, 97, 114, 97, 109, 101, 116, 101, 114, 115]
- example of using assignable variables in the context of parametric
objects in order to represent object state
- [97, 115, 115, 117, 109, 112, 116, 105, 111, 110, 115]
- simple example of implementation of ground linear and intuitionistic
assumptions
- [97, 116, 116, 118, 97, 114, 115]
- experimental example of using attributed variables within
Logtalk objects and categories
(requires Logtalk to be run with B-Prolog, SWI-Prolog, XSB, or YAP)
- [98, 101, 110, 99, 104]
- classic set of plain Prolog benchmark programs and Logtalk wrappers
for those programs
- [98, 101, 110, 99, 104, 109, 97, 114, 107, 115]
- simple benchmarks for helping to compare the performance of Logtalk
message sending when using different backend Prolog compilers and
for comparing performance of message sending calls with predicate
calls in plain Prolog and explicitly-qualified Prolog module calls
(when applicable)
- [98, 105, 114, 100, 115]
- bird identification expert system
(example adapted from the "Adventure in Prolog" Amzi! book)
- [98, 108, 111, 99, 107, 115]
- simpler version of the
bricks
example; illustrates how to use events
to avoid breaking object encapsulation when handling object relations
- [98, 111, 111, 107, 115]
- illustrates using the optional terms library to decouple data
acquisition, which must be able to represent optional values,
from data processing, which decides how to handle those values
and their absence
- [98, 111, 116, 116, 108, 101, 115]
- 99 bottles of beer on the wall! Sing along!
- [98, 114, 105, 99, 107, 115]
- example of representation and handling of relations using events;
illustrates how to use events to avoid breaking object encapsulation
- [99, 97, 114, 101, 110, 103, 105, 110, 101, 115]
- example of extending categories using car engines
- [99, 97, 115, 99, 97, 100, 101]
- example of using expected terms to call a conjunction of goals
where any of them may cause an error condition without using the
traditional catch/throw mechanism
- [99, 108, 97, 115, 115, 109, 101, 116, 104, 111, 100, 115]
- example of defining "class methods" as found on some class-based
object-oriented programming languages
- [99, 108, 97, 115, 115, 118, 97, 114, 115]
- example of implementation of class variables
(as found in Smalltalk; i.e. shared instance variables)
- [99, 108, 111, 115, 101, 100, 95, 119, 111, 114, 108, 100, 95, 97, 115, 115, 117, 109, 112, 116, 105, 111, 110]
- example illustrating the difference between declaring a predicate
and defining a predicate and the Closed World Assumption (CWA)
semantics as implemented in Logtalk when calling predicates and
sending messages
- [99, 108, 117, 115, 116, 101, 114, 105, 110, 103]
- example of using a Java library for performing clustering
of a set of numbers
- [99, 111, 105, 110, 100, 117, 99, 116, 105, 111, 110]
- experimental example of coinductive predicates
(requires Logtalk to be run with CxProlog, ECLiPSe, SICStus Prolog,
SWI-Prolog, or YAP)
- [99, 111, 109, 112, 108, 101, 109, 101, 110, 116, 115]
- examples of using a category to explicitly complement an existing
object, either for hot patching or for adding new functionality
- [99, 111, 110, 115, 116, 114, 97, 105, 110, 116, 115]
- several examples of using constraints within objects and categories
when using constraint domain solvers found in backend Prolog compilers
- [100, 99, 103, 115]
- examples of using DCG rules inside objects and categories
- [100, 101, 98, 117, 103, 95, 104, 111, 111, 107, 115]
- simple example of using compilation hooks and term expansion for
conditional compilation of debug statements
- [100, 101, 102, 97, 117, 108, 116, 121]
- example comparing defaulty and tagged data representations
- [100, 101, 108, 101, 103, 97, 116, 101, 115]
- simple implementation of the delegation design pattern
- [100, 101, 115, 105, 103, 110, 95, 112, 97, 116, 116, 101, 114, 110, 115]
- sample implementation of common object-oriented behavioral,
creational, and structural design patterns
- [100, 105, 97, 109, 111, 110, 100, 115]
- examples of problems and solutions for the "diamond problem"
(multi-inheritance conflicts and ambiguities)
- [100, 111, 99, 117, 109, 101, 110, 116, 95, 99, 111, 110, 118, 101, 114, 116, 101, 114]
- example of using a Java library for converting documents to text
- [100, 121, 110, 112, 114, 101, 100]
- example of using the built-in database handling methods to
implement dynamic object state
- [101, 100, 99, 103, 115]
- several examples of Extended Definite Clause Grammars (EDCGs)
- [101, 110, 99, 111, 100, 105, 110, 103, 115]
- very simple example of using the experimental encoding/1
directive (requires Logtalk to be run with a backend supporting
at least UTF-8 encoding)
- [101, 110, 103, 105, 110, 101, 115]
- examples of using threaded engines to implement fluents, lazy
meta-predicates, interactors, and simple agents (requires Logtalk
to be run with a multi-threaded backend)
- [101, 114, 114, 111, 114, 115]
- example showing the Logtalk compiler warning and error reporting
for common programming errors
- [101, 120, 112, 97, 110, 115, 105, 111, 110]
- example illustrating the term and goal expansion mechanisms
- [102, 97, 109, 105, 108, 121]
- classical family relations example
- [102, 97, 109, 105, 108, 121, 95, 97, 108, 116]
- alternative version of the
family
example using a family registry
implemented using multifile predicates to avoid dynamic binding
- [110, 99, 108]
- Net-Clause Language (NCL) examples (including the
figures
individual
example of network modeling for recognizing polyhedra represented as
graphs)
- [102, 117, 116, 117, 114, 101, 115]
- simple example of working with futures, a common concurrent programming
idiom, using the high-level multi-threading predicates
- [104, 97, 105, 108, 115, 116, 111, 110, 101]
- example of computing Hailstone sequences
- [104, 97, 117, 110, 116, 101, 100, 95, 119, 97, 115, 116, 101, 108, 97, 110, 100]
- solution for the Advent of Code 2023 Day 8 problem; illustrates the
use of the
dictionaries
and grammars
libraries plus cyclic terms
- [104, 101, 108, 108, 111, 95, 119, 111, 114, 108, 100]
- the unavoidable "hello world" programming example
- [104, 111, 111, 107, 115]
- simple example of using compiler hook objects and predicates
- [105, 110, 99, 108, 117, 100, 101, 115]
- simple example of using the include/1 directive as both a
file directive and an entity directive
- [105, 110, 104, 101, 114, 105, 116, 97, 110, 99, 101]
- examples of public, protected, and private inheritance using both
prototypes and classes/instances
- [105, 110, 108, 105, 110, 105, 110, 103]
- simple example for illustrating and testing inlining of predicate
definitions
- [105, 110, 115, 116, 109, 101, 116, 104, 111, 100, 115]
- example of instance defined methods; also illustrates the use of
"super calls" to call overridden method definitions
- [105, 110, 115, 116, 118, 97, 114, 115]
- example of defining instance variables, default variable values,
and setter and getter methods
- [106, 112, 108]
- examples adapted from SWI-Prolog/YAP JPL library; illustrates how
to use the
java
library minimal abstraction for calling Java
from Logtalk using familiar message sending syntax (requires Logtalk
to be run with SWI-Prolog, YAP, or JIProlog as the backend compiler)
- [108, 97, 109, 98, 100, 97, 115]
- example of using lambda expressions
- [108, 97, 109, 98, 100, 97, 115, 95, 99, 111, 109, 112, 105, 108, 101, 100]
- pseudo-example for testing compilation of calls to library
meta-predicates with lambda expressions as meta-arguments
- [108, 97, 112, 116, 111, 112, 115]
- example of defining an object as a composition of other objects
in order to contrast with category-based composition
- [108, 111]
- examples adapted from the Francis G. McCabe L&O system
- [108, 111, 99, 97, 108, 105, 122, 97, 116, 105, 111, 110, 115]
- simple example of supporting application localization in multiple
languages
- [108, 111, 103, 105, 99]
- example of a translator of first-order predicate logic propositions
to conjunctive normal form and to clausal form
- [108, 111, 103, 115]
- example of using a category to define a simple log support for objects
- [108, 112, 97]
- examples adapted from the LPA Prolog++ system (an expert system for
automobile fault diagnosis and a timetables example)
- [109, 101, 116, 97, 99, 108, 97, 115, 115, 101, 115]
- example of using classes and metaclasses
- [109, 101, 116, 97, 105, 110, 116, 101, 114, 112, 114, 101, 116, 101, 114, 115]
- some examples of simple meta-interpreters defined as categories
that can be imported by "database" objects
- [109, 101, 116, 97, 112, 114, 101, 100, 105, 99, 97, 116, 101, 115]
- example of using meta-predicates in Logtalk objects
- [109, 101, 116, 97, 112, 114, 101, 100, 105, 99, 97, 116, 101, 115, 95, 99, 111, 109, 112, 105, 108, 101, 100]
- pseudo-example for testing compilation of calls to library
meta-predicates
- [109, 105]
- simple multi-inheritance examples
- [109, 105, 115, 99, 101, 108, 108, 97, 110, 101, 111, 117, 115]
- unsorted examples
- [109, 105, 115, 115, 105, 110, 103, 95, 100, 97, 116, 97]
- illustrates using the expected terms library to decouple data
acquisition, which must be resilient to unexpected events,
from data processing, which decides how to handle those events
- [109, 111, 100, 117, 108, 101, 115]
- simple example of compiling Prolog modules as objects
- [109, 111, 100, 117, 108, 101, 95, 97, 108, 105, 97, 115, 101, 115]
- simple examples illustrating the use of module aliases
- [109, 115, 103, 108, 111, 103]
- example of using events and monitors for recording, replaying,
and printing user messages
- [109, 117, 108, 116, 105, 102, 105, 108, 101]
- example illustrates how to use multifile predicates within
Logtalk objects and categories
- [109, 121, 95, 116, 121, 112, 101, 115]
- example of defining new types using the user-extensible
type
library object
- [110, 97, 109, 101, 100, 95, 100, 97, 116, 97, 98, 97, 115, 101, 115]
- example of an implementation of the Lean Prolog API for named
databases for Prolog compilers with a module system
- [110, 101, 111, 52, 106]
- example of using the Java library to interface with Neo4j
- [110, 111, 119, 95, 121, 111, 117, 95, 115, 101, 101, 95, 109, 101]
- example illustrating requirements dictated by inheritance semantics
for the implementation of dynamic predicates
- [111, 98, 106, 101, 99, 116, 95, 97, 108, 105, 97, 115, 101, 115]
- simple examples illustrating the use of object aliases
- [111, 112, 101, 114, 97, 116, 111, 114, 115]
- example of using operators local to objects and categories
- [112, 97, 114, 97, 109, 101, 116, 114, 105, 99]
- simple examples of parametric objects
- [112, 97, 114, 100, 105, 99, 116, 115]
- simple SWI-Prolog only example of using a dictionary term
for representing object parameters
- [112, 97, 114, 118, 97, 114, 115]
- variant of the
parametric
example using parameter variables
- [112, 97, 116, 99, 104, 101, 115]
- example of using complementing categories to patch broken
object code
- [112, 97, 116, 99, 104, 105, 110, 103]
- another example of using complementing categories to patch
broken object code
- [112, 101, 110, 103, 105, 110, 101, 115]
- simple example of using SWI-Prolog pengines from objects
- [112, 101, 111, 112, 108, 101]
- simple example of defining object constructors
- [112, 101, 114, 115, 105, 115, 116, 101, 110, 99, 121]
- illustrates a very simple solution for persisting an object
dynamic state across sessions
- [112, 108, 97, 110, 101, 116, 115]
- simple example illustrating the concepts of protocol and category
- [112, 111, 101, 109]
- examples adapted from the Ben Staveley-Taylor POEM system
- [112, 111, 105, 110, 116, 115]
- example adapted from SICStus Objects documentation; defines
a simple class hierarchy of points illustrating how to use
categories as object components
- [112, 111, 108, 121, 103, 111, 110, 115]
- example of representation and handling of relations using events
- [112, 114, 101, 100, 105, 99, 97, 116, 101, 95, 108, 111, 111, 107, 117, 112, 115]
- example illustrating the predicate declaration and predicate definition
lookup algorithms used when sending a message to an object
- [112, 114, 111, 99, 101, 115, 115, 95, 109, 111, 100, 101, 108, 105, 110, 103]
- example of using parametric objects to represent and restore
shared variables between sets of constraints that are stored
in different objects
- [112, 114, 111, 102, 105, 108, 105, 110, 103]
- examples of using of events and monitors to implement profilers
- [112, 114, 111, 116, 111, 116, 121, 112, 101, 115]
- example illustrating the concept of prototypes
- [112, 114, 111, 120, 105, 101, 115]
- example of using parametric object proxies for an efficient
representation of objects with read-only state
- [112, 117, 122, 122, 108, 101, 115]
- several examples of solving logical puzzles
- [113, 117, 105, 99, 107, 95, 99, 104, 101, 99, 107]
- example of using the QuickCheck support provided by the
lgtunit
tool both for interactive testing at the top-level interpreter and
for defining unit tests
- [114, 101, 99, 105, 112, 101, 115]
- example of a possible solution for representing structured data
and also of hot patching of running code
- [114, 101, 102, 108, 101, 99, 116, 105, 111, 110]
- example of a simple class-based reflective system
- [114, 101, 108, 97, 116, 105, 111, 110, 115]
- objects implementing predicates for dealing with relations and
constrained relations between objects; used by other examples
- [114, 111, 108, 101, 115]
- simple example illustrating the different roles that can be
played by an object
- [114, 111, 111, 116, 115]
- objects, protocols, and categories used by other examples;
illustrates how you can define object creation and abolishing
methods, complete with initialization and termination options
- [115, 99, 111, 112, 101, 115]
- simple example illustrating predicate scope semantics
- [115, 99, 114, 97, 116, 99, 104, 99, 97, 114, 100, 115]
- solution for the Advent of Code 2023 Day 4 problem; illustrates the
use of the
dictionaries
and grammars
libraries
- [115, 101, 97, 114, 99, 104, 105, 110, 103]
- state-space searching framework
(this example includes some code adapted from Ivan Bratko's "Prolog
Programming for Artificial Intelligence" book)
- [115, 101, 99, 117, 114, 101, 109, 112]
- a set of source files for testing Logtalk secure
implementation of meta-predicates
- [115, 101, 108, 102, 95, 109, 101, 115, 115, 97, 103, 101, 115]
- simple example illustrating the semantics of messages to "self"
- [115, 101, 108, 102, 95, 118, 115, 95, 115, 117, 112, 101, 114]
- simple example illustrating the semantics of calling an inherited
meta-predicate using a message to "self" versus using a "super" call
- [115, 101, 108, 102, 95, 118, 115, 95, 116, 104, 105, 115]
- simple example illustrating the difference between "self" and "this"
- [115, 101, 114, 105, 97, 108, 105, 122, 97, 116, 105, 111, 110]
- simple example of serializing objects to a file
- [115, 104, 97, 112, 101, 115]
- simple geometric shapes implemented as both a prototype hierarchy
and a class hierarchy for comparing both approaches
- [115, 105, 99, 115, 116, 117, 115]
- examples adapted from SICStus Objects documentation
- [115, 108, 105, 100, 101, 115]
- example of using the library list zippers
- [115, 117, 112, 101, 114, 95, 99, 97, 108, 108, 115]
- simple example illustrating that "super" calls preserve the value
of "self"
- [115, 121, 109, 98, 105, 111, 115, 105, 115]
- examples of using Prolog non-standard built-in meta-predicates
and module meta-predicates that take closures as arguments
- [115, 121, 109, 100, 105, 102, 102]
- example of using parametric objects to implement symbolic
expression differentiation and simplification
- [116, 97, 98, 108, 105, 110, 103]
- simple example of using tabling directives within objects
(requires Logtalk to be run with a backend supporting tabling)
- [116, 99, 108, 116, 107]
- example illustrating how to add a portable GUI to an application using Tcl
and Tk
- [116, 101, 115, 116, 105, 110, 103]
- some examples of writing unit tests
- [116, 101, 115, 116, 115, 95, 100, 115, 108]
- example illustrating how to define a Domain Specific Language (DSL)
for writing tests
- [116, 104, 114, 101, 97, 100, 115]
- several simple examples of multi-threading programming, some of
them intended only for benchmarking multi-threading Prolog compilers
(requires Logtalk to be run with a multi-threaded backend)
- [116, 114, 101, 98, 117, 99, 104, 101, 116]
- solution for the Advent of Code 2023 Day 1 problem; illustrates the
use of push-bask lists in DCGs
- [117, 110, 105, 116, 115]
- Logtalk version of a GNU Prolog/CX parametric unit example
- [118, 105, 101, 119, 112, 111, 105, 110, 116, 115]
- example on how to implement property sharing and value sharing
with prototypes
- [119, 114, 97, 112, 112, 101, 114, 115]
- example of using the
begin_of_file
term generated when compiling
a source file to define object wrappers for plain Prolog code
- [120, 112, 99, 101]
- SWI-Prolog only example of using XPCE from Logtalk