SlideShare a Scribd company logo
1 of 41
Download to read offline
形式検証の視点から再確認したい
Maybe/Option型からnull許容データベースへの
データ変換
井上健太


2021/10/1
Qiitaの記事


「形式検証の視点から再確認したいMaybe/Option型からnull許容データベースへのデータ変換」
https://qiita.com/nekonibox/items/fd57bc9ad861642efbd6
自己紹介
井上健太
理学博士 (数学/数理論理学)
研究テーマ:定理証明支援系Coqによる定理の形式化
ソフトウェアの形式検証
ソフトウェアが正しく動作することを


数学的/論理的に証明する
背景
ソフトウェア
データベース
(特にRDB)
Maybe型/Option型 Null許容データ
RDB : Relational Database
この対応は本当に正しい?
形式検証の観点で考察する
toDB : ∀T, Convertible T → T → DB
fromDB : ∀T, Convertible T → DB → Result T
データ変換のモデル
T : 変換元の型
DB : データベース上の型
Result T : 返り値の型
DB
T Result T
toDBT fromDBT
返り値の型が T でないのは


全てのDB型をT型に


変換できないため
toDB : ∀T, Convertible T → T → DB
fromDB : ∀T, Convertible T → DB → Result T
データ変換のモデル
T : 変換元の型
DB : データベース上の型
E + T : 返り値の型
DB
T
toDBT fromDBT
返り値の型が T でないのは


全てのDB型をT型に


変換できないため
E + T
Result T の実装の多くは


適当な型Eに対するE + T 型
toDB : ∀T, Convertible T → T → DB
fromDB : ∀T, Convertible T → DB → Result T
f : ∀T, T →
データ変換のモデル
T : 変換元の型
DB : データベース上の型
Result T : 返り値の型
任意の で は単射
T fT
Result T
DB
T Result T
fT
toDBT fromDBT
toDB : ∀T, Convertible T → T → DB
fromDB : ∀T, Convertible T → DB → Result T
f : ∀T, T →
データ変換のモデル
T : 変換元の型
DB : データベース上の型
E + T : 返り値の型
は自然な単射
f
E + T
DB
T E + T
fT
toDBT fromDBT
E : 適当な集合
toDB : ∀T, Convertible T → T → DB
fromDB : ∀T, Convertible T → DB → Result T
データ変換のモデル
T : 変換元の型
DB : データベース上の型
Result T : 返り値の型
DB
T Result T
fT
toDBT fromDBT f : ∀T, T →
任意の で は単射
T fT
Result T
仕様:∀T, Convertible T → fromDBT ∘ toDBT = fT
DB
T
toDBT
toDB : ∀T, Convertible T → T → DB
データ変換の実装
Convertible T
T : 変換元の型
DB : データベース上の型
instance
DB
T
toDBT
データ変換の実装
Convertible Bool
Convertible Int
Convertible t => Convertible (Maybe t)
⋮
instance
DB
Maybe T
toDBMaybe T
データ変換の実装
Convertible t => Convertible (Maybe t)
instance
DB
Maybe T
toDBMaybe T
データ変換の実装
⟼
Nothing
Just x ⟼
Null
toDB x
この実装は仕様を満たすのか?
Convertible t => Convertible (Maybe t)
instance
toDB : ∀T, Convertible T → T → DB
fromDB : ∀T, Convertible T → DB → Result T
データ変換のモデル
T : 変換元の型
DB : データベース上の型
Result T : 返り値の型
DB
T Result T
fT
toDBT fromDBT
仕様:∀T, Convertible T → fromDBT ∘ toDBT = fT
f : ∀T, T →
任意の で は単射
T fT
Result T
toDB : ∀T, Convertible T → T → DB
fromDB : ∀T, Convertible T → DB → Result T
データ変換のモデル
T : 変換元の型
DB : データベース上の型
Result T : 返り値の型
DB
T Result T
fT
toDBT fromDBT f : ∀T, T →
任意の で は単射
T fT
Result T
単射
仕様:∀T, Convertible T → fromDBT ∘ toDBT = fT
toDB : ∀T, Convertible T → T → DB
fromDB : ∀T, Convertible T → DB → Result T
データ変換のモデル
T : 変換元の型
DB : データベース上の型
Result T : 返り値の型
DB
T Result T
fT
toDBT fromDBT f : ∀T, T →
任意の で は単射
T fT
Result T
単射
仕様:∀T, Convertible T → fromDBT ∘ toDBT = fT
データ変換の実装
toDB Nothing = Null
Convertible t => Convertible (Maybe t)
toDB x = Nullになる


