operational aspects of type systems
play

Operational Aspects of Type Systems Inter-Derivable Semantics of - PowerPoint PPT Presentation

Operational Aspects of Type Systems Inter-Derivable Semantics of Type Checking and Gradual Types for Object Ownership Ilya Sergey 14 November 2012 Types A type - is a set of data instances and operations on them true , false boolean =


  1. Operational Aspects of Type Systems Inter-Derivable Semantics of Type Checking and Gradual Types for Object Ownership Ilya Sergey 14 November 2012

  2. Types

  3. A type τ - is a set of data instances and operations on them true , false boolean = int 0, 1, -1, 2, ... = “abc”, kuleuven”, ... string = array = [1, 2, 3], [ true , “a”]

  4. A type τ - is a statement in a constructive logic ( A, B ) → A A ∧ B ⇒ A ≈ A typed program of type τ - is a proof of the statement A B λ ( x, y ) : ( A, B ) . x ≈ ∧ -left A

  5. Types help to recognize bad programs ? z }| { =? 3 × 2 × + | {z } | {z } crocodiles apples

  6. Type Systems

  7. Assigning Types To Programs Program Environment Type Γ ` ς : τ • Well-typed programs cannot go wrong • R. Milner, 1978 • Well-typed programs cannot get stuck • A. Wright and M. Felleisen, 1992 • Well-typed programs cannot be blamed • P . Wadler, 2009

  8. Type Systems Well-Typed Programs Don’t Go Wrong Γ , ∆ ` ς 0 : τ ς 0 ς 1 ς 2 ς n ς final . . . ς n ς 0 ς 1 ς 2 . . . . . .

  9. Type Systems Well-Typed Programs Don’t Go Wrong Γ , ∆ ` ς 0 : τ But not ς 0 ς 1 ς 2 ς n . . .

  10. Type Checking

  11. A Simple Language :: = n | x | λ x : τ . e | e e Expressions e :: = Numbers n number :: = n | λ x : τ . e Values v τ :: = num | τ → τ Types :: = 0 | Γ , x : τ Γ Typing environments / ( x : τ ) ∈ Γ Γ , x : τ 1 ⊢ e : τ 2 (t-var) (t-lam) Γ ⊢ x : τ Γ ⊢ λ x : τ 1 . e : τ 1 → τ 2 Γ ⊢ e 1 : τ 1 → τ 2 Γ ⊢ e 2 : τ 1 (t-app) (t-num) Γ ⊢ e 1 e 2 : τ 2 Γ ⊢ number : num Type-checking inference rules

  12. Another ill-typed program (which also goes wrong) f = λ x : num → num . λ y : num . x y ( λ z : num . x z ) ( ) 2 = f 1

  13. Type Checking via Inference Rules { x : num ! num , . . . } ` x : num ! num { x : num ! num , . . . } ` x : num ! num { z : num . . . } ` z : num { y : num . . . } ` y : num { x : num ! num , z : num , . . . } ` x z : num num { x : num ! num , y : num } ` x y : ( num ! num ) ! τ { x : num ! num , . . . } ` λ z : num . x z : num ! num { x : num ! num , y : num } ` x y ( λ z : num . x z ) : τ { x : num ! num } ` λ y : num . x y ( λ z : num . x z ) : τ ; ` λ x : num ! num . λ y : num . x y ( λ z : num . x z ) : τ

  14. The Context Understanding and Tracing a Type System

  15. ( x : τ ) ∈ Γ Γ , x : τ 1 ⊢ e : τ 2 (t-var) (t-lam) Γ ⊢ x : τ Γ ⊢ λ x : τ 1 . e : τ 1 → τ 2 Γ ⊢ e 1 : τ 1 → τ 2 Γ ⊢ e 2 : τ 1 (t-app) (t-num) Γ ⊢ e 1 e 2 : τ 2 Γ ⊢ number : num

  16. 14

  17. Thinking of a Type System Operationally

  18. Type Checking as a Rewriting System G. Kuan, D. MacQueen, R. B. Findler A Rewriting Semantics for Type Inference, ESOP 07

  19. Tracing Type Error Origin

  20. Type Checking as an Abstract Machine C. Hankin, D. Le Métayer. Deriving Algorithms From Type Inference Systems, POPL 94

  21. Recovering Type Checking Context

  22. The Problem Equivalence of Type Checking Semantics ( 1 ) ? ? ≈ ≈ ( 3 ) ( 2 )

  23. A Hard Solution To prove soundness and completeness ( 1 ) ( 2 ) G. Kuan, D. MacQueen, R. B. Findler, ESOP 07 ≈ ( 1 ) ≈ ( 3 ) C. Hankin, D. Le Métayer, POPL’94 • Non-reusable, should be proven for each new pair of semantics • Should be done a posteriori, after the semantics is constructed

  24. Our Solution Applying the Functional Correspondence Constructive Proof Semantics B Semantics A of Correspondence Semantics-preserving Interpreter B Interpreter A Program Transformations

  25. Example: Semantics of Fibonacci Numbers (fib-1) (fib-2) 1 ⇓ fib 1 2 ⇓ fib 1 ( n − 1 ) ⇓ fib v 1 ( n − 2 ) ⇓ fib v 2 (fib-n) n ⇓ fib v 1 + v 2 fun fib0 n = if n = 1 orelse n = 2 then 1 else let val v1 = fib0 (n - 1) val v2 = fib0 (n - 2) in v1 + v2 end

  26. Example: Semantics of Fibonacci Numbers fun fib_stack (s: int list , n: int) = if n = 1 orelse n = 2 then 1 :: s else let val s1 = fib_stack (s, n - 1) val s2 = fib_stack (s1, n - 2) in case s2 of v1 :: v2 :: s3 => (v1 + v2) :: s3 end fun fib1 n = fib_stack (nil , n)

  27. Example: Semantics of Fibonacci Numbers fun fib_cps (s, n, k) = if n = 1 orelse n = 2 then k (1 :: s) else fib_cps (s, n - 1, fn s1 => fib_cps (s1, n - 2, fn s2 => case s2 of v1 :: v2 :: s3 => k ((v1 + v2) :: s3))) fun fib2 n = fib_cps (nil , n, fn (x :: ) => x )

  28. Example: Semantics of Fibonacci Numbers datatype cont = CONT_MT | CONT_FIB1 of int * cont | CONT_FIB2 of cont fun fib_defun (s, n, C) = if n = 1 orelse n = 2 then continue (1 :: s, C) else fib_defun (s, n - 1, CONT FIB1 (n, C) ) and continue (s, CONT MT ) = ( case s of (x :: _) => x) | continue (s, CONT FIB1 (n, C) ) = fib_defun (s, n - 2, CONT FIB2 C ) | continue (s, CONT FIB2 C ) = case s of (v1 :: v2 :: s3) => continue ((v1 + v2) :: s3, C) fun fib3 n = fib_defun (nil , n, CONT MT )

  29. Example: Semantics of Fibonacci Numbers datatype cont ’ = CONT_MT ’ | CONT_FIB1 ’ of int * cont ’ | CONT_FIB2 ’ of cont ’ | NUM ’ of int * cont ’ fun fib_defun ’ (s, NUM’ (n, C) ) = if n = 1 orelse n = 2 then continue1 (1 :: s, C) else fib_defun ’ (s, NUM ’ (n - 1, CONT_FIB1 ’ (n, C))) and continue1 (s, CONT MT’ ) = ( case s of (x :: _) => x) | continue1 (s, CONT FIB1’ (n, C) ) = fib_defun ’ (s, NUM ’ (n - 2, CONT_FIB2 ’ C)) | continue1 (s, CONT FIB2’ C ) = case s of (v1 :: v2 :: s3) => continue1 ((v1 + v2) :: s3, C) fun fib4 n = fib_defun ’ (nil , NUM ’ (n, CONT_MT ’))

  30. Example: Semantics of Fibonacci Numbers datatype control_element = NUM of int | CF1 of int | CF2 fun fib_control (s, NUM n :: C ) = if n = 1 orelse n = 2 then fib_control (1 :: s, C) else fib_control(s, NUM (n - 1) :: CF1 n :: C) | fib_control (s, CF1 n :: C ) = fib_control (s, NUM (n - 2) :: CF2 :: C) | fib_control (s, CF2 :: C ) = ( case s of (v1 :: v2 :: s3) => fib_control ((v1 + v2) :: s3, C)) | fib_control (s, nil ) = ( case s of (x :: _) => x) fun fib5 n = fib_control (nil , NUM n :: nil)

  31. Example: Semantics of Fibonacci Numbers � S , Num ( 1 ) :: C � ⇒ SC fib � 1 :: S , C � � S , Num ( 2 ) :: C � ⇒ SC fib � 1 :: S , C � � S , Num ( n ) :: C � ⇒ SC fib � S , Num ( n − 1 ) :: CF 1 ( n ) :: C � � S , CF 1 ( n ) :: C � ⇒ SC fib � S , Num ( n − 2 ) :: CF 2 :: C � � v 1 :: v 2 :: S , CF 2 :: C � ⇒ SC fib � ( v 1 + v 2 ) :: S , C � type state = int list * control_element list (* step : state -> state *) fun step ( s, NUM 1 :: C ) = (1 :: s, C) | step ( s, NUM 2 :: C ) = (1 :: s, C) | step ( s, NUM n :: C ) = (s, NUM (n - 1) :: CF1 n :: C) | step (s, CF1 n :: C ) = (s, NUM (n - 2) :: CF2 :: C) | step ( v1 :: v2 :: s3, CF2 :: C ) = ((v1 + v2) :: s3, C) (* step : state -> int *) fun iterate (v :: _, nil) = v | iterate (s, C) = iterate (step (s, C))

  32. Functional Correspondence, applied • Evaluators with computational effects [Ager-al:TCS05] • Object calculi inter-derivation [Danvy-Johannsen:JCSS10] • Landin’s SECD machine [Danvy-Millikin:LMCS08] • Abstract machine for call-by-need lambda calculus [Ager-al:IPL04, Danvy-al:FLOPS10] • Formalizing semantics of Scheme [Biernacka-Danvy:LNCS5700] • Abstract Interpretation-based analyses [VanHorn-Might:ICFP10] • ...

  33. Operational Aspects of Type Systems Inter-Derivable Semantics of Type Checking and Gradual Types for Object Ownership

  34. Based on the Publications • Ilya Sergey and Dave Clarke. A correspondence between type checking via reduction and type checking via evaluation Information Processing Letters, January 2012. Elsevier. • Ilya Sergey and Dave Clarke. A correspondence between type checking via reduction and type checking via evaluation Accompanying code overview CW Reports, volume CW617. KU Leuven. January 2012. • Ilya Sergey and Dave Clarke. From type checking by recursive descent to type checking with an abstract machine In proceedings of the 11th Workshop on Language Descriptions, Tools and Applications (LDTA 2011), March 2011. ACM. A part of this work was carried out while visiting the BRICS PhD School of Aarhus University in September 2010.

  35. Employed Program Transformations • CPS Transformation • Lightweight Fusion • Direct-style • Lambda Lifting transformation • Closure Conversion • Defunctionalization • Control Stack Extraction • Refunctionalization • Refocusing • Transition compression

  36. � � � � � � The Resulting Derivation ( 2 ) ( 3 ) Reduction-Based Type-Checking Type Checker SEC Machine Refocusing (§ 3.4.1) Control Stack Introduction (§ 4.4.5) + + Contraction inlining (§ 3.4.2) Environment Extraction (§ 4.4.4) Big-Step CEK machine Reduction-Free Type Checker with Result Stack ( 1 ) Lightweight Fusion (§ 3.4.3) Defunctionalization (§ 4.4.3) + + Transition Compression (§ 3.4.4) CPS Transformation (§ 4.4.2) Switching domains (§ 3.4.6) Big-Step Recursive Stack-Threading CEK machine Descent Evaluator Refunctionalization (§ 3.4.7) Data Stack Introduction + (§ 4.4.1) Direct-Style Transformation (§ 3.4.8)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend