State of the Typelevel

A1216674d5c9747bcdcc716872439137?s=47 Lars Hupel
December 08, 2014

State of the Typelevel

The typelevel project is nearing its second anniversary -- a great time to look at what it is actually about. Starting out as a community of Scala projects aiming for principled library design, we have been continuously growing by adding more projects and most recently, forking the Scala compiler to explore improvements.

We offer solutions for a wide range of problems, ranging from functional data structures, testing and JSON processing to efficient numeric programming, stream processing and generic programming.

This talk will give a whirlwind tour of the ideas and projects behind typelevel, and how they can be used to improve your code base. We'll see some short examples of how to solve problems in a functional manner, and demonstrate some of the tooling around the libraries. Also, a critical review of the progress and an outline of the future plans of the projects will be made.

A1216674d5c9747bcdcc716872439137?s=128

Lars Hupel

December 08, 2014
Tweet

Transcript

  1. State of the Typelevel Lars Hupel (@larsr h) Dec 8th,

    2014
  2. A Brief History of Typelevel ▶ founded in early 2013

    ▶ ini ally consis ng of scalaz, shapeless and spire ▶ since then: numerous addi ons, SBT plugin, Scala fork 2
  3. Goals ▶ aims to provide a set of loosely-coupled libraries

    ▶ type-based design and reasoning ▶ general supplements for the standard library ▶ embrace Scala and its compiler 3
  4. Design Principles ▶ design decisions vary among projects ... this

    is a good thing! ▶ many projects heavily inspired by Haskell ▶ harnessing Scala’s speciali es 4
  5. The Typelevel Ecosystem ▶ scalaz, shapeless, spire 5

  6. The Typelevel Ecosystem ▶ scalaz, shapeless, spire ▶ specs2, scalacheck

    5
  7. The Typelevel Ecosystem ▶ scalaz, shapeless, spire ▶ specs2, scalacheck

    ▶ scodec, scalaz-stream 5
  8. The Typelevel Ecosystem ▶ scalaz, shapeless, spire ▶ specs2, scalacheck

    ▶ scodec, scalaz-stream ▶ Argonaut, Discipline, Monocle 5
  9. The Typelevel Ecosystem ▶ scalaz, shapeless, spire ▶ specs2, scalacheck

    ▶ scodec, scalaz-stream ▶ Argonaut, Discipline, Monocle ▶ ... 5
  10. A Tour Through Typelevel 6

  11. Working with Numbers ▶ in JVM land, fixed-precision arithme cs

    is s ll the “default” ▶ be er: use BigInt, BigDecimal, ... from the standard library ▶ But what if performance is a concern? 7
  12. Be er Numerics with Spire ▶ started out as a

    SIP dra presented at Scala Days 2012 ▶ developed into a full-fleged algebra and numerics library ▶ contains types, type classes, algorithms ▶ numeric stack (Monoid, Ring, ...) ▶ Natural, Rational, ... ▶ univariate polynomials ▶ algebraic numbers, computable reals ▶ random numbers 8
  13. Floa ng-Point Numbers > 0.15 + 0.15 0.3 > 0.1

    + 0.2 0.30000000000000004 > (0.15 + 0.15) == (0.1 + 0.2) false 9
  14. None
  15. Computable Reals Assump ons ▶ performance is not a concern

    ▶ precision is a concern ▶ correctness is a concern ▶ not interested in intermediate values 11
  16. Computable Reals Assump ons ▶ performance is not a concern

    ▶ precision is a concern ▶ correctness is a concern ▶ not interested in intermediate values Func onal Programming Solu on Treat numbers as func ons! 11
  17. Math Interlude f(n) − 1 n ≤ a ≤ f(n)

    + 1 n 12
  18. Computable Reals in Spire ▶ offers a func on toRational(p:

    Int): Rational ▶ returns a frac on with denominator 2p ▶ arithme c opera ons work with slightly higher precisions internally result is as precise as possible ▶ many common constants (e.g. π) can be represented 13
  19. None
  20. Computable Reals Advantages ▶ as precise as you want ▶

    decision can be deferred ▶ supports wide variety of opera ons Disadvantages ▶ not all real numbers can be represented ▶ no reliable equality ▶ performance reasoning difficult 15
  21. Se er and Ge er in Java class Person {

    private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } 16
  22. Se er and Ge er in Java company.getITDepartment() .getHead() .setName(”Grace

    Hopper”); 17
  23. Immutable Data in Scala case class Company(it: Department, hr: Department)

    case class Department(head: Person, budget: Currency) case class Person(name: String) 18
  24. Upda ng Immutable Data company.copy( it = company.it.copy( head =

    company.it.head.copy( name = ”Grace Hopper”))) 19
  25. Upda ng Immutable Data company.copy( it = company.it.copy( head =

    company.it.head.copy( name = ”Grace Hopper”))) 19
  26. Costate Comonad Coalgebra ...? 20

  27. Monocle ▶ Scala port of Haskell’s lens ▶ inspired by

    last year’s talk by SPJ 21
  28. Immutable Data with Monocle import monocle.macros.Lenses @Lenses case class Company(it:

    Department, hr: Department) @Lenses case class Department(head: Person, budget: Currency) @Lenses case class Person(name: String) 22
  29. Upda ng Immutable Data with Monocle import monocle.syntax._ company applyLens

    Company.it composeLens Department.head composeLens Person.name set ”Grace Hopper” 23
  30. Upda ng Immutable Data with Monocle import monocle.syntax._ company applyLens

    Company.it composeLens Department.head composeLens Person.name set ”Grace Hopper” 23
  31. Using Monocle ▶ nearing 1.0 release ▶ “ba eries included”

    ▶ comes with lenses for standard library, scalaz, shapeless ▶ lenses can be auto-derived by macros or wri en by hand ▶ likely to supersede scalaz’ lenses 24
  32. Binary Serializa on ▶ there are lot of libraries to

    deal with JSON scala.u l.JSON Rapture JSON Argonaut Jackson Jawn json4s Li Play Spray ... ▶ But what about binary formats? 25
  33. Binary Formats with scodec ▶ scodec solves the problem from

    the ground up: ▶ fast ByteString implementa on ▶ hex and binary string interpolators ▶ support for variants/sums/... ▶ supports generic programming via shapeless 26
  34. Coproduct Codecs val union = int32 :+: bool(8) :+: variableSizeBytes(uint8,

    ascii) val codec = union. discriminatedBy(fixedSizeBytes(1, ascii)). using(Sized(”i”, ”b”, ”s”)) 27
  35. Underlying Magic ▶ type classes abstract opera ons over data

    ▶ many type class instances are completely generic ▶ abstrac ng over type classes? 28
  36. Underlying Magic ▶ type classes abstract opera ons over data

    ▶ many type class instances are completely generic ▶ abstrac ng over type classes? 28
  37. Underlying Magic ▶ type classes abstract opera ons over data

    ▶ many type class instances are completely generic ▶ abstrac ng over type classes? ▶ driven by shapeless 28
  38. Automa c Instance Deriva on trait List[T] case class Nil[T]()

    extends List[T] case class Cons[T](hd: T, tl: List[T]) extends List[T] ▶ equality, ordering, prin ng, ... trivial to write ▶ scalac already does toString and equals ▶ shapeless can do even more 29
  39. Demo

  40. Lazy Implicit Resolu on ▶ there’s a more general abstrac

    on: lazy implicits! ▶ coming up in next shapeless version ▶ allows to write implicits in a natural way 31
  41. None
  42. Typelevel Scala ▶ experimental playground for new features ▶ less

    strict stability requirements ▶ faster rate of change expected ▶ features can move in both direc ons between Typesafe and Typelevel Scala 33
  43. Reasons for Typelevel Scala ▶ different interests than Typesafe ▶

    Typelevel projects can benefit from new compiler features ▶ we want a language which is open and moving quickly ▶ we want to see a greater diversity of ideas in the ecosystem 34
  44. None
  45. Progress of Typelevel Scala ▶ 12 pull requests already merged

    ▶ some even got reviews by Typesafe ▶ en rely community-driven ▶ issues, feature requests and patches welcome! 36
  46. More of Typelevel ▶ contrib projects ▶ compiler plugins ▶

    latest addi on: Scalaz+ScalaTest by Brendan (@rit) 37
  47. Binary Compa bility ▶ offer similar guarantees like Scala e.g.

    all releases in Scalaz 7.0.x backwards binary compa ble ▶ dra document which specifies precise constraints ▶ some projects already sa sfy these constraints ▶ SBT plugin to help enforce compa bility ▶ but: not quite there yet 38
  48. What’s next? ▶ more integra on between projects ▶ reduce

    transi ve dependency pain ▶ provide “stable Typelevel stack” ▶ Scala.JS ports 39
  49. None
  50. Q & A @larsr h @typelevel typelevel.org/blog