1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes...

25
1 SML fn x => e e 1 e 2 0, 1, 2, ..., +, -, ... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables

Transcript of 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes...

Page 1: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

1

SML

fn x => e

e1 e2

0, 1, 2, ..., +, -, ...

true, false, if e then e else e

patterns

datatypes

exceptions

structures

functors

fun f x = evariables

Page 2: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

2

What lies in the core of SML?

fn x => e

e1 e2

0, 1, 2, ..., +, -, ...

true, false, if e then e else e

patterns

datatypes

exceptions

structures

functors

fun f x = evariables

?

Page 3: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

3

Candidates?• booleans and if/then/else• integers• lists• variables• functions and function applications• datatypes• patterns• structures• functors• ...

Page 4: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

4

Core of SML

fn x => ee1 e2

x

Page 5: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

5

calculus

Page 6: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

CSE-321 Programming Languages

-Calculus

POSTECH

March 21, 2007

박성우

Page 7: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

8

Outline• Abstract syntax of the -calculus• Operational semantics of the -calculus• Substitutions• Programming in the -calculus

Page 8: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

9

Syntax for a Programming Language

Concrete syntax• program =

string of characters• specifies rules for parsing.

– operator precedence– associativity– keywords, ...

1 + 2 * 3

1 + (2 * 3)

1 + (2 * (3))

Abstract syntax• abstracts away from details

of parsing.• focuses on the high-level

structure of programs.• suitable for studying the

semantics

Page 9: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

10

• x– variable– z, s, t, f, arg, accum, ...

• x. e– -abstraction– x = formal argument, e = body– ¼ fn x => e

• e1 e2

– application– left-associative (as in SML):

• e1 e2 e3 = (e1 e2 ) e3

• e1 e2 e3 e1 (e2 e3 )

Abstract Syntax of the -Calculus

Page 10: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

11

Examples

Page 11: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

12

Outline• Abstract syntax of the -calculus V• Operational semantics of the -calculus• Substitutions• Programming in the -calculus

Page 12: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

13

Semantics of Languages• Answers "what is the meaning of a given program?"

– SML has a formal semantics.– What about C?

• Three styles– denotational semantics– axiomatic semantics– operational semantics

• The 1990s saw the renaissance of operational semantics.

Page 13: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

14

Operational Semantics• Specifies how to transform a program into a value

via a sequence of operations

Program ValueP2

operation operation operationPnoperation...

let fun fac 1 = 1 | fac n = n * fac (n - 1)in fac 4end

24

Page 14: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

15

Operational Semantics of -Calculus

• Specifies how to transform an expression into a value via a sequence of reductions

Expr ValueE2

reduction reduction reductionEnreduction...

Page 15: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

16

Values and Reductions

Page 16: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

17

Reductions

redex = reducible expression

: -reduction

Page 17: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

19

_____ = Redex

Page 18: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

21

Reduction Not Unique

So we need a reduction strategy.

Page 19: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

22

Call-by-name Call-by-value

Page 20: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

23

Call-by-name Call-by-value

Page 21: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

25

Page 22: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

26

Call-by-name Call-by-value• Used in Haskell• Lazy or non-strict

functional languages• The implementation uses

call-by-need.

• Superb!

• Used in SML• Eager or strict

functional languages

• Superb!

(fn x => 0) <some horrible computation>

(fn x => 0) <non-terminating computation>

Page 23: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

27

Assignments• Assignment 2

– average 86.68– Be sure to take a look at the sample solution.

• Assignment 3– to be out by midnight tonight.

• ???– due on April 2– Start early! ( Start coding early!)

Page 24: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

28

Quiz 1• Next Monday

• Read Chapter 2 of Course Notes.– 'fill in the blank' problems (i.e., easy)

• 15 minutes– Please show up on time.

Otherwise you might miss the quiz!

Page 25: 1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.

29

Anonymous Feedback• To be set up sometime today (hopefully)

– http://pl.postech.ac.kr/~gla/feedback/

• I will appreciate your feedback on this course.– lectures– assignments– schedule– topics

• It is you who will improve this course!