Variables and Scope

변수와 유효범위

HNU CE 프로그래밍언어론 (2021년 1학기)

여태껏 본 의미규칙의 특징

모든 곳에서 항상 같은 실행환경(σ)

 

프로그램의 모든 부분에서 같은 변수 바인딩을 계속 활용

의미규칙

\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

~\sigma,e \Downarrow v~
v \in \texttt{Value} \;=\; \texttt{Int} \uplus \texttt{Bool}
e \in \texttt{Expr} \quad n\in\texttt{Int} \quad b\in\texttt{Bool}

Syntax

\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}
\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}
\frac{\sigma,\,e\,\Downarrow\,b }{\sigma,\,\neg e \;\Downarrow\; \texttt{not}\,b}
\frac{}{\sigma,\,x \;\Downarrow\; \sigma(x)}
\frac{}{\sigma,\,n \;\Downarrow\; n}
\frac{}{\sigma,\,b \;\Downarrow\; b}

의미규칙

\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

~\sigma,e \Downarrow v~
v \in \texttt{Value} \;=\; \texttt{Int} \uplus \texttt{Bool}
e \in \texttt{Expr} \quad n\in\texttt{Int} \quad b\in\texttt{Bool}
\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}
\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

\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}
\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}
\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}
\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식 의미규칙

\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

~\sigma,e \Downarrow v~
v \in \texttt{Value} \;=\; \texttt{Int} \uplus \texttt{Bool}
e \in \texttt{Expr} \quad n\in\texttt{Int} \quad b\in\texttt{Bool}

Syntax

\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}
\sigma' = [x\mapsto v]\sigma
\begin{array}{l} \sigma'(x) = v \\ \sigma'(y) = \sigma(y) \quad (y\neq x) \end{array}

변수와 유효범위

By 안기영 (Ahn, Ki Yung)

변수와 유효범위

HNU CE 프로그래밍언어론 (2021년 1학기)

  • 706