SlideShare a Scribd company logo
42.type: 
Literal-based singleton types in Scala 
@folone @xeno_by @retronym @adriaanm @extempore2
About me: 
@folone 
likes types* 
*same reasons why @propensive does in 
his “Batshit crazy algebra with types” talk
42.type: Literal-based Singleton types
Examples
1. Records
val book = ("author" ->> "Benjamin Pierce") :: 
("title" ->> "TAPL") :: 
("id" ->> 262162091) :: 
("price" ->> 44.11) :: 
HNil 
! 
scala> book("author") // Note the result type 
res0: String = Benjamin Pierce 
! 
scala> book("id") // Note the result type 
res1: Int = 262162091
trait Assoc[K] { type V ; val v: V } 
defined trait Assoc 
! 
def mkAssoc[K, V0](k: K,v0: V0): 
Assoc[k.type] { type V = V0 } = 
new Assoc[k.type] {type V = V0 ; val v = v0} 
mkAssoc: [K, V0](k: K, v: V0)Assoc[k.type]{type V = V0} 
! 
def lookup[K](k: K) 
(implicit a: Assoc[k.type]): a.V = a.v 
lookup: [K](k: K)(implicit assoc: Assoc[k.type])assoc.V
> implicit def firstAssoc = mkAssoc(1, "Panda!") 
firstAssoc: Assoc[1.type]{type V = String} 
! 
> implicit def ageAssoc = mkAssoc("Age", 3) 
ageAssoc: Assoc["Age".type]{type V = Int} 
! 
> implicit def nmAssoc = mkAssoc(“Name", “Jane”) 
nmAssoc: Assoc["Name".type]{type V = String} 
scala> lookup(1) 
res0: String = Panda! 
scala> lookup("Age") 
res1: Int = 3 
scala> lookup("Name") 
res2: String = Jane
2. Residue
case class Residue[N <: Int: SingleInhabitant](n: Long){ 
lhs => 
def +(rhs: Residue[N]): Residue[N] = 
Residue((lhs.n + rhs.n) % inhabitant[N]) 
}
scala> Residue[15](15) + Residue[13](20) 
<console>:10: error: type mismatch; 
found : Residue[13.type] 
required: Residue[15.type] 
Residue[15](15) + Residue[13](20) 
^ 
scala> Residue[13](15) + Residue[13](20) 
res1: Residue[13.type] = Residue(9)
3. Ranged
class Ranged[From <: Int : SingleInhabitant, 
To <: Int : SingleInhabitant] { 
def sample = { 
val rnd = new scala.util.Random 
val from = inhabitant[From] 
val to = inhabitant[To] 
(from + rnd.nextInt(to - from + 1)) 
} 
}
scala> val range = new Ranged[10, 20] 
range: Ranged[10.type,20.type] = 
Ranged@78c22d25 
! 
scala> range.sample 
res0: Int = 13 
! 
scala> range.sample 
res1: Int = 11
Consistency
Here’s what you 
can do in Scala 
scala> val x = "panda!" 
x: String = panda! 
! 
scala> val t: x.type = x 
t: x.type = panda! 
! 
scala> final val k = "panda!" 
k: String("panda!") = panda!
Here’s what you 
cannot 
scala> val t: "panda!".type = "panda!" 
<console>:1: error: identifier expected 
but string literal found. 
val t: "panda!".type = "panda!" 
^
With 42.type 
scala> val t: "panda!".type = "panda!" 
t: "panda!".type = panda!
Even more 
scala> val x = 42 
x: Int = 42 
! 
scala> val t: x.type = 42 
<console>:8: error: type mismatch; 
found : x.type (with underlying type Int) 
required: AnyRef 
val t: x.type = 42 
^ 
scala> val workaround = Witness(42) 
workaround: shapeless.Witness{type T = Int(42)} = fresh$macro$3$1@35b45d3f 
! 
scala> val t: workaround.T = 42 
t: workaround.T = 42
Solution with 42.type 
scala> val t: 42.type = 42 
t: 42.type = 42 
! 
! 
! 
scala> val t: 42 = 42 
t: 42.type = 42 
Or even
42: Literal-based singleton types in Scala
Before 
SimpleType ::= Path ‘.’ type 
A singleton type is of the form p.type, 
where p is a path pointing to a value 
expected to conform to scala.AnyRef.
After 
SimpleType ::= Path ‘.’ type 
| Literal [‘.’ type]
4.
State of deptypes Scala 
“Scala vs Idris: Dependent 
Types, Now and in the Future”* 
*Edwin Brady & Miles Sabin, Strange Loop 2013
How can we 
make it better? 
Well, we can try. 
Scala + Z3
scala> import z3.scala._, z3.scala.dsl._ 
import z3.scala._ 
import z3.scala.dsl._
scala> findAll((x: Val[Int]) => x > 23 && x < 42).toList 
res0: List[Int] = List(24, 25, 26, 27, 28, 29, 30, 31, 
32, 33, 34, 35, 36, 37, 38, 39, 40, 41)
((x: Val[Int]) => x > 23 && x < 42)
((x: Int) => x > 23 && x < 42)
((x: Int) => x > 23 && x < 42).type
val magic: ((x: Int) => x > 23 && x < 42).type = 30
val magic: ((x: Int) => x > 23 && x < 42).type = 30 
val x: 42 = 42 val x: ((x: Int) => x == 42).type = 42
val magic: ((x: Int) => x > 23 && x < 42).type = 30 
val x: 42 = 42 val x: ((x: Int) => x == 42).type = 42 
val x: Int = 42 val x: ((x: Int) => x).type = 42
42.type: Literal-based Singleton types
bit.ly/42_type
Credits: 
this thing would not be possible without: 
@xeno_by, @retronym, @adriaanm, 
and initial impl by @paulp 
Scala Z3 bindings: LARA@EPFL 
Z3: Microsoft research 
slides, illustrations: @killnicole
42.type: Literal-based Singleton types
Thanks. Questions? 
- Contributing to Scala compiler 
- Type-level programming 
- Working/hackertime at SoundCloud 
Ask me about:

More Related Content

PDF
Demystifying Shapeless
PDF
Scala Types of Types @ Lambda Days
PDF
High Wizardry in the Land of Scala
PDF
Swift in SwiftUI
PDF
Quick swift tour
PDF
Scala cheatsheet
PDF
Working with Cocoa and Objective-C
PDF
Introduction to Swift 2
Demystifying Shapeless
Scala Types of Types @ Lambda Days
High Wizardry in the Land of Scala
Swift in SwiftUI
Quick swift tour
Scala cheatsheet
Working with Cocoa and Objective-C
Introduction to Swift 2

What's hot (19)

PDF
From android/java to swift (1)
PDF
Swift Study #3
PPTX
Javascript analysis
PDF
Strings
PPT
Manipulation of Strings
PPTX
JavaScript Basics and Trends
PDF
Scala 101
PDF
Scala intro workshop
PPT
JavaScript Tutorial
PDF
JavaScript 101 - Class 1
PPTX
Types by Adform Research, Saulius Valatka
PPT
Scala for Java Developers
PDF
javascript objects
PDF
Basic Operator, String and Characters in Swift.
PPTX
Hardened JavaScript
PDF
Be Smart, Constrain Your Types to Free Your Brain!
PDF
Intro toswift1
PDF
ZIO Prelude - ZIO World 2021
From android/java to swift (1)
Swift Study #3
Javascript analysis
Strings
Manipulation of Strings
JavaScript Basics and Trends
Scala 101
Scala intro workshop
JavaScript Tutorial
JavaScript 101 - Class 1
Types by Adform Research, Saulius Valatka
Scala for Java Developers
javascript objects
Basic Operator, String and Characters in Swift.
Hardened JavaScript
Be Smart, Constrain Your Types to Free Your Brain!
Intro toswift1
ZIO Prelude - ZIO World 2021
Ad

Similar to 42.type: Literal-based Singleton types (20)

PDF
42: Rise of the dependent types
PDF
00 - Scala. Intro
PDF
15 - Scala. Dependent function type (Π-type)
PDF
Power of functions in a typed world
PDF
Sigma type
PDF
13 - Scala. Dependent pair type (Σ-type)
PDF
Scala for Java Devs
PDF
Introduction to programming with dependent types in Scala
PDF
A Scala Corrections Library
PDF
Зависимые типы в GHC 8. Максим Талдыкин
PDF
Scala jargon cheatsheet
PDF
Exploring type level programming in Scala
PPT
scala.ppt
PDF
From DOT to Dotty
PDF
Scala Paradigms
ODP
Introducing scala
PDF
New lambdas
PDF
Scala Bootcamp 1
ODP
Type Parameterization
PDF
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
42: Rise of the dependent types
00 - Scala. Intro
15 - Scala. Dependent function type (Π-type)
Power of functions in a typed world
Sigma type
13 - Scala. Dependent pair type (Σ-type)
Scala for Java Devs
Introduction to programming with dependent types in Scala
A Scala Corrections Library
Зависимые типы в GHC 8. Максим Талдыкин
Scala jargon cheatsheet
Exploring type level programming in Scala
scala.ppt
From DOT to Dotty
Scala Paradigms
Introducing scala
New lambdas
Scala Bootcamp 1
Type Parameterization
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Ad

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
project resource management chapter-09.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
A Presentation on Touch Screen Technology
PPTX
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
project resource management chapter-09.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
cloud_computing_Infrastucture_as_cloud_p
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Group 1 Presentation -Planning and Decision Making .pptx
Encapsulation theory and applications.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Encapsulation_ Review paper, used for researhc scholars
SOPHOS-XG Firewall Administrator PPT.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Tartificialntelligence_presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology
WOOl fibre morphology and structure.pdf for textiles
Hindi spoken digit analysis for native and non-native speakers
A Presentation on Touch Screen Technology
Programs and apps: productivity, graphics, security and other tools

42.type: Literal-based Singleton types

  • 1. 42.type: Literal-based singleton types in Scala @folone @xeno_by @retronym @adriaanm @extempore2
  • 2. About me: @folone likes types* *same reasons why @propensive does in his “Batshit crazy algebra with types” talk
  • 6. val book = ("author" ->> "Benjamin Pierce") :: ("title" ->> "TAPL") :: ("id" ->> 262162091) :: ("price" ->> 44.11) :: HNil ! scala> book("author") // Note the result type res0: String = Benjamin Pierce ! scala> book("id") // Note the result type res1: Int = 262162091
  • 7. trait Assoc[K] { type V ; val v: V } defined trait Assoc ! def mkAssoc[K, V0](k: K,v0: V0): Assoc[k.type] { type V = V0 } = new Assoc[k.type] {type V = V0 ; val v = v0} mkAssoc: [K, V0](k: K, v: V0)Assoc[k.type]{type V = V0} ! def lookup[K](k: K) (implicit a: Assoc[k.type]): a.V = a.v lookup: [K](k: K)(implicit assoc: Assoc[k.type])assoc.V
  • 8. > implicit def firstAssoc = mkAssoc(1, "Panda!") firstAssoc: Assoc[1.type]{type V = String} ! > implicit def ageAssoc = mkAssoc("Age", 3) ageAssoc: Assoc["Age".type]{type V = Int} ! > implicit def nmAssoc = mkAssoc(“Name", “Jane”) nmAssoc: Assoc["Name".type]{type V = String} scala> lookup(1) res0: String = Panda! scala> lookup("Age") res1: Int = 3 scala> lookup("Name") res2: String = Jane
  • 10. case class Residue[N <: Int: SingleInhabitant](n: Long){ lhs => def +(rhs: Residue[N]): Residue[N] = Residue((lhs.n + rhs.n) % inhabitant[N]) }
  • 11. scala> Residue[15](15) + Residue[13](20) <console>:10: error: type mismatch; found : Residue[13.type] required: Residue[15.type] Residue[15](15) + Residue[13](20) ^ scala> Residue[13](15) + Residue[13](20) res1: Residue[13.type] = Residue(9)
  • 13. class Ranged[From <: Int : SingleInhabitant, To <: Int : SingleInhabitant] { def sample = { val rnd = new scala.util.Random val from = inhabitant[From] val to = inhabitant[To] (from + rnd.nextInt(to - from + 1)) } }
  • 14. scala> val range = new Ranged[10, 20] range: Ranged[10.type,20.type] = Ranged@78c22d25 ! scala> range.sample res0: Int = 13 ! scala> range.sample res1: Int = 11
  • 16. Here’s what you can do in Scala scala> val x = "panda!" x: String = panda! ! scala> val t: x.type = x t: x.type = panda! ! scala> final val k = "panda!" k: String("panda!") = panda!
  • 17. Here’s what you cannot scala> val t: "panda!".type = "panda!" <console>:1: error: identifier expected but string literal found. val t: "panda!".type = "panda!" ^
  • 18. With 42.type scala> val t: "panda!".type = "panda!" t: "panda!".type = panda!
  • 19. Even more scala> val x = 42 x: Int = 42 ! scala> val t: x.type = 42 <console>:8: error: type mismatch; found : x.type (with underlying type Int) required: AnyRef val t: x.type = 42 ^ scala> val workaround = Witness(42) workaround: shapeless.Witness{type T = Int(42)} = fresh$macro$3$1@35b45d3f ! scala> val t: workaround.T = 42 t: workaround.T = 42
  • 20. Solution with 42.type scala> val t: 42.type = 42 t: 42.type = 42 ! ! ! scala> val t: 42 = 42 t: 42.type = 42 Or even
  • 21. 42: Literal-based singleton types in Scala
  • 22. Before SimpleType ::= Path ‘.’ type A singleton type is of the form p.type, where p is a path pointing to a value expected to conform to scala.AnyRef.
  • 23. After SimpleType ::= Path ‘.’ type | Literal [‘.’ type]
  • 24. 4.
  • 25. State of deptypes Scala “Scala vs Idris: Dependent Types, Now and in the Future”* *Edwin Brady & Miles Sabin, Strange Loop 2013
  • 26. How can we make it better? Well, we can try. Scala + Z3
  • 27. scala> import z3.scala._, z3.scala.dsl._ import z3.scala._ import z3.scala.dsl._
  • 28. scala> findAll((x: Val[Int]) => x > 23 && x < 42).toList res0: List[Int] = List(24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41)
  • 29. ((x: Val[Int]) => x > 23 && x < 42)
  • 30. ((x: Int) => x > 23 && x < 42)
  • 31. ((x: Int) => x > 23 && x < 42).type
  • 32. val magic: ((x: Int) => x > 23 && x < 42).type = 30
  • 33. val magic: ((x: Int) => x > 23 && x < 42).type = 30 val x: 42 = 42 val x: ((x: Int) => x == 42).type = 42
  • 34. val magic: ((x: Int) => x > 23 && x < 42).type = 30 val x: 42 = 42 val x: ((x: Int) => x == 42).type = 42 val x: Int = 42 val x: ((x: Int) => x).type = 42
  • 37. Credits: this thing would not be possible without: @xeno_by, @retronym, @adriaanm, and initial impl by @paulp Scala Z3 bindings: LARA@EPFL Z3: Microsoft research slides, illustrations: @killnicole
  • 39. Thanks. Questions? - Contributing to Scala compiler - Type-level programming - Working/hackertime at SoundCloud Ask me about: