amortized heap-space analysis for first-order functional programs · amortized heap-space analysis...

68
Motivation Results Summary Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at TallinnUniv. of Technology Kalvi, 2005 Amortization for Heap Consumption

Upload: others

Post on 27-May-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortized Heap-Space Analysisfor First-Order Functional Programs

O. Shkaravska

Inst. of Cyberneticsat Tallinn Univ. of Technology

Kalvi, 2005

Amortization for Heap Consumption

Page 2: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Outline

1 MotivationAmortization-based Evaluation of Heap ConsumptionPrevious Work

2 ResultsHeap-aware Type System for Programs over ListsSoundness Theorem

Some problems are reported “on-line”...

Amortization for Heap Consumption

Page 3: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Outline

1 MotivationAmortization-based Evaluation of Heap ConsumptionPrevious Work

2 ResultsHeap-aware Type System for Programs over ListsSoundness Theorem

Some problems are reported “on-line”...

Amortization for Heap Consumption

Page 4: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Outline

1 MotivationAmortization-based Evaluation of Heap ConsumptionPrevious Work

2 ResultsHeap-aware Type System for Programs over ListsSoundness Theorem

Some problems are reported “on-line”...

Amortization for Heap Consumption

Page 5: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Practical Aspect

Heap-space deficit in run-time leads to crash.

Small devices: smartcards, mobile phones, ...

a few programs are expected to be run on one machine,

Solution: evaluate heap consumption before running programs.

Amortization for Heap Consumption

Page 6: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

What is Amortization

Given: a sequence of operations.

Find: the cost of the entire sequence.Remark:

The actual cost ti , not that important!The amortized cost ai , s.t.

� ji � 1ai � � j

i � 1ti .

Banker’s View

If ci ��� ai � ti � 0, it is called a credit.

Physicist’s View

Data: D0 ����� Di ���A Potential Function � Di ���� i � 0.

ci ��� i ��� i � 1

Amortization for Heap Consumption

Page 7: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

What is Amortization

Given: a sequence of operations.

Find: the cost of the entire sequence.Remark:

The actual cost ti , not that important!The amortized cost ai , s.t.

� ji � 1ai � � j

i � 1ti .

Banker’s View

If ci ��� ai � ti � 0, it is called a credit.

Physicist’s View

Data: D0 ����� Di ���A Potential Function � Di ���� i � 0.

ci ��� i ��� i � 1

Amortization for Heap Consumption

Page 8: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

What is Amortization

Given: a sequence of operations.

Find: the cost of the entire sequence.Remark:

The actual cost ti , not that important!The amortized cost ai , s.t.

� ji � 1ai � � j

i � 1ti .

Banker’s View

If ci ��� ai � ti � 0, it is called a credit.

Physicist’s View

Data: D0 ����� Di ���A Potential Function � Di ���� i � 0.

ci ��� i ��� i � 1

Amortization for Heap Consumption

Page 9: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

What is Amortization

Given: a sequence of operations.

Find: the cost of the entire sequence.Remark:

The actual cost ti , not that important!The amortized cost ai , s.t.

� ji � 1ai � � j

i � 1ti .

Banker’s View

If ci ��� ai � ti � 0, it is called a credit.

Physicist’s View

Data: D0 ����� Di ���A Potential Function � Di ���� i � 0.

ci ��� i ��� i � 1

Amortization for Heap Consumption

Page 10: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

What is Amortization

Given: a sequence of operations.

Find: the cost of the entire sequence.Remark:

The actual cost ti , not that important!The amortized cost ai , s.t.

� ji � 1ai � � j

i � 1ti .

Banker’s View

If ci ��� ai � ti � 0, it is called a credit.

Physicist’s View

Data: D0 ����� Di ���A Potential Function � Di ���� i � 0.

ci ��� i ��� i � 1

Amortization for Heap Consumption

Page 11: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Amortization � fine computable(!) resource bounds,resource information in types

f x � match x with Nil � cons�1 � Nil ��

