1 / 24

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

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

cloris
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 Sized Types Inference

  2. Meaning of Size 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 True append:: []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. Sized Types Inference

  3. annotated type size constraint Syntax of Sized Type e :: (,) 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,(m0)  (n0)  (p=m+n) ) Sized Types Inference

  4. 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 Sized Types Inference

  5. size variables of integer types linear arithmetic constraints on size variables Example: tail (x:xs) = xs polymorphic type: tail :: []  [] sized type: tail :: []m []p size (m>0) // pre-condition  (p+1=m) // output relation Sized Types Inference

  6. zip :: []  []  [(,)] zip [] [] = [] zip (x:xs) (y:ys) = (x,y):(zip xs ys) Example: Sized type: zip :: []m []n [(,)]p size (m0) (n0) (n=m) // pre-condition  (p=m) // output relation inv (0m+<m)  (0n+<n)  (m– m+= n – n+) // invariant (zip xsm ysn)  …(zip xs1m1 ys1n1)…  …(zip xs2m2 ys2n2)…  … (m+,n+){(m1,n1),(m2,n2),…,…} Sized Types Inference

  7. Syntax of Constraints Sized Types Inference

  8. Sized Type Inference 1. Extension of Polymorphic Type Rules.2. Requires linear arithmetic constraint solver.3. Recursive letrec requires new fixed point computation. Sized Types Inference

  9. Example f b xs = case b of False -> xs True -> append xs xs Assume: append :: []m []n []p st (m0)  (n0)  (p=m+n) f :: Booli []j []k What is the sized type for f? infer : ((i = 0)  (k = j))  ((i = 1)  (k = j+j)) Sized Types Inference

  10. variable-rule application-rule Example (non-recursive) f b xs = case b of False -> xs True -> append xs xs f :: Booli []j []k append :: []m []n []p st (m0)  (n0)  (p=m+n) xs :: []kst (k = j) (append xs xs) :: []kst (k = j+j) Sized Types Inference

  11. case-rule (case b ...) :: []kst ((i = 0)  (k = j))  ((i = 1)  (k = j+j)) Example (non-recursive) f :: Booli []j []k f b xs = case b of False -> xs True -> append xs xs :: []kst (k = j) :: []kst (k = j+j) Sized Types Inference

  12. Sized Inference : Recursive Function Steps:1. Infer recursive call constraint & terminating constraint.2. Calculate a generalised transitive constraint.3. Incorporate terminating constraint. Sized Types Inference

  13. []i []j[]k []m []n[]p 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) Recursive Call Constraint: U :: [m,n,p]  [i,j,k] = (i+1 = m)  (j = n)  (p=k+1) Sized Types Inference

  14. Fixed Point via Omega Calculator Theoretic Closure (Least fixed point) U+ = k=1Uk Practical Fixed Point (via Omega Calculator )  R. P=closure(R)  R  P  R+ Fixed-Point Check: (P=R+)  (P o R  P) 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+ ) Sized Types Inference

  15. Terminating Constraint: B :: [m,n,p] = (m = 0)  (n  0)  (p=n) Recursive Call Constraint: U :: [m,n,p]  [i,j,k] = (i+1 = m)  (j = n)  (p=k+1) 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)(n0)(p=n))  ((m1)(n0)(p=m+n)) = (m0)(n0)(p=m+n) Sized Types Inference

  16. 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 } Sized Types Inference

  17. 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) } Sized Types Inference

  18. Why Sized Type is Useful? • Safety Analysis: termination, bounded space. • Vector-Based Memoisation. • Array Bounds Check Elimination. • Context-based Specialisation. Sized Types Inference

  19. Termination Analysis Previous work based on abstract interpretation. e.g. Ullman, Jones, Plumer etc.KEY : Does a given recursion terminate? ack 0 m = m+1 ack (n+1) 0 = ack n 1 ack (n+1) (m+1) = ack n (ack (n+1) m) Sized type: ack :: Inti Intj Intk size (0 i) (0 j) // pre-condition inv (0 i+<i) ((i+ = i)  (1i+)  (0j+ <j)) // rec. invariant Sized Types Inference

  20. max(0,n++k-n)  k+  min(n+,k) Vector-Based Memoisation Eliminate redundant calls [Chin & Hagiya ICFP97] via vectorisation. bin(n,k) = case n of 0 -> 1 m+1 -> if (k <= 0 || k >= n) then 1 else bin(m-1,k-1) + bin(m,k) Sized type bin :: (Intn,Intk) -> Intp inv.(k+,1  k < n)  (k+,1  n+ < n)  (k+n+ n+k+)  0  k+ Problem - Bounds Analysis Sized Types Inference

  21. bin(n,k) = let bin’(n+) = case n+ of 0 -> \ k+ -> 1 m+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 Vector-Based Memoisation bin(n,k) = case n of 0 -> 1 m+1 -> if (k <= 0 || k >= n) then 1 else bin(m-1,k-1) + bin(m,k) max(0,n++k-n)  k+ min(n+,k) Sized Types Inference

  22. 0  lo  m  hi < length(arr) Bound Checks Elimination Bounds check can be eliminated by invariants over indexes. 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 -> m MORE -> look(m+1,hi) else -1 in look(0,(length arr)-1) arr ! m = if (0m <length(arr)) then sub(arr,m) else  Sized Types Inference

  23. zip1 xs ys = zip xs ys st(xs::[]m)  (ys::[]n)  (m  n) Context Specialisation Specialisation can eliminate redundant tests. zip :: []m []n [(,)]p 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’) zip1 :: []m []n [(,)]p size (0  m  n)  (p=m) zip1 xs ys = case xs of [] -> [] (x:xs’) -> (x,head ys):(zip1 xs’ (tail ys)) Sized Types Inference

  24. zip2 xs ys = zip xs ys st(xs::[]m)  (ys::[]n)  (n  m) Context Specialisation Specialisation can eliminate redundant tests. zip :: []m []n [(,)]p 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’) zip2 :: []m []n [(,)]p size (0  n  m)  (p=n) zip2 xs ys = case ys of [] -> [] (y:ys’) -> (head xs,y):(zip2 (tail xs) ys’) Sized Types Inference

More Related