LibraryReExportIsNPComplete

From APIDesign

Jump to: navigation, search


Reusing libraries produced by others is essential aspect of DistributedDevelopment. It simplifies Time To Market, it reduces long term cost of ownership and leads to creation of good technologies. However it does not come for free. Read details or directly jump to the implications that shall improve your every day development habits.

This page starts by describing a way to convert any 3SAT problem to a solution of finding whether there is a way to satisfy all dependencies of a library in a repository of libraries. Thus proving that the later problem is NP-Complete. Then it describes the importance of such observations on our development practices.

There are similar observations for other module systems (RPM and Debian, see the external references section), with almost identical proof. The only difference is that both RPM and Debian allow easy way to specify negation by use of obsolete directive (thus it is easy to map the 3SAT formula). The unique feature of this proof is that it does not need negation at all. Instead it deals with re-export of an API. As re-export of APIs is quite common in software development, it brings implications of this kind of problem closer to reality.

Contents

3SAT

The problem of satisfying a logic formula remains NP-complete even if all expressions are written in wikipedia::conjunctive normal form with 3 variables per clause (3-CNF), yielding the 3SAT problem. This means the expression has the form:

Failed to parse (Can't write to or create math temp directory): (x_{11} \vee x_{12} \vee x_{13}) \wedge
Failed to parse (Can't write to or create math temp directory): (x_{21} \vee x_{22} \vee x_{23}) \wedge
Failed to parse (Can't write to or create math temp directory): (x_{31} \vee x_{32} \vee x_{33}) \wedge
Failed to parse (Can't write to or create math temp directory): ...
Failed to parse (Can't write to or create math temp directory): (x_{n1} \vee x_{n2} \vee x_{n3})

where each Failed to parse (Can't write to or create math temp directory): x_{ab}

is a variable Failed to parse (Can't write to or create math temp directory): v_i
or a negation of a variable Failed to parse (Can't write to or create math temp directory): \neg v_i

. Each variable Failed to parse (Can't write to or create math temp directory): v_i

can appear multiple times in the expression.

Library Versioning Terminology

Let Failed to parse (Can't write to or create math temp directory): A, B, C, ...

denote various modules and their APIs.

Let Failed to parse (Can't write to or create math temp directory): A_{1.0}, A_{1.1}, A_{1.7}, A_{1.11}

denote compatible versions of module Failed to parse (Can't write to or create math temp directory): A

.

Let Failed to parse (Can't write to or create math temp directory): A_{1.0}, A_{2.0}, A_{3.1}

denote incompatible versions of module Failed to parse (Can't write to or create math temp directory): A

.

Let Failed to parse (Can't write to or create math temp directory): A_{x.y} > B_{u.v}

denote the fact that version x.y of module A depends on version u.v of module B.

Let Failed to parse (Can't write to or create math temp directory): A_{x.y} \gg B_{u.v}

denote the fact that version x.y of module A depends on version u.v of module B and that it re-exports module B's API to users of own API.

Let Repository Failed to parse (Can't write to or create math temp directory): R=(M,D)

be any set of modules with their various versions and their dependencies on other modules with or without re-export.

Let C be a Configuration in a repository Failed to parse (Can't write to or create math temp directory): R=(M,D) , if Failed to parse (Can't write to or create math temp directory): C \subseteq M , where following is satisfied:

  1. re-exported dependency is satisfied with some compatible version: Failed to parse (Can't write to or create math temp directory): \forall A_{x.y} \in C, \forall A_{x.y} \gg B_{u.v} \in D \Rightarrow \exists w >= v \wedge B_{u.w} \in C
  1. each dependency is satisfied with some compatible version: Failed to parse (Can't write to or create math temp directory): \forall A_{x.y} \in C, \forall A_{x.y} > B_{u.v} \in D \Rightarrow \exists w >= v \wedge B_{u.w} \in C
  1. each imported object has just one meaning for each importer: Let there be two chains of re-exported dependencies Failed to parse (Can't write to or create math temp directory): A_{p.q} \gg ... \gg B_{x.y}
and Failed to parse (Can't write to or create math temp directory): A_{p.q} \gg ... \gg B_{u.v}
then Failed to parse (Can't write to or create math temp directory): x = u \wedge y = v


Module Dependency Problem

Let there be a repository Failed to parse (Can't write to or create math temp directory): R=(M,D)

and a module Failed to parse (Can't write to or create math temp directory): A \in M

. Does there exist a configuration Failed to parse (Can't write to or create math temp directory): C

in the repository Failed to parse (Can't write to or create math temp directory): R

, such that the module Failed to parse (Can't write to or create math temp directory): A \in C , e.g. the module can be enabled?

Conversion of 3SAT to Module Dependencies Problem

Let there be 3SAT formula with with variables Failed to parse (Can't write to or create math temp directory): v_1, ..., v_m

as defined above.

Let's create a repository of modules Failed to parse (Can't write to or create math temp directory): R . For each variable Failed to parse (Can't write to or create math temp directory): v_i

let's create two modules Failed to parse (Can't write to or create math temp directory): M^i_{1.0}
and Failed to parse (Can't write to or create math temp directory): M^i_{2.0}

, which are mutually incompatible and put them into repository Failed to parse (Can't write to or create math temp directory): R .

For each formula Failed to parse (Can't write to or create math temp directory): (x_{i1} \vee x_{i2} \vee x_{i3})

let's create a module Failed to parse (Can't write to or create math temp directory): F^i

that will have three compatible versions. Each of them will depend on one variable's module. In case the variable is used with negation, it will depend on version 2.0, otherwise on version 1.0. So for formula 
Failed to parse (Can't write to or create math temp directory): v_a \vee \neg v_b \vee \neg v_c

we will get:

Failed to parse (Can't write to or create math temp directory): F^i_{1.1} \gg M^a_{1.0}
Failed to parse (Can't write to or create math temp directory): F^i_{1.2} \gg M^b_{2.0}
Failed to parse (Can't write to or create math temp directory): F^i_{1.3} \gg M^c_{2.0}

All these modules and dependencies are added into repository Failed to parse (Can't write to or create math temp directory): R


Now we will create a module Failed to parse (Can't write to or create math temp directory): T_{1.0}

that depends on all formulas: 
Failed to parse (Can't write to or create math temp directory): T_{1.0} \gg F^1_{1.0}
Failed to parse (Can't write to or create math temp directory): T_{1.0} \gg F^2_{1.0}
...
Failed to parse (Can't write to or create math temp directory): T_{1.0} \gg F^n_{1.0}

and add this module as well as its dependencies into repository Failed to parse (Can't write to or create math temp directory): R .

Claim: There Failed to parse (Can't write to or create math temp directory): \exists C

(a configuration) of repository Failed to parse (Can't write to or create math temp directory): R
and Failed to parse (Can't write to or create math temp directory): T_{1.0} \in C
Failed to parse (Can't write to or create math temp directory): \Longleftrightarrow
there is a solution to the 3SAT formula.

Proof

"Failed to parse (Can't write to or create math temp directory): \Leftarrow ": Let's have an evaluation of each variable to either true or false that evaluates the whole 3SAT formula to true. Then

Failed to parse (Can't write to or create math temp directory): C = \{ T_{1.0} \} \bigcup
Failed to parse (Can't write to or create math temp directory): \{ M^i_{1.0} : v_i \} \bigcup \{M^i_{2.0} : \neg v_i \} \bigcup
Failed to parse (Can't write to or create math temp directory): \{ F^i_{1.1} : x_{i1} \} \bigcup \{ F^i_{1.2} : \neg x_{i1} \wedge x_{i2} \} \bigcup \{ F^i_{1.3} : \neg x_{i1} \wedge \neg x_{i2} \wedge x_{i3} \}


It is clear from the definition that each Failed to parse (Can't write to or create math temp directory): M^i

and Failed to parse (Can't write to or create math temp directory): F^i
can be in the Failed to parse (Can't write to or create math temp directory): C
just in one version. Now it is important to ensure that each module is present always at least in one version. This is easy for Failed to parse (Can't write to or create math temp directory): M^i
as its Failed to parse (Can't write to or create math temp directory): v_i
needs to be true or false, and that means one of Failed to parse (Can't write to or create math temp directory): M^i_{1.0}
or  Failed to parse (Can't write to or create math temp directory): M^i_{2.0}
will be included. Can there be a Failed to parse (Can't write to or create math temp directory): F^i
which is not included? Only if Failed to parse (Can't write to or create math temp directory): \neg x_{i1} \wedge \neg x_{i2} \wedge \neg x_{i3}
but that would mean the whole 3-or would evaluate to false and as a result also the 3SAT formula would evaluate to false. This means that dependencies of Failed to parse (Can't write to or create math temp directory): T_{1.0}
on Failed to parse (Can't write to or create math temp directory): F^i
modules are satisfied. Are also dependencies of every Failed to parse (Can't write to or create math temp directory): F^i_{1.q}
satisfied? From all the three versions, there is just one Failed to parse (Can't write to or create math temp directory): F^i_{1.q}

, the one its Failed to parse (Can't write to or create math temp directory): x_{iq}

evaluates to true. However Failed to parse (Can't write to or create math temp directory): x_{iq}
can either be without negation, and as such Failed to parse (Can't write to or create math temp directory): F^i_{1.q}
depends on Failed to parse (Can't write to or create math temp directory): M^j_{1.0}
which is included as Failed to parse (Can't write to or create math temp directory): v_j
is true. Or Failed to parse (Can't write to or create math temp directory): x_{iq}
contains negation, and as such Failed to parse (Can't write to or create math temp directory): F^i_{1.q}
depends on Failed to parse (Can't write to or create math temp directory): M^j_{2.0}
which is included as Failed to parse (Can't write to or create math temp directory): v_j
is false. 

"Failed to parse (Can't write to or create math temp directory): \Rightarrow ": Let's have a Failed to parse (Can't write to or create math temp directory): C

configuration satisfies all dependencies of Failed to parse (Can't write to or create math temp directory): T_{1.0}

. Can we also find positive valuation of 3SAT formula?

For Failed to parse (Can't write to or create math temp directory): i -th 3-or there is Failed to parse (Can't write to or create math temp directory): T_{1.0} \gg F^i_{1.0}

dependency which is satisfied. That means Failed to parse (Can't write to or create math temp directory): F^i_{1.1} \in C \vee F^i_{1.2} \in C \vee F^i_{1.3} \in C
- at least one version of Failed to parse (Can't write to or create math temp directory): F^i
module is present in the configuration. The one Failed to parse (Can't write to or create math temp directory): F^i
that has the satisfied dependency reexports Failed to parse (Can't write to or create math temp directory): M^j_{1.0}
(which means Failed to parse (Can't write to or create math temp directory): v_j = true

) or Failed to parse (Can't write to or create math temp directory): M^j_{2.0}

(which means Failed to parse (Can't write to or create math temp directory): v_j = false

). Anyway each Failed to parse (Can't write to or create math temp directory): i

3-or evaluates to Failed to parse (Can't write to or create math temp directory): true

.

The only remaining question is whether a Failed to parse (Can't write to or create math temp directory): C

configuration can force truth variable Failed to parse (Can't write to or create math temp directory): v_j
to be true in one 3-or and false in another. However that would mean that there is re-export via Failed to parse (Can't write to or create math temp directory): T_{1.0} \gg F^i_{1.x} \gg M^j_{1.0}
and also another one via Failed to parse (Can't write to or create math temp directory): T_{1.0} \gg F^p_{1.u} \gg M^j_{2.0}

. However those two chain of dependencies ending in different versions of Failed to parse (Can't write to or create math temp directory): M^j

cannot be in one Failed to parse (Can't write to or create math temp directory): C
as that breaks the last condition of configuration definition (each imported object has just one meaning). Thus each Failed to parse (Can't write to or create math temp directory): M^j
is represented only by one version and each Failed to parse (Can't write to or create math temp directory): v_j
is evaluated either to true or false, but never both.

The 3SAT formula's evaluation based on the configuration Failed to parse (Can't write to or create math temp directory): C

is consistent and satisfies the formula.

qed.

Polemics

One of the critiques raised during the LtU review (linked in external sources) is that the kind of situation cannot happen in practise. Surprisingly it can. OSGi and its RangeDependencies lead naturally into NP-Complete problems. Read more...

Implications

If there is a repository of modules in various (incompatible) versions, with mutual dependencies and re-export of their APIs, then deciding whether some of them can be enabled is NP-complete problem.

As NP-complete problems are hard to solve, it is usually our best desire to avoid them in real life situations. What does that mean in case one decides to practise DistributedDevelopment (and it is inevitable that software for 21st century needs this development style)? If you want to avoid headaches with finding the right configuration of various version of the libraries that allows to execute them together, then stick with following simple rules.

Be Compatible!

If you develop your own libraries in backward compatible way, you can always select the most recent version of each library. That is the configuration you are looking for. It is easy to find (obviously) and also it is the most desirable, as it delivers the most modern features and bugfixes that users of such libraries want.

Reuse with Care!

If you happen to reuse libraries (and you should because reuse lowers Time To Market, just like it did for me when I was publishing my first animated movie), then choose such libraries that can be trusted to evolve compatibly.

Hide Incompatibilities!

If you happen to reuse a library that cannot be trusted to keep its BackwardCompatibility, then do whatever you can to not re-export its APIs! This has been discussed in Chapter 10, Cooperating with Other APIs, but in short: If you hide such library for internal use and do not export any of its interfaces, you can use whatever version of library you want (even few years older) and nobody shall notice. Moreover in many module systems there can even be multiple versions of the same library in case they are not re-exported.

Explicit Re-export

Looks like there is a way to eliminate the NP-Completeness by disabling implicit re-export. See LibraryWithoutImplicitExportIsPolynomial. However this works only in a system with standardized versioning policy and without use of RangeDependencies.

Conclusion

Avoid complexities and NP-complete problems. Learn to develop in backward compatible way. Reading TheAPIBook is a perfect entry point into such compatible software design of the 21st century.

External Links

  1. discussion at Lambda the Ultimate
  2. LtU guys pointed out that the proof has already been published: D. Burrows, Modelling and Resolving Software Dependencies
  3. Equinox is said to use SAT4J solver
  4. EDOS Project seems to find similar proof: See section 3.2 in edos-wp2d1.pdf


Personal tools