Slide 1

Slide 1 text

,POUMBOHͱ .POBEJD3FqFDUJPO ͋Δ͍͸ ϚΫϩͱϞδϡʔϧͱݶఆܧଓͰͭ͘ΔϞφυϥΠϒϥϦ ![FIOQBBSE

Slide 2

Slide 2 text

w ϓϩάϥϚ w -JTQྺ̐೥ w ͍ΖΜͳݴޠΛ৮ͬͨΓ࡞ͬͨΓ͢Δͷ͕޷͖ w ࠷ۙ͸झຯͰ0$BNMΛ࢖͏͜ͱ͕ଟ͍ w ͋ͻΔ͏͗͞͞ΜνʔϜʢޙظϰΟτήϯγϡλΠϯ೿ʣ w ϒϩάɿ[FIOQBBSEIBUFOBCMPHDPN w (JU)VCIUUQTHJUIVCDPN[FIOQBBSE ࣗݾ঺հ

Slide 3

Slide 3 text

ຊ೔ͷ࿩୊ w TIJGUSFTFUͰϞφυΛϥΠϒϥϦͱ࣮ͯ͠૷͍ͨ͠ w ͦͷͨΊʹඞཁͳύʔπ w ϚΫϩ w Ϟδϡʔϧ w ͭͬͯ͘ΈΔɾ࢖ͬͯΈΔ

Slide 4

Slide 4 text

TIJGUSFTFU

Slide 5

Slide 5 text

લճ·Ͱͷ͋Β͢͡ w ݶఆܧଓʹ͍ͭͯ஌Γ͍ͨͷͰݶఆܧଓ͕ѻ͑Δࣗ࡞ݴ ޠ,POUMBOHͷΠϯλϓϦλΛ࣮૷͍ͨ͠ w ݶఆܧଓΛ࣮૷͢ΔͳΒɺΠϯλϓϦλΛܧଓ౉͠ελΠ ϧͰॻ͚͹ָͦ͏ʁ w ͍Ζ͍Ζ໘ന͔ͬͨ w TIJGUSFTFU࣮૷͸ؒʹ߹Θͣɾɾɾ

Slide 6

Slide 6 text

,POUMBOH w $MPKVSFͬΆ͍ʁจ๏ w ͍Ζ͍Ζඍົʹҧ͏ w $MPKVSF͏Ζ͓΅͑ɾɾɾ w ಈతܕ෇͚

Slide 7

Slide 7 text

,POUMBOH w ७ਮؔ਺ܕʁ w ݶఆܧଓҎ֎ʂ w ͲͪΒ͔ͱ͍͏ͱʮՄมϝϞϦͰ͋ΔετΞΛఏڙ͠ ͳ͍ʯͱݴͬͨ΄͏͕ਖ਼͍͠ʁ

Slide 8

Slide 8 text

,POUMBOHΠϯλϓϦλ w ΠϯλϓϦλ͕$&,ந৅ػցʹ͍ۙ w EFGVODUJPOBMJ[F͞Εͨܧଓ౉͠ελΠϧͰॻ͔Ε͍ͯΔ w ΠϯλϓϦλͷ৺ଁ෦͸FWBMͱBQQMZ@DPOUͷೋؔ਺ w ͲͪΒ΋࠶ؼͤͣ&YFDVUFUܕΛฦ͢ɿ type t = | Done of Val.t | Eval of Env.t * Cont.t * Exp.t | ApplyCont of Env.t * Cont.t * Val.t

Slide 9

Slide 9 text

ܧଓελοΫ 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͕ݺ͹ΕΔͱ۠੾Γͱ͠ ͯ৽͍ۭ͠Ϧετ͕௥Ճ͞Ε Δ

Slide 10

Slide 10 text

TIJGUSFTFU͕Ͱ͖ͨ

Slide 11

Slide 11 text

TIJGUSFTFUͷڍಈ w SFTFUͰܧଓΛ۠੾Δ w TIJGU͸ݶఆܧଓΛͱͬͯ͘Δ w TIJGU. ͱ͢ΔͱʮݱࡏͷݶఆܧଓΛऔΓআ͖ɺͦ ΕΛؔ਺ʹͯ͠Lʹଋറ͠ɺ.Λ࣮ߦ͢ΔʯʢʮTIJGU SFTFUϓϩάϥϛϯάೖ໳ʯΑΓҾ༻ʣ http://pllab.is.ocha.ac.jp/~asai/cw2011tutorial/main-j.pdf

Slide 12

Slide 12 text

TIJGUSFTFUͷڍಈ ྦྷੵ࿨Λࢉग़͢ΔBDDVNVMBUPSɿ (letrec [(f [n] (shift [k] (cons n k))) (g [n] (g (+ n (f n)))) (accumulator [] (fn [n] (reset (g n))))]

Slide 13

Slide 13 text

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))

Slide 14

Slide 14 text

ӄཅ (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͚ͩݺͼग़͠ճ਺͕ϧʔϓͷ౓ʹ૿͑Δ

Slide 15

Slide 15 text

TIJGUSFTFUͷ࣮૷ w SFTFUͰܧଓελοΫʹ৽͍ۭ͠ϦετΛ௥Ճ͢Δ͜ͱͰʮܧଓ Λ۠੾Δʯ w SFTFU಺ͷࣜΛධՁ͢Δ࣌͸ͦͷۭϦετʹܧଓΛՃ͑Δ w TIJGUF ͱ͍͏ࣜΛධՁ͢Δ৔߹ɿ w ܧଓελοΫ͔Βઌ಄ͷϦετΛ֎͠ w ͦͷϦετΛอ࣋͢Δ7BM$POU஋ΛLͱ͍͏ม਺ʹଋറ w ͦͷม਺؀ڥͱܧଓελοΫͰFΛධՁ

Slide 16

Slide 16 text

TIJGUSFTFUͷ࣮૷ w 7BM$POU஋͕ؔ਺ͱͯ͠ݺͼग़͞Εͨ৔߹ɿ w ·ͣ͸Ҿ਺ΛධՁ w ܧଓελοΫʹ7BM$POUʹอ࣋͞Ε͍ͯΔܧଓͷϦετ Λ௥Ճ͢Δ w ৽͍͠ܧଓελοΫʹҾ਺ͷධՁ݁ՌͰ͋Δ஋Λ౉͢ ʢBQQMZ@DPOUΛݺͼग़͢ʣ

Slide 17

Slide 17 text

ݶఆܧଓͱϞφυ http://pllab.is.ocha.ac.jp/~asai/cw2011tutorial/main-j.pdf

Slide 18

Slide 18 text

ϞφυΛͭ͘Ζ͏ https://www.slideshare.net/dico_leque/ss-14149810

Slide 19

Slide 19 text

ϞφυΛͭ͘Ζ͏ https://dl.acm.org/doi/pdf/10.1145/174675.178047 http://www.cs.ioc.ee/mpc-amast06/msfp/filinski-slides.pdf

Slide 20

Slide 20 text

ϥΠϒϥϦԽʹඞཁͳ΋ͷ w ϚΫϩ w TIJGUͱSFTFUΛผʑʹӅṭ͍ͨ͠ w TIJGU͕SFTFU֎Ͱݺͼग़͞ΕΔͷΛආ͚ΔͨΊɺධՁΛ஗Ԇ ͍ͨ͠ w Ϟδϡʔϧ w ҧ͏Ϟφυ͕ಉ͡"1*ͰڞଘͰ͖ΔΑ͏ύοέʔδԽ͍ͨ͠ w ֎෦ϑΝΠϧʹهड़ͯ͠ϩʔυ͢Δ͜ͱͰඪ४ϥΠϒϥϦԽ ͍ͨ͠

Slide 21

Slide 21 text

ϚΫϩ

Slide 22

Slide 22 text

ϚΫϩ w ශऑͳ͕Βஔ׵ϕʔεͷୈҰڃϚΫϩ w ΫϩʔδϟΛ࣋ͨͳ͍ͷͰμΠφϛοΫɾείʔϓ w ϨΩγΧϧɾείʔϓʹ͢Δ৔߹ɺஔ׵͢ΔલʹҾ਺Λ UIVOL͢Ε͹͍͍ʁ w 0O-JTQ΍-FU0WFS-BNCEBΛಡΜͩ΄͏͕͍͍͔΋ (let [m (macro [x y] (do [y x]))] (m (println "hello") (println "goodbye")))

Slide 23

Slide 23 text

δΣωϨʔλ w ஗ԆϦετͱͯ͠ͷδΣωϨʔλ w ࣮૷ͱͯ͠͸ʮ஋ͱ࣍ͷؔ਺Λฦ̌͢Ҿ਺ؔ਺ʯ w Ϧετͱಉ͡Α͏ʹѻ͏ͨΊͷؔ਺܊Λ࣮૷Ͱ͖Δ w NBQ DPODBU NFSHF UBLF ESPQ IFBE UBJMͳͲͳͲ

Slide 24

Slide 24 text

δΣωϨʔλͱݶఆܧଓ w δΣωϨʔλͷ࣮૷ͷͨΊʹTIJGUSFTFUΛ࢖͏ඞཁ͸ͳ͍ w ωετͨ͠ߴ֊ؔ਺Ͱे෼ w ݶఆܧଓΛ࢖͏ͱZJFME͕ͭ͘ΕΔ w 1ZUIPO΍+BWB4DSJQUʹ͋ΔδΣωϨʔλߏจ w ௨ৗͷؔ਺ͱಉ͡هड़Ͱʮ్தͰఀࢭɾ࠶։ʯ͢Δؔ ਺͕ॻ͚Δ

Slide 25

Slide 25 text

δΣωϨʔλ 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))))] ...)

