SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
Non-Imperative Functional Programming
          序章: 要求駆動I/O

             2012-04-22

               山下伸夫
          nobsun@sampou.org
IO は命令書だ

main :: IO ()
main = do input    <- openFile "i" ReadMode
          contents <- hGetContents input
          hClose input
          output    <- openFile "o" WriteMode
          hPutStr output contents
          hClose output
          return ()
すぐやらなくてもいいLazyな命令
      書いちゃだめでしょ!

main :: IO ()
main = do input    <- openFile "i" ReadMode
          contents <- hGetContents input
          hClose input
          output    <- openFile "o" WriteMode
          hPutStr output contents
          hClose output
          return ()
命令書と実行結果
main :: IO ()
main = do line1 <-   getLine
          line2 <-   getLine
          putStrLn   line2
          putStrLn   line1
 ghci> :main
 world!↓
 Hello,↓
 Hello,
 world!
Lazyな命令があると?
main :: IO ()
main = do line1 <- lazy getLine
          line2 <- lazy getLine
          putStrLn line2
          putStrLn line1
          return ()
 ghci> :main

                    ?
Eagerな文脈にLazyな要素
main :: IO ()
main = do line1 <- lazy getLine
          line2 <- lazy getLine
          putStrLn line2
          putStrLn line1
          return ()
 ghci> :main
 world!↓
 world!
 Hello,↓
 Hello,
Eagerな文脈にLazyな要素
                                    !
main :: IO ()
main = do line1 <- lazy getLine    険
          line2 <- lazy getLine
          putStrLn line2          危
                な
          putStrLn line1

               る
          return ()
 ghci> :main
 world!↓
              ぜ
 world!
 Hello,↓
 Hello,
             ま
全部Lazyにしてしまえ!
main :: IO ()
main = do line1 <- lazy getLine
          line2 <- lazy getLine
          lazy (putStrLn line2)
          lazy (putStrLn line1)
          return ()
 ghci> :main


                  ?
Lazy = 要求駆動
main :: IO ()
main = do line1 <- lazy getLine
          line2 <- lazy getLine
          lazy (putStrLn line2)
                                ね
          lazy (putStrLn line1)
          return ()            だ
 ghci> :main                  然
 ghci>
                 当
                。
               ま
要求駆動I/O

main :: IO ()
main = do
  line1 <- lazy getLine
  line2 <- lazy getLine
  prn2 <- lazy (putStrLn line2)
  prn1 <- lazy (putStrLn line1)
  return $! sequencing [force line1
                        ,force line2
                        ,prn2
                        ,prn1]
要求駆動I/O
                          ス
                         ー !
                       ォ え
force :: a -> ()
force !x = ()         フ 使
                       を
sequencing :: [()] -> ()
sequencing sq = case dropWhile (()==) sq of
                  [] -> ()
IO は命令書だ

main :: IO ()
main = do
  input    <- lazy $ openFile "i" ReadMode
  icloseR <- lazy $ hClose input
  output   <- lazy $ openFile "o" WriteMode
  ocloseR <- lazy $ hClose output
  contents <- hGetContents input
  printR   <- lazy $ hPutStr output contents
  return $! sequencing
          $ [printR,icloseR,ocloseR]
ああ IO ()

main :: IO ()
main = do
  input    <- lazy $ openFile "i" ReadMode
  icloseR <- lazy $ hClose input
  output   <- lazy $ openFile "o" WriteMode
  ocloseR <- lazy $ hClose output
  contents <- hGetContents input
  printR   <- lazy $ hPutStr output contents
  return $! sequencing
          $ [printR,icloseR,ocloseR]
ああ IO ()



型はプログラムの
仕様じゃなかった
対話:: a :-> b としてのI/O

type a :-> b = OI a -> b

mainR :: a :-> ()
runI :: (a :-> b) -> IO b

main = runI mainR
oi package


実装についてはオイオイ
    to be continued

Weitere ähnliche Inhalte

Was ist angesagt?

