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

Stateモナドの解説 後編

on

  • 1,159 views

 

Statistics

Views

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

Actions

Likes
0
Downloads
15
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モナドの解説 後編 Stateモナドの解説 後編 Presentation Transcript

    • State 2009 6 1 id:hiratara
    • • State • (>>=) State
    • (1) (>>=)
    • Int State Int Int f Int State Int Int
    • f Int State Int Int ∋ a f Int State Int Int ∋ 1 (a+1, 2) 2 (a+2, 3) ... s0 (a+s0, s0+1)
    • : f 10 2 12 1 (11, 2) f 10 = 2 (12, 3) ... s0 (10+s0, s0+1)
    • f 1 (2, 1) 2 (4, 2) ... s0 (s0 × 2, s0)
    • 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)
    • (>>=) (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)
    • Int >>= State Int Int ∋ (s0 × 2, s0) (=<<) f f Int State Int Int ∋(s0 × 2 + s0, s0 + 1)
    • (1) • f • (>>=) f
    • (2) get put
    • put get • ( return (>>=) ) State • put get State
    • get • State • ( ) •
    • get • State • ( ) • → (s0, s0)
    • get get get = State $ s -> (s,s) → (s0, s0)
    • put s • State • () • s
    • put s • State • () • s → put s = ( (), s)
    • put s put put s = State $ _ -> ((),s) Int State Int Int put () State Int () → put s = ( (), s)
    • put s >>= put put s = State $ _ -> ((),s) Int State Int Int put (=<<) put () State Int () → put s = ( (), s)
    • (3)
    • State 0 1 1 1 2 2 2 3 3 ...
    • 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)
    • (s0 + 1, s0+1) = State Int Int 0 (1, 1) count = 1 (2, 2) ... s0 (s0 + 1, s0+1)
    • get put
    • Int get ∈ State Int Int put () State () Int () -> n+1 return Int State Int Int
    • put return >>= Int get ∈ State Int Int >>= put () State () Int return . ( ¥() -> n+1 ) >>= Int State Int Int
    • Int get = (s0, s0) ∈ State Int Int >>= put () ( (), s0 + 1) ∈ State () Int return . ( ¥() -> n+1 ) >>= Int ( s0 + 1, s0 + 1) ∈ State Int Int
    • • >>= • put get • do put get State
    • • runState evalState (v(s0), s(s0)) runState State Int Bool Int -> (Bool, Int) evalState Int Int -> Bool Int (Bool, Int) Bool