01 Intro
01 Intro
Languages
Lecture 1 : Overview
“Language Foundation,
Extensions and Reasoning
CS5205 Introduction 1
Course Objectives
CS5205 Introduction 2
Course Outline
CS5205 Introduction 3
Administrative Matters
- mainly IVLE
CS5205 Introduction 4
Paper Critique
• Focus on Language Innovations
• Select Topic (Week 3)
• 2-Page Summary (Week 5)
• Prepare Presentation (Week 7)
• Oral Presentation (Last 4 weeks)
• Paper Critique (Week 13)
• Possible Topics
• Concurrent and MultiCore Programming
• Software Transaction Memory
• GUI Programming with Array
• Testing with QuickCheck
• IDE for Haskell
• SELinks (OCaml)
CS5205 Introduction 5
Advanced Language - Haskell
CS5205 Introduction 6
Example - Haskell Program
• Apply a function to every element of a list.
a type variable
CS5205 Introduction 8
Type System – Lightweight Analysis
CS5205 Introduction 9
Specification with Separation Logic
• Why? sw reliability
CS5205 Introduction 10
Lambda Calculus
CS5205 Introduction 11
Untyped Lambda Calculus
CS5205 Introduction 12
Functions without Names
( x . x+1) (5)
CS5205 Introduction 13
Syntax
In purest form (no constraints, no built-in operations), the lambda calculus
has the following syntax.
t ::= terms
x variable
x.t abstraction
tt application
CS5205 Introduction 14
Conventions
• Parentheses are used to avoid ambiguities.
e.g. x y z can be either (x y) z or x (y z)
CS5205 Introduction 15
Scope
• An occurrence of variable x is said to be bound when it
occurs in the body t of an abstraction x . t
• Examples: x y
y. x y
x. x (identity
function)
( x. x x) ( x. x x) (non-stop loop)
x. x) y
x. x) x
CS5205 Introduction 16
Alpha Renaming
• Lambda expressions are equivalent up to bound variable
renaming.
e.g. x. x = y. y
y. x y = z. x z
But NOT:
y. x y = y. z y
CS5205 Introduction 17
Beta Reduction
• An application whose LHS is an abstraction, evaluates to
the body of the abstraction with parameter substitution.
e.g. ( x. x y) z !z y
( x. y) z !y
( x. x x) ( x. x x)!( x. x x) ( x. x x)
( x . t 1 ) t2 ! [x t2] t1
• Examples:
- full beta reduction
- normal order
- call by name
- call by value, etc
CS5205 Introduction 19
Full Beta Reduction
• Any redex can be chosen, and evaluation proceeds until no
more redexes found.
• Example Reduction:
CS5205 Introduction 21
Call by Name Reduction
• Chooses the leftmost, outermost redex, but never reduces
inside abstractions.
• Example:
CS5205 Introduction 22
Call by Value Reduction
• Chooses the leftmost, innermost redex whose RHS is a
value; and never reduces inside abstractions.
• Example:
CS5205 Introduction 23
Strict vs Non-Strict Languages
• Strict languages always evaluate all arguments to function
before entering call. They employ call-by-value evaluation
(e.g. C, Java, ML).
CS5205 Introduction 24
Programming Techniques in -Calculus
• Multiple arguments.
• Church Booleans.
• Pairs.
• Church Numerals.
• Recursion.
• Extended Calculus
CS5205 Introduction 25
Multiple Arguments
• Pass multiple arguments one by one using lambda
abstraction as intermediate results. The process is also
known as currying.
• Example:
f = (x,y).s f = x. ( y. s)
Application:
f(v,w) (f v) w
requires pairs as requires higher
primitve types order feature
CS5205 Introduction 26
Church Booleans
• Church’s encodings for true/false type with a conditional:
true = t. f. t
false = t. f. f
if = l. m. n. l m n
• Example:
if true v w
= ( l. m. n. l m n) true v w
! true v w
= ( t. f. t) v w
! v
pair = f. s. b. b f s
fst = p. p true
snd = p. p false
• Example:
snd (pair c d)
= ( p. p false) (( f. s. b. b f s) c d)
! ( p. p false) ( b. b c d)
! ( b. b c d) false
! false c d
! d
CS5205 Introduction 28
Church Numerals
CS5205 Introduction 29
Church Numerals
• Successor function can be defined as:
succ = n. s. z. s (n s z)
Example:
succ c1
= ( n. s. z. s (n s z)) ( s. z. s z)
s. z. s (( s. z. s z) s z)
! s. z. s (s z)
succ c2
= n. s. z. s (n s z) ( s. z. s (s z))
! s. z. s (( s. z. s (s z)) s z)
! s. z. s (s (s z))
CS5205 Introduction 30
Church Numerals
• Other Arithmetic Operations:
plus = m. n. s. z. m s (n s z)
times = m. n. m (plus n) c0
iszero = m. m ( x. false) true
CS5205 Introduction 31
Recursion
• Some terms go into a loop and do not have normal form.
Example:
( x. x x) ( x. x x)
! ( x. x x) ( x. x x)
! …
CS5205 Introduction 32
Example - Factorial
• We can define factorial as:
CS5205 Introduction 33
Example - Factorial
• Recall:
fact = fix ( h. n. if (n<=1) then 1 else times n (h (pred n)))
Example reduction:
fact 3 = fix g 3
= g (fix g) 3
= times 3 ((fix g) (pred 3))
= times 3 (g (fix g) 2)
= times 3 (times 2 ((fix g) (pred 2)))
= times 3 (times 2 (g (fix g) 1))
= times 3 (times 2 1)
= 6
CS5205 Introduction 34
Enriching the Calculus
• Example:
x. succ (succ x) 2 NB
x. true 2 NB
CS5205 Introduction 35
Formal Treatment of Lambda Calculus
CS5205 Introduction 36
Free Variables
FV(x) = {x}
CS5205 Introduction 37
Substitution
[x z. z w] ( y.x) = ( y. z. z w)
[x z. z w] ( x.x) ( x. z. z w)
[x z. z w] ( w.x) ( w. z. z w)
CS5205 Introduction 38
Formal Defn of Substitution
[x s] x = s if y=x
[x s] y = y if yx
[x s] (t1 t2) = ([x s] t1) ([x s] t2)
[x s] ( y.t) = y. [x s] t if y x Æ y FV(s)
[x s] ( y.t) = [x s] ( z. [y z] t)
if y x Æ y 2 FV(s) Æ fresh z
CS5205 Introduction 39
Syntax of Lambda Calculus
• Term:
t ::= terms
x variable
x.t abstraction
tt application
• Value:
v ::= value
x variable
x.t abstraction value
CS5205 Introduction 40
Call-by-Value Semantics
premise
t1 ! t’1
(E-App1)
t1 t2 ! t’1 t2
conclusion
t2 ! t’2
(E-App2)
v1 t2 ! v1 t’2
( x.t) v ! [x v] t (E-AppAbs)
CS5205 Introduction 41
Call-by-Name Semantics
t1 ! t’1
(E-App1)
t1 t2 ! t’1 t2
CS5205 Introduction 42
Getting Stuck
CS5205 Introduction 43
Boolean-Enriched Lambda Calculus
• Term:
t ::= terms
x variable
x.t abstraction
tt application
true constant true
false constant false
if t then t else t conditional
• Value:
v ::= value
x.t abstraction value
true true value
false false value
CS5205 Introduction 44
Key Ideas
CS5205 Introduction 45
Simple Types
• T ::= types
Bool type of booleans
T!T type of functions
• ! is right-associative:
CS5205 Introduction 46
Implicit or Explicit Typing
CS5205 Introduction 47
Explicitly Typed Lambda Calculus
• t ::= terms
…
x : T.t abstraction
…
• v ::= value
x : T.t abstraction value
…
• T ::= types
Bool type of booleans
T!T type of functions
CS5205 Introduction 48
Examples
true
x:Bool . x
( x:Bool . x) true
CS5205 Introduction 49