• Non ci sono risultati.

From Implicit Complexity to Quantitative Resource Analysis Complexity Analysis by Program Transformation

N/A
N/A
Protected

Academic year: 2021

Condividi "From Implicit Complexity to Quantitative Resource Analysis Complexity Analysis by Program Transformation"

Copied!
40
0
0

Testo completo

(1)

From Implicit Complexity to Quantitative Resource Analysis

Complexity Analysis by Program Transformation

Ugo Dal Lago

(Joint work with Martin Avanzini and Georg Moser)

PhD Open, University of Warsaw, June 25-27, 2015

(2)

Higher-Order Complexity Analysis

§

The Problem

§ Given an higher-order functional program and a first-order term M , evaluate the asymptotical time complexity of M .

§ We consider the unitary cost model.

§ The problem is undecidable in general. . .

§ . . . but decidable approximations exist.

§

Solutions

§ Type Systems.

§ Cost Functions.

§ Interpretations.

§

Automation

§ Not so much is known.

§ Tools exits, but most of them are not maintained.

(3)

Higher-Order Complexity Analysis

§

The Problem

§ Given an higher-order functional program and a first-order term M , evaluate the asymptotical time complexity of M .

§ We consider the unitary cost model.

§ The problem is undecidable in general. . .

§ . . . but decidable approximations exist.

§

Solutions

§ Type Systems.

§ Cost Functions.

§ Interpretations.

§

Automation

§ Not so much is known.

§ Tools exits, but most of them are not maintained.

(4)

Complexity Analysis of TRSs

§

The Problem

§ Given a TRS, evaluate its asymptotical time complexity.

§ First-order variation on the previous problem

§

Solutions

§ The interpretation Method.

§ Path Orders.

§ Variations on the dependency pair methods.

§ Combinations of the previous methods.

§

Automation

§ Many tools (here dubbed FOPs) exist.

§ There is a yearly complexity competition.

(5)

Complexity Analysis of TRSs

§

The Problem

§ Given a TRS, evaluate its asymptotical time complexity.

§ First-order variation on the previous problem

§

Solutions

§ The interpretation Method.

§ Path Orders.

§ Variations on the dependency pair methods.

§ Combinations of the previous methods.

§

Automation

§ Many tools (here dubbed FOPs) exist.

§ There is a yearly complexity competition.

(6)

...

Scheme

OCaml

? TRS FOPs complexity

certificate

(7)

...

Scheme

OCaml

HoCA TRS FOPs complexity

certificate

(8)

Higher-Order vs. First-Order

§

How should we translate higher-order programs into first-order ones?

§

We can use program transformations.

P x¨y xP y

§

But under which constraints?

§ They should be correct, i.e., preserve the meaning of programs.

§ They need to be complexity reflecting: xP y is not asymptotically more efficient than P .

§ It would be nice if they were complexity preserving.

§

Natural answer: Reynold’s defunctionalization.

(9)

Example: Reversing a List

(10)

Example: Plain Defunctionalisation

(11)

Example: Plain Defunctionalisation

(12)

Example: Defunctionalisation + Program

Transformations

(13)

Example: Defunctionalisation + Program

Transformations

(14)

Our Four Program Transformations

§

Inlining

§

Dead-Code Elimination

§

Instantiation

§

Uncurrying

(15)

Inlining

(16)

Inlining

(17)

Inlining

(18)

Inlining

§

We need to be careful about guaranteeing the transformation to be complexity reflecting.

§ We need to avoid “hiding” time complexity under the carpet of inlining.

§

We thus restrict to redex preserving inlining, i.e. to inlining which does not make the resulting program too efficient compared to the starting one.

§

But where and when should inlining be applied?

§ If done without care, the process can even diverge.

§

We apply inlining only if it makes a certain metric on

programs to decrease.

(19)

Inlining

§

We need to be careful about guaranteeing the transformation to be complexity reflecting.

§ We need to avoid “hiding” time complexity under the carpet of inlining.

§

We thus restrict to redex preserving inlining, i.e. to inlining which does not make the resulting program too efficient compared to the starting one.

§

But where and when should inlining be applied?

§ If done without care, the process can even diverge.

§

We apply inlining only if it makes a certain metric on

programs to decrease.

(20)

Inlining

§

We need to be careful about guaranteeing the transformation to be complexity reflecting.

§ We need to avoid “hiding” time complexity under the carpet of inlining.

§

We thus restrict to redex preserving inlining, i.e. to inlining which does not make the resulting program too efficient compared to the starting one.

§

But where and when should inlining be applied?

§ If done without care, the process can even diverge.

§

We apply inlining only if it makes a certain metric on

programs to decrease.

(21)

Inlining

§

We need to be careful about guaranteeing the transformation to be complexity reflecting.

§ We need to avoid “hiding” time complexity under the carpet of inlining.

§

We thus restrict to redex preserving inlining, i.e. to inlining which does not make the resulting program too efficient compared to the starting one.

§

But where and when should inlining be applied?

§ If done without care, the process can even diverge.

§

We apply inlining only if it makes a certain metric on

programs to decrease.

(22)

Dead Code Elimination

(23)

Dead Code Elimination

(24)

Instantiation

(25)

Instantiation

(26)

Instantation

§

We are interesting in the collecting semantics of a program.

§ For each program point (i.e., for each variable occurring in rewrite rules) which are the terms which can be substituted for it?

§

Understanding whether a specific term is part of the collecting semantics of (a variable occurrence in) a TRS is undecidable.

