shapeless HList to TupleN where the tuple shape need not exactly match the HList shape
Asked Answered
P

1

13

I would like to create the equivalent of:

def toTupleN[A1, ..., AN, L <: HList](l: L): TupleN[A1, ..., AN]

Code using toTupleN should compile only when there is exactly one N combination of values in l that the tuple could be created from. Anything else should generate a compile-time error. Available implicit conversions should be taken into account. Note that there are no restrictions on the size of l or the ordering of values in it.

Example:

val l = 23 :: (1, "wibble") :: (2, "wobble") :: "foo" :: HNil
// l: shapeless.::[Int,shapeless.::[(Int, String),shapeless.::[(Int, String),shapeless.::[String,shapeless.HNil]]]] = 23 :: (1,wibble) :: (2,wobble) :: foo :: HNil

val t2: (String, Int) = toTuple2(l)
// t2: (String, Int) = (foo,23)

val nope: (String, String) = toTuple2(l)
// Compiler error because no combination of l's values can create nope

val nein: ((Int, String)) = toTuple2(l)
// Another compiler error because there is more than one way l's values can create nein

This question arose from the answer to the following question. The more general machinery in this question could be used to both create data structures and call any standard function (whose arguments are of different types) using FunctionN#tupled.

Update:

Some examples to define the desired behavior with subtypes:

trait A
trait B extends A
trait C extends A

val a: A
val b: B
val c: C

toTuple2[(A, Int)](5 :: b :: HNil)      // (b, 5): subtypes match supertypes when there is no exact match
toTuple2[(A, Int)](5 :: b :: a :: HNil) // (a, 5): only one exact match is available
toTuple2[(A, Int)](5 :: a :: a :: HNil) // compile error: more than one exact match is available
toTuple2[(A, Int)](5 :: b :: c :: HNil) // compile error: more than one inexact match is available
Platto answered 18/1, 2016 at 17:55 Comment(0)
B
3

I haven't been able to make target type inference work out quite the way you wanted, but as compensation I've generalized to an arbitrary product type via shapeless's Generic,

import shapeless._, ops.hlist._, test._

object Demo {
  trait UniqueSelect[L <: HList, M <: HList] {
    def apply(l: L): M
  }

  object UniqueSelect {
    implicit def hnil[L <: HList]: UniqueSelect[L, HNil] =
      new UniqueSelect[L, HNil] {
        def apply(l: L): HNil = HNil
      }

    implicit def hcons[L <: HList, H, T <: HList, S <: HList]
      (implicit
        pt: Partition.Aux[L, H, H :: HNil, S],
        ust: UniqueSelect[S, T]
      ): UniqueSelect[L, H :: T] =
      new UniqueSelect[L, H :: T] {
        def apply(l: L): H :: T = {
          val (h :: HNil, s) = pt(l)
          h :: ust(s)
        }
      }
  }

  def toProductUniquely[P <: Product] = new ToProductUniquely[P]
  class ToProductUniquely[P <: Product] {
    def apply[L <: HList, M <: HList](l: L)
      (implicit gen: Generic.Aux[P, M], up: UniqueSelect[L, M]): P =
        gen.from(up(l))
  }

  val l = 23 :: (1, "wibble") :: (2, "wobble") :: "foo" :: HNil

  val t2 = toProductUniquely[(String, Int)](l)
  typed[(String, Int)](t2)
  assert(t2 == ("foo", 23))

  illTyped("""
  toProductUniquely[(String, String)](l)
  """)

  illTyped("""
  toProductUniquely[Tuple1[(Int, String)]](l)
  """)
}

Update 1

Adding support for the selection being satisfied by subtypes of the requested types is fairly straightforward if we say that where we have types A and B <: A then the selection of A from A :: B :: HNil is ambiguous because both elements conform to A. This can be done by adding a SubtypeUnifier to the witnesses in the previous definition of hcons,

import shapeless._, ops.hlist._, test._

object Demo extends App {
  trait UniqueSelect[L <: HList, M <: HList] {
    def apply(l: L): M
  }

  object UniqueSelect {
    implicit def hnil[L <: HList]: UniqueSelect[L, HNil] =
      new UniqueSelect[L, HNil] {
        def apply(l: L): HNil = HNil
      }

    implicit def hcons[L <: HList, M <: HList, H, T <: HList, S <: HList]
      (implicit
        su: SubtypeUnifier.Aux[L, H, M],
        pt: Partition.Aux[M, H, H :: HNil, S],
        upt: UniqueSelect[S, T]
      ): UniqueSelect[L, H :: T] =
      new UniqueSelect[L, H :: T] {
        def apply(l: L): H :: T = {
          val (h :: HNil, s) = pt(su(l))
          h :: upt(s)
        }
      }
  }

  def toProductUniquely[P <: Product] = new ToProductUniquely[P]
  class ToProductUniquely[P <: Product] {
    def apply[L <: HList, M <: HList](l: L)
      (implicit gen: Generic.Aux[P, M], up: UniqueSelect[L, M]): P =
        gen.from(up(l))
  }

  class A
  class B extends A
  class C

  val ac = new A :: new C :: HNil
  val bc = new B :: new C :: HNil
  val abc = new A :: new B :: new C :: HNil

  // Exact match
  val tac = toProductUniquely[(A, C)](ac)
  typed[(A, C)](tac)

  // Subtype
  val tbc = toProductUniquely[(A, C)](bc)
  typed[(A, C)](tbc)

  // Exact match again
  val tabc = toProductUniquely[(B, C)](abc)
  typed[(B, C)](tabc)

  // Ambiguous due to both elements conforming to A
  illTyped("""
  toProductUniquely[(A, C)](abc)
  """)
}

Update 2

We can also accommodate a unification semantics which gives preference to exact match and then falls back to a unique subtype as described in your updated question. We do this by combining the instances from the two solutions above: the exact match instance from the first at normal priority and the subtype match instance at low priority,

import shapeless._, ops.hlist._, test._

object Demo extends App {
  trait UniqueSelect[L <: HList, M <: HList] {
    def apply(l: L): M
  }

  object UniqueSelect extends UniqueSelect0 {
    implicit def hnil[L <: HList]: UniqueSelect[L, HNil] =
      new UniqueSelect[L, HNil] {
        def apply(l: L): HNil = HNil
      }

    implicit def hconsExact[L <: HList, H, T <: HList, S <: HList]
      (implicit
        pt: Partition.Aux[L, H, H :: HNil, S],
        upt: UniqueSelect[S, T]
      ): UniqueSelect[L, H :: T] =
      new UniqueSelect[L, H :: T] {
        def apply(l: L): H :: T = {
          val (h :: HNil, s) = pt(l)
          h :: upt(s)
        }
      }
  }

  trait UniqueSelect0 {
    implicit def hconsSubtype[L <: HList, M <: HList, H, T <: HList, S <: HList]
      (implicit
        su: SubtypeUnifier.Aux[L, H, M],
        pt: Partition.Aux[M, H, H :: HNil, S],
        upt: UniqueSelect[S, T]
      ): UniqueSelect[L, H :: T] =
      new UniqueSelect[L, H :: T] {
        def apply(l: L): H :: T = {
          val (h :: HNil, s) = pt(su(l))
          h :: upt(s)
        }
      }
  }

  def toProductUniquely[P <: Product] = new ToProductUniquely[P]
  class ToProductUniquely[P <: Product] {
    def apply[L <: HList, M <: HList](l: L)
      (implicit gen: Generic.Aux[P, M], up: UniqueSelect[L, M]): P = gen.from(up(l))
  }

  trait A
  trait B extends A
  trait C extends A

  val a: A = new A {}
  val b: B = new B {}
  val c: C = new C {}

  // (b, 5): subtypes match supertypes when there is no exact match
  toProductUniquely[(A, Int)](5 :: b :: HNil)

  // (a, 5): only one exact match is available
  toProductUniquely[(A, Int)](5 :: b :: a :: HNil)

  // compile error: more than one exact match is available
  illTyped("""
  toProductUniquely[(A, Int)](5 :: a :: a :: HNil)
  """)

  // compile error: more than one inexact match is available
  illTyped("""
  toProductUniquely[(A, Int)](5 :: b :: c :: HNil)
  """)
}
Baboon answered 19/1, 2016 at 13:24 Comment(5)
This is very interesting, Miles. For the moment, I'm stuck with shapeless 2.0.0 (deploying into a managed 2.10.5 environment that does not allow compiler plugins) so I had missed some of the additions to ops.hlist such as Partition. Pulling the Partition code in to run through your example, I see a dependency on toTuple2 which I cannot find in the codebase. Can you point me in the right direction, please?Platto
It's here.Baboon
This only works with exact type matches. For example, 5 :: Seq[Int](1,2,3) :: HNil cannot be turned into (Int, Seq[Any]) even though Seq[Int] <: Seq[Any]. Is there a way to address this?Platto
Given types A and B <: A, if we select A from A :: B :: HNil do you expect the result to be: 1) the A (exact match) 2) B (more precise) or 3) non-compilation because both elements conform to A, so ambiguous? If you could add an example which illustrates your choice to the question that would be useful.Baboon
Miles, I have added an example with the desired behavior covering your question specifically (desired result is option 1) and the other cases that might arise, e.g., more than one exact match and more than one inexact match. Does this define the problem sufficiently?Platto

© 2022 - 2024 — McMap. All rights reserved.