Slide 26

Slide 26 text

δΣωϨʔλ (letfn [count [] (letrec [f [n] (do [(yield n) (f (+ n 1))])] (generator (f 0)))] ...) w HFOFSBUPSͱZJFMEΛ࢖ͬͯ͢΂ͯͷࣗવ਺ͷ஗ԆϦετɿ

Slide 27

Slide 27 text

ͳͥϚΫϩ͕ඞཁʁ w HFOFSBUPSͷҾ਺ͷධՁΛ஗Ԇ͢ΔͨΊ w Ҿ਺಺ͰZJFME͕࢖ΘΕΔͷͰɺධՁΛ஗Ԇ͠ͳ͍ͱTIJGU͕SFTFU֎Ͱݺ ͼग़͞Εͯ͠·͏ w UIVOLͰ΋Մೳ w Ҿ਺ͷؔ਺ʹแΉ͜ͱͰධՁΛ஗Ԇ͢Δ w 0DIB$BNMͰ͸ϚΫϩ͕ͳ͍ͨΊUIVOLΛ࢖͍ͬͯΔ w ه๏తʹ͸ϚΫϩͷ΄͏͕͖ͬ͢Γ͍ͯ͠Δ w Ϟφυ࡞੒Ͱ΋ಉ͡खΛ࢖͏ʢϚΫϩʹΑΔධՁ஗Ԇʣ

