Upgrade to Pro — share decks privately, control downloads, hide ads and more …

State of the Typelevel

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.

Lars Hupel

December 08, 2014
Tweet

More Decks by Lars Hupel

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. Design Principles ▶ design decisions vary among projects ... this

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

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

    ▶ scodec, scalaz-stream ▶ Argonaut, Discipline, Monocle ▶ ... 5
  6. 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
  7. 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
  8. 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
  9. Computable Reals Assump ons ▶ performance is not a concern

    ▶ precision is a concern ▶ correctness is a concern ▶ not interested in intermediate values 11
  10. 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
  11. 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
  12. 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
  13. 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
  14. Immutable Data in Scala case class Company(it: Department, hr: Department)

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

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

    company.it.head.copy( name = ”Grace Hopper”))) 19
  17. 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
  18. Upda ng Immutable Data with Monocle import monocle.syntax._ company applyLens

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

    Company.it composeLens Department.head composeLens Person.name set ”Grace Hopper” 23
  20. 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
  21. 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
  22. 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
  23. Coproduct Codecs val union = int32 :+: bool(8) :+: variableSizeBytes(uint8,

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

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

    ▶ many type class instances are completely generic ▶ abstrac ng over type classes? 28
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. More of Typelevel ▶ contrib projects ▶ compiler plugins ▶

    latest addi on: Scalaz+ScalaTest by Brendan (@rit) 37
  33. 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
  34. What’s next? ▶ more integra on between projects ▶ reduce

    transi ve dependency pain ▶ provide “stable Typelevel stack” ▶ Scala.JS ports 39