Recursive implicit proofs with Shapeless HLists

Vladimir Pavkin
Vladimir PavkinScala Developer at Catch App
Recursive implicit proofs with Shapeless HLists
1.
2.
3.
4.
5.
6.
7.
8.
https://github.com/vpavkin/
recursive-implicit-proofs-talk
Recursive implicit proofs with Shapeless HLists
Recursive implicit proofs with Shapeless HLists
Recursive implicit proofs with Shapeless HLists
Ordering[A]
implicit val intOrdering = new
Ordering[Int] {…}
def sortBy[B](f: A => B) (implicit ord:
Ordering[B])
//or
def sortBy[B: Ordering](f: A => B)
implicit val intOrdering = …
implicit def optOrdering[T:Ordering] =
new Ordering[Option[T]] { … }
// Ok
implicitly[Ordering[Option[Int]]]
// Can’t prove
implicitly[Ordering[Option[String]]]
Recursive implicit proofs with Shapeless HLists
Recursive implicit proofs with Shapeless HLists
// type A = Int
implicit val intOrdering: Ordering[Int] = …
// type B = Option[A]
implicit def optOrdering[A]
(implicit ev: Ordering[A]): Ordering[Option[A]] = …
// type C = (A, B)
implicit def tuple2Ordering[A, B]
(implicit A: Ordering[A],
B: Ordering[B]): Ordering[(A, B)] = …
implicit def contravariantOrdering[A, B]
(implicit C: Converts[A, B],
O: Ordering[B]): Ordering[A] =
implicit def ordered[C <% Comparable[C]]: Ordering[C] = …
implicit def comparatorToOrdering[C]
(implicit cmp: Comparator[C]): Ordering[C] = …
A => C
+
B => C
Avoid ambiguous implicits!
You can’t ask for an absence of implicit
1 -> Container(Option(Container("a")))
(Int, Container[Option[Container[String]]])
String Container[String] Option[Container[String]]
Container[Option[Container[String]]] (Int, Container[Option[Container[String]]])
A A => B
Ord[T] => Ord[Container[T]]
A => B
Ord[T] => Ord[Option[T]]
A => B
Ord[T] => Ord[Container[T]]
A & B => C
Ord[A] & Ord[B] => Ord[(A,B)]
Int
A
Recursive implicit proofs with Shapeless HLists
Scala List shapeless.HList
Root type List HList
Cons
case class ::[A](
head: A,
tail: List[A])
case class ::[H, T <: HList](
head: H,
tail: T)
Nil Nil (singleton) HNil (singleton)
Type of
1 :: Nil
::[Int] Int :: HNil
Type of
1 :: “a” :: Nil
::[Any] Int :: String :: HNil
Type of
1 :: “a” :: true ::
Nil
::[Any] Int :: String :: Boolean :: HNil
•
•
•
•
•
final class HListOps[L <: HList](l : L) {
def at[N <: Nat](implicit at: At[L, N]): at.Out = at(l)
}
•
•
•
•
•
case class Person(name: String, age:Int)  String :: Int :: HNil
(Int, Boolean)  Int :: Boolean :: HNil
•
•
2nd most important property (along with keeping precise types):
Recursive implicit proofs with Shapeless HLists
Recursive implicit proofs with Shapeless HLists
Recursive implicit proofs with Shapeless HLists
OddProduct[Int :: String :: Boolean :: Int :: Int :: HNil]
Int :: HNil Boolean :: Int :: Int :: HNil
Int :: String:: Boolean :: Int :: Int :: HNil
A => B
Odd[T <: HList] => Odd[H1 :: H2 :: T]
A => B
Odd[T <: HList] => Odd[H1 :: H2 :: T]
A
Odd[T :: HNil]
Recursive step – resolving to the same implicit def
Recursive implicit proofs with Shapeless HLists
Recursive implicit proofs with Shapeless HLists
Generic.Aux[T, Repr] – Isomorphism, that
allows us to get Repr from T and vice versa.
Repr – some generic representation of T
Property[Repr] – our Property is proved for
type Repr.
We can (almost always) derive the
Property[T] by converting to/from Repr
and using Property[Repr]
Hint:
Repr here is going to be
an HList
Looks similar to
A & B => C.
Let’s encode the proof!
Recursive implicit proofs with Shapeless HLists
Person(name: String, age: Int)
Int :: HNil
A & B => C
Show[A] & Show[B] => Show[A :: B]
Int
A
Show[Int]
HNil
A
Show[HNil]
String
A
Show[String]
String :: Int :: HNil
A & B => C
Show[A] & Show[B] => Show[A :: B]
Generic.Aux[Person, String :: Int :: HNil]
A (macro)
Generic[T]
Person
A & B => C
Show[R] & Generic.Aux[T,R] => Show[T]
Recursive implicit proofs with Shapeless HLists
1.
2.
3.
4.
5.
Recursive implicit proofs with Shapeless HLists
Recursive implicit proofs with Shapeless HLists
1.
2.
3.
4.
5.
6.
Recursive implicit proofs with Shapeless HLists
1 of 36

Recommended

What's New in TypeScript by
What's New in TypeScriptWhat's New in TypeScript
What's New in TypeScriptMicrosoft Tech Community
215 views63 slides
Unit2 input output by
Unit2 input outputUnit2 input output
Unit2 input outputdeepak kumbhar
202 views10 slides
Managing Input Output Operations by
Managing Input Output OperationsManaging Input Output Operations
Managing Input Output OperationsMunazza-Mah-Jabeen
119 views6 slides
Kotlin Starter Pack by
Kotlin Starter PackKotlin Starter Pack
Kotlin Starter PackJimmy Morales
108 views40 slides
The Next Great Functional Programming Language by
The Next Great Functional Programming LanguageThe Next Great Functional Programming Language
The Next Great Functional Programming LanguageJohn De Goes
4.2K views48 slides
DEFCON 23 - Miaubiz - put on your tinfo_t hat by
DEFCON 23 - Miaubiz - put on your tinfo_t hatDEFCON 23 - Miaubiz - put on your tinfo_t hat
DEFCON 23 - Miaubiz - put on your tinfo_t hatFelipe Prado
73 views47 slides

More Related Content

What's hot

Monoids, Monoids, Monoids - ScalaLove 2020 by
Monoids, Monoids, Monoids - ScalaLove 2020Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Luka Jacobowitz
739 views58 slides
devLink - What's New in C# 4? by
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?Kevin Pilch
343 views25 slides
Descriptors by
DescriptorsDescriptors
DescriptorsHonza Král
652 views14 slides
Principled Error Handling with FP by
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FPLuka Jacobowitz
2.4K views37 slides
Core csharp and net quick reference by
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick referenceilesh raval
3.2K views2 slides
Testing in the World of Functional Programming by
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional ProgrammingLuka Jacobowitz
1.6K views37 slides

What's hot(20)

Monoids, Monoids, Monoids - ScalaLove 2020 by Luka Jacobowitz
Monoids, Monoids, Monoids - ScalaLove 2020Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020
Luka Jacobowitz739 views
devLink - What's New in C# 4? by Kevin Pilch
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?
Kevin Pilch343 views
Principled Error Handling with FP by Luka Jacobowitz
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FP
Luka Jacobowitz2.4K views
Core csharp and net quick reference by ilesh raval
Core csharp and net quick referenceCore csharp and net quick reference
Core csharp and net quick reference
ilesh raval3.2K views
Testing in the World of Functional Programming by Luka Jacobowitz
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
Luka Jacobowitz1.6K views
Generics and Inference by Richard Fox
Generics and InferenceGenerics and Inference
Generics and Inference
Richard Fox173 views
One Monad to Rule Them All by John De Goes
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them All
John De Goes4.6K views
Fp in scala part 1 by Hang Zhao
Fp in scala part 1Fp in scala part 1
Fp in scala part 1
Hang Zhao961 views
Abstracting over Execution with Higher Kinded Types by Philip Schwarz
Abstracting over Execution with Higher Kinded TypesAbstracting over Execution with Higher Kinded Types
Abstracting over Execution with Higher Kinded Types
Philip Schwarz631 views
Fp in scala with adts part 2 by Hang Zhao
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
Hang Zhao1K views
The Swift Compiler and Standard Library by Santosh Rajan
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
Santosh Rajan10.4K views
Python легко и просто. Красиво решаем повседневные задачи by Maxim Kulsha
Python легко и просто. Красиво решаем повседневные задачиPython легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачи
Maxim Kulsha1.3K views
FParsec Hands On - F#unctional Londoners 2014 by Phillip Trelford
FParsec Hands On -  F#unctional Londoners 2014FParsec Hands On -  F#unctional Londoners 2014
FParsec Hands On - F#unctional Londoners 2014
Phillip Trelford11.8K views
Python-02| Input, Output & Import by Mohd Sajjad
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
Mohd Sajjad1.3K views
Intro python-object-protocol by Shiyao Ma
Intro python-object-protocolIntro python-object-protocol
Intro python-object-protocol
Shiyao Ma771 views
Делаем пользовательское Api на базе Shapeless by Вадим Челышов
Делаем пользовательское Api на базе ShapelessДелаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе Shapeless
Functor, Apply, Applicative And Monad by Oliver Daff
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And Monad
Oliver Daff2.5K views

Viewers also liked

Beyond Scala Lens by
Beyond Scala LensBeyond Scala Lens
Beyond Scala LensJulien Truffaut
12.7K views67 slides
Scala lens: An introduction by
Scala lens: An introductionScala lens: An introduction
Scala lens: An introductionKnoldus Inc.
5.9K views33 slides
Ways To Give- Delaware & Lehigh- Delaware & Lehigh by
Ways To Give- Delaware & Lehigh- Delaware & LehighWays To Give- Delaware & Lehigh- Delaware & Lehigh
Ways To Give- Delaware & Lehigh- Delaware & LehighCathy Canevari
179 views8 slides
Castelul Bran by
Castelul BranCastelul Bran
Castelul BranAndrici Cezar
460 views13 slides
Echipele de proiect şi temele acestora by
Echipele de proiect şi temele acestoraEchipele de proiect şi temele acestora
Echipele de proiect şi temele acestoraAndrici Cezar
259 views2 slides
Golden eye gifts by
Golden eye giftsGolden eye gifts
Golden eye giftsGOLDEN EYE GIFTS GEN TRADING LLC
229 views24 slides

Viewers also liked(20)

Scala lens: An introduction by Knoldus Inc.
Scala lens: An introductionScala lens: An introduction
Scala lens: An introduction
Knoldus Inc.5.9K views
Ways To Give- Delaware & Lehigh- Delaware & Lehigh by Cathy Canevari
Ways To Give- Delaware & Lehigh- Delaware & LehighWays To Give- Delaware & Lehigh- Delaware & Lehigh
Ways To Give- Delaware & Lehigh- Delaware & Lehigh
Cathy Canevari179 views
Echipele de proiect şi temele acestora by Andrici Cezar
Echipele de proiect şi temele acestoraEchipele de proiect şi temele acestora
Echipele de proiect şi temele acestora
Andrici Cezar259 views
Hotel el tollo by HotelTollo
Hotel el tolloHotel el tollo
Hotel el tollo
HotelTollo212 views
Bowser jr. presentation by pikaori
Bowser jr. presentationBowser jr. presentation
Bowser jr. presentation
pikaori177 views
Practically Functional by djspiewak
Practically FunctionalPractically Functional
Practically Functional
djspiewak1.5K views
Catálogo 4-2016 de Oriflame España by Anika Díaz
Catálogo 4-2016 de Oriflame EspañaCatálogo 4-2016 de Oriflame España
Catálogo 4-2016 de Oriflame España
Anika Díaz415 views
Demystifying Shapeless by Jared Roesch
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless
Jared Roesch8.4K views
Catálogo 17 Oriflame España by Anika Díaz
Catálogo 17 Oriflame EspañaCatálogo 17 Oriflame España
Catálogo 17 Oriflame España
Anika Díaz802 views
Catálogo 14 Oriflame - Más en rincondeika.es by Anika Díaz
Catálogo 14 Oriflame - Más en rincondeika.esCatálogo 14 Oriflame - Más en rincondeika.es
Catálogo 14 Oriflame - Más en rincondeika.es
Anika Díaz1.2K views
Естетотерапія by yakusheva
ЕстетотерапіяЕстетотерапія
Естетотерапія
yakusheva822 views
"How about no grep and zabbix?". ELK based alerts and metrics. by Vladimir Pavkin
"How about no grep and zabbix?". ELK based alerts and metrics."How about no grep and zabbix?". ELK based alerts and metrics.
"How about no grep and zabbix?". ELK based alerts and metrics.
Vladimir Pavkin10.1K views
High Wizardry in the Land of Scala by djspiewak
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
djspiewak3.6K views
Megan Crewe-mai las-o moale cu fantomele by Ade MA
Megan Crewe-mai las-o moale cu fantomeleMegan Crewe-mai las-o moale cu fantomele
Megan Crewe-mai las-o moale cu fantomele
Ade MA1.5K views
Julie garwood foc si gheata-buchannan renard7 by Ade MA
Julie garwood foc si gheata-buchannan renard7Julie garwood foc si gheata-buchannan renard7
Julie garwood foc si gheata-buchannan renard7
Ade MA12K views
Christina-Lauren-Expertul-seducator Vol 3 by Ellen Kalis
Christina-Lauren-Expertul-seducator Vol 3Christina-Lauren-Expertul-seducator Vol 3
Christina-Lauren-Expertul-seducator Vol 3
Ellen Kalis6.7K views

Similar to Recursive implicit proofs with Shapeless HLists

JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e... by
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...PROIDEA
409 views410 slides
Scala implicits by
Scala implicitsScala implicits
Scala implicitsnkpart
688 views27 slides
ECMA 入门 by
ECMA 入门ECMA 入门
ECMA 入门chencheng 云谦
813 views39 slides
Tip Top Typing - A Look at Python Typing by
Tip Top Typing - A Look at Python TypingTip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python TypingPatrick Viafore
227 views114 slides
Monads and friends demystified by
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystifiedAlessandro Lacava
822 views25 slides
Types by Adform Research by
Types by Adform ResearchTypes by Adform Research
Types by Adform ResearchVasil Remeniuk
635 views29 slides

Similar to Recursive implicit proofs with Shapeless HLists(8)

JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e... by PROIDEA
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
PROIDEA409 views
Scala implicits by nkpart
Scala implicitsScala implicits
Scala implicits
nkpart688 views
Tip Top Typing - A Look at Python Typing by Patrick Viafore
Tip Top Typing - A Look at Python TypingTip Top Typing - A Look at Python Typing
Tip Top Typing - A Look at Python Typing
Patrick Viafore227 views
Type classes 101 - classification beyond inheritance by Alexey Raga
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritance
Alexey Raga480 views
Algebraic Data Types and Origami Patterns by Vasil Remeniuk
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
Vasil Remeniuk1.7K views

Recently uploaded

aATP - New Correlation Confirmation Feature.pptx by
aATP - New Correlation Confirmation Feature.pptxaATP - New Correlation Confirmation Feature.pptx
aATP - New Correlation Confirmation Feature.pptxEsatEsenek1
222 views6 slides
Supercharging your Python Development Environment with VS Code and Dev Contai... by
Supercharging your Python Development Environment with VS Code and Dev Contai...Supercharging your Python Development Environment with VS Code and Dev Contai...
Supercharging your Python Development Environment with VS Code and Dev Contai...Dawn Wages
5 views51 slides
Flask-Python by
Flask-PythonFlask-Python
Flask-PythonTriloki Gupta
10 views12 slides
Streamlining Your Business Operations with Enterprise Application Integration... by
Streamlining Your Business Operations with Enterprise Application Integration...Streamlining Your Business Operations with Enterprise Application Integration...
Streamlining Your Business Operations with Enterprise Application Integration...Flexsin
5 views12 slides
Mobile App Development Company by
Mobile App Development CompanyMobile App Development Company
Mobile App Development CompanyRichestsoft
5 views6 slides
Quality Assurance by
Quality Assurance Quality Assurance
Quality Assurance interworksoftware2
8 views6 slides

Recently uploaded(20)

aATP - New Correlation Confirmation Feature.pptx by EsatEsenek1
aATP - New Correlation Confirmation Feature.pptxaATP - New Correlation Confirmation Feature.pptx
aATP - New Correlation Confirmation Feature.pptx
EsatEsenek1222 views
Supercharging your Python Development Environment with VS Code and Dev Contai... by Dawn Wages
Supercharging your Python Development Environment with VS Code and Dev Contai...Supercharging your Python Development Environment with VS Code and Dev Contai...
Supercharging your Python Development Environment with VS Code and Dev Contai...
Dawn Wages5 views
Streamlining Your Business Operations with Enterprise Application Integration... by Flexsin
Streamlining Your Business Operations with Enterprise Application Integration...Streamlining Your Business Operations with Enterprise Application Integration...
Streamlining Your Business Operations with Enterprise Application Integration...
Flexsin 5 views
Mobile App Development Company by Richestsoft
Mobile App Development CompanyMobile App Development Company
Mobile App Development Company
Richestsoft 5 views
Electronic AWB - Electronic Air Waybill by Freightoscope
Electronic AWB - Electronic Air Waybill Electronic AWB - Electronic Air Waybill
Electronic AWB - Electronic Air Waybill
Freightoscope 6 views
ADDO_2022_CICID_Tom_Halpin.pdf by TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin96 views
Bootstrapping vs Venture Capital.pptx by Zeljko Svedic
Bootstrapping vs Venture Capital.pptxBootstrapping vs Venture Capital.pptx
Bootstrapping vs Venture Capital.pptx
Zeljko Svedic16 views
Automated Testing of Microsoft Power BI Reports by RTTS
Automated Testing of Microsoft Power BI ReportsAutomated Testing of Microsoft Power BI Reports
Automated Testing of Microsoft Power BI Reports
RTTS11 views
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile... by Stefan Wolpers
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...
Stefan Wolpers44 views
FOSSLight Community Day 2023-11-30 by Shane Coughlan
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30
Shane Coughlan8 views
tecnologia18.docx by nosi6702
tecnologia18.docxtecnologia18.docx
tecnologia18.docx
nosi67026 views
How to build dyanmic dashboards and ensure they always work by Wiiisdom
How to build dyanmic dashboards and ensure they always workHow to build dyanmic dashboards and ensure they always work
How to build dyanmic dashboards and ensure they always work
Wiiisdom16 views
Advanced API Mocking Techniques Using Wiremock by Dimpy Adhikary
Advanced API Mocking Techniques Using WiremockAdvanced API Mocking Techniques Using Wiremock
Advanced API Mocking Techniques Using Wiremock
Dimpy Adhikary5 views
JioEngage_Presentation.pptx by admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254559 views
Understanding HTML terminology by artembondar5
Understanding HTML terminologyUnderstanding HTML terminology
Understanding HTML terminology
artembondar58 views

Recursive implicit proofs with Shapeless HLists

  • 7. Ordering[A] implicit val intOrdering = new Ordering[Int] {…}
  • 8. def sortBy[B](f: A => B) (implicit ord: Ordering[B]) //or def sortBy[B: Ordering](f: A => B) implicit val intOrdering = … implicit def optOrdering[T:Ordering] = new Ordering[Option[T]] { … } // Ok implicitly[Ordering[Option[Int]]] // Can’t prove implicitly[Ordering[Option[String]]]
  • 11. // type A = Int implicit val intOrdering: Ordering[Int] = …
  • 12. // type B = Option[A] implicit def optOrdering[A] (implicit ev: Ordering[A]): Ordering[Option[A]] = …
  • 13. // type C = (A, B) implicit def tuple2Ordering[A, B] (implicit A: Ordering[A], B: Ordering[B]): Ordering[(A, B)] = … implicit def contravariantOrdering[A, B] (implicit C: Converts[A, B], O: Ordering[B]): Ordering[A] =
  • 14. implicit def ordered[C <% Comparable[C]]: Ordering[C] = … implicit def comparatorToOrdering[C] (implicit cmp: Comparator[C]): Ordering[C] = … A => C + B => C Avoid ambiguous implicits!
  • 15. You can’t ask for an absence of implicit
  • 16. 1 -> Container(Option(Container("a"))) (Int, Container[Option[Container[String]]]) String Container[String] Option[Container[String]] Container[Option[Container[String]]] (Int, Container[Option[Container[String]]]) A A => B Ord[T] => Ord[Container[T]] A => B Ord[T] => Ord[Option[T]] A => B Ord[T] => Ord[Container[T]] A & B => C Ord[A] & Ord[B] => Ord[(A,B)] Int A
  • 18. Scala List shapeless.HList Root type List HList Cons case class ::[A]( head: A, tail: List[A]) case class ::[H, T <: HList]( head: H, tail: T) Nil Nil (singleton) HNil (singleton) Type of 1 :: Nil ::[Int] Int :: HNil Type of 1 :: “a” :: Nil ::[Any] Int :: String :: HNil Type of 1 :: “a” :: true :: Nil ::[Any] Int :: String :: Boolean :: HNil
  • 20. final class HListOps[L <: HList](l : L) { def at[N <: Nat](implicit at: At[L, N]): at.Out = at(l) } • • • • •
  • 21. case class Person(name: String, age:Int)  String :: Int :: HNil (Int, Boolean)  Int :: Boolean :: HNil • • 2nd most important property (along with keeping precise types):
  • 25. OddProduct[Int :: String :: Boolean :: Int :: Int :: HNil] Int :: HNil Boolean :: Int :: Int :: HNil Int :: String:: Boolean :: Int :: Int :: HNil A => B Odd[T <: HList] => Odd[H1 :: H2 :: T] A => B Odd[T <: HList] => Odd[H1 :: H2 :: T] A Odd[T :: HNil] Recursive step – resolving to the same implicit def
  • 28. Generic.Aux[T, Repr] – Isomorphism, that allows us to get Repr from T and vice versa. Repr – some generic representation of T Property[Repr] – our Property is proved for type Repr. We can (almost always) derive the Property[T] by converting to/from Repr and using Property[Repr] Hint: Repr here is going to be an HList Looks similar to A & B => C. Let’s encode the proof!
  • 30. Person(name: String, age: Int) Int :: HNil A & B => C Show[A] & Show[B] => Show[A :: B] Int A Show[Int] HNil A Show[HNil] String A Show[String] String :: Int :: HNil A & B => C Show[A] & Show[B] => Show[A :: B] Generic.Aux[Person, String :: Int :: HNil] A (macro) Generic[T] Person A & B => C Show[R] & Generic.Aux[T,R] => Show[T]

