Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS...

66
The Functional Paradigm MinHS Functional Programming Languages Liam O’Connor CSE, UNSW (and data61) Term3 2019 1

Transcript of Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS...

Page 1: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Functional Programming Languages

Liam O’ConnorCSE, UNSW (and data61)

Term3 2019

1

Page 2: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Functional ProgrammingMany languages have been called functional over the years:

Lisp(define (max-of lst)

(cond[(= (length lst) 1) (first lst)][else (max (first lst) (max-of (rest lst)))]))

2

Page 3: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Functional ProgrammingMany languages have been called functional over the years:

Lisp(define (max-of lst)

(cond[(= (length lst) 1) (first lst)][else (max (first lst) (max-of (rest lst)))]))

HaskellmaxOf :: [Int] → IntmaxOf = foldr1 max

3

Page 4: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Functional ProgrammingMany languages have been called functional over the years:

Lisp(define (max-of lst)

(cond[(= (length lst) 1) (first lst)][else (max (first lst) (max-of (rest lst)))]))

HaskellmaxOf :: [Int] → IntmaxOf = foldr1 max

JavaScript?function maxOf(arr) {

var max = arr .reduce(function(a, b) {return Math.max(a, b);

});}

4

Page 5: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Functional ProgrammingMany languages have been called functional over the years:

Lisp(define (max-of lst)

(cond[(= (length lst) 1) (first lst)][else (max (first lst) (max-of (rest lst)))]))

HaskellmaxOf :: [Int] → IntmaxOf = foldr1 max

JavaScript?function maxOf(arr) {

var max = arr .reduce(function(a, b) {return Math.max(a, b);

});}

What do theyhave in common?

5

Page 6: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Definitions

Unlike imperative languages, functional programming languagesare not very crisply defined.

Attempt at a Definition

A functional programming language is a programming languagederived from or inspired by the λ-calculus, or derived from orinspired by another functional programming language.

The result? If it has λ in it, you can call it functional.

In this course, we’ll consider purely functional languages, whichhave a much better definition.

6

Page 7: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Definitions

Unlike imperative languages, functional programming languagesare not very crisply defined.

Attempt at a Definition

A functional programming language is a programming languagederived from or inspired by the λ-calculus, or derived from orinspired by another functional programming language.

The result? If it has λ in it, you can call it functional.

In this course, we’ll consider purely functional languages, whichhave a much better definition.

7

Page 8: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

8

Page 9: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

9

Page 10: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?

Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

10

Page 11: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

11

Page 12: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

12

Page 13: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

13

Page 14: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

14

Page 15: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

15

Page 16: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

16

Page 17: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

17

Page 18: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

18

Page 19: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

19

Page 20: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?

Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

20

Page 21: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

21

Page 22: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

22

Page 23: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Why Study FP Languages?

Think of a major innovation in the area of programming languages.

Garbage Collection?

Lisp, 1958

Functions as Values?Lisp, 1958

Polymorphism?

ML, 1973

Type Inference?

ML, 1973

Metaprogramming?

Lisp, 1958

Lazy Evaluation?

Miranda, 1985

Monads?Haskell, 1991

Software Transactional Memory?

GHC Haskell, 2005

23

Page 24: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Purely Functional Programming LanguagesThe term purely functional has a very crisp definition.

Definition

A programming language is purely functional if β-reduction (orevaluation in general) is actually a confluence.In other words, functions have to be mathematical functions, andfree of side effects.

Consider what would happen if we allowed effects in a functionallanguage:

count = 0;f x = {count := count + x ; return count};m = (λy . y + y) (f 3)

If we evaluate f 3 first, we will get m = 6, but if we β-reduce mfirst, we will get m = 9. ⇒ not confluent.

24

Page 25: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Purely Functional Programming LanguagesThe term purely functional has a very crisp definition.

Definition

A programming language is purely functional if β-reduction (orevaluation in general) is actually a confluence.In other words, functions have to be mathematical functions, andfree of side effects.

Consider what would happen if we allowed effects in a functionallanguage:

