Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
우리 언어에 추가할 것
•환경 추가 (유효 범위를 지원하기 위해)
•프로시져 (procedure) 추가 •Static & Dynamic Scoping
•Call-by-value & Call-by-reference
•Return Value
•Recursive Call
•레코드 (record) 추가
•포인터 (pointer) 추가
역사의 흐름을 따라!
기계 중심의
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
let x := Ein S
{int x = E;S
}
Ids1−1←→ Addr ←→ Val
for x := E1 to E2 do Sx := E1;high := E2;while not(high < x) do
S;x := x+ 1
The variable high should not appear in S.
1
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � S ⇓ M
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
let x := Ein S
{int x = E;S
}
Ids1−1←→ Addr ←→ Val
for x := E1 to E2 do Sx := E1;high := E2;while not(high < x) do
S;x := x+ 1
The variable high should not appear in S.
1
환경과 메모리Env Mem
xabc
1T3F
환경과 메모리
let x := 0in
let x := 10in
x := x + 1;x := x + 1
let x := 0in
let y := malloc(1)in
*y = x; x := &y;write **x
dom(M �)
Env {x �→ �1, y �→ �2}Memory {�2 �→ �3, �3 �→ 0, �1 �→ �2}
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
{x �→ �1}{�1 �→ 7, �2 �→ 2, �3 �→ 3, �4 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
1
C SEnv Mem{} {}{x �→ �} {� �→ 0}{x �→ ��} {� �→ 0, �� �→ 10}{x �→ ��} {� �→ 0, �� �→ 11}{x �→ �} {� �→ 0, �� �→ 11}{x �→ �} {� �→ 1, �� �→ 11}
let x := 0inlet x := 10in
x := x + 1;x := x + 1
let x := 0in
let y := malloc(1)in
*y = x; x := &y;write **x
dom(M �)
Env {x �→ �1, y �→ �2}Memory {�2 �→ �3, �3 �→ 0, �1 �→ �2}
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
{x �→ �1}{�1 �→ 7, �2 �→ 2, �3 �→ 3, �4 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
1
σ,M � S1 ⇓ M1 σ,M1 � S2 ⇓ M2
σ,M � S1;S2 ⇓ M2
Env Mem{} {}{x �→ �} {� �→ 0}{x �→ ��} {� �→ 0, �� �→ 10}{x �→ ��} {� �→ 0, �� �→ 11}{x �→ �} {� �→ 0, �� �→ 11}{x �→ �} {� �→ 1, �� �→ 11}
let x := 0inlet x := 10in
x := x + 1;x := x + 1
let x := 0in
let y := malloc(1)in
*y = x; x := &y;write **x
dom(M �)
Env {x �→ �1, y �→ �2}Memory {�2 �→ �3, �3 �→ 0, �1 �→ �2}
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
{x �→ �1}{�1 �→ 7, �2 �→ 2, �3 �→ 3, �4 �→ 2}
1
Procedure
•기계 중심의 프로그래밍 언어에서 procedure란 명령문들에 이름을 붙여 놓은 것
•인자를 가지고 어느 정도 일반화 시킨 것
procedure f(x) = S f(E)
Procedure 선언은 프로그램의 어디서 하도록 할까?
Procedure에 있는 자유로운 변수들은 어떻게 하나?
재사용가독성
문제 쪼개기
Syntax
ProgramP → S
StatementS → x := E
| S;S| if E then S else S| while E do S| for x := E to E do S| read x| write E| skip
StatementS → ...
| let procedure f(x) = S in S| call f(E)
ExpressionE → n | true | false | x
| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E
let x := 1 inlet y := 2
in x + y
let x := 1 inx + y
x := 0;y := 0;while x < 11 do
y := y + x;x := x + 1;
write y
for x:= 0 to 10 doy := y + x;
write y
Γ ∈ TypeEnv = Id → Type
Type → τ | Int | Bool | Type → Type
4
어떤 x가 증가할까?
let x := 1 inlet procedure addx(y) = x := x + y in...addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
let x := Ein S
{int x = E;S
}
Ids1−1←→ Addr ←→ Val
for x := E1 to E2 do Sx := E1;high := E2;while not(high < x) do
S;x := x+ 1
The variable high should not appear in S.
1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
let x := Ein S
{int x = E;S
}
Ids1−1←→ Addr ←→ Val
for x := E1 to E2 do Sx := E1;high := E2;while not(high < x) do
S;x := x+ 1
The variable high should not appear in S.
1
Static Scoping
•프로그램에서 이름은 이제 메모리 주소나 procedure를 지칭한다.
σ ∈ Env = Id → Addr + Procedure
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
let x := Ein S
{int x = E;S
}
Ids1−1←→ Addr ←→ Val
for x := E1 to E2 do Sx := E1;high := E2;while not(high < x) do
S;x := x+ 1
1
int x = 1;void f(int y){
x = x + y;}
{int x = 2;f(3);
};x = x + 1;
let x := 1in call x(2)
let procedure f(x) = x + 10in f + 3
let x := 1in
let y := truein x + y
while 1 do E
while (while x < 10 do x := x + 1) do E
if {name := 1 } then 2 else skip
σ,M � S1 ⇓ M1 σ,M1 � S2 ⇓ M2
σ,M � S1;S2 ⇓ M2
Env Mem{} {}{x �→ �} {� �→ 0}{x �→ ��} {� �→ 0, �� �→ 10}{x �→ ��} {� �→ 0, �� �→ 11}{x �→ �} {� �→ 0, �� �→ 11}{x �→ �} {� �→ 1, �� �→ 11}
let x := 0inlet x := 10in
x := x + 1;
1
Procedure = Id × S × Env
σ,M � E ⇓ v,M �
σ,M � x := E ⇓ ·,M �[v/σ(x)]
σ,M � E ⇓1 v,M1 � /∈ dom(M1) σ[�/x],M1[v/�] � E2 ⇓ v2,M2
σ,M � let x := E1 in E2 ⇓ v2,M2
σ[�x,E1,σ�/f ],M � E2 ⇓ v,M1
σ,M � let procedure f(x) = E1 in E2 ⇓ v,M1
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M) σ1[�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M)σ1[�x,E1,σ1�/f ][�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �y, E1,σ1� σ1[σ(x)/y],M � E1 ⇓ v,M �
σ,M � call f�x� ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure f(y) = x := x + y in
let x := 2in
f(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
5
C style
Static Scopingσ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
1
Procedure가 정의될 때의 환경을 같이 저장한다!
이름의 실체가 실행 전에 결정됨!
Env Memory{x �→ �1} , {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} , {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} , {�1 �→ 1, �2 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
inlet x := 2in
f(3);f(2);
x := x + 1
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let x := {height:= 176, weight = 89}in
x.height + x.weight
let tree := {left := {}, v := 0, right := {}}
1
S
C
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
inlet x := 2in
f(3);f(2);
x := x + 1
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + i
1
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
inlet x := 2in
f(3);f(2);
x := x + 1
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + i
1
메모리 생존 기간Memory Cell’s Lifetime
}}}x
xf
생존기간은 scope와 일치
우리 언어가 커지면 일치하지 않을수도!
Env Memory{x �→ �1} , {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} , {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} , {�1 �→ 1, �2 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
inlet x := 2in
f(3);f(2);
x := x + 1
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let x := {height:= 176, weight = 89}in
x.height + x.weight
let tree := {left := {}, v := 0, right := {}}
1
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
{x �→ �1}{�1 �→ 7, �2 �→ 2, �3 �→ 3, �4 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
inlet x := 2in
f(3);f(2);
x := x + 1
let x := 0in
let procedure sum(n) =
1
}
버려 버려!
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
{x �→ �1}{�1 �→ 7, �2 �→ 2, �3 �→ 3, �4 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
inlet x := 2in
f(3);f(2);
x := x + 1
let x := 0in
let procedure sum(n) =
1
환경
메모리
프로시져에 붙어 있는 환경도 고려
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
inlet x := 2in
f(3);f(2);
x := x + 1
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + i
1
Env Memory{x �→ �1} , {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} , {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} , {�1 �→ 1, �2 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
inlet x := 2in
f(3);f(2);
x := x + 1
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let x := {height:= 176, weight = 89}in
x.height + x.weight
let tree := {left := {}, v := 0, right := {}}
1
S
C
인자 넘기는 방법Parameter Passing
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
1
call-by-value값을 넘기고 있다!
인자 넘기는 방법Parameter Passing
call-by-reference어떨 땐 주소를 넘겨주고 싶은데?
let procedure reset(x) = x := 0in
let y := 1 in...; call reset(y);
let z := 2 in...; call reset(z);
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
1
좋아! 우리 언어에 추가하자!
인자의 주소를 넘기자call-by-reference
ProgramP → S
StatementS → x := E
| S;S| if E then S else S| while E do S| for x := E to E do S| read x| write E| skip
StatementS → ...
| let procedure f(x) = S in S| call f(E)| call f�x�
ExpressionE → n | true | false | x
| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E
let x := 1 inlet y := 2
in x + y
let x := 1 inx + y
x := 0;y := 0;while x < 11 do
y := y + x;x := x + 1;
write y
for x:= 0 to 10 doy := y + x;
write y
Γ ∈ TypeEnv = Id → Type
Type → τ | Int | Bool | Type → Type
5
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
1
별칭alias
함수가 돌려주는 값은?
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
1
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
1
call f(E)나 call f<x>가 값을 돌려주도록 하자!
함수가 계산하는 값을 저장할 변수
명령문이 값을 돌려주면?
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
let x := Ein S
{int x = E;S
}
Ids1−1←→ Addr ←→ Val
2
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ,M � S ⇓ M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
1
에잇 합체! fusion!
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ,M � E ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
1
통일된 의미 구조
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ,M � E ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
1
•값을 돌려주던 E는 받은 메모리를 그대로 값과 같이 돌려주면 되겠다.
•메모리를 돌려주던 S는 어떤 값을 계산해 낸다고 정의 할까?
void/텅 빈 값으로 하자.
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
v ∈ Val = Z + B + {·}
let x := Ein S
{int x = E;S
}
2
혹시 텅 빈 값을 만들고 싶으면 ()를 쓰자.
통일된 SyntaxProgram
P → E
ExpressionE → x := E
| E;E| if E then E else E| while E do E| for x := E to E do E| read x| write E| skip| let procedure f(x) = E in E| call f(E)| call f�x�| n | true | false | x| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E
ProgramP → S
StatementS → x := E
| S;S| if E then S else S| while E do S| for x := E to E do S| read x| write E| skip
StatementS → ...
| let procedure f(x) = S in S| call f(E)| call f�x�
ExpressionE → n | true | false | x
| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E
let x := 1 inlet y := 2
in x + y
6
통일된 Semantics의미 공간
semantic domain
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ,M � E ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
1
주소는 인데스로 쓸 수만 있으면 어떤 것이든 상관없다.
통일된 SemanticsProcedure = Id × S × Env
σ,M � E ⇓ v,M �
σ,M � x := E ⇓ ·,M �[v/σ(x)]
σ,M � E ⇓1 v,M1 � /∈ dom(M1) σ[�/x],M1[v/�] � E2 ⇓ v2,M2
σ,M � let x := E1 in E2 ⇓ v2,M2
σ[�x,E1,σ�/f ],M � E2 ⇓ v,M1
σ,M � let procedure f(x) = E1 in E2 ⇓ v,M1
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M) σ1[�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �y, E1,σ1� σ1[σ(x)/y],M � E1 ⇓ v,M �
σ,M � call f�x� ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
2
Procedure = Id × S × Env
σ,M � E ⇓ v,M �
σ,M � x := E ⇓ ·,M �[v/σ(x)]
σ,M � E1 ⇓ v,M1 � /∈ dom(M1) σ[�/x],M1[v/�] � E2 ⇓ v2,M2
σ,M � let x := E1 in E2 ⇓ v2,M2
σ[�x,E1,σ�/f ],M � E2 ⇓ v,M1
σ,M � let procedure f(x) = E1 in E2 ⇓ v,M1
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M) σ1[�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M)σ1[�x,E1,σ1�/f ][�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �y,E1,σ1� σ1[σ(x)/y],M � E1 ⇓ v,M �
σ,M � call f�x� ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure f(y) = x := x + y in
let x := 2in
f(3);x := x + 1;
σ,M � E ⇓ v
M(σ(x)) = v
σ,M � x ⇓ v
σ,M � E ⇓ v
σ,M � x := E ⇓ M [v/σ(x)]
σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S ⇓ M1
σ,M � let x := E in S ⇓ M1
σ ∈ Env = Id → AddrM ∈ Mem = Addr → Val
v ∈ Val = Z + B + {·}+
5
재귀 호출Recursive Call
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := true
1
incall inc(2)
σ,M � E ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ,M � E ⇓ v,M �
σ,M � x := E ⇓ ·,M �[v/σ(x)]
σ,M � E ⇓1 v,M1 � /∈ dom(M1) σ[�/x],M1[v/�] � E2 ⇓ v2,M2
σ,M � let x := E1 in E2 ⇓ v2,M2
σ[�x,E1,σ�/f ],M � E2 ⇓ v,M1
σ,M � let procedure f(x) = E1 in E2 ⇓ v,M1
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M) σ1[�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �y, E1,σ1� σ1[σ(x)/y],M � E1 ⇓ v,M �
σ,M � call f�x� ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
let x := 2in
addx(3);x := x + 1;
2
incall inc(2)
σ,M � E ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ,M � E ⇓ v,M �
σ,M � x := E ⇓ ·,M �[v/σ(x)]
σ,M � E ⇓1 v,M1 � /∈ dom(M1) σ[�/x],M1[v/�] � E2 ⇓ v2,M2
σ,M � let x := E1 in E2 ⇓ v2,M2
σ[�x,E1,σ�/f ],M � E2 ⇓ v,M1
σ,M � let procedure f(x) = E1 in E2 ⇓ v,M1
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M) σ1[�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M)σ1[�x,E1,σ1�/f ][�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �y, E1,σ1� σ1[σ(x)/y],M � E1 ⇓ v,M �
σ,M � call f�x� ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S
σ[�x, S1�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1� σ,M � E ⇓ v � /∈ dom(M) σ[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
let x := 1 inlet procedure addx(y) = x := x + yin
addx(3)
let x := 1 inlet procedure addx(y) = x := x + y in
2
동작하지 않는다. 이유는?어디에 넣지?
우리가 만든 언어Program
P → E
ExpressionE → x := E
| E;E| if E then E else E| while E do E| for x := E to E do E| read x| write E| skip| let procedure f(x) = E in E| call f(E)| call f�x�| n | true | false | x| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E
ProgramP → S
StatementS → x := E
| S;S| if E then S else S| while E do S| for x := E to E do S| read x| write E| skip
StatementS → ...
| let procedure f(x) = S in S| call f(E)| call f�x�
ExpressionE → n | true | false | x
| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E
let x := 1 inlet y := 2
in x + y
6
지원하는 feature들• for loop and while loop
•name memory cells
•name program codes
•names with scopes
• recursive calls
•call-by-value, call-by-reference
• integer I/O
하지만, 자료 구조는?tree, sets, lists ...
Record•기계 중심의 프로그래밍 언어에서 “레코
드”란 메모리 뭉치 + 뭉치에 있는 주소에 이름을 붙인 것
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{age �→ �1, name �→ �2}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
메모리
Record 추가
ProgramP → E
ExpressionE → x := E
| E;E| if E then E else E| while E do E| for x := E to E do E| read x| write E| skip| let procedure f(x) = E in E| call f(E)| call f�x�| n | true | false | x| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E
ExpressionE → ...
| {xi := E1, ..., xn := En}| E.x| E.x := E
6
만들기
사용하기
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let x := {height:= 176, weight = 89}in
x.height + x.weight
let tree := {left := {}, v := 0, right := {}}in
tree.left := 1;tree.right := {left := {}, v := 2, right := 3}
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
1
Record 의미
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ,M � E ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ[�x, S1,σ�/f ],M � S2 ⇓ M1
σ,M � let procedure f(x) = S1 in S2 ⇓ M1
σ(f) = �x, S1,σ1� σ,M � E ⇓ v � /∈ dom(M) σ1[�/x],M [v/�] � S1 ⇓ M1
σ,M � call f(E) ⇓ M1
σ(f) = �y, S1,σ1� σ1[σ(x)/y],M � S1 ⇓ M1
σ,M � call f�x� ⇓ M1
1
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ,M � E ⇓ v,M �
σ,M � E1 ⇓ v1,M1
σ,M1 � E2 ⇓ v2,M2...
σ,Mn � En ⇓ vn,Mn
�1, ..., �n /∈ dom(Mn) ∀i, j ≤ n. i �= j ⇒ �i �= �j
σ,M � {x1 := E1, ..., xn := En} ⇓ {x1 �→ �1, ..., xn �→ �n},Mn[v1/�1]...[vn/�n]
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ,M � E ⇓ v,M �
σ,M � x := E ⇓ ·,M �[v/σ(x)]
σ,M � E ⇓1 v,M1 � /∈ dom(M1) σ[�/x],M1[v/�] � E2 ⇓ v2,M2
σ,M � let x := E1 in E2 ⇓ v2,M2
σ[�x,E1,σ�/f ],M � E2 ⇓ v,M1
σ,M � let procedure f(x) = E1 in E2 ⇓ v,M1
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M) σ1[�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M)σ1[�x,E1,σ1�/f ][�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �y, E1,σ1� σ1[σ(x)/y],M � E1 ⇓ v,M �
σ,M � call f�x� ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
2
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ,M � E ⇓ v,M �
σ,M � E1 ⇓ v1,M1
σ,M1 � E2 ⇓ v2,M2...
σ,Mn � En ⇓ vn,Mn
�1, ..., �n /∈ dom(Mn) ∀i, j ≤ n. i �= j ⇒ �i �= �j
σ,M � {x1 := E1, ..., xn := En} ⇓ {x1 �→ �1, ..., xn �→ �n},Mn[v1/�1]...[vn/�n]
σ,M � E ⇓ r,M �
σ,M � E.x ⇓ M �(r(x)),M �
σ ∈ Env = Id → Addr + Procedure
Procedure = Id × S × Env
σ,M � E ⇓ v,M �
σ,M � x := E ⇓ ·,M �[v/σ(x)]
σ,M � E ⇓1 v,M1 � /∈ dom(M1) σ[�/x],M1[v/�] � E2 ⇓ v2,M2
σ,M � let x := E1 in E2 ⇓ v2,M2
σ[�x,E1,σ�/f ],M � E2 ⇓ v,M1
σ,M � let procedure f(x) = E1 in E2 ⇓ v,M1
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M) σ1[�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �x,E1,σ1� σ,M � E ⇓ v,M1 � /∈ dom(M)σ1[�x,E1,σ1�/f ][�/x],M1[v/�] � E1 ⇓ v�,M2
σ,M � call f(E) ⇓ v�,M2
σ(f) = �y, E1,σ1� σ1[σ(x)/y],M � E1 ⇓ v,M �
σ,M � call f�x� ⇓ v,M �
σ ∈ Env = Id → Addr + Procedure
2
let x := 0 inlet procedure inc(n) = x:= x + n in
call inc(1);let x := truein
call inc(2)
σ,M � E ⇓ v,M �
σ,M � E1 ⇓ v1,M1
σ,M1 � E2 ⇓ v2,M2...
σ,Mn � En ⇓ vn,Mn
�1, ..., �n /∈ dom(Mn) ∀i, j ≤ n. i �= j ⇒ �i �= �j
σ,M � {x1 := E1, ..., xn := En} ⇓ {x1 �→ �1, ..., xn �→ �n},Mn[v1/�1]...[vn/�n]
σ,M � E ⇓ r,M �
σ,M � E.x ⇓ M �(r(x)),M �
σ,M � E1 ⇓ r,M1 σ,M1 � E2 ⇓ v,M2
σ,M � E1.x := E2 ⇓ ·,M2[v/r(x)]
σ ∈ Env = Id → Addr + Procedure
2
Record 예
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let x := {height:= 176, weight = 89}in
x.height + x.weight
let tree := {left := {}, v := 0, right := {}}in
tree.left := 1;tree.right := {left := {}, v := 2, right := 3}
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
176
89
x heightweight
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
tree left
rightv
1
0
구현에 휘둘리지는 말자.
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
n ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}σ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = N
Record = Field → Addr
{left �→ �1, content �→ �2, right �→ �3}
let x := 0in
let procedure sum(n) =for i := 1 to n do
x := x + iin
call sum(10);write x+1;
let procedure sum(n) =let x := 0 in
for i := 1 to n dox := x + i;
return xin
write (call sum(10)) + 1
let procedure fac(n) =if n <= 0 then 1else n * call fac (n-1)
incall fac(2)
let procedure reset(x) = x := 0in
let y := 1 in...; call reset <y>;
let z := 2 in...; call reset <z>;
...
1
{}
2
3
left
rightv
우리가 만든 언어지원하는 feature들• for loop and while loop•name memory cells•name program codes•names with scopes• recursive calls•call-by-value, call-by-reference• integer I/O•primitive values: integers, booleans•compound values: records
하지만, 메모리 주소에 꼭 이름을 주어야 하나요?
ProgramP → E
ExpressionE → x := E
| E;E| if E then E else E| while E do E| for x := E to E do E| read x| write E| skip| let procedure f(x) = E in E| call f(E)| call f�x�| n | true | false | x| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E| {xi := E1, ..., xn := En}| E.x| E.x := E
ProgramP → S
StatementS → x := E
| S;S| if E then S else S| while E do S| for x := E to E do S| read x| write E| skip
StatementS → ...
| let procedure f(x) = S in S| call f(E)| call f�x�
ExpressionE → n | true | false | x
| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E
let x := 1 in
7
주소도 값이다!
주소를 만드는 방법주소를 사용하는 방법
let x := 0in
let y := malloc(1)in
*y = x; x := &y;write *x
Env {x �→ �1, y �→ �2}Memory {�2 �→ �3, �3 �→ 0, �1 �→ �2}
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
{x �→ �1}{�1 �→ 7, �2 �→ 2, �3 �→ 3, �4 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
1
Final Statelet x := 0in
let y := malloc(1)in
*y = x; x := &y;write **x
Env {x �→ �1, y �→ �2}Memory {�2 �→ �3, �3 �→ 0, �1 �→ �2}
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
{x �→ �1}{�1 �→ 7, �2 �→ 2, �3 �→ 3, �4 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
let x := 1in
let procedure f(y) =x := x + y
1
ProgramP → E
ExpressionE → x := E
| E;E| if E then E else E| while E do E| for x := E to E do E| read x| write E| skip| let procedure f(x) = E in E| call f(E)| call f�x�| n | true | false | x| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E| {xi := E1, ..., xn := En}| E.x| E.x := E
ExpressionE → ...
| malloc(E) | &x| ∗E | E := E
7
ProgramP → E
ExpressionE → x := E
| E;E| if E then E else E| while E do E| for x := E to E do E| read x| write E| skip| let procedure f(x) = E in E| call f(E)| call f�x�| n | true | false | x| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E| {xi := E1, ..., xn := En}| E.x| E.x := E| malloc(E) | &E| ∗E | E := E
8
포인터 관련식의 의미
4.4 !" !!! II: K- 89
Exercise 1 !!!! !"! !! !!!!, !!!! "!"", !!!" !
"!" !"! !!# """ #$!! ""!!# # ! ! $". "!%?
%# "%! "&& '".
!,M ! E " n,M !
!,M ! malloc E " ",M ! n > 0, {", " + 1, · · · , " + n # 1} $% Dom M !
!,M ! &x " !(x),M
!,M ! E " r,M !
!,M ! &E.x " r(x),M !
!,M ! E " ",M !
!,M ! *E " M !("),M !
!,M ! E1 " ",M1 !,M1 ! E2 " v,M2
!,M ! *E1 := E2 " v,M2{" &' v}
*E! ! '!# "( (!# !"! !"$% $!$ $# (!, %##
"#". !! x# !"$% $!!! %$!!!$ "" %## #" #"!.
*E # !"$% $!$ $! (!! E# #$! !"! !!"# #%". &
! !"! !!#% %". *E# !"$% %$!$ $! (!! E# #$!
!"! !!$ ""#" $! "# #%". (C!"# !#".)
!$" &$ ""!!# #'$# x# ##!! !!% "&# !!$!
3! ""$".
letx := malloc(2)
in*x := 1;*(x+1) := *x + 2
SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008
let x := 0in
let y := malloc(1)in
*y = x; x := &y;write **x
dom(M �)
Env {x �→ �1, y �→ �2}Memory {�2 �→ �3, �3 �→ 0, �1 �→ �2}
Env Memory{x �→ �1} {�1 �→ 1}
{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 1}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 1, �2 �→ 2}{f �→ �y, x := x+ y, {x �→ �1}�, x �→ �2} {�1 �→ 4, �2 �→ 2}{x �→ �2, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 6, �2 �→ 2}{x �→ �1, f �→ �y, x := x+ y, {x �→ �1}�} {�1 �→ 7, �2 �→ 2}
{x �→ �1}{�1 �→ 7, �2 �→ 2, �3 �→ 3, �4 �→ 2}
Env Memory{x �→ �1, y �→ �3} {�1 �→ 1, �2 �→ 2, �3 �→ 3}
{x �→ �1, y �→ �4} {�1 �→ 4, �2 �→ 2, �4 �→ 2}
r ∈ Record = Field → Addrn ∈ Zb ∈ B = {T, F}v ∈ Val = Z + B + {·}+ Recordσ ∈ Env = Id → Addr + ProcedureM ∈ Mem = Addr → Val
Procedure = Id × E × Env� ∈ Addr = Nf ∈ Field = Id
Record = Field → Addr
age name
{left �→ �1, content �→ �2, right �→ �3}
1
우리가 만든 언어지원하는 feature들• for loop and while loop•name memory cells•name program codes•names with scopes• recursive calls•call-by-value, call-by-reference• integer I/O•primitive values: integers, booleans•compound values: records• locations as values: pointers
ProgramP → E
ExpressionE → x := E
| E;E| if E then E else E| while E do E| for x := E to E do E| read x| write E| skip| let procedure f(x) = E in E| call f(E)| call f�x�| n | true | false | x| E + E | E − E | E ∗ E | E/E| E = E | E < E | not E| {xi := E1, ..., xn := En}| E.x| E.x := E| malloc(E) | &E| ∗E | E := E
8
훗!그래야
C언어 답지!
훗!!
훗!!
훗!이래야
C언어 답지!
타입 검사Type Checking
메모리 관리Memory Management