Upgrade to Pro — share decks privately, control downloads, hide ads and more …

アクターモデル

ytakano
February 02, 2018

 アクターモデル

ytakano

February 02, 2018
Tweet

More Decks by ytakano

Other Decks in Programming

Transcript

  1. 構⽂ 5 w ஫ऍ w ΞτϜͱ͸EFpOFఆ਺ͷΑ͏ͳ΋ͷ w ϖΞͷQS  

    ͸ɺλϓϧͷ   ͷΑ͏ͳ΋ͷ w JTQS ͸ϖΞ͔Ͳ͏͔Λݕࠪ͢Δؔ਺ w TU OE͸ɺϖΞͷ൪໨ɺ൪໨ͷཁૉΛऔಘ͢ Δؔ਺ A = {true, false, nil, . . . } ΞτϜ N = {0, 1, 2, . . . } ࣗવ਺ X = {x, y, z, . . . } ม਺ F = {+, , =, ispr?, 1st, 2nd, . . . } ϓϦϛςΟϒԋࢉࢠ V ::= ஋ A | N | X | X.E ϥϜμந৅ | pr(V, V) ϖΞίϯετϥΫλ E ::= ࣜ V | pr(E, E) ϖΞίϯετϥΫλ | E(E) ؔ਺ద༻ | F(E, . . . , E) ϓϦϛςΟϒؔ਺ద༻ | br(E, E, E) ৚݅෼ذ | letrec X = E in E ࠶ؼఆٛ | send(E, E) ϝοηʔδૹ৴ | new(E) ΞΫλʔੜ੒ | ready(E) ৼΔ෣͍มߋ
  2. 糖⾐構⽂ 6 let x = e1 in e2 x.e2(e1) seq(e1,

    e2) let z = e1 in e2 ͨͩ͠ z ͸৽ن seq(e1, . . . , en) seq(e1, seq(e2, . . . , seq(en 1, en)) . . . ) ͨͩ͠ n 3 if(e1, e2, e3) br(e1, z.e2, z.e3)(nil) ͨͩ͠ z ͸৽ن rec(f) x.f( y.x(x)(y))( x.f( y.x(x)(y))) • 注釈 • let x = 1, y = 2 in e というように、複数定義も可能 • この場合は、λy.λx.e(2)(1) と等価 • rec(f)はZコンビネータ(不動点コンビネータ)
  3. 簡約コンテキスト 11 ͋ΔΞΫλʔࣜ e ͸ҎԼͷΑ͏ʹදݱ͞ΕΔɻ e = R ӈลͷදݱΛ؆໿ίϯςΩετͱ͍͍ɺ͸ࣜ e

    ͷ࣍ʹධՁ͞ΕΔࣜͰ͋Γɺ R ͸ࣜ e தͷ ʹ૬౰͢Δ෦෼Λ Ͱஔ͖׵͑ͨ΋ͷͰ͋Δɻ ྫ send(new(e), 10) = send( , 10) new(e)
  4. 簡約可能な式と簡約コンテキストの構⽂ 12 Er ::= ؆໿Մೳͳࣜ V(V) ϥϜμద༻ | F(V, .

    . . , V) ؔ਺ద༻ | br(V, E, E) ৚࣮݅ߦ | letrec X = V in E ࠶ؼఆٛ | send(V, V) ϝοηʔδૹ৴ | new(V) ΞΫλʔੜ੒ | ready(V) ৼΔ෣͍มߋ R ::= ؆໿ίϯςΩετ Hole | pr(V, R) ϖΞίϯετϥΫλ | pr(R, E) ϖΞίϯετϥΫλ | V(R) ϥϜμద༻ | R(E) ϥϜμద༻ | F(V, . . . , V, R, E, . . . , E) ؔ਺ద༻ | br(R, E, E) ৚࣮݅ߦ | letrec X = R in E | send(V, R) ϝοηʔδૹ৴ | send(R, E) ϝοηʔδૹ৴ | new(R) ΞΫλʔੜ੒ | ready(R) ৼΔ෣͍มߋ
  5. 拡張ラムダ計算の簡約規則 13 x.e(v) e{x/v} f(v1, . . . , vn)

    v ͨͩ͠ɺ f F, v = [[f]](v1, . . . , v2) br(true, e, ) e br(false, , e) e 1st(pr(v, )) v 2nd(pr( , v)) v letrec x = v in e e{x/v{x/(letrec x = v in v)}}
  6. 操作的意味論 14 e e , [R e ]a || µ

    [fun:a] , [R e ]a || µ , [R new(b) ]a || µ [new:a,a ] , [R a ]a, [ready(b)]a || µ ͨͩ͠ɺ a ͸৽ن , [R send(a , v) ] || µ [snd:a] , [R nil ]a || µ { a v } , [R ready(b) ]a || { a v } µ [rcv:a,v] , [b(v)]a || µ
  7. 簡約の例 15 a b 10 [ ready(e1) ]a, [seq( ,

    ready(e2)) send(a, 10)) ]b || [snd:a] [ ready(e1) ]a, [seq( , ready(e2)) nil) ]b || a 10 [ ready(e1) ]a, [ ready(e2) ]b || a 10 [rcv:a,10] [ e1(10) ]a, [ ready(e2) ]b || (seqの箇所は⼀部省略)
  8. 計算⽊ (computation tree) 16 κ0 κ1 κ1ʼ κ2 κ2ʼ κ2ʼʼ

    κ3 計算⽊の例 l1 ΛΞΫλʔίϯϑΟάϨʔγϣϯͱ͢Δͱ͖ɺܭࢉ໦ ( ) ͸ɺز͔ͭͷ n N ʹର͢Δɺશͯͷ ༗ݶϥϕϧ෇͖ભҠྻ [ i li+1 i+1 |i < n] ͷू߹Ͱ͋Δɻ͜͜Ͱɺ = 0 Ͱ͋Γɺ N ͸൒ॱংू߹Ͱ͋Δɻ l1ʼ l2 l2ʼ l2ʼʼ l3 N = {0, 1, 1 , 2, 2 , 2 , 3} 0 < 1, 0 < 1 , 1 < 2, 1 < 2 , 1 < 2 , 2 < 3 ൒ॱংू߹ͷԼʹఆٛ͞ΕΔྻΛܭࢉྻ (computation sequence ) ͱݺͼɺ શॱংू߹΋ͱʹఆٛ͞ΕΔྻΛܭࢉ࿏ (computation path) ͱݺͿɻ 計算列 計算路 ( ) ΛɺΞΫλʔίϯϑΟάϨʔγϣϯ ͔Β ౸ୡՄೳͳύεͷશू߹ͱදهɻ
  9. 公平性 17 ͋ΔભҠϥϕϧ l ͕༗ޮͰ͋ΔͨΊͷඞཁे෼৚݅͸ɺ l ͱͳΔ ͕ଘࡏ͢Δ͜ͱͰ͋Δɻ ͜͜Ͱɺ͋ΔભҠϥϕϧ [rcv

    : a, v] ͕ӬଓతʹແޮԽ͞ΕΔͷ͸ɺ ΞΫλʔ a ͕ೋ౓ͱϝοηʔδͷड৴ঢ়ଶʹͳΒͳ͍ͱ͖Ͱ͋Δɻ ͋Δܭࢉ໦ ( ) தͷɺ͋Δܭࢉ࿏ = [ i li+1 i+1 |i < ] ͕ެฏͰ͋Δͱ͸ɺ ͦΕͧΕͷ༗ޮͳભҠϥϕϧ͕࠷ऴతʹى͖Δɺ΋͘͠͸ӬଓతʹແޮԽ͞ΕΔͱ͖Ͱ͋Δɻ ( ) தͷશͯͷެฏͳܭࢉ࿏ͷू߹Λ F( ) ͱදهɻ
  10. 公平性の例 18 A セマフォ B acquire acquire A ok A

    計算 セマフォ release A セマフォ acquire A ok time ・ ・ ・ Aしか実⾏できない公平でない計算路 (飢餓状態) A S B acquire acquire A ok 終了 計算 Aしか実⾏できないが公平な計算路 Sは永続的にメッセージを⾮受信状態に移⾏
  11. 例:排他実⾏ 19 sem = rec(λb.λh.λm. if(get?(m), if(h = nil, seq(send(cust(m),

    true), ready(b(cust(m)))), seq(send(cust(m), false), ready(b(h))), if(release?(m), ready(b(nil)), ready(b(h))))) customer = rec(λb.λself.λs.λm. seq(if(m, seq(/* critical section */, send(s, mkrelease(self))), send(s, mkget(self))), ready(b(self)(s)))) // 補助関数 mkget = λh.pr(true, h) mkrelease = pr(false, nil) get? = λm.if(ispr?(m), 1st(m), false) release? = λm.if(ispr?(m), not(1st(m)), false) cust = λm.2nd(m) // 消費者が2⼈で実⾏する例 letrec s = new(sem(nil)), a = new(customer(a)(s)), b = new(customer(b)(s)) in seq(send(a, false), send(b, false))