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 Minsky, Languages John Whitington Anil Madhavapeddy & Benjamin C. Pierce Jason Hickey Coherent Press (2013) MIT Press (2002) O’Reilly Media (2013) 3/ 29
Tooling OPAM Linux / OSX / VirtualBox OCaml package manager F ω F ω interpreter IOCaml 4/ 29
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
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
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
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
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
Motivation & background 9/ 29
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
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
� � Roadmap F ω � � � � � � � � F λ → 12/ 29
Inference rules premise 1 premise 1 . . . premise N rule name conclusion 13/ 29
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
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
Typing rules Γ ⊢ M : A → B Γ ⊢ N : A → -elim Γ ⊢ M N : B 14/ 29
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
λ → (simply typed lambda calculus) 16/ 29
λ → 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
Kinds in λ → ∗ -kind ∗ is a kind 18/ 29
Kinding rules (type formation) in λ → kind- B Γ ⊢ B :: ∗ Γ ⊢ A :: ∗ Γ ⊢ B :: ∗ kind- → Γ ⊢ A → B :: ∗ 19/ 29
A kinding derivation kind- B kind- B Γ ⊢ B :: ∗ Γ ⊢ B :: ∗ kind- → kind- B Γ ⊢ B → B :: ∗ Γ ⊢ B :: ∗ kind- → Γ ⊢ ( B → B ) → B :: ∗ 20/ 29
Environment formation rules Γ- · · is an environment Γ is an environment Γ ⊢ A :: ∗ Γ-: Γ , x : A is an environment 21/ 29
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
A typing derivation for the identity function · , x : A ⊢ x : A → -intro · ⊢ λ x : A . x : A → A 23/ 29
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
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
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
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
System F (polymorphic lambda calculus) 28/ 29
System F by example Λ α : : ∗ . λ x : α . x Λ α : : ∗ . Λ β : : ∗ . Λ γ : : ∗ . λ f : β → γ . λ g : α → β . λ x : α . f ( g x ) Λ α : : ∗ . Λ β : : ∗ . λ p : ( α → β ) × α . f s t p ( snd p) 29/ 29
New kinding rules for System F Γ , α :: K ⊢ A :: ∗ α :: K ∈ Γ tyvar kind- ∀ Γ ⊢ α :: K Γ ⊢ ∀ α :: K . A :: ∗ 30/ 29
New environment rule for System F Γ is an environment K is a kind Γ-:: Γ , α :: K is an environment 31/ 29
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
Existential types 33/ 29
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
Existential intuition Existentials correspond to abstract types 35/ 29
Kinding rules for existentials Γ , α :: K ⊢ A :: ∗ kind- ∃ Γ ⊢ ∃ α :: K . A :: ∗ 36/ 29
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
Recommend
More recommend