Stateモナドの解説 後編
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Stateモナドの解説 後編

on

  • 1,206 views

 

Statistics

Views

Total Views
1,206
Views on SlideShare
1,206
Embed Views
0

Actions

Likes
0
Downloads
17
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Stateモナドの解説 後編 Presentation Transcript

  • 1. State 2009 6 1 id:hiratara
  • 2. • State • (>>=) State
  • 3. (1) (>>=)
  • 4. Int State Int Int f Int State Int Int
  • 5. f Int State Int Int ∋ a f Int State Int Int ∋ 1 (a+1, 2) 2 (a+2, 3) ... s0 (a+s0, s0+1)
  • 6. : f 10 2 12 1 (11, 2) f 10 = 2 (12, 3) ... s0 (10+s0, s0+1)
  • 7. f 1 (2, 1) 2 (4, 2) ... s0 (s0 × 2, s0)
  • 8. f 1 (2, 1) 2 (4, 2) ... s0 (s0 × 2, s0) (=<<) f 1 1 (3, 2) 2 2 (6, 3) ... f 2+1 = 3 s0 (s0 × 2 + s0, s0 + 1)
  • 9. (>>=) (State x) >>= f = State $ s -> let (v,s') = x s in runState (f v) s' State(s0 ) (v(s0), s(s0)) ... (v, s') = (s0 × 2, s0) s = (s × 2, s) f v = f (s × 2) = (s × 2 + s0, s0 + 1) (f v) s' = (s × 2 + s0, s0 + 1) s = (s × 2 + s, s + 1) (s0 × 2, s0) >>= f = (s0 × 2 + s0, s0 + 1)
  • 10. Int >>= State Int Int ∋ (s0 × 2, s0) (=<<) f f Int State Int Int ∋(s0 × 2 + s0, s0 + 1)
  • 11. (1) • f • (>>=) f
  • 12. (2) get put
  • 13. put get • ( return (>>=) ) State • put get State
  • 14. get • State • ( ) •
  • 15. get • State • ( ) • → (s0, s0)
  • 16. get get get = State $ s -> (s,s) → (s0, s0)
  • 17. put s • State • () • s
  • 18. put s • State • () • s → put s = ( (), s)
  • 19. put s put put s = State $ _ -> ((),s) Int State Int Int put () State Int () → put s = ( (), s)
  • 20. put s >>= put put s = State $ _ -> ((),s) Int State Int Int put (=<<) put () State Int () → put s = ( (), s)
  • 21. (3)
  • 22. State 0 1 1 1 2 2 2 3 3 ...
  • 23. State 0 1 1 1 2 2 2 3 3 ... State Int Int 0 (1, 1) count = 1 (2, 2) ... s0 (s0 + 1, s0+1)
  • 24. (s0 + 1, s0+1) = State Int Int 0 (1, 1) count = 1 (2, 2) ... s0 (s0 + 1, s0+1)
  • 25. get put
  • 26. Int get ∈ State Int Int put () State () Int () -> n+1 return Int State Int Int
  • 27. put return >>= Int get ∈ State Int Int >>= put () State () Int return . ( ¥() -> n+1 ) >>= Int State Int Int
  • 28. Int get = (s0, s0) ∈ State Int Int >>= put () ( (), s0 + 1) ∈ State () Int return . ( ¥() -> n+1 ) >>= Int ( s0 + 1, s0 + 1) ∈ State Int Int
  • 29. • >>= • put get • do put get State
  • 30. • runState evalState (v(s0), s(s0)) runState State Int Bool Int -> (Bool, Int) evalState Int Int -> Bool Int (Bool, Int) Bool