プログラムの意味 is

自己紹介

自己紹介

@__pandaman64__

 

理工学部情報工学科3年

プログラミング言語が

すき

量子プログラミング

やっていくぞ

プログラム is 何

プログラム is 何

(だいたい)人が書く​

 

 

コンピュータが解釈して計算する

プログラム is 何

(だいたい)人が書く​

 

 

コンピュータが解釈して計算する

print("Hello, World!")
cout << "Hello, World!" << endl;
System.out.writeln("Hello, World!");

プログラム is 何

(だいたい)人が書く​

 

 

コンピュータが解釈して計算する

構文論

意味論

プログラムの意味 is

プログラムの意味 is

プログラムを実行した結果得る値(+α)

プログラムの意味の例

>>> 3
3
>>> "Hello, World!"
'Hello, World!'
>>> 1 + 2
3
>>> from operator import add
>>> add(1, 2)
3
>>> 1 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

プログラムの意味 is

なぜ

なぜプログラムの意味?

  • プログラムの性質を知りたい

    • 停止性とか

       

  • プログラムの最適化

    • ​プログラムを100倍速くしました!答えは全然違います!

    • 意味を変えずにプログラムを高速化したい

表示的意味論

単純な表示的意味

プログラム

意味(値)

3

3

1 + 2 + 3

6

2 × 3

6

表示的意味

括弧・関数の表示的意味

プログラム

意味(値)

(1 + 2) × 3

\(x \mapsto x + 1\)

9

\(\mathrm{fun}(x)\{x+1\}\)

6

\(\mathrm{fun}(x)\{x+1\}(5)\)

高階関数

プログラム

意味(値)

\(\mathrm{fun}\, \mathrm{inc}(x)\{x+1\}\)

\(x \mapsto x + 1\)

6

\(\mathrm{fun}(f)\{f(5)\}(\mathrm{inc})\)

6

\(\mathrm{fun}(f)\{f(5)\}\)

\(f \mapsto f(5)\)

\(\mathrm{inc}(x)\{x+1\}\)

場合分け

プログラム

意味(値)

\begin{aligned} &\mathrm{fun}\,\mathrm{abs}(x)\\ &| x \geq 0 \Rightarrow x \\ &|x < 0 \Rightarrow -x \end{aligned}
funabs(x)x0xx<0x\begin{aligned} &\mathrm{fun}\,\mathrm{abs}(x)\\ &| x \geq 0 \Rightarrow x \\ &|x < 0 \Rightarrow -x \end{aligned}
\begin{aligned} &\mathrm{abs}(x) = \\ &\begin{cases} x & (x \geq 0) \\ -x & (x < 0) \end{cases} \end{aligned}
abs(x)={x(x0)x(x<0)\begin{aligned} &\mathrm{abs}(x) = \\ &\begin{cases} x & (x \geq 0) \\ -x & (x < 0) \end{cases} \end{aligned}

となる関数abs

x\mapsto|x|
xxx\mapsto|x|

\(\perp\)(ボトム)

プログラム

意味(値)

1 / 0

(1 / 0) + 2

\(\mathrm{fun}(x)\{x + 1\}(1/0)\)

表示的意味

プログラムの表示的意味は

  1. 値(整数・関数・・・)
  2. ⊥(エラー・無限ループ)

​​のいずれか

複雑なプログラムの意味は

部分の意味から決まる(合成性

表示的意味

プログラムの表示的意味は

  1. 値(整数・関数・・・)
  2. ⊥(エラー・無限ループ

​​のいずれか

複雑なプログラムの意味は

部分の意味から決まる(合成性)

ループ?

繰り返し(再帰)

プログラム

意味(値)

?????

\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}(x) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times \mathrm{fact}(x - 1) \end{aligned}
funfact(x)x<0x=01x>0x×fact(x1)\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}(x) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times \mathrm{fact}(x - 1) \end{aligned}

繰り返し(再帰)

プログラム

意味(値)

factの定義にfact自身が出てくる

fact意味を決めるためにはfact意味を知らなくてはいけない?

\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}(x) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times \mathrm{fact}(x - 1) \end{aligned}
funfact(x)x<0x=01x>0x×fact(x1)\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}(x) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times \mathrm{fact}(x - 1) \end{aligned}

