CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back...

66
CSEP505: Programming Languages Lecture 5: continuations, types Dan Grossman Spring 2006

Transcript of CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back...

Page 1: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

CS

EP

505:

Pro

gram

min

g La

ngua

ges

Lect

ure

5: c

ontin

uatio

ns, t

ypes

Dan

Gro

ssm

anS

prin

g 20

06

Page 2: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an2

Rem

embe

r our

sym

bol-p

ile

Exp

ress

ions

:e::= x|λx.e|e e

Val

ues:

v::= λx.e

e1!λx

.e3

e2 !

v2e3

{v2/

x} !

v��

����

����

��[la

m]

���

����

����

����

����

����

����

��[a

pp]

λx.e

!λx

.e

e

1 e2

!v

Page 3: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an3

And

whe

re w

e w

ere

�G

o ba

ck to

mat

h m

etal

angu

age

�N

otes

on

conc

rete

syn

tax

(rel

ates

to C

aml)

�D

efin

e se

man

tics

with

infe

renc

e ru

les

�La

mbd

a en

codi

ngs

(sho

w o

ur la

ngua

ge is

mig

hty)

�D

efin

e su

bstit

utio

n pr

ecis

ely

�An

d re

visi

t fun

ctio

n eq

uiva

lenc

es�

Envi

ronm

ents

Smal

l-ste

p�

Def

ine

and

mot

ivat

e co

ntin

uatio

ns�

(ver

y fa

ncy

lang

uage

feat

ure)

Page 4: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an4

Sm

all-s

tep

CB

V

�Le

ft-to

-rig

ht s

mal

l-ste

p ju

dgm

ente

→e�

e1→

e1�

e2→

e2�

����

����

����

����

����

����

����

����

����

�e1

e2→

e1� e

2

v

e2→

ve2

x.e)

v →

e{v/

x}

�N

eed

an �o

uter

loop

� as

usua

l: e →

*e�

�*

mea

ns �0

or m

ore

step

s��

Don

�t us

ually

bot

her w

ritin

g ru

les,

but

they

�re e

asy:

e1→

e2

e

2→

* e3

����

����

����

����

����

����

����

����

e→

* e

e

1→

* e3

Page 5: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an5

In C

aml

type exp=

Vof string| Lof string*exp| Aof exp * exp

letsubste1_with e2_for s= …

let recinterp_onee=

matche with

V _-> failwith“interp_one”(*unbound var*)

|L _ -> failwith“interp_one”(*already done*)

|A(L(s1,e1),L(s2,e2)) -> subste1 L(s2,e2)s1

| A(L(s1,e1),e2) -> A(L(s1,e1),interp_one e2)

| A(e1,e2) -> A(interp_onee1, e2)

let recinterp_smalle=

matche with

V _-> failwith“interp_small”(*unbound var*)

|L _ -> e

| A(e1,e2) -> interp_small(interp_onee)

Page 6: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an6

Unr

ealis

tic, b

ut�

�C

an d

istin

guis

h in

finite

-loop

s fro

m s

tuck

pro

gram

s

�It�

s cl

oser

to a

con

text

ual s

eman

tics

that

can

def

ine

cont

inua

tions

�An

d ca

n be

mad

e ef

ficie

nt b

y �k

eepi

ng tr

ack

of w

here

yo

u ar

e� a

nd u

sing

env

ironm

ents

�Ba

sic

idea

firs

t in

the

SE

CD

mac

hine

[Lan

din

1960

]!�

Triv

ial t

o im

plem

ent i

n as

sem

bly

plus

mal

loc!

�Ev

en w

ith c

ontin

uatio

ns

Page 7: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an7

Red

ivis

ion

of la

bor

typeectxt=Hole

|Leftof ectxt* exp

|Rightof exp * ectxt(*exp a value*)

let recsplit e=

matche with

A(L(s1,e1),L(s2,e2)) -> (Hole,e)

| A(L(s1,e1),e2) -> let (ctx2,e3) =split e2in

(Right(L(s1,e1),ctx2), e3)

| A(e1,e2) -> let (ctx2,e3) =split e1in

(Left(ctx2,e2), e3)

|_ ->failwith“bad argsto split”

let recfill (ctx,e) = (* plug the hole *)

matchctxwith

Hole

-> e

|Left(ctx2,e2) -> A(fill(ctx2,e), e2)

|Right(e2,ctx2) -> A(e2, fill (ctx2,e))

Page 8: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an8

So

wha

t?

�H

aven

�t do

ne m

uch

yet: e = fill(splite)

�Bu

t we

can

writ

e in

terp

_sm

allw

ith th

em�

Show

s a

step

has

thre

e pa

rts: s

plit,

sub

st, f

ill

let recinterp_smalle=

matche with

V _-> failwith“interp_small”(*unbound var*)

|L _ -> e

| _ ->

matchsplit e with

(ctx, A(L(s3,e3),v))->

interp_small(fill(ctx, subste3 v s3))

| _-> failwith“bad split”

Page 9: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an9

Aga

in, s

o w

hat?

�W

ell,

now

we

�hav

e ou

r han

ds� o

n a

cont

ext

�C

ould

sav

e an

d re

stor

e th

em

�(li

ke h

w2

with

hea

ps, b

ut th

is is

the

cont

rols

tack

)�

It�s

easy

giv

en th

is s

eman

tics!

�Su

ffici

ent f

or:

�Ex

cept

ions

�C

oope

rativ

e th

read

s�

Cor

outin

es�

�Tim

e tra

vel�

with

sta

cks

Page 10: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an10

Lang

uage

w/ c

ontin

uatio

ns

�N

ow 2

kin

ds o

f val

ues,

but

use

L fo

r bot

h�

Cou

ld in

stea

d ha

ve 2

kin

ds o

f app

licat

ion

+ er

rors

�N

ew k

ind

stor

es a

con

text

(tha

t can

be

rest

ored

)�

Letc

cge

ts th

e cu

rrent

con

text

type exp= (* change: 2 kinds of L + Letcc*)

Vof string| Lof string*body| Aof exp * exp

|Letccofstring * exp

andbody=Expofexp |Ctxtofectxt

andectxt=Hole(* no change *)

|Leftof ectxt* exp

|Rightof exp * ectxt

Page 11: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an11

Spl

it w

ith L

etcc

�O

ld: a

ctiv

e ex

pres

sion

(thi

ng in

the

hole

) alw

ays

som

eA(L(s1,e1),L(s2,e2))

�N

ew: c

ould

als

o be

som

e Letcc(s1,e1)

let recsplit e= (* change: one new case *)

matche with

Letcc(s1,e1)-> (Hole,e) (* new *)

|A(L(s1,e1),L(s2,e2)) -> (Hole,e)

| A(L(s1,e1),e2) -> let (ctx2,e3) =split e2in

(Right(L(s1,e1),ctx2), e3)

| A(e1,e2) -> let (ctx2,e3) =split e1in

(Left(ctx2,e2), e3)

|_ ->failwith“bad argsto split”

let recfill (ctx,e) = … (* no change *)

Page 12: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an12

All

the

actio

n•Letcc

beco

mes

an

L th

at �g

rabs

the

curre

nt c

onte

xt�

•A

whe

re b

ody

is a

Ctxt

�igno

res

curr

ent c

onte

xt�

let recinterp_smalle=

matche with

V _-> failwith“interp_small”(*unbound var*)

|L _ -> e

| _ -> matchsplit e with

(ctx, A(L(s3,Exp e3),v))->

interp_small(fill(ctx, subste3 v s3))

|(ctx, Letcc(s3,e3)) ->

interp_small(fill(ctx,

subste3 (L("",Ctxtctx)) s3))(*woah!!!*)

|(ctx, A(L(s3,Ctxt c3),v)) ->

interp_small(fill(c3, v)) (*woah!!!*)

| _-> failwith“bad split”

Page 13: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an13

Exa

mpl

es

�C

ontin

uatio

ns fo

r exc

eptio

ns is

�eas

y��

Letc

cfo

r try

, App

ly fo

r rai

se�

Cor

outin

esca

n yi

eld

to e

ach

othe

r (ex

ampl

e: C

GI!)

�Pa

ss a

roun

d a

yiel

d fu

nctio

n th

at ta

kes

an

argu

men

t ��h

ow to

rest

art m

e��

Body

of y

ield

app

lies

the

�old

how

to re

star

t me�

pa

ssin

g th

e �n

ew h

ow to

rest

art m

e��

Can

gen

eral

ize

to c

oope

rativ

e th

read

-sch

edul

ing

�W

ith m

utat

ion

can

real

ly d

o st

rang

e st

uff

�Th

e �g

oto

of fu

nctio

nal p

rogr

amm

ing�

Page 14: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an14

A lo

wer

-leve

l vie

w

�If

you�

re c

onfu

sed,

thin

k ca

ll-st

acks

�W

hat i

f YFL

had

thes

e op

erat

ions

:�

Stor

e cu

rrent

sta

ck in

x (c

f. Le

tcc)

�R

epla

ce c

urre

nt s

tack

with

sta

ck in

x�

You

need

to �f

ill th

e st

ack�

s ho

le� w

ith s

omet

hing

di

ffere

nt o

r you

�ll h

ave

an in

finite

loop

�C

ompi

ling

Letc

c�

Can

act

ually

cop

y st

acks

(exp

ensi

ve)

�O

r can

avo

id s

tack

s (p

ut fr

ames

in h

eap)

�Ju

st s

hare

and

rely

on

garb

age

colle

ctio

n

Page 15: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an15

Whe

re a

re w

e

Fini

shed

maj

or p

arts

of t

he c

ours

e�

Func

tiona

l pro

gram

min

g (o

ngoi

ng)

�IM

P, lo

ops,

mod

elin

g m

utat

ion

�La

mbd

a-ca

lcul

us, m

odel

ing

func

tions

�Fo

rmal

sem

antic

s�

Con

text

s, c

ontin

uatio

nsM

oral

? P

reci

se d

efin

ition

s of

rich

lang

uage

s is

diff

icul

t bu

t ele

gant

Maj

or n

ew to

pic:

Typ

es!

�C

ontin

ue u

sing

lam

bda-

calc

ulus

as

our m

odel

Page 16: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an16

Type

s In

tro

Naï

ve th

ough

t: M

ore

pow

erfu

l PL

is b

ette

r�

Be T

urin

g C

ompl

ete

�H

ave

real

ly fl

exib

le th

ings

(lam

bda,

con

tinua

tions

, �)

�H

ave

conv

enie

nces

to k

eep

prog

ram

s sh

ort

By

this

met

ric, t

ypes

are

a s

tep

back

war

d�

Who

le p

oint

is to

allo

w fe

wer

pro

gram

s�

A �fi

lter�

bet

wee

n pa

rse

and

com

pile

/inte

rp�

Why

a g

reat

idea

?

Page 17: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an17

Why

type

s

1.C

atch

