When using HList with GADTs I am having to cast using asInstanceOf[H]. Is there a way to avoid the cast?

孤者浪人 提交于 2021-01-27 12:24:51

问题


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)

回答1:


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)


来源:https://stackoverflow.com/questions/56362440/when-using-hlist-with-gadts-i-am-having-to-cast-using-asinstanceofh-is-there

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!