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

Egisonパターンマッチによる彩色

 Egisonパターンマッチによる彩色

Egison Workshop 2019
https://connpass.com/event/150001/

での講演スライド

Naoya Umezaki

November 24, 2019
Tweet

More Decks by Naoya Umezaki

Other Decks in Programming

Transcript

  1. ূ໌ ϊʔυͷݸ਺ʹ͍ͭͯͷؼೲ๏ Lemma ฏ໘άϥϑʹ͸ඞͣ࣍਺ 5 ҎԼͷϊʔυ͕ଘࡏ͢Δ ิ୊ΑΓ࣍਺ 5 ҎԼ͕ଘࡏ͢ΔͷͰɺͦͷΑ͏ͳϊʔυ v

    ΛҰͭ બͿɻv Λআ͍ͨάϥϑ͸ϊʔυͷݸ਺͕ҰͭݮΔͷͰؼೲ๏ͷ Ծఆ͔Β৚݅Λຬͨ͢Α͏ʹ࠼৭Մೳɻ 8
  2. 1 ͱ 3 ͷΈΛ୧ͬͯͭͳ͙͜ͱ͕Ͱ͖ͳ͍৔߹ ϊʔυ 1 ͔Β৭ 3 ͱ৭ 1

    ͷΈΛ௨ͬͯͰ͖Δ෦෼ʹ͍ͭͯ৭ 1 ͱ ৭ 3 ΛೖΕସ͑Δɻ͢ΔͱબΜͩϊʔυͷपΓͷ৭͕ 4 ৭ʹͳΔ ͷͰ࠼৭Մೳɻ 11
  3. 1 ͱ 3 ͷΈΛ୧ͬͯͭͳ͙͜ͱ͕Ͱ͖Δ৔߹ ฏ໘άϥϑͳͷͰϊʔυ 2 ͱϊʔυ 4 ͕৭ 2

    ͱ৭ 4 ͷΈͰܨ͕Β ͳ͍ɻϊʔυ 2 ͔Β৭ 2 ͱ৭ 4 ͷΈΛ௨ͬͯͰ͖Δ෦෼ͷ৭ͷ 2 ͱ 4 ΛೖΕସ͑·͢ɻ͢ΔͱબΜͩϊʔυͷपΓͷ৭͕ 4 ৭ʹͳ ΔͷͰ࠼৭ՄೳͰ͢ɻ 12
  4. άϥϑσʔλͷྫ 1 (define $graph-data 2 {[[1 Nothing] {[2 Nothing]}] 3

    [[2 Nothing] {[1 Nothing]}]}) 4 5 (define $graph-data2 6 {[[1 Nothing] {[2 (Just 1)]}] 7 [[2 (Just 1)] {[3 (Just 2)] [1 Nothing]}] 8 [[3 (Just 2)] {[4 (Just 1)] [2 (Just 1)]}] 9 [[4 (Just 1)] {[5 (Just 2)] [3 (Just 2)]}] 10 [[5 (Just 2)] {[4 (Just 1)]}]}) 13
  5. node Ϛονϟʔ 1 (define $node [integer (maybe integer)]) 2 (define

    $graph (set [node (list node)])) node Ϛονϟʔ͸ integer ͱ maybe integer ͷ tuple ͱͯ͠ ఆٛ͠·͢ɻ࣮ࡍͷσʔλͱͯ͠͸ɺ࠷ॳͷ integer ͕ϊʔυͷ id ͰɺޙΖͷ maybe integer ͸ͦͷϊʔυͷ৭Ͱ͢ɻ৭ͷ໊લ Λ੔਺ʹ͓͖ͯ͠ɺ৭͕ృΒΕ͍ͯΔ৔߹ʹ͸ Just integer Ͱ ৭͕ృΒΕ͍ͯͳ͍৔߹͸ Nothing Ͱ͢ɻ 14
  6. graph Ϛονϟʔ 1 (define $node [integer (maybe integer)]) 2 (define

    $graph (set [node (list node)])) graph Ϛονϟʔ͸্Ͱఆٛͨ͠ node ͱ node ͷ list ͷ tuple ͷ set ͱͯ͠ఆٛ͠·͢ɻάϥϑͷ֤ϊʔυ͔Βܨ͕͍ͬͯΔ ϊʔυͨͪͷ৘ใΛ list node ͱͯ͠ɺͦΕΛूΊͨ΋ͷ͕άϥ ϑͰ͢ɻ 15
  7. Ϛονϟʔͷ࢖༻ྫ 1 (define $graph-data2 2 {[[1 Nothing] {[2 (Just 1)]}]

    3 [[2 (Just 1)] {[3 (Just 2)] [1 Nothing]}] 4 [[3 (Just 2)] {[4 (Just 1)] [2 (Just 1)]}] 5 [[4 (Just 1)] {[5 (Just 2)] [3 (Just 2)]}] 6 [[5 (Just 2)] {[4 (Just 1)]}]}) 7 ৭ 1 ͷϊʔυ id ΛશͯऔΓग़͢ίʔυ͸ҎԼͷ௨ΓͰ͢ɻ 1 (define $c (Just 1)) 2 (match-all graph-data2 graph 3 [<cons [[$id ,c] _] _> id]) 4 ;{2 4} 5 16
  8. Ϛονϟʔͷ࢖༻ྫ 1 (define $graph-data2 2 {[[1 Nothing] {[2 (Just 1)]}]

    3 [[2 (Just 1)] {[3 (Just 2)] [1 Nothing]}] 4 [[3 (Just 2)] {[4 (Just 1)] [2 (Just 1)]}] 5 [[4 (Just 1)] {[5 (Just 2)] [3 (Just 2)]}] 6 [[5 (Just 2)] {[4 (Just 1)]}]}) 7 ϊʔυ id3 ͷ৘ใΛऔΓग़͢ίʔυ͸ҎԼͷ௨ΓͰ͢ɻ 1 (match graph-data2 graph 2 {[<cons [[,3 _] $node-info] _> node-info] 3 [_ 0]}) 4 ;{[4 {1}] [2 {1}]} 5 17
  9. rewrite-node ؔ਺ id ͕ n ͷϊʔυΛࢦఆ͞Εͨ৭ c ʹॻ͖׵͑Δɻ 1 (define

    $node [integer (maybe integer)]) 2 node Ϛονϟʔ͸ id ͱ৭ͷ૊Λදݱ͢Δɻ 1 (match n node 2 {[[,id _] [id (Just c)]] 3 [_ n]}) 4 18
  10. ࢖༻ྫ 1 (match n node 2 {[[,id _] [id (Just

    c)]] 3 [_ n]}) 4 1 (define $n1 [1 Nothing]) 2 (define $n2 [2 (Just 1)]) 3 (rewrite-node 1 1 n1) 4 ;[1 {1}] 5 6 (rewrite-node 2 2 n2) 7 ;[2 {2}] 8 9 (rewrite-node 2 2 n1) 10 ;[1 {}] 11 19
  11. assign-color ؔ਺ rewrite-node Λ map ͯ͠ɺάϥϑͷࢦఆ͞Εͨ id ͷϊʔυʹࢦ ఆ͞Εͨ৭ c

    ΛృΓ·͢ɻ άϥϑ͸ࡾ֯ܗͰϊʔυ id ͕ 1, 2, 3 ͰͦΕͧΕ৭͕͍͍ͭͯͳ ͍ঢ়ଶͰ͢ɻ͜ͷάϥϑͷ id ͕ 1 ͷϊʔυͷ৭Λ 2 ʹృΓ·͢ɻ 1 (define $graph-data 2 {[[1 Nothing] {[2 Nothing] [3 Nothing]}] 3 [[2 Nothing] {[1 Nothing] [3 Nothing]}] 4 [[3 Nothing] {[1 Nothing] [2 Nothing]}]}) 5 6 (assign-color 1 2 graph-data) 7 ;{[[1 {2}] {[2 {}] [3 {}]}] [[2 {}] {[1 {2}] [3 {}]}] → [[3 {}] {[1 {2}] [2 {}]}]} 8 20
  12. loop ύλʔϯ ͋Δϊʔυ͔Βผͷϊʔυ΁ͷܦ࿏ΛऔΓग़͢ɻ 1 (match-all graph-data graph 2 [<cons [[$id_1

    _] <cons [$id_2 _] _>] 3 (loop $i [3 $n] 4 <cons [[,id_(- i 1) _] <cons [$id_i _] _>] 5 ...> 6 <cons [[,id_n _] <cons [,1 _] _>] _>)> id]) 7 21
  13. flip-color ؔ਺ ࢦఆ͞Εͨೋ৭Ͱͭͳ͕͍ͬͯΔ෦෼ͷ৭ΛೖΕସ͑·͢ɻ͸͡ Ίϊʔυ id1 ͷ৭͕ c2 Ͱϊʔυ id2 ͕৭

    c1 ͷঢ়ଶ͔Βɺid2 ͷ ৭Λ c2 ʹృΓɺid1 ͱٯํ޲ͷ৭ c2 ͷϊʔυΛ୳ͯͦ͠ΕΛ id3 ͱ͠࠶ؼతʹ࣮ߦ͠·͢ɻ 1 (define $flip-color 2 (lambda [$id1 $id2 $c1 $c2 $graph-data] 3 (match graph-data graph 4 {[<cons [[,id2 (just ,c1)] 5 <join _ <cons (& [$id3 (just ,c2)] ![,id1 _]) _>>] 6 _> 7 (assign-color id2 c2 (flip-color id2 id3 c2 c1 graph → -data))] 8 [_ (assign-color id2 c2 graph-data)]}))) 22
  14. ࢖༻ྫ 1 (define $graph-data 2 {[[1 Nothing] {[2 (Just 1)]}]

    3 [[2 (Just 1)] {[3 (Just 2)] [1 Nothing]}] 4 [[3 (Just 2)] {[4 (Just 1)] [2 (Just 1)]}] 5 [[4 (Just 1)] {[5 (Just 2)] [3 (Just 2)]}] 6 [[5 (Just 2)] {[4 (Just 1)]}]}) 7 8 (flip-color 1 2 1 2 graph-data) 9 ;{[[1 {}] {[2 {2}]}] [[2 {2}] {[3 {1}] [1 {}]}] [[3 {1}] → {[4 {2}] [2 {2}]}] [[4 {2}] {[5 {1}] [3 {1}]}] [[5 → {1}] {[4 {2}] [6 {3}]}] [[6 {3}] {[5 {1}]}]} 23
  15. ஫ҙ ࣮͸্ͷ࣮૷Ͱ͸͏·͍͍ͬͯ͘ͳ͍ʢଟ෼ loop ύλʔϯΛ ࢖͏ʣ ͋Δϊʔυ͔Βಉ͡৭ͷϊʔυʹ 3 ͭҎ্ܨ͕͍ͬͯΔ৔߹ 1 (define

    $graph-data2 2 {[[1 Nothing] {[2 (Just 1)]}] 3 [[2 (Just 1)] {[3 (Just 2)] [1 Nothing]}] 4 [[3 (Just 2)] {[4 (Just 1)] [2 (Just 1)]}] 5 [[4 (Just 1)] {[5 (Just 2)] [3 (Just 2)] [6 (Just 2) → ]}] 6 [[5 (Just 2)] {[4 (Just 1)]}] 7 [[6 (Just 2)] {[4 (Just 1)]}]}) 8 9 (flip-color 1 2 1 2 graph-data2) 10 ;{[[1 {}] {[2 {2}]}] [[2 {2}] {[3 {1}] [1 {}]}] [[3 → {1}] {[4 {2}] [2 {2}]}] [[4 {2}] {[5 {1}] [3 {1}] → [6 {2}]}] [[5 {1}] {[4 {2}]}] [[6 {2}] {[4 {2}]}]} 24
  16. ΞϧΰϦζϜ ࣍਺ͷ 5 ҎԼͷϊʔυΛऔΓআ͍ͯ࠶ؼతʹ࠼৭͢Δɻ ࠷ޙͷϊʔυΛృΔͱ͖ʹɺ 1. पΓͷ৭͕ 4 ৭ҎԼͳΒ࢒͍ͬͯΔ৭ΛృΔ 2.

    पΓͷ৭͕ 5 ৭ͷ৔߹ɺ1 ͱ 3 ͕ͦͷೋ৭ͷΈͰܨ͕Δ͔ ൱͔ 2.1 ܨ͕Βͳ͍৔߹ɺ1 ͔Β 3 ΛͨͲͬͯ৭ΛೖΕସ͑Ε͹पΓ ͸ 4 ৭ʹͳΔ 2.2 ܨ͕Δ৔߹ɺ2 ͱ 4 ͕ܨ͕Βͳ͍ͷͰɺͲͪΒ͔ͷ৭ΛృΓ ସ͑Ε͹पΓ͸ 4 ৭ʹͳΔ 25
  17. ࣍਺͕ 5 ҎԼͷύλʔϯϚον 1 (match graph-data graph 2 {[<cons (&

    [[$id (nothing)] 3 !<cons _ 4 (loop $i [2 N] <cons _ ...> _) 5 >] $node-info) _> 6 (colorize id (add-node node-info (main (remove- → node id graph-data))))] 7 [_ graph-data]}))) 8 26
  18. remove-node ؔ਺ ࢦఆ͞Εͨ id ͷϊʔυΛऔΓআ͘ 1 (match-all graph-data graph 2

    [<cons (& ![[,id _] _] [$nid $edges]) _> 3 [nid (match-all edges (multiset node) 4 [<cons (& ![,id _] $n) _> n])]]) 5 ,id ͸ value ύλʔϯɺ!͸ not ύλʔϯͰ node ͷ id ͕ࢦఆ͞Ε ͨ id ͱҟͳΔͱ͖Ϛονɺ&͸ and ύλʔϯͰม਺ nid ͱ edges ΁ͷଋറɺ͞Βʹͦͷϊʔυʹͭͳ͕͍ͬͯΔ෦෼͕༩͑ΒΕͨ id ͱҟͳΔϊʔυͷΈऔΓग़͢ɻ match-all ͳͷͰࢦఆ͞Εͨ id Ҏ֎ͷϊʔυΛશͯूΊͨ΋ͷ͕ ฦΔ 27
  19. ࢖༻ྫ 1 (define $graph-data 2 {[[1 Nothing] {[2 Nothing]}] 3

    [[2 Nothing] {[1 Nothing]}]}) 4 5 (remove-node 1 graph-data) 6 ;{[[2 {}] {}]} 7 28
  20. add-node ؔ਺ औΓআ͍ͨϊʔυͷ৘ใ node-info Λάϥϑ graph-data ʹ໭͢ 1 (define $add-node

    2 (lambda [$node-info $graph-data] 3 {[(fst node-info) 4 (match-all [(snd node-info) graph-data] [(set node → ) graph] 5 {[[<cons [$id _] _> <cons [[,id $c] _] _>] [id c]] 6 [[<cons [$id $c] _> !<cons [[,id _] _] _>] [id c → ]]})] 7 @(match-all [graph-data (snd node-info)] [graph (set → node)] 8 {[[<cons [[$id $c] $edges] _> <cons [,id _] _>] 9 [[id c] {(fst node-info) @edges}]] 10 [[<cons [[$id $c] $edges] _> !<cons [,id _] _>] 11 [[id c] edges]]})})) 12 29
  21. ࢖༻ྫ ͜͜Ͱ͸ϊʔυ 1 ΛऔΓআ͍ͨޙɺϊʔυ 2 ʹ৭ 1 Λృͬͯɺ ϊʔυ 1

    Λ໭͍ͯ͠·͢ɻ 1 (define $graph-data 2 {[[1 Nothing] {[2 Nothing]}] 3 [[2 Nothing] {[1 Nothing]}]}) 4 5 (add-node [[1 Nothing] {[2 Nothing]}] (main (remove- → node 1 graph-data))) 6 ;{[[1 {}] {[2 {1}]}] [[2 {1}] {[1 {}]}]} 7 30
  22. पΓͷϊʔυʹ 5 ৭શͯ࢖ΘΕ͍ͯΔ͔ ͋ͨ͑ΒΕͨ id Λ΋ͭϊʔυͷपΓͷϊʔυʹ 5 ৭શͯ࢖ΘΕ ͍ͯΔ͔Ͳ͏͔ͷύλʔϯϚον colors

    ͸৭൪߸ͷ set ͱͯ͠ఆٛ͞Εͨ΋ͷɻ 1 (match [colors graph-data] [(set integer) graph] 2 {[[<cons $c _> <cons [[,id (nothing)] !<join _ <cons [ → _ (just ,c)] _>>] _>] 3 (assign-color id c graph-data)] 4 [_ graph-data]}) 5 31
  23. ৭ 1 ͱ 3 ͷΈͰϊʔυ 1 ͱ 3 ͕ܨ͕Δ͔ ύλʔϯϚονͰม਺ͷଋറ

    1 (match [colors graph-data] [(set integer) graph] 2 [[_ <cons [[,id (nothing)] <cons [$nid_1 (just $c_1)] 3 (loop $i [2 N] 4 <cons [$nid_i (just $c_i)] 5 ...> 6 <nil>)>] _>] 7 32
  24. ৭ 1 ͱ 3 ͷΈͰϊʔυ 1 ͱ 3 ͕ܨ͕Δ͔ 1

    (match graph-data graph 2 {[<cons [[,nid_1 _] 3 <join _ 4 <cons [$cnid_1 (| (just ,c_1) (just ,c_3))] _>>] 5 (loop $i [2 $n] 6 <cons [[,cnid_(- i 1) _] 7 <join _ 8 <cons [$cnid_i (| (just ,c_1) (just ,c_3))] _>>] 9 ...> 10 <cons [[,cnid_n _] 11 <join _ 12 <cons [,nid_3 (| (just ,c_1) (just ,c_3))] _>>] → _>)> 33
  25. ࣮ྫ 1 (define $graph-data-colored 2 {[[0 Nothing] {[1 (Just 1)]

    [2 (Just 2)] [3 (Just 3) → ] [4 (Just 4)] [5 (Just 5)]}] 3 [[1 (Just 1)] {[2 (Just 2)] [0 Nothing] [5 (Just → 5)]}] 4 [[2 (Just 2)] {[3 (Just 3)] [0 Nothing] [1 (Just → 1)]}] 5 [[3 (Just 3)] {[4 (Just 4)] [0 Nothing] [2 (Just 34
  26. ࣮ྫ 1 (main graph-data-colored) 2 ;{[[0 {1}] {[1 {3}] [2

    {2}] [3 {3}] [4 {4}] [5 {5}]}] → [[1 {3}] {[0 {1}] [2 {2}] [5 {5}]}] [[2 {2}] {[0 → {1}] [3 {3}] [1 {3}]}] [[3 {3}] {[0 {1}] [4 {4}] [2 → {2}]}] [[4 {4}] {[0 {1}] [5 {5}] [3 {3}]}] [[5 → {5}] {[0 {1}] [1 {3}] [4 {4}]}]} 3 35
  27. ೔ຊ஍ਤ 1 (define $graph-honshu 2 {[[2 Nothing] {[3 Nothing] [5

    Nothing]}] 3 [[3 Nothing] {[2 Nothing] [4 Nothing] [5 Nothing]}] 4 [[4 Nothing] {[3 Nothing] [7 Nothing] [6 Nothing] [5 → Nothing]}] 5 [[5 Nothing] {[2 Nothgin] [3 Nothing] [4 Nothing] [6 → Nothing]}] 6 [[6 Nothing] {[5 Nothing] [4 Nothing] [7 Nothing] → [15 Nothing]}] 7 [[7 Nothing] {[4 Nothing] [8 Nothing] [9 Nothing] → [10 Nothing] [15 Nothing] [6 Nothing]}] 8 [[8 Nothing] {[7 Nothing] [12 Nothing] [11 Nothing] → [9 Nothing]}] 9 [[9 Nothing] {[7 Nothing] [8 Nothing] [11 Nothing] → [10 Nothing]}] 10 [[10 Nothing] {[7 Nothing] [9 Nothing] [11 Nothing] → [20 Nothing] [15 Nothing]}] 36
  28. 1 (main graph-honshu) 2 ;{[[2 {3}] {[3 {2}] [5 {1}]}]

    [[3 {2}] {[2 {3}] [4 → {4}] [5 {1}]}] [[5 {1}] {[2 {3}] [3 {2}] [4 {4}] [6 → {2}]}] [[4 {4}] {[3 {2}] [7 {3}] [6 {2}] [5 {1}]}] → [[6 {2}] {[4 {4}] [5 {1}] [7 {3}] [15 {1}]}] [[7 → {3}] {[4 {4}] [6 {2}] [8 {2}] [9 {1}] [10 {2}] [15 → {1}]}] [[15 {1}] {[6 {2}] [7 {3}] [10 {2}] [16 {2}] → [20 {3}]}] [[8 {2}] {[7 {3}] [9 {1}] [11 {4}] [12 → {1}]}] [[9 {1}] {[7 {3}] [8 {2}] [11 {4}] [10 → {2}]}] [[10 {2}] {[7 {3}] [9 {1}] [11 {4}] [20 {3}] → [15 {1}]}] [[11 {4}] {[8 {2}] [9 {1}] [10 {2}] [12 → {1}] [13 {2}] [19 {1}] [20 {3}]}] [[12 {1}] {[8 → {2}] [11 {4}] [13 {2}]}] [[20 {3}] {[10 {2}] [11 → {4}] [15 {1}] [16 {2}] [19 {1}] [23 {1}] [21 {4}] → [22 {2}]}] [[13 {2}] {[11 {4}] [12 {1}] [14 {3}] → [19 {1}]}] [[19 {1}] {[11 {4}] [13 {2}] [14 {3}] → [20 {3}] [22 {2}]}] [[14 {3}] {[13 {2}] [22 {2}] → [19 {1}]}] [[22 {2}] {[14 {3}] [19 {1}] [20 {3}] → [23 {1}]}] [[16 {2}] {[15 {1}] [17 {1}] [21 {4}] 37