�stu

pid

mis

take

s� e

arly

�3

+ �h

ello

��

prin

t_st

ring

(Stri

ng.a

ppen

d�h

i�)�

But m

ay b

e to

o ea

rly (c

ode

not u

sed,

�)

2.P

reve

nt g

ettin

g st

uck

/ goi

ng h

ayw

ire�

Kno

wev

alua

tion

cann

ot e

verg

et to

the

poin

t w

here

the

next

ste

p �m

akes

no

sens

e��

Alte

rnat

e: la

ngua

ge m

akes

eve

ryth

ing

mak

e se

nse

(e.g

., C

lass

Cas

tExc

eptio

n)�

Alte

rnat

e: la

ngua

ge c

an d

o w

hate

ver ?

!

Page 18: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an18

Dig

ress

ion/

serm

on

Uns

afe

lang

uage

s ha

ve o

pera

tions

whe

re u

nder

som

e si

tuat

ions

the

impl

emen

tatio

n �c

an d

o an

ythi

ng�

IMP

with

uns

afe

C a

rray

s ha

s th

is ru

le (a

ny H

�;s�!)

:

Abs

tract

ion,

mod

ular

ity, e

ncap

sula

tion

are

impo

ssib

le

beca

use

one

bad

line

can

have

arb

itrar

y gl

obal

effe

ctA

n en

gine

erin

g di

sast

er (c

f. ci

vil e

ngin

eerin

g)

H;e

1!

{v1,

�,v

n}

H;e

2!

ii>

n��

����

����

����

����

����

����

����

����

���

H; e

1[i]=

e2!

H�;s

Page 19: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an19

Why

type

s, c

ontin

ued

3.E

nfor

ce a

stro

ng in

terfa

ce (v

ia a

n ab

stra

ct ty

pe)

�C

lient

s ca

n�t b

reak

inva

riant

s�

Clie

nts

can�

t ass

ume

an im

plem

enta

tion

�As

sum

es s

afet

y4.

Allo

w fa

ster

impl

emen

tatio

ns�

Com

pile

r kno

ws

run-

time

type

-che

cks

unne

eded

�C

ompi

ler k

now

s pr

ogra

m c

anno

t det

ect

spec

ializ

atio

n/op

timiz

atio

n5.

Sta

tic o

verlo

adin

g (e

.g.,

with

+)

�N

ot s

o in

tere

stin

g�

Late

-bin

ding

ver

y in

tere

stin

g (c

ome

back

to th

is)

Page 20: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an20

Why

type

s, c

ontin

ued

6.N

ovel

use

s�

A po

wer

ful w

ay to

thin

k ab

out m

any

cons

erva

tive

prog

ram

ana

lyse

s/re

stric

tions

�Ex

ampl

es: r

ace-

cond

ition

s, m

anua

l mem

ory

man

agem

ent,

secu

rity

leak

s, �

�I d

o so

me

of th

is; �

a ty

pes

pers

on�

We�

ll fo

cus

on s

afet

y an

d st

rong

inte

rface

s�

And

late

r dis

cuss

the

�sta

tic ty

pes

or n

ot� d

ebat

e (it

�s re

ally

a c

ontin

uum

)

Page 21: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an21

Our

pla

n

�Si

mpl

y-ty

ped

Lam

bda-

Cal

culu

s�

Safe

ty =

(pre

serv

atio

n +

prog

ress

)�

Exte

nsio

ns (p

airs

, dat

atyp

es, r

ecur

sion

, etc

.)�

Dig

ress

ion:

sta

tic v

s. d

ynam

ic ty

ping

�D

igre

ssio

n: C

urry

-How

ard

Isom

orph

ism

�Su

btyp

ing

�Ty

pe V

aria

bles

: �

Gen

eric

s (∀

), A

bstra

ct ty

pes

(∃),

Rec

ursi

ve ty

pes

�Ty

pe in

fere

nce

Page 22: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an22

Add

ing

inte

gers

Add

ing

inte

gers

to th

e la

mbd

a-ca

lcul

us:

Exp

ress

ions

:e

::= x

|λx.

e|e

e |

cV

alue

s:

v::=

λx.

e |c

Cou

ld a

dd +

and

oth

er p

rimiti

ves

or ju

st p

aram

eter

ize

�pro

gram

s� b

y th

em: λ

plus

.λm

inus

.� e

�Li

ke P

erva

sive

sin

Cam

l�

A gr

eat i

dea

for k

eepi

ng la

ngua

ge d

efin

ition

s sm

all!

Page 23: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an23

Stu

ck

�Ke

y is

sue:

can

a p

rogr

am e

�get

stu

ck� (

smal

l-ste

p):

�e →

* e1

�e1

is n

ot a

val

ue�

Ther

e is

no

e2 s

uch

that

e1 →

e2�

�Wha

t is

stuc

k� d

epen

ds o

n th

e se

man

tics:

e1→

e1�

e2→

e2�

����

����

����

����

����

����

����

����

����

�e1

e2→

e1� e

2

v

e2→

ve2

x.e)

v →

e{v/

x}

Page 24: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an24

STL

C S

tuck

•S::=c e|x e|(λx.e) x |S e |(λx.e) S

�It�

s no

t nor

mal

to d

efin

e th

ese

expl

icitl

y, b

ut a

gre

at

way

to th

ink

abou

t it.

�M

ost p

eopl

e do

n�t r

ealiz

e �s

afet

y�de

pend

s on

the

sem

antic

s:

�W

e ca

n ad

d �c

heat

�rul

es to

