SlideShare uma empresa Scribd logo
1 de 30
Baixar para ler offline
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

Mais conteúdo relacionado

Destaque

レンズ (ぶつかり稽古の没プレゼン)
レンズ (ぶつかり稽古の没プレゼン)レンズ (ぶつかり稽古の没プレゼン)
レンズ (ぶつかり稽古の没プレゼン)Masahiro Honma
 
ウヰスキーとPSGI
ウヰスキーとPSGIウヰスキーとPSGI
ウヰスキーとPSGIMasahiro Honma
 
Stateモナドの解説 前編
Stateモナドの解説 前編Stateモナドの解説 前編
Stateモナドの解説 前編Masahiro Honma
 
モナモナ言うモナド入門.tar.gz
モナモナ言うモナド入門.tar.gzモナモナ言うモナド入門.tar.gz
モナモナ言うモナド入門.tar.gzMasahiro Honma
 
Hachioji.pm in Machida の LT
Hachioji.pm in Machida の LTHachioji.pm in Machida の LT
Hachioji.pm in Machida の LTMasahiro Honma
 
すべてが@__kanになる
すべてが@__kanになるすべてが@__kanになる
すべてが@__kanになるMasahiro Honma
 
カレーとHokkaidopm
カレーとHokkaidopmカレーとHokkaidopm
カレーとHokkaidopmMasahiro Honma
 
循環参照のはなし
循環参照のはなし循環参照のはなし
循環参照のはなしMasahiro Honma
 
Monads in python
Monads in pythonMonads in python
Monads in pythoneldariof
 
モナモナ言うモナド入門
モナモナ言うモナド入門モナモナ言うモナド入門
モナモナ言うモナド入門Masahiro Honma
 
すごいMonad入門
すごいMonad入門すごいMonad入門
すごいMonad入門真一 北原
 
YAPCレポートの舞台裏
YAPCレポートの舞台裏YAPCレポートの舞台裏
YAPCレポートの舞台裏Masahiro Honma
 

Destaque (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レポートの舞台裏
 

Semelhante a Stateモナドの解説 後編

Ejercicios varios de algebra widmar aguilar
Ejercicios varios de  algebra   widmar aguilarEjercicios varios de  algebra   widmar aguilar
Ejercicios varios de algebra widmar aguilarWidmar Aguilar Gonzalez
 
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 CSPFilippo Vitale
 
Communication systems solution manual 5th edition
Communication systems solution manual 5th editionCommunication systems solution manual 5th edition
Communication systems solution manual 5th editionTayeen Ahmed
 
Playing with State Monad
Playing with State MonadPlaying with State Monad
Playing with State MonadDavid Galichet
 
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 aguilarWidmar Aguilar Gonzalez
 
Geurdes Monte Växjö
Geurdes Monte VäxjöGeurdes Monte Växjö
Geurdes Monte VäxjöRichard Gill
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in ScalaTim Dalton
 
ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)
ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)
ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 4 - ΤΕΛΕΣΤΕΣ και η ΔΟΜΗ ΕΛΕΓΧΟΥ (ΕΚΤΥΠΩΣΗ)Dimitris Psounis
 
Magnet Design - Hollow Cylindrical Conductor
Magnet Design - Hollow Cylindrical ConductorMagnet Design - Hollow Cylindrical Conductor
Magnet Design - Hollow Cylindrical ConductorPei-Che Chang
 

Semelhante a 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
 

Último

Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 

Último (20)

201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 

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