可能性がある
toDB (Just Nothing) = Null
Convertible (Maybe (Maybe Int))
DB
Maybe T
toDBMaybe T
⟼
Nothing
Just x ⟼
Null
toDB x
単射
instance
DB
Maybe T
toDBMaybe T
データ変換の実装
⟼
Nothing
Just x ⟼
Null
toDB x
単射 toDB x = Nullになる


可能性がある
Convertible t => Convertible (Maybe t)
instance
toDBが単射にならず、仕様を満たさない
実はHaskellでも…
print $ map toJSON s


where


s :: [Maybe (Maybe Int)]


s = [Nothing, Just Nothing, Just (Just 3)]
HaskellのJSONへの変換も先ほどと同じ実装なので…
[Null, Null, Number 3.0]
実行結果
(Data.Aeson 1.6.0.0)
原因
Maybe型とNullableなデータ型は一般に1対1対応しない
Nothing
Just (値)
Null
(値)
Maybe Nullable
原因
Maybe型とNullableなデータ型は一般に1対1対応しない
Nothing
Just (値)
Null
(値)
Maybe Nullable
Just Nothing (存在しない)
Nullを値として扱えない
Nothingは値になりうる
(値としてのNullとNull自身を区別できない)
Maybe T
解決策1
1 + T
プログラム 数学
ただし 1 = {∅}
Either E T E + T
Either型に帰着
DB
Either E T
toDBEither E T
解決策1
⟼
Left e
Right t ⟼
(“Left”, toDB e)
(“Right”, toDB t)
(Convertible e, Convertible t) =>


Convertible (Either e t)
instance
Either型に帰着
DB
Maybe T
toDBMaybe T
解決策1
⟼
Nothing
Just t ⟼
(“Nothing”, Null)
(“Just”, toDB t)
instance
Convertible t => Convertible (Maybe t)
Either型に帰着
DB
Maybe T
toDBMaybe T
解決策1
⟼
Nothing
Just t ⟼
(“Nothing”, Null)
(“Just”, toDB t)
Either型に帰着
しかし
Null
DB
⟼
(??)
⟼ x
DB
Maybe T
toDBMaybe T
解決策1
Nothing Null
⟼
toDB Nothing = Nullを保ったまま
toDBが単射になるものを定義する
Just t ⟼ toDB t
Nothing
Just (値)
Null
(値)
Maybe Nullable
考え方
Nothing
Just (値)
Null
(値)
Maybe Nullable
考え方
DBの(値)にNullは記述できない
Nothing
Just (値)
Null
(値)
Maybe Nullable
考え方
Justの引数をNullに変換できないようにすればよい
DBの(値)にNullは記述できない
解決策2
instanceを変更
instance
Convertible T : Nullableデータに変換可能
Convertible t => Convertible (Maybe t)
変更前
NNConvertible t => Convertible (Maybe t)
変更後
NNConvertible T : Nullを許さないデータに変換可能
解決策2
instanceを変更
instance
Convertible T : Nullableデータに変換可能
NNConvertible T : Nullを許さないデータに変換可能
解決策2
instanceを変更
Convertible T NNConvertible T
toDB : ∀T, Convertible T → T → DB
data DB = DBBool Bool


