Logtalk est un langage de programmation logique, orienté objet, sur-ensemble de Prolog.
Le langage supporte une large gamme de concepts objets :
la programmation orientée prototype, les classes, les interfaces, l'héritage multiple, la réflexion, la réutilisation de code via le concept de catégorie, le hot-patching.
Son implémentation consiste à étendre un système moderne et standard de Prolog via un script adaptateur. Par ce moyen, il supporte à ce jour :
B-Prolog 8.1 ou supérieur Ciao Prolog 1.22.0 ou supérieur (experimental) CxProlog 0.98.1 ou supérieur ECLiPSe 6.1#143 ou supérieur GNU Prolog 1.4.5 ou supérieur JIProlog 4.1.7.1 ou supérieur LVM 4.1.0 ou supérieur Quintus Prolog 3.3~3.5 (experimental) Scryer Prolog 0.9.1 ou supérieur (experimental) SICStus Prolog 4.1.0 ou supérieur SWI Prolog 6.6.0 ou supérieur Tau Prolog 0.3.2 ou supérieur Trealla Prolog 2.2.5 ou supérieur (experimental) XSB 3.8.0 ou supérieur YAP 6.3.4 ou supérieur
Exemple %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % This file is part of Logtalk <https://logtalk.org/> % Copyright 1998-2021 Paulo Moura <[email protected]> % 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. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% a protocol contains a functionality cohesive set of predicate declarations % and can be implemented by any number of objects and categories; in this % example, we start by defining a protocol declaring predicates for common % physical properties:
:- protocol(physical_properties).
:- public([ mass/1, volume/1 ]).
:- end_protocol.
% next, we define two objects, m1 and m2, implementing the protocol:
:- object(m1, implements(physical_properties)).
mass(3). volume(2.17).
:- end_object.
:- object(m2, implements(physical_properties)).
mass(4). volume(9.21).
:- end_object.
% 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
% categories can be interpreted as the dual concept of protocols, with % both aiming for functional cohesion with the difference being that % categories can both declare and define predicates
% in this example, we define a category for declaring planetary properties % and declaring and defining planetary computations; we don't want to use % an object as the gravitational_acceleration/1 predicate can only be defined % for a concrete planet
:- category(planet).
:- public([ gravitational_acceleration/1, weight/2 ]).
weight(Object, Weight) :- Object::mass(Mass), ::gravitational_acceleration(Acceleration), Weight is Mass * Acceleration.
:- end_category.
% this category can be imported by any number of objects representing actual % planets, for example, Earth and Mars:
:- object(earth, imports(planet)).
gravitational_acceleration(9.80665).
:- end_object.
:- object(mars, imports(planet)).
gravitational_acceleration(3.72076).
:- end_object.
Notes et références
Catégorie:Langage de programmation Catégorie:Langage de programmation logique Catégorie:Langage orienté objet