2

我仍在弄清楚这里涉及的确切打字规则/含义。

如果示例中的类型“足够简单”以“适合”,就像几乎所有简单示例中的情况一样,这似乎更容易/更容易,但是在将事物与打字进行比较时变得更加有趣/困难(至少对我而言)由 tiark rompf 给出:

|- e: A@cpsParam[B,C]; {[|r|]}: U
-----------------------------------------------------
[|val x: A = e; r|] = [|e|].map( (x: A) => {[|r|]} )

所以结果[|e|].map( (x: A) => {[|r|]} )将具有Shift[U,B,C]根据tiark论文中给出的map定义的类型。

这里 U 不一定与 B 相同。

到目前为止,我不明白为什么 U 被允许与 B 不同,而没有像 U <: B 在 tiark 的论文中的 map 定义中给出的东西。

我在这里分别错过了什么?

任何提示/想法?

4

1 回答 1

1

I had a second look at this as wanted to see what the result of the selective cps transform will yield in both cases.

  1. U <: B
  2. U is not a subtype of B

I used the following simple example:

package sample

import scala.util.continuations._

class Depp {
  override def toString = "DEPP"
}

class Sepp extends Depp {
  override def toString = "DEPP->SEPP"
}

object Sample extends Application {
  val depp = new Depp
  val sepp = new Sepp
  val res = reset {
    shift {
      (k: Int => Depp) => k(7)
    }
    val z = sepp
    z
  }
  println("Result = "+ res)
}

Compiling this using

scalac -P:continuations:enable -Xprint:selectivecps Sample.scala

proves successful and yields the following (interesting part only):

private[this] val res: sample.Depp = scala.util.continuations.package.reset[sample.Sepp, sample.Depp]({
   package.this.shiftR[Int, sample.Depp, sample.Depp](((k: (Int) => sample.Depp) => k.apply(7))).map[sample.Sepp]
     tmp1;
     val z: sample.Sepp = Sample.this.sepp;
     z
   }))

ok so the type of the resulting (application of map) Shift object is [Sepp,Depp,Depp] as expected :)

which is fine because i understand how Shift objects like A@cpsParam[A,C] come into existence (the reset function given in Tiark's paper operates on such Shift objects)

Now changing the following in the simple example to yield a type unrelated to Depp: z.asInstanceOf[Float]

compiling this with

scalac -P:continuations:enable -Xprint:selectivecps -explaintypes Sample.scala

brings up the following error which tells what is actually checked:

Sample.scala:16: error: type mismatch;
 found   : Float @scala.util.continuations.cpsParam[sample.Depp,sample.Depp] @scala.util.continuations.cpsSynth
 required: Float @scala.util.continuations.cpsParam[Float,sample.Depp]
  val res = reset {
                  ^
Float @scala.util.continuations.cpsParam[sample.Depp,sample.Depp] @scala.util.continuations.cpsSynth <: Float @scala.util.continuations.cpsParam[Float,sample.Depp]?
  scala.util.continuations.cpsParam[sample.Depp,sample.Depp] <: scala.util.continuations.cpsParam[Float,sample.Depp]?
    Float <: sample.Depp?
      <notype> <: sample.Depp?
      false
    false
  false
false
one error found

ahh and here is the test: Float <: sample.Depp? so it fails because Float of course isn't a subtype of Depp

question: shouldn't the transformation rule then better be given as:

e: A@cpsParam[B,C]  {[|r|]}: U  U <: B
-----------------------------------------------------
[|val x: A = e; r|] = [|e|].map( (x: A) => {[|r|]} )

to clearly express this?

于 2010-11-23T19:08:48.927 回答