SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
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

Contenu connexe

En vedette

Stateモナドの解説 前編
Stateモナドの解説 前編Stateモナドの解説 前編
Stateモナドの解説 前編
Masahiro Honma
 
Monads in python
Monads in pythonMonads in python
Monads in python
eldariof
 

En vedette (20)

Monads in perl
Monads in perlMonads in perl
Monads in perl
 
定理3
定理3定理3
定理3
 
レンズ (ぶつかり稽古の没プレゼン)
レンズ (ぶつかり稽古の没プレゼン)レンズ (ぶつかり稽古の没プレゼン)
レンズ (ぶつかり稽古の没プレゼン)
 
Git入門
Git入門Git入門
Git入門
 
ウヰスキーとPSGI
ウヰスキーとPSGIウヰスキーとPSGI
ウヰスキーとPSGI
 
Stateモナドの解説 前編
Stateモナドの解説 前編Stateモナドの解説 前編
Stateモナドの解説 前編
 
モナモナ言うモナド入門.tar.gz
モナモナ言うモナド入門.tar.gzモナモナ言うモナド入門.tar.gz
モナモナ言うモナド入門.tar.gz
 
Hachioji.pm in Machida の LT
Hachioji.pm in Machida の LTHachioji.pm in Machida の LT
Hachioji.pm in Machida の LT
 
すべてが@__kanになる
すべてが@__kanになるすべてが@__kanになる
すべてが@__kanになる
 
Math::Category
Math::CategoryMath::Category
Math::Category
 
TraitとMoose::Role
TraitとMoose::RoleTraitとMoose::Role
TraitとMoose::Role
 
カレーとHokkaidopm
カレーとHokkaidopmカレーとHokkaidopm
カレーとHokkaidopm
 
Arrows in perl
Arrows in perlArrows in perl
Arrows in perl
 
AnyEvent and Plack
AnyEvent and PlackAnyEvent and Plack
AnyEvent and Plack
 
循環参照のはなし
循環参照のはなし循環参照のはなし
循環参照のはなし
 
Levenshtein Automata
Levenshtein AutomataLevenshtein Automata
Levenshtein Automata
 
Monads in python
Monads in pythonMonads in python
Monads in python
 
モナモナ言うモナド入門
モナモナ言うモナド入門モナモナ言うモナド入門
モナモナ言うモナド入門
 
すごいMonad入門
すごいMonad入門すごいMonad入門
すごいMonad入門
 
YAPCレポートの舞台裏
YAPCレポートの舞台裏YAPCレポートの舞台裏
YAPCレポートの舞台裏
 

Similaire à Stateモナドの解説 後編

ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)
ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)
ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)
Dimitris Psounis
 

Similaire à Stateモナドの解説 後編 (15)

Ejercicios varios de algebra widmar aguilar
Ejercicios varios de  algebra   widmar aguilarEjercicios varios de  algebra   widmar aguilar
Ejercicios varios de algebra widmar aguilar
 
Send + More = Money – Let’s mash 2 monads to solve a simple CSP
Send + More = Money – Let’s mash 2 monads to solve a simple CSPSend + More = Money – Let’s mash 2 monads to solve a simple CSP
Send + More = Money – Let’s mash 2 monads to solve a simple CSP
 
como era... patroa
como era... patroacomo era... patroa
como era... patroa
 
Bessel functionsoffractionalorder1
Bessel functionsoffractionalorder1Bessel functionsoffractionalorder1
Bessel functionsoffractionalorder1
 
Communication systems solution manual 5th edition
Communication systems solution manual 5th editionCommunication systems solution manual 5th edition
Communication systems solution manual 5th edition
 
Ejercicio 211 del libro de baldor
Ejercicio 211 del libro de baldorEjercicio 211 del libro de baldor
Ejercicio 211 del libro de baldor
 
Prelude to halide_public
Prelude to halide_publicPrelude to halide_public
Prelude to halide_public
 
ejercicio 211 del libro de Baldor
ejercicio 211 del libro de Baldorejercicio 211 del libro de Baldor
ejercicio 211 del libro de Baldor
 
Ejercicio 211 del libro de Baldor
Ejercicio 211 del libro de BaldorEjercicio 211 del libro de Baldor
Ejercicio 211 del libro de Baldor
 
Playing with State Monad
Playing with State MonadPlaying with State Monad
Playing with State Monad
 
Ejercicios prueba de algebra de la UTN- widmar aguilar
Ejercicios prueba de algebra de la UTN-  widmar aguilarEjercicios prueba de algebra de la UTN-  widmar aguilar
Ejercicios prueba de algebra de la UTN- widmar aguilar
 
Geurdes Monte Växjö
Geurdes Monte VäxjöGeurdes Monte Växjö
Geurdes Monte Växjö
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in Scala
 
ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)
ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)
ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)
 
Magnet Design - Hollow Cylindrical Conductor
Magnet Design - Hollow Cylindrical ConductorMagnet Design - Hollow Cylindrical Conductor
Magnet Design - Hollow Cylindrical Conductor
 

Dernier

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Stateモナドの解説 後編

  • 1. State 2009 6 1 id:hiratara
  • 2. • State • (>>=) State
  • 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)
  • 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