PHPerのためのPerl入門@ Kansai.pm#12
PHPerのためのPerl入門@ Kansai.pm#12PHPerのためのPerl入門@ Kansai.pm#12
PHPerのためのPerl入門@ Kansai.pm#12Kazuki KOMORI
 
Writing a compiler in go
Writing a compiler in goWriting a compiler in go
Writing a compiler in goYusuke Kita
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲームNoritada Shimizu
 
プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話tatsunori ishikawa
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDayfcofdezc
 
Perl6 operators and metaoperators
Perl6   operators and metaoperatorsPerl6   operators and metaoperators
Perl6 operators and metaoperatorsSimon Proctor
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
 
Асинхронность и многопоточность в Яндекс.Такси — Дмитрий Курилов
Асинхронность и многопоточность в Яндекс.Такси — Дмитрий КуриловАсинхронность и многопоточность в Яндекс.Такси — Дмитрий Курилов
Асинхронность и многопоточность в Яндекс.Такси — Дмитрий КуриловYandex
 
Getting Rest With Webmachine
Getting Rest With WebmachineGetting Rest With Webmachine
Getting Rest With Webmachinekevsmith
 
3分くらいで分かるassert()
3分くらいで分かるassert()3分くらいで分かるassert()
3分くらいで分かるassert()Ippei Ogiwara
 
言語の設計判断
言語の設計判断言語の設計判断
言語の設計判断nishio
 
Clojure + MongoDB on Heroku
Clojure + MongoDB on HerokuClojure + MongoDB on Heroku
Clojure + MongoDB on HerokuNaoyuki Kakuda
 
String C# - Lec10 (Workshop on C# Programming: Learn to Build)
String C# - Lec10 (Workshop on C# Programming: Learn to Build)String C# - Lec10 (Workshop on C# Programming: Learn to Build)
String C# - Lec10 (Workshop on C# Programming: Learn to Build)Jannat Ruma
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Lin Yo-An
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesOXUS 20
 

Was ist angesagt? (20)

Rakudo
RakudoRakudo
Rakudo
 
PHPerのためのPerl入門@ Kansai.pm#12
PHPerのためのPerl入門@ Kansai.pm#12PHPerのためのPerl入門@ Kansai.pm#12
PHPerのためのPerl入門@ Kansai.pm#12
 
Writing a compiler in go
Writing a compiler in goWriting a compiler in go
Writing a compiler in go
 
Mozilla とブラウザゲーム
Mozilla とブラウザゲームMozilla とブラウザゲーム
Mozilla とブラウザゲーム
 
Introduzione a C#
Introduzione a C#Introduzione a C#
Introduzione a C#
 
プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDay
 
Perl6 operators and metaoperators
Perl6   operators and metaoperatorsPerl6   operators and metaoperators
Perl6 operators and metaoperators
 
Shell and perl scripting classes in mumbai
Shell and perl scripting classes in mumbaiShell and perl scripting classes in mumbai
Shell and perl scripting classes in mumbai
 
Everyday's JS
Everyday's JSEveryday's JS
Everyday's JS
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Асинхронность и многопоточность в Яндекс.Такси — Дмитрий Курилов
Асинхронность и многопоточность в Яндекс.Такси — Дмитрий КуриловАсинхронность и многопоточность в Яндекс.Такси — Дмитрий Курилов
Асинхронность и многопоточность в Яндекс.Такси — Дмитрий Курилов
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
Getting Rest With Webmachine
Getting Rest With WebmachineGetting Rest With Webmachine
Getting Rest With Webmachine
 
3分くらいで分かるassert()
3分くらいで分かるassert()3分くらいで分かるassert()
3分くらいで分かるassert()
 
言語の設計判断
言語の設計判断言語の設計判断
言語の設計判断
 
Clojure + MongoDB on Heroku
Clojure + MongoDB on HerokuClojure + MongoDB on Heroku
Clojure + MongoDB on Heroku
 
String C# - Lec10 (Workshop on C# Programming: Learn to Build)
String C# - Lec10 (Workshop on C# Programming: Learn to Build)String C# - Lec10 (Workshop on C# Programming: Learn to Build)
String C# - Lec10 (Workshop on C# Programming: Learn to Build)
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI Examples
 

Andere mochten auch (17)

El agua es una riqueza
El agua es una riquezaEl agua es una riqueza
El agua es una riqueza
 
Social Networks
Social NetworksSocial Networks
Social Networks
 
Fisling
FislingFisling
Fisling
 
digitális lábnyom 1
digitális lábnyom 1digitális lábnyom 1
digitális lábnyom 1
 
Other corporate profile
Other corporate profileOther corporate profile
Other corporate profile
 
Gerlee tungaa
Gerlee tungaaGerlee tungaa
Gerlee tungaa
 
The biofeedback mouse
The biofeedback mouseThe biofeedback mouse
The biofeedback mouse
 
Gerlee tungaaaaaa
Gerlee tungaaaaaaGerlee tungaaaaaa
Gerlee tungaaaaaa
 
Gerlee tungaa
Gerlee tungaaGerlee tungaa
Gerlee tungaa
 
00 introduction
00   introduction00   introduction
00 introduction
 
Susses criteria
Susses criteriaSusses criteria
Susses criteria
 
2share2
2share22share2
2share2
 
Cbl group 3
Cbl group 3Cbl group 3
Cbl group 3
 
Nutritional skincare
Nutritional skincareNutritional skincare
Nutritional skincare
 
Cbl group 3
Cbl group 3Cbl group 3
Cbl group 3
 
Manduuhai
ManduuhaiManduuhai
Manduuhai
 
Manduuhai
ManduuhaiManduuhai
Manduuhai
 

Ähnlich wie ああLazy io

"Lego Programming" with Lorzy
"Lego Programming" with Lorzy"Lego Programming" with Lorzy
"Lego Programming" with Lorzyclkao
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaWiem Zine Elabidine
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix TaskHermann Hueck
 
Transition graph using free monads and existentials
Transition graph using free monads and existentialsTransition graph using free monads and existentials
Transition graph using free monads and existentialsAlexander Granin
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonakaptur
 
Metaprogramming in Haskell
Metaprogramming in HaskellMetaprogramming in Haskell
Metaprogramming in HaskellHiromi Ishii
 
Modern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlModern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlNova Patch
 
エンタープライズ・クラウドと 並列・分散・非同期処理
エンタープライズ・クラウドと 並列・分散・非同期処理エンタープライズ・クラウドと 並列・分散・非同期処理
エンタープライズ・クラウドと 並列・分散・非同期処理maruyama097
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分bob_is_strange
 
6. processes and threads
6. processes and threads6. processes and threads
6. processes and threadsMarian Marinov
 
Frege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMFrege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMDierk König
 
Programming simple games with a raspberry pi and
Programming simple games with a raspberry pi andProgramming simple games with a raspberry pi and
Programming simple games with a raspberry pi andKellyn Pot'Vin-Gorman
 

Ähnlich wie ああLazy io (18)

05. haskell streaming io
05. haskell streaming io05. haskell streaming io
05. haskell streaming io
 
"Lego Programming" with Lorzy
"Lego Programming" with Lorzy"Lego Programming" with Lorzy
"Lego Programming" with Lorzy
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix Task
 
Transition graph using free monads and existentials
Transition graph using free monads and existentialsTransition graph using free monads and existentials
Transition graph using free monads and existentials
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
Metaprogramming in Haskell
Metaprogramming in HaskellMetaprogramming in Haskell
Metaprogramming in Haskell
 
Modern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlModern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in Perl
 
エンタープライズ・クラウドと 並列・分散・非同期処理
エンタープライズ・クラウドと 並列・分散・非同期処理エンタープライズ・クラウドと 並列・分散・非同期処理
エンタープライズ・クラウドと 並列・分散・非同期処理
 
Haskell Jumpstart
Haskell JumpstartHaskell Jumpstart
Haskell Jumpstart
 
Swiftの関数型っぽい部分
Swiftの関数型っぽい部分Swiftの関数型っぽい部分
Swiftの関数型っぽい部分
 
