cs5205: foundation in programming languages lecture 1 : overview

49
CS5205 Introduction 1 CS5205: Foundation in Programming Languages Lecture 1 : Overview Lecturer : Chin Wei Ngan Email : [email protected] Office : S15 06-01 “Language Foundation, Extensions and Reasoning

Upload: erelah

Post on 06-Feb-2016

26 views

Category:

Documents


1 download

DESCRIPTION

CS5205: Foundation in Programming Languages Lecture 1 : Overview. “Language Foundation, Extensions and Reasoning. Lecturer : Chin Wei Ngan Email : [email protected] Office : S15 06-01. Course Objectives. - graduate-level course with foundation focus - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 1

CS5205: Foundation in Programming Languages

Lecture 1 : Overview

Lecturer : Chin Wei Ngan

Email : [email protected]

Office : S15 06-01

“Language Foundation,Extensions and Reasoning

Page 2: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 2

Course Objectives Course Objectives

- graduate-level course with foundation focus

- languages as tool for programming

- foundations for reasoning about programs

- explore various language innovations

Page 3: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 3

Course Outline Course Outline

• Lecture Topics (13 weeks)• Lambda Calculus• Advanced Language (Haskell)

http://www.haskell.org• Type System for Lightweight Analysis

http://www.cs.cmu.edu/~rwh/plbook/book.pdf• Semantics• Formal Reasoning – Separation Logic + Provers• Language Innovations (paper readings)

Page 4: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 4

Administrative Matters Administrative Matters

- mainly IVLE

- Reading Materials (mostly online): www.haskell.orgRobert Harper : Foundations of Practical Programming Languages.

Free PL books : http://www.cs.uu.nl/~franka/ref

- Lectures + Assignments + Presentation + Exam- Assignment/Quiz (30%)- Paper Reading + Critique (25%)- Exam (45%)

Page 5: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 5

Paper Critique 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)

Page 6: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 6

Advanced Language - Haskell Advanced Language - Haskell

• Strongly-typed with polymorphism• Higher-order functions• Pure Lazy Language. • Algebraic data types + records• Exceptions• Type classes, Monads, Arrows, etc• Advantages : concise, abstract, reuse

• Why use Haskell ? cool & greaterproductivity

Page 7: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 7

Example - Haskell ProgramExample - Haskell Program

• Apply a function to every element of a list.

data List a = Nil | Cons a (List a)

map f Nil = Nilmap f (Cons x xs) = Cons (f x) (map f xs)

map inc (Cons 1 (Cons 2 (Cons 3 Nil)))==> (Cons 2 (Cons 3 (Cons 4 Nil)))

a type variable

type is : (a b) (List a) (List b)

Page 8: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 8

Some ApplicationsSome Applications

• Hoogle – search in code library

• Darcs – distributed version control

• Programming user interface with arrows..

• How to program multicore?

• map/reduce and Cloud computing

• Some issues to be studied in Paper Reading.

Page 9: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 9

Type System – Lightweight Analysis Type System – Lightweight Analysis

• Abstract description of code + genericity

• Compile-time analysis that is tractable

• Guarantees absence of some bad behaviors

• Issues – expressivity, soundness, completeness, inference?

• How to use, design and prove type system.

• Why? detect bugs

Page 10: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 10

Specification with Separation Logic Specification with Separation Logic

• Is sorting algorithm correct?• Any memory leaks?• Any null pointer dereference?• Any array bound violation?

• What is the your specification/contract?

• How to verify program correctness?

• Issues – mutation and aliasing

• Why? sw reliability

Page 11: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 11

Lambda Calculus Lambda Calculus

• Untyped Lambda Calculus• Evaluation Strategy• Techniques - encoding, extensions, recursion• Operational Semantics• Explicit Typing

Introduction to Lambda Calculus: http://www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf http://www.cs.chalmers.se/Cs/Research/Logic/TypesSS05/Extra/geuvers.pdfLambda Calculator : http://ozark.hendrix.edu/~burch/proj/lambda/download.html

Page 12: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 12

Untyped Lambda Calculus Untyped Lambda Calculus

• Extremely simple programming language which captures core aspects of computation and yet allows programs to be treated as mathematical objects.

• Focused on functions and applications.

• Invented by Alonzo (1936,1941), used in programming (Lisp) by John McCarthy (1959).

Page 13: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 13

Functions without Names Functions without Names

Usually functions are given a name (e.g. in language C):

int plusOne(int x) { return x+1; }…plusOne(5)…

However, function names can also be dropped:

(int (int x) { return x+1;} ) (5)

Notation used in untyped lambda calculus:

( x . x+1) (5)

Page 14: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 14

Syntax Syntax

