Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In shapeless, have two lists such that one contains typeclasses of the other

In shapeless I'm trying to write a function such that takes two HLists l1 and l2 of arbitrary length which exhibit the following properties:

  1. Length of l1 and l2 are the same.
  2. l2 contains the exact types of l1, wrapped in a constant outer type constructor.

So, if l1 was

1 :: 1.2 :: "hello" :: HNil`

l2 could be

Ordering[Int] :: Ordering[Double] :: Ordering[String] :: HNil

Using UnaryTCConstraint and LengthAux lets me constrain the lengths and require a static outer constructor for l2, however having them conform has become a problem.

Any ideas on how I could go about it?

like image 433
Refefer Avatar asked Jan 08 '14 19:01

Refefer


1 Answers

Mapped provides precisely this constraint without the additional need for Length. From the documentation:

Type class witnessing that the result of wrapping each element of HList L in type constructor F is Out.

Here's how it looks in 1.2.4:

import shapeless._

def foo[L1 <: HList, L2 <: HList](l1: L1, l2: L2)(implicit
  ev: MappedAux[L1, Ordering, L2]
) = ()

val l1 = 1 :: 1.2 :: "hello" :: HNil
val l2 = Ordering[Int] :: Ordering[Double] :: Ordering[String] :: HNil
val l3 = Ordering[Int] :: Ordering[Double] :: Ordering[Char] :: HNil

And then:

scala> foo(l1, l2)

scala> foo(l1, l3)
<console>:17: error: could not find implicit value for parameter ev: ...

As expected. For 2.0 just add a shapeless.ops.hlist._ import and replace MappedAux with Mapped.Aux and you're ready to go.

like image 65
Travis Brown Avatar answered Nov 13 '22 19:11

Travis Brown