そんなことないよ!

繰り返し(再帰)

関数を引数に

とっている

自分自身の代わりに

引数の関数を呼び出す

\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}'(x, f) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times f(x - 1) \end{aligned}
funfact(x,f)x<0x=01x>0x×f(x1)\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}'(x, f) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times f(x - 1) \end{aligned}

引数の関数で

再帰呼び出しを置き換える

繰り返し(再帰)

自分自身を呼び出さないので

これまで同様

意味を決めることができる!

\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}'(x, f) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times f(x - 1) \end{aligned}
funfact(x,f)x<0x=01x>0x×f(x1)\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}'(x, f) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times f(x - 1) \end{aligned}

引数の関数で

再帰呼び出しを置き換える

プログラム

意味

\begin{aligned} & (x,f) \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times f(x - 1) & (x > 0) \end{cases} \end{aligned}
(x,f){(x<0)1(x=0)x×f(x1)(x>0)\begin{aligned} & (x,f) \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times f(x - 1) & (x > 0) \end{cases} \end{aligned}
\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}'(x, f) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times f(x - 1) \end{aligned}
funfact(x,f)x<0x=01x>0x×f(x1)\begin{aligned} & \mathrm{fun}\,\,\mathrm{fact}'(x, f) \\ & \lvert x < 0 \Rightarrow \perp \\ & \lvert x = 0 \Rightarrow 1 \\ & \lvert x > 0 \Rightarrow x\times f(x - 1) \end{aligned}