Slide 28

Slide 28 text

Ϟδϡʔϧ

Slide 29

Slide 29 text

Ϟδϡʔϧͱ͸ w ಠࣗͷ໊લۭؒ w ֎෦ϑΝΠϧΛಡΈࠐΜͰϞδϡʔϧʹͰ͖Δ w NPEVMF<ʜ> ͰϞδϡʔϧΦϒδΣΫτΛ࡞੒ w ʜͷதʹ͸ EFpOFYʜ ͔ଞͷ͕ࣜೖΔ w .YͰϞδϡʔϧ.಺ͰEFpOF͞Εͨม਺YʹΞΫηεͰ͖ Δ

Slide 30

Slide 30 text

֎෦Ϟδϡʔϧ w ֎෦ϑΝΠϧʹ EFpOFʜ ͰϞδϡʔϧม਺Λఆ͍ٛͯ͘͠ w JNQPSUlpMFOBNFz Ͱ֎෦ϑΝΠϧΛಡΈࠐΈϞδϡʔϧΦϒδΣ ΫτΛ࡞੒ (let [List (import "list.ktl")] (List.map (fn [x] (* x x)) (List.range 5)))

Slide 31

Slide 31 text

ϞδϡʔϧΛ։͘ w Ϟδϡʔϧ.ͷม਺Λ.YͰ͸ͳ͘Yͱ௚઀࢖͍͍ͨ w ී௨ͷࣜͷதͰ࢖͑Δ PQFO.ʜ w ϞδϡʔϧఆٛͰ࢖͑Δ NPEVMF< JODMVEF. ʜ> ͱ NPEVMF< VTJOH . ʜ> w JODMVEF. ͸ఆٛ͞Ε͍ͯΔϞδϡʔϧͷม਺ͱͳΔ w VTJOH. ͸ɿ w Ϟδϡʔϧఆٛத͚ͩ.ͷม਺͕ΞΫηεͰ͖Δ w Ϟδϡʔϧ֎෦͔Β͸͜ΕΒม਺ʹΞΫηεͰ͖ͳ͍

