| 1 | ================================================================ |
|---|
| 2 | Logtalk - Open source object-oriented logic programming language |
|---|
| 3 | Release 2.35.0 |
|---|
| 4 | |
|---|
| 5 | Copyright (c) 1998-2009 Paulo Moura. All Rights Reserved. |
|---|
| 6 | Logtalk is free software. You can redistribute it and/or modify |
|---|
| 7 | it under the terms of the "Artistic License 2.0" as published by |
|---|
| 8 | The Perl Foundation. Consult the "LICENSE.txt" file for details. |
|---|
| 9 | ================================================================ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | To load this example and for sample queries, please see the SCRIPT |
|---|
| 13 | file. |
|---|
| 14 | |
|---|
| 15 | This example illustrates some variants of the "diamond problem" |
|---|
| 16 | (multi-inheritance conflicts and ambiguities) and its respective |
|---|
| 17 | solutions on Logtalk. |
|---|
| 18 | |
|---|
| 19 | This classical problem can be simply described by constructing a |
|---|
| 20 | "diamond" of objects and inheritance links as follows: |
|---|
| 21 | |
|---|
| 22 | A -- contains default definition for a predicate m/0 |
|---|
| 23 | / \ |
|---|
| 24 | B C -- contains redefinitions of the predicate m/0 |
|---|
| 25 | \ / |
|---|
| 26 | D -- inherits both redefinitions of the predicate m/0 |
|---|
| 27 | |
|---|
| 28 | As such, the object D inherits two conflicting definitions for the |
|---|
| 29 | predicate m/0, one from object B and one from object C. If we send |
|---|
| 30 | the message m/0 to object D, is ambiguous which inherited definition |
|---|
| 31 | should be used to answer it. Depending on the nature of the objects |
|---|
| 32 | A, B, C, and D, the correct answer can be the redefinition of m/0 in |
|---|
| 33 | object B, the redefinition m/0 in object C, or both redefinitions. |
|---|
| 34 | A programming language supporting multi-inheritance should provide |
|---|
| 35 | programming mechanisms allowing easy implementation of each possible |
|---|
| 36 | solution. |
|---|
| 37 | |
|---|
| 38 | Note that, in the context of Logtalk, the diamond problem may occur with |
|---|
| 39 | prototype hierarchies, class hierarchies, protocol hierarchies, or when |
|---|
| 40 | using category composition. |
|---|
| 41 | |
|---|
| 42 | This example deals with three variants of the diamond problem, illustrated |
|---|
| 43 | using prototype hierarchies: |
|---|
| 44 | |
|---|
| 45 | diamond1 |
|---|
| 46 | illustrates the inherited definition which is visible due to the |
|---|
| 47 | Logtalk predicate lookup algorithm |
|---|
| 48 | diamond2 |
|---|
| 49 | presents a solution for making the overridden inherited definition |
|---|
| 50 | the visible one |
|---|
| 51 | diamond3 |
|---|
| 52 | presents a solution which allows both inherited definitions to be |
|---|
| 53 | used in D |
|---|