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
220
Refinement types
Refinement types for Swift
hectr
March 21, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
Claude Code Skill入門
mayahoney
0
440
Claude Codeログ基盤の構築
giginet
PRO
7
3.7k
Codex CLI でつくる、Issue から merge までの開発フロー
amata1219
0
230
モダンOBSプラグイン開発
umireon
0
190
仕様漏れ実装漏れをなくすトレーサビリティAI基盤のご紹介
orgachem
PRO
7
3.3k
Java 21/25 Virtual Threads 소개
debop
0
300
車輪の再発明をしよう!PHP で実装して学ぶ、Web サーバーの仕組みと HTTP の正体
h1r0
2
440
どんと来い、データベース信頼性エンジニアリング / Introduction to DBRE
nnaka2992
1
340
AWS×クラウドネイティブソフトウェア設計 / AWS x Cloud-Native Software Design
nrslib
16
3.4k
「接続」—パフォーマンスチューニングの最後の一手 〜点と点を結ぶ、その一瞬のために〜
kentaroutakeda
4
2.1k
メッセージングを利用して時間的結合を分離しよう #phperkaigi
kajitack
3
490
ネイティブアプリとWebフロントエンドのAPI通信ラッパーにおける共通化の勘所
suguruooki
0
210
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.5k
[SF Ruby Conf 2025] Rails X
palkan
2
870
RailsConf 2023
tenderlove
30
1.4k
Skip the Path - Find Your Career Trail
mkilby
1
91
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
490
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
290
Git: the NoSQL Database
bkeepers
PRO
432
67k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
330
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
Building an army of robots
kneath
306
46k
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