Variables and Scope
변수와 유효범위
HNU CE 프로그래밍언어론 (2021년 1학기)
여태껏 본 의미규칙의 특징
모든 곳에서 항상 같은 실행환경(σ)
프로그램의 모든 부분에서 같은 변수 바인딩을 계속 활용
의미규칙
e::=x∣n∣b∣e+e∣e∧e∣¬e ∣e=e∣e<e∣ifetheneelsee
\begin{array}{l}
e ::= x \mid n \mid b
\mid e + e
\mid e \land e \mid \neg e
\\~\quad~~
\mid e = e \mid e \lt e \mid \texttt{if}\;e\;\texttt{then}\;e\;\texttt{else}\;e\;
\end{array}
Semantics
σ,e⇓v
~\sigma,e \Downarrow v~
v∈Value=Int⊎Bool
v \in \texttt{Value} \;=\; \texttt{Int} \uplus \texttt{Bool}
e∈Exprn∈Intb∈Bool
e \in \texttt{Expr} \quad n\in\texttt{Int} \quad b\in\texttt{Bool}
Syntax
σ,e1+e2⇓n1+n2σ,e1⇓n1σ,e2⇓n2
\frac{\sigma,\,e_1\Downarrow\,n_1 \quad \sigma,\,e_2\Downarrow\,n_2}{\sigma,\,e_1+\,e_2 \;\Downarrow\; n_1\texttt{+}\,n_2}
σ,e1∧e2⇓b1&&b2σ,e1⇓b1σ,e2⇓b2
\frac{\sigma,\,e_1\Downarrow\,b_1 \quad \sigma,\,e_2\Downarrow\,b_2}{\sigma,\,e_1\land\,e_2 \;\Downarrow\; b_1\texttt{\&\&}\,b_2}
σ,¬e⇓notbσ,e⇓b
\frac{\sigma,\,e\,\Downarrow\,b }{\sigma,\,\neg e \;\Downarrow\; \texttt{not}\,b}
σ,x⇓σ(x)
\frac{}{\sigma,\,x \;\Downarrow\; \sigma(x)}
σ,n⇓n
\frac{}{\sigma,\,n \;\Downarrow\; n}
σ,b⇓b
\frac{}{\sigma,\,b \;\Downarrow\; b}
의미규칙
e::=x∣n∣b∣e+e∣e∧e∣¬e ∣e=e∣e<e∣ifetheneelsee
\begin{array}{l}
e ::= x \mid n \mid b
\mid e + e
\mid e \land e \mid \neg e
\\~\quad~~
\mid e = e \mid e \lt e \mid \texttt{if}\;e\;\texttt{then}\;e\;\texttt{else}\;e\;
\end{array}
Semantics
σ,e⇓v
~\sigma,e \Downarrow v~
v∈Value=Int⊎Bool
v \in \texttt{Value} \;=\; \texttt{Int} \uplus \texttt{Bool}
e∈Exprn∈Intb∈Bool
e \in \texttt{Expr} \quad n\in\texttt{Int} \quad b\in\texttt{Bool}
σ,ifethene1elsee0⇓v0σ,e⇓Falseσ,e0⇓v0
\frac{\sigma,\,e\,\Downarrow\,\texttt{False} \quad \sigma,\,e_0\,\Downarrow\,v_0}{
\sigma,\,\texttt{if}\;e\;\texttt{then}\;e_1\;\texttt{else}\;e_0\;\Downarrow\; v_0}
σ,ifethene1elsee1⇓v1σ,e⇓Trueσ,e1⇓v1
\frac{\sigma,\,e\,\Downarrow\,\texttt{True} \quad \sigma,\,e_1\,\Downarrow\,v_1}{
\sigma,\,\texttt{if}\;e\;\texttt{then}\;e_1\;\texttt{else}\;e_1\;\Downarrow\; v_1}
Syntax
σ,e1=e2⇓n1==n2σ,e1⇓n1σ,e2⇓n2
\frac{\sigma,\,e_1\,\Downarrow\,n_1 \quad \sigma,\,e_2\,\Downarrow\,n_2}{
\sigma,\,e_1 =\, e_2 \;\Downarrow\; n_1 \texttt{==}\, n_2}
σ,e1=e2⇓b1==b2σ,e1⇓b1σ,e2⇓b2
\frac{\sigma,\,e_1\,\Downarrow\,b_1 \quad \sigma,\,e_2\,\Downarrow\,b_2}{
\sigma,\,e_1 =\, e_2 \;\Downarrow\; b_1 \texttt{==}\, b_2}
σ,e1<e2⇓n1<n2σ,e1⇓n1σ,e2⇓n2
\frac{\sigma,\,e_1\,\Downarrow\,n_1 \quad \sigma,\,e_2\,\Downarrow\,n_2}{
\sigma,\,e_1 <\, e_2 \;\Downarrow\; n_1 \texttt{<}\, n_2}
σ,e1<e2⇓b1<b2σ,e1⇓b1σ,e2⇓b2
\frac{\sigma,\,e_1\,\Downarrow\,b_1 \quad \sigma,\,e_2\,\Downarrow\,b_2}{
\sigma,\,e_1 <\, e_2 \;\Downarrow\; b_1 \texttt{<}\, b_2}
실행환경에 영향을 주는 언어 요소
프로그램의 특정 범위에 한정된 변수 활용
즉 유효범위(scope)가 있는 변수 선언
- 지역변수 선언
- 함수 파라미터(형식인자)
- ...
let식 의미규칙
e::=x∣n∣b∣e+e∣e∧e∣¬e∣e=e∣e<e ∣ifetheneelsee∣letx=eine
\begin{array}{l}
e ::= x \mid n \mid b
\mid e + e
\mid e \land e \mid \neg e
\mid e = e \mid e \lt e
\\~\quad~~
\mid \texttt{if}\;e\;\texttt{then}\;e\;\texttt{else}\;e\; \mid \texttt{let}\;x=e\;\texttt{in}\;e\;
\end{array}
Semantics
σ,e⇓v
~\sigma,e \Downarrow v~
v∈Value=Int⊎Bool
v \in \texttt{Value} \;=\; \texttt{Int} \uplus \texttt{Bool}
e∈Exprn∈Intb∈Bool
e \in \texttt{Expr} \quad n\in\texttt{Int} \quad b\in\texttt{Bool}
Syntax
σ,letx=eine1⇓v1σ,e⇓v[x↦v]σ,e1⇓v1
\displaystyle\frac{\sigma,\,e\,\Downarrow\,v \quad [x\mapsto v]\sigma,\,e_1\,\Downarrow\,v_1 }{
\sigma,\,\texttt{let}\;x=e\;\texttt{in}\;e_1 \;\Downarrow\; v_1}
σ′=[x↦v]σ
\sigma' = [x\mapsto v]\sigma
σ′(x)=vσ′(y)=σ(y)(y=x)
\begin{array}{l}
\sigma'(x) = v \\
\sigma'(y) = \sigma(y) \quad (y\neq x)
\end{array}
Variables and Scope 변수와 유효범위 HNU CE 프로그래밍언어론 (2021년 1학기)
변수와 유효범위
By 안기영 (Ahn, Ki Yung)
변수와 유효범위
HNU CE 프로그래밍언어론 (2021년 1학기)
- 859