2

プログラミング言語における型とポリモーフィズムに関する論文を読んで、Scala を使用して型メンバーの同様の全称量化を表現できるのではないかと思いました。紙の例:

type GenericID = ∀A.A ↦ A

これは一般的な同一性関数の型であり、紙の言語Funでの次の例は正しかった:

value inst = fun(f: ∀a.a ↦ a) (f[Int], f[Bool])
value intId = fst(inst(id))   // return a function Int ↦ Int

Scalaで同様のことを表現する方法はありますか?

これは type constructor と同じではありません。これは、がジェネリック関数の型であるtype GenericId[A] = A => A場合の型操作であるためです。∀A.A ↦ A

4

2 に答える 2

2

上記の私のコメントに続いて:

scala> type Gen[+_] = _ => _
defined type alias Gen

scala> def f(x: List[Int]): Gen[List[Int]] = x map (y => s"{$y!$y}")
f: (x: List[Int])Gen[List[Int]]

scala> f(List(1, 4, 9))
res0: Function1[_, Any] = List({1!1}, {4!4}, {9!9})

つまり、型の同一性は によって保持されていませんGen[+_] = _ => _

補遺

scala> type Identity[A] = A => A
defined type alias Identity

scala> def f(x: List[Int]): Identity[List[Int]] = x => x.reverse
f: (x: List[Int])List[Int] => List[Int]

scala> f(List(1, 4, 9))
res1: List[Int] => List[Int] = <function1>

scala> def g(x: List[Int]): Identity[List[Int]] = x => x map (y => s"{$y!$y}")
<console>:35: error: type mismatch;
 found   : List[String]
 required: List[Int]
       def g(x: List[Int]): Identity[List[Int]] = x => x map (y => s"{$y!$y}")
于 2013-10-16T20:57:56.957 に答える
0

Try: type Gen[+_] = _ => _

scala> def f(x:List[Int]):Gen[List[Int]] = x.reverse
f: (x: List[Int])Gen[List[Int]]

scala> f(List(3,4))
res0: Function1[_, Any] = List(4, 3)

scala> def f(x:List[Number]):Gen[List[Number]] = x.reverse
f: (x: List[Number])Gen[List[Number]]

scala> f(List(3,4))
res1: Function1[_, Any] = List(4, 3)
于 2013-10-16T10:05:20.357 に答える