pl’17 exam preparation: lambda calculus, javascriptmsagiv/courses/pl17/pl17_exam... ·...

29
PL’17 Exam Preparation: Lambda Calculus, JavaScript, Type Inference Oded Padon & Mooly Sagiv

Upload: others

Post on 30-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

PL’17 Exam Preparation:Lambda Calculus,

JavaScript,Type Inference

Oded Padon & Mooly Sagiv

Page 2: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Untyped Lambda Calculus - Syntax

t ::= termsx variable x. t abstractiont t application

• Terms can be represented as abstract syntax trees

• Syntactic Conventions:

• Applications associates to left :e1 e2 e3 (e1 e2) e3

• The body of abstraction extends as far as possible:x. y. x y x x. (y. ((x y) x))

2

Page 3: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Non-Deterministic Operational Semantics

( x. t1) t2 [x ↦t2] t1

t2 t’2

t1 t2 t1 t’2

t t’

x. t x. t’

t1 t’1

t1 t2 t’1 t2

Why is this semantics non-deterministic?

(E-AppAbs) (E-Abs)

(E-App1) (E-App2)

3

Page 4: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Different Evaluation Orders

(x. (add x x)) (add 2 3) (x. (add x x)) (5) add 5 5 10

(x. (add x x)) (add 2 3) (add (add 2 3) (add 2 3))

(add 5 (add 2 3)) (add 5 5) 10

This example: same final result but lazy performs more computations

( x. t1) t2 [x ↦t2] t1

t2 t’2

t1 t2 t1 t’2

t t’

x. t x. t’

t1 t’1

t1 t2 t’1 t2

(E-AppAbs) (E-Abs)

(E-App1) (E-App2)

4

Page 5: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Different Evaluation Orders

(x. y. x) 3 (div 5 0) Exception: Division by zero

(x. y. x) 3 (div 5 0) (y. 3) (div 5 0) 3

This example: lazy suppresses erroneous division and reduces to final result

Can also suppress non-terminating computation.

Many times we want this, for example:

if i < len(a) and a[i]==0: print “found zero”

( x. t1) t2 [x ↦t2] t1

t2 t’2

t1 t2 t1 t’2

t t’

x. t x. t’

t1 t’1

t1 t2 t’1 t2

(E-AppAbs) (E-Abs)

(E-App1) (E-App2)

5

Page 6: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

t t’

x. t x. t’

(E-Abs)

t1 t’1

t1 t2 t’1 t2

(E-App1)

t2 t’2

t1 t2 t1 t’2

(E-App2)

( x. t1) t2 [x ↦t2] t1

(E-AppAbs)

t1 t’1

t1 t2 t’1 t2

(E-App1)

( x. t1) t2 [x ↦t2] t1

(E-AppAbs)

t1 t’1

t1 t2 t’1 t2

(E-App1)

t2 t’2

t1 t2 t1 t’2

(E-App2)

( x. t1) t2 [x ↦t2] t1

(E-AppAbs)

Strict Lazy Normal Order

precedence

precedence

precedence

precedence

6

Page 7: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

v ::= x. t abstraction values

Call-by-value Operations Semantics via Inductive Definition (no precedence)

( x. t1) v2 [x ↦v2] t1 (E-AppAbs)

t ::= terms

x variable

x. t abstraction

t t application

t1 t’1

t1 t2 t’1 t2(E-APPL1)

t2 t’2v1 t2 v1 t’2

(E-APPL2)

7

Page 8: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Summary Order of Evaluation

• Full-beta-reduction

– All possible orders

• Applicative order call by value (strict, eager)

– Left to right

– Fully evaluate arguments before function application

• Normal order

– The leftmost, outermost redex is always reduced first

• Call by name (lazy)

– Evaluate arguments as needed

• Call by need

– Evaluate arguments as needed and store for subsequent usages

– Implemented in Haskell8

Page 9: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

2016 Moed B

9

Page 10: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Typed Lambda Calculus

Chapter 9

Benjamin Pierce

Types and Programming Languages

Page 11: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Simple Types

T ::= typesBool type of BooleansT T type of functions

T1 T2 T3 = T1 ( T2 T3)

11

Page 12: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Simple Typed Lambda Calculus

t ::= termsx variable x: T. t abstractiont t application

T::= typesBool atomic type for BooleansT T types of functions

Tying Relation • Type fact: t:T means term t has type T (e.g. 1+3:Int, f:IntInt)• Typing relation : relates sets of type facts and type facts• t:T means under (context, environment), term t has type T• t:T means term t has type T under the empty environment (no

assumptions)• Can t have free variables?

12

Page 13: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Typing Relation Examples

13

• Type fact: t:T means term t has type T (e.g. 1+3:Int, f:IntInt)• t:T means under (context, environment), term t has type T• t:T means closed term t has type T under the empty environmentExamples• x:Int, y:Int x+y : ?• x:Int, y:Bool x+y : ?• x:Int x+y : ?• x:Int, y:Int, b:Bool if b then x else y : ?• x:Int, y:Bool, b:Bool if b then x else y : ?• x:Int, b:Bool if b then x else y : ?• x:Int, f:Int Bool f x : ?• x:Int, f: Int Int Bool f x x : ?• x:Int, f: Int Int Bool f x: ?• f: Int Int Bool f x x : ?• 1+2 : ?• true : ?• x:Int, y:Int, f: Int Int Bool 1+2 : ?

