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

Kontlang-Monadic Reflection

Kontlang-Monadic Reflection

自作S式言語Kontlangに限定継続・マクロ・モジュールを使って「Representing Monads」ベースのモナド・ライブラリを追加した話

zehnpaard

June 25, 2020
Tweet

More Decks by zehnpaard

Other Decks in Programming

Transcript

  1. ܧଓελοΫ w ΠϯλϓϦλͷঢ়ଶม਺ͷҰͭ type Execute.t = | Done of Val.t

    | Eval of Env.t * Cont.t * Exp.t | ApplyCont of Env.t * Cont.t * Val.t type cont = | Call of Exp.t list * Val.t list | If of Exp.t * Exp.t …… | Env type Cont.t = cont list list w ʮܧଓʯͷϦετͷϦετ w SFTFU͕ݺ͹ΕΔͱ۠੾Γͱ͠ ͯ৽͍ۭ͠Ϧετ͕௥Ճ͞Ε Δ
  2. TIJGUSFTFUͷڍಈ ྦྷੵ࿨Λࢉग़͢ΔBDDVNVMBUPSɿ (letrec [(f [n] (shift [k] (cons n k)))

    (g [n] (g (+ n (f n)))) (accumulator [] (fn [n] (reset (g n))))]
  3. TIJGUSFTFUͷڍಈ BDDVNVMBUPSΛ࢖ͬͯΈΔɿ (let* [(acc (accumulator)) (res (acc 1)) (a (car

    res)) (acc (cdr res)) (res (acc 2)) (b (car res)) (acc (cdr res)) (res (acc 3)) (c (car res))] (list a b c))
  4. ӄཅ (reset (let [(yin ((fn [k] (do [(print "x") k]))

    (shift [k] (k k)))) (yang ((fn [k] (do [(print "_") k])) (shift [k] (k k))))] (yin yang))) w AY@Y@@Y@@@Y@@@@Y@@@@@YʜAͱແݶʹग़ྗ͢Δ w MFUSFDΛ࢖͍ͬͯͳ͍ͷʹ࠶ؼ͍ͯ͠Δ w ZJOͱZBOHͷఆٛ͸΄΅ಉҰͳͷʹڍಈ͕ҧ͏ w ZBOH͚ͩݺͼग़͠ճ਺͕ϧʔϓͷ౓ʹ૿͑Δ
  5. TIJGUSFTFUͷ࣮૷ w SFTFUͰܧଓελοΫʹ৽͍ۭ͠ϦετΛ௥Ճ͢Δ͜ͱͰʮܧଓ Λ۠੾Δʯ w SFTFU಺ͷࣜΛධՁ͢Δ࣌͸ͦͷۭϦετʹܧଓΛՃ͑Δ w TIJGU<L>F ͱ͍͏ࣜΛධՁ͢Δ৔߹ɿ w

    ܧଓελοΫ͔Βઌ಄ͷϦετΛ֎͠ w ͦͷϦετΛอ࣋͢Δ7BM$POU஋ΛLͱ͍͏ม਺ʹଋറ w ͦͷม਺؀ڥͱܧଓελοΫͰFΛධՁ
  6. ϥΠϒϥϦԽʹඞཁͳ΋ͷ w ϚΫϩ w TIJGUͱSFTFUΛผʑʹӅṭ͍ͨ͠ w TIJGU͕SFTFU֎Ͱݺͼग़͞ΕΔͷΛආ͚ΔͨΊɺධՁΛ஗Ԇ ͍ͨ͠ w Ϟδϡʔϧ

    w ҧ͏Ϟφυ͕ಉ͡"1*ͰڞଘͰ͖ΔΑ͏ύοέʔδԽ͍ͨ͠ w ֎෦ϑΝΠϧʹهड़ͯ͠ϩʔυ͢Δ͜ͱͰඪ४ϥΠϒϥϦԽ ͍ͨ͠
  7. δΣωϨʔλ w ϚΫϩͱݶఆܧଓͰ1ZUIPO΍+BWBTDSJQUʹ͋ΔδΣωϨʔ λߏจͷZJFMEΛͭ͘ΕΔ (define generator (macro [expr] (reset (let

    [result expr] (fn [] (cons result (fn [] nil))))))) (letfn [yield [x] (shift [k] (fn [] (cons x (k nil))))] ...)
  8. δΣωϨʔλ (letfn [count [] (letrec [f [n] (do [(yield n)

    (f (+ n 1))])] (generator (f 0)))] ...) w HFOFSBUPSͱZJFMEΛ࢖ͬͯ͢΂ͯͷࣗવ਺ͷ஗ԆϦετɿ
  9. ͳͥϚΫϩ͕ඞཁʁ w HFOFSBUPSͷҾ਺ͷධՁΛ஗Ԇ͢ΔͨΊ w Ҿ਺಺ͰZJFME͕࢖ΘΕΔͷͰɺධՁΛ஗Ԇ͠ͳ͍ͱTIJGU͕SFTFU֎Ͱݺ ͼग़͞Εͯ͠·͏ w UIVOLͰ΋Մೳ w Ҿ਺ͷؔ਺ʹแΉ͜ͱͰධՁΛ஗Ԇ͢Δ

    w 0DIB$BNMͰ͸ϚΫϩ͕ͳ͍ͨΊUIVOLΛ࢖͍ͬͯΔ w ه๏తʹ͸ϚΫϩͷ΄͏͕͖ͬ͢Γ͍ͯ͠Δ w Ϟφυ࡞੒Ͱ΋ಉ͡खΛ࢖͏ʢϚΫϩʹΑΔධՁ஗Ԇʣ
  10. ϞδϡʔϧΛ։͘ w Ϟδϡʔϧ.ͷม਺Λ.YͰ͸ͳ͘Yͱ௚઀࢖͍͍ͨ w ී௨ͷࣜͷதͰ࢖͑Δ PQFO.ʜ  w ϞδϡʔϧఆٛͰ࢖͑Δ NPEVMF<

    JODMVEF. ʜ> ͱ NPEVMF< VTJOH . ʜ>  w JODMVEF. ͸ఆٛ͞Ε͍ͯΔϞδϡʔϧͷม਺ͱͳΔ w VTJOH. ͸ɿ w Ϟδϡʔϧఆٛத͚ͩ.ͷม਺͕ΞΫηεͰ͖Δ w Ϟδϡʔϧ֎෦͔Β͸͜ΕΒม਺ʹΞΫηεͰ͖ͳ͍
  11. .BZCFϞφυ (define reify (macro [expr] (reset expr))) (define reflect (letfn

    [reflect [m] (shift [k] (if (nil? m) m (k m)))] reflect)) w ʮϞφυΛͭ͘Ζ͏ʯͰग़ͯͨྫ w ͨͩ͠4PNFBc/POFͱ͍ͬͨܕ͸࢖͍ͬͯͳ͍ w -JTQFSͳΒ໧ͬͯOJMΛฦ͢ w LPOUMBOHʹ୅਺తσʔλܕ͕ͳ͍ͱ͍͏ͷ΋ཧ༝ɾɾɾ
  12. .BZCFϞφυ࢖༻ྫ (let [(M Stdlib.MaybeMonad) (L Stdlib.List)] (M.reify (let [(x (M.reflect

    (L.find (fn [x] (= x 2)) (list 1 0 3)))) (y (M.reflect (L.find (fn [x] (= x 3)) (list 1 2 3))))] (cons x y)))) w ඪ४ϥΠϒϥϦͷϞδϡʔϧΛ࢖͍ͬͯΔ w EPͳͲͷಛघߏจ͸࢖ΘͣEJSFDUTUZMFͰॻ͍͍ͯΔ͕ɺӅ ṭ͞ΕͨTIJGUSFTFUʹΑͬͯ .SFqFDU ͷதͷ͕ࣜOJMͱධ Ձ͞Εͨ৔߹ɺ.SFJGZશମͷ஋͕OJMʹͳΔ
  13. 4UBUFϞφυ (define reify (macro [expr] (reset (let [result expr] (fn

    [state] (cons result state)))))) (define reflect (letfn [reflect [f] (shift [k] (fn [state] (let* [(val-state (f state)) (val (car val-state)) (state1 (cdr val-state))] ((k val) state1))))] reflect)) (define get (letfn [get [] (reflect (fn [state] (cons state state)))] get)) (define put (letfn [put [val] (reflect (fn [state] (cons nil val)))] put)) (define run_state (letfn [run_state [m state] (m state)] run_state)) w ʮTIJGUSFTFUϓϩάϥϛϯάೖ໳ʯͰ΋ݴٴ͞Ε͍ͯͨ w "1*͸SFJGZSFqFDUΑΓʹมߋ
  14. -JTUϞφυ w όοΫτϥοΫ͢Δඇܾఆੑϓϩάϥϛϯά (define reify (macro [expr] (reset (list expr))))

    (define reflect (letfn [reflect [m] (shift [k] (List.merge (List.map k m)))] reflect)) (define fail (letfn [fail [] (reflect nil)] fail)) (define assert (letfn [assert [x] (if x nil (fail))] assert))
  15. (FOFSBUPSϞφυ w -JTUͱಉ͘͡ඇܾఆੑϓϩάϥϛϯά (define reify (macro [expr] (reset (let [result

    expr] (fn [] (cons result (fn [] nil))))))) (define reflect (letfn [reflect [m] (shift [k] (Generator.merge (Generator.map k m)))] reflect)) (define fail (letfn [fail [] (reflect (fn [] nil))] fail)) (define assert (letfn [assert [x] (if x nil (fail))] assert))
  16. όοΫτϥοΫ୳ࡧ (let* [(G Stdlib.Generator) (GM Stdlib.GeneratorMonad) (g (GM.reify (let* [(gen

    (G.from_list (list 1 2 3 4 5)) (x (GM.reflect gen)) (y (GM.reflect gen)) (z (GM.reflect gen)) (_ (GM.assert (= (* z z) (+ (* x x) (* y y)))))] (list x y z))))] (G.head g))
  17. ࠓޙ΍Γ͍ͨ͜ͱ w ͍ΖΜͳϞφυʹձ͍ʹ͍͘ w 1PPSNBO`TDPODVSSFODZNPOBEͰڠௐతεϨουΛ࣮૷ͯ͠Έ͍ͨ w $PSPVUJOF͸(FOFSBUPSͱ΄΅ಉ࣮͡૷Ͱ࡞Εͦ͏ w ͪΌΜͱͨ͠3&1-Λͭ͘Δ w

    3&1-ೖྗΛϞδϡʔϧ಺ఆٛͩͱೝࣝͤ͞Ε͹͍͚ͦ͏ w λΠϜτϥϕϧՄೳͳΠϯλϓϦλ w Τϥʔ࣌ʹҰ୴ఀࢭɾר͖໭ͯ͠͠ධՁதͷࣜɺม਺؀ڥɺܧଓελοΫͷத਎Λ ฤूͰ͖ΔΑ͏ʹ͍ͨ͠ w $14ม׵ʹΑΔίϯύΠϥ