a semantics for lazy types
play

A Semantics for Lazy Types Bachelor Thesis Georg Neis Advisor: - PowerPoint PPT Presentation

A Semantics for Lazy Types Bachelor Thesis Georg Neis Advisor: Andreas Rossberg Programming Systems Lab Saarland University June 8, 2006 Motivation Open and distributed programming: dynamic loading and linking Type checking at


  1. A Semantics for Lazy Types Bachelor Thesis Georg Neis Advisor: Andreas Rossberg Programming Systems Lab Saarland University June 8, 2006

  2. Motivation ◮ Open and distributed programming: dynamic loading and linking ◮ Type checking at runtime ◮ Laziness

  3. Motivation ◮ Open and distributed programming: dynamic loading and linking ◮ Type checking at runtime ◮ Laziness ◮ Dynamic type checking: comparison of type expressions ◮ Reduction to normal-form ◮ Due to laziness: may interleave with term-level reduction!

  4. Motivation ◮ Open and distributed programming: dynamic loading and linking ◮ Type checking at runtime ◮ Laziness ◮ Dynamic type checking: comparison of type expressions ◮ Reduction to normal-form ◮ Due to laziness: may interleave with term-level reduction! ◮ Task: develop calculus (based on F ω ) that models this

  5. An example from Alice ML (* A *) type t = int val x : t = 5 (* B *) import type t; val x : t from "A" type u = t val y : u*u = (x,x) (* C *) import type u = int; val y : u*u from "B" type v = int val z = #1 y

  6. Lazy evaluation in Alice ML ◮ Provided by lazy futures ◮ Example: (fn f ⇒ f 1) (lazy (fn x ⇒ x) (fn n ⇒ n+41))

  7. Modeling lazy evaluation in the untyped lambda calculus ◮ e ::= x | λ x . e | e 1 e 2 | let x = e 1 in e 2 | lazy x = e 1 in e 2

  8. Modeling lazy evaluation in the untyped lambda calculus ◮ e ::= x | λ x . e | e 1 e 2 | let x = e 1 in e 2 | lazy x = e 1 in e 2 ◮ Translation of the previous example: (fn f ⇒ f 1) (lazy (fn x ⇒ x) (fn n ⇒ n+41)) � ( λ f . f 1) (lazy y = ( λ x . x ) ( λ n . n + 41) in y )

  9. Modeling lazy evaluation in the untyped lambda calculus Reduction: ◮ ( λ f . f 1) (lazy y = ( λ x . x ) ( λ n . n + 41) in y ) − →

  10. Modeling lazy evaluation in the untyped lambda calculus Reduction: ◮ ( λ f . f 1) (lazy y = ( λ x . x ) ( λ n . n + 41) in y ) − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in ( λ f . f 1) y − →

  11. Modeling lazy evaluation in the untyped lambda calculus Reduction: ◮ ( λ f . f 1) (lazy y = ( λ x . x ) ( λ n . n + 41) in y ) − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in ( λ f . f 1) y − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in y 1 − →

  12. Modeling lazy evaluation in the untyped lambda calculus Reduction: ◮ ( λ f . f 1) (lazy y = ( λ x . x ) ( λ n . n + 41) in y ) − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in ( λ f . f 1) y − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in y 1 − → ◮ let y = ( λ x . x ) ( λ n . n + 41) in y 1 − →

  13. Modeling lazy evaluation in the untyped lambda calculus Reduction: ◮ ( λ f . f 1) (lazy y = ( λ x . x ) ( λ n . n + 41) in y ) − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in ( λ f . f 1) y − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in y 1 − → ◮ let y = ( λ x . x ) ( λ n . n + 41) in y 1 − → ◮ let y = ( λ n . n + 41) in y 1 − →

  14. Modeling lazy evaluation in the untyped lambda calculus Reduction: ◮ ( λ f . f 1) (lazy y = ( λ x . x ) ( λ n . n + 41) in y ) − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in ( λ f . f 1) y − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in y 1 − → ◮ let y = ( λ x . x ) ( λ n . n + 41) in y 1 − → ◮ let y = ( λ n . n + 41) in y 1 − → ◮ ( λ n . n + 41) 1 − →

  15. Modeling lazy evaluation in the untyped lambda calculus Reduction: ◮ ( λ f . f 1) (lazy y = ( λ x . x ) ( λ n . n + 41) in y ) − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in ( λ f . f 1) y − → ◮ lazy y = ( λ x . x ) ( λ n . n + 41) in y 1 − → ◮ let y = ( λ x . x ) ( λ n . n + 41) in y 1 − → ◮ let y = ( λ n . n + 41) in y 1 − → ◮ ( λ n . n + 41) 1 − → ◮ 1 + 41 − → 42

  16. Modeling lazy evaluation in the untyped lambda calculus Contexts: ◮ L ::= | lazy x = e in L ◮ E ::= | E e | v E | . . .

  17. Modeling lazy evaluation in the untyped lambda calculus Contexts: ◮ L ::= | lazy x = e in L ◮ E ::= | E e | v E | . . . ◮ Suspend : LE [ lazy x = e 1 in e 2 ] − → L [ lazy x = e 1 in E [ e 2 ]]

  18. Modeling lazy evaluation in the untyped lambda calculus Contexts: ◮ L ::= | lazy x = e in L ◮ E ::= | E e | v E | . . . ◮ Suspend : LE [ lazy x = e 1 in e 2 ] − → L [ lazy x = e 1 in E [ e 2 ]] ◮ Trigger1 : L 1 [ lazy x = e 1 in L 2 E [ x v ]] − → L 1 [ let x = e 1 in L 2 E [ x v ]]

  19. Existential types ◮ To model a simple form of modules ◮ Example: structure S = struct type t = bool val n = 42 end : sig type t val n : int end � � bool , 42 � : ∃ α. int

  20. Existential types ◮ Accessing a module: let � α, x � = e 1 in e 2

  21. Existential types ◮ Accessing a module: let � α, x � = e 1 in e 2 ◮ Reduction: LE [let � α, x � = � τ, v � : τ ′ in e ] − → LE [ e [ α := τ ][ x := v ]]

  22. Existential types ◮ Accessing a module: let � α, x � = e 1 in e 2 ◮ Reduction: LE [let � α, x � = � τ, v � : τ ′ in e ] − → LE [ e [ α := τ ][ x := v ]] ◮ Lazy variant for loading a module: lazy � α, x � = e 1 in e 2

  23. Typecase ◮ Comparison of two types τ 1 and τ 2 ◮ tcase e : τ 1 of x : τ 2 then e 1 else e 2

  24. Typecase ◮ Comparison of two types τ 1 and τ 2 ◮ tcase e : τ 1 of x : τ 2 then e 1 else e 2 ◮ Reduction: ◮ LE [tcase v : ν of x : ν then e 1 else e 2 ] − → LE [ e 1 [ x := e ]] ◮ LE [tcase v : ν of x : ν ′ then e 1 else e 2 ] − → LE [ e 2 ] ( ν � = ν ′ )

  25. Lazy types ◮ Consider: lazy � α, x � = e in tcase x : α of y :int then y else 0 ◮ α ? = int

  26. Lazy types ◮ Consider: lazy � α, x � = e in tcase x : α of y :int then y else 0 ◮ α ? = int ◮ lazy � α, x � = e in tcase x : α of y :int then y else 0 − → let � α, x � = e in tcase x : α of y :int then y else 0

  27. Lazy types ◮ Consider: lazy � α, x � = e in tcase x : α of y :int then y else 0 ◮ α ? = int ◮ lazy � α, x � = e in tcase x : α of y :int then y else 0 − → let � α, x � = e in tcase x : α of y :int then y else 0 ◮ Trigger2 : L 1 [lazy � α, x � = e in L 2 ET [ α ]] − → L 1 [let � α, x � = e in L 2 ET [ α ]]

  28. An example from Alice ML (* A *) type t = int val x : t = 5 (* B *) import type t; val x : t from "A" type u = t val y : u*u = (x,x) (* C *) import type u = int; val y : u*u from "B" type v = int val z = #1 y

  29. Translation into the calculus (* A *) type t = int val x : t = 5 � = λ :1 . � int , 5 � : ∃ α.α a

  30. Translation into the calculus (* B *) import type t; val x : t from "A" type u = t val y : u*u = (x,x) � = λ :1 . lazy � t , x � = a () b in � t , � x , x �� : ∃ α.α × α

  31. Translation into the calculus (* C *) import type u = int; val y : u*u from "B" type v = int val z = #1 y � = λ :1 . lazy � , y � = c let � u , y � = b () in tcase y : u × u of y ′ : int × int then � int , y ′ � : ∃ α. int × int else ⊥ in � int , fst y � : ∃ α. int

  32. Design Space ◮ Three ways of type-level reduction – different degrees of laziness ◮ Consider: int × (( λβ.ν ) α ) ? = bool × int (where α lazy)

  33. Design Space ◮ Three ways of type-level reduction – different degrees of laziness ◮ Consider: int × (( λβ.ν ) α ) ? = bool × int (where α lazy) 1. Naive: use the trigger rule

  34. Design Space ◮ Three ways of type-level reduction – different degrees of laziness ◮ Consider: int × (( λβ.ν ) α ) ? = bool × int (where α lazy) 1. Naive: use the trigger rule 2. Clever: perform the application

  35. Design Space ◮ Three ways of type-level reduction – different degrees of laziness ◮ Consider: int × (( λβ.ν ) α ) ? = bool × int (where α lazy) 1. Naive: use the trigger rule 2. Clever: perform the application 3. Smart: weak-head reduction

  36. References ◮ Mart´ ın Abadi, Luca Cardelli, Benjamin Pierce, and Didier R´ emy. Dynamic typing in polymorphic languages. Journal of Functional Programming, January 1995. ◮ John Maraist, Martin Odersky, and Philip Wadler. A call-by-need lambda calculus. Journal of Functional Programming, May 1998. ◮ Benjamin Pierce. Types and Programming Languages. The MIT Press, February 2002. ◮ Zena Ariola and Matthias Felleisen. The call-by-need lambda calculus. Journal of Functional Programming, 1997. ◮ Matthias Berg. Polymorphic lambda calculus with dynamic types, FoPra Thesis, October 2004. http://www.ps.uni-sb.de/ ∼ berg/fopra.html ◮ Andreas Rossberg, Didier Le Botlan, Guido Tack, Thorsten Brunklaus, and Gert Smolka. Alice through the looking glass. Trends in Functional Programming, volume 5, Intellect, 2005

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