cons�h � t ��� cons

�1 � cons

�2 � Nil ���

The bound is: T�length � �

�1 � length � 02 � length � 1 �

Typing: L�Int � k � � 1 � L

�Int � 0 � � 0

We assign:

1 extra heap unit before the computation,

An extra heap unit to the first element: k�1 � � 1,

Other elements do not need extras: k�i � � 0, i � 2.

Amortization for Heap Consumption

Page 12: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Amortization � fine computable(!) resource bounds,resource information in types

f x � match x with Nil � cons�1 � Nil ��

cons�h � t ��� cons

�1 � cons

�2 � Nil ���

The bound is: T�length � �

�1 � length � 02 � length � 1 �

Typing: L�Int � k � � 1 � L

�Int � 0 � � 0

We assign:

1 extra heap unit before the computation,

An extra heap unit to the first element: k�1 � � 1,

Other elements do not need extras: k�i � � 0, i � 2.

Amortization for Heap Consumption

Page 13: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Amortization � fine computable(!) resource bounds,resource information in types

f x � match x with Nil � cons�1 � Nil ��

cons�h � t ��� cons

�1 � cons

�2 � Nil ���

The bound is: T�length � �

�1 � length � 02 � length � 1 �

Typing: L�Int � k � � 1 � L

�Int � 0 � � 0

We assign:

1 extra heap unit before the computation,

An extra heap unit to the first element: k�1 � � 1,

Other elements do not need extras: k�i � � 0, i � 2.

Amortization for Heap Consumption

Page 14: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Amortization (mainly for Time) - Reading in Progress

Basic:Cormen, Leiserson, Rivest - “Introduction to algorithms”Okasaki - “Purely Functional Data Structures “

fine treatment of recursive calls(binary increment in logarithm)Okasaki: lazy-eval. with suspesnions

Schoenmakers - PhD thesis “Data Structuresand Amortized Complexity in a Functional Setting”:

algebraic approachlinear usagefine treatment of compositions/recursive callstime

Amortization for Heap Consumption

Page 15: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Amortization (mainly for Time) - Reading in Progress

Basic:Cormen, Leiserson, Rivest - “Introduction to algorithms”Okasaki - “Purely Functional Data Structures “

fine treatment of recursive calls(binary increment in logarithm)Okasaki: lazy-eval. with suspesnions

Schoenmakers - PhD thesis “Data Structuresand Amortized Complexity in a Functional Setting”:

algebraic approachlinear usagefine treatment of compositions/recursive callstime

Amortization for Heap Consumption

Page 16: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Amortization (mainly for Time) - Reading in Progress

Basic:Cormen, Leiserson, Rivest - “Introduction to algorithms”Okasaki - “Purely Functional Data Structures “

fine treatment of recursive calls(binary increment in logarithm)Okasaki: lazy-eval. with suspesnions

Schoenmakers - PhD thesis “Data Structuresand Amortized Complexity in a Functional Setting”:

algebraic approachlinear usagefine treatment of compositions/recursive callstime

Amortization for Heap Consumption

Page 17: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Amortization (mainly for Time) - Reading in Progress

Basic:Cormen, Leiserson, Rivest - “Introduction to algorithms”Okasaki - “Purely Functional Data Structures “

fine treatment of recursive calls(binary increment in logarithm)Okasaki: lazy-eval. with suspesnions

Schoenmakers - PhD thesis “Data Structuresand Amortized Complexity in a Functional Setting”:

algebraic approachlinear usagefine treatment of compositions/recursive callstime

Amortization for Heap Consumption

Page 18: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Problem: Fine Treatment of Recursive Calls

I can not type-check the increment-for-logarithm examplein the presented type system!The solution exists, but it leads to singleton types.May be there are other solutions: later ...

Amortization for Heap Consumption

Page 19: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Hofmann-Jost System for Linear Heap Bounds

Example

The program “copy”copy x � match x with

Nil � Nil�cons

�h � t ��� let y � copy t

in cons�h � y �

has typing: L�Int � 1 � � 0 � L

�Int � 0 � � 0:

assign to each element of an input list - 1 extra heap unit.

Semantics

Typing L�Int � k � � k0 � L

�Int � k � � � k �0 means

heap consumption k l�

k0,

gain k � l ��

k �0

Amortization for Heap Consumption

Page 20: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Hofmann-Jost System for Linear Heap Bounds

Example

The program “copy”copy x � match x with

Nil � Nil�cons

�h � t ��� let y � copy t

in cons�h � y �

has typing: L�Int � 1 � � 0 � L

�Int � 0 � � 0:

assign to each element of an input list - 1 extra heap unit.

Semantics

Typing L�Int � k � � k0 � L

�Int � k � � � k �0 means

heap consumption k l�

k0,

gain k � l ��

k �0

Amortization for Heap Consumption

Page 21: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Amortization-based Evaluation of Heap ConsumptionPrevious Work

Hofmann-Jost System for Linear Heap Bounds

Example

The program “copy”copy x � match x with

Nil � Nil�cons

�h � t ��� let y � copy t

in cons�h � y �

has typing: L�Int � 1 � � 0 � L

�Int � 0 � � 0:

assign to each element of an input list - 1 extra heap unit.

Semantics

Typing L�Int � k � � k0 � L

�Int � k � � � k �0 means

heap consumption k l�

k0,

gain k � l ��

k �0

Amortization for Heap Consumption

Page 22: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

What Amortization Brings to Types

Credits are type annotaions carrying resource information.

Zero-Order, Sized and Unsized, Annotated Types

T � Int�Ll�T � k � � L � T � k �

k �����

�k�i � is the credit of the i th cons-cell.� li � 1 k

�i � is the potential of a list of integers

k is a constant in HJ system.

The hint for Type-checking

Unary Functions over Lists.Let F has a bounded on ��� ��� derivaive, with 0 ���� 1.Perform type-checking for input with k

�x � � F �

�x � .

Total consumption is� l

i � 1 k�i ����� l

i ��� k�x � d x � F

�x � � F

� � �Amortization for Heap Consumption

Page 23: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

What Amortization Brings to Types

Credits are type annotaions carrying resource information.

Zero-Order, Sized and Unsized, Annotated Types

T � Int�Ll�T � k � � L � T � k �

k �����

�k�i � is the credit of the i th cons-cell.� li � 1 k

�i � is the potential of a list of integers

k is a constant in HJ system.

The hint for Type-checking

Unary Functions over Lists.Let F has a bounded on ��� ��� derivaive, with 0 ���� 1.Perform type-checking for input with k

�x � � F �

�x � .

Total consumption is� l

i � 1 k�i ����� l

i ��� k�x � d x � F

�x � � F

� � �Amortization for Heap Consumption

Page 24: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

What Amortization Brings to Types

Credits are type annotaions carrying resource information.

Zero-Order, Sized and Unsized, Annotated Types

T � Int�Ll�T � k � � L � T � k �

k �����

�k�i � is the credit of the i th cons-cell.� li � 1 k

�i � is the potential of a list of integers

k is a constant in HJ system.

The hint for Type-checking

Unary Functions over Lists.Let F has a bounded on ��� ��� derivaive, with 0 ���� 1.Perform type-checking for input with k

�x � � F �

�x � .

Total consumption is� l

i � 1 k�i ����� l

i ��� k�x � d x � F

�x � � F

� � �Amortization for Heap Consumption

Page 25: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

What Amortization Brings to Types

Credits are type annotaions carrying resource information.

Zero-Order, Sized and Unsized, Annotated Types

T � Int�Ll�T � k � � L � T � k �

k �����

�k�i � is the credit of the i th cons-cell.� li � 1 k

�i � is the potential of a list of integers

k is a constant in HJ system.

The hint for Type-checking

Unary Functions over Lists.Let F has a bounded on ��� ��� derivaive, with 0 ���� 1.Perform type-checking for input with k

�x � � F �

�x � .

Total consumption is� l

i � 1 k�i ����� l

i ��� k�x � d x � F

�x � � F

� � �Amortization for Heap Consumption

Page 26: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

What Amortization Brings to Types

Credits are type annotaions carrying resource information.

Zero-Order, Sized and Unsized, Annotated Types

T � Int�Ll�T � k � � L � T � k �

k �����

�k�i � is the credit of the i th cons-cell.� li � 1 k

�i � is the potential of a list of integers

k is a constant in HJ system.

The hint for Type-checking

Unary Functions over Lists.Let F has a bounded on ��� ��� derivaive, with 0 ���� 1.Perform type-checking for input with k

�x � � F �

�x � .

Total consumption is� l

i � 1 k�i ����� l

i ��� k�x � d x � F

�x � � F

� � �Amortization for Heap Consumption

Page 27: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Typing Judgement

Judgement�� n � �

e � T� � � n ����

– annotated contexts,T – an annotated type, n � n � – nonnegative numbers

Example – destructive length

length � x �match x with Nil � 0�

cons�h � t ��� _ � let y � length � t

in 1�y

x � Ll�Int � 0 � � 0 � �

length � x � Intx � Ll

�Int � 1 � � � 0

Amortization for Heap Consumption

Page 28: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Typing Judgement

Judgement�� n � �

e � T� � � n ����

– annotated contexts,T – an annotated type, n � n � – nonnegative numbers

Example – destructive length

length � x �match x with Nil � 0�

cons�h � t ��� _ � let y � length � t

in 1�y

x � Ll�Int � 0 � � 0 � �

length � x � Intx � Ll

�Int � 1 � � � 0

Amortization for Heap Consumption

Page 29: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Typing Judgement

Judgement�� n � �

e � T� � � n ����

– annotated contexts,T – an annotated type, n � n � – nonnegative numbers

Example – destructive length

length � x �match x with Nil � 0�

cons�h � t ��� _ � let y � length � t

in 1�y

x � Ll�Int � 0 � � 0 � �

length � x � Intx � Ll

�Int � 1 � � � 0

Amortization for Heap Consumption

Page 30: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Some Rules: Constructor

h � T � t � Ll�T � k � � k

�l

�1 � �

1 � �cons

�h � t � � Ll

�1�T � k �

h � Z�T � � t � Z

�Ll�T � k ��� � � 0

where zero-annotation map is efined incductively:

Z�Int � ��� Int ,

Z�Ll�T � k ��� ��� Ll

�Z�T � � 0 � ,

� Z � � � � � x � ��� Z� � �

x ��� .

Amortization for Heap Consumption

Page 31: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

First-Order Types and Function Call

Ll�T � k � k � � � � k0 � Ll �

�T � � k � � � k �0

� ��� �l � l � � k � k � � � k0 � k � � k �0 �� �

l � l � � k � k � � � k0 � k � � k �0 �x � Ll

�T � k � � k0 � �

f�x � � Ll �

�T � � k � �

x � Ll�T � k � � � � � k �0

l is the length of input,l � is the length of output

The predicate�

manages mutual and recursive calls.For type-checking may have, say, the form l � � p

�l � .

HJ system: no need, because annotations are constants,no dependency on the position of an element.

Amortization for Heap Consumption

Page 32: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

First-Order Types and Function Call

Ll�T � k � k � � � � k0 � Ll �

�T � � k � � � k �0

� ��� �l � l � � k � k � � � k0 � k � � k �0 �� �

l � l � � k � k � � � k0 � k � � k �0 �x � Ll

�T � k � � k0 � �

f�x � � Ll �

�T � � k � �

x � Ll�T � k � � � � � k �0

l is the length of input,l � is the length of output

The predicate�

manages mutual and recursive calls.For type-checking may have, say, the form l � � p

�l � .

HJ system: no need, because annotations are constants,no dependency on the position of an element.

Amortization for Heap Consumption

Page 33: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

First-Order Types and Function Call

Ll�T � k � k � � � � k0 � Ll �

�T � � k � � � k �0

� ��� �l � l � � k � k � � � k0 � k � � k �0 �� �

l � l � � k � k � � � k0 � k � � k �0 �x � Ll

�T � k � � k0 � �

f�x � � Ll �

�T � � k � �

x � Ll�T � k � � � � � k �0

l is the length of input,l � is the length of output

The predicate�

manages mutual and recursive calls.For type-checking may have, say, the form l � � p

�l � .

HJ system: no need, because annotations are constants,no dependency on the position of an element.

Amortization for Heap Consumption

Page 34: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

First-Order Types and Function Call

Ll�T � k � k � � � � k0 � Ll �

�T � � k � � � k �0

� ��� �l � l � � k � k � � � k0 � k � � k �0 �� �

l � l � � k � k � � � k0 � k � � k �0 �x � Ll

�T � k � � k0 � �

f�x � � Ll �

�T � � k � �

x � Ll�T � k � � � � � k �0

l is the length of input,l � is the length of output

The predicate�

manages mutual and recursive calls.For type-checking may have, say, the form l � � p

�l � .

HJ system: no need, because annotations are constants,no dependency on the position of an element.

Amortization for Heap Consumption

Page 35: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

�is complex to infer

We want to use the type system for

“parametric type-checking”

.E.g. : I expect that my program

has something like quadratic heap consumption,the task: to obtain � a x2 � � b x

� � c for heap,

and has the length of the output is linearw.r.t. the length of an input,the task: to obtain � d x

� � d � for output length.

Amortization for Heap Consumption

Page 36: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

�is complex to infer

We want to use the type system for

“parametric type-checking”

.E.g. : I expect that my program

has something like quadratic heap consumption,the task: to obtain � a x2 � � b x

� � c for heap,

and has the length of the output is linearw.r.t. the length of an input,the task: to obtain � d x

� � d � for output length.

Amortization for Heap Consumption

Page 37: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

�is complex to infer

We want to use the type system for

“parametric type-checking”

.E.g. : I expect that my program

has something like quadratic heap consumption,the task: to obtain � a x2 � � b x

� � c for heap,

and has the length of the output is linearw.r.t. the length of an input,the task: to obtain � d x

� � d � for output length.

Amortization for Heap Consumption

Page 38: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Skip it: Destructive match

�� n � �

e1 � T �� � � n ��� h � T � t � Ll � 1

�T � k � � n �

1�

k�l � � �

e2 � T ��� h � T � t � Ll � 1

�T � k � � � n k l���

the benign sharing for Match� �

�� t � Ll

�T � k � � n �

���������

match x withNil � e1�cons

�h � t ��� _ � e2

� T

�� x � Ll

�T � k � �

���������� �

n �

Amortization for Heap Consumption

Page 39: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Let: Sharing is not a Monster

Int � Int � IntLl�T1 � k1 ��� Ll

�T2 � k2 � � Ll

�T1 � T2 � k1

�k2 �

L�T1 � k1 ��� L

�T2 � k2 � � L

�T1 � T2 � k1

�k2 �� �

1 � �2 � � x � � �

1�x � x � dom

� �1 ��� dom

� �2 ��

2�x � x � dom

� �2 ��� dom

� �1 ��

1�x � � �

2�x � x � dom

� �1 ��� dom

� �2 �

Amortization for Heap Consumption

Page 40: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Skip it: Let

�1 � n � �

e1 � T0�1

� � n0�2 � x � T0 � n0 � �

e2 � T�2 � x � Z

�T0 � � � n ����

the benign sharing for Let� �

�1 � �

2 � n �����let x � e1

in e2

� T

�1 � �

2

����� � n �

Amortization for Heap Consumption

Page 41: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Some Rules: Budget

�� n � �

e � T� � � n �

n r r � n�� r � �

e � T��

� � r �

