SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
LiquidHaskell で
普通の型システムの上を行け
チェシャ猫 (@y_taka_23)
NGK2017B 昼の部 (2017/12/2)
#NGK2017B
イケてる言語 Haskell
● 簡潔かつ強力な言語機能
○ 高階関数をフル活用した関数型スタイル
● 純粋性によるメリット
○ コンパイラによる最適化
○ 並行・並列実行に有利
● 静的型付けによる堅牢性
○ コンパイル時に型エラーを検出
#NGK2017B
静的型付けによる堅牢性
#NGK2017B
…本当に?
#NGK2017B
型では検出できないバグ
output :: Text
output = takeWord16 10 (pack “input”)
main :: IO ()
main = print output
#NGK2017B
型では検出できないバグ
output :: Text
output = takeWord16 10 (pack “input”)
main :: IO ()
main = print output
-- “input2301SOHNUL436005201”
#NGK2017B
型では検出できないバグ
output :: Text
output = takeWord16 10 (pack “input”)
main :: IO ()
main = print output
-- “input2301SOHNUL436005201”
#NGK2017B
Heartbleed
#NGK2017B
(C 言語と同レベルの)堅牢性…
#NGK2017B
Haskell の限界
● 値の「種類」は型で保証できる
○ 確かに動的言語に比べれば堅い
● 値の「内容」は型では保証できない
○ 長さ n 以上の文字列
○ 整列済みのリスト
○ 左右がバランスしている探索二分木
● ロジックのバグに対して型は意外と無力
#NGK2017B
LiquidHaskell
#NGK2017B
コンパイル可能だが実行時エラー
head :: [a] -> a
head [] = error “empty list”
head (x:_) = x
main :: IO ()
main = print $ head ([] :: Int)
#NGK2017B
そもそもコンパイルエラー
{-@ head :: { xs:[a] | len xs > 0 } -> a @-}
head :: [a] -> a
head [] = error “empty list”
head (x:_) = x
main :: IO ()
main = print $ head ([] :: Int)
#NGK2017B
そもそもコンパイルエラー
{-@ head :: { xs:[a] | len xs > 0 } -> a @-}
head :: [a] -> a
head [] = error “empty list”
head (x:_) = x
main :: IO ()
main = print $ head ([] :: Int)
#NGK2017B
ふるいがた
篩型
(Refinement Types)
#NGK2017B
foo :: a -> b
a 型から b 型への関数
#NGK2017B
foo :: { x:a | P x } -> { y:b | Q y }
a 型のうち条件 P を満たす値から
b 型のうち条件 Q を満たす値への関数
#NGK2017B
篩型 = 型 + 述語
#NGK2017B
篩型と契約プログラミング
● 事前条件 = 引数の篩型
○ 呼び出される箇所すべてで制約を満たすか
● 事後条件 = 戻り値の篩型
○ 事前条件 + 関数内のロジックと整合するか
● 不変条件 = 値コンストラクタの篩型
○ immutable なのでコンストラクタを確認すれば OK
#NGK2017B
篩型で Heartbleed を検出
{-@ measure tlen :: Text -> Int @-}
{-@
assume pack :: s:String
-> { t:Text | tlen t == len s }
@-}
{-@
assume takeWord16 :: n:Int
-> { t:Text | tlen t >= n }
-> Text
@-}
#NGK2017B
篩型で Heartbleed を検出
{-@ measure tlen :: Text -> Int @-}
{-@
assume pack :: s:String
-> { t:Text | tlen t == len s }
@-}
{-@
assume takeWord16 :: n:Int
-> { t:Text | tlen t >= n }
-> Text
@-}
#NGK2017B
篩型で Heartbleed を検出
-- コンパイル通る
safeOutput = takeWord16 3 (pack “input”)
-- コンパイル通らない(型エラー)
unsafeOutput = takeWord16 10 (pack “input”)
#NGK2017B
篩型で Heartbleed を検出
-- コンパイル通る
safeOutput = takeWord16 3 (pack “input”)
-- コンパイル通らない(型エラー)
unsafeOutput = takeWord16 10 (pack “input”)
#NGK2017B
まとめ
● 通常の Haskell の型システムは不十分
○ 具体的な値に関する条件が書けない
● LiquidHaskell で値の条件を記述
○ 篩型 = 通常の型 + 述語による制限
○ 特殊コメントを付ける(通常コンパイルも可能)
● より堅牢なプログラミングが可能に
○ ロジックのバグまでコンパイル時に検出
#NGK2017B
Refine Your Haskell Life!
Presented by チェシャ猫 (@y_taka_23)
#NGK2017B

Contenu connexe

Dernier

Dernier (7)

Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
NewSQLの可用性構成パターン(OCHaCafe Season 8 #4 発表資料)
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 

En vedette

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
ThinkNow
 
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
Kurio // The Social Media Age(ncy)
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
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...
 

Liquid Haskell で普通の型システムの上を行け #NGK2017B