Type Inference def CONS[T]( x:T , lst:List [T]):List[T]={...}

# Type Inference def CONS[T]( x:T , lst:List [T]):List[T]={...}

Télécharger la présentation

## Type Inference def CONS[T]( x:T , lst:List [T]):List[T]={...}

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Type Inference defCONS[T](x:T, lst:List[T]):List[T]={...} deflistInt() : List[Int] = {...} deflistBool() : List[Bool] = {...} defbaz(a, b) = CONS(a(b), b)def test(f,g) = (baz(f,listInt), baz(g,listBool))

2. Solving ‘baz’ def CONS[T](x:T, lst:List[T]) : List[T] = {…}def baz(a:TA, b:TB):TD = CONS(a(b):TC, b:TB):TDTA = (TB => TC) CONS : T1 x List[T1] => List[T1] TC = T1 TB = List[T1] TD = List[T1] TA = (List[T1]=>T1) Solved form. Generalize over T1 def baz[T1](a:List[T1]=>T1,b:List[T1]):List[T1] = CONS[T1](a(b),b)

3. Using generalized ‘baz’ def baz[T1](a: List[T1]=>T1,b:List[T1]):List[T1] = CONS[T1](a(b),b) deftest(f,g) = (baz(f,listInt), baz(g,listBool)) test : (List[Int] => Int) x (List[Bool] => Bool) => List[Int] x List[Bool]

4. Omega (Iteration) Function Bonus: Type check omega function defw(f)(x) = f(w(f)(f(x))) defw(f:TF)(x:TX):TR = f(w(f)(f(x):TA):TB) TF = TX => TA TA = TX omega: TF x TX => TR TR = TB TF = TB => TR Therefore : TF = TX => TX TX => TX == TB => TB TB = TX def w[T](f: T => T, x: T): T

5. Find most general types def twice(f:TF,x:TX): TR=f(f(x)) TF = A => B A = TX A = B TR = B therefore: deftwice[T](f: T => T, x: T): T = f(f(x))

6. Self-Application Type Check Attempt def selfApp(f) = f(f) defselftApp(f:TF):TR = (f:TF)(f:TF) TF = (TF => TR) because of the OCCURS CHECK, this constraint has no solutions. Therefore, we have a type error.

7. Try to infer types for Y combinator In λ calculus: λf.(λx.f (x x)) (λx.f (x x)) In our language: def Y(f) = { def repeat(x) = { f(x(x)) } repeat(repeat) }

8. Try to infer types for Y combinator defY(f:TF) = { defrepeat(x:TX) = { f(x(x):TA):TB }(repeat(repeat:TC)):TD } So Y : TF => TD and repeat : TX => TB x(x):TA gives: TX = TX => TA  no solution for this constraint, f(x(x)):TB gives: TF = TA => TB so we should report a type error. repeat:TC gives: TC = TX => TB repeat(repeat) : (TX => TB) = (TC => TD)

9. Reminder : physical units A unit expression is defined by following grammar u, v := b | 1 | u * v | u-1 where u, v are unit expressions themselves and b is a base unit: b := m | kg | s | A | K | cd | mol You may use B to denote the set of the unit types B = { m, kg, s, A, K, cd, mol} For readability reasons, we use the syntactic sugar u^n= u * … * u if n > 0 1 if n = 0 u-1 * … * u-1 if n < 0 and u/v = u * v-1

10. Reminder: Rules Γ˫a : U Γ˫b : U Γ˫a : U Γ˫b : V Γ˫a + b : U Γ˫a * b : U*V Γ˫a : U*U Γ˫a : U Γ˫b : V Γ˫a / b : U/V Γ˫√a: U Γ˫a : U Γ˫a : (U*U)-1 Γ˫a : U Γ˫a : 1 Γ˫a : simplify(U) Γ˫√a: U-1 Γ˫abs(a): U Γ˫sin(a): 1

11. Physical Units Type Inference val g = 9.87.m / (1.s * 1.s) valhref = 1.92.m def f(x, y, z) = sqrt(x/y) + z def T(L) = 2*pi*sqrt(L/g) + 0.s deffall(t, v) = -0.5*g*t*t + t*v + href deffreq(t, w) = href * sin(2*pi*t*w) + g*(t/w) defeinstein(E, p, v) = E – p*v*v == 0 && E/1.s – p*g*v == 0 && p > 0.kg def prof(a, b) = if(b > 1.s) sqrt(prof(a+a,b-1.s)/b) else a

12. def f(x, y, z) = sqrt(x/y) + z TX TY TZ TX TY :TR Tx/y Tsqrt T+ T+ = TRTsqrt = T+TZ = T+Tsqrt * Tsqrt = Tx/yTx/y = TX / TY Therefore: Tsqrt = TR TZ = TR TR*TR = TX / TY soTX = TR*TR/TY def f[T,V](x: T*T/V, y:V, z: T): T

13. def T(L) = 2pi*sqrt(L/g) + 0.s TL :TR 1 TLG TSQRT TM TP TR = TP TP = s TP = TM TM = 1 * TSQRT TSQRT*TSQRT = TLG TLG = TL / (m/(s*s)) Therefore: TR = s s = TQSRT s * s = TLG s*s = TL / (m/(s*s)) So: TL = m and T(L: <m>): <s>

14. deffall(t, v) = (-0.5*g*t*t + t*v) + href TT TV :TR 1 TM1 TM4 TM2 TM3 TP1 TP2 TR = TP2, TM1 = 1*m/(s*s), TM2 = TM1*TT, TM3 = TM2*TT, TM4 = TT*TV, TP1 = TM4, TP1 = TM3, TP2 = TP1, TP2 = m Therefore: TM2 = m/(s*s)*TT, TM3 = m/(s*s)*TT*TT, TP1 = TT*TV, TP1 = m/(s*s)*TT*TT, TT*TV = m/(s*s)*TT*TT TV = m/(s*s)*TT TR = m, TP1 = m, so m = m/(s*s)*TT*TT, thereforeTT=s TV = m/s fall(t: <s>): <m>

15. deffreq(t, w) = href* sin(2pi*t*w) + g*(t/w) TT TW: TR m 1 1 TT TW m/s² TTOW TTW TM2 TM1 TP TP = TM1, TM1 = TM2, TM2 = m/s²*TTOW, TTOW=TT/TW, TTW = TT*TW, TTW = 1, TM1 = m*1, TR = TP Therefore TR = m m = m/s² * TTOW, so TT/TW = s² 1 = TT * TW so TW = 1/TT and TT² = s², soTT = s and TW = 1/s deffreq(t: <s>, w: <1/s>): <m>

16. defein(E, p, v): Bool = TE TP TV E – p*v*v == 0 && E/1.s – p*g*v == 0 && p > 0.kg TE TM1 TES TM2 TP = kg, TE = TM1, TM1 = TP * TV², TES = TE/s, TES = TM2, TM2 = TP*m/s²*TV TE/s = TM2, TE/s = kg*m/s²*TV, TE = kg*TV² So kg*TV²/s = kg*m/s²*TV therefore: TV = m/s, TE = kg*m²/s² defein(E: <kg*m²/s²>, p: <kg>, v: <m/s>): Bool

17. def prof(a, b) = TA TB :TR if(b > 1.s) sqrt(prof(a+a,b-1.s)/b) elsea BOOL TPA TB1 TR TRB TSQRT TB = s TSQRT = TA= TR TQSRT*TSQRT = TRB TRB = TR / TB TA = TA = TA = TA, TB = s = TB TR * TR = TR / s soTR = 1/s, TA = 1/s defprof(a: <1/s>, b: <s>): <1/s>

18. Theorem Theorem: Suppose that the result type T does not contain a base unit b1(or, equivalently, this base type occurs only with the overall exponent 0, as b10). If we multiply all variables of type b1by a fixed numerical constant K, the final result of the expression does not change. Question 1: Generalize the theorem. Question 2: Prove the theorem.

19. Theoremexamples • Center of mass x: <m> = (x1*m1 + x2*m2)/(m1+m2) Multiplying all mass variables by 2 does not change the center of mass. • Gravity estimation t1 = √(t2^2 * h1/h2) Changing h1 and h2 by any factor will not change the time.

20. Solution – part I Lemma: If wemultiply all variables of type B by constant K, and the result has type T where B has exponent N, then the value of the expression ismultiplied by K^N

21. Solution – part II Suppose thatwe have proved the lemma for expressions of size < n. Let give us an expression of size n. If the last applied rule is +, then: Let us assume that B appears in U withexponent N. If wemultiply all variables of type B in E and F by K, by recurrenceE ismultiplied by K^n. Therefore E+F istransformed to (E*K^N + F*K^N) = (E+F)*K^N Otherrules are similar. Γ˫E : U Γ˫F : U Γ˫E + F : U