2007-06-12 11:39:47 +01:00
|
|
|
================================================================
|
|
|
|
Logtalk - Open source object-oriented logic programming language
|
2007-11-06 01:50:09 +00:00
|
|
|
Release 2.30.7
|
2004-09-15 00:11:12 +01:00
|
|
|
|
2007-01-10 12:46:10 +00:00
|
|
|
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
2007-06-12 11:39:47 +01:00
|
|
|
================================================================
|
2004-09-15 00:11:12 +01:00
|
|
|
|
|
|
|
|
2004-11-29 20:36:31 +00:00
|
|
|
To load this example and for sample queries, please see the SCRIPT
|
|
|
|
file.
|
|
|
|
|
2004-09-15 00:11:12 +01:00
|
|
|
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
|