Complexity Analysis by Polymorphic Sized Type Inference and - - PowerPoint PPT Presentation

complexity analysis by polymorphic sized type inference
SMART_READER_LITE
LIVE PREVIEW

Complexity Analysis by Polymorphic Sized Type Inference and - - PowerPoint PPT Presentation

Complexity Analysis by Polymorphic Sized Type Inference and Constraint Solving. ELICA meeting Martin Avanzini 1 (Joint work with Ugo Dal Lago 2 ) 1 University of Innsbruck 2 Universit di Bologna & INRIA, Sophia Antipolis Motivation


slide-1
SLIDE 1

Complexity Analysis by Polymorphic Sized Type Inference and Constraint Solving.

Martin Avanzini1 (Joint work with Ugo Dal Lago2)

1University of Innsbruck 2Università di Bologna & INRIA, Sophia Antipolis

ELICA meeting

slide-2
SLIDE 2

Motivation

  • worst-case time-complexity analysis of functional programs
  • analysis should be intensionally strong, precise, amendable to

automation and modular

slide-3
SLIDE 3

Example

f :: List Int → List Int → List (Int × Int) f ms ns = filter (/=) (product ms ns) product :: ∀αβ. List α → List β → List (α × β) product ms ns = foldr (λ m ps. foldr (λ n. Cons (n, m)) ps ns) Nil ms filter :: ∀α. (α → Bool) → List α → List α filter p Nil = Nil filter p (Cons x xs)= if p x then Cons x (filter p xs) else filter p xs foldr :: ∀αβ. (α → β → β) → β → List α → β foldr f b Nil = b foldr f b (Cons x xs)= f x (foldr f b xs)

slide-4
SLIDE 4

Example

f :: List Int → List Int → List (Int × Int) f ms ns = filter (/=) (product ms ns) product :: ∀αβ. List α → List β → List (α × β) product ms ns = foldr (λ m ps. foldr (λ n. Cons (n, m)) ps ns) Nil ms filter :: ∀α. (α → Bool) → List α → List α filter p Nil = Nil filter p (Cons x xs)= if p x then Cons x (filter p xs) else filter p xs foldr :: ∀αβ. (α → β → β) → β → List α → β foldr f b Nil = b foldr f b (Cons x xs)= f x (foldr f b xs)

slide-5
SLIDE 5

Higher-order combinators are hard to analyse…

foldr (◦) b [e1, e2, . . . , en] = e1 ◦ (e2 ◦ (. . . (en ◦ b) . . . )) complexity depends very much on how (◦) uses its arguments

  • 1. foldr append Nil e

e en complexity O n m , where m binds length of ei’s

  • 2. foldr flip append Nil e

e en complexity O

n i

i m O n m

  • 3. foldr

e Cons append xs e Nil e e en complexity O k n where k is the length of xs.

size analysis crucial step in runtime analysis complexity depends not only on arguments, but also

  • n the environment
slide-6
SLIDE 6

Higher-order combinators are hard to analyse…

foldr (◦) b [e1, e2, . . . , en] = e1 ◦ (e2 ◦ (. . . (en ◦ b) . . . )) complexity depends very much on how (◦) uses its arguments

  • 1. foldr append Nil [e1, e2, . . . , en]

⇒ complexity O(n · m), where m binds length of ei’s

  • 2. foldr flip append Nil e

e en complexity O

n i

i m O n m

  • 3. foldr

e Cons append xs e Nil e e en complexity O k n where k is the length of xs.

size analysis crucial step in runtime analysis complexity depends not only on arguments, but also

  • n the environment
slide-7
SLIDE 7

Higher-order combinators are hard to analyse…

foldr (◦) b [e1, e2, . . . , en] = e1 ◦ (e2 ◦ (. . . (en ◦ b) . . . )) complexity depends very much on how (◦) uses its arguments

  • 1. foldr append Nil [e1, e2, . . . , en]

⇒ complexity O(n · m), where m binds length of ei’s

  • 2. foldr (flip append) Nil [e1, e2, . . . , en]

⇒ complexity O(∑n−1

i=0 i · m) = O(n2 · m)

  • 3. foldr

e Cons append xs e Nil e e en complexity O k n where k is the length of xs.

size analysis crucial step in runtime analysis complexity depends not only on arguments, but also

  • n the environment
slide-8
SLIDE 8

Higher-order combinators are hard to analyse…

foldr (◦) b [e1, e2, . . . , en] = e1 ◦ (e2 ◦ (. . . (en ◦ b) . . . )) complexity depends very much on how (◦) uses its arguments

  • 1. foldr append Nil [e1, e2, . . . , en]

