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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
hectr
March 21, 2019
Programming
220
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Refinement types
Refinement types for Swift
hectr
March 21, 2019
Other Decks in Programming
See All in Programming
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
140
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
330
共通化で考えるべきは、実装より公開する型だった
codeegg
0
260
自作OSでスライド発表する
uyuki234
1
3.9k
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
310
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
210
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
500
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
150
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
17
9.2k
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
360
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
530
OSINT for SRE: 学術論文とポストモーテムから探る システム障害の共通パターン / SRE NEXT 2026
tomoyk
1
4k
Featured
See All Featured
Game over? The fight for quality and originality in the time of robots
wayneb77
1
220
Everyday Curiosity
cassininazir
0
260
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
460
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
330
AI: The stuff that nobody shows you
jnunemaker
PRO
9
840
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
470
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Prompt Engineering for Job Search
mfonobong
0
380
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