Skip to main content
gamedev.net
🔒 Locked

[CS] Dynamic dispatch checking

Started by ToohrVyk 2 replies 1k views
Original Post
ToohrVyk
ToohrVyk
This is a bit of a theoretical problem: I have a set of types and a supertype relationship between them. I also have a function signature and several implementations of that function for various argument type combinations. I need to prove that, for any combination of argument types, there exists a single nonambiguous implementation that is compatible with this combination. An example:
Types : {A, B1, B2, C1, C2 } with A > B1 > B2 and A > C1 > C2 Function signature: f(A,A) Implementations: f(A,A); f(A,B1); f(B2,A); f(C1,C1); f(C2,C2) In this example, calling f with arguments of type (B2,B2) is problematic, since f(A,A); f(A,B1); f(B2,A) are all compatible with that argument combination, but there is not a single nonambiguous choice of implementation available.
Note that ambiguity is not discussed in terms of distance, but rather in terms of lattices: an implementation f(x,y) is nonambiguous iff for each other implementation f(x',y') compatible with the arguments, x' ≥ x and y' ≥ y (that is, the implementation is better than all other implementations for each argument types). What efficient algorithms (or theoretical research) can I find about this problem?
deffer
deffer
EDIT: ignore, it turns out that it's not true...


It seems to me that there can be established a Partial Order between implementations.

f(T1,..,TN) <= f(U1,..,UN) <=> for_each(i,1<=i<=N) Ti <= Ui

Even a Strict Partial Order:
f(T1,..,TN) < f(U1,..,UN)
<=>
for_each(i,1<=i<=N) Ti <= Ui
and
exists(i,1<=i<=N) Ti < Ui


Then, the implementations are unambiguous <=> the Order forms a tree.
Am I right?


[Edited by - deffer on October 12, 2006 1:20:41 PM]
deffer
deffer
Do we take into account multiple inheritance for type hierarchy?

Types : { A, B, C, D } with A > B > D and A > C > D
ToohrVyk
ToohrVyk
Quote:
Original post by deffer
Do we take into account multiple inheritance for type hierarchy?

Types : { A, B, C, D } with A > B > D and A > C > D


Yes. You are also right about the partial order. But it's partial. And it's not even a lattice or semi-lattice. In practice, this would require a graph traversal algorithm, which is pretty simple but takes O(Tn) complexity, where T is the number of types available and n the number of arguments.

A more evolved version consists in comparing only the implementations two by two, extracting the minima of their upper bounds and checking that every one of them is also an implementation. This takes O(nTI²) complexity, where I is the number of implementations. That's quite better. However, I'm pretty sure that I can cut on both the comparison factor AND the type-search factor to reach something like O(T + log T I log I)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.