In purest form (no constraints, no built-in operations), the lambda calculus has the following syntax.

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

This is simplest universal programming language!

Page 15: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 15

Conventions Conventions

• Parentheses are used to avoid ambiguities.e.g. x y z can be either (x y) z or x (y z)

• Two conventions for avoiding too many parentheses:• Applications associates to the left

e.g. x y z stands for (x y) z

• Bodies of lambdas extend as far as possible.e.g. x. y. x y x stands for x. ( y. ((x y) x)).

• Nested lambdas may be collapsed together. e.g. x. y. x y x can be written as x y. x y x

Page 16: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 16

Scope Scope

• An occurrence of variable x is said to be bound when it occurs in the body t of an abstraction x . t

• An occurrence of x is free if it appears in a position where it is not bound by an enclosing abstraction of x.

• Examples: x yy. x y

x. x (identity function)( x. x x) ( x. x x) (non-stop loop) x. x) y x. x) x

Page 17: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 17

Alpha Renaming 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

• Alpha renaming rule:

x . E = z . [x z] E (z is not free in E)

Page 18: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 18

Beta Reduction 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)

• Beta reduction rule (operational semantics):

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

Expression of form ( x . t1 ) t2 is called a redex (reducible expression).

Page 19: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 19

Evaluation Strategies Evaluation Strategies

• A term may have many redexes. Evaluation strategies can be used to limit the number of ways in which a term can be reduced.

• An evaluation strategy is deterministic, if it allows reduction with at most one redex, for any term.

• Examples: - full beta reduction - normal order - call by name - call by value, etc

Page 20: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 20

Full Beta Reduction Full Beta Reduction • Any redex can be chosen, and evaluation proceeds until no

more redexes found.

• Example: (x.x) ((x.x) (z. (x.x) z)) denoted by id (id (z. id z))

Three possible redexes to choose: id (id (z. id z))

id (id (z. id z)) id (id (z. id z))

• Reduction:id (id (z. id z))

! id (id (z.z)) ! id (z.z)

! z.z !

Page 21: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 21

Normal Order Reduction Normal Order Reduction

• Deterministic strategy which chooses the leftmost, outermost redex, until no more redexes.

• Example Reduction:

id (id (z. id z)) ! id (z. id z)) ! z.id z ! z.z !

Page 22: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 22

Call by Name Reduction Call by Name Reduction

• Chooses the leftmost, outermost redex, but never reduces inside abstractions.

• Example:

id (id (z. id z)) ! id (z. id z)) ! z.id z !

Page 23: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 23

Call by Value Reduction Call by Value Reduction

• Chooses the leftmost, innermost redex whose RHS is a value; and never reduces inside abstractions.

• Example:

id (id (z. id z)) ! id (z. id z) ! z.id z !

Page 24: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 24

Strict vs Non-Strict Languages 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).

• Non-strict languages will enter function call and only evaluate the arguments as they are required. Call-by-name (e.g. Algol-60) and call-by-need (e.g. Haskell) are possible evaluation strategies, with the latter avoiding the re-evaluation of arguments.

• In the case of call-by-name, the evaluation of argument occurs with each parameter access.

Page 25: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 25

Programming Techniques in Programming Techniques in -Calculus-Calculus

• Multiple arguments.

• Church Booleans.

• Pairs.

• Church Numerals.

• Recursion.

• Extended Calculus

Page 26: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 26

Multiple Arguments 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 primitve types

requires higherorder feature

Page 27: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 27

Church Booleans Church Booleans • Church’s encodings for true/false type with a conditional: true = t. f. t

false = t. f. fif = 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

• Boolean and operation can be defined as:and a. b. if a b false

= a. b. ( l. m. n. l m n) a b false= a. b. a b false

Page 28: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 28

Pairs Pairs • Define the functions pair to construct a pair of values, fst to

get the first component and snd to get the second component of a given pair as follows:

pair = f. s. b. b f s

fst = p. p truesnd = 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

Page 29: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 29

Church Numerals Church Numerals

• Numbers can be encoded by:c0 = s. z. z

c1 = s. z. s zc2 = s. z. s (s z)c3 = s. z. s (s (s z))

:

Page 30: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 30

Church Numerals 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))

Page 31: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 31

Church Numerals 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

• Exercise : Try out the following. plus c1 x times c0 xtimes x c1

iszero c0

iszero c2

Page 32: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 32

Recursion 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)! …

• However, others have an interesting property fix = λf. (λx. f (x x)) (λx. f (x x))

fix = f. ( x. f ( y. x x y)) ( x. f ( y. x x y))

that returns a fix-point for a given functional. Given x = h x