Page 14: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Formally Defining

14

t ::= terms

x variable

x: T. t abstraction

t t application

T::= types

T T types of functions

::= context

empty context

, x : T term variable binding

x : T

x : T(T-VAR)

, x : T1 t2 : T2

x : T1. t2 : T1 T2

(T-ABS)

t1 t2 : T12

(T-APP) t1 : T11 T12 t2 : T11

Page 15: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Adding Booleans

15

t ::= termsx variable x: T. t abstractiont t application true constant truefalse constant falseif t then t else t conditional

T::= typesBool type of BooleansT T types of functions

::= context empty context, x : T term variable binding

true : Bool

false : Bool

t1:Bool t2:T t3:T

if t1 then t2 else t3 : T(T-IF)

(T-FALSE)

(T-TRUE)

x : T

x : T(T-VAR)

, x : T1 t2 : T2

x : T1. t2 : T1 T2

(T-ABS)

t1 t2 : T12

(T-APP) t1 : T11 T12 t2 : T11

Page 16: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Example Typing Tree

T-IF

16

f:Bool Bool, x:Bool false:Bool f:Bool Bool , x:Bool x:Bool f:Bool Bool , x:Bool f x : Bool

f:Bool Bool, x:Bool if false then x else f x : Bool

f:Bool Bool x:Bool if false then x else f x : Bool BoolT-ABS

T-FALSE T-VAR T-APP

f:Bool Bool , x:Bool f:Bool Boolf:Bool Bool , x:Bool x:Bool T-VART-VAR

t1:Bool t2:T t3:T

if t1 then t2 else t3 : T(T-IF) false : Bool (T-FALSE)

x : T

x : T(T-VAR)

, x : T1 t2 : T2

x : T1. t2 : T1 T2

(T-ABS) t1 t2 : T12

(T-APP) t1 : T11 T12 t2 : T11

Page 17: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Homework 3

17

x : T

x : T(T-VAR)

, x : T1 t2 : T2

x : T1. t2 : T1 T2

(T-ABS)

t1 t2 : T12

(T-APP) t1 : T11 T12 t2 : T11

true : Bool

false : Bool

t1:Bool t2:T t3:T

if t1 then t2 else t3 : T(T-IF)

(T-FALSE)

(T-TRUE)

Page 18: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

JavaScript

18

Page 19: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

2015 A

19

Page 20: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

20

2016 A

Page 21: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

JavaScript Event Loop

21

Page 22: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Type Inference

22

Page 23: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

Type Inference Algorithm

• Parse program to build parse tree

• Assign type variables to nodes in tree

• Generate constraints:

– From environment: literals (2), built-in operators (+), known functions (tail)

– From form of parse tree: e.g., application and abstraction nodes

• Solve constraints using unification

• Determine types of top-level declarations

Page 24: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

let isempty l = match l with

|[] -> true

| _ -> false

fun

isempty : t_0 l : t_1 match : t_2

l : t_1 Case 1 Case 2

[] : [t_3] true : bool _ : t_4 false : bool

t_0 = t_1 -> t_2 (Fun)

t_1 = [t_3] (Case 1)

t_2 = bool (Case 1)

t_1 = t_4 (Case 2)

t_2 = bool (Case 2)

Unification Constrains:

isempty : [t_3] -> bool

Page 25: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

2016 B

25

Page 26: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

let rec absent:t1 x:t2 xs:t3 =

(match xs:t3 with

| []:[t5] -> true:bool

| (h:t6 :: t:t7):t8 ->

(absent:t1 x:t2 t:t7):t9):t4

26

Page 27: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

t1 = t2 -> t3 -> t4

t3 = [t5]

t4 = bool

t7 = [t6]

t8 = [t6]

t8 = t3

t1 = t2 -> t7 -> t9

t9 = t4

--------------------

absent : t2 -> [t5] -> bool

27

let rec absent:t1 x:t2 xs:t3 =

(match xs:t3 with

| []:[t5] -> true:bool

| (h:t6 :: t:t7):t8 ->

(absent:t1 x:t2 t:t7):t9):t4

Unification Constrains

Page 28: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

let rec absent:t1 x:t2 xs:t3 =

(match xs:t3 with

| []:[t5] -> true:bool

| (h:t6 :: t:t7):t8 ->

((h:t6 <> x:t2):bool and

(absent:t1 x:t2 t:t7):t9):t10):t4

28

Page 29: PL’17 Exam Preparation: Lambda Calculus, JavaScriptmsagiv/courses/pl17/pl17_exam... · 2017-01-29 · Untyped Lambda Calculus - Syntax t ::= terms x variable x. t abstraction t

t1 = t2 -> t3 -> t4

t3 = [t5]

t4 = bool

t7 = [t6]

t8 = [t6]

t8 = t3

t1 = t2 -> t7 -> t9

t6=t2

t9=bool

t10=bool

t10 = t4

--------------------

absent : t5 -> [t5] -> bool

29

Unification Constrainslet rec absent:t1 x:t2 xs:t3 =

(match xs:t3 with

| []:[t5] -> true:bool

| (h:t6 :: t:t7):t8 ->

((h:t6 <> x:t2):bool and

(absent:t1 x:t2 t:t7):t9):t10):t4