Polymorphic function values • Functional dependencies • shapeless • Main goal – explore abstractions over shapes richer than can be captured by traditional parametric polymorphism (even with higher-kinded types) • Initial objective – an HList which supports map
both lists and tuples • Tuples are fixed length sequences of elements of distinct types • Lists are variable length sequences of elements all of the same type • HLists are variable length sequences of elements of distinct types • Code ...
If we're going to map over an HList we need “functions” which can be applied to all of its element types • Natural transformations? F[T] => G[T] Not general enough ... only gets us to KLists • SYB gives us the clue Represent polymorphic function values via type classes and instances (taking in singleton and dependent types on the way) • Code ...
• How do we specify its signature? Int :: String :: Boolean :: HNil => Boolean • We need some way of encoding the relationship between the type of the whole HList and the type of its last element • Implicits and dependent types to the rescue • Code ...
an instance of a general pattern • Type-level recursion “computes” the result type • Value-level recursion computes the result value • We do the two in tandem • Let's look at another slightly more complex example – reverse • Code ...
that HShit! • This time our type-level recursion will • Compute the result type – as before • Select the appropriate type-specific case of our polymorphic function for each element as we go • Code ...