§

It can however be overapproximated by a control-flow analysis based on tree automata

[Jones2007,KochemsOng2011].

§ . . . which, however, needs to be tailored to our needs.

(27)

Instantation

§

We are interesting in the collecting semantics of a program.

§ For each program point (i.e., for each variable occurring in rewrite rules) which are the terms which can be substituted for it?

§

Understanding whether a specific term is part of the collecting semantics of (a variable occurrence in) a TRS is undecidable.

§

It can however be overapproximated by a control-flow analysis based on tree automata

[Jones2007,KochemsOng2011].

§ . . . which, however, needs to be tailored to our needs.

(28)

Instantation

§

We are interesting in the collecting semantics of a program.

§ For each program point (i.e., for each variable occurring in rewrite rules) which are the terms which can be substituted for it?

§

Understanding whether a specific term is part of the collecting semantics of (a variable occurrence in) a TRS is undecidable.

§

It can however be overapproximated by a control-flow analysis based on tree automata

[Jones2007,KochemsOng2011].

§ . . . which, however, needs to be tailored to our needs.

(29)

Uncurrying

(30)

Uncurrying

(31)

Strategy

(32)

Experimental Evaluation

§

HoCA has been implemented, and is available online:

http://cbr.uibk.ac.at/tools/hoca/

§

We built a testbed of around 30 higher-order programs.

(33)

Some Relevant Cases

§

HO Insertion Sort.

§ The comparison function is passed as an argument;

§ Correctly dubbed quadratic.

§

HO Merge Sort

§ A divide and conquer combinator [Bird1989];

§ FOPs can only prove it terminating.

§

Okasaki’s Parsing Combinators

§ Really exploits higher-order functions;

§ Hundreds of lines of code;

§ Again, only termination can be proved through FOPs.

(34)

Some Relevant Cases

§

HO Insertion Sort.

§ The comparison function is passed as an argument;

§ Correctly dubbed quadratic.

§

HO Merge Sort

§ A divide and conquer combinator [Bird1989];

§ FOPs can only prove it terminating.

§

Okasaki’s Parsing Combinators

§ Really exploits higher-order functions;

§ Hundreds of lines of code;

§ Again, only termination can be proved through FOPs.

(35)

Some Relevant Cases

§

HO Insertion Sort.

§ The comparison function is passed as an argument;

§ Correctly dubbed quadratic.

§

HO Merge Sort

§ A divide and conquer combinator [Bird1989];

§ FOPs can only prove it terminating.

§

Okasaki’s Parsing Combinators

§ Really exploits higher-order functions;

§ Hundreds of lines of code;

§ Again, only termination can be proved through FOPs.

(36)

Conclusions

§

We show how to effectively reduce HO analysis to FO analysis.

§

An alternative title could be: “The Art of Defunctionalisation for Complexity Analysis”.

§

Pros:

§ Outperforms, e.g., type systems, in terms of expressivity;

§ Very efficient;

§ Reveals the weaknesses of FOPs.

§

Cons:

§ Not modular.

§

Question: should we drop the “reflection” constraint?

(37)

Conclusions

§

We show how to effectively reduce HO analysis to FO analysis.

§

An alternative title could be: “The Art of Defunctionalisation for Complexity Analysis”.

§

Pros:

§ Outperforms, e.g., type systems, in terms of expressivity;

§ Very efficient;

§ Reveals the weaknesses of FOPs.

§

Cons:

§ Not modular.

§

Question: should we drop the “reflection” constraint?

(38)

Conclusions

§

We show how to effectively reduce HO analysis to FO analysis.

§

An alternative title could be: “The Art of Defunctionalisation for Complexity Analysis”.

§

Pros:

§ Outperforms, e.g., type systems, in terms of expressivity;

§ Very efficient;

§ Reveals the weaknesses of FOPs.

§

Cons:

§ Not modular.

§

Question: should we drop the “reflection” constraint?

(39)

Conclusions

§

We show how to effectively reduce HO analysis to FO analysis.

§

An alternative title could be: “The Art of Defunctionalisation for Complexity Analysis”.

§

Pros:

§ Outperforms, e.g., type systems, in terms of expressivity;

§ Very efficient;

§ Reveals the weaknesses of FOPs.

§

Cons:

§ Not modular.

§

Question: should we drop the “reflection” constraint?

(40)

Thank You!

Questions?

Riferimenti

Documenti correlati

Abstract-The microbial ecology of cork oak rhizosphere was investigated using the Biolog Community Level Physiological Profile (CLPP) that provides a unique metabolic

To investigate whether better classification performances can be achieved with respect to pixel and wavelet image representations previously presented, a novel ranklet

In distinct contrast to the loggerhead turtles, the first satellite tracking studies on leatherback turtles revealed that almost all individuals migrated into pelagic

Le professeur avait eu l’idée de donner comme exercice la recherche d’un arbre dans le Jardins des Plantes, il fallait se retrouver au même lieu, le même jour, à

But finding it to be the Lord Abbot, shee fell on her knees weeping, as fearing now to receive publike shame, by

There are partial functions which are not computable whenever the notion of computability is formulated by way of languages, as in counter programs and Turing

Nella sequenza del primo episodio della prima stagione di House of Cards targato Netflix, Frank Underwood, che si trova con la moglie a una festa di fine anno, si rivolge

Purtroppo è così, siamo spinti dalla grande curiosità, dalla frenesia di poter disporre rapidamente di quel che si può (un po’ anche perché la stessa tecnologia non sta ad