�avo

id�b

eing

stu

ck.

Page 25: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an25

Sou

nd a

nd c

ompl

ete

�D

efin

ition

: A ty

pe s

yste

m is

sou

nd if

it n

ever

acc

epts

a

prog

ram

that

can

get

stu

ck

�D

efin

ition

: A ty

pe s

yste

m is

com

plet

e if

it al

way

s ac

cept

s a

prog

ram

that

can

not g

et s

tuck

�So

undn

ess

and

com

plet

enes

s ar

e de

sira

ble

�Bu

t im

poss

ible

(und

ecid

able

) for

lam

bda-

calc

ulus

�If

e ha

s no

con

stan

ts o

r fre

e va

riabl

es th

en e

(3 4

)ge

ts s

tuck

iffe

term

inat

es�

As is

any

non

-triv

ial p

rope

rty fo

r a T

urin

g-co

mpl

ete

PL

Page 26: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an26

Wha

t to

do

�O

ld c

oncl

usio

n: �s

trong

type

s fo

r wea

k m

inds

��

Nee

d an

unc

heck

ed c

ast (

a ba

ck-d

oor)

�M

oder

n co

nclu

sion

: �

Mak

e fa

lse

posi

tives

rare

and

fals

e ne

gativ

es

impo

ssib

le (b

e so

und

and

expr

essi

ve)

�M

ake

wor

karo

unds

reas

onab

le�

Just

ifica

tion:

fals

e ne

gativ

es to

o ex

pens

ive,

hav

e co

mpi

le-ti

me

reso

urce

s fo

r �fa

ncy�

type

-che

ckin

g

�O

kay,

let�s

act

ually

try

to d

o it�

Page 27: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an27

Wro

ng a

ttem

pt

τ::=int|function

A ju

dgm

ent: ├e :τ

(for w

hich

we

hope

ther

e�s

an e

ffici

ent a

lgor

ithm

)

����

����

����

����

����

����

����

��├c :int

├(λx.e):function

├e1

: function

├e2: int

����

����

����

����

����

����

����

��├e1 e2: int

Page 28: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an28

So

very

wro

ng

1.U

nsou

nd: (λx.y)

32.

Dis

allo

ws

func

tion

argu

men

ts: (λx.

x 3)(λy.y)

3.Ty

pes

not p

rese

rved

: (λx.(λy.y)) 3

�R

esul

t is

not a

n in

tege

r

����

����

����

����

����

����

����

��├c :int

├(λx.e):function

├e1: function

├e2: int

����

����

����

����

����

����

����

��├e1 e2: int

Page 29: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an29

Get

ting

it rig

ht

1.N

eed

to ty

pe-c

heck

func

tion

bodi

es, w

hich

hav

e fre

e va

riabl

es2.

Nee

d to

dis

tingu

ish

func

tions

acc

ordi

ng to

arg

umen

t an

d re

sult

type

s

For (

1):

Γ::=. | Γ,

x : τ

and Γ├e :τ

�A

type

-che

ckin

g en

viro

nmen

t (ca

lled

a co

ntex

t)

For (

2):

τ::=int| τ→

τ�

Arro

w is

par

t of t

he (t

ype)

lang

uage

(not

met

a)�

An in

finite

num

ber o

f typ

es�

Just

like

Cam

l

Page 30: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an30

Exa

mpl

es a

nd s

ynta

x

�Ex

ampl

es o

f typ

esin

t→in

t(in

t→in

t) →

int

int→

(int→

int)

�C

oncr

etel

y →

is ri

ght-a

ssoc

iativ

e, i.

e.,

�i.e

., τ1→

τ2→

τ3is

τ1→

(τ2→

τ3)

�Ju

st li

ke C

aml

Page 31: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an31

STL

C in

one

slid

eE

xpre

ssio

ns:e::= x|λx.e|e e |c

Val

ues:

v::= λx.e |e e

Type

s:

τ::=int

| τ→

τC

onte

xts:

Γ::=. | Γ,

x : τ

e1→

e1�

e2→

e2�

����

����

����

����

����

����

����

����

����

�e1

e2→

e1� e

2

v

e2→

ve2

x.e)

v →

e{v/

x}

����

����

���

����

����

����

Γ├c :int

Γ├x : Γ(x)

Γ,x:τ1├e:τ2

Γ├e1:τ1→

τ2 Γ├e2:τ1

����

����

����

����

����

����

����

����

����

����

��Γ├(λx.e):τ1→

τ2 Γ├e1 e2:τ2

Page 32: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an32

Rul

e-by

-rul

e

�C

onst

ant r

ule:

con

text

irre

leva

nt�

Varia

ble

rule

: loo

kup

(no

inst

antia

tion

if x

not i

n Γ)

�Ap

plic

atio

n ru

le: �

yeah

, tha

t mak

es s

ense

��

Func

tion

rule

the

inte

rest

ing

one�

����

����

���

����

����

����

Γ├c :int

Γ├x : Γ(x)

Γ,x:τ1├e:τ2

Γ├e1:τ1→

τ2 Γ├e2:τ1

����

����

����

����

����

����

����

����

����

����

��Γ├(λx.e):τ1→

τ2 Γ├e1 e2:τ2

Page 33: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an33

The

func

tion

rule

�W

here

did

τ1co

me

from

?�

Our

rule

�inf

erre

d� o

r �gu

esse

d� it

�To

be

synt

ax-d

irect

ed, c

hang

e λx.e

toλx: τ.

e

and

use

that

τ�

If w

e th

ink

of Γ

