Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
自己関手の圏における モノイド対象 in Scala
Search
Kazuhiro Ichikawa
March 19, 2022
Programming
650
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
自己関手の圏における モノイド対象 in Scala
Kazuhiro Ichikawa
March 19, 2022
More Decks by Kazuhiro Ichikawa
See All by Kazuhiro Ichikawa
Tuples and Mirrors in Scala3 and Higher-Kinded Data
phenan
0
1.8k
ValiantParsing
phenan
1
140
Intro to typeclass in Scala
phenan
10
2.6k
Scalalr
phenan
1
2.6k
Other Decks in Programming
See All in Programming
気持ちぃ〜角丸 Squircle
ryu1013
0
130
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
350
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
130
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
110
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
110
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
450
Workers Cache を知る
syumai
0
350
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
210
dotNET_vulnclicker_intro.pdf
deka_morita
0
130
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
130
AWSに止められる覚悟してますか?
morizo_1984
2
520
The Rails Doctrine Decade
koic
2
530
Featured
See All Featured
Are puppies a ranking factor?
jonoalderson
2
4k
Believing is Seeing
oripsolob
1
240
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
260
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
550
The Curious Case for Waylosing
cassininazir
1
560
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
570
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.2k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
66k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
520
Visualization
eitanlees
153
17k
Transcript
⾃⼰関⼿の圏における モノイド対象 in Scala @phenan
おことわり ´ 圏論的に厳密な話はしません ´ 雰囲気だけ分かった気持ちになるところが⽬標
モナドは単なる⾃⼰関⼿の圏における モノイド対象だよ。何か問題でも︖ Philip Lee Wadler
有名な煽り⽂句 ´ モナドを勉強しようとした⼈の意思を粉砕する ´ 雰囲気だけでも理解しておきたい︕
分解: ⾃⼰関⼿の圏におけるモノイド対象 ´ 圏 ´ 関⼿ ´ ⾃⼰関⼿ ´ ⾃⼰関⼿の圏
´ モノイド対象
圏 (category) ´ 数学的対象とそれらの間の関係(射)の集まり ´ 射は合成可能 ´ 抽象的すぎていまいちピンとこない
型を対象とし、関数の型の関係を考える ´ 右図だと Byte, Int, String が対象 ´ Byte =>
Int, Int => String, Byte => String が射 ´ 射の合成可能性 = 関数の合成可能性 Int String Byte
全部 Option 型にしてみる ´ これも圏 ´ 対象: Option[Byte], Option[Int], Option[String]
´ 射: Option[Byte] => Option[Int], (略) Option[Int] Option[String] Option[Byte]
⾼階型 F[_] の表現する圏 ´ 対象: 任意の型 T に対する F[T] ´
射: 任意の型 T1, T2 に対して F[T1] => F[T2] ´ だいたいこいつを考えておけばOK
Hask圏 ´ 対象: 任意の型 ´ 射: 任意の型 T1, T2 に対して
T1 => T2 ´ ⾼階型 F[_] の表現する圏の F が Id のバージョンとも考えられる
関⼿ ´ 圏から圏への対応付け ´ 射の合成を保存する
具体例 ´ Option をつけたものに対応させる Int String Byte Option[Int] Option[String] Option[Byte]
我々はこれを知っている ´ Int => String が Option[Int] => Option[String] に対応する
´ Option.map: (A => B) => Option[A] => Option[B] Int String Byte Option[Int] Option[String] Option[Byte]
Cats, Scalaz の Functor ´ 射 A => B を
射 F[A] => F[B] に対応させる ´ Hask圏から⾼階型F[_]の表現する圏への関⼿ trait Functor[F[_]] { def map[A, B](f: A => B): F[A] => F[B] }
⾃⼰関⼿ ´ 関⼿のうち、圏をその圏⾃⾝の⼀部分に対応させるもの ´ フラクタル的なイメージ︖
Cats, Scalaz の Functor は⾃⼰関⼿ ´ 射 F[A] => F[B]
は⾃明にHask圏の射でもある trait Functor[F[_]] { def map[A, B](f: A => B): F[A] => F[B] }
関⼿の圏 ´ 関⼿は圏を別の圏に移す ´ 関⼿同⼠も合成可能 ´ 関⼿を射とみなして圏を作れる B C A
関⼿ F: A => B 関⼿G∘F: A => C 関⼿G: B => C
具体例 ´ Option と Future の合成 ´ Option.map: (A =>
B) => Option[A] => Option[B] ´ Future.map: (A => B) => Future[A] => Future[B] Id Option.map Future.map Future[Option[_]] Option[_]
⾃⼰関⼿の圏 ´ ⾃⼰関⼿同⼠は合成しても⾃⼰関⼿ ´ ⾃⼰関⼿同⼠を合成する演算 ⊗ を考えると、結果も⾃⼰関⼿になる ´ モノイドっぽい︕==> モノイド圏と呼ばれる
´ モノイドの要件として単位元が必要
具体例 ´ G[_] ⊗ F[_] -> G[F[_]] になる ´ 単位元:
Id Id Option.map Future.map Future[Option[_]] def compose[A, B](f: A => B): G[F[A]] => G[F[B]] = { Functor[G].map(Functor[F].map(f)) } Option[_]
モノイド対象 ´ モノイド圏の対象Mのうち以下の条件を満たすもの ´ M ⊗ M が M に戻る
´ 単位元 を I として I => M の射が存在する ´ イメージとしては、それ⾃⾝がモノイドであるような対象 ´ ⊗ がそれ⾃⾝の⼆項演算になる ´ それ⾃⾝の単位元をモノイド圏全体の単位元から導ける
OptionはFunctorの圏のモノイド対象 ´ Option[_] ⊗ Option[_] = Option[Option[_]] ´ flatten すれば
Option[_] に戻せる︕ ´ 単位元 Id から Option への射: T => Option[T] ´ これは Some(_) のこと
Functorの圏のモノイド対象 ´ flatten: F[F[A]] => F[A] と unit: A =>
F[A] があれば良い ´ つまりこうなる trait Monad[F[_]] extends Functor[F[_]] { def flatten[A](f: F[F[A]]): F[A] def unit[A](f: A): F[A] }