count = 0;f x = {count := count + x ; return count};m = (λy . y + y) (f 3)

If we evaluate f 3 first, we will get m = 6, but if we β-reduce mfirst, we will get m = 9. ⇒ not confluent.

25

Page 26: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Making a Functional Language

We’re going to make a language called MinHS.

1 Three types of values: integers, booleans, and functions.

2 Static type checking (not inference)

3 Purely functional (no effects)

4 Call-by-value (strict evaluation)

In your Assignment 1, you will be implementing an evaluator for aslightly less minimal dialect of MinHS.

26

Page 27: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Making a Functional Language

We’re going to make a language called MinHS.

1 Three types of values: integers, booleans, and functions.

2 Static type checking (not inference)

3 Purely functional (no effects)

4 Call-by-value (strict evaluation)

In your Assignment 1, you will be implementing an evaluator for aslightly less minimal dialect of MinHS.

27

Page 28: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Making a Functional Language

We’re going to make a language called MinHS.

1 Three types of values: integers, booleans, and functions.

2 Static type checking (not inference)

3 Purely functional (no effects)

4 Call-by-value (strict evaluation)

In your Assignment 1, you will be implementing an evaluator for aslightly less minimal dialect of MinHS.

28

Page 29: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Making a Functional Language

We’re going to make a language called MinHS.

1 Three types of values: integers, booleans, and functions.

2 Static type checking (not inference)

3 Purely functional (no effects)

4 Call-by-value (strict evaluation)

In your Assignment 1, you will be implementing an evaluator for aslightly less minimal dialect of MinHS.

29

Page 30: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Making a Functional Language

We’re going to make a language called MinHS.

1 Three types of values: integers, booleans, and functions.

2 Static type checking (not inference)

3 Purely functional (no effects)

4 Call-by-value (strict evaluation)

In your Assignment 1, you will be implementing an evaluator for aslightly less minimal dialect of MinHS.

30

Page 31: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Syntax

Integers n ::= · · ·Identifiers f , x ::= · · ·Literals b ::= True | FalseTypes τ ::= Bool | Int | τ1 → τ2Infix Operators ~ ::= * | + | == | · · ·Expressions e ::= x | n | b | (e) | e1 ~ e2

| if e1 then e2 else e3

| e1 e2| recfun f :: (τ1 → τ2) x = e↑ Like λ, but with recursion.

As usual, this is ambiguous concrete syntax. But all theprecedence and associativity rule apply as in Haskell. We assume asuitable parser.

31

Page 32: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Syntax

Integers n ::= · · ·Identifiers f , x ::= · · ·Literals b ::= True | FalseTypes τ ::= Bool | Int | τ1 → τ2Infix Operators ~ ::= * | + | == | · · ·Expressions e ::= x | n | b | (e) | e1 ~ e2

| if e1 then e2 else e3| e1 e2

| recfun f :: (τ1 → τ2) x = e↑ Like λ, but with recursion.

As usual, this is ambiguous concrete syntax. But all theprecedence and associativity rule apply as in Haskell. We assume asuitable parser.

32

Page 33: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Syntax

Integers n ::= · · ·Identifiers f , x ::= · · ·Literals b ::= True | FalseTypes τ ::= Bool | Int | τ1 → τ2Infix Operators ~ ::= * | + | == | · · ·Expressions e ::= x | n | b | (e) | e1 ~ e2

| if e1 then e2 else e3| e1 e2| recfun f :: (τ1 → τ2) x = e↑ Like λ, but with recursion.

As usual, this is ambiguous concrete syntax. But all theprecedence and associativity rule apply as in Haskell. We assume asuitable parser.

33

Page 34: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Examples

Example (Stupid division by 5)

recfun divBy5 :: (Int→ Int) x =if x < 5

then 0else 1 + divBy5 (x − 5)

Example (Average Function)

recfun average :: (Int→ (Int→ Int)) x =recfun avX :: (Int→ Int) y =

(x + y) / 2

As in Haskell, (average 15 5) = ((average 15) 5).

34

Page 35: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

We don’t need no let

This language is so minimal, it doesn’t even need let expressions.How can we do without them?

let x :: τ1 = e1 in e2 :: τ2 ≡ (recfun f :: (τ1 → τ2) x = e2) e1

35

Page 36: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

We don’t need no let

This language is so minimal, it doesn’t even need let expressions.How can we do without them?

let x :: τ1 = e1 in e2 :: τ2 ≡ (recfun f :: (τ1 → τ2) x = e2) e1

36

Page 37: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Abstract Syntax

Moving to first order abstract syntax, we get:

1 Things like numbers and boolean literals are wrapped in terms(Num, Lit)

2 Operators like a + b become (Plus a b).

3 if c then t else e becomes (If c t e).

4 Function applications e1 e2 become explicit (Apply e1 e2).

5 recfun f :: (τ1 → τ2) x = e becomes (Recfun τ1 τ2 f x e).

6 Variable usages are wrapped in a term (Var x).

What changes when we move to higher order abstract syntax?

1 Var terms go away – we use the meta-language’s variables.

2 (Recfun τ1 τ2 f x e) now uses meta-language abstraction:(Recfun τ1 τ2 (f . x . e)).

37

Page 38: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Abstract Syntax

Moving to first order abstract syntax, we get:

1 Things like numbers and boolean literals are wrapped in terms(Num, Lit)

2 Operators like a + b become (Plus a b).

3 if c then t else e becomes (If c t e).

4 Function applications e1 e2 become explicit (Apply e1 e2).

5 recfun f :: (τ1 → τ2) x = e becomes (Recfun τ1 τ2 f x e).

6 Variable usages are wrapped in a term (Var x).

What changes when we move to higher order abstract syntax?

1 Var terms go away – we use the meta-language’s variables.

2 (Recfun τ1 τ2 f x e) now uses meta-language abstraction:(Recfun τ1 τ2 (f . x . e)).

38

Page 39: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Abstract Syntax

Moving to first order abstract syntax, we get:

1 Things like numbers and boolean literals are wrapped in terms(Num, Lit)

2 Operators like a + b become (Plus a b).

3 if c then t else e becomes (If c t e).

4 Function applications e1 e2 become explicit (Apply e1 e2).

5 recfun f :: (τ1 → τ2) x = e becomes (Recfun τ1 τ2 f x e).

6 Variable usages are wrapped in a term (Var x).

What changes when we move to higher order abstract syntax?

1 Var terms go away – we use the meta-language’s variables.

2 (Recfun τ1 τ2 f x e) now uses meta-language abstraction:(Recfun τ1 τ2 (f . x . e)).

39

Page 40: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Abstract Syntax

Moving to first order abstract syntax, we get:

1 Things like numbers and boolean literals are wrapped in terms(Num, Lit)

2 Operators like a + b become (Plus a b).

3 if c then t else e becomes (If c t e).

4 Function applications e1 e2 become explicit (Apply e1 e2).

5 recfun f :: (τ1 → τ2) x = e becomes (Recfun τ1 τ2 f x e).

6 Variable usages are wrapped in a term (Var x).

What changes when we move to higher order abstract syntax?

1 Var terms go away – we use the meta-language’s variables.

2 (Recfun τ1 τ2 f x e) now uses meta-language abstraction:(Recfun τ1 τ2 (f . x . e)).

40

Page 41: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Abstract Syntax

Moving to first order abstract syntax, we get:

1 Things like numbers and boolean literals are wrapped in terms(Num, Lit)

2 Operators like a + b become (Plus a b).

3 if c then t else e becomes (If c t e).

4 Function applications e1 e2 become explicit (Apply e1 e2).

5 recfun f :: (τ1 → τ2) x = e becomes (Recfun τ1 τ2 f x e).

6 Variable usages are wrapped in a term (Var x).

What changes when we move to higher order abstract syntax?

1 Var terms go away – we use the meta-language’s variables.

2 (Recfun τ1 τ2 f x e) now uses meta-language abstraction:(Recfun τ1 τ2 (f . x . e)).