r � 0�� n � �

e � T� � � n �

�� n

�r � �

e � T� � � n ��

r

Amortization for Heap Consumption

Page 42: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Shuffle

�� x � Ll

�T � k � � n � �

e � T ��� x � Ll

�T � k � � � � n � k � k � �

�� x � Ll

�T � k � k � � � � n

� � li � 1 k � �

�i � � �

e � T ��� x � Ll

�T � k � � � � n �

Amortization for Heap Consumption

Page 43: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Some Rules: Weakening

�� n � �

e � T� � � n �

���� � n � �

e � T����

� � n �

Amortization for Heap Consumption

Page 44: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Well-defined First-Order Signature

A first-order signature � is well-definedif for any function f � dom

�� � with �

�f � �

Ll�T � k � k � � � � k0 � Ll �

�T � � k � � � k �0

� � �l � l � � k � k � � � k0 � k � � k �0 �

one can successfully type-check the body ef of f:

x � Ll�T � k � � k0 � �

ef�x � � Ll �

�T � � k � �

x � Ll�T � k � � � � � k �0

provided that� �

l � l � � k � k � � � k0 � k � � k �0 � holds.

Amortization for Heap Consumption

Page 45: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Well-defined First-Order Signature

A first-order signature � is well-definedif for any function f � dom

�� � with �

�f � �

Ll�T � k � k � � � � k0 � Ll �

�T � � k � � � k �0

� � �l � l � � k � k � � � k0 � k � � k �0 �

one can successfully type-check the body ef of f:

x � Ll�T � k � � k0 � �

ef�x � � Ll �

�T � � k � �

x � Ll�T � k � � � � � k �0

provided that� �

l � l � � k � k � � � k0 � k � � k �0 � holds.

Amortization for Heap Consumption

Page 46: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Example: Destructive Halfleaves every 2nd element of an input list

half x � match x withNil � Nil�cons

�h � t ��� _ � match t with

Nil � Nil�cons

�hh � tt ��� _ �

let y � half ttin cons

�hh � y �

has typing Ll�T � k � k � � � � 0 � Ll �

�T � k � � � 0

� �l � � p

�l � ,

where p�l � ��� l2

�and k � k ��� 0, k � ��� 1

2.

Amortization for Heap Consumption

Page 47: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Example: Destructive Halfleaves every 2nd element of an input list

half x � match x withNil � Nil�cons

�h � t ��� _ � match t with

Nil � Nil�cons

�hh � tt ��� _ �

let y � half ttin cons

�hh � y �

has typing Ll�T � k � k � � � � 0 � Ll �

�T � k � � � 0

� �l � � p

�l � ,

where p�l � ��� l2

�and k � k ��� 0, k � ��� 1

2.

Amortization for Heap Consumption

Page 48: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Example: Logarithm

log x �let y � half x

in match y with Nil � Nil�cons

�h � t � � let z � log y

in cons�1 � z �

If list x has length l , then the program frees l heap unitsbut consumes O

�log2

�l ��� .

Type-checked the credit functions k�x � � a

x, k � �

�x � � 1,

have found that a � 2.

Amortization for Heap Consumption

Page 49: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem(s): Is the Type System Refineable?

merge the “budget rules” with the syntactical ones as muchas possible, to reduce complexity of type-checking, findheuristics for the “shuffle rules”,

non-strict sizes (if-rule is restrictive, ... )???

add the number of recursive calls as a parameter forfirst-order types?

(very) dependent types for the fine “if”-rule and recursivecalls?

verify calls “in-the-context” for fine treatment ofcompositions?

Amortization for Heap Consumption

Page 50: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem(s): Is the Type System Refineable?

merge the “budget rules” with the syntactical ones as muchas possible, to reduce complexity of type-checking, findheuristics for the “shuffle rules”,

non-strict sizes (if-rule is restrictive, ... )???

add the number of recursive calls as a parameter forfirst-order types?

(very) dependent types for the fine “if”-rule and recursivecalls?

