Slide 1

Slide 1 text

,POUMBOH TIJGUSFTFUͷ͋Δ-JTQ ͋Δ͍͸ ΠϯλϓϦλΛܧଓ౉͠ελΠϧͰॻ͘ͱԿ͕خ͍͠ͷ͔ ![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 ͦͷͨΊʹTIJGUSFTFUͷ͋ΔݴޠΛ࣮૷͍ͨ͠ w ܧଓ౉͠ελΠϧͰΠϯλϓϦλॻ͍ͯΈͨ w ͍Ζ͍Ζ໘ന͍͜ͱ͕͋ͬͨ

Slide 4

Slide 4 text

ݶఆܧଓͱ͸ʁߏ଄΍ࣗ࡞ํ๏ɺ Φεεϝͷݶఆܧଓͷ࢖͍ํΛղ આʂܧଓ΍Ϟφυͱͷҧ͍ͱ͸ʁ

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

ܧଓ w ʮ͋ΔܭࢉΛ࣮ߦͨͦ͠ͷޙΛද͢΋ͷʯ w ʮ͜Ε͔ΒߦΘΕΔͰ͋Ζ͏ܭࢉΛύοέʔδԽͨ͠΋ ͷʯ w ʮϓϩάϥϜͷ جຊతʹ͸τοϓϨϕϧʹ໭Δ·Ͱͷ ࢒Γͷॲཧʯ ʮϞφυΛͭ͘Ζ͏ʯɹ!EJDP@MFRVFࢯ ʮͳΜͰ΋ܧଓʯɹ!BOPIBOBࢯ ʮ4DIFNFͱܧଓʯɹ!IBUTVHBJࢯ ϓϩάϥϛϯάͷੈքͷ֓೦ʹ͸ɺષͷެҊͷΑ͏ͳ΋ͷ͕͋Δɻ ͦΕΛઆ໌͢Δจষ͸΄ΜͷҰจͳͷʹɺ࠷ॳʹ໨ ʹ͢Δ࣌ɺ ͦͷจ͸શ͘ҙຯΛͳ͞ͳ͍ɺ҉߸ͷΑ͏ʹײ͡ΒΕΔɻ ͕ͩͻͱͨͼͦͷ֓೦Λཧղ͢Δͱɺ ͦͷ֓೦ ͷઆ໌͸͔֬ʹͦͷҰจͰઆ໌͞Ε͍ͯΔͷ͕Θ͔ΔͷͩɻɹʮͳΜͰ΋ܧଓʯɹ@anohanaࢯ https://www.slideshare.net/dico_leque/ss-14149810 http://practical-scheme.net/docs/cont-j.html http://lqtmirage.hatenablog.com/entry/2017/05/23/101744

Slide 7

Slide 7 text

ܧଓ (+ 1 2 (* 3 4) (+ 5 6)) ྫ͑͹͜Μͳ͕ࣜ͋Δͱͯ͠

Slide 8

Slide 8 text

ܧଓ (+ 1 2 (* 3 4) (+ 5 6)) ͜ͷ෦෼ࣜͷධՁΛߟ͑Δͱ

Slide 9

Slide 9 text

ܧଓ (+ 1 2 X (+ 5 6))) (fn [X] ͦͷपғʹ͋Δ࢒Γͷ͕ࣜͦͷ ෦෼ࣜͷʮܧଓʯͱͳΔɻ ͜ͷΑ͏ͳؔ਺Ͱ ͷܧଓ ͕දݱͰ͖Δ

Slide 10

Slide 10 text

ܧଓͱݶఆܧଓ w ޡɿʮݶఆܧଓʯ͸ʮܧଓʯػೳΛݶఆͯ͠࢖͍΍͘͢͠ ͨɾ࣮૷͠΍ͨ͘͢͠΋ͷͰ͋Δ w ਖ਼ɿʮݶఆܧଓʯ͸ัଊ͢ΔܧଓͷൣғΛҙਤతʹݶఆ͢ Δػೳ͕௥Ճ͞Ε͍ͯΔɻදݱྗ͸ݶఆܧଓͷํ্͕

Slide 11

Slide 11 text

ݶఆܧଓͱϞφυ https://dl.acm.org/doi/pdf/10.1145/174675.178047 http://okmij.org/ftp/continuations/against-callcc.html

Slide 12

Slide 12 text

໰୊఺ w TIJGUSFTFUϓϩάϥϛϯάೖ໳͸0DIB$BNMΛ࢖͍ͬͯΔ w 0$BNMϕʔεͰTIJGUSFTFU͕ೖͬͨಠࣗݴޠ w νϡʔτϦΞϧΛ΍ΔͨΊ͚ͩʹॲཧܥΠϯετʔϧ͢Δ ͷ΋ͳ͊ɾɾɾ w ΋ͬͱ௚઀ʮܧଓʯʹ৮ΕͯΈ͍ͨʂ

Slide 13

Slide 13 text

ͦ͏ͩɺΠϯλϓϦλࣗ࡞͠Α͏

Slide 14

Slide 14 text

λωຊ w ΠϯλϓϦλ࣮૷Λ௨ͯ͡ϓϩ άϥϛϯάݴޠͷॾ֓೦ΛֶͿ ͨΊͷຊ w ͍ΖΜͳػೳΛஈ֊తʹ௥Ճ w ୈষɺୈষ͕ܧଓͷ࿩ w ୈষ͕۩ମతʹΠϯλϓϦλ Λܧଓ౉͠Ͱॻ͖ͳ͓͢ํ๏

Slide 15

Slide 15 text

&P1-ྱࢍ w ࠓճͷτϐοΫҎ֎ʹ΋ωλຬࡌ w ϛϡʔλϒϧͳม਺ɾλϓϧ w ܕਪ࿦ w .-Ϟδϡʔϧ΍ϑΝϯΫλʔ w ΦϒδΣΫτ w ྫ֎ɺεϨουɺ$BMMCZ/BNFʜ

Slide 16

Slide 16 text

&P1-ͷݴޠબ୒ ϗετݴޠɿ3BDLFU ιʔεݴޠɿ.-ͬΆ͍ let rec even x = if zero?(x) then 1 else (odd -(x,1)) and odd x = if zero?(x) then 0 else (even -(x,1)) in odd 13 (define value-of (exp env) (cases expression exp (const-exp (num) (num-val num)) (var-exp (var) (apply-env env var)) (diff-exp (exp1 exp2) (let ((val1 (value-of exp1 env)) (val2 (value-of exp2 env))) (let ((num1 (expval->num val1)) (num2 (expval->num val2))) (num-val (- num1 num2))))) ...))

Slide 17

Slide 17 text

,POUMBOHͷݴޠબ୒ ϗετݴޠɿ0$BNM let rec eval env e = match e with | Exp.Int n -> Val.Int n | Exp.Var s -> Env.find env s | Exp.Call(e1, e2) -> let v1 = eval env e1 in let v2 = eval env e2 in call v1 v2 ... ιʔεݴޠɿ$MPKVSFͬΆ͍ (letfn [double [x] (* x 2)] (letrec [map [f xs] (if (nil? xs) nil (cons (f (car xs)) (map f (cdr xs))))] (map double (list 1 2 3 4 5))))

Slide 18

Slide 18 text

0$BNMͷར఺ w ͔͕ͬ͜গͳ͍ʢӕͰ͢ɺ෺Λ౤͛ͳ͍Ͱʣ w ୅਺తσʔλܕͱύλʔϯϚον w ڧྗͳύʔαδΣωϨʔλ.FOIJS w ͋Β͔ͨؔ਺ܕɺ࣌ʑ෭࡞༻

Slide 19

Slide 19 text

-JTQจ๏ͷར఺ w ύʔαॻ͘ͷָ͕ʂ w จ๏΄΅ߟ͑ͳ͍͍ͯ͘ w ϚΫϩʁ

Slide 20

Slide 20 text

,POUMBOHͱ͸ʁߏ଄΍ࣗ࡞ํ๏ɺΦ εεϝͷΠϯλϓϦλͷબͼํΛղ આʂ4DIFNF΍$MPKVSFͱͷҧ͍ͱ͸ʁ

Slide 21

Slide 21 text

,POUMBOH

Slide 22

Slide 22 text

,POUMBOH঺հ w $MPKVSFͬΆ͍ʢʁʣจ๏ w ΄΅७ਮؔ਺ܕ w ஋͸Πϛϡʔλϒϧ w ϝϞϦʹରͯ͠มߋΛՃ͑Δํ๏͸ͳ͍ w QSJOU΍SFBE͸͋Δ w ϚΫϩ w ݶఆܧଓ

Slide 23

Slide 23 text

ίʔυαϯϓϧ (letfn [double [x] (* x 2)] (letrec [map [f xs] (if (nil? xs) nil (cons (f (car xs)) (map f (cdr xs))))] (map double (list 1 2 3 4 5))))

Slide 24

Slide 24 text

ϚΫϩ w ශऑͳ͕Βஔ׵ϕʔεͷୈҰڃϚΫϩ w ΫϩʔδϟΛ࣋ͨͳ͍ͷͰμΠφϛοΫɾείʔϓ w ϨΩγΧϧɾείʔϓʹ͢Δ৔߹ɺஔ׵͢ΔલʹҾ਺Λ UIVOL͢Ε͹͍͍ʁ w ʮϞφυΛͭ͘Ζ͏ʯͷSFJGZΛ࣮૷͍ͨ͠ (let [m (macro [x y] (do [y x]))] (m (println "hello") (println "goodbye")))

Slide 25

Slide 25 text

ͪΐͬͱσϞ EVOFFYFDNBJOFYF

Slide 26

Slide 26 text

࣮૷ղઆ w ࣜͱ஋ w ܧଓ౉͠ελΠϧ w εςοϓ࣮ߦ w ຤ඌݺͼग़͠࠷దԽ

Slide 27

Slide 27 text

ࣜͱ஋

Slide 28

Slide 28 text

ࣜͱ஋ w ͳΜͪΌͬͯ4ࣜ w ࣜͱ஋͸ผ෺ w ࣜΛධՁͯ͠஋ʹ͢Δ w ΞτϜͱϦετͰ͸ͳ͘"45͕͋Δ w ΋ͱ΋ͱ.-ͬΆ͍ݴޠʹ-JTQจ๏Λඃ͍ͤͯΔͷͰɾɾɾ

Slide 29

Slide 29 text

ࣜͱ஋ module Env = struct type t = (string * Val.t) list let rec find env s = match env with | [] -> failwith "Not found" | (s',v)::env' -> if s = s' then v else find env' s end let rec eval env e = match e with | Exp.Int n -> Val.Int n | Exp.Var s -> Env.find env s | Exp.Call(e1, e2) -> let v1 = eval env e1 in let v2 = eval env e2 in call v1 v2 module Exp = struct type t = | Int of int | Var of string | Call of t * t end module Val = struct type t = | Int of int | Op of string * (Val.t -> Val.t) end

Slide 30

Slide 30 text

ܧଓ౉͠ελΠϧ

Slide 31

Slide 31 text

ී௨ͷΠϯλϓϦλ let rec eval env e = match e with | Exp.Int n -> Val.Int n | Exp.Var s -> Env.find env s | Exp.Call(e1, e2) -> let v1 = eval env e1 in let v2 = eval env e2 in call v1 v2

Slide 32

Slide 32 text

ܧଓ౉͠ελΠϧ let rec eval env cont e = match e with | Exp.Int n -> cont (Val.Int n) | Exp.Var s -> cont (Env.find env s) | Exp.Call(e1, e2) -> let cont’ v = let cont’’ w = cont (call v w) in eval env cont’’ e2 in eval env cont’ e1

Slide 33

Slide 33 text

%FGVODUJPOBMJ[BUJPO let rec eval env cont e = match e with | Exp.Int n -> apply_cont env cont (Val.Int n) | Exp.Var s -> apply_cont env cont (Env.find env s) | Exp.Call(e1, e2) -> eval env (Cont.Call1(e2)::cont) e1 and apply_cont env cont v = match cont with | [] -> v | Cont.Call1(e)::cont' -> eval env (Cont.Call2(v)::cont') e | Cont.Call2(v')::cont' -> apply_cont env cont' (call v' v) module Cont = struct type t' = | Call1 of Exp.t | Call2 of Val.t type t = t' list end

Slide 34

Slide 34 text

τϥϯϙϦϯԽ let eval env cont e = match e with | Exp.Int n -> Res.ApplyCont(env, cont, Val.Int n) | Exp.Var s -> Res.ApplyCont(env, cont, Env.find env s) | Exp.Call(e1, e2) -> Res.Eval(env, Cont.Call1(e2)::cont, e1) let apply_cont env cont v = match cont with | [] -> Res.Done v | Cont.Call1(e)::cont' -> Res.Eval(env, Cont.Call2(v)::cont', e) | Cont.Call2(v')::cont' -> Res.ApplyCont(env, cont', call v' v) let rec trampoline r = match r with | Res.Done v -> v | Res.Eval(env, cont, e) -> trampoline (eval env cont e) | Res.ApplyCont(env, cont, v) -> trampoline (apply_cont env cont v) module Res = struct type t = | Done of Val.t | Eval of Env.t * Cont.t * Exp.t | ApplyCont of Env.t * Cont.t * Val.t end

Slide 35

Slide 35 text

εςοϓ࣮ߦ ௨ৗͷΠϯλϓϦλͱ͸ผʹɺεςοϓ͝ͱʹΠϯλϓϦλ ͷঢ়ଶΛදࣔ͢ΔTUFQXJTFΠϯλϓϦλ͕͋Δ let rec trampoline’ r = match r with | Res.Done v -> v | Res.Eval(env, cont, e) -> pause_and_display r; trampoline (eval env cont e) | Res.ApplyCont(env, cont, v) -> pause_and_display r; trampoline (apply_cont env cont v)

Slide 36

Slide 36 text

ͪΐͬͱσϞ EVOFFYFDTUFQXJTFFYF

Slide 37

Slide 37 text

຤ඌݺͼग़͠࠷దԽ ຤ඌ࠶ؼ (letrec [f [acc xs] (if (nil? xs) acc (f (+ acc (car xs)) (cdr xs)))] (f 0 (list 1 2 3))) ඇ຤ඌ࠶ؼ (letrec [f [xs] (if (nil? xs) 0 (+ (car xs) (f (cdr xs))))] (f (list 1 2 3))) w φΠʔϒͳ࣮૷ͩͱ຤ඌ࠶ؼͰ΋࠶ ؼ͢Δ͝ͱʹܧଓελοΫɺม਺ε λοΫͱ΋ʹੵΈ্͕ͬͯ͠·͏ w ؔ਺͕຤ඌݺͼग़͠ͷܗʹͳͬͯ ͍Δ৔߹ɺؔ਺ద༻ͷ࣌఺Ͱܧଓ ελοΫͷઌ಄ʹ$POU&OW͕ࡌͬ ͍ͯΔɻͦΕʹରԠ͢Δม਺؀ڥ ͱͱ΋ʹελοΫ͔Β֎ͯ͠΍Δͩ ͚Ͱ຤ඌݺͼग़͠࠷దԽ͕࣮૷Ͱ ͖Δ

Slide 38

Slide 38 text

΋͏ҰճσϞ EVOFFYFDTUFQXJTFFYF

Slide 39

Slide 39 text

຤ඌݺͼग़͠࠷దԽ w ߦ௥ՃͰ຤ඌݺͼग़͠࠷దԽ͕࣮૷Ͱ͖ͨ

Slide 40

Slide 40 text

͋Εɺݶఆܧଓ͸ʁ

Slide 41

Slide 41 text

ࠓޙͷ՝୊ w ݶఆܧଓΛ࣮૷͍ͨ͠ɾɾɾ w Πϝʔδ͸͋Δɻ͋ͱͪΐͬͱʢόά͍ͬͯΔʣ w Ϋϩʔδϟ͕େม w ؔ਺ͷ෦෼ద༻ w εςοϓ࣮ߦΛਐԽ w 6*ͷ޲্ w Τϥʔ࣌ʹఀࢭɺר͖໭͠ɺࣜΛҰ෦มߋ

Slide 42

Slide 42 text

ݴ͍͍ͨ͜ͱ w &P1-͸͍͍ͧ w ܧଓ౉͠ελΠϧ͸͍͍ͧ w EFGVODUJPOBMJ[BUJPO͸͍͍ͧ w ͪͳΈʹࠓճͷ͜Εɺ$&,ػցͱ͍͏ந৅ػցͱ΄΅ಉ Ұͷ΋ͷͷΑ͏ͩʢࢿྉ࡞੒தʹ஌ͬͨʣ w ఏএऀ͸'FMMFJTFOͱ'MBUUͷ໛༷ɻ΍ͬͺΓ-JTQ͔

Slide 43

Slide 43 text

ଓ͖͸ϒϩάͰ https://zehnpaard.hatenablog.com