⇒ complexity O(n · m), where m binds length of ei’s

  • 2. foldr (flip append) Nil [e1, e2, . . . , en]

⇒ complexity O(∑n−1

i=0 i · m) = O(n2 · m)

  • 3. foldr

e Cons append xs e Nil e e en complexity O k n where k is the length of xs.

size analysis crucial step in runtime analysis complexity depends not only on arguments, but also

  • n the environment
slide-9
SLIDE 9

Higher-order combinators are hard to analyse…

foldr (◦) b [e1, e2, . . . , en] = e1 ◦ (e2 ◦ (. . . (en ◦ b) . . . )) complexity depends very much on how (◦) uses its arguments

  • 1. foldr append Nil [e1, e2, . . . , en]

⇒ complexity O(n · m), where m binds length of ei’s

  • 2. foldr (flip append) Nil [e1, e2, . . . , en]

⇒ complexity O(∑n−1

i=0 i · m) = O(n2 · m)

  • 3. foldr (λe.Cons (append xs e)) Nil [e1, e2, . . . , en]

⇒ complexity O(k · n) where k is the length of xs.

size analysis crucial step in runtime analysis complexity depends not only on arguments, but also

  • n the environment
slide-10
SLIDE 10

Higher-order combinators are hard to analyse…

foldr (◦) b [e1, e2, . . . , en] = e1 ◦ (e2 ◦ (. . . (en ◦ b) . . . )) complexity depends very much on how (◦) uses its arguments

  • 1. foldr append Nil [e1, e2, . . . , en]

⇒ complexity O(n · m), where m binds length of ei’s

  • 2. foldr (flip append) Nil [e1, e2, . . . , en]

⇒ complexity O(∑n−1

i=0 i · m) = O(n2 · m)

  • 3. foldr (λe.Cons (append xs e)) Nil [e1, e2, . . . , en]

⇒ complexity O(k · n) where k is the length of xs.

size analysis crucial step in runtime analysis complexity depends not only on arguments, but also

  • n the environment
slide-11
SLIDE 11

Sized-types

  • 1. annotate datatypes with sizes

List1 α, List2 α, List3 α, . . .

  • 2. extended type system that

enables reasoning about sizes append :: ∀ij. Listi α → Listj α → Listi+j α

  • 3. inference generates set of

constraints, solved by external tool (e.g. SMT solver)

slide-12
SLIDE 12

From sized-types to time complexity

idea: instrument program to compute complexity ⟨τ → ρ⟩ ⇒ ⟨τ⟩ → N → (⟨ρ⟩ × N); foldr List N N foldr f b Nil t b Succ t foldr f b Cons x xs t let e t foldr f b xs t in let e t f x t in let e t e e t in e Succ t foldr N List N foldr f t foldr f t foldr N List N foldr f b t foldr f b t

slide-13
SLIDE 13

From sized-types to time complexity

idea: instrument program to compute complexity ⟨τ → ρ⟩ ⇒ ⟨τ⟩ → N → (⟨ρ⟩ × N); foldr3 :: ∀αβ. ⟨α → β → β⟩ → ⟨β⟩ → ⟨List α⟩ → N → (⟨β⟩ × N) foldr3 f b Nil t = (b, Succ t) foldr3 f b (Cons x xs) t = let (e1, t1) = foldr3 f b xs t in let (e2, t2) = f x t1 in let (e3, t3) = e2 e1 t2 in (e3, Succ t3) foldr1 :: ∀αβ. ⟨α → β → β⟩ → N → (⟨β → List α → β⟩ × N) foldr1 f t = (foldr2 f, t) foldr2 :: ∀αβ. ⟨α → β → β⟩ → ⟨β⟩ → N → (⟨List α → β⟩ × N) foldr2 f b t = (foldr3 f b, t)

slide-14
SLIDE 14

Practical sized-type analysis (i)

  • consider reversal of lists:

rev :: ∀α. List α → List α → List α rev Nil ys = ys rev (Cons x xs) ys = rev xs (Cons x ys)

  • usual let-polymorphism requires that recursive call is typed under

monotype, …

  • in sized-type setting, types of e.g. second argument changes from

Listi α to Listi+1 α extension

1 : type recursive calls with type polymorphic in size indices

slide-15
SLIDE 15

Practical sized-type analysis (i)

  • consider reversal of lists:

rev :: ∀α. List α → List α → List α rev Nil ys = ys rev (Cons x xs) ys = rev xs (Cons x ys)

  • usual let-polymorphism requires that recursive call is typed under

monotype, …

  • in sized-type setting, types of e.g. second argument changes from

Listi α to Listi+1 α extension

1 : type recursive calls with type polymorphic in size indices

slide-16
SLIDE 16

Practical sized-type analysis (ii)

  • consider higher-order combinator twice:

twice :: ∀α. (α → α) → α → α twice f x = f (f x)

  • term twice Succ, where Succ :: ∀i.Ni → Ni+1, cannot be typed
  • even when specializing

to N, type in prenex form not enough twice ij Nj Nj Ni Ni

  • concerns all function that use functional argument more than
  • nce, in particular recursive higher-order functions

extension

2 : arbitrary-rank index polymorphic

twice i j Nj Nj Ni Ni foldr klm ij Ni Lj Lj

k

Ll Lm Lk m

l

slide-17
SLIDE 17

Practical sized-type analysis (ii)

  • consider higher-order combinator twice:

twice :: ∀α. (α → α) → α → α twice f x = f (f x)

  • term twice Succ, where Succ :: ∀i.Ni → Ni+1, cannot be typed
  • even when specializing α to N, type in prenex form not enough

twice :: ∀ij. (Nj → Nj+1) → Ni → Ni+2

  • concerns all function that use functional argument more than
  • nce, in particular recursive higher-order functions

extension

2 : arbitrary-rank index polymorphic

twice i j Nj Nj Ni Ni foldr klm ij Ni Lj Lj

k

Ll Lm Lk m

l

slide-18
SLIDE 18

Practical sized-type analysis (ii)

  • consider higher-order combinator twice:

twice :: ∀α. (α → α) → α → α twice f x = f (f x)

  • term twice Succ, where Succ :: ∀i.Ni → Ni+1, cannot be typed
  • even when specializing α to N, type in prenex form not enough

twice :: ∀ij. (Nj → Nj+1) → Ni → Ni+2

  • concerns all function that use functional argument more than
  • nce, in particular recursive higher-order functions

extension

2 : arbitrary-rank index polymorphic

twice i j Nj Nj Ni Ni foldr klm ij Ni Lj Lj

k

Ll Lm Lk m

l

slide-19
SLIDE 19

Practical sized-type analysis (ii)

  • consider higher-order combinator twice:

twice :: ∀α. (α → α) → α → α twice f x = f (f x)

  • term twice Succ, where Succ :: ∀i.Ni → Ni+1, cannot be typed
  • even when specializing α to N, type in prenex form not enough

twice :: ∀ij. (Nj → Nj+1) → Ni → Ni+2

  • concerns all function that use functional argument more than
  • nce, in particular recursive higher-order functions

extension

2 : arbitrary-rank index polymorphic

twice :: ∀i. (∀j.Nj → Nj+1) → Ni → Ni+2 foldr :: ∀klm.(∀ij.Ni → Lj → Lj+k) → Ll → Lm → Lk·m+l

slide-20
SLIDE 20

Sized-types revisited

Computational model (simple types) τ, ρ ::= B base type | τ × ρ pair type | τ → ρ function type (expressions) s, t ::= xτ variable | fτ function | Cτ constructor | (sτ→ρ tτ)ρ application | (sτ1, tτ2)τ1×τ2 pair | (let sτ1×τ2 be (xτ1, yτ2) in tρ)ρ pair destructor (patterns) p ::= xτ | (Cτ1→···→τn→B pτ1

1 · · · pτn n )B

(equations) e ::= (f p1 · · · pn)τ = sτ

  • program P is set of non-overlapping, left-linear equations
slide-21
SLIDE 21

Sized-types revisited

computational model

  • call-by-value reduction semantics
  • plain simple types, e.g. NatList instead of List N, for simplicity

– extension to polymorphic setting straight forward

  • no conditionals, case-expressions,
  • abstractions …

– does not improve expressiveness of our language – again straight forward to incorporate

slide-22
SLIDE 22

Sized-types revisited

computational model

  • call-by-value reduction semantics
  • plain simple types, e.g. NatList instead of List N, for simplicity

– extension to polymorphic setting straight forward

  • no conditionals, case-expressions,
  • abstractions …

– does not improve expressiveness of our language – again straight forward to incorporate

slide-23
SLIDE 23

Sized-types revisited

computational model

  • call-by-value reduction semantics
  • plain simple types, e.g. NatList instead of List N, for simplicity

– extension to polymorphic setting straight forward

  • no conditionals, case-expressions, λ-abstractions …

– does not improve expressiveness of our language – again straight forward to incorporate

slide-24
SLIDE 24

Sized-types revisited

ingredients (type) τ, ρ ::= Ba indexed base type | τ × ρ pair type | σ → τ function type (schema) σ ::= Ba | ∀ ⃗

  • i. σ → τ

(size index) a b i index variable f a ak function application

  • we suppose functions are uncurried, to simplify notions
  • quantification to the right of arrow does not add in strength
  • meaning to index functions f given by a weakly monotonic

