3

Given 2 GADT Algebras which know about each other and 2 interpreters that are mutually recursive, I am having issues having to cast from type A to type h <: HList even though in the context of the pattern match, it should be implied that type A is type h.

Is there a way to avoid the asInstanceOf[h] call in the interpreter?

  abstract class KvpHList[H<:HList]
  object KvpNil extends KvpHList[HNil]
  case class KvpCons[H <: A :: T,A, T<:HList](head: KvpValue[A], tail: KvpHList[T])(implicit isHCons: IsHCons.Aux[H,A,T]) extends KvpHList[H] {
    val hCons: IsHCons.Aux[H,A,T] = isHCons
  }

  abstract class KvpValue[A]
  case object StringData extends KvpValue[String]
  case class HListData[H <:HList](member: KvpHList[H]) extends KvpValue[H]

  def hListInterpreter[H<:HList](hList: KvpHList[H]): H => String = {
      hList match {
        case KvpNil => (hNil: H) => "Nil"
        case cons: KvpCons[H,a,t]=> {
          implicit val hCons = cons.hCons
          (input: H) => {
            s"${kvpInterpreter(cons.head)(input.head)} :: ${hListInterpreter(cons.tail)(input.tail)}"
          }
        }
      }
  }

  def kvpInterpreter[A](kvpValue: KvpValue[A]): A => String = {
    kvpValue match {
      case StringData => (str: String) => str
      case h: HListData[h] => {
        (input: A) => {                               // tried (input: h) as well
          val toString: h => String = hListInterpreter(h.member)
          toString(input.asInstanceOf[h])             // <--- CASTING :(
        }
      }
    }
  }

  kvpInterpreter(HListData(KvpCons(StringData, KvpNil))).apply("Hello" :: HNil)
Travis Stevens
  • 2,198
  • 2
  • 17
  • 25

1 Answers1

3

Since H in KvpCons is uniquely determined by A and T, KvpCons can be parametrized with two type parameters rather than three.

Type-level pattern matching is type classes

  abstract class KvpHList[H <: HList]
  object KvpNil extends KvpHList[HNil]
  case class KvpCons[A, T <: HList](head: KvpValue[A], tail: KvpHList[T]) extends KvpHList[A :: T]

  abstract class KvpValue[A]
  case object StringData extends KvpValue[String]
  case class HListData[H <: HList](member: KvpHList[H]) extends KvpValue[H]

  trait HListInterpreter[H <: HList] {
    def apply(hList: KvpHList[H]): H => String
  }

  object HListInterpreter {
    implicit val nil: HListInterpreter[HNil] = new HListInterpreter[HNil] {
      override def apply(hList: KvpHList[HNil]): HNil => String = _ => "Nil"
    }

    implicit def cons[A, T <: HList](implicit
                                     headKvpInterpreter: KvpInterpreter[A],
                                     tailHListInterpreter: HListInterpreter[T]
                                    ): HListInterpreter[A :: T] = new HListInterpreter[A :: T] {
      override def apply(hList: KvpHList[A :: T]): A :: T => String = hList match {
        case cons: KvpCons[_, _] => input => s"${headKvpInterpreter(cons.head)(input.head)} :: ${tailHListInterpreter(cons.tail)(input.tail)}"
      }
    }
  }

  def hListInterpreter[H <: HList](hList: KvpHList[H])(implicit hListInterp: HListInterpreter[H]): H => String = hListInterp(hList)

  trait KvpInterpreter[A] {
    def apply(kvpValue: KvpValue[A]): A => String
  }

  object KvpInterpreter {
    implicit val string: KvpInterpreter[String] = new KvpInterpreter[String] {
      override def apply(kvpValue: KvpValue[String]): String => String = str => str
    }

    implicit def hList[H <: HList : HListInterpreter]: KvpInterpreter[H] = new KvpInterpreter[H] {
      override def apply(kvpValue: KvpValue[H]): H => String = kvpValue match {
        case h: HListData[H] => input => {
          val toString: H => String = hListInterpreter(h.member)
          toString(input)
        }
      }
    }
  }

  def kvpInterpreter[A](kvpValue: KvpValue[A])(a: A)(implicit kvpInterp: KvpInterpreter[A]): String = kvpInterp(kvpValue)(a)
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • Can you explain what you mean by "Type-level pattern matching is type classes". – Travis Stevens May 29 '19 at 18:21
  • My `HListInterpreter` and `KvpInterpreter` are type classes. When you write `hList match { case KvpNil => ???; case KvpCons(h, t) => ???}` you match `hList` with patterns at runtime. When you write `implicit val nil: HListInterpreter[HNil] = ???; implicit def cons... : HListInterpreter[A :: T] = ???` you kind of match type `H` with types `HNil`, `A :: T` at compile time. When you did runtime pattern matching you lacked information about types (so you had to cast). – Dmytro Mitin May 29 '19 at 18:37
  • Why is it that I only lose type information with HList types? When matching on StringData, the compiler seemed to know that the A type was a String. – Travis Stevens May 29 '19 at 19:28
  • I guess because of type erasure in generics. – Dmytro Mitin May 29 '19 at 19:36