SIGPLwinterschool2012

Similar documents
slide2


ENE414 프로그래밍언어론강의노트 1 1 문법, 핵심구문나무, 인터프리터 한양대학교 ERICA캠퍼스컴퓨터공학과도경구 2012년 1학기 (version 0.35) 1 c David A. Schmidt, 도경구 (2012). 본문서는 Kansas State Univers

HW5 Exercise 1 (60pts) M interpreter with a simple type system M. M. M.., M (simple type system). M, M. M., M.

1

OCaml

PowerPoint 프레젠테이션

C++-¿Ïº®Çؼ³10Àå

03장.스택.key

프로그램을 학교 등지에서 조금이라도 배운 사람들을 위한 프로그래밍 노트 입니다. 저 역시 그 사람들 중 하나 입니다. 중고등학교 시절 학교 도서관, 새로 생긴 시립 도서관 등을 다니며 책을 보 고 정리하며 어느정도 독학으르 공부하긴 했지만, 자주 안하다 보면 금방 잊어



13주-14주proc.PDF


chap10.PDF

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

본 발명은 중공코어 프리캐스트 슬래브 및 그 시공방법에 관한 것으로, 자세하게는 중공코어로 형성된 프리캐스트 슬래브 에 온돌을 일체로 구성한 슬래브 구조 및 그 시공방법에 관한 것이다. 이를 위한 온돌 일체형 중공코어 프리캐스트 슬래브는, 공장에서 제작되는 중공코어 프

Microsoft PowerPoint - PL_03-04.pptx

Modern Javascript

歯처리.PDF

2힉년미술

K&R2 Reference Manual 번역본

Microsoft PowerPoint - semantics

2014밝고고운동요부르기-수정3

2005프로그램표지

특허청구의 범위 청구항 1 몸체(110)의 일측에는 테스트의 필요성에 따라 여타한 디젤 자동차(100)에서 분리시킨 상태의 분리형 커먼레일 인젝트(110)를 고정할 수 있는 분리형 인젝터 고정부(20)가 구비되고, 그 고정부(20)의 하측에는 분리형 커먼 레일 인젝터(

179

February


C# Programming Guide - Types

- 2 -

6 강남구 청담지구 청담동 46, 삼성동 52 일대 46,592-46,592 7 강남구 대치지구 대치동 922번지 일대 58,440-58,440 8 강남구 개포지구 개포동 157일대 20,070-20,070 9 강남구 개포지구중심 포이동 238 일대 25,070-25,

27집최종10.22

황룡사 복원 기본계획 Ⅵ. 사역 및 주변 정비계획 가. 사역주변 정비구상 문화유적지구 조성 1. 정비방향의 설정 황룡사 복원과 함께 주변 임해전지(안압지) 海殿址(雁鴨池)와 분황사 등의 문화유적과 네트워크로 연계되는 종합적 정비계획안을 수립한다. 주차장과 광장 등 주변

4. #include <stdio.h> #include <stdlib.h> int main() { functiona(); } void functiona() { printf("hihi\n"); } warning: conflicting types for functiona

제4장 기본 의미구조 (Basic Semantics)

6.1 Addresses and Pointers Recall memory concepts from Ch2 ch6_testbasicpointer.c int x1=1, x2=7; double distance; int *p; int q=8; p = &q; name addre

Semantic Consistency in Information Exchange

Mobile Service > IAP > Android SDK [ ] IAP SDK TOAST SDK. IAP SDK. Android Studio IDE Android SDK Version (API Level 10). Name Reference V

텀블러514

untitled

Analytics > Log & Crash Search > Unity ios SDK [Deprecated] Log & Crash Unity ios SDK. TOAST SDK. Log & Crash Unity SDK Log & Crash Search. Log & Cras

Microsoft Word - FunctionCall

슬라이드 1

Exercise (10pts) k-친수 일반적으로 k진수(k > 1)는 다음과 같이 표현한다. d0 dn 여기서 di {0,, k 1}. 그리고 d0 dn 은 크기가 d0 k dn k n 인 정수를 표현한다. 이것을 살짝 확장해서 k친수 를 다음과 같이 정의

Deok9_Exploit Technique

Part Part

£01¦4Àå-2

PART

½ºÅ丮ÅÚ¸µ3_³»Áö

272*406OSAKAÃÖÁ¾-¼öÁ¤b64ٽÚ

MySQL-Ch10

chap 5: Trees

DBPIA-NURIMEDIA

untitled

untitled

많이 이용하는 라면,햄버그,과자,탄산음료등은 무서운 병을 유발하고 비만의 원인 식품 이다. 8,등겨에 흘려 보낸 영양을 되 찾을 수 있다. 도정과정에서 등겨에 흘려 보낸 영양 많은 쌀눈과 쌀껍질의 영양을 등겨를 물에 우러나게하여 장시간 물에 담가 두어 영양을 되 찾는다

PL10

기본자료형만으로이루어진인자를받아서함수를결과값으로반환하는고차함수 기본자료형과함수를인자와결과값에모두이용하는고차함수 다음절에서는여러가지예를통해서고차함수가어떤경우에유용한지를설명한다. 2 고차함수의 예??장에서대상체만바뀌고중간과정은동일한계산이반복될때함수를이용하면전체연산식을간 단

Microsoft PowerPoint - chap6 [호환 모드]

Chap04(Signals and Sessions).PDF

Week5

Homework 3 SNU , 2011 가을이광근 Program Due: 10/10, 24:00 Essay Due: 10/12, 15:30 이번숙제의목적은 : 수업시간에살펴본, 상식적인명령형언어의정확한정의를이해하고그실행기를구현해보기. 상식적인수준에서디자인

Application TI-89 / Voyage TM 200 PLT application. application, application. APPLICATIONS :, N. 1. O application. 2. application : D C application,. a

확률 및 분포

Microsoft PowerPoint - 기계공학실험1-1MATLAB_개요2D.pptx

[ 융합과학 ] 과학고 R&E 결과보고서 뇌파를이용한곤충제어 연구기간 : ~ 연구책임자 : 최홍수 ( 대구경북과학기술원 ) 지도교사 : 박경희 ( 부산일과학고 ) 참여학생 : 김남호 ( 부산일과학고 ) 안진웅 ( 부산일과학고 )

T100MD+

Observational Determinism for Concurrent Program Security

歯표지.PDF

12-file.key

Microsoft PowerPoint - a10.ppt [호환 모드]

Javascript.pages

Homework 2 SNU , Fall 2015 Kwangkeun Yi due: 9/30, 24:00 Exercise 1 (10pts) k- 친수 일반적으로 k 진수 (k > 1) 는다음과같이표현한다. d 0 d n 여기서 d i {0,, k 1}. 그리

PowerPoint 프레젠테이션

목차 BUG offline replicator 에서유효하지않은로그를읽을경우비정상종료할수있다... 3 BUG 각 partition 이서로다른 tablespace 를가지고, column type 이 CLOB 이며, 해당 table 을 truncate

C++ Programming

SNU =10100 =minusby by1000 ÇÁto0.03exÇÁto0.03exÇÁ=10100 =minusby by1000 ·Îto0.03ex·Îto0.03ex·Î=10100 =minusby by1000 ±×to0.

NoSQL

C프로-3장c03逞풚

SNU =10100 =minusby by1000 ÇÁto0.03exÇÁto0.03exÇÁ=10100 =minusby by1000 ·Îto0.03ex·Îto0.03ex·Î=10100 =minusby by1000 ±×to0.

특허청구의 범위 청구항 1 앵커(20)를 이용한 옹벽 시공에 사용되는 옹벽패널에 있어서, 단위패널형태의 판 형태로 구성되며, 내부 중앙부가 후방 하부를 향해 기울어지도록 돌출 형성되어, 전면이 오 목하게 들어가고 후면이 돌출된 결속부(11)를 형성하되, 이 결속부(11

0. 표지에이름과학번을적으시오. (6) 1. 변수 x, y 가 integer type 이라가정하고다음빈칸에 x 와 y 의계산결과값을적으시오. (5) x = (3 + 7) * 6; x = 60 x = (12 + 6) / 2 * 3; x = 27 x = 3 * (8 / 4

歯PLSQL10.PDF

6주차.key

歯9장.PDF

λx.x (λz.λx.x z) (λx.x)(λz.(λx.x)z) (λz.(λx.x) z) Call-by Name. Normal Order. (λz.z)

5.스택(강의자료).key

untitled

Microsoft PowerPoint - lecture2.ppt

8장 문자열

3ÆÄÆ®-14

90

컴파일러

강의10

untitled

PowerPoint 프레젠테이션

금오공대 컴퓨터공학전공 강의자료

商用

Transcription:

1994

1992 2001 2008

2002

Semantics Engineering with PLT Redex Matthias Felleisen, Robert Bruce Findler and Matthew Flatt 2009 Text

David A. Schmidt

EXPRESSION E ::= N ( E1 O E2 ) OPERATOR O ::= + - NUMERAL N ::= D D N DIGIT D ::= 0 1 2 3 4 5 6 7 8 9 : ((2 + 1) - (3-4)) ETREE ::= NUM [ OP, ETREE, ETREE ] OP ::= + - NUM ::= <string of digits> : ["-", ["+", "2", "1"], ["-", "3", "4"]]

def interpetree(t): # pre: t == ETREE # where ETREE ::= NUM [ OP, ETREE, ETREE ] # OP ::= + - # NUM ::= <string of digits> # post: ans == t ( ) # returns: ans if isinstance(t, str): if t.isdigit(): ans = int(t) else: raise Exception else: if t[0] == + : ans = interpetree(t[1]) + interpetree(t[2]) elif t[0] == - : ans = interpetree(t[1]) - interpetree(t[2]) else: print( interpetree error:,t, is illegal. ) raise Exception return ans

interpetree( ["-", ["+", "2", "1"], ["-", "3", "4"]]) => ans = interpetree(["+", "2", "1"]) - interpetree(["-", "3", "4"]) => ans = interpetree("2") + interpetree("1") => ans = 2 = 2 + interpetree("1") => ans = 1 = 2 + 1 = 3 = 3 - interpetree(["-", "3", "4"]) => ans = interpetree("3") - interpetree("4") => ans = 3 = 3 - interpetree("4") = 3 - => ans = 4 = 3-4 = -1 = 3 - (-1) = 4 = 4

Program P ::= CL CommandList CL ::= C C ; CL Command C ::= I = E print I while E : CL end Expression E ::= N I ( E O E ) Operator O ::= + - Numeral N ::= <strings of digits> Variable I := <strings> PTREE ::= CLIST CLIST ::= [ CTREE+ ] CTREE ::= ["=", ID, ETREE] ["print", ID] ["while", ETREE, CLIST] ETREE ::= NUM ID [OP, ETREE, ETREE]

x = 3 ; while x : x = x - 1 end ; print x [["=", "x", "3"], ["while", "x", [["=", "x", ["-", "x", "1"]]]], ["print", "x"] ]

x = 3 ; while x : x = x - 1 end ; print x program [["=", "x", "3"], ["while", "x", [["=", "x", ["-", "x", "1"]]]], ["print", "x"] ] interpptree interpctree interpetree namespace x 3 def interpptree(program): # pre: program == PTREE ::= CLIST # CLIST ::= [ CTREE+ ] # post: namespace == program ( ) global namespace namespace = {} # for command in program: interpctree(command) print( final namespace =, namespace)

def interpctree(c): # pre: c == CTREE ::= [ =, ID, ETREE] [ print, ID] [ while, ETREE, CLIST] # post: namespace == c ( ) op = c[0] if op == = : var = c[1] expr = c[2] exprval = interpetree(expr) namespace[var] = exprval elif op == print : var = c[1] if var in namespace: val = namespace[var] print(val) else: crash( variable name undefined ) elif op == while : expr = c[1] body = c[2] while (interpetree(expr)!= 0) interpclist(body) else: crash( invalid command )

def interpetree(e): # pre: e == ETREE ::= NUM ID [OP, ETREE, ETREE] OP ::= + - # post: val == e ( ) # returns: val if isinstance(e,str) and e.isdigit(): val = int(e) elif isinstance(e,str) and len(e)>0 and e[0].isalpha(): if e in namespace: val = namespace[e] else: crash( variable name undefined ) else: op = e[0] val1 = interpetree(e[1]) val2 = interpetree(e[2]) if op == + : val = val1 + val2 elif op == - : val = val1 - val2 else: crash( illegal arithmetic operator ) return val

interpptree([["=", "x", "3"], ["while", "x", [["=", "x", ["-", "x", "1"]]]], ["print", "x"] ])

Program P ::= CL CommandList CL ::= C C ; CL Command C ::= L = E while E : CL end print L Expression E ::= N L &L ( E + E ) LefthandSide L ::= I *L Numeral N ::= <strings of digits> Variable I ::= <strings> PTREE ::= CLIST CLIST ::= [ CTREE+ ] CTREE ::= ["=", LTREE, ETREE] ["while", ETREE, CLIST] ["print", LTREE] ETREE ::= NUM LTREE [ &, LTREE] [ +, ETREE, ETREE] LTREE ::= ID [ *, LTREE]

y = 5; z = 0; x = (6 + y) [["=", "y", "5"], ["=", "z", "0"], ["=", "x", [ +, 6, y ]] ] program interpptree interpctree interpetree interpltree namespace 0 1 2... memory

y = 5; z = 0; x = (6 + y) [["=", "y", "5"], ["=", "z", "0"], ["=", "x", [ +, 6, y ]] ] program interpptree interpctree interpetree interpltree y 0 namespace 0 1 2 5... memory

y = 5; z = 0; x = (6 + y) [["=", "y", "5"], ["=", "z", "0"], ["=", "x", [ +, 6, y ]] ] program interpptree interpctree interpetree interpltree y 0 z 1 namespace 0 1 2 5 0... memory

y = 5; z = 0; x = (6 + y) [["=", "y", "5"], ["=", "z", "0"], ["=", "x", [ +, 6, y ]] ] program interpptree interpctree interpetree interpltree y 0 z 1 x 2 namespace 0 1 2 5 0 11... memory

y = 5; z = &y; x = (6 + *z) program [["=", "y", "5"], ["=", "z", ["&", "y"]], ["=", "x", [ +, 6, [ *, z ]]] ] interpptree interpctree interpetree interpltree namespace 0 1 2... memory

y = 5; z = &y; x = (6 + *z) program [["=", "y", "5"], ["=", "z", ["&", "y"]], ["=", "x", [ +, 6, [ *, z ]]] ] interpptree interpctree interpetree interpltree y 0 namespace 0 1 2 5... memory

y = 5; z = &y; x = (6 + *z) program [["=", "y", "5"], ["=", "z", ["&", "y"]], ["=", "x", [ +, 6, [ *, z ]]] ] interpptree interpctree interpetree interpltree y 0 z 1 namespace 0 1 2 5 0... memory

y = 5; z = &y; x = (6 + *z) program [["=", "y", "5"], ["=", "z", ["&", "y"]], ["=", "x", [ +, 6, [ *, z ]]] ] interpptree interpctree interpetree interpltree y 0 z 1 x 2 namespace 0 1 2 5 0 11... memory

memory = [] # memory = [5,0,11] namespace = {} # namespace = { y =0, z =1, x =2} def interpc(program): # pre: program == PTREE ::= [ CTREE+ ] # post: memory == program ( ) global namespace, memory namespace = {} memory = [] for command in program: interpctree(command) print( final namespace =, namespace) print( final memory =, memory)

def interpltree(x): # pre: x == LTREE ::= ID [ *, LTREE] # post: loc == x # returns: loc if isinstance(x,str): if x in namespace: loc = namespace[x] else: memory.append( err ) namespace[x] = len(memory) - 1 loc = namespace[x] else: y = interpltree(x[1]) loc = memory[y] return loc

def interpctree(c): # pre: c == CTREE ::= [ =, LTREE, ETREE] [ while, ETREE, CLIST] [ print, LTREE] # post: memory == c ( ) op = c[0] if op == = : lval = interpltree(c[1]) rval = interpetree(c[2]) memory[lval] = rval elif op == while : expr = c[1] body = c[2] while (interpetree(expr)!= 0) interpclist(body) elif op == print : print(interpltree(c[1])) else: crash( invalid command )

def interpetree(e): # pre: e == ETREE ::= NUM LTREE [ &, LTREE] [ +, ETREE, ETREE] # post: val == e ( ) # returns: val if isinstance(e,str) and e.isdigit(): val = int(e) elif isinstance(e,list) and e[0] == + :: val1 = interpetree(e[1]) val2 = interpetree(e[2]) val = val1 + val2 elif isinstance(e,list) and e[0] == & : val = interpltree(e[1]) else: x = interpltree(e) val = memory[x] return val

declare y : int; declare z : *int; y = 5; z = &y; declare x : int; x = (6 + *z) declare x : int; x = 0; declare y : int; y = (6 + &x) declare x : int; x = 0; *x = 999

Program CommandList Command P ::= CL CL ::= C C ; CL C ::= L = E while E : CL end print L declare I : T Type T ::= int *T Expression E ::= N L &L ( E1 + E2 ) LefthandSide L ::= I *L Numeral Variable N ::= <strings of digits> I ::= <strings> PTREE ::= CLIST CLIST ::= [ CTREE+ ] CTREE ::= ["=", LTREE, ETREE] ["while", ETREE, CLIST] ["print", LTREE] [ dec, ID, TYPE] TYPE ::= [ ptr *, int ] ETREE ::= NUM LTREE [ &, LTREE] [ +, ETREE, ETREE] LTREE ::= ID [ *, LTREE]

declare y : int; declare z : *int; y = 5; z = &y; declare x; x = (6 + *z) program interpptree interpctree interpetree interpltree namespace [[ dec, y, [ int ]], [ dec, z, [ ptr, int ]], ["=", "y", "5"], ["=", "z", ["&", "y"]], [ dec, x, [ int ]], ["=", "x", [ +, 6, [ *, z ]]] ] 0 1 2... memory

declare y : int; declare z : *int; y = 5; z = &y; declare x; x = (6 + *z) program interpptree interpctree interpetree interpltree y [ int ], 0 namespace [[ dec, y, [ int ]], [ dec, z, [ ptr, int ]], ["=", "y", "5"], ["=", "z", ["&", "y"]], [ dec, x, [ int ]], ["=", "x", [ +, 6, [ *, z ]]] ] 0 1 2... memory

declare y : int; declare z : *int; y = 5; z = &y; declare x; x = (6 + *z) program interpptree interpctree interpetree interpltree y [ int ], 0 z [ ptr, int ], 1 namespace [[ dec, y, [ int ]], [ dec, z, [ ptr, int ]], ["=", "y", "5"], ["=", "z", ["&", "y"]], [ dec, x, [ int ]], ["=", "x", [ +, 6, [ *, z ]]] ] 0 1 2... memory

declare y : int; declare z : *int; y = 5; z = &y; declare x; x = (6 + *z) program interpptree interpctree interpetree interpltree y [ int ], 0 z [ ptr, int ], 1 namespace [[ dec, y, [ int ]], [ dec, z, [ ptr, int ]], ["=", "y", "5"], ["=", "z", ["&", "y"]], [ dec, x, [ int ]], ["=", "x", [ +, 6, [ *, z ]]] ] 0 1 2 5... memory

declare y : int; declare z : *int; y = 5; z = &y; declare x; x = (6 + *z) program interpptree interpctree interpetree interpltree y [ int ], 0 z [ ptr, int ], 1 namespace [[ dec, y, [ int ]], [ dec, z, [ ptr, int ]], ["=", "y", "5"], ["=", "z", ["&", "y"]], [ dec, x, [ int ]], ["=", "x", [ +, 6, [ *, z ]]] ] 0 1 2 5 0... memory

declare y : int; declare z : *int; y = 5; z = &y; declare x; x = (6 + *z) program interpptree interpctree interpetree interpltree y [ int ], 0 z [ ptr, int ], 1 x [ int ], 2 namespace [[ dec, y, [ int ]], [ dec, z, [ ptr, int ]], ["=", "y", "5"], ["=", "z", ["&", "y"]], [ dec, x, [ int ]], ["=", "x", [ +, 6, [ *, z ]]] ] 0 1 2 5 0... memory

declare y : int; declare z : *int; y = 5; z = &y; declare x; x = (6 + *z) program interpptree interpctree interpetree interpltree y [ int ], 0 z [ ptr, int ], 1 x [ int ], 2 namespace [[ dec, y, [ int ]], [ dec, z, [ ptr, int ]], ["=", "y", "5"], ["=", "z", ["&", "y"]], [ dec, x, [ int ]], ["=", "x", [ +, 6, [ *, z ]]] ] 0 1 2 5 0 11... memory

memory = [] # memory = [5,0,11] namespace = {} # namespace = { y =([ int ],0), # z =([ ptr, int ],1), # x =([ int ],2)} def interpct(program): # pre: program == PTREE ::= [ CTREE+ ] # post: memory == program ( ) global namespace, memory namespace = {} memory = [] for command in program: interpctree(command) print( final namespace =, namespace) print( final memory =, memory)

def interpltree(x): # pre: x == LTREE ::= ID [ *, LTREE] # post: ans == x (, ) # returns: ans if isinstance(x,str): if x in namespace: ans = namespace[x] else: crash( variable,x, undeclared ) else: type, loc = interpltree(x[1]) if type[0] == ptr : ans = (type[1:],memory[loc]) else: crash( variable not a pointer ) return ans

def interpctree(c): # pre: c == CTREE ::= [ =, LTREE, ETREE] [ while, ETREE, CLIST] [ print, LTREE] [ dec, ID, TYPE] TYPE ::= [ ptr *, int ] # post: memory == c ( ) op = c[0] if op == = : type1,lval = interpltree(c[1]) type2,rval = interpetree(c[2]) if type1 == type2: memory[lval] = rval else: crash( incompatible types for assignment ) elif op == while : expr = c[1] body = c[2] type,val = interpetree(expr) while (val!= 0) interpclist(body) type,val = interpetree(expr) elif op == print : type,num = interpltree(c[1]) print(num) elif op == dec : x = c[1] if x in namespace: crash( variable,x, redeclared ) else: memory.append( err ) namespace[x] = (c[2],len(memory)-1) else: crash( invalid command )

def interpetree(e): # pre: e == ETREE ::= NUM LTREE [ &, LTREE] [ +, ETREE, ETREE] # post: ans == e (, ) # returns: ans if isinstance(e,str) and e.isdigit(): ans = ([ int ],int(e)) elif isinstance(e,list) and e[0] == + :: type1,val1 = interpetree(e[1]) type2,val2 = interpetree(e[2]) if type1 == [ int ] and type2 == [ int ]: ans = ([ int ],val1 + val2) elif isinstance(e,list) and e[0] == & : type,val = interpltree(e[1]) ans = ([ ptr ]+type,val) else: type,loc = interpltree(e) ans = (type,memory[loc]) return ans

x = 7; y = new {f, g}; y.f = x; y.g = new {r}; y.g.r = y.f program interpptree interpctree interpetree interpltree α namespace α heap

x = 7; y = new {f, g}; y.f = x; y.g = new {r}; y.g.r = y.f program interpptree interpctree interpetree interpltree α namespace α x 7 heap

x = 7; y = new {f, g}; y.f = x; y.g = new {r}; y.g.r = y.f program interpptree interpctree interpetree interpltree α namespace α x 7 y β heap β f g

x = 7; y = new {f, g}; y.f = x; y.g = new {r}; y.g.r = y.f program interpptree interpctree interpetree interpltree α namespace α x 7 y β heap β f 7 g

x = 7; y = new {f, g}; y.f = x; y.g = new {r}; y.g.r = y.f program interpptree interpctree interpetree interpltree α namespace α x 7 y β heap β f 7 g ɣ ɣ r

x = 7; y = new {f, g}; y.f = x; y.g = new {r}; y.g.r = y.f program interpptree interpctree interpetree interpltree α namespace α x 7 y β heap β f 7 g ɣ ɣ r 7

Program CommandList Command Expression LefthandSide FieldNames Numeral Variable P ::= CL CL ::= C C ; CL C ::= L = E if E : CL1 else CL2 end print L E ::= N L ( E + E ) new { F } L ::= I L. I F ::= I I, F N ::= <strings of digits> I ::= <strings> PTREE ::= CLIST CLIST ::= [ CTREE+ ] CTREE ::= ["=", LTREE, ETREE] ["if", ETREE, CLIST, CLIST] ["print", LTREE] ETREE ::= NUM [ deref, LTREE] [ +, ETREE, ETREE] [ new, OBJ] LTREE ::= [ ID+ ] OBJ ::= [ ID+ ] a = new { f,g }; a.f = 3; a.g = (1 + a.f) [ ["=", ["a"], ["new", ["f","g"]]], ["=", ["a", "f"], "3"], ["=", ["a", "g"], ["+", "1", ["deref", ["a", "f"]]]] ]

heap = {} # { ( = )+ } heap_count = 0 # x = 7; y = new {f,g}; y.g = 5; z = new {r}; z.r = (y.g + x) heap 0 namespace 0

heap = {} # { ( = )+ } heap_count = 0 # x = 7; y = new {f,g}; y.g = 5; z = new {r}; z.r = (y.g + x) heap 0 x 7 namespace 0

heap = {} # { ( = )+ } heap_count = 0 # x = 7; y = new {f,g}; y.g = 5; z = new {r}; z.r = (y.g + x) heap 0 x 7 y 1 namespace 0 1 f g

heap = {} # { ( = )+ } heap_count = 0 # x = 7; y = new {f,g}; y.g = 5; z = new {r}; z.r = (y.g + x) heap 0 x 7 y 1 namespace 0 1 f g 5

heap = {} # { ( = )+ } heap_count = 0 # x = 7; y = new {f,g}; y.g = 5; z = new {r}; z.r = (y.g + x) heap 0 x 7 y 1 z 2 namespace 0 1 f g 5 2 r

heap = {} # { ( = )+ } heap_count = 0 # x = 7; y = new {f,g}; y.g = 5; z = new {r}; z.r = (y.g + x) heap 0 x 7 y 1 z 2 namespace 0 1 f g 5 2 r 12

heap = {} # { ( = )+ } heap_count = 0 # x = 7; y = new {f,g}; y.g = 5; z = new {r}; z.r = (y.g + x) heap 0 x 7 y 1 z 2 namespace 0 heap = { "0": {"x":7, "y":"1", "z":"2"}, "1": {"f":"nil", "g":5}, "2": {"r":12} } heap_count = 3 1 2 f g 5 r 12

def cleartheheap(): global heap_count, heap heap_count = 0 heap = {} def allocatens(): global heap_count newloc = str(heap_count) heap[newloc] = {} heap_count = heap_count + 1 return newloc def dereference(lval): ans = nil loc,f = lval if isinstance(loc,str) and loc.isdigit(): ans = heap[loc][f] else: crash( dereference error: lval =,str(lval)) return ans def store(lval,rval): loc, qual = lval if isinstance(loc,str) and loc.isdigit(): heap[loc][qual] = rval else: crash( store error: lval =,str(lval))

def interpct(program): # pre: program == PTREE ::= [ CTREE+ ] # post: memory == program ( ) global namespace, heap cleartheheap() namespace = allocatens() for command in program: interpctree(command) print( final namespace =,namespace) print( final heap =,heap)

def interpctree(c): # pre: c == CTREE ::= [ =, LTREE, ETREE] [ if, ETREE, CLIST, CLIST] [ print, LTREE] # post: heap == c ( ) op = c[0] if op == = : lval = interpltree(c[1]) rval = interpetree(c[2]) store(lval,rval) elif op == if : test = interpetree(c[1]) if test!= 0: interpclist(c[2]) else: interpclist(c[3]) elif op == print : val = dereference(interpltree(c[1])) print(val) else: crash( invalid command )

def interpetree(e): # pre: e == ETREE ::= NUM [ deref, LTREE] [ +, ETREE, ETREE] [ new OBJ] OBJ ::= [ ID+ ] # post: ans == or or nil (+ ) # returns: ans ans = nil if isinstance(e,str) and e.isdigit(): ans = int(e) elif e[0] == + :: val1 = interpetree(e[1]) val2 = interpetree(e[2]) if isinstance(val1,int) and isinstance(val2,int): ans = val1 + val2 else: crash( addition error - non-int value used ) elif e[0] == deref : ans = dereference(interpltree(e[1])) elif e[0] == new : handle = allocatens() fields = e[1] for f in fields: store((handle,f), nil ) ans = handle else: crash( invalid expression ) return ans

def interpltree(x): # pre: x == LTREE ::= [ ID+ ] # post: lval == (, ) # returns: ans lval = (namespace,x[0]) indexlist = x[1:] while indexlist!= []: fieldname = indexlist[0] indexlist = indexlist[1:] handle = dereference(lval) lval = (handle, fieldname) return lval

x y.f y.g.h ["x"] ["y", "f"] ["y", "g", h ] ( 0, x ) ( 1, f ) crash! heap namespace 0 x 7 y 1 z 2 0 1 f g 5 2 r 12

Peter J. Landin, "Correspondence between ALGOL 60 and Church's Lambda-notation: part I & II". Communications of the ACM, (February 1965).

Program Declaration Command Expression Numeral Variable P ::= D ; C D ::= int I D1 ; D2 C ::= I = E C1 ; C2 while E : C print E E ::= N E1 + E2 E1 == E2 @I N ::= <strings of digits> I ::= <alphanumeric strings>

Program Declaration Command Expression Numeral Variable P ::= D ; C D ::= int I D1 ; D2 proc I = C C ::= I = E C1 ; C2 while E : C print E call I E ::= N E1 + E2 E1 == E2 @I N ::= <strings of digits> I ::= <alphanumeric strings>

Program Declaration Command Expression Numeral Variable P ::= D ; C D ::= int I D1 ; D2 proc I = C fun I = E C ::= I = E C1 ; C2 while E : C print E call I E ::= N E1 + E2 E1 == E2 @I I() N ::= <strings of digits> I ::= <alphanumeric strings>

int i; i = 0; fun f = (i + i) + 1; i = i + 1; i = f() + f() - i int i; i = 0; fun f = 1; i = i + 1; i = f() + f() - i int i; i = 0; fun f = 1; i = i + 1; i = 1 + 1 - i

int i; i = 0; fun f = (i + i) + 1; i = i + 1; i = f() + f() - i int i; i = 0; fun f = (i + i) + 1; i = i + 1; i = (i + i) + 1 + (i + i) + 1 - i

Program Declaration Command Expression Numeral Variable P ::= D ; C D ::= int I D1 ; D2 proc I = C fun I = { C ; return E} C ::= I = E C1 ; C2 while E : C print E call I E ::= N E1 + E2 E1 == E2 @I I() N ::= <strings of digits> I ::= <alphanumeric strings>

Program Declaration Command P ::= D ; C D ::= int I D1 ; D2 proc I(I *) = C C ::= I = E C1 ; C2 while E : C print E I(E*) Program Declaration Command P ::= D ; C D ::= int I D1 ; D2 proc I(I ) = C C ::= I = E C1 ; C2 while E : C print E I(C)

(a) int x; int[3] y; proc p(x, z) { z = x + y[1]; y[x] = z }; x = 1; y[1] = 5; p(x + x, 0); print x; (a) namespace heap α α β x 1 y β p ɣ nil 0 1 2 0 5 0 ɣ codeptr α

int x; int[3] y; proc p(x, z) { (b) α δ z = x + y[1]; y[x] = z }; x = 1; y[1] = 5; p(x + x, 0); print x; (b) namespace α β heap x 1 y p β ɣ nil 0 1 2 0 5 0 δ x 2 z 0 α ɣ codeptr α

(c) int x; int[3] y; proc p(x, z) { z = x + y[1]; y[x] = z }; x = 1; y[1] = 5; p(x + x, 0); print x; (c) namespace heap α δ α β x 1 y β p ɣ nil 0 1 2 0 5 7 δ x 2 z 7 α ɣ codeptr α

(d) int x; int[3] y; proc p(x, z) { z = x + y[1]; y[x] = z }; x = 1; y[1] = 5; p(x + x, 0); print x; (d) namespace heap α α β x 1 y β p ɣ nil 0 1 2 0 5 7 δ x 2 z 7 α ɣ codeptr α

(a) (a) int x; x = 1; proc f(n) { if n > 0 { x = x * n; f(n - 1) } }; f(4) namespace α heap α x 1 f β nil β codeptr α

(b) (b) int x; x = 1; proc f(n) { if n > 0 { x = x * n; f(n - 1) } }; f(4) namespace α ɣ heap α x 1 f β ɣ n 4 α nil β codeptr α

(c) (c) int x; x = 1; proc f(n) { if n > 0 { x = x * n; f(n - 1) } }; f(4) namespace α ɣ heap α x 4 f β ɣ n 4 α nil β codeptr α

(d) (d) int x; x = 1; proc f(n) { if n > 0 { x = x * n; f(n - 1) } }; f(4) namespace α ɣ δ heap α x 4 f β ɣ n 4 α δ n 3 α nil β codeptr α

(e) (e) int x; x = 1; proc f(n) { if n > 0 { x = x * n; f(n - 1) } }; f(4) namespace α ɣ δ heap α x 12 f β ɣ n 4 α δ n 3 α nil β codeptr α

(f) int x; x = 1; proc f(n) { if n > 0 { x = x * n; f(n - 1) } }; f(4) (f) namespace α ɣ δ ε heap α ɣ δ ε x 12 n 4 n 3 n 2 f β nil β codeptr α α α α

define I = U define F(I) =... F(U)

Declaration D ::= int I D1 ; D2 proc I(I ) { C } Command C ::= I = E C1 ; C2 while E : C print E I(E) begin D in C end int x = 0; # begin int y # in y = x + 1; x = y + y end; # y print x # 2 int x = 0; # int y = 9; # begin int y # in y = x + 1; x = y + y print y end; print y # 1 # 9

int i = 0; proc p() { i = i + 1 }; begin int i = 9 in p(); print i # 9? 10? end; p(); print i # 1? 2?

(a) proc f(n) { begin int i; proc g(m) { print m + n + i } in i = 1; return g end }; int i = 9; h = f(2); h(3) (a) namespace α heap α f β i 9 nil β codeptr α

(b) proc f(n) { begin int i; proc g(m) { print m + n + i } in i = 1; return g end }; int i = 9; h = f(2); h(3) (b) namespace α ɣ heap α ɣ f β i 9 nil β codeptr α n 2 i 1 g δ α δ codeptr ɣ

(c) proc f(n) { begin int i; proc g(m) { print m + n + i } in i = 1; return g end }; int i = 9; h = f(2); h(3) (c) namespace α heap α f i β 9 ɣ n i 2 1 h δ g δ nil α β codeptr α δ codeptr ɣ

(d) proc f(n) { begin int i; proc g(m) { print m + n + i } in i = 1; return g end }; int i = 9; h = f(2); h(3) (d) namespace α ε heap α f β ɣ n 2 ε i 9 i 1 h δ g δ nil α m 3 ɣ β δ codeptr codeptr α ɣ

(d) proc f(n) { begin int i; proc g(m) { print m + n + i } in i = 1; return g end }; int i = 9; h = f(2); h(3) (d) namespace α ε heap α f β ɣ n 2 ε i 9 i 1 h δ g δ nil α m 3 ɣ β δ codeptr codeptr α ɣ