interpretation f

k

slide-25
SLIDE 25

Sized-types revisited

ingredients (type) τ, ρ ::= Ba indexed base type | τ × ρ pair type | σ → τ function type (schema) σ ::= Ba | ∀ ⃗

  • i. σ → τ

(size index) a b i index variable f a ak function application

  • we suppose functions are uncurried, to simplify notions
  • quantification to the right of arrow does not add in strength
  • meaning to index functions f given by a weakly monotonic

interpretation f

k

slide-26
SLIDE 26

Sized-types revisited

ingredients (type) τ, ρ ::= Ba indexed base type | τ × ρ pair type | σ → τ function type (schema) σ ::= Ba | ∀ ⃗

  • i. σ → τ

(size index) a b i index variable f a ak function application

  • we suppose functions are uncurried, to simplify notions
  • quantification to the right of arrow does not add in strength
  • meaning to index functions f given by a weakly monotonic

interpretation f

k

slide-27
SLIDE 27

Sized-types revisited

ingredients (type) τ, ρ ::= Ba indexed base type | τ × ρ pair type | σ → τ function type (schema) σ ::= Ba | ∀ ⃗

  • i. σ → τ

(size index) a, b ::= i index variable | f(a1, . . . , ak) function application

  • we suppose functions are uncurried, to simplify notions
  • quantification to the right of arrow does not add in strength
  • meaning to index functions f given by a weakly monotonic

interpretation f

k

slide-28
SLIDE 28

Sized-types revisited

ingredients (type) τ, ρ ::= Ba indexed base type | τ × ρ pair type | σ → τ function type (schema) σ ::= Ba | ∀ ⃗

  • i. σ → τ

(size index) a, b ::= i index variable | f(a1, . . . , ak) function application

  • we suppose functions are uncurried, to simplify notions
  • quantification to the right of arrow does not add in strength
  • meaning to index functions f given by a weakly monotonic

interpretation f : Nk → N

slide-29
SLIDE 29

Type-checking

auxiliary notions

  • each function f declared by one or more closed schemas σ, in

notation f :: σ, obeying to the following restrictions:

  • 1. datatypes to the left of arrow annotated by variables

half :: ∀i.N2·i → Ni ⇒ half :: ∀i.Ni → Ni/2

  • 2. all these variables must be pairwise distinct

f :: ∀i.Ni → Ni → τ ⇒ f :: ∀ij.Ni → Nj → τ ′

  • 3. schema closes over all variables occurring in negative position

g :: ∀ij. (Ni → Ni+j) → τ ⇒ g :: ∀j. (∀i. Ni → Ni+j) → τ

  • constructor declarations count, e.g.

Succ i Ni Ni

  • typing judgement are of the form

x xn

n

s

slide-30
SLIDE 30

Type-checking

auxiliary notions

  • each function f declared by one or more closed schemas σ, in

notation f :: σ, obeying to the following restrictions:

  • 1. datatypes to the left of arrow annotated by variables

half :: ∀i.N2·i → Ni ⇒ half :: ∀i.Ni → Ni/2

  • 2. all these variables must be pairwise distinct

f :: ∀i.Ni → Ni → τ ⇒ f :: ∀ij.Ni → Nj → τ ′

  • 3. schema closes over all variables occurring in negative position

g :: ∀ij. (Ni → Ni+j) → τ ⇒ g :: ∀j. (∀i. Ni → Ni+j) → τ

  • constructor declarations count, e.g.

Succ :: ∀i. Ni → Ni+1

  • typing judgement are of the form

x xn

n

s

slide-31
SLIDE 31

Type-checking

auxiliary notions

  • each function f declared by one or more closed schemas σ, in

notation f :: σ, obeying to the following restrictions:

  • 1. datatypes to the left of arrow annotated by variables

half :: ∀i.N2·i → Ni ⇒ half :: ∀i.Ni → Ni/2

  • 2. all these variables must be pairwise distinct

f :: ∀i.Ni → Ni → τ ⇒ f :: ∀ij.Ni → Nj → τ ′

  • 3. schema closes over all variables occurring in negative position

g :: ∀ij. (Ni → Ni+j) → τ ⇒ g :: ∀j. (∀i. Ni → Ni+j) → τ

  • constructor declarations count, e.g.

Succ :: ∀i. Ni → Ni+1

  • typing judgement are of the form

x1 : σ1, · · · , xn : σn ⊢ s : τ

slide-32
SLIDE 32

Type-checking

