--- language: Logtalk contributors: - ["Paulo Moura", "http://github.com/pmoura"] filename: learnlogtalk.lgt --- 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. To keep its size reasonable, this tutorial necessarily assumes that the reader have a working knowledge of Prolog and is biased towards describing Logtalk object-oriented features. # Syntax 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. Some of the most important 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) * `<>)/2` infix operator connecting them to the lambda. Some simple examples using library meta-predicates: ``` ?- {library(metapredicates_loader)}. yes ?- meta::map([X,Y]>>(Y is 2*X), [1,2,3], Ys). Ys = [2,4,6] yes ``` Currying is also supported: ``` ?- meta::map([X]>>([Y]>>(Y is 2*X)), [1,2,3], Ys). Ys = [2,4,6] yes ``` # Macros 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: ``` :- object(source). :- public(bar/1). bar(X) :- foo(X). foo(a). foo(b). foo(c). :- end_object. ``` Let's define an hook object, saved in a `my_macros.lgt` file, that changes clauses and calls to the `foo/1` local predicate: ``` :- object(my_macros, implements(expanding)). % built-in protocol for expanding predicates term_expansion(foo(Char), baz(Code)) :- char_code(Char, Code). % standard built-in predicate goal_expansion(foo(X), baz(X)). :- end_object. ``` After loading the macros file, we can then expand our source file with it: ``` ?- logtalk_load(my_macros), logtalk_load(source, [hook(my_macros)]). yes ?- source::bar(X). X = 97 ; X = 98 ; X = 99 true ``` The Logtalk library provides support for combining hook objects using different workflows (for example, defining a pipeline of expansions). # Further information Visit the [Logtalk website](http://logtalk.org) for more information.