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
Transcript
23 slides
⾃⼰関⼿の圏における モノイド対象 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] }
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.7k
Scalalr
phenan
1
2.6k
Other Decks in Programming
See All in Programming
JavaScriptはシングルバイナリの夢を見るか / Does JavaScript Dream of a Single Binary?
ssssota
1
1.3k
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
150
Kotlin JVM用に作られたライブラリのKMPを前提とした再設計
jsoizo
0
240
自宅サーバはどこまで再現できるのか - Proxmox VE上のVM環境をIaCとバックアップで戻してみた
youto
0
450
Getting Started with Vibe Coding
miccheng
0
260
エージェントの管理もエージェントに – herdr-core と自作バックログで作る司令塔 –
nearme_tech
PRO
2
180
疎結合とは何か?ソフトウェア設計における結合バランスについて考える #phpcon_niigata
kajitack
2
450
Beyond Chatbots: Agentic UI with Open Standards @W-JAX 2026, München
manfredsteyer
PRO
0
130
Debian JP Projectのサーバー構成
uwabami
1
190
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
470
手の動かし方がわからなかった私が個人開発者になるまで
usagi0920_pochi
0
360
AppFunctionsの内部実装を紐解く
hiroaki404
1
130
Featured
See All Featured
ラッコキーワード サービス紹介資料
rakko
5
6.1M
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
860
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
590
A better future with KSS
kneath
240
18k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
310
Ethics towards AI in product and experience design
skipperchong
2
480
Optimising Largest Contentful Paint
csswizardry
37
4k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
590
The World Runs on Bad Software
bkeepers
PRO
72
12k