\( \mathrm{fact}' \)の意味

fact'に関数を渡す

\mathrm{fun}\,\,\mathrm{fact}_0(x)\{ \mathrm{fact}'(x, \mathrm{abort})\}
funfact0(x){fact(x,abort)}\mathrm{fun}\,\,\mathrm{fact}_0(x)\{ \mathrm{fact}'(x, \mathrm{abort})\}

ただしabortは

\mathrm{fun}\,\,\mathrm{abort}(x)\{\perp\}
funabort(x){}\mathrm{fun}\,\,\mathrm{abort}(x)\{\perp\}

(即座にエラーを返す関数)

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{abort}(x - 1) & (x > 0) \\ = \perp \end{cases} \end{aligned}
x{(x<0)1(x=0)x×abort(x1)(x>0)=\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{abort}(x - 1) & (x > 0) \\ = \perp \end{cases} \end{aligned}

fact'fabortで置き換える

\( \mathrm{fact}_0 \)の意味

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \neq 0) \\ 1 & (x = 0) \end{cases} \end{aligned}
x{(x0)1(x=0)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \neq 0) \\ 1 & (x = 0) \end{cases} \end{aligned}

これだけだと無意味・・・

\( \mathrm{fact}_0 \)の意味

\( \mathrm{fact}' \)に関数を渡す(再)

\begin{aligned} &\mathrm{fun}\,\,\mathrm{fact}_1(x)\{ \\ & \quad\mathrm{fact}'(x, \mathrm{fact}_0) \\ &\} \end{aligned}
funfact1(x){fact(x,fact0)}\begin{aligned} &\mathrm{fun}\,\,\mathrm{fact}_1(x)\{ \\ & \quad\mathrm{fact}'(x, \mathrm{fact}_0) \\ &\} \end{aligned}

\( \mathrm{fact}_0 \)が出てきた

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_0(x - 1) & (x > 0) \end{cases} \end{aligned}
x{(x<0)1(x=0)x×fact0(x1)(x>0)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_0(x - 1) & (x > 0) \end{cases} \end{aligned}

\( \mathrm{fact}_1\)の意味

fact'\( f \) \( \mathrm{fact}_0 \)で置き換える

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \neq 0, 1) \\ 1 & (x = 0) \\ 1 \times {\mathrm{fact}_0(0)} = 1 & (x = 1) \end{cases} \end{aligned}
x{(x0,1)1(x=0)1×fact0(0)=1(x=1)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \neq 0, 1) \\ 1 & (x = 0) \\ 1 \times {\mathrm{fact}_0(0)} = 1 & (x = 1) \end{cases} \end{aligned}

ちょっと増えた

\( \mathrm{fact}_1 \)の意味

\( \mathrm{fact}' \)に関数を渡す(再)

\begin{aligned} &\mathrm{fun}\,\,\mathrm{fact}_2(x)\{ \\ & \quad\mathrm{fact}'(x, \mathrm{fact}_1) \\ &\} \end{aligned}
funfact2(x){fact(x,fact1)}\begin{aligned} &\mathrm{fun}\,\,\mathrm{fact}_2(x)\{ \\ & \quad\mathrm{fact}'(x, \mathrm{fact}_1) \\ &\} \end{aligned}

\( \mathrm{fact}_1 \)が出てきた

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_1(x - 1) & (x > 0) \end{cases} \end{aligned}
x{(x<0)1(x=0)x×fact1(x1)(x>0)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_1(x - 1) & (x > 0) \end{cases} \end{aligned}

\( \mathrm{fact}_2 \)の意味

fact'\( f \) \( \mathrm{fact}_1 \)で置き換える

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \neq 0, 1 ,2) \\ 1 & (x = 0) \\ 1 \times {\mathrm{fact}_1(0)} = 1 & (x = 1) \\ 2 \times \mathrm{fact}_1(1) = 2 & (x = 2) \end{cases} \end{aligned}
x{(x0,1,2)1(x=0)1×fact1(0)=1(x=1)2×fact1(1)=2(x=2)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \neq 0, 1 ,2) \\ 1 & (x = 0) \\ 1 \times {\mathrm{fact}_1(0)} = 1 & (x = 1) \\ 2 \times \mathrm{fact}_1(1) = 2 & (x = 2) \end{cases} \end{aligned}

さらに増えた

\( \mathrm{fact}_2 \)の意味

\( \mathrm{fact}' \)に関数を渡す(再)

\begin{aligned} &\mathrm{fun}\,\,\mathrm{fact}_3(x)\{ \\ & \quad\mathrm{fact}'(x, \mathrm{fact}_2) \\ &\} \end{aligned}
funfact3(x){fact(x,fact2)}\begin{aligned} &\mathrm{fun}\,\,\mathrm{fact}_3(x)\{ \\ & \quad\mathrm{fact}'(x, \mathrm{fact}_2) \\ &\} \end{aligned}

\( \mathrm{fact}_2 \)が出てきた

\( \mathrm{fact}_3 \)の意味

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_2(x - 1) & (x > 0) \end{cases} \end{aligned}
x{(x<0)1(x=0)x×fact2(x1)(x>0)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_2(x - 1) & (x > 0) \end{cases} \end{aligned}

fact'\( f \) \( \mathrm{fact}_2 \)で置き換える

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \notin [0,3]) \\ 1 & (x = 0) \\ 1 \times {\mathrm{fact}_2(0)} = 1 & (x = 1) \\ 2 \times \mathrm{fact}_2(1) = 2 & (x = 2) \\ 3 \times \mathrm{fact}_2(2) = 6 & (x = 3) \end{cases} \end{aligned}
x{(x[0,3])1(x=0)1×fact2(0)=1(x=1)2×fact2(1)=2(x=2)3×fact2(2)=6(x=3)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \notin [0,3]) \\ 1 & (x = 0) \\ 1 \times {\mathrm{fact}_2(0)} = 1 & (x = 1) \\ 2 \times \mathrm{fact}_2(1) = 2 & (x = 2) \\ 3 \times \mathrm{fact}_2(2) = 6 & (x = 3) \end{cases} \end{aligned}

もひとつ増えた

\( \mathrm{fact}_3 \)の意味

\( \mathrm{fact}' \)に関数を渡す(再)

\begin{aligned} &\mathrm{fun}\,\,\mathrm{fact}_n(x)\{ \\ & \quad\mathrm{fact}'(x, \mathrm{fact}_{n-1}) \\ &\} \end{aligned}
funfactn(x){fact(x,factn1)}\begin{aligned} &\mathrm{fun}\,\,\mathrm{fact}_n(x)\{ \\ & \quad\mathrm{fact}'(x, \mathrm{fact}_{n-1}) \\ &\} \end{aligned}

\( \mathrm{fact}_{n-1} \)が出てきた

\( \mathrm{fact}_n \)の意味

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_{n-1}(x - 1) & (x > 0) \end{cases} \end{aligned}
x{(x<0)1(x=0)x×factn1(x1)(x>0)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_{n-1}(x - 1) & (x > 0) \end{cases} \end{aligned}

fact'\( f \) \( \mathrm{fact}_{n-1} \)で置き換える

\( \mathrm{fact}_n \) の意味

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \notin [0,n]) \\ x! & (x \in [0,n]) \end{cases} \end{aligned}
x{(x[0,n])x!(x[0,n])\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x \notin [0,n]) \\ x! & (x \in [0,n]) \end{cases} \end{aligned}

階乗関数に近づいてきた!

これを無限に繰り返す

\( \mathrm{fact}_\infty \)

\( \mathrm{fact}_\infty \) の意味

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ x! & (x \geq 0) \end{cases} \end{aligned}
x{(x<0)x!(x0)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ x! & (x \geq 0) \end{cases} \end{aligned}

階乗関数!!!!!!!

不動点としての\( \mathrm{fact}_\infty \) 

\( \mathrm{fact}'(x, \mathrm{fact}_\infty) \)意味

\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_\infty(x - 1) & (x > 0) \end{cases} \end{aligned}
x{(x<0)1(x=0)x×fact(x1)(x>0)\begin{aligned} & x \mapsto \\ &\begin{cases} \perp & (x < 0) \\ 1 & (x = 0) \\ x \times \mathrm{fact}_\infty(x - 1) & (x > 0) \end{cases} \end{aligned}

階乗関数!!!!!!!

不動点としての\( \mathrm{fact}_\infty \) 

\( \mathrm{fact}'(x, \mathrm{fact}_\infty) \)\( \mathrm{fact}_\infty \)意味は等しい

\( \mathrm{fact}_\infty \)\( \mathrm{fact}' \)不動点(fixpoint)

\( \mathrm{fact}_\infty = \mathrm{fix}(\mathrm{fact}') \)

まとめ

  • プログラム意味プログラム解釈した結果得た
  • 意味プログラムの構造から合成的に決められる
  • 整数リテラルの意味は整数
  • 関数プログラムの意味は数学上の関数
  • 繰り返し(ループ)は再帰によって表現できる
  • 再帰関数\( \mathrm{fun} f(x) \)の意味は
  1. \( f \)自身を呼ぶところを引数の関数に置き換えた関数\( \mathrm{fun} f'(x, f) \)をつくる
  2. \( \mathrm{fun} f(x) \)の意味 \( = \mathrm{fix}(f') \)
  3. \( \mathrm{fix}(f') \)は\( f_0 = f'(x, \mathrm{abort}) \)から初めて\( f_n = f'(x, f_{n-1}) \)を繰り返していった\( n \to \infty \)の極限

補足

  • 今回はプログラム意味論のうち表示的意味論(Denotational Semantics)について話しました
  • 他にも操作的意味論(Operational Semantics)公理的意味論(Axiomatics Semantics)などがあります
  • このスライドは簡単のため数学的な議論をすっ飛ばしています
    • 値って何?
    • ⊥って何?
    • 極限って何?存在するの?
  • ここら辺をしっかりやる数学の分野を領域理論(Domain Theory)と呼びます

プログラムの意味 is 何

By pandaman64

プログラムの意味 is 何

  • 1,067