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
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
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
Thanks. Questions? 
- Contributing to Scala compiler 
- Type-level programming 
- Working/hackertime at SoundCloud 
Ask me about:

More Related Content

What's hot

From android/java to swift (1)
From android/java to swift (1)From android/java to swift (1)
From android/java to swift (1)
allanh0526
 
Swift Study #3
Swift Study #3Swift Study #3
Swift Study #3
chanju Jeon
 
Javascript analysis
Javascript analysisJavascript analysis
Javascript analysis
Uchitha Bandara
 
Strings
StringsStrings
Strings
Jancypriya M
 
Manipulation of Strings
Manipulation of StringsManipulation of Strings
Manipulation of Strings
Jancypriya M
 
JavaScript Basics and Trends
JavaScript Basics and TrendsJavaScript Basics and Trends
JavaScript Basics and Trends
Kürşad Gülseven
 
Scala 101
Scala 101Scala 101
Scala 101
Andrey Myatlyuk
 
Scala intro workshop
Scala intro workshopScala intro workshop
Scala intro workshop
Fredrik Vraalsen
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
Bui Kiet
 
JavaScript 101 - Class 1
JavaScript 101 - Class 1JavaScript 101 - Class 1
JavaScript 101 - Class 1
Robert Pearce
 
Types by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaTypes by Adform Research, Saulius Valatka
Types by Adform Research, Saulius Valatka
Vasil Remeniuk
 
Scala vs Ruby
Scala vs RubyScala vs Ruby
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java Developers
RamnivasLaddad
 
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
Basic Operator, String and Characters in Swift.
Basic Operator, String and Characters in Swift.Basic Operator, String and Characters in Swift.
Basic Operator, String and Characters in Swift.
HSIEH CHING-FAN
 
Hardened JavaScript
Hardened JavaScriptHardened JavaScript
Hardened JavaScript
KrisKowal2
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!
Jorge Vásquez
 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1
Jordan Morgan
 
ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021
Jorge Vásquez
 

What's hot (19)

From android/java to swift (1)
From android/java to swift (1)From android/java to swift (1)
From android/java to swift (1)
 
Swift Study #3
Swift Study #3Swift Study #3
Swift Study #3
 
Javascript analysis
Javascript analysisJavascript analysis
Javascript analysis
 
Strings
StringsStrings
Strings
 
Manipulation of Strings
Manipulation of StringsManipulation of Strings
Manipulation of Strings
 
JavaScript Basics and Trends
JavaScript Basics and TrendsJavaScript Basics and Trends
JavaScript Basics and Trends
 
Scala 101
Scala 101Scala 101
Scala 101
 
Scala intro workshop
Scala intro workshopScala intro workshop
Scala intro workshop
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
JavaScript 101 - Class 1
JavaScript 101 - Class 1JavaScript 101 - Class 1
JavaScript 101 - Class 1
 
Types by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaTypes by Adform Research, Saulius Valatka
Types by Adform Research, Saulius Valatka
 
Scala vs Ruby
Scala vs RubyScala vs Ruby
Scala vs Ruby
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java Developers
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Basic Operator, String and Characters in Swift.
Basic Operator, String and Characters in Swift.Basic Operator, String and Characters in Swift.
Basic Operator, String and Characters in Swift.
 
Hardened JavaScript
Hardened JavaScriptHardened JavaScript
Hardened JavaScript
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!
 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1
 
ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021
 

Similar to 42.type: Literal-based Singleton types

42: Rise of the dependent types
42: Rise of the dependent types42: Rise of the dependent types
42: Rise of the dependent types
George Leontiev
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Meetu Maltiar
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idris
Conor Farrell
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
Vladimir Parfinenko
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Aleksandar Prokopec
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kirill Rozov
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
Jakub Kahovec
 
Introduction to scala
Introduction to scalaIntroduction to scala
Introduction to scala
Michel Perez
 
Scala case of case classes
Scala   case of case classesScala   case of case classes
Scala case of case classes
VulcanMinds
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
Łukasz Bałamut
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
shinolajla
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with Scala
Ostap Andrusiv
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
Stephan Janssen
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
Tim (dev-tim) Zadorozhniy
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
Ruslan Shevchenko
 
“Insulin” for Scala’s Syntactic Diabetes
“Insulin” for Scala’s Syntactic Diabetes“Insulin” for Scala’s Syntactic Diabetes
“Insulin” for Scala’s Syntactic Diabetes
Tzach Zohar
 
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
Eric Pederson
 

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

42: Rise of the dependent types
42: Rise of the dependent types42: Rise of the dependent types
42: Rise of the dependent types
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idris
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
 
Introduction to scala
Introduction to scalaIntroduction to scala
Introduction to scala
 
Scala case of case classes
Scala   case of case classesScala   case of case classes
Scala case of case classes
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with Scala
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
“Insulin” for Scala’s Syntactic Diabetes
“Insulin” for Scala’s Syntactic Diabetes“Insulin” for Scala’s Syntactic Diabetes
“Insulin” for Scala’s Syntactic Diabetes
 
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 

Recently uploaded

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 

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
  • 3.
  • 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
  • 35.
  • 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
  • 38.
  • 39. Thanks. Questions? - Contributing to Scala compiler - Type-level programming - Working/hackertime at SoundCloud Ask me about: