lambda calculus
play

Lambda calculus (Advanced Functional Programming) Jeremy Yallop - PowerPoint PPT Presentation

Lambda calculus (Advanced Functional Programming) Jeremy Yallop Computer Laboratory University of Cambridge January 2015 1/ 29 Course outline 2/ 29 Books OCaml from the very Real World OCaml Types and Programming beginning Yaron


  1. Lambda calculus (Advanced Functional Programming) Jeremy Yallop Computer Laboratory University of Cambridge January 2015 1/ 29

  2. Course outline 2/ 29

  3. Books OCaml from the very Real World OCaml Types and Programming beginning Yaron Minsky, Languages John Whitington Anil Madhavapeddy & Benjamin C. Pierce Jason Hickey Coherent Press (2013) MIT Press (2002) O’Reilly Media (2013) 3/ 29

  4. Tooling OPAM Linux / OSX / VirtualBox OCaml package manager F ω F ω interpreter IOCaml 4/ 29

  5. Philosophy and approach ◮ practical : with theory as necessary for understanding ◮ real-world : patterns and techniques from real applications ◮ reusable : general, widely applicable techniques ◮ current : mostly the topics of ongoing research 5/ 29

  6. Philosophy and approach ◮ practical : with theory as necessary for understanding ◮ real-world : patterns and techniques from real applications ◮ reusable : general, widely applicable techniques ◮ current : mostly the topics of ongoing research ◮ opinionated (but you don’t have to agree) 5/ 29

  7. Mailing list cl-acs-28@lists.cam.ac.uk Announcements, questions and discussion. Feel free to post! Have a question but feeling shy? Mail a lecturer instead and we’ll anonymise and post your question: jeremy.yallop@cl.cam.ac.uk leo.white@cl.cam.ac.uk 6/ 29

  8. Exercises assessed and unassessed Unassessed exercises : Useful preparation for the assessed exercises, so we recommend that you work through them. Hand in for feedback, discuss freely on the mailing list. Assessed exercises : Mon 2 Feb Mon 16 Feb Mon 9 March ↓ ↓ ↓ Mon 9 Feb Mon 2 March Fri 24 April 7/ 29

  9. Course structure ◮ Technical background Lambda calculus; type inference ◮ Themes Propositions as types; duality; parametricity and abstraction ◮ (Fancy) types Higher-rank and higher-kinded polymorphism; modules and functors; generalised algebraic types; rows ◮ Applications Monads and related concepts; domain-specific languages; datatype-generic programming; staged programming 8/ 29

  10. Motivation & background 9/ 29

  11. System F ω Function composition in OCaml: fun f g x − > f ( g x ) Function composition in System F ω : Λ α : : ∗ . Λ β : : ∗ . Λ γ : : ∗ . λ f : α → β . λ g : γ → α . λ x : : γ . f ( g x ) 10/ 29

  12. What’s the point of System F ω ? A framework for understanding language features and programming patterns: ◮ the elaboration language for type inference ◮ the proof system for reasoning with propositional logic ◮ the setting for dualities ◮ the background for parametricity properties ◮ the language underlying higher-order polymorphism in OCaml ◮ the elaboration language for modules ◮ the core calculus for GADTs 11/ 29

  13. � � Roadmap F ω � � � � � � � � F λ → 12/ 29

  14. Inference rules premise 1 premise 1 . . . premise N rule name conclusion 13/ 29

  15. Inference rules premise 1 premise 1 . . . premise N rule name conclusion all M are P all S are M modus barbara all S are P 13/ 29

  16. Inference rules premise 1 premise 1 . . . premise N rule name conclusion all M are P all S are M modus barbara all S are P all programs are buggy all functional programs are programs modus barbara all functional programs are buggy 13/ 29

  17. Typing rules Γ ⊢ M : A → B Γ ⊢ N : A → -elim Γ ⊢ M N : B 14/ 29

  18. Terms, types, kinds Kinds : K, K 1 , K 2 , . . . Environments : Γ K is a kind Γ is an environment Types : A, B, C, . . . Terms : L, M, N, . . . Γ ⊢ A :: K Γ ⊢ M : A 15/ 29

  19. λ → (simply typed lambda calculus) 16/ 29

  20. λ → by example In λ → : In OCaml : λ x :A. x fun x − > x λ f :B → C. fun f g x − > f ( g x ) λ g :A → B. λ x :A. f ( g x ) 17/ 29

  21. Kinds in λ → ∗ -kind ∗ is a kind 18/ 29

  22. Kinding rules (type formation) in λ → kind- B Γ ⊢ B :: ∗ Γ ⊢ A :: ∗ Γ ⊢ B :: ∗ kind- → Γ ⊢ A → B :: ∗ 19/ 29

  23. A kinding derivation kind- B kind- B Γ ⊢ B :: ∗ Γ ⊢ B :: ∗ kind- → kind- B Γ ⊢ B → B :: ∗ Γ ⊢ B :: ∗ kind- → Γ ⊢ ( B → B ) → B :: ∗ 20/ 29

  24. Environment formation rules Γ- · · is an environment Γ is an environment Γ ⊢ A :: ∗ Γ-: Γ , x : A is an environment 21/ 29

  25. Typing rules (term formation) in λ → x : A ∈ Γ tvar Γ ⊢ x : A Γ ⊢ M : A → B Γ , x : A ⊢ M : B Γ ⊢ N : A → -elim → -intro Γ ⊢ M N : B Γ ⊢ λ x : A . M : A → B 22/ 29

  26. A typing derivation for the identity function · , x : A ⊢ x : A → -intro · ⊢ λ x : A . x : A → A 23/ 29

  27. Products by example In λ → with products : In OCaml : λ p : (A → B) × A. fun ( f , p) − > f p f s t p ( snd p) λ x :A. � x , x � fun x − > ( x , x ) λ f :A → C. fun f g ( x , y ) − > ( f x , g y ) λ g .B → C. λ p .A × B. � f f s t p , g snd p � λ p .A × B. � snd p , f s t p � fun ( x , y ) − > ( y , x ) 24/ 29

  28. Kinding and typing rules for products Γ ⊢ A :: ∗ Γ ⊢ B :: ∗ kind- × Γ ⊢ A × B :: ∗ Γ ⊢ M : A Γ ⊢ M : A × B × -elim-1 Γ ⊢ N : B Γ ⊢ fst M : A × -intro Γ ⊢ � M , N � : A × B Γ ⊢ M : A × B × -elim-2 Γ ⊢ snd M : B 25/ 29

  29. Sums by example In λ → with sums : In OCaml : λ f :A → C. fun f g s − > λ g :B → C. match s with λ s :A+B. I n l x − > f x case s of | I n r y − > g y x . f x | y . g y λ s :A+B. function case s of I n l x − > I n r x x . i n r [B] x | I n r y − > I n l y | y . i n r [A] y 26/ 29

  30. Kinding and typing rules for sums Γ ⊢ A :: ∗ Γ ⊢ B :: ∗ kind-+ Γ ⊢ A + B :: ∗ Γ ⊢ M : A Γ ⊢ L : A + B +-intro-1 Γ ⊢ inl [ B ] M : A + B Γ , x : A ⊢ M : C Γ , y : B ⊢ N : C +-elim Γ ⊢ N : B +-intro-2 Γ ⊢ case L of x . M | y . N : C Γ ⊢ inr [ A ] N : A + B 27/ 29

  31. System F (polymorphic lambda calculus) 28/ 29

  32. System F by example Λ α : : ∗ . λ x : α . x Λ α : : ∗ . Λ β : : ∗ . Λ γ : : ∗ . λ f : β → γ . λ g : α → β . λ x : α . f ( g x ) Λ α : : ∗ . Λ β : : ∗ . λ p : ( α → β ) × α . f s t p ( snd p) 29/ 29

  33. New kinding rules for System F Γ , α :: K ⊢ A :: ∗ α :: K ∈ Γ tyvar kind- ∀ Γ ⊢ α :: K Γ ⊢ ∀ α :: K . A :: ∗ 30/ 29

  34. New environment rule for System F Γ is an environment K is a kind Γ-:: Γ , α :: K is an environment 31/ 29

  35. New typing rules for System F Γ , α :: K ⊢ M : A ∀ -intro Γ ⊢ Λ α :: K . M : ∀ α :: K . A Γ ⊢ M : ∀ α :: K . A Γ ⊢ B :: K ∀ -elim Γ ⊢ M [ B ] : A [ α := B ] 32/ 29

  36. Existential types 33/ 29

  37. What’s the point of existentials? ◮ ∀ and ∃ in logic are closely connected to polymorphism and existentials in type theory ◮ As in logic, ∀ and ∃ for types are closely related ◮ Module types can be viewed as a kind of existential type ◮ OCaml’s variant types now support existential variables 34/ 29

  38. Existential intuition Existentials correspond to abstract types 35/ 29

  39. Kinding rules for existentials Γ , α :: K ⊢ A :: ∗ kind- ∃ Γ ⊢ ∃ α :: K . A :: ∗ 36/ 29

  40. Typing rules for existentials Γ ⊢ M : A [ α := B ] Γ ⊢ ∃ α :: K . A :: ∗ ∃ -intro Γ ⊢ pack B , M as ∃ α :: K . A : ∃ α :: K . A Γ ⊢ M : ∃ α :: K . A Γ , α :: K , x : A ⊢ M ′ : B ∃ -elim Γ ⊢ open M as α, x in M ′ : B 37/ 29

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