C3 linearization


In computing, C3 superclass linearization is an algorithm used primarily to obtain the order in which methods should be inherited in the presence of multiple inheritance. In other words, the output of C3 superclass linearization is a deterministic Method Resolution Order.
C3 superclass linearization results in three important properties:
It was first published at the 1996 OOPSLA conference, in a paper entitled "A Monotonic Superclass Linearization for Dylan". It was adapted to the Open Dylan implementation in January 2012 following an enhancement proposal. It has been chosen as the default algorithm for method resolution in Python 2.3, Raku, Parrot,, Solidity, and PGF/TikZ's Object-Oriented Programming module. It is also available as an alternative, non-default MRO in the core of Perl 5 starting with version 5.10.0. An extension implementation for earlier versions of Perl 5 named Class::C3 exists on CPAN.
Python's Guido van Rossum summarizes C3 superclass linearization thusly:

Description

The C3 superclass linearization of a class is the sum of the class plus a unique merge of the linearizations of its parents and a list of the parents itself. The list of parents as the last argument to the merge process preserves the local precedence order of direct parent classes.
The merge of parents' linearizations and parents list is done by selecting the first head of the lists which does not appear in the tail of any of the lists. Note, that a good head may appear as the first element in multiple lists at the same time, but it is forbidden to appear anywhere else. The selected element is removed from all the lists where it appears as a head and appended to the output list. The process of selecting and removing a good head to extend the output list is repeated until all remaining lists are exhausted. If at some point no good head can be selected, because the heads of all remaining lists appear in any one tail of the lists, then the merge is impossible to compute due to inconsistent orderings of dependencies in the inheritance hierarchy and no linearization of the original class exists.
A naive divide and conquer approach to computing the linearization of a class may invoke the algorithm recursively to find the linearizations of parent classes for the merge-subroutine. However, this will result in an infinitely looping recursion in the presence of a cyclic class hierarchy. To detect such a cycle and to break the infinite recursion, the recursive invocation should be shielded against re-entrance of a previous argument by means of a cache or memoization.
This algorithm is similar to finding a topological ordering.

Example

Given
class O
class A extends O
class B extends O
class C extends O
class D extends O
class E extends O
class K1 extends A, B, C
class K2 extends D, B, E
class K3 extends D, A
class Z extends K1, K2, K3
the linearization of Z is computed as
L := // the linearization of O is trivially the singleton list , because O has no parents
L := + merge // the linearization of A is A plus the merge of its parents' linearizations with the list of parents...
= + merge
= //...which simply prepends A to its single parent's linearization
L := // linearizations of B, C, D and E are computed similar to that of A
L :=
L :=
L :=
L := + merge, L, L // first, find the linearizations of K1's parents, L, L, and L, and merge them with the parent list
= + merge // class A is a good candidate for the first merge step, because it only appears as the head of the first and last lists
= + merge // class O is not a good candidate for the next merge step, because it also appears in the tails of list 2 and 3; but class B is a good candidate
= + merge // class C is a good candidate; class O still appears in the tail of list 3
= + merge // finally, class O is a valid candidate, which also exhausts all remaining lists
=
L := + merge, L, L
= + merge // select D
= + merge // fail O, select B
= + merge // fail O, select E
= + merge // select O
=
L := + merge, L
= + merge // select D
= + merge // fail O, select A
= + merge // select O
=
L := + merge, L, L
= + merge // select K1
= + merge // fail A, select K2
= + merge // fail A, fail D, select K3
= + merge // fail A, select D
= + merge // select A
= + merge // select B
= + merge // select C
= + merge // fail O, select E
= + merge // select O
= // done

Example demonstrated in Python 3

First, a metaclass to enable a short representation of the objects by name instead of, for example, :

class Type:
def __repr__:
return cls.__name__
class O: pass

Then we construct the inheritance tree.

class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
class K1: pass
class K2: pass
class K3: pass
class Z: pass

And now:

>>> Z.mro

Example demonstrated in Raku

uses C3 linearization for classes by default:

class A
class B
class C
class D
class E
class K1 is A is B is C
class K2 is D is B is E
class K3 is D is A
class Z is K1 is K2 is K3
say Z.^mro; # OUTPUT: )