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. State of the Typelevel
    Lars Hupel (@larsr h)
    Dec 8th, 2014

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  5. The Typelevel Ecosystem
    ▶ scalaz, shapeless, spire
    5

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. A Tour Through Typelevel
    6

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  14. View Slide

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

    View Slide

  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

    View Slide

  17. Math Interlude
    f(n) − 1
    n
    ≤ a ≤
    f(n) + 1
    n
    12

    View Slide

  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

    View Slide

  19. View Slide

  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

    View Slide

  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

    View Slide

  22. Se er and Ge er in Java
    company.getITDepartment()
    .getHead()
    .setName(”Grace Hopper”);
    17

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  26. Costate Comonad Coalgebra ...?
    20

    View Slide

  27. Monocle
    ▶ Scala port of Haskell’s lens
    ▶ inspired by last year’s talk by SPJ
    21

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  34. Coproduct Codecs
    val union =
    int32 :+:
    bool(8) :+:
    variableSizeBytes(uint8, ascii)
    val codec = union.
    discriminatedBy(fixedSizeBytes(1, ascii)).
    using(Sized(”i”, ”b”, ”s”))
    27

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  39. Demo

    View Slide

  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

    View Slide

  41. View Slide

  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

    View Slide

  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

    View Slide

  44. View Slide

  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

    View Slide

  46. More of Typelevel
    ▶ contrib projects
    ▶ compiler plugins
    ▶ latest addi on: Scalaz+ScalaTest by Brendan (@rit)
    37

    View Slide

  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

    View Slide

  48. What’s next?
    ▶ more integra on between projects
    ▶ reduce transi ve dependency pain
    ▶ provide “stable Typelevel stack”
    ▶ Scala.JS ports
    39

    View Slide

  49. View Slide

  50. Q & A
    @larsr h
    @typelevel
    typelevel.org/blog

    View Slide