41

Page 42: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Abstract Syntax

Moving to first order abstract syntax, we get:

1 Things like numbers and boolean literals are wrapped in terms(Num, Lit)

2 Operators like a + b become (Plus a b).

3 if c then t else e becomes (If c t e).

4 Function applications e1 e2 become explicit (Apply e1 e2).

5 recfun f :: (τ1 → τ2) x = e becomes (Recfun τ1 τ2 f x e).

6 Variable usages are wrapped in a term (Var x).

What changes when we move to higher order abstract syntax?

1 Var terms go away – we use the meta-language’s variables.

2 (Recfun τ1 τ2 f x e) now uses meta-language abstraction:(Recfun τ1 τ2 (f . x . e)).

42

Page 43: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Abstract Syntax

Moving to first order abstract syntax, we get:

1 Things like numbers and boolean literals are wrapped in terms(Num, Lit)

2 Operators like a + b become (Plus a b).

3 if c then t else e becomes (If c t e).

4 Function applications e1 e2 become explicit (Apply e1 e2).

5 recfun f :: (τ1 → τ2) x = e becomes (Recfun τ1 τ2 f x e).

6 Variable usages are wrapped in a term (Var x).

What changes when we move to higher order abstract syntax?

1 Var terms go away – we use the meta-language’s variables.

2 (Recfun τ1 τ2 f x e) now uses meta-language abstraction:(Recfun τ1 τ2 (f . x . e)).

43

Page 44: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Working Statically with HOAS

To Code

We’re going to write code for an AST and pretty-printer for MinHSwith HOAS.Seeing as this requires us to look under abstractions withoutevaluating the term, we have to extend the AST with special “tag”values.

44

Page 45: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static SemanticsTo check if a MinHS program is well-formed, we need to check:

1 Scoping – all variables used must be well defined2 Typing – all operations must be used on compatible types.

Our judgement is an extension of the scoping rules to include types:

Γ ` e : τ

Under this context of assumptions

The expression is assigned this type

The context Γ includes typing assumptions for the variables:

x : Int, y : Int ` (Plus x y) : Int

45

Page 46: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static SemanticsTo check if a MinHS program is well-formed, we need to check:

1 Scoping – all variables used must be well defined2 Typing – all operations must be used on compatible types.

Our judgement is an extension of the scoping rules to include types:

Γ ` e : τ

Under this context of assumptions

The expression is assigned this type

The context Γ includes typing assumptions for the variables:

x : Int, y : Int ` (Plus x y) : Int

46

Page 47: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) :

τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ,

x : τ1, f : (τ1 → τ2)

` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) :

τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

47

Page 48: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) :

τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ,

x : τ1, f : (τ1 → τ2)

` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) :

τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

48

Page 49: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool

Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) :

τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ,

x : τ1, f : (τ1 → τ2)

` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) :

τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

49

Page 50: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ,

x : τ1, f : (τ1 → τ2)

` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) :

τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

50

Page 51: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ,

x : τ1, f : (τ1 → τ2)

` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) :

τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

51

Page 52: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ,

x : τ1, f : (τ1 → τ2)

` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) :

τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

52

Page 53: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ, x : τ1,

f : (τ1 → τ2)

` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) :

τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

53

Page 54: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ, x : τ1, f : (τ1 → τ2) ` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) :

τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

54

Page 55: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ, x : τ1, f : (τ1 → τ2) ` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) : τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

55

Page 56: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ, x : τ1, f : (τ1 → τ2) ` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) : τ1 → τ2

Γ ` e1 : τ1 → τ2

Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

56

Page 57: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ, x : τ1, f : (τ1 → τ2) ` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) : τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) :

τ2

Let’s implement a type checker.

57

Page 58: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Static Semantics

Γ ` (Num n) : Int Γ ` (Lit b) : Bool

Γ ` e1 : Int Γ ` e2 : Int

Γ ` (Plus e1 e2) : Int

Γ ` e1 : Bool Γ ` e2 : τ Γ ` e3 : τ