Slide 32

Slide 32 text

ϞφυϥΠϒϥϦ

Slide 33

Slide 33 text

.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ʹ୅਺తσʔλܕ͕ͳ͍ͱ͍͏ͷ΋ཧ༝ɾɾɾ

Slide 34

Slide 34 text

.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ʹͳΔ

Slide 35

Slide 35 text

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ΑΓʹมߋ

Slide 36

Slide 36 text

-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))

Slide 37

Slide 37 text

(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))

Slide 38

Slide 38 text

όοΫτϥοΫ୳ࡧ (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))

Slide 39

Slide 39 text

Ϟφυɾɾɾʁ w ͜͜·Ͱ࣮૷͖ͯͨ͠΋ͷ͸ຊ౰ʹϞφυͳͷ͔ʁ w SFqFDUͱSFJGZʁSFUVSOͱCJOE͸ʁ w SFqFDUSFJGZͰSFUVSOCJOE͸ॻ͚Δ w SFUVSOCJOEͱTIJGUSFTFUͰSFqFDUSFJGZ͸ॻ͚Δ

Slide 40

Slide 40 text

SFqFDUSFJGZˠSFUVSOCJOE w SFqFDUSFJGZͰSFUVSOCJOE͸ॻ͚Δ (letfn [return [a] (reify a)] ...) (let [bind (macro [m f] (reify (reflect (f (reflect m)))))] ...)

Slide 41

Slide 41 text

SFUVSOCJOEˠSFqFDUSFJGZ w SFUVSOCJOEͰSFqFDUSFJGZΛॻ͘ʹ͸TIJGUSFTFU͕ඞཁ w ʮϞφυΛͭ͘Ζ͏ʯʮ3FQSFTFOUJOH.POBETʯࢀর w TIJGUSFTFUΛ࢖͏͜ͱͰEPͷΑ͏ͳಛघߏจͳ͠ʹEJSFDU TUZMFͰهड़Ͱ͖Δ

Slide 42

Slide 42 text

ࠓޙ΍Γ͍ͨ͜ͱ w ͍ΖΜͳϞφυʹձ͍ʹ͍͘ w 1PPSNBO`TDPODVSSFODZNPOBEͰڠௐతεϨουΛ࣮૷ͯ͠Έ͍ͨ w $PSPVUJOF͸(FOFSBUPSͱ΄΅ಉ࣮͡૷Ͱ࡞Εͦ͏ w ͪΌΜͱͨ͠3&1-Λͭ͘Δ w 3&1-ೖྗΛϞδϡʔϧ಺ఆٛͩͱೝࣝͤ͞Ε͹͍͚ͦ͏ w λΠϜτϥϕϧՄೳͳΠϯλϓϦλ w Τϥʔ࣌ʹҰ୴ఀࢭɾר͖໭ͯ͠͠ධՁதͷࣜɺม਺؀ڥɺܧଓελοΫͷத਎Λ ฤूͰ͖ΔΑ͏ʹ͍ͨ͠ w $14ม׵ʹΑΔίϯύΠϥ