as a

par

tial f

unct

ion,

we

need

x n

ot

alre

ady

in it

(alp

ha-c

onve

rsio

n al

low

s)

Γ,x:τ1├e:τ2

����

����

����

����

��Γ├(λx.e):τ1→

τ2

Page 34: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an34

Our

pla

n

�Si

mpl

y-ty

ped

Lam

bda-

Cal

culu

s�

Safe

ty =

(pre

serv

atio

n +

prog

ress

)�

Exte

nsio

ns (p

airs

, dat

atyp

es, r

ecur

sion

, etc

.)�

Dig

ress

ion:

sta

tic v

s. d

ynam

ic ty

ping

�D

igre

ssio

n: C

urry

-How

ard

Isom

orph

ism

�Su

btyp

ing

�Ty

pe V

aria

bles

: �

Gen

eric

s (∀

), A

bstra

ct ty

pes

(∃),

Rec

ursi

ve ty

pes

�Ty

pe in

fere

nce

Page 35: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an35

Is it

�rig

ht�?

�C

an d

efin

e an

y ty

pe s

yste

m w

e w

ant

�W

hat w

e de

fined

is s

ound

and

inco

mpl

ete

�C

an p

rove

inco

mpl

ete

with

one

exa

mpl

e�

Ever

y va

riabl

e ha

s ex

actly

one

sim

ple

type

�Ex

ampl

e (d

oesn

�t ge

t stu

ck, d

oesn

�t ty

pech

eck)

(λx. (x(λy.y)) (x 3)) (λz.z)

Page 36: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an36

Sou

nd

�St

atem

ent o

f sou

ndne

ss th

eore

m:

If .├

e:τ

and

e →

*e2,

then

e2

is a

val

ue o

r the

re

exis

ts a

n e3

suc

h th

at e

2 →

e3�

Proo

f is

toug

h �

Mus

t hol

d fo

r all

e an

d an

y nu

mbe

r of s

teps

�Bu

t eas

y if

thes

e tw

o th

eore

ms

hold

1.P

rogr

ess:

If

.├e:τ

then

e is

a v

alue

or t

here

ex

ists

an

e�su

ch th

at e→

e�2.

Pre

serv

atio

n: If

.├e:τ

and

e→e�

then

.├e:τ

Page 37: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an37

Let�s

pro

ve it

Pro

ve: I

f .├

e:τ

and

e →

*e2,

then

e2

is a

val

ue o

r ∃e

3 su

ch th

at e

2 →

e3, a

ssum

ing:

1.If

.├e:τ

then

e is

a v

alue

or ∃

e�su

ch th

at e→

e�2.

If .├

e:τ

and

e→e�

then

.├e:τ

Pro

ve s

omet

hing

stro

nger

: Als

o sh

ow .├e2:τ

Pro

of: B

y in

duct

ion

on n

whe

re e

→*e

2 in

n s

teps

�C

ase

n=0:

imm

edia

te fr

om p

rogr

ess

(e=e

2)�

Cas

e n>

0: th

en ∃

e2�s

uch

that

Page 38: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an38

Wha

t�s th

e po

int

�Pr

ogre

ss is

wha

t we

care

abo

ut�

But P

rese

rvat

ion

is th

e in

varia

ntth

at h

olds

no

long

er

how

long

we

have

bee

n ru

nnin

g�

(Pro

gres

s an

d P

rese

rvat

ion)

impl

ies

Sou

ndne

ss

�Th

is is

a v

ery

gene

ral/p

ower

ful r

ecip

e fo

r sho

win

g yo

u �d

on�t

get t

o a

bad

plac

e��

If in

varia

nt h

olds

, you

�re in

a g

ood

plac

e (p

rogr

ess)

and

you

go

to a

goo

d pl

ace

(pre

serv

atio

n)

�D

etai

ls o

n ne

xt 2

slid

es le

ss im

porta

nt�

Page 39: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an39

Forg

et a

cou

ple

thin

gs?

Pro

gres

s: If

.├

e:τ

then

e is

a v

alue

or t

here

exi

sts

an e

�suc

h th

at e→

e�

Pro

of: I

nduc

tion

on (h

eigh

t of)

deriv

atio

n tre

e fo

r .├e:τ

Rou

gh id

ea:

�Tr

ivia

l unl

ess

e is

an

appl

icat

ion

�Fo

r e =

e1

e2,

�If

left

or ri

ght n

ot a

val

ue, i

nduc

tion

�If

both

val

ues,

e1

mus

t be

a la

mbd

a�

Page 40: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an40

Forg

et a

cou

ple

thin

gs?

Pre

serv

atio

n: If

.├e:τ

and

e→e�

then

.├e:τ

Als

o by

indu

ctio

n on

ass

umed

typi

ng d

eriv

atio

n.

The

troub

le is

whe

n e→

e�in

volv

es s

ubst

itutio

n �

requ

ires

anot

her t

heor

em

Sub

stitu

tion:

If Γ

,x:τ1├e:τa

ndΓ├e1:τ1

, the

nΓ├e{e1/x}:τ

Page 41: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an41

Our

pla

n

�Si

mpl

y-ty

ped

Lam

bda-

Cal

culu

s�

Safe

ty =

(pre

serv

atio

n +

prog

ress

)�

Exte

nsio

ns (p

airs

, dat

atyp

es, r

ecur

sion

, etc

.)�

Dig

ress

ion:

sta

tic v

s. d

ynam

ic ty

ping

�D

igre

ssio

n: C

urry

-How

ard

Isom

orph

