pl’17 exam preparation: lambda calculus, javascriptmsagiv/courses/pl17/pl17_exam... ·...
TRANSCRIPT
PL’17 Exam Preparation:Lambda Calculus,
JavaScript,Type Inference
Oded Padon & Mooly Sagiv
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
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
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
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
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
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
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
2016 Moed B
9
Typed Lambda Calculus
Chapter 9
Benjamin Pierce
Types and Programming Languages
Simple Types
T ::= typesBool type of BooleansT T type of functions
T1 T2 T3 = T1 ( T2 T3)
11
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
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 : ?
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
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
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
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)
JavaScript
18
2015 A
19
20
2016 A
JavaScript Event Loop
21
Type Inference
22
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
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
2016 B
25
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
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
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
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