SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
「やらなければならないこと」としてのHaskellの
Monad
山本悠滋
2014-05-11
はじめまして!
山本悠滋(@igrep) 25歳♂
Sansanという会社でRailsを触ってます。
Haskellの勉強会をツキイチでやってます!
今日話すこと・話さないこと
やたら難しいと言われるHaskellのMonadについて私なりの理解を説明してみま
す。
これ↓
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
-- 以下略
圏論のモナドはよくわかりません!
「もう知ってるぜ!」という部分も多いかとは思いますがあしからず。
先に結論
HaskellのMonadを
「関数が値を返す度にやらなければならないこと」
として捉えると、
なんだかいろいろスッキリ理解できたよ!
Monadは型クラス
(再掲)こんな感じのやつ↓
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
-- 以下略
そもそも型クラスって
まぁ言ってしまえば
JavaやC#などのinterface
Rubyのmix-inされるmodule
みたいなもの
=> 同じ振る舞いをする型達を、ひっくるめて扱う仕組み
型クラスのいいところ
新しい型を作った時、必要なメソッドを定義しておくだけで、
その型クラスに対して使える色々な関数が使える!
新しい型に固有な、新しい型のみに必要な処理は、
必要な(未定義の)メソッドに全て押し込めちゃえばいい!
Monadの場合は?
必要なreturnメソッドと(>>=)メソッドを定義しておくだけで!
do記法を使ったり、
いろいろできる!
新しいMonad(クラスのインスタンス)に固有な処理は、
必要な(未定義の)メソッドに全て押し込めちゃえばいい!
ここで(>>=)を見てみましょう。
(>>=) :: m a -> (a -> m b) -> m b
他の型クラスと同様、Monadも必要なメソッド(>>=)に、
固有の処理を書くことで、抽象化している。
ここで(>>=)を見てみましょう。
(>>=) :: m a -> (a -> m b) -> m b
例えば...
Maybeでは、m aのaを(a -> m b)に渡すため、
Just aなのかNothingなのかどうか判定している
Readerでは、m aのaを(a -> m b)に渡すため、
関数に、足りない分の引数を与えている
Parserでは、m aのaを(a -> m b)に渡すため、
与えられた文字列を消費している
ここで(>>=)を見てみましょう。
(>>=) :: m a -> (a -> m b) -> m b
いずれも、
m aのaを(a -> m b)に渡すために、
必要な処理をそれぞれ持っている。
そして、
(a -> m b)が返したm bをまた別の関数に渡すよう、必要な処理を繰り返している。
つまり!!
Monadは(>>=)のm aのaを(a -> m b)に渡す部分に、
やらなければならないことをすべて押し込んでいる!
(a -> m b)が値を返す度に、
別の(a -> m b)な関数にMonadに包まれていない値を渡すために、
やらなければならないことを(>>=)に任せている。
要するに!!
(a -> m b)という型の、
値を返す度に、やらなければならないことがある関数
がいっぱいある時、Monadは役に立つ。
例えば!!
「失敗したのかどうか、実行する度に確認しなきゃいけない」
関数がいっぱいあるときは、
=> Maybe Monad
「実行する度に、結果のログを追記しなきゃいけない」
関数がいっぱいあるときは、
=> Writer Monad
「実行する度に、入出力など、何かしら副作用のある計算をしなきゃいけない」
関数がいっぱいあるときは、
=> IO Monad
で、なにがうれしいの?
「文脈だ!」とか、
「手続きだ!」とか、
「いやモナドだ!」とか、
いろいろとMonadを簡単に言い換える言葉は多く出てきたけど、
どれも結構抽象的で、ピンと来なかった。
で、なにがうれしいの?
それに対して「関数が値を返すたびにやらなければならないこと」だと、
少なくともわれわれプログラマはイメージしやすい(はず)。
あくまでも「型クラスとしてのMonad」の性質に即して説明できる。
「どう役立つか」説明できるので、メリットを感じさせやすい。
特に「毎回やらなければならないことを一箇所にまとめられる」と言えば、 コードのDRYさに敏
感な人に効果がありそう。
「失敗系」とか「状態系」とかに分けなくていい。
まとめ
HaskellのMonadは型クラス
型クラスは、同じ振る舞いをする型達を、まとめて扱うために使用する。
Monadは「値を返すたびにやらなければならないこと」をまとめる。
こうして、おびただしい数のMonadチュートリアルの歴史に、 また一つ新たな
ページを刻んでしまうのであった...。

Más contenido relacionado

Destacado

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Destacado (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

2014 05-11-関数型lt大会-「やらなければならないこと」としてのhaskellのmonad