ism

�Su

btyp

ing

�Ty

pe V

aria

bles

: �

Gen

eric

s (∀

), A

bstra

ct ty

pes

(∃),

Rec

ursi

ve ty

pes

�Ty

pe in

fere

nce

Page 42: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an42

Hav

ing

laid

the

grou

ndw

ork�

�So

far:

�O

ur la

ngua

ge (S

TLC

) is

tiny

�W

e us

ed h

eavy

-dut

y to

ols

to d

efin

e it

�N

ow:

�Ad

d lo

ts o

f thi

ngs

quic

kly

�Be

caus

e ou

r too

ls a

re a

ll w

e ne

ed

�An

d ea

ch a

dditi

on w

ill ha

ve th

e sa

me

form

Page 43: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an43

A m

etho

d to

our

mad

ness

�Th

e pl

an�

Add

synt

ax�

Add

new

sem

antic

rule

s (in

clud

ing

subs

titut

ion)

�Ad

d ne

w ty

ping

rule

s

�If

our a

dditi

on e

xten

ds th

e sy

ntax

of t

ypes

, the

n�

We

will

have

new

val

ues

(of t

hat t

ype)

�An

d w

ays

to m

ake

the

new

val

ues

�(c

alle

d in

trodu

ctio

n fo

rms)

�An

d w

ays

to u

se th

e ne

w v

alue

s�

(cal

led

elim

inat

ion

form

s)

Page 44: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an44

Let b

indi

ngs

(CB

V)

e ::=

� |

letx

=e1

ine2

(no

new

val

ues

or ty

pes)

e1→

e1�

����

����

����

����

����

����

���

letx

=e1

ine2→

letx

=e1

�in

e2��

����

����

����

����

�le

tx =

v in

e2→

e2{v

/x}

Γ├e1:τ1

Γ,x:τ1├e2:τ2

����

����

����

����

����

����

��Γ├

letx

=e1

ine2

: τ2

Page 45: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an45

Let a

s su

gar?

Let i

s ac

tual

ly s

o m

uch

like

lam

bda,

we

coul

d us

e 2

othe

r diff

eren

t but

equ

ival

ent s

eman

tics

2.le

t x=e

1 in

e2

is s

ugar

(a d

iffer

ent c

oncr

ete

way

to

writ

e th

e sa

me

abst

ract

syn

tax)

for (λx

.e2)

e1

3.In

stea

d of

sem

antic

rule

s on

last

slid

e, u

se ju

st��

����

����

����

����

����

����

�le

tx =

e1 in

e2→

(λx.

e2) e

1

Not

e: In

Cam

l, le

t is

nots

ugar

for a

pplic

atio

n be

caus

e le

t is

type

-che

cked

diff

eren

tly (t

ype

varia

bles

)

Page 46: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an46

Boo

lean

s

e ::

= �

|tru

|fls

|e ?

e :e

v ::

= �

|tru

|fls

τ::=

� |

bool

e1→

e1�

����

����

����

����

����

����

����

����

�e1

?e2

:e3→

e1� ?

e2 :

e3

Γ├

tru:bool

����

����

����

����

����

����

�tru

?e2

:e3→

e2

Γ├

fls:bool

����

����

����

��Γ├

e1:boolΓ├

e2:τΓ├

e3:τ

fls?

e2 :

e3→

e3

����

����

����

����

����

����

���

Γ├e1

?e2

:e3

: τ

Page 47: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an47

Cam

l? L

arge

-ste

p?

�In

hom

ewor

k 3,

you

�ll ad

d co

nditi

onal

s, p

airs

, etc

. to

our e

nviro

nmen

t-bas

ed la

rge-

step

inte

rpre

ter

�C

ompa

red

to la

st s

lide

�D

iffer

ent m

eta-

lang

uage

(cas

es re

arra

nged

)�

Larg

e-st

ep in

stea

d of

sm

all

�If

test

s an

inte

ger f

or 0

(lik

e C

)�

Larg

e-st

ep b

oole

ans

with

infe

renc

e ru

les

e1!

true2

!v

e1!

flse3

!v

����

����

����

����

���

����

����

����

���

e1 ?

e2 :

e3!

v

e

1 ?

e2 :

e3!

v��

����

����

����

��tru

!tru

fls!

fls

Page 48: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an48

Pai

rs (C

BV

, lef

t-to-

right

)

e ::

= �

|(e

,e)|

e.1

|e.2

v ::

= �

|(v

,v)

τ::=

� | τ*τ

e1→

e1�

e

2→

e2�

e→

e�

e→

e���

����

����

����

����

����

����

����

����

����

����

(e1,

e2)→

(e1�

,e2)

(v

,e2)→

(v,e

2�)

e.1→

e�.1

e

.2→

e�.2

����

����

����

����

����

����

(v1,

v2).1→

v1

(v

1,v2

).2→

v2

Γ├e1:τ1 Γ├

e2:τ2 Γ├

e:τ1*τ2 Γ├

e:τ1*τ2

����

����

����

����

���

����

����

���

����

����

���

Γ├(e

1,e2

): τ1

*τ2 Γ├

e.1:τ1 Γ├

e.2:τ2

Page 49: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an49

Bes

t gue

ss o

f whe

re le

ctur

e 5

will

end

Page 50: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an50

Tow

ard

Sum

s

�N

ext a

dditi

on: s

ums

(muc

h lik

e M

L da

taty

pes)

�In

form

al re

view

of M

L da

taty

peba

sics

type t = A of t1 | B of t2 | C of t3

�In

trodu

ctio

n fo

rms:

con

stru

ctor

-app

lied-

to-e

xp�

Elim

inat

ion

form

s: m

atch

e1

with

pat

->

exp

��

Typi

ng: I

f e h

as ty

pe t1

, the

n A

e h

as ty

pe t

Page 51: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an51

Unl

ike

ML,

par

t 1

�M

L da

taty

pes

do a

lot a

t onc

e�

Allo

w re

curs

ive

type

s�

Intro

duce

a n

ew n

ame

for a

type

�Al

low

type

par

amet

ers

�Al

low

fanc

y pa

ttern

mat

chin

g�

Wha

t we

do w

ill be

sim

pler

�Ad

d re

curs

ive

type

s se

para

tely

late

r�

Avoi

d na

mes

(a b

it si

mpl

er in

theo

ry)

�Av

oid

type

par

amet

ers

(for s

impl

icity

)�

Onl

y pa

ttern

s of

form

A x

(res

t is

suga

r)

Page 52: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an52

Unl

ike

ML,

par

t 2

�W

hat w

e ad

d w

ill a

lso

be d

iffer

ent

�O

nly

two

cons

truct

ors

A a

nd B

�Al

l sum

type

s us

e th

ese

cons

truct

ors

�So

A e

can

hav

e an

y su

m ty

pe a

llow

ed b

y e�

sty

pe�

No

need

to d

ecla

re s

um ty

pes

in a

dvan

ce�

Like

func

tions

, will

�gue

ss ty

pes�

in o

ur ru

les

�Th

is s

houl

d st

ill h

elp

expl

ain

wha

t dat

atyp

esar

e

�Af

ter f

orm

alis

m, w

ill c

ompa

re to

C u

nion

s an

d O

OP

Page 53: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an53

The

mat

h (w

ith ty

pe ru

les

to c

ome)

e ::

= �

|A

e |B

e |m

atch

e w

ithA

x ->

e |B

y ->

ev

::=

� |

Av

|Bv

τ::=

� | τ+τ

e→

e�e→

e�

e1→

e1�

����

����

���

����

���

����

����

����

����

����

����

���

A e→

A e�

B e →

B e�

mat

che1

with

Ax->

e2 |B

y ->

e3→

mat

che1

� with

Ax->

e2 |B

y ->

e3

����

����

����

����

����

����

����

����

����

����

mat

chA

v w

ithA

x->

e2 |

By ->

e3 →

e2{v

/x}

����

����

����

����

����

����

����

����

����

����

mat

chB

v w

ithA

x->

e2 |

By ->

e3 →

e3{y

/x}

Page 54: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an54

Low

-leve

l vie

w

You

can

thin

k of

dat

atyp

eva

lues

as

�pai

rs�

�Fi

rst c

ompo

nent

is A

or B

(or 0

or 1

if y

ou p

refe

r)�

Seco

nd c

ompo

nent

is �t

he d

ata�

�e2

or e

3 ev

alua

ted

with

�the

dat

a� in

pla

ce o

f the

va

riabl

e�

This

is a

ll lik

e C

amla

s in

lect

ure

1�

Exam

ple

valu

es o

f typ

e in

t+ (i

nt->

int):

017

1

λx. λ

y.x+

y[(�

y�,6

)]

Page 55: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an55

Typi

ng ru

les

�Ke

y id

ea fo

r dat

atyp

eex

p: �o

ther

can

be

anyt

hing

��

Key

idea

for m

atch

es: �

bran

ches

nee

d sa

me

type

��

Just

like

con

ditio

nals

Γ├e:τ1

Γ├e:τ2

����

����

����

����

����

����

���

Γ├A

e: τ1+τ2 Γ├

B e

: τ1+τ2

Γ├e1

: τ1+τ2 Γ,

x:τ1├

e2: τ

Γ,x:τ2├

e3: τ

����

����

����

����

����

����

����

����

����

����

Γ├m

atch

e1 w

ithA

x->

e2 |

By ->

e3 : τ

Page 56: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an56

Com

pare

to p

airs

, par

t 1

��p

airs

and

sum

s� is

a b

ig id

ea�

Lang

uage

s sh

ould

hav

e bo

th (i

n so

me

form

)�

Som

ehow

pai

rs c

ome

acro

ss a

s si

mpl

er, b

ut

they

�re re

ally

�dua

l� (s

ee C

urry

-How

ard

soon

)�

Intro

duct

ion

form

s:

�pa

irs �n

eed

both

�, su

ms

�nee

d on

e�

Γ├e1:τ1 Γ├

e2:τ2

Γ├e:τ1

Γ├e:τ2

����

����

����

����

����

����

����

����

����

����

���

Γ├(e

1,e2

): τ1

*τ2

Γ├A

e: τ1+τ2 Γ├

B e

: τ1+τ2

Page 57: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an57

Com

pare

to p

airs

, par

t 2

�El

imin

atio

n fo

rms

�Pa

irs g

et e

ither

, sum

s m

ust b

e pr

epar

ed fo

r eith

er

Γ├e:τ1*τ2 Γ├

e:τ1*τ2

����

����

���

����

����

���

Γ├e.1:τ1 Γ├

e.2:τ2

Γ├e1

: τ1+τ2

Γ,

x:τ1├

e2: τ

Γ,x:τ2├

e3: τ

����

����

����

����

����

����

����

����

����

����

Γ├m

atch

e1 w

ithA

x->

e2 |

By ->

e3 : τ

Page 58: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an58

Livi

ng w

ith ju

