+ All Categories
Home > Documents > “Calculating Sized Types” by Wei-Ngan Chin and Siau-Cheng Khoo published in PEPM00 and HOSC01

“Calculating Sized Types” by Wei-Ngan Chin and Siau-Cheng Khoo published in PEPM00 and HOSC01

Date post: 31-Dec-2015
Category:
Upload: estralita-mendez
View: 17 times
Download: 0 times
Share this document with a friend
Description:
Introduction to Sized Type Inference. “Calculating Sized Types” by Wei-Ngan Chin and Siau-Cheng Khoo published in PEPM00 and HOSC01 Presented by Xu Na. Meaning of Size. Int m – size of an integer is the integer itself - PowerPoint PPT Presentation
24
Sized Types Inference 1 “Calculating Sized Types” by Wei-Ngan Chin and Siau-Cheng Khoo published in PEPM00 and HOSC01 Presented by Xu Na
Transcript
Page 1: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 1

“Calculating Sized Types” by Wei-Ngan Chin and Siau-Cheng Khoo

published in PEPM00 and HOSC01

Presented by Xu Na

Page 2: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 2

Intm – size of an integer is the integer itself[]m – size of a recursive data consists of its

maximum depth.Boolm – m=0 for False and m=1 for Trueappend :: []m ! []n ! []p

s.t. m¸0 Æ n¸0 Æ p=m+n – size of a function is a relation between

its input and output sizes.

Meaning of Size Meaning of Size

Page 3: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 3

Syntax of Sized Type Syntax of Sized Type

e :: (,)

annotated type size constraint

Examples:

3 :: (Inti, (i = 3) )

[3] :: ([Inti]j, (i = 3) (j = 1) )

[3,4] :: ([Inti]j, ((i = 3) (i = 4)) (j = 2) )

:: ([Int]j, (j = 2) )

True :: (Boolb, (b = 1) )

False :: (Boolb, (b = 0) )

append :: ([]m []n []p, (m0) (n0) (p=m+n) )

Page 4: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 4

What is Sized Type? What is Sized Type?

An advanced type system to capture size relation of:

• output in terms of inputs

• pre-conditions on inputs

• input invariant across recursion

Page 5: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 5

Example:

tail (x:xs) = xs

polymorphic type:

tail :: [] []

sized type:

tail :: []m []p

size size (m>0) // pre-condition

(p+1=m) // output relation

size variables ofinteger types

linear arithmeticconstraints on size variables

Page 6: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 6

Example:zip :: [] [] [(,)]

zip [] [] = []

zip (x:xs) (y:ys) = (x,y):(zip xs ys)

Sized type:

zip :: []m []n [(,)]p

size size (m0) (n0) (n=m) // pre-condition

(p=m) // output relation

inv inv (0m+ <m) (0n+ <n) (m – m+ = n – n+ ) // invariant

(zip xsm ysn) …(zip xs1m1 ys1

n1)… …(zip xs2m2 ys2

n2)… …

(m+,n+) {(m1,n1),(m2,n2),…,…}

Page 7: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 7

Syntax of Constraints Syntax of Constraints

Page 8: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 8

Sized Type Inference Sized Type Inference

1. Extension of Polymorphic Type Rules.

2. Requires linear arithmetic constraint solver.

3. Recursive letrec requires new fixed point computation.

Page 9: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 9

Example Example

f b xs = case b of False -> xs

True -> append xs xs

Assume:

append :: []m []n []p st st (m0) (n0) (p=m+n)

f :: Booli []j []k

infer : infer : ((i = 0) (k = j)) ((i = 1) (k = j+j))

What is the sized type for f?

Page 10: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 10

Example (non-recursive) Example (non-recursive)

f b xs = case b of False -> xs

True -> append xs xs

f :: Booli []j []k

append :: []m []n []p st st (m0) (n0) (p=m+n)

xs :: []k st st (k = j)

(append xs xs) :: []k st st (k = j+j)

variable-rule application-rule

Page 11: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 11

Example (non-recursive) Example (non-recursive)

f :: Booli []j []k

