TaPL 読書会 20. Recursive Types

83fb9f537f0aedcfbfc22dc395e85c84?s=47 daimatz
December 05, 2012

TaPL 読書会 20. Recursive Types

83fb9f537f0aedcfbfc22dc395e85c84?s=128

daimatz

December 05, 2012
Tweet

Transcript

  1.  8FE !EBJNBU[ 5B1-ಡॻձ 3FDVSTJWF5ZQFT

  2. 3FDVSTJWF5ZQFT ‣ ͰϦετߏ଄ΛϓϦϛςΟϒܕͱ͢Δ֦ுΛࣔͨ͠  ͦͷଞʹ΋ΩϡʔɺπϦʔͳͲ΋ಉ༷ͷߏ଄ͱݴ͑Δ ‣ ࣅͨΑ͏ͳ࠶ؼతͳߏ଄Λ࣋ͭ͜ΕΒͷσʔλܕΛͦΕͧΕ ΛϓϦϛςΟϒͱ͢Δͷ͸خ͘͠ͳ͍ ‣ ୯७ͳཁૉ͔Β͜ΕΒΛఆٛͰ͖Δ࠶ؼܕΛߟ͑Δ

    2
  3. 3FDVSTJWF5ZQFT ‣ Ϧετߏ଄͸࣍ͷΑ͏ʹߟ͑Δ͜ͱ͕Ͱ͖Δ ‣ ͔͜͠͠Ε͸྆ลʹ/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
  4. &YBNQMFT ‣ ॻ͚Δ΋ͷ͸ )BTLFMMͰॻ͍ͯΈ·ͨ͠  IUUQTHJTUHJUIVCDPN 4

  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 9G 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. )VOHSZ'VODUJPOT ‣ ()$ͷ5FYU1SJOUG Մม௕Ҿ਺ ͸ܕΫϥεΛؤுͬͯۦ࢖ ͢Δ͜ͱͰ࣮ݱ͍ͯ͠Δ  ܕਪ࿦ΊͬͪΌେมΒ͍͠  IUUQEIBUFOBOFKQHPUI@XSJTU@DVU

    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)))
  10. 1SPDFTT ‣ ϓϩηεͷ࢖͍ํ͸ ͷΑ͏ʹ͢Δ 10 curr (send 20 (send 3

    (send 5 p)))
  11. 0CKFDUT ‣ ϨίʔυܕΛΦϒδΣΫτͱߟ͑ΔɻΦϒδΣΫτ಺ͷϝ ιουΛݺͼग़͢ͱ৽͍͠ΦϒδΣΫτΛੜ੒ͯ͠ฦ͢ɻ ‣ Χ΢ϯλΦϒδΣΫτͷྫ 11

  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. 3FDVSTJWF7BMVFTGSPN 3FDVSTJWF5ZQFT ‣ ࠶ؼܕΛ࢖ͬͯෆಈ఺ԋࢉࢠΛఆٛͰ͖Δɻ ʹܕΛ෇͚Δɻ  YY ͱ͍͏ͱ͜Ζ͔ΒɺY͸ؔ਺ܕ͔ͭͦͷҾ਺ͷܕͰͳ ͚Ε͹͍͚ͳ͍Θ͚͕ͩɺ͕ͦͪ͜ΐ͏Ͳ࠶ؼܕͷఆٛʹ ౰ͯ͸·Δ

    ‣ ڧਖ਼نԽੑΛյ͢ྫ͕࡞ΕΔ ͜ͷؔ਺ʹVOJUΛ౉͢ͱධՁ͕ࢭ·Βͳ͍ɻ ‣ )BTLFMMͰ͸0DDVST$IFDLͷ͓͔͛Ͱ͜Μͳྫ͸ ௚઀͸  ॻ͚ͳ͍ 13 fix = f. ( x. f ( x x )) ( x. f ( x x ))
  14. 6OUZQFE-BNCEB$BMDVMVT  3FEVY ‣ লུ͍ͤͯͩ͘͞͞ 14

  15. 'PSNBMJUJFT ‣ ࠶ؼͷఆࣜԽͷํ๏͸ͭ͋Δɻ  FRVJSFDVSTJWFBQQSPBDI  JTPSFDVSTJWFBQQSPBDI ‣ ຊ࣭తͳҧ͍ʹ͍ͭͯ͸ͱͷؔ܎Λߟ ͑Δ

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

    ‣ ར఺ɿ͜Ε·Ͱݟ͖ͯͨܕγεςϜͱൺ΂ͯʮ࠶ؼܕʹΑͬͯ ܕͷදݱ͕࠶ؼతʹͰ͖ΔΑ͏ʹͳΔʯͱ͍͏͜ͱ͚͕ͩҧ͏  ܕͷؼೲ๏ʹΑΒͳ͍ఆٛ΍ఆཧɺূ໌͸͜Ε·Ͱͱಉ͡Α ͏ʹͰ͖Δ ‣ JOpOJUFTUSVDUVSFΛͦͷ··Ͱ͸ѻ͑ͳ͍ͨΊɺ UZQFDIFDLFSͷ໾ׂ͸େมʹͳΔ  ͞ΒʹଞͷܕγεςϜΛಋೖ͢ΔͱෳࡶʹͳΓɺཧ࿦্೉͠ ͘ͳͬͨΓܕνΣοΫͷܾఆෆೳੑʹ΋ͭͳ͕Δ 16 µX.T [X 7! µX.T] T
  17. JTPSFDVSTJWFBQQSPBDI ‣ ͱ ‣ ͭͷදݱ͸ҟͳΔ͕ಉܕ 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. JTPSFDVSTJWFBQQSPBDI ‣ ࠶ؼܕʹର͢Δͭͷؔ਺Λಋೖ͢Δ  ͭͷಉܕͰ͋ΔܕΛߦ͖དྷ͢Δ΋ͷͰɺϓϦϛςΟϒͱ͠ ͯ༻ҙ͞ΕΔ 18 unfold [ µX.T

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

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

    GPMEͱVOGPMEͷܕ஫ऍ͕ҧ͍͍ͬͯͷ͸ɺ࣮ߦ࣌ʹܕ νΣοΫΛ͢Δ͔Βɻͨͩ͠XFMMUZQFEͳϓϩάϥϜͷධ ՁͳΒɺ͜ͷنଇΛద༻͢Δͱ͖ʹ͸ͭͷܕ͸ಉ͡ͷ͸ͣ 20 E-Unfldfld
  21. JTPSFDVSTJWFBQQSPBDI ‣ ͜ͷํ๏͸࠶ؼܕΛ࢖͏ͱ͖ʹ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Λߟ͑ͯɺ ͱͷؔ܎͸ʁ  ؆୯ʹߟ͑Δํ๏͸ɺFRVJSFDVSTJWFʹѻ͏͜ͱ 23 µX.Nat

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

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