chapter 4. denotational semantics - semantics with...
TRANSCRIPT
. . . . . .
Chapter 4. Denotational Semantics
Chapter 4. Denotational SemanticsSemantics With Applications
木谷有沙
お茶の水女子大学 人間文化創成科学研究科 理学専攻
November 27, 2008
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
1 はじめに
2 4.1 Direct style semantics: specificationRequirements on fixed point
3 4.2 Fixed point theoryComplete partially ordered setsContinuous functions
4 最後に
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
はじめに
はじめに
前章までで扱った operational semanticsでは、「プログラムがどのように実行されるか」に関心を置いていた。
本章で扱う denotational semanticsではそれよりも、「プログラムの実行によってもたらされる結果」、すなわち初期状態と最終状態の間にある関係に着目する。
そのため、各 syntactic category(統語範疇?)に対しsemantic function(意味関数?)を定義することを考える。
semantic functionとは、構文の各要素を、その実行結果を表現する数学的オブジェクト(たいていは関数)にマッピングするもの。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
はじめに
Denotational Semanticsの特徴
semantics functionは compositionalに定義される。つまり、
syntactic categoryの各基本要素に対し semantic clause(意味項?)がある
複合要素に対しては、その構成要素に適用される semanticfunctionによって、 semantic clauseが定義される。
1章で定義したA,Bは denotational definition.Sns, Ssosは compositionalでないため、denotational definitionではない。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
4.1 Direct style semantics : specification
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
定義
Sの実行によってもたらされる結果とは、stateの変化。よって、Sの意味を以下のような state上の部分関数として定義する。Sds : Stm→ (State ↪→ State)表 4.1にWhileの denotational semanticsがまとめられている。
condと FIXという補助関数を用いている。(詳細は後で説明)
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
表 4.1の説明 1 (skip, sequencing)
skipの項で使われている関数 idは State上の恒等関数Sds[skip]s = s
S1; S2の実行結果は、S1の実行結果と S2の実行結果の合成。したがって関数合成の定義より、S1か S2のどちらかがundefinedだと、S1; S2の結果も undefined.(p.86下部参照)
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
表 4.1の説明 2 (conditional)
条件文では、以下のような補助関数 condが用いられている。cond : (State→ T) × (State ↪→ State) × (State ↪→ State)
→ (State ↪→ State)
cond(p, g1, g2)s =
g1 s if p s= tt
g2 s if p s= ff選ばれたほうの分岐の結果が undefinedであれば、条件文全体も undefined.B[b]が全体関数なので、条件部分が undefになることはない。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
表 4.1の説明 3 (while)while b do Sの結果は次と等しくなるはず。if b then (S; while b do S) else skipこれを Sdsの既に定義済みの部分を使って書くとSds[while b do S] =
cond(B[b], Sds[while b do S] ◦ Sds, id) ...(*)(*)は Sdsが compositionalでないためこのまま定義として使うことはできないが、Sds[while b do S] が次のような関数Fの不動点 (fixed point)であるということを表している。
F g = cond(B[b], g ◦ Sds, id)つまり Sds[while b do S] = F (Sds[while b do S])
これを使って表 4.1の compositionalな定義を得ている。補助関数 FIXは不動点を返す
FIX : ((State ↪→ State)→ (State ↪→ State))→ (State ↪→ State)
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Example 4.1
while ¬(x = 0) do skipこれに対応する関数 F′ は次のように定義される。
(F′ g) s =
g s if sx , 0
s if sx = 0このとき、関数 g1を
g1 s =
undef if sx , 0
s if sx = 0と定義すると、
(F′ g1) s =
g1 s if sx , 0
s if sx = 0=
undef if sx , 0
s if sx = 0= g1 s
より、g1は F′ の不動点である。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Example 4.1
同様に、以下のような g2を考えてみる。g2 s = undef for all s s′ = xとなる s′ において、(F′ g2) s′ = s′ となる一方で g2 s′ =undefとなるためg2は F′ の不動点ではない。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
この定義の問題点
じつはこれだけでは Sds[while b do S] を満足に定義できない。二つの問題点がある。
二つ以上不動点をもつ関数がある
不動点を一つも持たない関数がある
次節でこれらの問題の解決する。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Exercise 4.2, 4.3
Exercise 4.2while ¬(x = 0) do x := x-1これに対応する関数 F を求めよ。また、 g1から g5のうちF の不動点であるものはどれか。
Exercise 4.3while ¬(x = 0) do (y := y*x; x:= x-1)これに対応する関数 F と、その不動点を二つ以上求めよ。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
Requirements on fixed theory
前述の二つの問題点を解決するために、次のようなことを行う。
不動点を選ぶ際に必要となる条件を課し、それらを満たす不動点は高々一つであることを示す。
Whileのプログラムの関数 F は必ず上の条件を満たす不動点を持つことを示す。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
3つのケース
条件を選びだすために、state s0からの while b do Sの実行結果を考えてみる。すると次の三つが考えられる。
A 終了する。
B localにループ。
C globalにルーブ。
各ケースにおいて、関数 Fとその不動点について何が言えるかをを調べていく。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
The case A
while b do Sが終了する。つまり、次のような state s1, s2, ...sn
がある。
B[b] si =
tt if i < n
ff if i = nかつSds si = si+1 for i < nこれを満たす例 (xは正の値を持つ)while 0 ≤ x do x := x - 1
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
The case A
goが Fの不動点 (F g0 = g0を満たす)だとすると i<nのときg0 si = (F g0) si
= cond(B[b], go ◦ Sds[S], id) si
= go (Sds[S] si)= go si+1
i=nのときg0 sn = (F g0) sn
= cond(B[b], go ◦ Sds[S], id) sn
=id sn
= sn
よって関数 Fの全ての不動点 g0が以下の式を満たす。g0 s0 = sn
ケース Aでは、不動点を選ぶ際に必要な条件は見つからなかった。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
The case B
while b do Sがローカルにループする。つまり、次のようなstate s1, s2, ...snがある。B[b] si = tt for i ≤ nかつ
Sds[S] si =
si+1 for i < n
undef for i = nこれを満たす例 (xは正の値を持つ)while 0 ≤ x do (if x=0 then (while true then do skip)
else x := x - 1)
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
The case B
goが Fの不動点 (F g0 = g0を満たす)だとすると i<nのときg0 si = g0 si+1
i=nのときg0 sn = (F g0) sn
= cond(B[b], go ◦ Sds[S], id) sn
= (go ◦ Sds[S]) sn
= undefよって関数 Fの全ての不動点 g0は以下の式を満たす。
g0 s0 = undefケース Bでも、不動点を選ぶ際に必要な条件は見つからなかった。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
The case C
while b do Sがグローバルにループする。つまり、次のようなstate s1, s2, ...がある。B[b] si = tt for all iかつSds[S] si = si+1 for all iこれを満たす例 (xは 0でない)while ¬(x=0) do skip
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
The case C
goが Fの不動点 (F g0 = g0を満たす)だとすると、i≥0をみたす全ての iに対し
g0 si = g0 si+1
よってg0 s0 = g0 si for all i
となり、 g0 soが定まらない。これは F が複数の異なる不動点を持つということ。これは特に驚くべきことではなく、例えば Example 4.1は
(F′ g) s =
g s if s x , 0
s if s x = 0という関数 F′ を持っていたが、「s x = 0のとき g s= s」さえ満たせば、どんな State ↪→ Stateの部分関数 gも F′の不動点となれる。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
The case C
とはいえ経験上、ループを区別するために、次のものが欲しくなる。
Sds[while¬(x=0) do skip] so =
undef if s0 x , 0
s0 if s0 x = 0よって、より好ましい F′の不動点とは以下のように定義された関数 g0である。
g0 s =
undef if sx , 0
s if sx = 0この g0と F′の他の不動点 g′との違いは、g0 s = s′であればg′ s = s′が成り立つが、逆は成り立たないところである。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
Generalizing
ここまでの話を一般化すると、欲しい不動点 FIX F は次の条件を満たす State ↪→ Stateの部分関数 g0ということになる。
g0は F の不動点。つまり F g0 = g0
gを F の他の不動点としたとき、全ての s, s′に対し、g0 s = s′ならば g s= s′
(注意:g0 s = undefならば g sは何でも良い)
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.1 Direct style semantics: specification
Requirements on fixed point
Exercise 4.4, 4.5
Exercise 4.4Exercise 4.2で求めた不動点のうち、先の条件を満たす不動点はどれか。
Exercise 4.5Exercise 4.3で求めた関数に対し、先の条件を満たす不動点を求めよ。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
4.2 Fixed point theory
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
4.2 Fixed point theory
条件を満たす不動点が存在することを保証する前に、もう少し形式的に FIX F の条件を定義し直そう。まず FIX F が他の全ての不動点とその結果を共有しているという条件から定式化する。そのために、 State ↪→ Stateの部分関数に順序 vを定義する。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
vの定義
任意の s, s′ に対してg1 s = s′ ならば g2 s = s′
が成り立つとき、g1 v g2
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Example 4.6, Exercise 4.7, 4.8
Example 4.6vの例。Exercise 4.7vを使って書かれた条件を満たす関数を定義する問題。Exercise 4.8vについては次のようにも書けます。g1 v g2 if and only if graph(g1) ⊆ graph(g2)これが本当に正しいことを示しなさい。(関数 graphについては p.214参照)
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
partial order
集合 Dと D上の関係 vD が以下を満たす時、組 (D,vD)を半順序集合という。
d vD d (reflexivity)
d1 vD d2 and d2 vD d3ならば d1 vD d3 (transitivity)
d1 vD d2 and d2 vD d1ならば d1 = d2 (anti-symmetry)
関係 vD を D上の半順序と言い、しばしば添字の Dを省いて vと書かれる。場合によっては wも使う。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
least element
d v d′ for all d′ of Dを満たす Dの要素 dを、Dの least elementと呼び、「何の情報も含んでいない」と言う。
Fact 4.9半順序集合 (D,v)が least element dを持っているとき、dは一意に定まる。
背理法で証明できる
Dの least elementを ⊥D もしくは ⊥と表記する。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Example 4.10, Exercise 4.11, 4.12
Example 4.10Sは、次を満たす空でない集合。P = {K|K ⊆ S}このとき (P,⊆)が半順序集合であることの証明と、そのleast element
Exercise 4.11(P,⊇)が半順序集合であることを証明し、その least elementを求めよ。
Exercise 4.12Sは、次を満たす空でない集合。P f in = {K|K is finite andK ⊆ S}このとき、(P f in ,⊆), (P f in ,⊇)が半順序集合であることを証明せよ。また、これらは least elementをもつか答えよ。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Lemma 4.13
(State ↪→ State, v)は半順序集合である。⊥ s = undef for all sを満たす部分関数 ⊥ : State ↪→ Stateは、State ↪→ Stateの least elementである。
証明
vが半順序の三つの条件を満たす事を示す⊥が State ↪→ Stateの least elementであることを示す
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
more precise statement of the requirements to FIX F
部分関数上の順序を導入して、FIX F のより正確な定義を得られるようになった。
FIX F は F の不動点。つまり F (FIX F) = FIX F
FIX F は F の least fixed point. つまりF g = gならば FIX F v g
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Exercise 4.14, 4.15
Exercise 4.14Fact4.9と同様に、F が least fixed point g0を持つならば g0
は一意であることを示せ。
Exercise 4.15Exercise 4.2, 4.3の関数の least fixed pointを求めよ。また、Exercise 4.4, 4.5と比較せよ。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Complete partially ordered sets
Least upper bound
(D,v)を半順序集合、Yをその部分集合としたとき、次のような Dの要素 dを Yの upper boundという。∀d′ ∈ Y. d′ v d
以下を満たす Yの upper bound dを least upper bondという。
d′ が Yの upper bound⇒ d v d′
Exercise 4.16Fact. 4.9と同様に、Yが least upper bound dを持つならば dは一意であることを示せ。
Yが least upper boundをもつならば、それを⊔
Yと表記する。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Complete partially ordered sets
chain
次を満たす Yを chainと呼ぶ。∀d1, d2 ∈ Y. d1 v d2 or d2 v d1
Example 4.17Example 4.10の (P({a, b, c}),⊆)の部分集合で chainになる例とならない例と、それぞれの least upper bound。
Exercise 4.18(P(S),⊆)の空でない部分集合は必ず least upper boundを持つ事を証明せよ。(P(S),⊇)についても同様に証明せよ。
Exercise 4.19(P f in(S),⊆)が、least upper boundがない chainを持つような Sの例を挙げよ。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Complete partially ordered sets
Example 4.20, Exercise 4.21, 4.22
Example 4.20
Exercise 4.21
Exercise 4.22
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Complete partially ordered sets
chain complete
半順序集合 (D,v)の全ての chain Yに対し常に⊔
Yが存在するとき、半順序集合 (D,v)を chain complete (ccpo)であるという。Dの全ての部分集合 Yに
⊔Yが存在するときは、complete
lattice(完備束)であるという。
Example 4.23
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Complete partially ordered sets
Fact 4.24, Lemma 4.25
Fact 4.24 (D,v)が ccpoであれば、⊥ = ⊔ ∅で与えられるleast element ⊥を持つLemma 4.25(State ↪→ State,v)は ccpo.Yの least upper bound
⊔Yは
graph(⊔
Y) =⋃{ graph(g) |g ∈ Y}
で与えられる。つまり、(⊔
Y)s = s′ ⇔ g s= s′ for some g ∈ Y
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Continuous functions
monotone
(D,v), (D′,v′)は ccpoであるとし、関数 f : D → D′ を考える。すべての d1, d2に対し
d1 v d2 ⇒ f d1 v′ f d2
となるとき、 f は monotone(単調)であるという。
Example 4.26
Exercise 4.27
Exercise 4.28
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Continuous functions
properties of monotone functions
Fact 4.29(D,v), (D′,v′), (D′′,v′′)は ccpoであるとし、関数f : D → D′, f ′ : D′ → D′′ は monotoneであるとする。このとき、 f ′ ◦ f : D → D′′ も monotone.
Lemma 4.30(D,v), (D′,v′)は ccpoであるとし、関数 f : D → D′ はmonotoneであるとする。このとき、Yが Dでの chainであれば、{ f d|d ∈ Y}は D′ での chainであり、さらに⊔′{ f d|d ∈ Y} v′ f (
⊔Y)
一般に、⊔′{ f d|d ∈ Y} = f (
⊔Y)であるとは期待できない。
Example 4.31⊔′{ f d|d ∈ Y} = f (⊔
Y)でない例
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Continuous functions
continuous functions
ccpoである (D,v)と (D′,v′)上の関数 f : D → D′ がmonotoneで、かつ Yの全ての空でない chainについて⊔′{ f d|d ∈ Y} = f (
⊔Y)
であるとき、この関数 f を continuous function(連続関数)という。空の chainに対しても
⊔′{ f d|d ∈ Y} = f (⊔
Y) であれば、 f は strictであるという。
Example 4.32
Exercise 4.33
Exercise 4.34
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Continuous functions
property of continuous functions
Lemma 4.35(D,v), (D′,v′), (D′′,v′′)は ccpoであるとし、関数f : D → D′, f ′ : D′ → D′′ は continuousであるとする。このとき、 f ′ ◦ f : D → D′′ も continuous.
Exercise 4.36
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Continuous functions
define the required fixed point operator FIX !
Theorem 4.37関数 f : D → Dは、(D,v) 上で least element ⊥を持つcontinuous functionであるとする。このとき、
FIX f =⊔{ f n⊥|n ≥ 0}
は Dの要素であり、 f の least fixed point.
Example 4.38
Exercise 4.39
Exercise 4.40
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Continuous functions
Fixed Point Theory
least fixed pointの存在を示すためにしたことのまとめ。
1 chain complete partially ordered sets (ccpo)に限定する。
2 continuous functionに限定する。
3 ccpo上の continuous functionは必ず least fixed pointを持つことを示す。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
4.2 Fixed point theory
Continuous functions
Exercise 4.41, 4.42
Exercise 4.41
Exercise 4.42
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
最後に
まとめ
Denotational semanticsでWhileを定義した。
不動点を求める関数 FIXを使う事で、compositionallyに定義している。
関数 FIXで求める不動点の満たすべき条件について考え、FIXを定義した。
木谷有沙 Chapter 4. Denotational Semantics
. . . . . .
Chapter 4. Denotational Semantics
最後に
おしまい
おわりだよ!来週につづく。
木谷有沙 Chapter 4. Denotational Semantics