| DBInt Int




| DBArray [DB]


| DBNull
⋮
data NNDB = NNDBBool Bool


| NNDBInt Int




| NNDBArray [DB]
⋮
toNNDB : ∀T, NNConvertible T → T → NNDB
解決策2
instanceを変更
Convertible T NNConvertible T
toDB : ∀T, Convertible T → T → DB
data DB = DBBool Bool


| DBInt Int




| DBArray [DB]


| DBNull
⋮
data NNDB = NNDBBool Bool


| NNDBInt Int




| NNDBArray [DB]
⋮
toNNDB : ∀T, NNConvertible T → T → NNDB
解決策2
instanceを変更
Convertible T NNConvertible T
toDB : ∀T, Convertible T → T → DB
data DB = Maybe NNDB data NNDB = NNDBBool Bool


| NNDBInt Int




| NNDBArray [DB]
⋮
toNNDB : ∀T, NNConvertible T → T → NNDB
null = Nothing :: Maybe NNDB
解決策2
instanceを変更
NNConvertible T
data NNDB = NNDBBool Bool


| NNDBInt Int




| NNDBArray [DB]
⋮
NNConvertible Bool
NNConvertible Int
∀T, Convertible T → NNConvertible [T]
解決策2
instanceを変更
NNConvertible T
data NNDB = NNDBBool Bool


| NNDBInt Int




| NNDBArray [DB]
⋮
Convertible T
Just : NNDB → DB
∀T, NNConvertible T → Convertible T
toDBT := Just ∘ toNNDBT
data DB = Maybe NNDB
解決策2
instanceを変更
NNConvertible T
data NNDB = NNDBBool Bool


| NNDBInt Int




| NNDBArray [DB]
⋮
Convertible T
Just : NNDB → DB
data DB = Maybe NNDB
∀T, NNConvertible T → Convertible (Maybe T)
解決策2
instanceを変更
∀T, NNConvertible T → Convertible (Maybe T)
DB
Maybe T
toDBMaybe T
Nothing Nothing
⟼
Just t ⟼ Just (toNNDB t)
Nullになり得ない
toDBMaybe T := fmapMaybe toNNDBT
fmap : ∀F, Functor F → ∀A B, (A → B) → F A → F B
解決策2
instanceを変更
Convertible T : Nullableデータに変換可能
NNConvertible T : Nullを許さないデータに変換可能
toDBMaybe T := fmapMaybe toNNDBT DB
Maybe T
toDBMaybe T
Nothing Nothing
⟼
Just t ⟼ Just (toNNDB t)
instance
NNConvertible t => Convertible (Maybe t)
解決策2
instanceを変更
instance
NNConvertible t => Convertible (Maybe t)
はそのままでは定義されないが…
toDBMaybe (Maybe T)
NNConvertible t => NNConvertible (Maybe t)


を解決策1と同じように定義してやれば定義できます
詳細はQiitaの記事にて
「形式検証の視点から再確認したいMaybe/Option型からnull許容データベースへのデータ変換」
fromDB
DB
T Result T
fT
toDBT fromDBT
単射
の単射性により
toDBT
を満たす


の存在性が言える
fromDBT ∘ toDBT = fT
fromDBT
かつ選択公理を仮定すると
(Result T = ∅ → DB = ∅)
に関して
fromDB
先ほど定義した に対する


も構成的に定義できる
toDB
fromDB
まとめ
Maybe型/Option型とnull許容データは


任意の型に対しては1対1対応しない
¬(∀T, maybe T ∼ nullable T)
Maybe型/Option型からデータ変換するときには


型引数からの変換がNullになり得ないことを保証する必要がある

More Related Content

Featured

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 HubspotMarius Sescu
 
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
 

Featured (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...
 

形式検証の視点から再確認したいMaybe/Option型からnull許容データベースへのデータ変換