excerpt a ≤ b Ba ⊑ Bb σ2 ⊑ σ1 τ1 ⊑ τ2 σ1 → τ1 ⊑ σ2 → τ2 τ1 ⊑ τ2{⃗ a/ ⃗ j} ⃗ i ̸∈ FV(∀ ⃗ j.τ2) ∀ ⃗ i.τ1 ⊑ ∀ ⃗ j.τ2

Figure: subtyping.

Γ(x) = ∀ ⃗ i.τ Γ ⊢ x : τ{⃗ a/ ⃗ i} f :: ∀ ⃗ i.τ Γ ⊢ f : τ{⃗ a/ ⃗ i} Γ ⊢ s : (∀ ⃗ i.ρ) → τ Γ ⊢ t : ρ ⃗ i ̸∈ FV(Γ) Γ ⊢ s t : τ Γ ⊢ s : ρ ρ ⊑ τ Γ ⊢ s : τ

Figure: type-checking.

slide-33
SLIDE 33

Subject reduction

Definition

A program P is well-typed if for all equations f p1 · · · pn = r of P, Γ ⊢FP f p1 · · · pn : τ = ⇒ Γ ⊢ r : τ , holds for all contexts Γ and types τ.

  • the footprint judgement assigns to terms “most general” type, e.g.,

x N xs Li ys Lj

FP append Cons x xs ys

L i

j

where append ij Li Lj Li

j

  • it can be understood as a function footprint s

Theorem (Subject reduction)

Suppose P is well-typed. If s and s

P t then

t .

slide-34
SLIDE 34

Subject reduction

Definition

A program P is well-typed if for all equations f p1 · · · pn = r of P, Γ ⊢FP f p1 · · · pn : τ = ⇒ Γ ⊢ r : τ , holds for all contexts Γ and types τ.

  • the footprint judgement assigns to terms “most general” type, e.g.,

x : N, xs : Li, ys : Lj ⊢FP append (Cons x xs) ys : L(i+1)+j where append :: ∀ij. Li → Lj → Li+j

  • it can be understood as a function footprint s

Theorem (Subject reduction)

Suppose P is well-typed. If s and s

P t then

t .

slide-35
SLIDE 35

Subject reduction

Definition

A program P is well-typed if for all equations f p1 · · · pn = r of P, Γ ⊢FP f p1 · · · pn : τ = ⇒ Γ ⊢ r : τ , holds for all contexts Γ and types τ.

  • the footprint judgement assigns to terms “most general” type, e.g.,

x : N, xs : Li, ys : Lj ⊢FP append (Cons x xs) ys : L(i+1)+j where append :: ∀ij. Li → Lj → Li+j

  • it can be understood as a function footprint(s) := (Γ, τ)

Theorem (Subject reduction)

Suppose P is well-typed. If s and s

P t then

t .

slide-36
SLIDE 36

Subject reduction

Definition

A program P is well-typed if for all equations f p1 · · · pn = r of P, Γ ⊢FP f p1 · · · pn : τ = ⇒ Γ ⊢ r : τ , holds for all contexts Γ and types τ.

  • the footprint judgement assigns to terms “most general” type, e.g.,

x : N, xs : Li, ys : Lj ⊢FP append (Cons x xs) ys : L(i+1)+j where append :: ∀ij. Li → Lj → Li+j

  • it can be understood as a function footprint(s) := (Γ, τ)

Theorem (Subject reduction)

Suppose P is well-typed. If ⊢ s : τ and s − →P t then ⊢ t : τ.

slide-37
SLIDE 37

Inference

  • verview
  • index language extended with second-order index variables A

a ::= i | f(a1, . . . , ak) | A

  • second-order index variable A represents index term
  • input simply-typed program

concatMap N L L L lam N L N L L concatMap f foldr lam f Nil lam f x append f x

  • outputs
  • 1. size-annotated type declarations
  • 2. semantic interpretation functions
slide-38
SLIDE 38

Inference

  • verview
  • index language extended with second-order index variables A

a ::= i | f(a1, . . . , ak) | A

  • second-order index variable A represents index term
  • input simply-typed program

concatMap :: (N → L) → L → L lam :: (N → L) → N → L → L concatMap f = foldr (lam f) Nil lam f x = append (f x)

  • outputs
  • 1. size-annotated type declarations
  • 2. semantic interpretation functions
slide-39
SLIDE 39

Inference

  • verview
  • index language extended with second-order index variables A

a ::= i | f(a1, . . . , ak) | A

  • second-order index variable A represents index term
  • input simply-typed program

concatMap :: (N → L) → L → L lam :: (N → L) → N → L → L concatMap f = foldr (lam f) Nil lam f x = append (f x)

  • outputs
  • 1. size-annotated type declarations
  • 2. semantic interpretation functions ·
slide-40
SLIDE 40

Inference

step

1 : annotation