st p

airs

�If

stub

born

you

can

cra

m s

ums

into

pai

rs (d

on�t!

)�

Rou

nd-p

eg, s

quar

e-ho

le�

Less

effi

cien

t (du

mm

y va

lues

)�

Flat

tene

d pa

irs d

on�t

chan

ge th

at�

Mor

e er

ror-

pron

e (m

ay u

se d

umm

y va

lues

)�

Exam

ple:

int+ (int-> int)

beco

mes

int* (int

* (int

-> int))

1λx

. λy.

x+y

[(�y�

,6]

0

0λx

. λy.

x+y

[ ]17

Page 59: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an59

Sum

s in

oth

er g

uise

s

typet =A oft1 |B oft2 |C oft3

match

e with

A x ->

Mee

ts C

:structt {

enum

{A,B,C}

tag;

union{t1 a;t2 b;t3 c;}data;

};

… switch(e->tag){caseA:t1 x=e->data.a;…

�N

o st

atic

che

ckin

g th

at ta

g is

obe

yed

�As

fat a

s th

e fa

ttest

var

iant

(avo

idab

le w

ith c

asts

)�

Mut

atio

n bi

tes

agai

n!�

Sham

eles

s pl

ug: C

yclo

neha

s M

L-st

yle

data

type

s

Page 60: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an60

Sum

s in

oth

er g

uise

s

typet =A oft1 |B oft2 |C oft3

match

e with

A x ->

Mee

ts J

ava:

abstract classt {abstractObject m();}

classA extendst {t1 x;Object m(){…}}

classB extendst {t2 x;Object m(){…}}

classC extendst {t3 x;Object m(){…}}

… e.m() …

�A

new

met

hod

for e

ach

mat

ch e

xpre

ssio

n�

Supp

orts

orth

ogon

al fo

rms

of e

xten

sibi

lity

(will

com

e ba

ck to

this

)

Page 61: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an61

Whe

re a

re w

e

�H

ave

adde

d le

t, bo

ols,

pai

rs, s

ums

�C

ould

hav

e do

ne s

tring

, flo

ats,

reco

rds,

��

Amaz

ing

fact

:�

Even

with

eve

ryth

ing

we

have

add

ed s

o fa

r, ev

ery

prog

ram

term

inat

es!

�I.e

., if

.├e:τ

then

ther

e ex

ists

a v

alue

vsu

ch

that

e →

* v�

Cor

olla

ry: O

ur e

ncod

ing

of fi

x w

on�t

type

-che

ck�

To re

gain

Tur

ing-

com

plet

enes

s, w

e ne

ed e

xplic

it su

ppor

t for

recu

rsio

n

Page 62: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an62

Rec

ursi

on

�W

e co

uld

add

�fix

e� (a

sk m

e if

you�

re c

urio

us),

but

mos

t peo

ple

find

�letre

cf x

e� m

ore

intu

itive

e ::

= �

|le

trec

f x e

v ::

= �

|le

trec

f x e

(no

new

type

s)�S

ubst

itute

arg

umen

t lik

e la

mbd

a &

who

le fu

nctio

n fo

r f�

����

����

����

����

����

����

����

����

��(le

trec

f x e

) v →

(e{v

/x})

{(le

trec

f x e

)/f}

Γ, f:τ1→

τ2,x

:τ1 ├e:τ2

����

����

����

����

����

���

Γ├le

trec

f x e

: τ1→

τ2

Page 63: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an63

Our

pla

n

�Si

mpl

y-ty

ped

Lam

bda-

Cal

culu

s�

Safe

ty =

(pre

serv

atio

n +

prog

ress

)�

Exte

nsio

ns (p

airs

, dat

atyp

es, r

ecur

sion

, etc

.)�

Dig

ress

ion:

sta

tic v

s. d

ynam

ic ty

ping

�D

igre

ssio

n: C

urry

-How

ard

Isom

orph

ism

�Su

btyp

ing

�Ty

pe V

aria

bles

: �

Gen

eric

s (∀

), A

bstra

ct ty

pes

(∃),

Rec

ursi

ve ty

pes

�Ty

pe in

fere

nce

Page 64: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an64

A c

oupl

e sl

ides

for c

onte

xt e

xam

ples

Page 65: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an65

Red

ivis

ion

of la

bor

typeectxt=Hole

|Leftof ectxt* exp

|Rightof exp * ectxt(*exp a value*)

let recsplit e=

matche with

A(L(s1,e1),L(s2,e2)) -> (Hole,e)

| A(L(s1,e1),e2) -> let (ctx2,e3) =split e2in

(Right(L(s1,e1),ctx2), e3)

| A(e1,e2) -> let (ctx2,e3) =split e1in

(Left(ctx2,e2), e3)

Page 66: CSEP505: Programming Languages Lecture 5: continuations, types€¦ · And where we were Ł Go back to math metalanguage Œ Notes on concrete syntax (relates to Caml) Œ Define semantics

18 A

pril

2006

CSE

P50

5 Sp

ring

2006

Dan

Gro

ssm

an66

Red

ivis

ion

of la

bor

typeectxt=Hole

|Leftof ectxt* exp

|Rightof exp * ectxt(*exp a value*)

let recsplit e=

matche with

A(L(s1,e1),L(s2,e2)) -> (Hole,e)

| A(L(s1,e1),e2) -> let (ctx2,e3) =split e2in

(Right(L(s1,e1),ctx2), e3)

| A(e1,e2) -> let (ctx2,e3) =split e1in

(Left(ctx2,e2), e3)