Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Scala's new capture checking #7

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

lazy val commonSettings = Seq(
version := "0.1.0-SNAPSHOT",
scalaVersion := "3.3.1",
scalaVersion := "3.4.0-RC1-bin-20240105-d2cc3ae-NIGHTLY",
// javaOptions ++= Seq(
// "-XX:-DetectLocksInCompiledFrames",
// "-XX:+UnlockDiagnosticVMOptions",
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/monadic/examples/Capabilities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ object library {
/**
* Provide services to the computation, fully satisfying its requirements
*/
inline def provide(service: R): EFF[Any, E, A] = effectful { this.force()(using service) }
infix inline def provide(service: R): EFF[Any, E, A] = effectful { this.force()(using service) }

inline def catchAll[R1 <: R, E1, A1 >: A](handler: E => Eff[R1, E1, A1]): EFF[R1, E1, A1] = effectful {
try { this.force()(using use[R1], ()) }
Expand Down
91 changes: 91 additions & 0 deletions core/src/main/scala/monadic/examples/Capture.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package monadic
package examples
package capture

import language.experimental.captureChecking


object redesign extends App {

trait Effect {

/**
* The monadic type constructor
*/
type M[A]

/**
* The Monad instance for [[ M ]]
*/
def Monad: Monadic[M]

/**
* The interface of effect operations, supported by the monad
*/
type Operations

/**
* Implementation of the effect operations in terms of monadic reflection.
*/
def Operations: CanReflect[M] ?=> Operations

/**
* Runs a [[ program ]] that can make use of the effect [[ Operations ]]
* to compute a result of type [[ A ]] in monad [[ M ]].
*/
def apply[A](program: Operations^ ?=> A): M[A] =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the only position where we mark a capability as tracked, so far.

Monad.reify { cap ?=> program(using Operations) }

/**
* Makes the effect operations available on the effect instance
*/
implicit inline def api(self: this.type)(using impl: Operations^): Operations^{impl} = impl
}


trait State[S] {
def get(): S
def set(s: S): Unit
def update(fn: S => S): Unit
}

class StateMonad[S] extends Effect {

type M[A] = S => (S, A)

class Operations(using CanReflect[M]) extends State[S] {
def get() = Monad.reflect(s => (s, s))
def set(s: S) = Monad.reflect(_ => (s, ()))
def update(fn: S => S) = Monad.reflect(s => (fn(s), ()))
}

def Operations = new Operations

class Monad extends Monadic[M] {
def pure[A](a: A): M[A] = s => (s, a)
def sequence[X, R](init: M[X])(f: X => Either[M[X], M[R]]): M[R] =
@scala.annotation.tailrec
def go(prog: M[X], state: S): (S, R) =
val (newState, x) = prog(state)
f(x) match {
case Left(mx) => go(mx, newState)
case Right(res) => res(newState)
}
s => go(init, s)
}
// with capture checking enabled, this cannot be defined by an object
val Monad = new Monad
}

object Number extends StateMonad[Int]

val result = Number {
while (Number.get() > 0) {
Number.set(Number.get() - 1)
//if (Number.get() == 1) throw new Exception("Abort!")
}
}
try { println(result(1000)) } catch {
case e: Exception => e.printStackTrace()
}
}
6 changes: 3 additions & 3 deletions core/src/main/scala/monadic/syntax.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package monadic
package syntax

type in[A, M[_]] = CanReflect[M] ?=> A
infix type in[A, M[_]] = CanReflect[M] ?=> A

inline def reify[M[_]: Monadic]: ReifyBuilder[M] = ReifyBuilder()

case class ReifyBuilder[M[_]]()(using M: Monadic[M]) {
inline def in[R](prog: R in M) = M.reify[R] { prog }
infix inline def in[R](prog: R in M) = M.reify[R] { prog }
inline def apply[R](prog: R in M) = M.reify[R] { prog }
}

extension [M[_], R](mr: M[R])
inline def reflect(using r: CanReflect[M]): R = r.reflect(mr)
inline def reflect(using r: CanReflect[M]): R = r.reflect(mr)