f b xs = case b of False -> xs

True -> append xs xs

case-rule

(case b ...) :: []k st st ((i = 0) (k = j)) ((i = 1) (k = j+j))

:: []k st st (k = j)

:: []k st st (k = j+j)

Page 12: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 12

Sized Inference : Recursive Function Sized Inference : Recursive Function

Steps:

1. Infer recursive call constraint & terminating constraint.

2. Calculate a generalised transitive constraint.

3. Incorporate terminating constraint.

Page 13: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 13

Example (recursive) Example (recursive)

append xs ys = case xs of

[] -> ys

(x:xs’) -> x:(append xs’ ys)

Terminating Constraint:

B :: [m,n,p]

= = (m = 0) (n 0)

(p=n)

[]m []n []p

[]i []j []k

Recursive Call Constraint:

U :: [m,n,p] [i,j,k]

= = (i+1 = m) (j = n)

(p=k+1)

Page 14: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 14

Fixed Point via Omega Calculator Fixed Point via Omega Calculator

Theoretic Closure (Least fixed point)

U+ = k=1

Uk

U :: [m,n,p][i,j,k]= (i+1=m) (j = n) (p=k+1)

closure(U) ::[m,n,p][m+ ,n+ ,p+ ]

= (0 m+ <m) (n 0) (n+ = n)

(m - m+ = p - p+ )

Practical Fixed Point (via Omega Calculator )

R. P=closure(R) R P R+

Fixed-Point Check:

(P=R+) (P o R P)

Page 15: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 15

Recursive Call Constraint:

U :: [m,n,p] [i,j,k]

= = (i+1 = m) (j = n)

(p=k+1)

Terminating Constraint:

B :: [m,n,p]

= = (m = 0) (n 0)

(p=n)

U+ ::[m,n,p][m+ ,n+ ,p+ ]

= (0 m+ <m) (n 0) (n+ = n)

(m - m+ = p - p+ )

Append :: [m,n,p] = B[m,n,p] ( m+,n+,p+ : U+ (m, n, p, m+,n+,p+) B[m+,n+,p+] )

= ((m=0)(n0)(p=n)) ((m1)(n0)(p=m+n))

= (m0)(n0)(p=m+n)

Page 16: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 16

Prototype System Prototype System

Fun f x y = x + y {[s4,s5] -> [w3] :w3 = s4+s5 }

Fun f1 x = f 0 x {[s14] -> [w3] :s14 = w3 }

