Calculating Sized Types - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Calculating Sized Types

Description:

Context-based Specialisation. Sized Types Inference. 19. ack 0 m = m 1. ack (n 1) 0 = ack n 1 ... Specialisation can eliminate redundant tests. Context Specialisation ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 25
Provided by: dis75
Category:

less

Transcript and Presenter's Notes

Title: Calculating Sized Types


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

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 m0 for False and m1 for True
  • append ?m ! ?n ! ?p
  • s.t. m0 Æ n0 Æ pmn
  • size of a function is a relation between its
    input and output sizes.

3

Syntax of Sized Type
e (?,?)
Examples 3 (Inti, (i 3) ) 3
(Intij, (i 3) ? (j 1) ) 3,4
(Intij, ((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) ? (pmn)
)
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

5
Example
tail (xxs) xs
polymorphic type tail ? ? ?
sized type tail ?m ? ?p size (mgt0)
// pre-condition ? (p1m) // output
relation
6
Example
zip ? ? ? ? (?,?) zip zip
(xxs) (yys) (x,y)(zip xs ys)
Sized type zip ?m ? ?n ? (?,?)p size
(m?0) ? (n?0) ? (nm) // pre-condition
? (pm) // output relation inv (0?m ltm) ?
(0?n ltn) ? (m m n n ) // invariant
(zip xsm ysn) ? (zip xs1m1 ys1n1) ? (zip xs2m2
ys2n2) ? (m,n) ?(m1,n1),(m2,n2),,
7

Syntax of Constraints
8

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

Example
f b xs case b of False -gt xs True -gt append
xs xs
Assume
append ?m ? ?n ? ?p st (m?0) ? (n?0)
? (pmn) f Booli ? ?j ? ?k
What is the sized type for f?
infer ((i 0) ? (k j)) ? ((i 1) ? (k
jj))
10

Example (non-recursive)
f b xs case b of False -gt xs True -gt append
xs xs
f Booli ? ?j ? ?k append ?m ? ?n
? ?p st (m?0) ? (n?0) ? (pmn)
xs ?k st (k j) (append xs xs) ?k
st (k jj)
11

Example (non-recursive)
f Booli ? ?j ? ?k f b xs case b of
False -gt xs True -gt append xs xs
?k st (k j)
?k st (k jj)
12

Sized Inference Recursive Function
Steps1. Infer recursive call constraint
terminating constraint.2. Calculate a
generalised transitive constraint.3.
Incorporate terminating constraint.
13

Example (recursive)
append xs ys case xs of -gt ys (xxs) -gt
x(append xs ys)
Terminating Constraint B m,n,p (m
0) ? (n ? 0) ? (pn)
Recursive Call Constraint U m,n,p ?
i,j,k (i1 m) ? (j n) ?
(pk1)
14
Fixed Point via Omega Calculator
Theoretic Closure (Least fixed point) U ??k1
Uk
Practical Fixed Point (via Omega Calculator
) ? R. Pclosure(R) ? R ? P ? R Fixed-Point
Check (PR) ? (P o R ? P)
U m,n,p?i,j,k (i1m) ? (j n) ?
(pk1) closure(U) m,n,p?m ,n ,p
(0? m ltm) ? (n ? 0) ? (n n) ? (m - m
p - p )
15
Recursive Call Constraint U m,n,p ?
i,j,k (i1 m) ? (j n) ?
(pk1)
Terminating Constraint B m,n,p (m
0) ? (n ? 0) ? (pn)
U m,n,p?m ,n ,p (0? m ltm) ? (n ?
0) ? (n n) ? (m - m p - p )
Append m,n,p Bm,n,p ? (? m,n,p U
(m, n, p, m,n,p) ? Bm,n,p )
((m0)?(n?0)?(pn)) ? ((m?1)?(n?0)?(pmn))
(m?0)?(n?0)?(pmn)
16

Prototype System
Fun f x y x y s4,s5 -gt w3 w3 s4s5
Fun f1 x f 0 x s14 -gt w3 s14 w3
Fun append xs ys case xs of -gt ys
(xxs') -gt x append xs' ys s26,s27 -gt
s28 s28 s27s26 0 lt s27 0 lt s26
Fun f4 xs append xs xs s74 -gt w3 w3
2s74 0 lt s74 Fun filter p s case s of
-gt (xxs) -gt case p s of True -gt x
filter p xs False -gt filter p xs
s120,s121,s122 -gt s123 0 lt s121 lt 1
0 lt s123 lt s122 0 lt s120
17

Prototype System
Fun zipB xs ys case xs of -gt ys
(xxs') -gt case ys of -gt (yys')
-gt x zipB xs' ys' s201,s202 -gt s203
s203 s202 0 lt s202 0 lt s201 Fun
zipA xs ys case xs of -gt (xxs') -gt
case ys of -gt (yys') -gt x zipA
xs' ys' s235,s236 -gt s237 0 lt s237
lt s235, s236 Fun zip xs ys case xs of
-gt ys (xxs') -gt case ys of -gt xs
(yys') -gt x zip xs' ys' s167,s168
-gt s169 (s167lts168 s168 s169) OR
(s168lts167 s167 s169)
18
Why Sized Type is Useful?
  • Safety Analysis termination, bounded space.
  • Vector-Based Memoisation.
  • Array Bounds Check Elimination.
  • Context-based Specialisation.

19

Termination Analysis
Previous work based on abstract
interpretation. e.g. Ullman, Jones, Plumer
etc.KEY Does a given recursion terminate?
ack 0 m m1 ack (n1) 0 ack n 1 ack (n1)
(m1) ack n (ack (n1) m)
Sized type ack Inti ? Intj ? Intk size
(0 ? i) ? (0 ? j) // pre-condition inv (0
? i lt i) ? ((i i) ? (1? i) ? (0? j ltj)) //
rec. invariant
20

Vector-Based Memoisation
Eliminate redundant calls Chin Hagiya ICFP97
via vectorisation.
bin(n,k) case n of 0 -gt 1 m1 -gt if (k lt
0 k gt n) then 1 else bin(m-1,k-1) bin(m,k)
Sized type bin (Intn,Intk) -gt Intp inv.(k,1 ?
k lt n) ? (k,1 ? n lt n) ? (kn ? nk)
? 0 ? k
Problem - Bounds Analysis
21

Vector-Based Memoisation
bin(n,k) case n of 0 -gt 1 m1 -gt if (k lt
0 k gt n) then 1 else bin(m-1,k-1) bin(m,k)
max(0,nk-n) ? k ? min(n,k)
22

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 (higtlo) then let
mlo(hi-lo)/2 xarr!m in case
cmp(key,x) of LESS -gt look(lo,m-1) EQUAL -gt
m MORE -gt look(m1,hi) else -1 in
look(0,(length arr)-1)
arr ! m if (0 ? m ltlength(arr)) then
sub(arr,m) else ?
23

Context Specialisation
Specialisation can eliminate redundant tests.
zip ?m ? ?n ? (?,?)p size ((0 ? m ? n)
? (pm)) ? ((0 ? n ? m) ? (pn)) zip xs ys case
xs of -gt (xxs) -gt case ys of -gt
(yys) -gt (x,y)(zip xs ys)
zip1 ?m ? ?n ? (?,?)p size (0 ? m ? n)
? (pm) zip1 xs ys case xs of -gt
(xxs) -gt (x,head ys)(zip1 xs (tail ys))
24

Context Specialisation
Specialisation can eliminate redundant tests.
zip ?m ? ?n ? (?,?)p size ((0 ? n ? m)
? (pn)) ? ((0 ? m ? n) ? (pm)) zip xs ys case
xs of -gt (xxs) -gt case ys of -gt
(yys) -gt (x,y)(zip xs ys)
zip2 ?m ? ?n ? (?,?)p size (0 ? n ? m)
? (pn) zip2 xs ys case ys of -gt
(yys) -gt (head xs,y)(zip2 (tail xs) ys)
Write a Comment
User Comments (0)
About PowerShow.com