verify calls “in-the-context” for fine treatment ofcompositions?

Amortization for Heap Consumption

Page 51: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem(s): Is the Type System Refineable?

merge the “budget rules” with the syntactical ones as muchas possible, to reduce complexity of type-checking, findheuristics for the “shuffle rules”,

non-strict sizes (if-rule is restrictive, ... )???

add the number of recursive calls as a parameter forfirst-order types?

(very) dependent types for the fine “if”-rule and recursivecalls?

verify calls “in-the-context” for fine treatment ofcompositions?

Amortization for Heap Consumption

Page 52: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem(s): Is the Type System Refineable?

merge the “budget rules” with the syntactical ones as muchas possible, to reduce complexity of type-checking, findheuristics for the “shuffle rules”,

non-strict sizes (if-rule is restrictive, ... )???

add the number of recursive calls as a parameter forfirst-order types?

(very) dependent types for the fine “if”-rule and recursivecalls?

verify calls “in-the-context” for fine treatment ofcompositions?

Amortization for Heap Consumption

Page 53: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem(s): Is the Type System Refineable?

merge the “budget rules” with the syntactical ones as muchas possible, to reduce complexity of type-checking, findheuristics for the “shuffle rules”,

non-strict sizes (if-rule is restrictive, ... )???

add the number of recursive calls as a parameter forfirst-order types?

(very) dependent types for the fine “if”-rule and recursivecalls?

verify calls “in-the-context” for fine treatment ofcompositions?

Amortization for Heap Consumption

Page 54: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Potential = the sum of the credits of all nodes

The list � � 10 � 20 � 30 � � � 10 � �of type L

�L�Int � k1 � � k2 � with k1

�x � � x � k2

�x � � 2x

has the potential 2 � 1� �

1 � �2 � 2

� �1

�2

�3 �

Amortization for Heap Consumption

Page 55: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Potential = the sum of the credits of all nodes

The list � � 10 � 20 � 30 � � � 10 � �of type L

�L�Int � k1 � � k2 � with k1

�x � � x � k2

�x � � 2x

has the potential 2 � 1� �

1 � �2 � 2

� �1

�2

�3 �

Amortization for Heap Consumption

Page 56: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Potential = the sum of the credits of all nodes

The list � � 10 � 20 � 30 � � � 10 � �of type L

�L�Int � k1 � � k2 � with k1

�x � � x � k2

�x � � 2x

has the potential 2 � 1� �

1 � �2 � 2

� �1

�2

�3 �

Amortization for Heap Consumption

Page 57: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Potential is a dynamic notion

� Heap � Val � T � ����

is defined as

�h � v � Int � ��� 0 �

�h � null � L0

�T � k � � ��� 0 �

�h �

�� Ll

�T � k � � ��� � h � h

� HD � T � �

k�l � �

�h � h

� TL � Ll � 1

�T � k � �

for���� null �

�h �

�� L

�T � k � � ��� � h � � � Ll

�T � k � � � where l � D

�h �

� � Extended to stack environments and typing contexts:

�h � E �

� � � � x � dom ���� �h � E

�x � � � �

x � � .

Amortization for Heap Consumption

Page 58: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Potential is a dynamic notion

� Heap � Val � T � ����

is defined as

�h � v � Int � ��� 0 �

�h � null � L0

�T � k � � ��� 0 �

�h �

�� Ll

�T � k � � ��� � h � h

� HD � T � �

k�l � �

�h � h

� TL � Ll � 1

�T � k � �

for���� null �

�h �

�� L

�T � k � � ��� � h � � � Ll

�T � k � � � where l � D

�h �

� � Extended to stack environments and typing contexts:

�h � E �

� � � � x � dom ���� �h � E

�x � � � �

x � � .

Amortization for Heap Consumption

Page 59: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

Soundness with the Feelist Model

�n

������ �

�� �h � E �

� �������� the intact part

of the freelist:q units

eval e� �

�n �� �� �� �� �

the output potential