= fix h That is: fix h ! h (fix h) ! h (h (fix h)) ! …

x is fix-point of h

Page 33: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 33

Example - FactorialExample - Factorial

• We can define factorial as:

fact = n. if (n<=1) then 1 else times n (fact (pred n))

= ( h. n. if (n<=1) then 1 else times n (h (pred n))) fact

= fix ( h. n. if (n<=1) then 1 else times n (h (pred n)))

Page 34: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 34

Example - FactorialExample - Factorial

• Recall:fact = fix ( h. n. if (n<=1) then 1 else times n (h (pred n)))

• Let g = ( 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

Page 35: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 35

Enriching the Calculus Enriching the Calculus

• We can add constants and built-in primitives to enrich -calculus. For example, we can add boolean and arithmetic constants and primitives (e.g. true, false, if, zero, succ, iszero,

pred) into an enriched language we call NB:

• Example: x. succ (succ x) 2 NB

x. true 2 NB

Page 36: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 36

Formal Treatment of Lambda CalculusFormal Treatment of Lambda Calculus

• Let V be a countable set of variable names. The set of terms is the smallest set T such that:1. x 2 T for every x 2 V

2. if t1 2 T and x 2 V, then x. t1 2 T

3. if t1 2 T and t2 2 T, then t1 t2 2 T

• Recall syntax of lambda calculus:t ::= terms

x variable x.t abstractiont t application

Page 37: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 37

Free VariablesFree Variables

• The set of free variables of a term t is defined as:

FV(x) = {x}

FV( x.t) = FV(t) \ {x}

FV(t1 t2) = FV(t1) [ FV(t2)

Page 38: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 38

SubstitutionSubstitution

• Works when free variables are replaced by term that does not clash:

[x z. z w] ( y.x) = ( y. z. z w)

• However, problem if there is name capture/clash:

[x z. z w] ( x.x) ( x. z. z w)

[x z. z w] ( w.x) ( w. z. z w)

Page 39: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 39

Formal Defn of SubstitutionFormal 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.t if y=x

[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

Page 40: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 40

Syntax of Lambda CalculusSyntax of Lambda Calculus

• Term:t ::= terms

x variable x.t abstractiont t application

• Value:v ::= value

x variable x.t abstraction value

Page 41: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 41

Call-by-Value SemanticsCall-by-Value Semantics

( x.t) v ! [x v] t (E-AppAbs)

t1 ! t’1

t1 t2 ! t’1 t2

(E-App1)

t2 ! t’2

v1 t2 ! v1 t’2

(E-App2)

premise

conclusion

Page 42: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 42

Call-by-Name SemanticsCall-by-Name Semantics

( x.t) t2 ! [x t2] t (E-AppAbs)

t1 ! t’1

t1 t2 ! t’1 t2

(E-App1)

Page 43: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 43

Getting StuckGetting Stuck

• Evaluation can get stuck. (Note that only values are -abstraction)

e.g. (x y)

• In extended lambda calculus, evaluation can also get stuck due to the absence of certain primitive rules.

( x. succ x) true ! succ true !

Page 44: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 44

Boolean-Enriched Lambda CalculusBoolean-Enriched Lambda Calculus• Term:

t ::= termsx variable x.t abstractiont t applicationtrue constant truefalse constant falseif t then t else t conditional

• Value:v ::= value

x.t abstraction valuetrue true valuefalse false value

Page 45: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 45

Key IdeasKey Ideas

• Exact typing impossible.

if <long and tricky expr> then true else ( x.x)

• Need to introduce function type, but need argument and result types.

if true then ( x.true) else ( x.x)

Page 46: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 46

Simple TypesSimple Types

• The set of simple types over the type Bool is generated by the following grammar:

• T ::= typesBool type of booleansT ! T type of functions

• ! is right-associative:

T1 ! T2 ! T3 denotes T1 ! (T2 ! T3)

Page 47: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 47

Implicit or Explicit TypingImplicit or Explicit Typing

• Languages in which the programmer declares all types are called explicitly typed. Languages where a typechecker infers (almost) all types is called implicitly typed.

• Explicitly-typed languages places onus on programmer but are usually better documented. Also, compile-time analysis is simplified.

Page 48: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 48

Explicitly Typed Lambda CalculusExplicitly Typed Lambda Calculus• t ::= terms

… x : T.t abstraction …

• v ::= value x : T.t abstraction value

• T ::= typesBool type of booleansT ! T type of functions

Page 49: CS5205: Foundation in Programming Languages Lecture 1 : Overview

CS5205 Introduction 49

ExamplesExamples

true

x:Bool . x

( x:Bool . x) true

if false then ( x:Bool . True) else ( x:Bool . x)