Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
About purely functional language
Search
Ryosuke TAKASHIMA
August 28, 2020
0
120
About purely functional language
Ryosuke TAKASHIMA
August 28, 2020
Tweet
Share
More Decks by Ryosuke TAKASHIMA
See All by Ryosuke TAKASHIMA
GCP native cloud migration
rst76
1
500
Cloud migration of the essential service
rst76
1
560
performance-tuning-of-the-microservice.pdf
rst76
0
1.5k
red-black-tree.pdf
rst76
0
87
Understanding Subtype in 5 Minutes
rst76
0
160
Fun of Types
rst76
0
52
Embedded Probabilistic Programming
rst76
0
330
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
For a Future-Friendly Web
brad_frost
179
9.8k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Designing for humans not robots
tammielis
253
25k
Speed Design
sergeychernyshev
32
1k
Writing Fast Ruby
sferik
628
62k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Music & Morning Musume
bryan
46
6.6k
A better future with KSS
kneath
239
17k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
Transcript
純粋関数型言語の話
自己紹介 • 高島 亮祐(@rst76) • エムスリー 基盤チーム(2019/01-) • 推してるマンガ: チェンソーマン、宝石の国、ゴールデンゴールド •
推してる◦◦映画:デビルマン実写版(116分) • 今日は純粋関数型言語の布教活動をします
Simple か Easy か • Simple ⇔ Complex • Easy
⇔ Difficult • Simple で Easy なら理想的だが・・・ • Easy を指向しているもの: Java の Annotation、Python のライブラリ群 • 個人的には Simple を優先したい → Haskell、Lazy K、F#
ラムダ計算 • プログラム = データ + プロセス(メソッド/関数) • データだけでプログラムを実現することはできないが、 プロセスだけで実現することはできる
• データをプロセス(関数)で表現 = ラムダ計算 • プログラムが扱う対象が1つだけになる(Simple!) データ プロセス データ
ラムダ計算でのデータ型(1) • Numeric ◦ 0 = \f x -> x
◦ 1 = \f x -> f x ◦ 2 = \f x -> f (f x) ◦ f = (+ 1), x = 0 と考えると分かりやすい • Boolean ◦ true = \x y -> x ◦ false = \x y -> y
ラムダ計算でのデータ型(2) • List ◦ cons = \x y f ->
f x y ◦ car = \z -> z true car (cons x y) = cons x y true = true x y = x ◦ cdr = \z -> z false cdr (cons x y) = cons x y false = false x y = y • 文字列を数字のリストと捉えれば、文字列も扱える
コンビネータ • よく使う関数に名前をつける • 恒等関数 I = \x -> x •
関数合成 B = \f g x -> f (g x) • 引数交換 C = \f x y -> f y x • 破棄 K = \x y -> x • 複製 W = \f x -> f x x
コンビネータを組み合わせる • true = \x y -> x = K
• false = \x y -> y = CK • 0 = \f x -> x = CK • 1 = \f x -> f x = I • 2 = \f x -> f (f x) = WB その他の関数もコンビネータを組み合わせて実現できる
コンビネータを組み合わせる • C K x y = (\f a b
-> f b a) K x y = K y x = (\a b -> a) y x = y • 0 x y = (\f a -> a) x y = y
SK コンビネータ • S = \x y z -> x
z (y z) • K = \x y -> x • S と K ですべてのコンビネータを表現できる(Simple!) (実はコンビネータは1つで十分 → Iota)
I = S K K • S K K x
= (K x) (K x) = K x (K x) = x = I x S = \x y z -> x z (y z) K = \x y -> x
B = S (K S) K • S (K S)
K f g x = (K S) f (K f) g x = K S f (K f) g x = S (K f) g x = (K f) x (g x) = K f x (g x) = f (g x) = B f g x S = \x y z -> x z (y z) K = \x y -> x
SK で表す数(デモ) • 0 = SK • 1 = SKK
• 2 = SS(SK)(S(KS)K) • 3 = SS(SS(SK))(S(KS)K) • 8 = 3 2 = SS(SS(SK))(S(KS)K)(SS(SK)(S(KS)K)) https://codepen.io/rst76/pen/BaKLjVN
まとめ 関数型言語は楽しいので、みんなやりましょう