如何在Scala中使用半环实现的运行时工厂对象



我正在尝试在Scala中为代数半环建模。我遇到了类型转换问题,这些问题使我看起来不可能在Scala中做我想做的事情。我希望有人能告诉我Scala打字系统中我误解的方面。

(请耐心等待这个问题的漫长安排。我已经尽可能地精简了它。)

半环是一组项,在这些项上定义了二进制加法(+)和乘法(*)运算符及其单位元素,分别称为零和一。例如,整数半环是在整数上定义的,其中+和*是算术的标准运算,0和1是整数0和1。一个更奇特的例子是布尔半环,它是在True和False值上定义的,其中+是逻辑OR,*是逻辑and,0是False,1是True。

为了对此进行建模,我定义了一个特性来指定适当的二进制运算符。

trait SemiringElement {
/**
* The element type
*/
type E
/**
* The type returned by the the addition and multiplication operators
*/
type R <: SemiringElement
val value: E
def +(that: R): R
def *(that: R): R
override def toString = value.toString
}

Case类实例化特定半环的元素。例如,布尔半环看起来是这样的。

case class BooleanSemiringElement(init: Boolean) extends SemiringElement {
type E = Boolean
type R = BooleanSemiringElement
val value = init
def +(that: BooleanSemiringElement#R) = BooleanSemiringElement(value || that.value)
def *(that: BooleanSemiringElement#R) = BooleanSemiringElement(value && that.value)
}

此外,我还有一个半环特征,它指定零和一个元素。

trait Semiring {
type E <: SemiringElement
/**
* The addition identity
*/
val zero: E
/**
* The multiplication identity
*/
val one: E
}

特定的半环对象返回适当类型的零和一元素。

object BooleanSemiring extends Semiring {
type E = BooleanSemiringElement
val zero = BooleanSemiringElement(false)
val one = BooleanSemiringElement(true)
}

Semiring对象本质上是知道如何返回适当类型的标识元素的工厂singleton。

我希望能够编写一般使用半环元素的算法。我使用半环工厂对象是为了能够在运行时而不是编译时指定特定的半环。例如,假设我有一个对象,它维护字符串和半环元素之间的映射。

class ElementMap(s: Semiring) {
val m = mutable.Map[String, SemiringElement]()
}

如果我用这样的调用实例化它:

val x = new ElementMap(BooleanSemiring)

我希望x.m是一个String->BooleanSemiringElement映射。问题是我所拥有的实际上是一个String->SemiringElement映射。

scala> val x = new ElementMap(BooleanSemiring)
x: ElementMap = ElementMap@46cf97b
scala> x.m
res2: scala.collection.mutable.Map[String,SemiringElement] = Map()
scala> x.m("one") = BooleanSemiring.one
scala> x.m("one") + BooleanSemiring.one
<console>:12: error: type mismatch;
found   : BooleanSemiring.one.type (with underlying type BooleanSemiring.BooleanSemiringElement)
required: _1.R where val _1: SemiringElement
x.m("one") + BooleanSemiring.one
^

如果我愿意在编译时而不是运行时指定类型,我可以将元素类型设置为泛型,如下所示:

class ElementMap[BooleanSemiring]...

但是我需要一个工厂方法来创建所有不同类型的ElementMap对象。将工厂的智能融入半成品的特质在建筑上更有意义。我想说的是:

class ElementMap(s: Semiring) {
val m = mutable.Map[String, s.E]()
}

即:创建一个从字符串到元素类型E的映射,该映射由提供给构造函数的Semiring对象返回。我不知道该怎么做。我尝试过各种语法技巧和隐含转换,但都无济于事。

有没有一种方法可以在运行时编写一个使用半环构造函数参数配置的ElementMap,或者我采取了错误的方法?我是Scala的新手,尝试以Scala风格做事。我觉得我把自己描绘成了一个角落,但我不确定失误到底在哪里。

您是否尝试捕获特定类型的s作为类型参数?

scala> class ElementMap[S <: Semiring](s: S) {
|   val m = collection.mutable.Map[String, S#E]()
| }
defined class ElementMap
scala> val x = new ElementMap(BooleanSemiring)
x: ElementMap[BooleanSemiring.type] = ElementMap@6544c984
scala> x.m("one") = BooleanSemiring.one
scala> x.m("one") + BooleanSemiring.one
res0: BooleanSemiringElement = true

您可以对元素类型做类似的操作:

scala> class ElementMap[SE <: SemiringElement](s: Semiring { type E = SE }) {
|   val m = collection.mutable.Map[String, SE]()
| }
defined class ElementMap
scala> val x = new ElementMap(BooleanSemiring)
x: ElementMap[BooleanSemiringElement] = ElementMap@4cf353e5
scala> x.m("one") = BooleanSemiring.one
scala> x.m("one") + BooleanSemiring.one
res1: BooleanSemiringElement = true

您的版本的问题在于,您丢弃了关于s的所有类型信息——从ElementMap的角度来看,它只是一个Semiring

(顺便说一句,通过类型类的ad-hoc多态性可能是解决Scala中这个问题的一种更自然的方法。例如,请参阅Scalaz7表示类似代数结构的方式。)

最新更新