�h � � v � T ��

�� � �h � E �

� ��

������ the intact partof the freelist:q units

Amortization for Heap Consumption

Page 60: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem: Which Prover

I trust myself, but:it would be more convenient to prove the soundness of thepresent system using a proof assistant,proviso: the operational semantics was already incoded.... and the things become more complicated...

General question:If one needs to encode the gentleman’s set:

the syntax of the language,

the operational semantics,

the semantics of a typing judgement,

the soundness proofs,

which prover to choose?

Amortization for Heap Consumption

Page 61: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem: Which Prover

I trust myself, but:it would be more convenient to prove the soundness of thepresent system using a proof assistant,proviso: the operational semantics was already incoded.... and the things become more complicated...

General question:If one needs to encode the gentleman’s set:

the syntax of the language,

the operational semantics,

the semantics of a typing judgement,

the soundness proofs,

which prover to choose?

Amortization for Heap Consumption

Page 62: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem: Which Prover

I trust myself, but:it would be more convenient to prove the soundness of thepresent system using a proof assistant,proviso: the operational semantics was already incoded.... and the things become more complicated...

General question:If one needs to encode the gentleman’s set:

the syntax of the language,

the operational semantics,

the semantics of a typing judgement,

the soundness proofs,

which prover to choose?

Amortization for Heap Consumption

Page 63: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Heap-aware Type System for Programs over ListsSoundness Theorem

The Problem: Which Prover

I trust myself, but:it would be more convenient to prove the soundness of thepresent system using a proof assistant,proviso: the operational semantics was already incoded.... and the things become more complicated...

General question:If one needs to encode the gentleman’s set:

the syntax of the language,

the operational semantics,

the semantics of a typing judgement,

the soundness proofs,

which prover to choose?

Amortization for Heap Consumption

Page 64: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Summary

We have designedthe heap-space aware, amortization based, type systemfor first-order functional programs over polymorphic lists.

It generalises Hofmann-Jost type systemby making annotations variable.

The system is sound.

Future Work

Conider other than lists data structures.

Adjust the approach for an object-oriented setting(code structures which have funcional equivalents,(co)algebraic data types,...)

Amortization for Heap Consumption

Page 65: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Summary

We have designedthe heap-space aware, amortization based, type systemfor first-order functional programs over polymorphic lists.

It generalises Hofmann-Jost type systemby making annotations variable.

The system is sound.

Future Work

Conider other than lists data structures.

Adjust the approach for an object-oriented setting(code structures which have funcional equivalents,(co)algebraic data types,...)

Amortization for Heap Consumption

Page 66: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Summary

We have designedthe heap-space aware, amortization based, type systemfor first-order functional programs over polymorphic lists.

It generalises Hofmann-Jost type systemby making annotations variable.

The system is sound.

Future Work

Conider other than lists data structures.

Adjust the approach for an object-oriented setting(code structures which have funcional equivalents,(co)algebraic data types,...)

Amortization for Heap Consumption

Page 67: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Summary

We have designedthe heap-space aware, amortization based, type systemfor first-order functional programs over polymorphic lists.

It generalises Hofmann-Jost type systemby making annotations variable.

The system is sound.

Future Work

Conider other than lists data structures.

Adjust the approach for an object-oriented setting(code structures which have funcional equivalents,(co)algebraic data types,...)

Amortization for Heap Consumption

Page 68: Amortized Heap-Space Analysis for First-Order Functional Programs · Amortized Heap-Space Analysis for First-Order Functional Programs O. Shkaravska Inst. of Cybernetics at Tallinn

MotivationResults

Summary

Summary

We have designedthe heap-space aware, amortization based, type systemfor first-order functional programs over polymorphic lists.

It generalises Hofmann-Jost type systemby making annotations variable.

The system is sound.

Future Work

Conider other than lists data structures.

Adjust the approach for an object-oriented setting(code structures which have funcional equivalents,(co)algebraic data types,...)

Amortization for Heap Consumption