Guarded Command Language
Guarded Command Language(GCL)とは、エドガー・ダイクストラが述語変換意味論向けに定義した言語である[1]
ガード付きコマンド
[編集 ]ガード付きコマンドはGCLの最重要要素である。ガード付きコマンドは名前の通り、ガードが付いている。ガードは一種の命題であり、その文を実行する前に真でなければならない。文の実行前に、そのガードは真であると仮定される。また、ガードが偽であった場合、その文は実行されない。ガード付きコマンドを使うことで、プログラムがその仕様に適合していることを証明することが容易になる。ガード付きコマンドの本体(実行すべき文)がガード付きコマンドになっていることが多い。
構文
[編集 ]ガード付きコマンドは、 {\displaystyle G\rightarrow {S}} という形式の文であり、ここで
- {\displaystyle G} は、ガードと呼ばれる命題である。
- {\displaystyle S} は文である。
{\displaystyle G\equiv {\mbox{True}}} なら、ガード付きコマンドは単に {\displaystyle S} と記述される。
意味論
[編集 ]計算の流れで {\displaystyle G} が出てくると、それを評価する。
- {\displaystyle G} が真なら {\displaystyle S} を実行する。
- {\displaystyle G} が偽なら、コンテキストに従って次にすべきことを決定する。
Skip と Abort
[編集 ]Skip と Abort は非常に単純だがガード付きコマンドと同様に重要な要素である。Abort は未定義命令であり、何もしない。Abort は完了することさえ保証されていない。プログラムが何らかの証明の定式化であるとすれば、Abort は証明の失敗に対応する。Skip は空の命令であり、何もしない。文法上、文が必要とされるが、プログラマが状態を変更したくない場合、プログラム内で使われる。
構文
[編集 ]- Skip
- Abort
意味論
[編集 ]- Skip: 何もしない
- Abort: 何もしない
代入
[編集 ]代入文は変数に値を代入する。
構文
[編集 ]- {\displaystyle v:=E}
- {\displaystyle v_{0},v_{1},...,v_{n}:=E_{0},E_{1},...,E_{n}}
ここで
- v はプログラムの変数(群)
- E は対応する変数と同じデータ型の式
連結
[編集 ]代入文はセミコロン(;)を間に書いて、並べて記述される。
条件文: if
[編集 ]選択(一般に、条件文とかif文と呼ばれる)はガード付きコマンドのリストであり、そのうちの1つの文が選択実行される。複数のガードが真である場合、実行する文はランダムまたは非決定的に選択される。どのガードも真でない場合、結果は未定義である。少なくとも1つのガードが真でなければならないので、空文の Skip を使うことが多い。
構文
[編集 ]- {\displaystyle {\mbox{if}}}
- {\displaystyle G_{0}\rightarrow S_{0}}
- {\displaystyle []\ G_{1}\rightarrow S_{1}}
- {\displaystyle ...}
- {\displaystyle []\ G_{n}\rightarrow S_{n}}
- {\displaystyle {\mbox{fi}}}
意味論
[編集 ]- どのガードも真でない場合: Abort と同じ
- 1つの {\displaystyle G_{x}} だけが真の場合、{\displaystyle S_{x}} を実行
- {\displaystyle G_{x_{0}}\ ...\ G_{x_{m}}} がいずれも真の場合、任意の {\displaystyle S_{x_{y}}}(ただし {\displaystyle 0\leq y\leq m})を実行
例
[編集 ]単純な例
[編集 ]以下のような擬似コードを考える:
- if a < b then c := True
- else c := False
GCL ではこれが以下のようになる:
- {\displaystyle {\mbox{if}}}
- {\displaystyle a<b\rightarrow c:=True}
- {\displaystyle []\ a\geq b\rightarrow c:=False}
- {\displaystyle {\mbox{fi}}}
スキップを使用した例
[編集 ]擬似コード:
- if error = True then x := 0
GCLの場合:
- {\displaystyle {\mbox{if}}}
- {\displaystyle error={\mbox{True}}\rightarrow x:=0}
- {\displaystyle []\ error={\mbox{False}}\rightarrow {\mbox{skip}}}
- {\displaystyle {\mbox{fi}}}
2つ目のガードを省いた場合、error = False なら、結果は Abort となる。
複数のガードが真の場合
[編集 ]- {\displaystyle {\mbox{if}}}
- {\displaystyle a\geq b\rightarrow max:=a}
- {\displaystyle []\ a\leq b\rightarrow max:=b}
- {\displaystyle {\mbox{fi}}}
a = b の場合、max の新たな値として a または b が選ばれるが、結果は同じである。しかし、実装によっては、一方がもう一方より性能的に有利だったり容易だったりする場合もある。プログラマから見れば違いはないので、いかようにも実装してよい。
繰り返し: do
[編集 ]繰り返しは、全てのガードが真でなくなるまでガード付きコマンド群を繰り返し実行する。通常、ガードはひとつだけである。
構文
[編集 ]- {\displaystyle {\mbox{do}}}
- {\displaystyle G_{0}\rightarrow S_{0}}
- {\displaystyle []\ G_{1}\rightarrow S_{1}}
- {\displaystyle ...}
- {\displaystyle []\ G_{n}\rightarrow S_{n}}
- {\displaystyle {\mbox{od}}}
意味論
[編集 ]- 全てのガードが真でない場合: Skip
- {\displaystyle G_{x}} だけが真の場合: {\displaystyle S_{x}} を実行し、再度ガード群を評価する
- {\displaystyle G_{x_{0}}\ ...\ G_{x_{m}}} が真の場合: いずれかの {\displaystyle S_{x_{y}}} (ただし、{\displaystyle 0\leq y\leq m})を実行し、再度ガード群を評価する
例
[編集 ]ユークリッドの互除法
[編集 ]- {\displaystyle a,b:=A,B;}
- {\displaystyle {\mbox{do}}}
- {\displaystyle a>b\rightarrow a:=a-b}
- {\displaystyle []\ b>a\rightarrow b:=b-a}
- {\displaystyle {\mbox{od}}}
この繰り返しは a = b のとき終了し、その際に a と b には A と B の最大公約数が格納されている。
ユークリッドの互除法の拡張
[編集 ]- {\displaystyle a,b,x,y,u,v:=A,B,1,0,0,1;}
- {\displaystyle {\mbox{do}}}
- {\displaystyle b\neq 0\rightarrow }
- {\displaystyle q,r:=a\ {\mbox{div}}\ b,a\ {\mbox{mod}}\ b;}
- {\displaystyle a,b,x,y,u,v:=b,r,u,v,x-q*u,y-q*v}
- {\displaystyle b\neq 0\rightarrow }
- {\displaystyle {\mbox{od}}}
この繰り返しは b = 0 のとき終了し、その際に各変数は xa + yb = gcd(a,b) の解を格納している。
応用
[編集 ]非同期回路
[編集 ]GCLでは、異なるコマンドの選択による様々な遅延を許容した繰り返しが可能であるため、QDIモデル(Quasi Delay Insensitive)に基づいた回路設計に適している。この場合、以下のようにノード y を駆動する論理ゲートは2つのガード付きコマンドで表現される。
- {\displaystyle PulldownGuard\rightarrow y:=0}
- {\displaystyle PullupGuard\rightarrow y:=1}
PulldownGuard と PullupGuard はその論理ゲートの入力の関数であり、それぞれゲートの出力を上げたり下げたりする。古典的な回路評価モデルとは異なり、(非同期回路に対応した)ガード付きコマンド群の繰り返しは、正確にその回路の全ての動的振る舞いを説明できる。モデルによっては、ガード付きコマンドに何らかの制限を加える必要があるだろう。典型的な制限としては、安定性、非干渉、自己無効化コマンドを許さない、などがある[2] 。
モデル検査
[編集 ]ガード付きコマンドは Promela というプログラミング言語で使われている。Promela はSPINモデルチェッカで使われている。SPINは並行ソフトウェアアプリケーションのモデル検査用ツールである。
その他
[編集 ]Perlの Commands::Guarded モジュールは、ダイクストラのガード付きコマンドを決定論的に修正したバージョンである。
参考文献
[編集 ]- ^ Dijkstra, Edsger W. "EWD472: Guarded commands, non-determinacy and formal. derivation of programs.". 2006年8月16日閲覧。
- ^ Martin, Alain J. "Synthesis of Asynchronous VLSI Circuits". 2008年12月7日閲覧。