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 2017 1/ 64 Course outline 2/ 64 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 2017 1/ 64

  2. Course outline 2/ 64

  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/ 64

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

  5. Philosophy and approach ▶ practical : with theory as necessary for understanding ▶ real-world : patterns and techniques from real applications ▶ reusable : general, broadly applicable techniques ▶ current : topics of ongoing research 5/ 64

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

  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 me directly and I’ll anonymise and post your question: jeremy.yallop@cl.cam.ac.uk 6/ 64

  8. Exercises assessed and unassessed Unassessed exercises Useful preparation for assessed exercises; optional but recommended. Hand in for feedback, discuss freely on mailing list. Assessed exercises Mon 30 Jan Mon 20 Feb Mon 13 Mar ↓ ↓ ↓ Mon 13 Feb Mon 6 Mar Tue 25 Apr ( ∼ 30%) ( ∼ 35%) ( ∼ 35%) 7/ 64

  9. Course structure ▶ Technical background Lambda calculus; type inference ▶ Themes Propositions as types; parametricity and abstraction ▶ (Fancy) types Higher-rank and higher-kinded polymorphism; modules and functors; generalised algebraic types; structured overloading ▶ Patterns and techniques Monads, applicatives, arrows, etc.; datatype-generic programming; staged programming ▶ Applications (E.g.) a foreign function library 8/ 64

  10. Motivation & background 9/ 64

  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/ 64

  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 background for parametricity properties ▶ the language underlying higher-order polymorphism in OCaml ▶ the elaboration language for modules ▶ the core calculus for GADTs 11/ 64

  13. � � Roadmap F ω ⑧ ⑧ ⑧ ⑧ ⑧ ⑧ ⑧ ⑧ F λ → 12/ 64

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

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

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

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

  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/ 64

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

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

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

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

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

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

  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/ 64

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

  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. λ g : B → C. fun f g (x,y) -> (f x, g y) λ 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/ 64

  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/ 64

  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. Inl x -> f x case s of | Inr y -> g y x.f x | y.g y λ s : A + B. case s of function x. inr [B] x Inl x -> Inr x | y. inl [A] y | Inr y -> Inl y 26/ 64

  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/ 64

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

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

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

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

  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/ 64

Recommend


More recommend