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
Refinement types
Search
hectr
March 21, 2019
Programming
0
210
Refinement types
Refinement types for Swift
hectr
March 21, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
「コードは上から下へ読むのが一番」と思った時に、思い出してほしい話
panda728
PRO
39
26k
Claude Codeの「Compacting Conversation」を体感50%減! CLAUDE.md + 8 Skills で挑むコンテキスト管理術
kmurahama
1
650
Cell-Based Architecture
larchanjo
0
150
Basic Architectures
denyspoltorak
0
130
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
150
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
410
Python札幌 LT資料
t3tra
7
1.1k
AIエージェントの設計で注意するべきポイント6選
har1101
5
2.5k
モデル駆動設計をやってみようワークショップ開催報告(Modeling Forum2025) / model driven design workshop report
haru860
0
290
[AtCoder Conference 2025] LLMを使った業務AHCの上⼿な解き⽅
terryu16
6
840
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
6
4k
gunshi
kazupon
1
120
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Google's AI Overviews - The New Search
badams
0
870
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
38
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
210
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
61
44k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
48
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
94
Designing for Performance
lara
610
69k
RailsConf 2023
tenderlove
30
1.3k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.3k
Transcript
Refinement types github.com/hectr
a refinement type is a type endowed with a predicate
which is assumed to hold for any element of the refined type. — Wikipedia github.com/hectr
refinement types a.k.a encoding valida,ons at the typelevel github.com/hectr
Why? github.com/hectr
Stringly typed example struct Foo: Codable { let id: String
} • id cannot be nil ! • id could be empty " github.com/hectr
Refined string example struct Foo: Codable { let id: String.NonEmpty
} • id cannot be nil ! • id cannot be empty " github.com/hectr
Refined string example struct Foo: Codable { let id: String.NonEmpty
} • id cannot be nil ! • id cannot be empty " ... we'll explain this later. github.com/hectr
Almost any type is suscep0ble to be refined: • Int
(e.g. divisible by...) • Float (greater than...) • Double, etc.: (e.g. nega7ve) • String: (e.g. starts with...) • Array (e.g. contains...) • Set (e.g. count is...) github.com/hectr
The idea is not new: make illegal states unrepresentable github.com/hectr
Refinement types implementa.ons: (frameworks or dialects) • Scala • Haskell
• TypeScript • Perl • ... github.com/hectr
What about Swi,? github.com/hectr
There isn't language support for refinement types. ! github.com/hectr
There isn't language support for refinement types (yet). ! github.com/hectr
But... github.com/hectr
refined type = base type + predicate github.com/hectr
refined type ≈ wrapper type + generic constraint github.com/hectr
wrapper type + generic constraint struct Refined<Constraint: Predicate> { let
wrapped: Constraint.Value init?(_ value: Constraint.Value) { guard Constraint.isValid(value: value) else { return nil } self.wrapped = value } } github.com/hectr
wrapper type + generic constraint protocol Predicate { associatedtype Value
static func isValid(value: Value) -> Bool } github.com/hectr
Refined string example (cont) struct Not<P: Predicate>: Predicate { static
func isValid(value: P.Value) -> Bool { return !P.isValid(value: value) } } struct IsEmpty: Predicate { static func isValid(value: String) -> Bool { return value.isEmpty } } github.com/hectr
Refined string example (cont) let foo: Refined<Not<IsEmpty>> github.com/hectr
Refined string example (cont) let foo: Refined<Not<IsEmpty>> ! github.com/hectr
Refined string example (cont) // let foo: Refined<Not<IsEmpty>> extension String
{ typealias Empty = Refined<IsEmpty> typealias NonEmpty = Refined<Not<IsEmpty>> } github.com/hectr
Refined string example (cont) // let foo: Refined<Not<IsEmpty>> extension String
{ typealias Empty = Refined<IsEmpty> typealias NonEmpty = Refined<Not<IsEmpty>> } let foo: String.NonEmpty github.com/hectr
Is it that easy? github.com/hectr
No, it is not... github.com/hectr
Refined types need to seamlessly integrate with their base types.
github.com/hectr
type transparency github.com/hectr
type transparency struct Photo: Codable { var title: Refined<Count<String, LessThan<Int_20>>>
var height: Refined<RangeOf<Double_100, Double_1000>> var width: Refined<RangeOf<Double_100, Double_1000>> } let jsonString = "{\"width\":150,\"height\":150,\"title\":\"Apple\"}" let jsonData = jsonString.data(using: .utf8)! let photo = try! JSONDecoder().decode(Photo.self, from: jsonData) _ = photo.width * 2.0 _ = "photo called " + photo.title XCTAssertEqual(photo.title, "Apple") XCTAssertEqual(photo.width, 150) github.com/hectr
But that is another story... github.com/hectr
github.com/hectr
github.com/hectr
github.com/hectr
github.com/refined-swi4 github.com/hectr