-
Notifications
You must be signed in to change notification settings - Fork 7
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
b-studios
wants to merge
1
commit into
main
Choose a base branch
from
experiment/capture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = | ||
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.