Fun append xs ys = case xs of { [] -> ys ; (x:xs') -> x : append xs' ys } {[s26,s27] -> [s28] :s28 = s27+s26 && 0 <= s27 && 0 <= s26 }

Fun f4 xs = append xs xs {[s74] -> [w3] :w3 = 2s74 && 0 <= s74 }

Fun filter p s = case s of { [] -> [] ; (x:xs) -> case p s of { True -> x : filter p xs ;

False -> filter p xs } } {[s120,s121,s122] -> [s123] :0 <= s121 <= 1

&& 0 <= s123 <= s122 && 0 <= s120 }

Page 17: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 17

Prototype System Prototype System

Fun zipB xs ys = case xs of { [] -> ys ; (x:xs') -> case ys of { [] -> [] ;

(y:ys') -> x : zipB xs' ys' } } {[s201,s202] -> [s203] :s203 = s202 && 0 <= s202 && 0 <= s201 }

Fun zipA xs ys = case xs of { [] -> [] ; (x:xs') -> case ys of { [] -> [] ;

(y:ys') -> x : zipA xs' ys' } } {[s235,s236] -> [s237] :0 <= s237 <= s235, s236 }

Fun zip xs ys = case xs of { [] -> ys ; (x:xs') -> case ys of { [] -> xs ;

(y:ys') -> x : zip xs' ys' } } {[s167,s168] -> [s169] :(s167<=s168 && s168 = s169)

OR :(s168<s167 && s167 = s169) }

Page 18: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 18

Why Sized Type is Useful? Why Sized Type is Useful?

• Safety Analysis: termination, bounded space.

• Vector-Based Memoisation.

• Array Bounds Check Elimination.

• Context-based Specialisation.

Page 19: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 19

ack 0 m = m+1

ack (n+1) 0 = ack n 1

ack (n+1) (m+1) = ack n (ack (n+1) m)

Previous work based on abstract interpretation.e.g. Ullman, Jones, Plumer etc.

KEY : Does a given recursion terminate?

Termination Analysis Termination Analysis

Sized type:

ack :: Inti Intj Intk

size size (0 i) (0 j) // pre-condition

inv inv (0 i+ < i) ((i+ = i) (1 i+) (0 j+ <j)) // rec. invariant

Page 20: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 20

Eliminate redundant calls [Chin & Hagiya ICFP97] via vectorisation.

Vector-Based Memoisation Vector-Based Memoisation

bin(n,k) = case n of 0 -> 1m+1 -> if (k <= 0 || k >= n) then 1

else bin(m-1,k-1) + bin(m,k)

Sized typebin :: (Intn

,Intk) -> Intp

invinv.(k+,1 k < n) (k+,1 n+ < n) (k+n+ n+k+) 0 k+

max(0,n++k-n) k+ min(n+,k)

Problem - Bounds Analysis

Page 21: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 21

Vector-Based Memoisation Vector-Based Memoisation

bin(n,k) = case n of 0 -> 1m+1 -> if (k <= 0 || k >= n) then 1

else bin(m-1,k-1) + bin(m,k)

bin(n,k) = let bin’(n+) = case n+ of

0 -> \ k+ -> 1m+1 -> let z = bin’(m)

bn k+ = if (k+ <= 0 || k+ >= n) then 1 else z(k+-1) + z(k+)

l = max(0,n++k-n) u = min(n+,k) vec = array(l,u)[a:=bn(a)| a<-[l..u]] in (!)vec in if (0<=k && max(1,k)<=n) then bin’(n)(k) else 1

max(0,n++k-n) k+ min(n+,k)

Page 22: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 22

Bounds check can be eliminated by invariants over indexes.

Bound Checks Elimination Bound Checks Elimination

bsearch cmp key arr = let look:: (Intlo,Inthi) Intk INV (lo lo+) (hi+ hi) look(lo,hi) = if (hi>=lo) then

let m=lo+(hi-lo)/2; x=arr!m in case cmp(key,x) of

LESS -> look(lo,m-1)EQUAL -> mMORE -> look(m+1,hi)

else -1 in look(0,(length arr)-1)

arr ! m = if (0 m <length(arr)) then sub(arr,m) else

0 lo m hi < length(arr)

Page 23: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 23

zip :: []m []n [(,)]p size size ((0 m n) (p=m)) ((0 n m) (p=n))

zip xs ys = case xs of [] -> [](x:xs’) -> case ys of

[] -> [](y:ys’) -> (x,y):(zip xs’ ys’)

Specialisation can eliminate redundant tests.

Context Specialisation Context Specialisation

zip1 :: []m []n [(,)]p size size (0 m n) (p=m)

zip1 xs ys = case xs of [] -> []

(x:xs’) -> (x,head ys):(zip1 xs’ (tail ys))

zip1 xs ys = zip xs ys stst (xs::[]m) (ys::[]n) (m n)

Page 24: “Calculating Sized Types”  by  Wei-Ngan Chin and Siau-Cheng Khoo published in  PEPM00 and HOSC01

Sized Types Inference 24

zip :: []m []n [(,)]p size size ((0 n m) (p=n)) ((0 m n) (p=m))

zip xs ys = case xs of [] -> []

(x:xs’) -> case ys of [] -> []

(y:ys) -> (x,y):(zip xs’ ys’)

Specialisation can eliminate redundant tests.

Context Specialisation Context Specialisation

zip2 :: []m []n [(,)]p size size (0 n m) (p=n)

zip2 xs ys = case ys of [] -> []

(y:ys’) -> (head xs,y):(zip2 (tail xs) ys’)

zip2 xs ys = zip xs ys stst (xs::[]m) (ys::[]n) (n m)


Recommended