我正在学习 Scala 中的类型级编程,我很好奇是否可以使用类型级编程来表示树或层次结构。
简单的情况是多级树
A_
|
B_
|C
|D
|
E
如何表示这样的结构?
最佳答案
在 Scala 中有很多方法可以表示异构树,最简单的方法之一是这样的:
type MyTreeShape[A, B, C, D, E] = (A, (B, (C, D), E))
但是,这有一些限制(例如,您不能将元组作为叶子的值,因为我们在表示中使用了元组性)。对于这个答案的其余部分,我将使用一个稍微复杂的表示,涉及 Shapeless的
HList
:import shapeless._
type MyTreeShape[A, B, C, D, E] =
A ::
(B ::
(C :: HNil) ::
(D :: HNil) ::
HNil) ::
(E :: HNil) ::
HNil
这里的一棵树是
HList
其头部是值,尾部是 HList
的 child 树。如果我们想对这些种类的树做任何有用的通用操作,我们将需要一些类型类。我会写一个快速的深度优先
FlattenTree
关于 Shapeless 中的类型类的模型 ops.hlist
包为例。可以类似地实现用于大小、深度等的其他类型类。这是类型类和一个方便使用的方法:
trait FlattenTree[T <: HList] extends DepFn1[T] { type Out <: HList }
def flattenTree[T <: HList](t: T)(implicit f: FlattenTree[T]): f.Out = f(t)
现在举个例子,我们将把它放在伴随对象中:
object FlattenTree {
type Aux[T <: HList, Out0 <: HList] = FlattenTree[T] { type Out = Out0 }
implicit def flattenTree[H, T <: HList](implicit
tf: FlattenForest[T]
): Aux[H :: T, H :: tf.Out] = new FlattenTree[H :: T] {
type Out = H :: tf.Out
def apply(t: H :: T): H :: tf.Out = t.head :: tf(t.tail)
}
}
请注意,这需要一个辅助类型类,
FlattenForest
:trait FlattenForest[F <: HList] extends DepFn1[F] { type Out <: HList }
object FlattenForest {
type Aux[F <: HList, Out0 <: HList] = FlattenForest[F] { type Out = Out0 }
implicit val hnilFlattenForest: Aux[HNil, HNil] = new FlattenForest[HNil] {
type Out = HNil
def apply(f: HNil): HNil = HNil
}
implicit def hconsFlattenForest[
H <: HList,
OutH <: HList,
T <: HList,
OutT <: HList
](implicit
hf: FlattenTree.Aux[H, OutH],
tf: Aux[T, OutT],
pp: ops.hlist.Prepend[OutH, OutT]
): Aux[H :: T, pp.Out] = new FlattenForest[H :: T] {
type Out = pp.Out
def apply(f: H :: T): pp.Out = pp(hf(f.head), tf(f.tail))
}
}
现在我们可以这样使用它:
val myTree: MyTreeShape[String, Int, Char, Symbol, Double] =
"foo" :: (10 :: HList('a') :: HList('z) :: HNil) :: HList(0.0) :: HNil
val flattened = flattenTree(myTree)
让我们展示一下静态类型是否合适:
flattened: String :: Int :: Char :: Symbol :: Double :: HNil
而这正是我们想要的。
你可以在没有 Shapeless 的情况下完成所有这些,但它会涉及大量的样板。
关于Scala 类型级编程 - 表示层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28609203/