150

# TaPL 読書会 20. Recursive Types

#### daimatz

December 05, 2012

## Transcript

2
3. ### 3FDVSTJWF5ZQFT ‣ Ϧετߏ଄͸࣍ͷΑ͏ʹߟ͑Δ͜ͱ͕Ͱ͖Δ ‣ ͔͜͠͠Ε͸྆ลʹ/BU-JTUΛؚΉͨΊఆٛͱ͸͍͑ͳ͍ɻ ͜ͷΑ͏ͳ࠶ؼΛද͢ه߸ͱͯ͠Λ࢖͍ɺ࣍ͷΑ͏ʹද͢ ‣ ௚ײతʹ͸ʮ/BU-JTU͸ ͱ͍͏ํఔࣜΛຬͨ͢Α͏ͳແݶͷܕͰ͋Δʯͱ͍͏͜ͱ 3

µ NatList = h nil : Unit, cons : { Nat, NatList }i NatList = µX. h nil : Unit, cons : { Nat, X }i X = h nil : Unit, cons : { Nat, X }i

5. ### -JTUT ‣ Ϧετ͸ઌͷྫͷΑ͏ʹॻ͚Δ 5 nil = h nil = unit

i as NatList cons = n : Nat. l : NatList. h cons = { n, l }i as NatList isnil = l : NatList. case l of h nil = u i ) true | h cons = p i ) false hd = l : NatList. case l of h nil = u i ) 0 | h cons = p i ) p. 1 tl = l : NatList. case l of h nil = u i ) 1 | h cons = p i ) p. 2 sumlist = fix ( s : NatList ! Nat. l : NatList. if isnil l then 0 else plus ( hd l ) ( s ( tl l ))) data NatList = Nil () | Cons (Nat, NatList) deriving (Eq, Show) nil :: NatList nil = Nil () cons :: Nat -> NatList -> NatList cons = \n -> \l -> Cons (n, l) isnil :: NatList -> Bool isnil = \l -> case l of Nil _ -> True Cons _ -> False hd :: NatList -> Nat hd = \l -> case l of Nil _ -> 0 Cons p -> fst p tl :: NatList -> NatList tl = \l -> case l of Nil _ -> l Cons p -> snd p sumlist :: NatList -> Nat sumlist = \l -> if isnil l then 0 else hd l + sumlist (tl l)
6. ### )VOHSZ'VODUJPOT ‣ Ҿ਺Λ͍ͭ͘Ͱ΋औΕΔؔ਺  )BTLFMMͰ͸௚઀ॻ͚ͳ͍ͬΆ͍  0DDVST\$IFDL w 9G 9

ͷΑ͏ͳؔ਺͸࠶ؼ͕ࢭ·Βͳ͍ͷͰఆٛͰ͖ ͳ͍ w 1SPMPHͷ୯ҰԽ VOJpDBUJPO ͷͱ͖ʹΑ͘ى͜Δ 6 Hungry = µA. Nat ! A f = fix ( f : Nat ! Hungry. n : Nat. f) data Hungry = Hungry (Nat -> Hungry) -- hungry = \(Hungry n) -> hungry

7
8. ### 4USFBNT ‣ ೚ҙͷVOJUܕͷ஋ΛҾ਺ʹऔΓɺͦͷͨͼʹࣗવ਺ͱετ ϦʔϜͷϖΞΛฦ͢΋ͷ  ετϦʔϜʹର͢ΔIFBE UBJMͱɺҎ্ͷࣗવ਺Λੜ੒ ͢ΔແݶετϦʔϜVQGSPN 8 Stream

= µA. Unit ! { Nat, A } hd = s : Stream. ( s unit ) . 1 tl = s : Stream. ( s unit ) . 2 upfrom 0 = fix ( f : Nat ! Stream. n : Nat. : Unit. { n, f ( succ n )}) 0 data Stream a = Stream (() -> (Nat, Stream a)) hd_s :: Stream Nat -> Nat hd_s = \(Stream s) -> fst (s ()) tl_s :: Stream Nat -> Stream Nat tl_s = \(Stream s) -> snd (s ()) upfrom0 :: Stream Int upfrom0 = f 0 where f n = Stream (\_ -> (n, f (n+1)))
9. ### 1SPDFTT ‣ ετϦʔϜͷҰൠԽͱͯ͠ϓϩηε͕ߟ͑ΒΕΔ  ϓϩηε͸ࣗવ਺Λड͚औΓࣗવ਺ͱ৽͍͠ϓϩηεͷϖ ΞΛฦ͢ ‣ ͦΕ·Ͱʹ༩͑ΒΕͨ਺ͷ࿨Λܭࢉ͢Δϓϩηε Process =

µA. Nat ! { Nat, A } curr = s : Process. ( s 0) . 1 send = n : Nat. s : Process. ( s n ) . 2 p = fix ( f : Nat ! Process. acc : Nat. n : Nat. { acc + n, f ( acc + n )}) 0 data Process a = Process (Nat -> (Nat, Process a)) curr :: Process a -> Nat curr = \(Process s) -> fst (s 0) send :: Nat -> Process a -> Process a send = \n -> \(Process s) -> snd (s n) process :: Process a process = f 0 where f acc = Process (\n -> (acc+n, f (acc+n)))

(send 5 p)))

12. ### 0CKFDUT 12 data Counter = Counter { get :: Nat,

inc :: () -> Counter, dec :: () -> Counter } data X = X { x :: Nat } counter :: Counter counter = create \$ X { x = 0 } where create s = Counter { get = x s, inc = \_ -> create \$ X { x = x s + 1 }, dec = \_ -> create \$ X { x = x s - 1 } }
13. ### 3FDVSTJWF7BMVFTGSPN 3FDVSTJWF5ZQFT ‣ ࠶ؼܕΛ࢖ͬͯෆಈ఺ԋࢉࢠΛఆٛͰ͖Δɻ ʹܕΛ෇͚Δɻ  YY ͱ͍͏ͱ͜Ζ͔ΒɺY͸ؔ਺ܕ͔ͭͦͷҾ਺ͷܕͰͳ ͚Ε͹͍͚ͳ͍Θ͚͕ͩɺ͕ͦͪ͜ΐ͏Ͳ࠶ؼܕͷఆٛʹ ౰ͯ͸·Δ

‣ ڧਖ਼نԽੑΛյ͢ྫ͕࡞ΕΔ ͜ͷؔ਺ʹVOJUΛ౉͢ͱධՁ͕ࢭ·Βͳ͍ɻ ‣ )BTLFMMͰ͸0DDVST\$IFDLͷ͓͔͛Ͱ͜Μͳྫ͸ ௚઀͸  ॻ͚ͳ͍ 13 fix = f. ( x. f ( x x )) ( x. f ( x x ))

15. ### 'PSNBMJUJFT ‣ ࠶ؼͷఆࣜԽͷํ๏͸ͭ͋Δɻ  FRVJSFDVSTJWFBQQSPBDI  JTPSFDVSTJWFBQQSPBDI ‣ ຊ࣭తͳҧ͍ʹ͍ͭͯ͸ͱͷؔ܎Λߟ ͑Δ

 ۩ମతʹ͸ɺͱͦΕΛҰஈ֊؆໿ͨ͠ ͷؔ܎ 15 µX.T [X 7! µX.T] T NatList NatList = < nil : Unit, cons : { Nat, NatList } >
16. ### FRVJSFDVSTJWFBQQSPBDI ‣ ͱ ‣ ͭͷදݱ͕ఆٛͱͯ͠ಉ͡Ͱ͋Δ ৗʹೖΕସ͑Մೳ ͱ͢Δ  ʮ͋Δܕͷ߲͕ɺҧ͏ܕͷ߲Λظ଴͢Δؔ਺ͷҾ਺ͱͯ͠ڐ ͞ΕΔ͜ͱʯΛอূ͢Δͷ͸UZQFDIFDLFSͷ໾ׂͱ͢Δ

‣ ར఺ɿ͜Ε·Ͱݟ͖ͯͨܕγεςϜͱൺ΂ͯʮ࠶ؼܕʹΑͬͯ ܕͷදݱ͕࠶ؼతʹͰ͖ΔΑ͏ʹͳΔʯͱ͍͏͜ͱ͚͕ͩҧ͏  ܕͷؼೲ๏ʹΑΒͳ͍ఆٛ΍ఆཧɺূ໌͸͜Ε·Ͱͱಉ͡Α ͏ʹͰ͖Δ ‣ JOpOJUFTUSVDUVSFΛͦͷ··Ͱ͸ѻ͑ͳ͍ͨΊɺ UZQFDIFDLFSͷ໾ׂ͸େมʹͳΔ  ͞ΒʹଞͷܕγεςϜΛಋೖ͢ΔͱෳࡶʹͳΓɺཧ࿦্೉͠ ͘ͳͬͨΓܕνΣοΫͷܾఆෆೳੑʹ΋ͭͳ͕Δ 16 µX.T [X 7! µX.T] T
17. ### JTPSFDVSTJWFBQQSPBDI ‣ ͱ ‣ ͭͷදݱ͸ҟͳΔ͕ಉܕ JTPNPSQIJD Ͱ͋Δͱͯ͠ѻ͏ ‣ ܗࣜతʹ͸ͷల։͸ɺຊମͰ͋Δ5ͷதʹݱΕΔ͢΂ ͯͷ9ΛͰஔ͖׵͑ͨ΋ͷ

 ʹͳΔ  ྫ͑͹ɺ ͸ ʹల։Ͱ͖Δɻ 17 µX.T µX.T [X 7! µX.T] T µX. h nil : Unit, cons : { Nat, X }i µX.T [X 7! µX.T] T h nil : Unit, cons : { Nat, µX. h nil : Unit, cons : { Nat, X }i
18. ### JTPSFDVSTJWFBQQSPBDI ‣ ࠶ؼܕʹର͢Δͭͷؔ਺Λಋೖ͢Δ  ͭͷಉܕͰ͋ΔܕΛߦ͖དྷ͢Δ΋ͷͰɺϓϦϛςΟϒͱ͠ ͯ༻ҙ͞ΕΔ 18 unfold [ µX.T

] : µX.T ! [ X 7! µX.T ] T fold [ µX.T ] : [ X 7! µX.T ] T ! µX.T µX.T

20. ### JTPSFDVSTJWFBQQSPBDI ‣ GPMEͱVOGPME͕ରԠ͢Δ͜ͱ͸نଇͰΘ͔Δ  ରԠͨ͠VOGPMEʹૺ۰ͨ͠৔߹GPMEΛফ͢ w ͜ͷํ๏ͷ࣮૷ʹ͸৽ͨͳܕνΣοΫΞϧΰϦζϜ͕ඞ ཁ ষ 

GPMEͱVOGPMEͷܕ஫ऍ͕ҧ͍͍ͬͯͷ͸ɺ࣮ߦ࣌ʹܕ νΣοΫΛ͢Δ͔Βɻͨͩ͠XFMMUZQFEͳϓϩάϥϜͷධ ՁͳΒɺ͜ͷنଇΛద༻͢Δͱ͖ʹ͸ͭͷܕ͸ಉ͡ͷ͸ͣ 20 E-Unfldfld
21. ### JTPSFDVSTJWFBQQSPBDI ‣ ͜ͷํ๏͸࠶ؼܕΛ࢖͏ͱ͖ʹGPMEͱVOGPMEΛ໌ࣔ͠ͳ͚ Ε͹ͳΒͳ͍ ‣ ͔࣮͠͠ࡍʹ͸ɺଞͷه๏ʹࠞͥͯ͜ΕΒͷهड़ΛʮӅ͢ʯ ͜ͱ͕Ͱ͖Δɻ  ྫ͑͹.-Ͱ͸EBUBUZQFఆٛ͸࠶ؼܕͷಋೖΛ҉ࣔ͢Δ w

ͦͷܕͷ஋Λ࡞ΔͨΊʹίϯετϥΫλΛ࢖͏ͱ͖ʹ͸ શͯGPME͞ΕɺύλʔϯϚονʹݱΕΔ͢΂ͯͷίϯε τϥΫλ͸VOGPME͞ΕΔ  ಉ༷ʹ+BWBͰ͸Ϋϥεఆ͕ٛ࠶ؼܕͷಋೖΛ҉ࣔ͢Δ w ΦϒδΣΫτ಺ͷϝιουݺͼग़͕͠VOGPMEΛߦ͏ 21
22. ### 'PSNBMJUJFT ‣ .-Ͱॻ͍ͨ/BU-JTUͷྫͱ࣮ࡍͷҙຯΛࣔ͢ 22 type nlbody = Nil of unit

| Cons of int * nlbody let isnil l = match l with Nil (_) -> true | Cons (_) -> false let hd l = match l with Nil (_) -> 0 | Cons (i1, _) -> i1 let tl l = match l with Nil (_) -> l | Cons (_, tl) -> tl NLBody = h nil : Unit, cons : { Nat, NatList }i nil = fold [ NatList ](h nil = unit i as NLBody cons = n : Nat. l : NatList. fold [ NatList ]h cons = { n, l }i as NLBody isnil = l : NatList. case unfold [ NatList ] l of h nil = u i ) true | h cons = p i ) false hd = l : NatList. case unfold [ NatList ] l of h nil = u i ) 0 | h cons = p i ) p. 1 tl = ...
23. ### 4VCUZQJOH ‣ ࠶ؼܕͱଞͷϝδϟʔͳܕମܥͷ૊Έ߹Θͤ͸ʁ  ͜͜Ͱ͸෦෼ܕɻ/BUͷ෦෼ܕͰ͋Δ&WFOΛߟ͑ͯɺ ͱͷؔ܎͸ʁ  ؆୯ʹߟ͑Δํ๏͸ɺFRVJSFDVSTJWFʹѻ͏͜ͱ 23 µX.Nat

! (Even ⇥ X) µX.Even ! (Nat ⇥ X)
24. ### 4VCUZQJOH ‣ ઌͷྫ͸྆ํͱ΋ϓϩηεͷܕͱݟΔ͜ͱ͕Ͱ͖Δ  ਺Λ༩͑ΒΕͯɺผͷ਺ͱ৽͍͠ϓϩηεΛฦ͢  ͸೚ҙͷࣗવ਺Λऔͬͯۮ਺ΛੜΈग़͢  ͸೚ҙͷࣗવ਺ΛੜΈग़͕͢ɺۮ਺Λ༩ ͑ΒΕΔͱظ଴͞ΕΔ

 ؔ਺ͷ෦෼ܕͷ࿩Λߟ͑Δͱɺ௚ײతʹ͸લऀ͸ޙऀͷαϒλ Πϓͱظ଴͞ΕΔɻৄ͘͠͸ষɻ 24 µX.Nat ! (Even ⇥ X) µX.Nat ! (Even ⇥ X)