Logtalk



Realtalk ni Logtalk. 967 likes 43 talking about this. Logtalk is an object-oriented logic programming language that extends and leverages Prolog with modern code encapsulation and code reuse mechanisms without compromising its declarative programming features. Logtalk is implemented in highly portable code and can use most modern and standards compliant Prolog implementations as a back-end compiler. Take a look to the multi-threading examples in the current Logtalk development version, especially to the 'primes' example. If you chose to use the latest development version of Logtalk to take advantage of the multi-threading features, be sure to use the latest development versions of SWI-Prolog or YAP.

Logtalk uses standard Prolog syntax with the addition of a few operators and directives for a smooth learning curve and wide portability. One important consequence is that Prolog code can be easily encapsulated in objects with little or no changes. Moreover, Logtalk can transparently interpret most Prolog modules as Logtalk objects.

The main operators are:

  • ::/2 - sending a message to an object
  • ::/1 - sending a message to self (i.e. to the object that received the message being processed)
  • ^^/1 - super call (of an inherited or imported predicate)

Wwe 2k11 for pc. Some of the most important entity and predicate directives will be introduced in the next sections.

Logtalk provides objects, protocols, and categories as first-class entities. Relations between entities define patterns of code reuse and the roles played by the entities. For example, when an object instantiates another object, the first object plays the role of an instance and the second object plays the role of a class. An extends relation between two objects implies that both objects play the role of prototypes, with one of them extending the other, its parent prototype.

An object encapsulates predicate declarations and definitions. Objects can be created dynamically but are usually static and defined in source files. A single source file can contain any number of entity definitions. A simple object, defining a list member public predicate:

Assuming that the code above for the list object is saved in a list.lgt file, it can be compiled and loaded using the logtalk_load/1 built-in predicate or its abbreviation, {}/1, with the file path as argument (the extension can be omitted):

In general, entities may have dependencies on entities defined in other source files (e.g. library entities). To load a file and all its dependencies, the advised solution is to define a loader file that loads all the necessary files for an application. A loader file is simply a source file, typically named loader.lgt, that makes calls to the logtalk_load/1-2built-in predicates, usually from an initialization/1 directive for portability andstandards compliance. Loader files are provided for all libraries, tools, and examples.

The ::/2 infix operator is used to send a message to an object. As in Prolog, we can backtrack for alternative solutions:

Encapsulation is enforced. A predicate can be declared public, protected, or private. It can also be local when there is no scope directive for it. For example:

Assuming the object is saved in a scopes.lgt file:

When the predicate in a message is unknown for the object (the role it plays determines the lookup procedures), we also get an error. For example:

A subtle point is that predicate scope directives specify predicate calling semantics, not definition semantics. For example, if an object playing the role of a class declares a predicate private, the predicate can be defined in subclasses and instances but can only be called in its instances from the class.

Logtalk testing

Protocols contain predicate declarations that can be implemented by any number of objects and categories:

The scope of the protocol predicates can be restricted using protected or private implementation. For example:

In fact, all entity relations (in an entity opening directive) can be qualified as public (the default), protected, or private.

An object without an instantiation or specialization relation with another object plays the role of a prototype. A prototype can extend another object, its parent prototype.

When answering a message sent to an object playing the role of a prototype, we validate the message and look for an answer first in the prototype itself and, if not found, we delegate to the prototype parents if any:

A message is valid if the corresponding predicate is declared (and the sender is within scope) but it will fail, rather then throwing an error, if the predicate is not defined. This is called the closed-world assumption. For example, consider the following object, saved in a foo.lgt file:

Loading the file and trying to call the bar/0 predicate fails as expected. Note that this is different from calling an unknown predicate, which results in an error:

Logtalk Programming Language

In order to define objects playing the role of classes and/or instances, an object must have at least an instantiation or a specialization relation with another object. Objects playing the role of meta-classes can be used when we need to see a class also as an instance. We use the following example to also illustrate how to dynamically create new objects at runtime:

When answering a message sent to an object playing the role of an instance, we validate the message by starting in its class and going up to its class superclasses if necessary. Assuming that the message is valid, then we look for an answer starting in the instance itself:

A category is a fine grained unit of code reuse, used to encapsulate a cohesive set of predicate declarations and definitions, implementing a single functionality, that can be imported into any object. A category can thus be seen as the dual concept of a protocol. In the following example, we define categories representing car engines and then import them into car objects:

Categories are independently compiled and thus allow importing objects to be updated by simple updating the imported categories without requiring object recompilation. Categories also provide runtime transparency. I.e. the category protocol adds to the protocol of the objects importing the category:

Categories can be also be used for hot-patching objects. A category can add new predicates to an object and/or replace object predicate definitions. For example, consider the following object:

Assume that the object prints the wrong string when sent the message p/0:

If the object source code is not available and we need to fix an application running the object code, we can simply define a category that fixes the buggy predicate:

After compiling and loading the category into the running application we will now get:

As hot-patching forcefully breaks encapsulation, there is a complements compiler flag that can be set (globally or on a per-object basis) to allow, restrict, or prevent it.

Objects and categories can be parameterized by using as identifier a compound term instead of an atom. Object and category parameters are logical variables shared with all encapsulated predicates. An example with geometric circles:

Parametric objects are used just as any other object, usually providing values for the parameters when sending a message:

Parametric objects also provide a simple way of associating a set of predicates with a plain Prolog predicate. Prolog facts can be interpreted as parametric object proxies when they have the same functor and arity as the identifiers of parametric objects. Handy syntax is provided to for working with proxies. For example, assuming the following clauses for a circle/2 predicate:

With these clauses loaded, we can easily compute for example a list with the areas of all the circles:

The {Goal}::Message construct proves Goal, possibly instantiating any variables in it, and sends Message to the resulting term.

Logtalk supports event-driven programming by allowing defining events and monitors for those events. An event is simply the sending of a message to an object. Interpreting message sending as an atomic activity, a before event and an after event are recognized. Event monitors define event handler predicates, before/3 and after/3, and can query, register, and delete a system-wide event registry that associates events with monitors. For example, a simple tracer for any message being sent using the ::/2 control construct can be defined as:

Assuming that the tracer object and the list object defined earlier are compiled and loaded, we can observe the event handlers in action by sending a message:

Events can be set and deleted dynamically at runtime by calling the define_events/5 and abolish_events/5 built-in predicates.

Event-driven programming can be seen as a form of computational reflection. But note that events are only generated when using the ::/2 message-sending control construct.

Logtalk supports lambda expressions. Lambda parameters are represented using a list with the (>>)/2 infix operator connecting them to the lambda. Some simple examples using library meta-predicates:

Currying is also supported:

Lambda free variables can be expressed using the extended syntax {Free1, ..}/[Parameter1, ..]>>Lambda.

Terms and goals in source files can be expanded at compile time by specifying a hook object that defines term-expansion and goal-expansion rules. For example, consider the following simple object, saved in a source.lgt file:

LogtalkSwi prolog logtalk

Assume the following hook object, saved in a my_macros.lgt file, that expands clauses and calls to the foo/1 local predicate:

Logtalk Download

After loading the macros file, we can then expand our source file with it using the hook compiler flag:

The Logtalk library provides support for combining hook objects using different workflows (for example, defining a pipeline of expansions).

Visit the Logtalk website for more information.

Got a suggestion? A correction, perhaps? Open an Issue on the Github Repo, or make a pull request yourself!

Logtalk Intellij

Datedif function excel for mac 2016. Originally contributed by Paulo Moura, and updated by 1 contributor(s).