decorate simple types with uninterpreted indices append :: ∀ij. Li → Lj → Lapd(i,j) lam :: ∀jkl. (∀i.Ni → Lf(i,j)) → Nk → Ll → Llm(j,k,l) foldr :: ∀klm. (∀ij.Ni → Lj → Lg(i,j,k)) → Ll → Lm → Lfld(k,l,m)

slide-41
SLIDE 41

Inference

step

2 : constraint generation

for all equations in P, generate two sets of constraints, e.g. for lam f x = append (f x) as follows

  • 1. footprint gives environment and template type of left-hand side

footprint lam f x f i Ni Lf i j x Nk Ll Llm j k l

  • 2. inference infer

r on right-hand side r generates template for r and first set of constraints infer append f x LA Lapd A

A

f A j A k A

  • 3. well-typedness condition enforced by second set of constraints

subtypeOf LA Lapd A

A

l A apd A A lm j k l

slide-42
SLIDE 42

Inference

step

2 : constraint generation

for all equations in P, generate two sets of constraints, e.g. for lam f x = append (f x) as follows

  • 1. footprint gives environment and template type of left-hand side

footprint(lam f x) = ({f : ∀i.Ni → Lf(i,j), x : Nk}

  • :=Γ

, Ll → Llm(j,k,l)

  • :=τ

)

  • 2. inference infer

r on right-hand side r generates template for r and first set of constraints infer append f x LA Lapd A

A

f A j A k A

  • 3. well-typedness condition enforced by second set of constraints

subtypeOf LA Lapd A

A

l A apd A A lm j k l

slide-43
SLIDE 43

Inference

step

2 : constraint generation

for all equations in P, generate two sets of constraints, e.g. for lam f x = append (f x) as follows

  • 1. footprint gives environment and template type of left-hand side

footprint(lam f x) = ({f : ∀i.Ni → Lf(i,j), x : Nk}

  • :=Γ

, Ll → Llm(j,k,l)

  • :=τ

)

  • 2. inference infer(Γ, r) on right-hand side r generates template for r

and first set of constraints infer(Γ, append (f x)) = (LA3 → Lapd(A2,A3), {f(A1, j) ≤ A2, k ≤ A1})

  • 3. well-typedness condition enforced by second set of constraints

subtypeOf LA Lapd A

A

l A apd A A lm j k l

slide-44
SLIDE 44

Inference

step

2 : constraint generation

for all equations in P, generate two sets of constraints, e.g. for lam f x = append (f x) as follows

  • 1. footprint gives environment and template type of left-hand side

footprint(lam f x) = ({f : ∀i.Ni → Lf(i,j), x : Nk}

  • :=Γ

, Ll → Llm(j,k,l)

  • :=τ

)

  • 2. inference infer(Γ, r) on right-hand side r generates template for r

and first set of constraints infer(Γ, append (f x)) = (LA3 → Lapd(A2,A3), {f(A1, j) ≤ A2, k ≤ A1})

  • 3. well-typedness condition enforced by second set of constraints

subtypeOf(LA3 → Lapd(A1,A2), τ) = {l ≤ A3, apd(A1, A2) ≤ lm(j, k, l)}

slide-45
SLIDE 45

Inference

step

2 : constraint generation

{a ≤ b} ⊢ST Ba ⊑ Bb C1 ⊢ST σ2 ⊑ σ1 C2 ⊢ST τ1 ⊑ τ2 C1 ∪ C2 ⊢ST σ1 → τ1 ⊑ σ2 → τ2 C ⊢ST τ1 ⊑ τ2{⃗ a/ ⃗ j} ⃗ i ̸∈ FV(∀ ⃗ j.τ2) C ⊢ST ∀ ⃗ i.τ1 ⊑ ∀ ⃗ j.τ2 Figure: subtyping. Γ(x) = ∀ ⃗ i.τ ∅; Γ ⊢I x : τ{⃗ a/ ⃗ i} f :: ∀ ⃗ i.τ ∅; Γ ⊢I f : τ{⃗ a/ ⃗ i} C1; Γ ⊢I s : (∀ ⃗ i.ρ) → τ C2; Γ ⊢I t : ρ′ C3 ⊢ST ρ′ ⊑ ρ ⃗ i ̸∈ FV(Γ) C1, C2, C3; Γ ⊢I s t : τ Figure: type inference.

slide-46
SLIDE 46

Inference

step

2 : constraint generation

{a ≤ b} ⊢ST Ba ⊑ Bb C1 ⊢ST σ2 ⊑ σ1 C2 ⊢ST τ1 ⊑ τ2 C1 ∪ C2 ⊢ST σ1 → τ1 ⊑ σ2 → τ2 C ⊢ST τ1 ⊑ τ2{⃗ a/ ⃗ j} ⃗ i ̸∈ FV(∀ ⃗ j.τ2) C ⊢ST ∀ ⃗ i.τ1 ⊑ ∀ ⃗ j.τ2 Figure: subtyping. Γ(x) = ∀ ⃗ i.τ ∅; Γ ⊢I x : τ{⃗ a/ ⃗ i} f :: ∀ ⃗ i.τ ∅; Γ ⊢I f : τ{⃗ a/ ⃗ i} C1; Γ ⊢I s : (∀ ⃗ i.ρ) → τ C2; Γ ⊢I t : ρ′ C3 ⊢ST ρ′ ⊑ ρ ⃗ i ̸∈ FV(Γ) C1, C2, C3; Γ ⊢I s t : τ Figure: type inference.

slide-47
SLIDE 47

Inference

step

2 : constraint generation

{a ≤ b} ⊢ST Ba ⊑ Bb C1 ⊢ST σ2 ⊑ σ1 C2 ⊢ST τ1 ⊑ τ2 C1 ∪ C2 ⊢ST σ1 → τ1 ⊑ σ2 → τ2 C ⊢ST τ1 ⊑ τ2{⃗ A/ ⃗ j} ⃗ i ̸∈ FV(∀ ⃗ j.τ2) ⃗ A fresh C ⊢ST ∀ ⃗ i.τ1 ⊑ ∀ ⃗ j.τ2 Figure: subtyping. Γ(x) = ∀ ⃗ i.τ ⃗ A fresh ∅; Γ ⊢I x : τ{⃗ A/ ⃗ i} f :: ∀ ⃗ i.τ ⃗ A fresh ∅; Γ ⊢I f : τ{⃗ A/ ⃗ i} C1; Γ ⊢I s : (∀ ⃗ i.ρ) → τ C2; Γ ⊢I t : ρ′ C3 ⊢ST ρ′ ⊑ ρ ⃗ i ̸∈ FV(Γ) C1, C2, C3; Γ ⊢I s t : τ Figure: type inference.

slide-48
SLIDE 48

Inference

step

2 : constraint generation

{a ≤ b} ⊢ST Ba ⊑ Bb C1 ⊢ST σ2 ⊑ σ1 C2 ⊢ST τ1 ⊑ τ2 C1 ∪ C2 ⊢ST σ1 → τ1 ⊑ σ2 → τ2 C ⊢ST τ1 ⊑ τ2{⃗ A/ ⃗ j} ⃗ i ̸∈ FV(∀ ⃗ j.τ2) ⃗ A fresh C,⃗ i ̸∈sol SOVars(τ1) ∪ SOVars(τ2) ⊢ST ∀ ⃗ i.τ1 ⊑ ∀ ⃗ j.τ2 Figure: subtyping. Γ(x) = ∀ ⃗ i.τ ⃗ A fresh ∅; Γ ⊢I x : τ{⃗ A/ ⃗ i} f :: ∀ ⃗ i.τ ⃗ A fresh ∅; Γ ⊢I f : τ{⃗ A/ ⃗ i} C1; Γ ⊢I s : (∀ ⃗ i.ρ) → τ C2; Γ ⊢I t : ρ′ C3 ⊢ST ρ′ ⊑ ρ ⃗ i ̸∈ FV(Γ) C1, C2, C3,⃗ i ̸∈sol SOVars(Γ) ∪ SOVars(ρ); Γ ⊢I s t : τ Figure: type inference.

slide-49
SLIDE 49

Inference

step

3 : constraint solving

  • find a model (ϑ, ·) for collected constraints C consisting of

– ϑ assigns index terms to second-order variables – · assigns meaning to index functions

(ϑ, ·) ⊨ C :⇐ ⇒ { lϑ ≤ rϑ for all l ≤ r ∈ C i ̸∈ FV(ϑ(A)) for all (i ̸∈ A) ∈ C

  • 1. eliminate second-order variables by skolemization

f A j A f sk k j sk k j k A k sk k l A l sk l apd A A lm j k l apd sk k sk k j lm j k l

  • 2. first-order constraints solvable with our tool GUBS
slide-50
SLIDE 50

Inference

step

3 : constraint solving

  • find a model (ϑ, ·) for collected constraints C consisting of

– ϑ assigns index terms to second-order variables – · assigns meaning to index functions

(ϑ, ·) ⊨ C :⇐ ⇒ { lϑ ≤ rϑ for all l ≤ r ∈ C i ̸∈ FV(ϑ(A)) for all (i ̸∈ A) ∈ C

  • 1. eliminate second-order variables by skolemization

f(A1, j) ≤ A2 f(sk1(k), j) ≤ sk2(k, j) k ≤ A1 k ≤ sk1(k) l ≤ A3

l ≤ sk3(l) apd(A1, A2) ≤ lm(j, k, l) apd(sk1(k), sk2(k, j)) ≤ lm(j, k, l) . . . . . .

  • 2. first-order constraints solvable with our tool GUBS
slide-51
SLIDE 51

Inference

step

3 : constraint solving

  • find a model (ϑ, ·) for collected constraints C consisting of

– ϑ assigns index terms to second-order variables – · assigns meaning to index functions

(ϑ, ·) ⊨ C :⇐ ⇒ { lϑ ≤ rϑ for all l ≤ r ∈ C i ̸∈ FV(ϑ(A)) for all (i ̸∈ A) ∈ C

  • 1. eliminate second-order variables by skolemization

f(A1, j) ≤ A2 f(sk1(k), j) ≤ sk2(k, j) k ≤ A1 k ≤ sk1(k) l ≤ A3

l ≤ sk3(l) apd(A1, A2) ≤ lm(j, k, l) apd(sk1(k), sk2(k, j)) ≤ lm(j, k, l) . . . . . .

  • 2. first-order constraints solvable with our tool GUBS
slide-52
SLIDE 52

Soundness and relative completeness

Theorem (Soundness and Completeness)

Program P is well-typed if and only if (ϑ, ·) ⊨ C for some (ϑ, ·) and constraints C generated as explained before.

Essential proof steps.

  • 1. C

ST

, e.g., a b

ST Ba

Bb a b Ba Bb since a b = a b

  • 2. C

I s

s , e.g. x i A fresh

I x

A i x i x A i

slide-53
SLIDE 53

Soundness and relative completeness

Theorem (Soundness and Completeness)

Program P is well-typed if and only if (ϑ, ·) ⊨ C for some (ϑ, ·) and constraints C generated as explained before.

Essential proof steps.

  • 1. C ⊢ST τ ⊑ ρ ⇐

⇒ τϑ ⊑ ρϑ, e.g., {a ≤ b} ⊢ST Ba ⊑ Bb ⇐ ⇒ aϑ ≤ bϑ Baϑ ⊑ Bbϑ since (ϑ, ·) ⊨ {a ≤ b} = ⇒ aϑ ≤ bϑ

  • 2. C

I s

s , e.g. x i A fresh

I x

A i x i x A i

slide-54
SLIDE 54

Soundness and relative completeness

Theorem (Soundness and Completeness)

Program P is well-typed if and only if (ϑ, ·) ⊨ C for some (ϑ, ·) and constraints C generated as explained before.

Essential proof steps.

  • 1. C ⊢ST τ ⊑ ρ ⇐

⇒ τϑ ⊑ ρϑ, e.g., {a ≤ b} ⊢ST Ba ⊑ Bb ⇐ ⇒ aϑ ≤ bϑ Baϑ ⊑ Bbϑ since (ϑ, ·) ⊨ {a ≤ b} = ⇒ aϑ ≤ bϑ

  • 2. C; Γ ⊢I s : τ ⇐

⇒ Γ ⊢ s : τϑ, e.g. Γ(x) = ∀ ⃗ i.τ ⃗ A fresh ∅; Γ ⊢I x : τ{⃗ A/ ⃗ i} ⇐ ⇒ Γ(x) = ∀ ⃗ i.τ Γ ⊢ x : τ{ ⃗ ϑ(A)/ ⃗ i}

slide-55
SLIDE 55

Conclusion

  • fully polymorphic sized-type system
  • ticking transformation reduces runtime-complexity to size

analysis

  • fully working implementation on top of Hindley-Milner type

system, including constraint solver http://cl-informatik.uibk.ac.at/zini

  • currently, whole program analysis, but …

– each (mutual) recursive definition gives rise to an SCC – bottom-up per SCC-analysis implemented

slide-56
SLIDE 56

Conclusion

  • fully polymorphic sized-type system
  • ticking transformation reduces runtime-complexity to size

analysis

  • fully working implementation on top of Hindley-Milner type

system, including constraint solver http://cl-informatik.uibk.ac.at/zini

  • currently, whole program analysis, but …

– each (mutual) recursive definition gives rise to an SCC – bottom-up per SCC-analysis implemented

slide-57
SLIDE 57

Conclusion

  • fully polymorphic sized-type system
  • ticking transformation reduces runtime-complexity to size

analysis

  • fully working implementation on top of Hindley-Milner type

system, including constraint solver http://cl-informatik.uibk.ac.at/zini

  • currently, whole program analysis, but …

– each (mutual) recursive definition gives rise to an SCC – bottom-up per SCC-analysis implemented