Γ ` (If e1 e2 e3) : τ

(x : τ) ∈ Γ

Γ ` x : τ

Γ, x : τ1, f : (τ1 → τ2) ` e : τ2

Γ ` (Recfun τ1 τ2 (f . x . e)) : τ1 → τ2

Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1

Γ ` (Apply e1 e2) : τ2

Let’s implement a type checker.

58

Page 59: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Dynamic Semantics

Structural Operational Semantics (Small-Step)

Initial states:

All well typed expressions.Final states: (Num n), (Lit b), Recfun too!

Evaluation of built-in operations:

e1 7→ e ′1

(Plus e1 e2) 7→ (Plus e ′1 e2)

(and so on as per arithmetic expressions)

59

Page 60: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Dynamic Semantics

Structural Operational Semantics (Small-Step)

Initial states: All well typed expressions.Final states:

(Num n), (Lit b), Recfun too!

Evaluation of built-in operations:

e1 7→ e ′1

(Plus e1 e2) 7→ (Plus e ′1 e2)

(and so on as per arithmetic expressions)

60

Page 61: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Dynamic Semantics

Structural Operational Semantics (Small-Step)

Initial states: All well typed expressions.Final states: (Num n), (Lit b),

Recfun too!

Evaluation of built-in operations:

e1 7→ e ′1

(Plus e1 e2) 7→ (Plus e ′1 e2)

(and so on as per arithmetic expressions)

61

Page 62: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Dynamic Semantics

Structural Operational Semantics (Small-Step)

Initial states: All well typed expressions.Final states: (Num n), (Lit b), Recfun too!

Evaluation of built-in operations:

e1 7→ e ′1

(Plus e1 e2) 7→ (Plus e ′1 e2)

(and so on as per arithmetic expressions)

62

Page 63: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

Specifying If

e1 7→ e ′1

(If e1 e2 e3) 7→ (If e ′1 e2 e3)

(If (Lit True) e2 e3) 7→ e2

(If (Lit False) e2 e3) 7→ e3

63

Page 64: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

How about Functions?Recall that Recfun is a final state – we don’t need to evaluate itwhen it’s alone.

Evaluating function application requires us to:

1 Evaluate the left expression to get the function being applied

2 Evaluate the right expression to get the argument value

3 Evaluate the function’s body, after supplying substitutions forthe abstracted variables.

e1 7→ e′1(Apply e1 e2) 7→ (Apply e′1 e2)

e2 7→ e′2(Apply (Recfun . . . ) e2) 7→ (Apply (Recfun . . . ) e′2)

64

Page 65: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

How about Functions?Recall that Recfun is a final state – we don’t need to evaluate itwhen it’s alone.

Evaluating function application requires us to:

1 Evaluate the left expression to get the function being applied

2 Evaluate the right expression to get the argument value

3 Evaluate the function’s body, after supplying substitutions forthe abstracted variables.

e1 7→ e′1(Apply e1 e2) 7→ (Apply e′1 e2)

e2 7→ e′2(Apply (Recfun . . . ) e2) 7→ (Apply (Recfun . . . ) e′2)

65

Page 66: Functional Programming Languagescs3161/19t3/Week 05/1Mon/Slides.pdfThe Functional Paradigm MinHS Functional Programming Many languages have been calledfunctionalover the years: Lisp

The Functional Paradigm MinHS

How about Functions?Recall that Recfun is a final state – we don’t need to evaluate itwhen it’s alone.

Evaluating function application requires us to:

1 Evaluate the left expression to get the function being applied

2 Evaluate the right expression to get the argument value

3 Evaluate the function’s body, after supplying substitutions forthe abstracted variables.

e1 7→ e′1(Apply e1 e2) 7→ (Apply e′1 e2)

e2 7→ e′2(Apply (Recfun . . . ) e2) 7→ (Apply (Recfun . . . ) e′2)

v ∈ F

(Apply (Recfun τ1 τ2 (f .x . e)) v) 7→ e[x := v , f := (Recfun τ1 τ2 (f .x . e))]

66