Kotlin Coroutines - the new async
Kotlin Coroutines - the new asyncKotlin Coroutines - the new async
Kotlin Coroutines - the new async
 
Perl세미나
Perl세미나Perl세미나
Perl세미나
 
Perl세미나
Perl세미나Perl세미나
Perl세미나
 
6. processes and threads
6. processes and threads6. processes and threads
6. processes and threads
 
Frege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMFrege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVM
 
Programming simple games with a raspberry pi and
Programming simple games with a raspberry pi andProgramming simple games with a raspberry pi and
Programming simple games with a raspberry pi and
 

Kürzlich hochgeladen

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 

Kürzlich hochgeladen (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

ああLazy io

  • 1. Non-Imperative Functional Programming 序章: 要求駆動I/O 2012-04-22 山下伸夫 nobsun@sampou.org
  • 2. IO は命令書だ main :: IO () main = do input <- openFile "i" ReadMode contents <- hGetContents input hClose input output <- openFile "o" WriteMode hPutStr output contents hClose output return ()
  • 3. すぐやらなくてもいいLazyな命令 書いちゃだめでしょ! main :: IO () main = do input <- openFile "i" ReadMode contents <- hGetContents input hClose input output <- openFile "o" WriteMode hPutStr output contents hClose output return ()
  • 4. 命令書と実行結果 main :: IO () main = do line1 <- getLine line2 <- getLine putStrLn line2 putStrLn line1 ghci> :main world!↓ Hello,↓ Hello, world!
  • 5. Lazyな命令があると? main :: IO () main = do line1 <- lazy getLine line2 <- lazy getLine putStrLn line2 putStrLn line1 return () ghci> :main ?
  • 6. Eagerな文脈にLazyな要素 main :: IO () main = do line1 <- lazy getLine line2 <- lazy getLine putStrLn line2 putStrLn line1 return () ghci> :main world!↓ world! Hello,↓ Hello,
  • 7. Eagerな文脈にLazyな要素 ! main :: IO () main = do line1 <- lazy getLine 険 line2 <- lazy getLine putStrLn line2 危 な putStrLn line1 る return () ghci> :main world!↓ ぜ world! Hello,↓ Hello, ま
  • 8. 全部Lazyにしてしまえ! main :: IO () main = do line1 <- lazy getLine line2 <- lazy getLine lazy (putStrLn line2) lazy (putStrLn line1) return () ghci> :main ?
  • 9. Lazy = 要求駆動 main :: IO () main = do line1 <- lazy getLine line2 <- lazy getLine lazy (putStrLn line2) ね lazy (putStrLn line1) return () だ ghci> :main 然 ghci> 当 。 ま
  • 10. 要求駆動I/O main :: IO () main = do line1 <- lazy getLine line2 <- lazy getLine prn2 <- lazy (putStrLn line2) prn1 <- lazy (putStrLn line1) return $! sequencing [force line1 ,force line2 ,prn2 ,prn1]
  • 11. 要求駆動I/O ス ー ! ォ え force :: a -> () force !x = () フ 使 を sequencing :: [()] -> () sequencing sq = case dropWhile (()==) sq of [] -> ()
  • 12. IO は命令書だ main :: IO () main = do input <- lazy $ openFile "i" ReadMode icloseR <- lazy $ hClose input output <- lazy $ openFile "o" WriteMode ocloseR <- lazy $ hClose output contents <- hGetContents input printR <- lazy $ hPutStr output contents return $! sequencing $ [printR,icloseR,ocloseR]
  • 13. ああ IO () main :: IO () main = do input <- lazy $ openFile "i" ReadMode icloseR <- lazy $ hClose input output <- lazy $ openFile "o" WriteMode ocloseR <- lazy $ hClose output contents <- hGetContents input printR <- lazy $ hPutStr output contents return $! sequencing $ [printR,icloseR,ocloseR]
  • 15. 対話:: a :-> b としてのI/O type a :-> b = OI a -> b mainR :: a :-> () runI :: (a :-> b) -> IO b main = runI mainR