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
ӄཅ
(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Λݺͼग़͢ʣ
ϥΠϒϥϦԽʹඞཁͳͷ
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 ௨ৗͷؔͱಉ͡هड़Ͱʮ్தͰఀࢭɾ࠶։ʯ͢Δؔ
͕ॻ͚Δ
δΣωϨʔλ
(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ʹͳΔ
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มʹΑΔίϯύΠϥ