summaryrefslogtreecommitdiff
path: root/test/files/pos/depmet_implicit_oopsla_session_2.scala
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@epfl.ch>2010-09-16 22:26:24 +0000
committerAdriaan Moors <adriaan.moors@epfl.ch>2010-09-16 22:26:24 +0000
commite557acb9a7d672c0635c3eaf9fe385adc41e5c86 (patch)
treed13db6639464acc57f0e44b4b3ef6f3e607ad403 /test/files/pos/depmet_implicit_oopsla_session_2.scala
parentce223fe7abc47af712382a64404604e75f9f4d20 (diff)
downloadscala-e557acb9a7d672c0635c3eaf9fe385adc41e5c86.tar.gz
scala-e557acb9a7d672c0635c3eaf9fe385adc41e5c86.tar.bz2
scala-e557acb9a7d672c0635c3eaf9fe385adc41e5c86.zip
part 2 of the dependent method refactoring: imp...
part 2 of the dependent method refactoring: improved interaction with implicit search (needed for oopsla paper) more to come in this area, see e.g. #3346 (stanford edsl stuff) reopens #13, which wasn't fixed properly before imo, anyway (have a look at -Xprint:typer output before this commit: a type that's not expressible in surface syntax is inferred -- also removed duplicate test file) closes #3731: co-evolve type alias type symbols when their rhs is updated and they are referenced by type selections (see typemap) review by odersky
Diffstat (limited to 'test/files/pos/depmet_implicit_oopsla_session_2.scala')
-rw-r--r--test/files/pos/depmet_implicit_oopsla_session_2.scala87
1 files changed, 87 insertions, 0 deletions
diff --git a/test/files/pos/depmet_implicit_oopsla_session_2.scala b/test/files/pos/depmet_implicit_oopsla_session_2.scala
new file mode 100644
index 0000000000..5c3b78e3f5
--- /dev/null
+++ b/test/files/pos/depmet_implicit_oopsla_session_2.scala
@@ -0,0 +1,87 @@
+object Sessions {
+ def ?[T <: AnyRef](implicit w: T): w.type = w
+
+ // session states
+ sealed case class Stop()
+ sealed case class In[-Data, +Cont](recv: Data => Cont)
+ sealed case class Out[+Data, +Cont](data: Data, cont: Cont)
+
+ // the type theory of communicating sessions:
+
+ // an instance of type Session[S]{type Dual=D} is evidence that S and D are duals
+ // such a value witnesses this fact by describing how to compose an instance of S with an instance of D (through the run method)
+ trait Session[S] { type Self = S
+ type Dual
+ type HasDual[D] = Session[Self]{type Dual=D}
+ def run(self: Self, dual: Dual): Unit
+ }
+
+ // friendly interface to the theory
+ def runSession[S, D: Session[S]#HasDual](session: S, dual: D) =
+ ?[Session[S]#HasDual[D]].run(session, dual)
+
+ // facts in the theory:
+
+ // ------------------------[StopDual]
+ // Stop is the dual of Stop
+ implicit object StopDual extends Session[Stop] {
+ type Dual = Stop
+
+ def run(self: Self, dual: Dual): Unit = {}
+ }
+
+ // CD is the dual of Cont
+ // -------------------------------------------[InDual]
+ // Out[Data, CD] is the dual of In[Data, Cont]
+ implicit def InDual[Data, Cont](implicit cont: Session[Cont]) = new Session[In[Data, Cont]] {
+ type Dual = Out[Data, cont.Dual]
+
+ def run(self: Self, dual: Dual): Unit =
+ cont.run(self.recv(dual.data), dual.cont)
+ }
+
+ // CD is the dual of Cont
+ // -------------------------------------------[OutDual]
+ // In[Data, CD] is the dual of Out[Data, Cont]
+ implicit def OutDual[Data, Cont](implicit cont: Session[Cont]) = new Session[Out[Data, Cont]] {
+ type Dual = In[Data, cont.Dual]
+
+ def run(self: Self, dual: Dual): Unit =
+ cont.run(self.cont, dual.recv(self.data))
+ }
+
+ // a concrete session
+ def addServer =
+ In{x: Int =>
+ In{y: Int => System.out.println("Thinking")
+ Out(x+y,
+ Stop())}}
+
+ def addClient =
+ Out(3,
+ Out(4, { System.out.println("Waiting")
+ In{z: Int => System.out.println(z)
+ Stop()}}))
+
+ def myRun = runSession(addServer, addClient)
+}
+
+/* future improvements:
+
+
+ // def runSession[S, D](p: S, dp: D)(implicit s: Session[S]#HasDual[D]) =
+ // s.run(p, dp)
+ //
+ // def runSession[S, D](p: S, dp: D)(implicit s: Session[S]{type Dual=D}) =
+ // s.run(p, dp)
+
+ // TODO: can we relax the ordering restrictions on dependencies so that we can write
+ // one possibility: graph of dependencies between arguments must be acyclic
+ // def runSession[S](p: S, dp: s.Dual)(implicit s: Session[S]) =
+ // s.run(p, dp)
+ // to emphasise similarity of type parameters and implicit arguments:
+ // def runSession[S][val s: Session[S]](p: S, dp: s.Dual) =
+ // s.run(p, dp)
+
+
+*/ \ No newline at end of file