In order to automatically infer the resource consumption of programs, analyzers track how data sizes change along a program’s execution. Typically, analyzers measure the sizes of data by applying norms which are mappings from data to natural numbers that represent the sizes of the corresponding data. When norms are defined by taking type information into account, they are named typed-norms. We define a transformational approach to resource analysis with typed-norms. The analysis is based on a transformation of the program into an intermediate abstract program in which each variable is abstracted with respect to all considered norms which are valid for its type. We also sketch a simple analysis that can be used to automatically infer the required, useful, typed-norms from programs.
Risk Assessment For Installation of Drainage Pipes.pdf
A Transformational Approach to Resource Analysis with Typed-Norms
1. A Transformational Approach to Resource Analysis
with Typed-Norms
Elvira Albert1, Samir Genaim1, and Ra´ul Guti´errez2,3
1Universidad Complutense de Madrid
2Universidad Polit´ecnica de Madrid
3Universitat Polit`ecnica de Val`encia
Universidad Complutense de Madrid
June 14, 2016
Madrid, Spain
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 1 / 25
2. Motivation
Automated resource analysis needs to infer how the sizes of data are
modified along program’s execution.
Norms define how the size of term are computed (list-length,
tree-depth, term-size, . . . ).
Type-norms allow defining norms based on type information.
The choice of the right norm is crucial to obtain a proof for
termination and get accurate upper and lower bounds.
Allowing multiple norms simultaneously give us more chances to
success, but the efficiency of the analysis can be degraded
considerably.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 2 / 25
3. Main Contribution
1 We propose a transformational approach to use multiple type-norms
in resource analysis:
1 transform the program into an intermediate abstract program in which
the relation among data and control flow are preserved,
2 from such intermediate abstract program, together with a the size
relations, we obtain a cost relation system, in which each variable is
abstracted with respect to all considered norms valid for its type,
3 if a solution if obtained from the cost relation system constraints (using
a constraint solver), then the solution is transformed into upper and
lower resource bounds automatically.
2 we outline an algorithm for the inference of typed-norms which can
detect which norms are useful to later infer the resource consumption,
and discard norms that are useless for this purpose.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 3 / 25
4. Outline
1 Leading Example
2 Size Abstraction using Typed-Norms
3 Inference of Typed-Norms
4 Experiments
5 Conclusions
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 4 / 25
5. Example: A Simple Functional Language
1 def Int fibonacci(Int n)
2 = case (n <= 0) {
3 true => case n {
4 0 => 0 ;
5 1 => 1 ;
6 m => fibonacci(m − 1) + fibonacci(m − 2) ;
7 } ;
8 false => −1 ;
9 };
10
11 def List<Int> fibonacci list(List<Int> l)
12 = case l {
13 Nil => Nil ;
14 Cons(n,rest l) => Cons(fibonacci(n),fibonacci list(rest l)) ;
15 };
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 5 / 25
6. Intermediate Form: An Abstract Syntax
r ::= m(¯x, y) → g, b1, . . . , bn.
b ::= x:=t | m(¯x, y)
g ::= true | g ∧ g | e op e | match(x, t) | nonmatch(x, t)
t ::= e | Co(¯t)
e ::= x | n | e+e | e−e
where op ∈ {>, =, ≥}, m(¯x, ¯y) is the head of the rule, g specifies the
conditions for the rule to be applicable and b1, . . . , bn is the rule’s body.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 6 / 25
7. Example: Intermediate Representation
fibonacci list(xs,ys)
→ case0(xs,ys).
case0(xs,ys)
→ match(xs,Nil),
ys := Nil.
case0(xs,ys)
→ nonmatch(xs,Nil),
match(xs,Cons(n,zs)),
fibonacci(n,x),
fibonacci list(zs,ws),
ys := Cons(x,ws).
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 7 / 25
8. Outline
1 Leading Example
2 Size Abstraction using Typed-Norms
3 Inference of Typed-Norms
4 Experiments
5 Conclusions
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 8 / 25
9. Term Size Norm
t ts =
1 + n
i=1 ti ts if t = Co(t1, . . . , tn)
1 otherwise
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 9 / 25
10. Term Size Norm
t ts =
1 + n
i=1 ti ts if t = Co(t1, . . . , tn)
1 otherwise
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 9 / 25
11. Term Size Norm
t ts =
1 + n
i=1 ti ts if t = Co(t1, . . . , tn)
1 otherwise
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 9 / 25
12. Term Size Norm
t ts =
1 + n
i=1 ti ts if t = Co(t1, . . . , tn)
1 otherwise
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
O(lts × 2lts )
Complexity (Upper Bound): Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 9 / 25
13. Typed-Norms
t σ =
t σ = Int and t is an integer
length(t) σ = String and t is an string
1 +
n
i=1 ti σ if t = Co(t1, . . . , tn) and type(t) = σ
n
i=1 ti σ if t = Co(t1, . . . , tn) and type(t) = σ
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 10 / 25
14. Typed-Norms
t σ =
t σ = Int and t is an integer
length(t) σ = String and t is an string
1 +
n
i=1 ti σ if t = Co(t1, . . . , tn) and type(t) = σ
n
i=1 ti σ if t = Co(t1, . . . , tn) and type(t) = σ
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 10 / 25
15. Typed-Norms
t σ =
t σ = Int and t is an integer
length(t) σ = String and t is an string
1 +
n
i=1 ti σ if t = Co(t1, . . . , tn) and type(t) = σ
n
i=1 ti σ if t = Co(t1, . . . , tn) and type(t) = σ
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 10 / 25
16. Typed-Norms
t σ =
t σ = Int and t is an integer
length(t) σ = String and t is an string
1 +
n
i=1 ti σ if t = Co(t1, . . . , tn) and typet = σ
maxn
i=1 ti σ if t = Co(t1, . . . , tn) and typet = σ
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 11 / 25
17. Typed-Norms
t σ =
t σ = Int and t is an integer
length(t) σ = String and t is an string
1 +
n
i=1 ti σ if t = Co(t1, . . . , tn) and typet = σ
maxn
i=1 ti σ if t = Co(t1, . . . , tn) and typet = σ
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 11 / 25
18. Typed-Norms
t σ =
t σ = Int and t is an integer
length(t) σ = String and t is an string
1 +
n
i=1 ti σ if t = Co(t1, . . . , tn) and typet = σ
maxn
i=1 ti σ if t = Co(t1, . . . , tn) and typet = σ
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 11 / 25
19. Size Abstraction Transformation
Given a program P, its size abstraction Pα is a program obtained by
replacing each rule p(x, y) → g, b1, . . . , bn ∈ P by
p(X, Y ) → gα, bα
1 , . . . , bα
n where:
g1 ∧ g2 is transformed into gα
1 ∧ gα
2 ,
nonmatch(x, t) is transformed into true,
p(¯x, ¯y) is transformed into p( ¯X, ¯Y ),
match(x, t) and x := t is transformed into
∧{Xσ = t σ | σ ∈ typed norms(x)}, where typed norms(x)
corresponds to the set of types we want x to be measured,
e1 op e2 is transformed into (e1 op e2)[y/YInt] if
Int ∈ typed norms(x) and true otherwise.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 12 / 25
20. Example: Size Abstraction
fibonacci list(xs,ys)
→ case0(xs,ys).
case0(xs,ys)
→ match(xs,Nil),
ys := Nil.
case0(xs,ys)
→ nonmatch(xs,Nil),
match(xs,Cons(n,zs))
fibonacci(n,x),
fibonacci list(zs,ws),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
21. Example: Size Abstraction
fibonacci list(xs,ys)
→ case0(xs,ys).
case0(xs,ys)
→ match(xs,Nil),
ys := Nil.
case0(xs,ys)
→ nonmatch(xs,Nil),
match(xs,Cons(n,zs))
fibonacci(n,x),
fibonacci list(zs,ws),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
22. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(xs,ys)
→ match(xs,Nil),
ys := Nil.
case0(xs,ys)
→ nonmatch(xs,Nil),
match(xs,Cons(n,zs))
fibonacci(n,x),
fibonacci list(zs,ws),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
23. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
ys := Nil.
case0(xs,ys)
→ nonmatch(xs,Nil),
match(xs,Cons(n,zs))
fibonacci(n,x),
fibonacci list(zs,ws),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
{YSL = 1},
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
24. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(xs,ys)
→ nonmatch(xs,Nil),
match(xs,Cons(n,zs))
fibonacci(n,x),
fibonacci list(zs,ws),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
case0(XSI ,XSL,YSI ,YSL)
→ true,
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
25. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ match(xs,Cons(n,zs))
fibonacci(n,x),
fibonacci list(zs,ws),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
{XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
26. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(n,x),
fibonacci list(zs,ws),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
fibonacci(NI ,XI ),
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
27. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(zs,ws),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
fibonacci list(ZSI ,ZSL,WSI ,WSL),
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
28. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
ys := Cons(x,ws)
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
29. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
fibonacci(n,m)
→ x := 0,
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
fibonacci(NI ,MI )
→ {XI = 0},
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
30. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
fibonacci(NI ,MI )
→ {XI = 0},
case1(x,n,m).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
case1(XI ,NI ,MI ).
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
31. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
fibonacci(NI ,MI )
→ {XI = 0},
case1(XI ,NI ,MI ).
case1(x,n,m)
→ n ≥ x,
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
case1(XI ,NI ,MI )
→ {NI ≥ XI },
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
32. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
fibonacci(NI ,MI )
→ {XI = 0},
case1(XI ,NI ,MI ).
case1(XI ,NI ,MI )
→ {NI ≥ XI },
case2(n,m).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
case2(NI ,MI ).
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
33. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
fibonacci(NI ,MI )
→ {XI = 0},
case1(XI ,NI ,MI ).
case1(XI ,NI ,MI )
→ {NI ≥ XI },
case2(NI ,MI ).
case1(x,n,m)
→ n < x,
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
case1(XI ,NI ,MI )
→ {XI ≥ NI + 1},
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
34. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
fibonacci(NI ,MI )
→ {XI = 0},
case1(XI ,NI ,MI ).
case1(XI ,NI ,MI )
→ {NI ≥ XI },
case2(NI ,MI ).
case1(XI ,NI ,MI )
→ {XI ≥ NI + 1},
m := -1.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
{MI = −1}.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
35. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
fibonacci(NI ,MI )
→ {XI = 0},
case1(XI ,NI ,MI ).
case1(XI ,NI ,MI )
→ {NI ≥ XI },
case2(NI ,MI ).
case1(XI ,NI ,MI )
→ {XI ≥ NI + 1},
{MI = −1}.
case2(n,m)
→ match(n,0),
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
case2(NI ,MI )
→ {NI = 0},
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
36. Example: Size Abstraction
fibonacci list(XSI ,XSL,YSI ,YSL)
→ case0(XSI ,XSL,YSI ,YSL).
case0(XSI ,XSL,YSI ,YSL)
→ {XSL = 1},
{YSL = 1}.
case0(XSI ,XSL,YSI ,YSL)
→ {XSI ≥ NI , XSI ≥ ZSI ,
XSL = ZSL + 1, ZSL ≥ 1},
fibonacci(NI ,XI ),
fibonacci list(ZSI ,ZSL,WSI ,WSL),
{YSI ≥ XI , YSI ≥ WSI ,
YSL = WSL + 1, WSL ≥ 1},
fibonacci(NI ,MI )
→ {XI = 0},
case1(XI ,NI ,MI ).
case1(XI ,NI ,MI )
→ {NI ≥ XI },
case2(NI ,MI ).
case1(XI ,NI ,MI )
→ {XI ≥ NI + 1},
{MI = −1}.
case2(NI ,MI )
→ {NI = 0},
m := 0.
case2(n,m)
→ nonmatch(n,0),
match(n,1),
m := 1.
case2(n,m)
→ nonmatch(n,0),
nonmatch(n,1),
match(n,x),
y := x - 1,
fibonacci(y,z),
v := x - 2,
fibonacci(v,w),
m := z + w.
{MI = 0}.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 13 / 25
48. Example: Complexity
t σ =
t σ = Int and t is an integer
length(t) σ = String and t is an string
1 +
n
i=1 ti σ if t = Co(t1, . . . , tn) and typet = σ
maxn
i=1 ti σ if t = Co(t1, . . . , tn) and typet = σ
2 3 1 3 4 1 5 2 5 4
Example: Fibonacci List
O(lList<Int> × 2lInt )
Complexity (Upper Bound): Fibonacci List
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 14 / 25
49. Outline
1 Leading Example
2 Size Abstraction using Typed-Norms
3 Inference of Typed-Norms
4 Experiments
5 Conclusions
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 15 / 25
50. Inference of Typed-Norms
Automated resource analysis needs to infer how the sizes of data are
modified along program’s execution.
Norms define how the size of term are computed (list-length,
tree-depth, term-size, . . . ).
Type-norms allow defining norms based on type information.
The choice of the right norm is crucial to obtain a proof for
termination and get accurate upper and lower bounds.
Allowing multiple norms simultaneously give us more chances to
success, but the efficiency of the analysis can be degraded
considerably.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 16 / 25
51. Inference of Typed-Norms
Allowing multiple norms simultaneously give us more chances to
success, but the efficiency of the analysis can be degraded
considerably.
Goal: develop an analysis that eliminates useless abstractions:
Remove variables that do not affect the cost.
Remove useless (typed) size information.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 16 / 25
52. Inference of Typed-Norms: Algorithm
Initialization. This step starts by setting typed norms(x) to ∅ for each
variable x in the program. Then, it identifies the set of
cost-significant guards, and uses each such guard to modify related
typed norms(x) as follows:
If the guard is match(x, t), variable x has a type T, and T is a recursive
type, then T is added to typed norms(x).
If the guard is of the form e1 op e2, and variable x appears in e1 or e2,
then Int is added to typed norms(x).
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 17 / 25
54. Inference of Typed-Norms: Algorithm
Propagation. The initial information computed in the first step must
be propagated backwards to other variables in the program:
For match(x,t) and nonmatch(x,t), if y in vars(t), and T in
typed norms(y), then we add T to typed norms(x).
For x := t, if T in typed norms(x) we add T to typed norms(y) for
each variable y in vars(t) as far as type(y) ≤ T.
Built-in functions propagate norms to its arguments.
All other instructions do not modify any information.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 19 / 25
65. Experiments
The experiments have been performed on an Intel Core 2 Duo at 2.4GHz
with 8GB of RAM, running OS X 10.9.
Table: Run-Time statistics (in ms.) on 61 functions: (1) using term-size; (2)
using all type-norms; and (3) using only significant typed-norms. Total is
Tsa+Tac +Tub.
Configuration Tsa Av. Tsa Tac Av. Tac Tub Av. Tub Total
(1) 0 0 120 2 3911 65 4031
(2) 1633 27 631 11 14230 234 16494
(3) 2161 36 255 5 4488 74 6904
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 23 / 25
66. Outline
1 Leading Example
2 Size Abstraction using Typed-Norms
3 Inference of Typed-Norms
4 Experiments
5 Conclusions
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 24 / 25
67. Conclusions
We have presented a novel transformational approach to resource
analysis with typed-norms.
This transformation has the advantage that its formalization can be
done by only adapting the first phase of cost analysis in which the
program is transformed into an intermediate abstract program.
We also outlined an algorithm for the inference of typed-norms which
can detect automatically which norms are useful to later infer the
resource consumption.
Albert, Genaim, Guti´errez Resource Analysis with Typed-Norms UCM - Madrid 25 / 25