Editor's Notes

  1. Excited to see so many smart people here We’re going to play with the dark side of Scala that opens lots of opportunities and powerful tecniques. There will always be debates, whether it’s applicable to real-life programming or not, but I’ve already seen it being applied with big success and I believe that haters are just afraid to admit they can’t or don’t want to deeper understand the type system
  2. Nothing new from the technical side – just another point of view which shows real beauty of implicits. Also this section prepares the “proof”-oriented mind set, that will help with the rest of the talk
  3. Theorem here is very very complex – it’s the set of all requirements to our system So in reality we decompose it in a set of smaller pieces and constructing proofs for each piece.
  4. Compile time and runtime proofs. Runtime is a difference – when: compile-time proof is impossible (external inputs) or impractical (typesystem weakness)
  5. All of these are just simple introductory thoughts to seed the idea. We will formalize this system a little later and see how exactly you build mathematical proof primitives with implicits
  6. Don’t forget to show the implicit resolution with Shift+Cmd+P Say that recursion is important feature, that is required for HList based proofs
  7. Will go through basic building blocks, the primitives of proof construction. We’ll also see some differences that arise in the process of comparison
  8. Basic axiom that doesn’t require any other conditions
  9. By chaining several of such blocks we can build proofs of arbitrary length
  10. This one is not restricted to 2 elements – we can require as many conditions as we want to produce a new property
  11. This one is not used often: Only one single proof can be available for any call – having 2 or more is not allowed. This means you must be sure, that if you have several proof rules for the same type, maximum 1 is active in every particular context.
  12. Talk about the reverse order that compiler takes to find the proof
  13. Some of you are familiar with basics of HLists, but to be consistent and sure that everyone is able to understand the following material, I’ll give a short review.
  14. HList difference: Cons has 2 type parameters which allows to store precise type of the tail. In the end HList doesn’t lose any type information at all.
  15. Pure means it doesn’t carry any helpful methods along – only some fact about the type. You won’t see this kind of typeclasses very often, though they can be used. for example to build proofs for phantom types. Show implicit chain for 5 elements Show proof step by step
  16. Pure means it doesn’t carry any helpful methods along – only some fact about the type. You won’t see this kind of typeclasses very often, though they can be used. for example to build proofs for phantom types. Show implicit chain for 5 elements Show proof step by step
  17. Talk about the reverse order that compiler takes to find the proof Talk about similarity between recursive HList proofs and math induction
  18. Talk about the reverse order that compiler takes to find the proof
  19. Generic definition Generic.Aux definition Example without Aux
  20. Generic definition Generic.Aux definition Example without Aux
  21. Generic definition Generic.Aux definition Example without Aux