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. 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)
  2. 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
  3. 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)))
  4. 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)))
  5. 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 } }
  6. 13.

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

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

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

     ۩ମతʹ͸ɺͱͦΕΛҰஈ֊؆໿ͨ͠ ͷؔ܎ 15 µX.T [X 7! µX.T] T NatList NatList = < nil : Unit, cons : { Nat, NatList } >
  8. 16.

    FRVJSFDVSTJWFBQQSPBDI ‣ ͱ ‣ ͭͷදݱ͕ఆٛͱͯ͠ಉ͡Ͱ͋Δ ৗʹೖΕସ͑Մೳ ͱ͢Δ  ʮ͋Δܕͷ߲͕ɺҧ͏ܕͷ߲Λظ଴͢Δؔ਺ͷҾ਺ͱͯ͠ڐ ͞ΕΔ͜ͱʯΛอূ͢Δͷ͸UZQFDIFDLFSͷ໾ׂͱ͢Δ

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

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

    GPMEͱVOGPMEͷܕ஫ऍ͕ҧ͍͍ͬͯͷ͸ɺ࣮ߦ࣌ʹܕ νΣοΫΛ͢Δ͔Βɻͨͩ͠XFMMUZQFEͳϓϩάϥϜͷධ ՁͳΒɺ͜ͷنଇΛద༻͢Δͱ͖ʹ͸ͭͷܕ͸ಉ͡ͷ͸ͣ 20 E-Unfldfld
  11. 21.

    JTPSFDVSTJWFBQQSPBDI ‣ ͜ͷํ๏͸࠶ؼܕΛ࢖͏ͱ͖ʹGPMEͱVOGPMEΛ໌ࣔ͠ͳ͚ Ε͹ͳΒͳ͍ ‣ ͔࣮͠͠ࡍʹ͸ɺଞͷه๏ʹࠞͥͯ͜ΕΒͷهड़ΛʮӅ͢ʯ ͜ͱ͕Ͱ͖Δɻ  ྫ͑͹.-Ͱ͸EBUBUZQFఆٛ͸࠶ؼܕͷಋೖΛ҉ࣔ͢Δ w

    ͦͷܕͷ஋Λ࡞ΔͨΊʹίϯετϥΫλΛ࢖͏ͱ͖ʹ͸ શͯGPME͞ΕɺύλʔϯϚονʹݱΕΔ͢΂ͯͷίϯε τϥΫλ͸VOGPME͞ΕΔ  ಉ༷ʹ+BWBͰ͸Ϋϥεఆ͕ٛ࠶ؼܕͷಋೖΛ҉ࣔ͢Δ w ΦϒδΣΫτ಺ͷϝιουݺͼग़͕͠VOGPMEΛߦ͏ 21
  12. 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 = ...