Sized Types Inference1 “Calculating Sized Types” by Wei-Ngan Chin and Siau-Cheng Khoo published in PEPM00 and HOSC01 Presented by Xu Na.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Tom Schrijvers K.U.Leuven, Belgium with Manuel Chakravarty, Martin Sulzmann and Simon Peyton Jones.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Determinacy Inference for Logic Programs Lunjin Oakland University In collaboration with Andy Kent University, UK.
Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Reasoning About Code; Hoare Logic, continued
Inferring Disjunctive Postconditions Corneliu Popeea and Wei-Ngan Chin School of Computing National University of Singapore - ASIAN
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Deterministic Selection and Sorting Prepared by John Reif, Ph.D. Analysis of Algorithms.
Functional Design and Programming Lecture 11: Functional reasoning.
CATCH: Case and Termination Checker for Haskell Neil Mitchell (Supervised by Colin Runciman)
1 CPS Transform for Dependent ML Hongwei Xi University of Cincinnati and Carsten Schürmann Yale University.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
1 COMP313A Programming Languages Functional Programming (7)
From Program Verification to Program Synthesis Saurabh Srivastava * Sumit Gulwani ♯ Jeffrey S. Foster * * University of Maryland, College Park ♯ Microsoft.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
©Ian Sommerville 2000Software Engineering, Chapter 10 Slide 1 Chapter 10 Formal Specification.
Introduction to the λ-Calculus and Functional Programming Languages Arne Kutzner Hanyang University 2015.
Sequences Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Program Correctness. 2 Program Verification An object is a finite state machine: –Its attribute values are its state. –Its methods optionally: Transition.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
Verifying Safety Policies with Size Properties and Alias Controls Wei Ngan Chin 1,2, Siau-Cheng Khoo 1, Shengchao Qin 3, Corneliu Popeea 1, Huu Hai Nguyen.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Deriving Pre-Condition for ABCE1 Deriving Preconditions for Array Bound Check Elimination Dana N. XU Joint work with W.N. CHIN and S.C. KHOO Dept of Computer.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
©Ian Sommerville 2000Software Engineering, Chapter 10 Slide 1 Chapter 10 Formal Specification.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Lesson 10 Type Reconstruction
Types CSCE 314 Spring 2016.
dr Robert Kowalczyk WMiI UŁ
ML: a quasi-functional language with strong typing
Theory of Computation Lecture 4: Programs and Computable Functions II
Advanced Functional Programming
Extended Static Checking for Haskell (ESC/Haskell)
CSE 341 Section 5 Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
PROGRAMMING IN HASKELL
Presentation transcript:

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

Sized Types Inference2 Int m – size of an integer is the integer itself [  ] m – size of a recursive data consists of its maximum depth. Bool m – 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. Meaning of Size

Sized Types Inference3 Syntax of Sized Type e :: ( ,  ) annotated type size constraint Examples: 3 :: (Int i, (i = 3) ) [3] :: ([Int i ] j, (i = 3)  (j = 1) ) [3,4] :: ([Int i ] j, ((i = 3)  (i = 4))  (j = 2) ) :: ([Int  ] j, (j = 2) ) True :: (Bool b, (b = 1) ) False :: (Bool b, (b = 0) ) append :: ([  ] m  [  ] n  [  ] p, (m  0)  (n  0)  (p=m+n) )

Sized Types Inference4 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 Inference5 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 of integer types linear arithmetic constraints on size variables

Sized Types Inference6 Example: zip :: [  ]  [  ]  [( ,  )] zip [] [] = [] zip (x:xs) (y:ys) = (x,y):(zip xs ys) Sized type: zip :: [  ] m  [  ] n  [( ,  )] p size size (m  0)  (n  0)  (n=m) // pre-condition  (p=m) // output relation inv inv (0  m + <m)  (0  n + <n)  (m – m + = n – n + ) // invariant (zip xs m ys n )  …(zip xs 1 m 1 ys 1 n 1 )…  …(zip xs 2 m 2 ys 2 n 2 )…  … (m +,n + )  {(m 1,n 1 ),(m 2,n 2 ),…,…}

Sized Types Inference7 Syntax of Constraints

Sized Types Inference8 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 Inference9 Example f b xs = case b of False -> xs True -> append xs xs Assume: st append :: [  ] m  [  ] n  [  ] p st (m  0)  (n  0)  (p=m+n) f :: Bool i  [  ] j  [  ] k infer : infer : ((i = 0)  (k = j))  ((i = 1)  (k = j+j)) What is the sized type for f?

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

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

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

Sized Types Inference13 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)

Sized Types Inference14 Fixed Point via Omega Calculator Theoretic Closure (Least fixed point) U + =   k=1 U k 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)

Sized Types Inference15 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)  (n  0)  (p=n))  ((m  1)  (n  0)  (p=m+n)) = (m  0)  (n  0)  (p=m+n)

Sized Types Inference16 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 Inference17 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 Inference18 Why Sized Type is Useful? Safety Analysis: termination, bounded space. Vector-Based Memoisation. Array Bounds Check Elimination. Context-based Specialisation.

Sized Types Inference19 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 Sized type: ack :: Int i  Int j  Int k size size (0  i)  (0  j) // pre-condition inv inv (0  i + < i)  ((i + = i)  (1  i + )  (0  j + < j)) // rec. invariant

Sized Types Inference20 Eliminate redundant calls [Chin & Hagiya ICFP97] via vectorisation. Vector-Based Memoisation bin(n,k) = case n of 0 -> 1 m+1 -> if (k = n) then 1 else bin(m-1,k-1) + bin(m,k) Sized type bin :: (Int n, Int k ) -> Int p inv inv.(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

Sized Types Inference21 Vector-Based Memoisation bin(n,k) = case n of 0 -> 1 m+1 -> if (k = n) then 1 else bin(m-1,k-1) + bin(m,k) bin(n,k) = let bin’(n + ) = case n + of 0 -> \ k + -> 1 m+1 -> let z = bin’(m) bn k + = if (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)

Sized Types Inference22 Bounds check can be eliminated by invariants over indexes. Bound Checks Elimination bsearch cmp key arr = let look:: (Int lo,Int hi )  Int k 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  0  lo  m  hi < length(arr)

Sized Types Inference23 size 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’) Specialisation can eliminate redundant tests. Context Specialisation size 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)) zip1 xs ys = zip xs ys st st (xs::[  ] m )  (ys::[  ] n )  (m  n)

Sized Types Inference24 size 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’) Specialisation can eliminate redundant tests. Context Specialisation size 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’) zip2 xs ys = zip xs ys st st (xs::[  ] m )  (ys::[  ] n )  (n  m)