Maude20100719

1,166 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,166
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
6
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Maude20100719

  1. 1. MaudeTerm Rewriting Logic簡単な紹介<br />2010/07/19<br />tmiya<br />
  2. 2. Maude<br />http://maude.cs.uiuc.edu/<br />Membership equational logic, Rewriting Logicに基づくプログラミング言語<br />OBJ系の仲間<br />宣言的。原理は明確でシンプルだが表現力は高い<br />でもパフォーマンスも出る<br />LTL -> Büchi automataの変換のJavaプログラムより3倍速かった<br />複数の用途<br />Deterministic:関数モジュール<br />Concurrent, nondeterministic:システムモジュール<br />OO:オブジェクトモジュール<br />何に使えるか<br />プログラミング:DSL。宣言的プログラミング。<br />形式仕様記述:代数的仕様記述<br />モデル検査:停止性とかCR性とか色々<br />
  3. 3. Maude<br />教科書<br />“All About Maude - A High-Performance Logical Framework: How to Specify, Program, and Verify Systems in Rewriting Logic” SpringerのLNCSシリーズ (2007)<br />Maude 2 Primer : PDF(2003)<br />A Maude Tutorial : PS(2000)<br />インストール<br />Core Maudeインストール<br />Linux on Intel, MacOS on Intel -> 実行可能バイナリあり<br />その他:ソースから頑張って<br />Full Maude : Maudeで書かれている<br />その他のツール<br />Inductive Theorem Prover<br />Maude Termination Tool : eq仕様の停止性<br />Church Rosser Checker : CR性の検査<br />Real-Time Maude : timed-rewriting, LTL, などなど。<br />
  4. 4. Peano自然数(定義)<br />Maude> fmod PEANO-NAT-EXTRA is // 関数モジュール<br />> sort Nat . // sortは型とか集合みたいなもの<br />> op 0 : -> Nat [ctor] . // [ctor]はコンストラクタ<br />> op s : Nat -> Nat [ctoriter] . // [iter]は繰り返し有り<br />> op _+_ : Nat Nat -> Nat . // _でプレースホルダ<br />> vars M N : Nat .<br />> eq 0 + N = N . // eqは書き換え規則<br />> eqs(M) + N = s(M + N) .<br />> endfm<br />Maude><br />
  5. 5. Peano自然数(簡約)<br />Maude> set trace on . // 変形をトレース表示<br />Maude> reduce s(0) + s(s(0)) .<br />reduce in PEANO-NAT-EXTRA : s(0) + s^2(0) .<br />*********** equation<br />eqs(M) + N = s(M + N) .<br />M --> 0<br />N --> s^2(0)<br />s(0) + s^2(0)<br />---><br />s(0 + s^2(0))<br />*********** equation<br />eq 0 + N = N .<br />N --> s^2(0)<br />0 + s^2(0)<br />---><br />s^2(0)<br />rewrites: 2 in 0ms cpu (0ms real) (9009 rewrites/second)<br />result Nat: s^3(0)<br />Maude> <br />
  6. 6. タバコ(定義)<br />Maude> mod CIGARETTE is // モジュール<br />> sort State .<br />> op c : -> State [ctor] . *** cigarette<br />> op b : -> State [ctor] . *** butt<br />> op __ : State State -> State [ctor assoc comm] .<br />> rl [smoke] : c => b . // 状態遷移規則<br />> rl [makenew] : bbbb => c .<br />> endm<br />Maude> <br />[assoc comm] : 結合則、可換則<br />項書き換えの無限ループを避け、効率化するため、明示的に指定<br />
  7. 7. タバコ(実行)<br />rewrite [100] in CIGARETTE : cccccccccccccccc .<br />*********** rule<br />rlc => b [label smoke] .<br />empty substitution<br />c<br />---><br />b<br />中略<br />*********** rule<br />rlbbbb => c [label makenew] .<br />empty substitution<br />ccccccccccccbbbb<br />---><br />(cccccccccccc) c<br />中略<br />*********** rule<br />rlc => b [label smoke] .<br />empty substitution<br />c<br />---><br />b<br />rewrites: 26 in 1ms cpu (1ms real) (14046 rewrites/second)<br />result State: b<br />Maude> <br />
  8. 8. アーケードクレーン(定義)<br />Maude> mod ARCADE-CRANE is<br />> protecting QID .<br />> sorts ToyID State .<br />> subsortQid < ToyID .<br />> op floor : ToyID -> State [ctor] .<br />> op on : ToyIDToyID -> State [ctor] .<br />> op clear : ToyID -> State [ctor] .<br />> op hold : ToyID -> State [ctor] .<br />> op empty : -> State [ctor] .<br />> op 1 : -> State [ctor] . *** identity state<br />> op _&_ : State State -> State [ctor assoc comm id: 1] .<br />> vars X Y : ToyID .<br />> rl [pickup] : empty & clear(X) & floor(X) => hold(X) .<br />> rl [putdown] : hold(X) => empty & clear(X) & floor(X) .<br />> rl [unstack] : empty & clear(X) & on(X,Y) => hold(X) & clear(Y) .<br />> rl [stack] : hold(X) & clear(Y) => empty & clear(X) & on(X,Y) .<br />> endm<br />Maude> <br />
  9. 9. アーケードクレーン(状態)<br />Maude> search in ARCADE-CRANE : empty & floor('mothergoose) & on('teddybear, 'mothergoose) & on('soccerball, 'teddybear) & clear('soccerball) & floor('dragondude) & clear('dragondude) =>+ empty & floor('teddybear) & on ('mothergoose,'teddybear) & on('soccerball,'mothergoose) & clear('soccerball) & floor('dragondude) & clear('dragondude) .<br />*********** rule<br />rl empty & floor(X) & clear(X) => hold(X) [label pickup] .<br />X --> 'dragondude<br />empty & floor('dragondude) & floor('mothergoose) & clear('dragondude) & clear(<br /> 'soccerball) & on('soccerball, 'teddybear) & on('teddybear, 'mothergoose)<br />---><br />(floor('mothergoose) & clear('soccerball) & on('soccerball, 'teddybear) & on(<br /> 'teddybear, 'mothergoose)) & hold('dragondude)<br />中略<br />*********** rule<br />rl empty & clear(X) & on(X, Y) => clear(Y) & hold(X) [label unstack] .<br />X --> 'soccerball<br />Y --> 'dragondude<br />empty & floor('teddybear) & clear('soccerball) & on('dragondude, 'mothergoose)<br /> & on('mothergoose, 'teddybear) & on('soccerball, 'dragondude)<br />---><br />(floor('teddybear) & on('dragondude, 'mothergoose) & on('mothergoose,<br /> 'teddybear)) & clear('dragondude) & hold('soccerball)<br />No more solutions.<br />states: 125 rewrites: 272 in 37ms cpu (391ms real) (7237 rewrites/second)<br />Maude> <br />
  10. 10. アーケードクレーン(状態)<br />Maude> search in ARCADE-CRANE : empty & floor('mothergoose) & on('teddybear,'mothergoose) & on('soccerball,'teddybear) & clear('soccerball) & floor('dragondude) & clear('dragondude) =>+ empty & floor('teddybear) & floor('mothergoose) & clear('teddybear) & clear('mothergoose) & X:State .<br />Solution 1 (state 10)<br />states: 11 rewrites: 16 in 0ms cpu (0ms real) (49844 rewrites/second)<br />X:State --> floor('dragondude) & clear('soccerball) & on('soccerball,<br /> 'dragondude)<br />Solution 2 (state 15)<br />states: 16 rewrites: 23 in 0ms cpu (0ms real) (40280 rewrites/second)<br />X:State --> floor('dragondude) & floor('soccerball) & clear('dragondude) &<br />clear('soccerball)<br />Solution 3 (state 33)<br />states: 34 rewrites: 54 in 1ms cpu (1ms real) (39560 rewrites/second)<br />X:State --> floor('soccerball) & clear('dragondude) & on('dragondude,<br /> 'soccerball)<br />No more solutions.<br />states: 125 rewrites: 272 in 5ms cpu (5ms real) (50670 rewrites/second)<br />Maude> <br />X<br />
  11. 11. 川渡しパズル(定義1)<br />Maude> mod RIVER-CROSSING is<br />> sorts Side Group .<br />> ops left right : -> Side [ctor] .<br />> op change : Side -> Side .<br />> eqchange(left) = right .<br />> eqchange(right) = left .<br />> ops swlc : Side -> Group [ctor] .<br />> op __ : Group Group -> Group [ctor assoc comm] .<br />> var S : Side .<br />> rl [shepherd] : s(S) => s(change(S)) .<br />> rl [wolf] : s(S) w(S) => s(change(S)) w(change(S)) .<br />> rl [lamb] : s(S) l(S) => s(change(S)) l(change(S)) .<br />> rl [cabbage] : s(S) c(S) => s(change(S)) c(change(S)) .<br />> endm<br />
  12. 12. 川渡しパズル(定義2)<br />Maude> mod RIVER-CROSSING-PROP is<br />> protecting RIVER-CROSSING .<br />> including MODEL-CHECKER .<br />> subsort Group < State .<br />> op initial : -> Group .<br />> eq initial = s(left) w(left) l(left) c(left) .<br />> ops disaster success : -> Prop .<br />> vars S S' S'' : Side .<br />> ceq (w(S) l(S) s(S') c(S'') |= disaster) = true if S =/= S' .<br />> ceq (w(S'') l(S) s(S') c(S) |= disaster) = true if S =/= S' .<br />> eq (s(right) w(right) l(right) c(right) |= success) = true .<br />> endm<br />Maude> <br />
  13. 13. 川渡しパズル(実行)<br />% ./maude.intelDarwin -interactive model-checker.maude<br />Maude> red modelCheck(initial,<br />> <> success -> (<> disaster / ((success) U disaster))) .<br />reduce in RIVER-CROSSING-PROP : modelCheck(initial, <> success -> <> disaster<br /> / (success U disaster)) .<br />rewrites: 76 in 1ms cpu (1ms real) (60995 rewrites/second)<br />result ModelCheckResult: counterexample({s(left) w(left) l(left) c(left),'lamb}<br /> {s(right) w(left) l(right) c(left),'shepherd} {s(left) w(left) l(right) c(<br />left),'wolf} {s(right) w(right) l(right) c(left),'lamb} {s(left) w(right)<br />l(left) c(left),'cabbage} {s(right) w(right) l(left) c(right),'shepherd} {<br />s(left) w(right) l(left) c(right),'lamb} {s(right) w(right) l(right) c(<br />right),'lamb} {s(left) w(right) l(left) c(right),'shepherd} {s(right) w(<br /> right) l(left) c(right),'wolf} {s(left) w(left) l(left) c(right),'lamb} {s(<br /> right) w(left) l(right) c(right),'cabbage} {s(left) w(left) l(right) c(<br />left),'wolf}, {s(right) w(right) l(right) c(left),'lamb} {s(left) w(right)<br />l(left) c(left),'lamb})<br />Maude> <br />
  14. 14. 触り始めたばかりだが<br />Maudeは割と面白い<br />普通に項書き換え系プログラミングとして遊べる<br />例えば型無しλ計算とか、当たり前だけど書ける<br />違和感があるところ<br />Subsorts Nat < NeList < List<br />Nat は要素1個の (Nat)のList である!<br />

×