20dcf89f9a
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1171 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
57 lines
2.1 KiB
Plaintext
57 lines
2.1 KiB
Plaintext
=================================================================
|
|
Logtalk - Object oriented extension to Prolog
|
|
Release 2.21.5
|
|
|
|
Copyright (c) 1998-2004 Paulo Moura. All Rights Reserved.
|
|
=================================================================
|
|
|
|
|
|
This example illustrates some variants of the "diamond problem"
|
|
(multi-inheritance conflicts and ambiguities) and its respective
|
|
solutions on Logtalk.
|
|
|
|
This classical problem can be simply described by constructing a
|
|
"diamond" of objects and inheritance links as follows:
|
|
|
|
A -- contains default definition for a predicate m/0
|
|
/ \
|
|
B C -- contains redefinitions of the predicate m/0
|
|
\ /
|
|
D -- inherits both redefinitions of the predicate m/0
|
|
|
|
As such, the object D inherits two conflicting definitions for the
|
|
predicate m/0, one from object B and one from object C. If we send
|
|
the message m/0 to object D, is ambiguous which inherited definition
|
|
should be used to answer it. Depending on the nature of the objects
|
|
A, B, C, and D, the correct answer can be the redefinition of m/0 in
|
|
object B, the redefinition m/0 in object C, or both redefinitions.
|
|
A programming language supporting multi-inheritance should provide
|
|
programming mechanisms allowing easy implementation of each possible
|
|
solution.
|
|
|
|
Note that, in the context of Logtalk, the diamond problem may occur with
|
|
prototype hierarchies, class hierarchies, protocol hierarchies, or when
|
|
using category composition.
|
|
|
|
This example deals with three variants of the diamond problem, illustrated
|
|
using prototype hierarchies:
|
|
|
|
diamond1
|
|
illustrates the inherited definition which is visible due to the
|
|
Logtalk predicate lookup algorithm
|
|
diamond2
|
|
presents a solution for making the overridden inherited definition
|
|
the visible one
|
|
diamond3
|
|
presents a solution which allows both inherited definitions to be
|
|
used in D
|
|
|
|
To load all entities in this example compile and load the loader file:
|
|
|
|
| ?- logtalk_load(loader).
|
|
|
|
To load only a specific variant, compile and load the respective metafile.
|
|
For example:
|
|
|
|
| ?- logtalk_load(diamond2).
|