Orientation des objets Prolog pour les programmeurs Python

One of the best kept secrets in Prolog programming, is the fact that the ISO module standard can be used for object oriented programming. To convey the idea how this is done, we will here compare the approach with Python. This is an easy 4 step tutorial with links to further information at the end.

Prolog Single Dispatch

The Pythonesk self-parameter is a convention to allow a procedure to act as an object method. Analogously a Prolog object receiver can be accessed as the first parameter of a Prolog predicate. Python has a convenience that the dot operator “.” is overloaded, and that it can now be used to invoke a method.

Example (Python):

class Account:
    def deposit(self, amount):
         ...

myaccount.deposit(1499)

In the Jekejeke Prolog runtime library the Pythonesk self-parameter approach has been adopted as well. Overloading dot operator “.” is not feasible for the Prolog programming language since it doesn’t have types. Therefore we decided to introduce a new explicit message sending operator (::)/2.

Example (Prolog):

:- module(account, [deposit/2]).
deposit(Self, Amount) :-
         ...

MyAccount::deposit(1499)

The operator is similar to the module invocation operator (:)/2. The later operator can be used for an ordinary predicate call inside a module. The new operator uses some apparatus of the old operator but additionally places the object receiver as the first argument. In other Prolog systems like SWI-Prolog the operator can be bootstrapped as follows, but we provide it natively:

SWI-Prolog Bootstrapping:

O::M :-
   functor(O, C, _),
   M =.. [F|L],
   G =.. [F,O|L],
   C:G.

Prolog Predicate Default

The most surprising thing is the fact that the ISO module directive reexport/1 can be used for inheritance. But currently a Prolog system will differ from Python, in that overriding will be made explicit. This is unlike python where we can override without warning:

Example (Python):

class Parent():
def show(self):
         ...

class Child(Parent):
def show(self):
         ... super().show() ...

The Jekejeke Prolog runtime library issues a warning when it sees some overriding. Unless the corresponding predicate clauses are preceded by the override directive. SWI-Prolog shows a similar warning, but doesn't have and override directive. In SWI-Prolog reexport/2 with an except/1 option would be needed:

Example (Prolog):

:- module(parent, [show/1]).
show(Self) :-
         ...

:- module('child', [show/1]).
:- reexport(parent).
:- override show/1.
show(Self) :-
         ... parent:show(Self) ...

A predicate might want to call the overridden predicate. Python provides the super() construct to allow calling an overridden method without late binding. The ISO module standard allows doing the same by explicitly mentioning the super module in a qualified call. The qualified call will require the Pythonesk convention in that it will have to mention the receiver object.

Prolog Slot Access

The classes are Prolog modules approach comes handy. Ordinary Prolog terms can play the role of immutable value objects and partake in Prolog object oriented programming based on the Pythonesk self-parameter convention. Python objects can be created by mentioning the class name and an objects lifecycle ends with a delete.

Example (Python):

    ref = myclass(arg1, arg2, ...)
    del(ref)

Prolog objects can be directly written down as ordinary Prolog terms. The ordinary Prolog terms has an associated module which is the functor of the atom or compound. It is not necessary to explicitly delete a Prolog object, it subject to the ordinary Prolog term lifecycle.

Example (Prolog):

    Ref = mymodule(arg1, arg2, ...)

Because classes are Prolog modules, the functor of a Prolog term defines its class and the arguments of a Prolog term define its state. The state can be accessed by ISO core standard predicates such as arg/3. Jekejeke Prolog provides a further predicate set_arg/4 which can be bootstrapped in SWI-Prolog as follows, but is provided natively in out system:

SWI-Prolog Bootstrapping:

set_arg(N, C, A, D) :-
   C =.. [F|L],
   nth1(N, L, _, H),
   nth1(N, R, A, H),
   D =.. [F|R].

The Dogs Example

We demonstrate how ordinary Prolog terms might carry some state and serve as Prolog instances of Prolog classes. This is a convenient way to have a multitude of instances without the need to record the instance relationship. Further in principle Prolog terms do not require more than the ISO core standard for their construction and/or deconstruction: image004.png The class beagle has a parent class dog. We place some common functionality for all dogs into the module dog. In particular we will provide a predicate name/2 that will access the name of a dog. The predicate name/2 is then used in the predicate bark/1, displaying the name of the dog when producing a message:

Module “dog”:

name(Self, Name) :-
   arg(1, Self, Name).

bark(Self) :-
   Self::barking(Barking),
   Self::name(Name),
   write(Name), write(' says '), write(Barking), write('.'), nl.

There is the default implementation of barking/1 that returns "woof" as the dog voice. But the dog type "rottweiler" overrides barking/1 and returns "ruff". Here is what running the bark/1 method from within the top-level of our Prolog system will give:

Examples:

?- example02/beagle(sally)::bark.
sally says woof.

?- example02/beagle(coco)::bark.
coco says woof.

The module "dog" also implements a convenience method set_name/3 which uses set_arg/4 under the hood. Since Prolog terms are immutable, it will yield a new Prolog term with a changed name. Here is a screenshot of invoking the set_name/3 and the bark/1 method from within the top-level of our Prolog system: appolo.png This completes our introduction to Prolog Object Orientation for Python Programmers. There is more to say how an integration with Java is possible but we spare this for another Qiita tutorial. The links at the end of this post point to the full Jekejeke Prolog runtime library tutorial and example code.

User Manual: Advanced Programming http://www.jekejeke.ch/idatab/doclet/prod/en/docs/05_run/10_docu/06_advanced/package.html

Open Source: Advanced Examples https://github.com/jburse/jekejeke-samples/tree/master/jekrun/advanced

Recommended Posts

Orientation des objets Prolog pour les programmeurs Python
Mémorandum de base Python Partie 3-A propos de l'orientation des objets-
Une introduction à Python pour les programmeurs en langage C
python [pour moi]
Objet intégré Python
Objet intégré Python
[Français] Premiers pas avec Rust pour les programmeurs Python
Bases de Python ② pour déclaration
À propos de Python, pour ~ (plage)
[Introduction à python] Introduction rapide à Python pour les programmeurs C ++ occupés
Manuel python pour les débutants
Refactoring des outils utilisables avec Python
Orienté objet en python
Toolchain pour Python pour Android
Python: obtenir une liste de méthodes pour un objet
À propos de Python3 ... (objet Ellipsis)
OpenCV pour les débutants en Python
Installez Python (pour Windows)
[Python] pour une erreur d'instruction
Maintenance de l'environnement Python pour les projets
Articles pour guitaristes et bassistes pour bien comprendre l'orientation des objets
[Python] Contre-mesures pour "AttributeError: l'objet 'NoneType' de googletrans n'a pas d'attribut'group '"
Code Python pour l'écriture de données CSV dans le stockage d'objets DSX
[Statistiques pour les programmeurs] Théorème de Bayes
Mémo Python (pour moi): Array
[Statistiques pour les programmeurs] Box whiskers
Prise en charge de Fabric pour Python 3
Liste Python, pour instruction, dictionnaire
Python pour l'analyse des données Chapitre 4
Flux d'apprentissage pour les débutants en Python
Procédure d'installation de Python 3.6 [pour Windows]
Méthodes d'objet chaîne en Python
Intégration BigQuery pour les utilisateurs Python
[python] Valeur de l'objet fonction (?)
Plan d'apprentissage Python pour l'apprentissage de l'IA
Comparaison d'objets nuls en Python
Configurer pour Mac (Python)
Mémo Python Tkinter (pour moi)
La modernisation de l'orientation objet de Python est-elle?
Installation d'OpenCV3 pour Python3 @macOS
Mémo de code personnel Python
Balise xmp [Python] pour les photos
Construction de l'environnement Python pour Mac
Techniques de tri en Python
pp4 (puissance python pour tout)
Construction de l'environnement Python3 (pour les débutants)
Feuille de route pour la publication de packages Python
Installation de la série Python 3 pour Mac
Python #function 2 pour les super débutants
Modèle Python pour Codeforces-test manuel-
Grammaire de base Python pour les débutants
Python lancé par des programmeurs C
Notes depuis 3 mois depuis que j'ai commencé Python
Mise à jour automatique de l'application Qt pour Python
Python pour l'analyse des données Chapitre 2
Co-localisation d'objets pour la reconnaissance faciale
Variables Python et ID d'objet
Pandas 100 coups pour les débutants en Python
La recommandation de Checkio pour apprendre Python