[英]Scala equivalent of 'forall a. Set a -> Set a -> Set a'
[英]Translate/encode Haskell's `data Obj = forall a. (Show a) => Obj a` in Scala
我无法提出如何在Scala中编码Obj
的方法:
{-# LANGUAGE ExistentialQuantification #-}
data Obj = forall a. (Show a) => Obj a
instance Show Obj where show (Obj a) = "Obj " ++ show a
main = print $ show [Obj "hello", Obj 3, Obj True]
运行时,上面的代码会产生以下输出:
[Obj "hello",Obj 3,Obj True]
但是,在Scala中,这似乎无法编译:
forSome { type T; implicit val ev: Show[T] }
而且也不是:
forSome { type T : Show[T] }
这在类型系统级别甚至可能吗,还是我需要使用类似以下方式“捕获”类型类实例:
class Obj[T](val x: T)(implicit val: Show[T]) // ...or similar
任何见识将不胜感激!
您几乎完全正确:
import scalaz._
import scalaz.Scalaz._
trait Obj {
type T // existential type
val x: T
implicit val show: Show[T]
}
implicit val objSow: Show[Obj] = Show.shows[Obj] { (x: Obj) =>
x.show.shows(x.x)
}
object Obj {
/* "constructor" */
def apply[U](_x: U)(implicit _show: Show[U]): Obj = new Obj {
type T = U
val x = _x
val show = _show
}
}
val test: List[Obj] = List(Obj(1), Obj(true), Obj("foo"))
/*
scala> test.shows
res0: String = [1,true,"foo"]
*/
PS我想用T
并在apply
show
; 不是U
和_show
。 如果有人知道如何避免阴影,我将不胜感激!
或者,您可以使用forSome
:
import scala.language.existentials
trait ObjE {
val pair: Tuple2[T, Show[T]] forSome { type T }
}
/* And to define Show instance we have to help compiler unify `T` in pair components. */
def showDepPair[T] = Show.shows[Tuple2[T, Show[T]]] { x => x._2.shows(x._1) }
implicit val showObjE = Show.shows[ObjE] { x => showDepPair.shows(x.pair) }
在这里,我们必须使用Tuple2
(或其他辅助类型)来捕获Show
。 我更喜欢以前的变体。 对我来说,将类型的成员缠起来更容易。
同样在Scala中,“ Don Giovanni” forSome
语法将被取消,而使用val pair: ({ type λ[T] = Tuple2[T, Show[T]] })#λ[_] }
也已经起作用。 我希望还将对lambda类型提供一些语法支持。 kind-projector在这种情况下无济于事(重复使用T
)。 也许类似于Typelevel scalac
: val pair: ([T] => Tuple2[T, Show[T])[_])
。
另一个基本变化将是:
一个单一的基本概念(类型成员)可以为泛型,存在性类型,通配符和种类较多的类型赋予精确的含义。
因此,从编译器的角度来看,这两种形式都是等效的(以前,我们对元组进行解压缩)。 我不确定是否存在100%的当前差异 。
PS 带类型的麻烦帮助我了解了scala当前的类型系统怪癖。
我已经将Oleg的答案“打包”到了这个通用的(貌似)可重用的结构中:
import scala.language.{ higherKinds, implicitConversions }
trait AnyWithTC[TC[_]] { type T; val x: T; implicit val ev: TC[T] }
// don't like the 'implicit' here; suggestions welcome
implicit def AnyWithTC[T, TC[_]](x: T)(implicit ev: TC[T]) = {
type T0 = T; val x0 = x; val ev0 = ev
new AnyWithTC[TC] { type T = T0; val x = x0; val ev = ev0 }
}
那么, data Obj = forall a. (Show a) => Obj a
data Obj = forall a. (Show a) => Obj a
可以这样实现:
type Obj = AnyWithTC[Show]
implicit val objShow = Show.shows[Obj] { x => "Obj " + x.show.shows(x.x) }
val xs: List[Obj] = List(1, true, "hello")
println(xs.shows) // prints [Obj 1,Obj true, Obj hello]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.