My Scala by the Bay 2014 talk on exploring the ideas behind the implementation of the generic library shapeless, and general ideas about how to do "type level" programming in Scala.
Static reasoning
• allows for us to implement powerful compile time
abstractions
• you only pay a minor cost with extra compile time,
and passing around proof terms
8
Type safe casting of an arbitrary List to Product:
9
case class Point(x: Int, y: Int)
val xs: List[Any] = List(1,2)
xs.as[Point]
https://gist.github.com/jroesch/52727c6d77a9d98458d5
scala> val q = sql("select name, age from person")
scala> q() map (_ get "age")
res0: List[Int] = List(36, 14)
compile time checked SQL:
from: https://github.com/jonifreeman/sqltyped
10
Dependent Types
• relaxation of the “phase distinction”
• simply: remove the distinction between types and
values (allow types to depend on values)
• many things billed as type level programming are
attempts at emulating dependent types
11
The phase distinction exists in Scala:
!
Vect : (Nat, Type) => Type
!
we would like to write something like this:
!
trait Vect[N <: Nat, A]
!
we are still writing this:
!
Vect : (Type, Type) => Type
12
Types as Logic
• type systems == logical framework
• types correspond to propositions, and programs to
proofs
• types can be boring (i.e Int, String, User); not all
proofs are interesting
13
How do we emulate
dependent types?
• We still have the phase distinction to get around
• We need to promote values to the type level (i.e 0
can either act as a value or type)
• Vect[0, Int] and val x = 0
14
Nat
• Natural numbers (0, 1 …)
• Let’s look at both the value level and the type level
• We need these to reason about numeric constraints
like size, ordering, indexing, and so on.
• Usually represented by a Zero element and a
Successor function.
15
sealed trait Nat
case object Zero extends Nat
case class Succ(n : Nat) extends Nat
16
A value representing a Natural number:
How do we encode a type level representation of naturals?
17
implicit val x: Int = 10
!
def needsImplicit(implicit ev: Int) = ???
implicit arguments allow us to pass extra parameters
around with help of the compiler:
19
type members allow for values to have type information:
trait User { type Email; val email : Email }
!
20
def userWithEmail[E](e : E) = new User {
type Email = E
val email = e
}
!
21
We make a type part of the value:
val user = userWithEmail(“jroesch@invoca.com”)
val email : user.Email = user.email
!
def takesUser(u: User) =
/* the type of email is hidden here */
22
The type is now existential:
also we make use of structural refinement types:
sealed trait Database
!
sealed trait Postgres extends Database
case object Postgres extends Postgres
!
sealed trait MySQL extends Database
case object MySQL extends MySQL
!
trait DataStore { type DB <: Database … }
23
// Refined type
type PostgreSQL = DataStore { type DB = Postgres }
!
def writeJSONB(ds: PostgreSQL, jsonb: JSONB) = ???
def dontCare(ds: DataStore, …) = ???
!
val postgres: PostgresSQL = new DataStore { … }
!
val otherDataStore = new DataStore { … }
!
writeJSONB(postgres, value) //works
writeJSONB(otherDataStore, value) //fails
24
We can use this to make our type more specific:
trait Unrefined
type Refined = Unrefined { type T = String }
!
implicitly[Refined <:< Unrefined]
25
refined types are subtypes of unrefined types:
trait ObligationFor[N <: Nat]
!
def proof[N <: Nat](implicit ev: ObligationFor[N])
26
we can use this sub-typing rule and type bounds to our
advantage during implicit selection:
vs
def proof(implicit ev: ObligationFor[Nat])
/* Shapeless Typelevel Natural */
trait Nat {
type N <: Nat
}
!
class _0 extends Nat {
type N = _0
}
!
class Succ[P <: Nat]() extends Nat {
type N = Succ[P]
}
27
implicit def intToNat(i: Int): Nat = …
!
val n: Nat = 1
!
type One = n.N // Succ[_0]
28
We can add an implicit conversion for Naturals:
def lessThan(n: Nat, m: Nat): Bool =
match (n, m) {
case (Zero, Succ(_)) =>
true
case (Succ(np), Succ(mp)) =>
lessThan(np, mp)
case (_, _) =>
false
}
How do we translate a value level algorithm to one
that constructs a proof object instead
29
trait IsHCons[L <: HList] {
type H
type T <: HList
def head(l : L) : H
def tail(l : L) : T
}
35
object IsHCons {
…
!
type Aux[L <: HList, Head, Tail <: HList] =
IsHCons[L] { type H = Head; type T = Tail }
!
implicit def hlistIsHCons[Head, Tail <: HList] =
new IsHCons[Head :: Tail] {
type H = Head
type T = Tail
def head(l : Head :: Tail) : H = l.head
def tail(l : Head :: Tail) : T = l.tail
}
}
36
def head(implicit c : IsHCons[L]) : c.H = c.head(l)
!
def tail(implicit c : IsHCons[L]) : c.T = c.tail(l)
We then demand proof when we implement methods on
HList’s:
37
Proofs as black boxes
• Proof objects can be treated as black boxes, we
only need to know what relationship they express,
not proof details.
• We can use shapeless as a standard library of
useful tools.
38
case class Point(x: Int, y: Int)
!
val generic = Generic.Aux[Point, Int :: Int :: HNil] =
Generic[Point]
!
val point = Point(1,2)
!
val list: Int :: Int :: HNil = generic.to(point)
!
assert(generic.from(list) == point)
39
Applying it
• We can build things using many of the same ideas
• typed SQL, JSON with schema, static string
encoding, and plenty of other uses (ex. Spray)
40
42
trait Transcode[Initial <: Encoding] {
type Result <: Encoding
!
def transcode(s: EncodedString[Initial]):
EncodedString[Result]
}
43
trait Concatable[Prefix <: Encoding, Suffix <: Encoding] {
type Result <: Encoding
/* Concat is a little verbose, we just ask for
both our strings. */
def concat(s1: EncodedString[Prefix],
s2: EncodedString[Suffix])
/* We get proof that a transcode can happen for both */
(implicit t1: Transcode.Aux[Prefix, Result]
t2: Transcode.Aux[Suffix, Result]):
/* And we get the result */
EncodedString[Result]
}
50
object SubSeq extends LowPrioritySubSeq {
type Aux[L <: HList, S <: HList] = SubSeq[L] {
type Sub = S
}
…
}
51
/* Low priority case where we just keep scanning the list. */
trait LowPrioritySubSeq {
implicit def hconsSubSeq[Sub <: HList, SH, ST <: HList]
(implicit subseq: SubSeq.Aux[Sub, ST]) =
new SubSeq[Sub] {
type Sub = SH :: ST
}
}
52
object SubSeq extends LowPrioritySubSeq {
…
/* HNil is a SubSeq of any HList */
implicit def hnilSubSeq[H <: HList] =
new SubSeq[HNil] {
type Sub = H
}
!
…
}
53
object SubSeq extends LowPrioritySubSeq {
…
implicit def hconsSubSeqIso
[H, SH, T <: HList, ST <: HList]
(implicit iso: H =:= SH,
subseq: SubSeq.Aux[T, ST]) =
new SubSeq[H :: T] {
type Sub = SH :: ST
}
}
54
There are few ways to improve upon our last example, I’ll
leave it as a fun puzzle for you.