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

lambda calculus
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Lambda calculus

(Advanced Functional Programming) Jeremy Yallop

Computer Laboratory University of Cambridge

January 2015

1/ 29

slide-2
SLIDE 2

Course outline

2/ 29

slide-3
SLIDE 3

Books

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

3/ 29

slide-4
SLIDE 4

Tooling

OPAM OCaml package manager IOCaml Linux / OSX / VirtualBox

Fω interpreter

4/ 29

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

  • n the mailing list.

Assessed exercises: Mon 2 Feb ↓ Mon 9 Feb Mon 16 Feb ↓ Mon 2 March Mon 9 March ↓ Fri 24 April

7/ 29

slide-9
SLIDE 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

slide-10
SLIDE 10

Motivation & background

9/ 29

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 13

Roadmap

Fω F

  • λ→
  • 12/ 29
slide-14
SLIDE 14

Inference rules

premise 1 premise 1 . . . premise N rule name conclusion

13/ 29

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 17

Typing rules

Γ ⊢ M : A → B Γ ⊢ N : A →-elim Γ ⊢ M N : B

14/ 29

slide-18
SLIDE 18

Terms, types, kinds

Kinds: K, K1, K2, . . . K is a kind Types: A, B, C, . . . Γ ⊢ A :: K Environments: Γ Γ is an environment Terms: L, M, N, . . . Γ ⊢ M : A

15/ 29

slide-19
SLIDE 19

λ→

(simply typed lambda calculus)

16/ 29

slide-20
SLIDE 20

λ→ by example

In λ→: λx :A. x λf :B→C. λg :A→B. λx :A. f ( g x ) In OCaml: fun x −> x fun f g x −> f ( g x )

17/ 29

slide-21
SLIDE 21

Kinds in λ→

∗-kind ∗ is a kind

18/ 29

slide-22
SLIDE 22

Kinding rules (type formation) in λ→

kind-B Γ ⊢ B :: ∗ Γ ⊢ A :: ∗ Γ ⊢ B :: ∗ kind-→ Γ ⊢ A → B :: ∗

19/ 29

slide-23
SLIDE 23

A kinding derivation

kind-B Γ ⊢ B :: ∗ kind-B Γ ⊢ B :: ∗ kind-→ Γ ⊢ B → B :: ∗ kind-B Γ ⊢ B :: ∗ kind-→ Γ ⊢ (B → B) → B :: ∗

20/ 29

slide-24
SLIDE 24

Environment formation rules

Γ-· · is an environment Γ is an environment Γ ⊢ A :: ∗ Γ-: Γ, x : A is an environment

21/ 29

slide-25
SLIDE 25

Typing rules (term formation) in λ→

x : A ∈ Γ tvar Γ ⊢ x : A Γ, x : A ⊢ M : B →-intro Γ ⊢ λx : A.M : A → B Γ ⊢ M : A → B Γ ⊢ N : A →-elim Γ ⊢ M N : B

22/ 29

slide-26
SLIDE 26

A typing derivation for the identity function

·, x : A ⊢ x : A →-intro · ⊢ λx : A.x : A → A

23/ 29

slide-27
SLIDE 27

Products by example

In λ→ with products: λp : (A→B)×A. f s t p ( snd p) λx :A. x , x λf :A→C. λg .B→C. λp .A×B. f f s t p , g snd p λp .A×B. snd p , f s t p In OCaml: fun ( f , p) −> f p fun x −> ( x , x ) fun f g ( x , y ) −> ( f x , g y ) fun ( x , y ) −> ( y , x )

24/ 29

slide-28
SLIDE 28

Kinding and typing rules for products

Γ ⊢ A :: ∗ Γ ⊢ B :: ∗ kind-× Γ ⊢ A × B :: ∗ Γ ⊢ M : A Γ ⊢ N : B ×-intro Γ ⊢ M, N : A × B Γ ⊢ M : A × B ×-elim-1 Γ ⊢ fst M : A Γ ⊢ M : A × B ×-elim-2 Γ ⊢ snd M : B

25/ 29

slide-29
SLIDE 29

Sums by example

In λ→ with sums: λf :A→C. λg :B→C. λs :A+B. case s

  • f

x . f x | y . g y λs :A+B. case s

  • f

x . i n r [B] x | y . i n r [A] y In OCaml: fun f g s −> match s with I n l x −> f x | I n r y −> g y function I n l x −> I n r x | I n r y −> I n l y

26/ 29

slide-30
SLIDE 30

Kinding and typing rules for sums

Γ ⊢ A :: ∗ Γ ⊢ B :: ∗ kind-+ Γ ⊢ A + B :: ∗ Γ ⊢ M : A +-intro-1 Γ ⊢ inl [B] M : A + B Γ ⊢ N : B +-intro-2 Γ ⊢ inr [A] N : A + B Γ ⊢ L : A + B Γ, x : A ⊢ M : C Γ, y : B ⊢ N : C +-elim Γ ⊢ case L of x.M | y.N : C

27/ 29

slide-31
SLIDE 31

System F

(polymorphic lambda calculus)

28/ 29

slide-32
SLIDE 32

System F by example

Λα : : ∗ . λx : α . x Λα : : ∗ . Λβ : : ∗ . Λγ : : ∗ . λf : β → γ . λg : α → β . λx : α . f ( g x ) Λα : : ∗ . Λβ : : ∗ . λp : ( α → β )×α . f s t p ( snd p)

29/ 29

slide-33
SLIDE 33

New kinding rules for System F

Γ, α::K ⊢ A :: ∗ kind-∀ Γ ⊢ ∀α::K.A :: ∗ α::K ∈ Γ tyvar Γ ⊢ α :: K

30/ 29

slide-34
SLIDE 34

New environment rule for System F

Γ is an environment K is a kind Γ-:: Γ, α::K is an environment

31/ 29

slide-35
SLIDE 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

slide-36
SLIDE 36

Existential types

33/ 29

slide-37
SLIDE 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

slide-38
SLIDE 38

Existential intuition

Existentials correspond to abstract types

35/ 29

slide-39
SLIDE 39

Kinding rules for existentials

Γ, α::K ⊢ A :: ∗ kind-∃ Γ ⊢ ∃α::K.A :: ∗

36/ 29

slide-40
SLIDE 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