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

ToyIO demonstration of a basic IO implementation for runtimes #1162

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

johnynek
Copy link
Owner

@johnynek johnynek commented Mar 3, 2024

The idea here is to encode general recursion into a data structure, then have the recursive loop be implemented in the runtime or host language.

/**
* fix(f) = f(fix(f))
*/
case class ApplyFix[E, A, B](arg: A, fixed: (A => ToyIO[E, B]) => (A => ToyIO[E, B])) extends ToyIO[E, B] {
Copy link
Owner Author

Choose a reason for hiding this comment

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

this constructor can't be written in Bosatsu because ToyIO[E, B] is in an invariant position (contravariant and covariant in the fixed function, and we can only have co-variant recursion in bosatsu. But we could define this constructor outside of bosatsu and return it via an external def.

def run[E, A](io: ToyIO[E, A]): Either[E, A] = {

@annotation.tailrec
def loop[E1, A1](arg: ToyIO[E1, A1], stack: Stack[E1, A1, E, A]): Either[E, A] =
Copy link
Owner Author

Choose a reason for hiding this comment

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

this loop is the only place with explicit recursion. This loop would be implemented in the runtime or host language using a loop to avoid blowing the stack.

@@ -183,8 +183,8 @@ lazy val core =
// periodically we use acyclic to ban cyclic dependencies and make compilation faster
,
autoCompilerPlugins := true,
addCompilerPlugin("com.lihaoyi" % "acyclic_2.13.12" % "0.3.11"),
scalacOptions += "-P:acyclic:force"
//addCompilerPlugin("com.lihaoyi" % "acyclic_2.13.12" % "0.3.11"),
Copy link
Owner Author

Choose a reason for hiding this comment

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

Commented this because it blows up the repl

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 87.50000% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 91.41%. Comparing base (04a0e87) to head (d9f2901).
Report is 4 commits behind head on main.

Files Patch % Lines
core/src/main/scala/org/bykn/bosatsu/ToyIO.scala 87.50% 3 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1162      +/-   ##
==========================================
+ Coverage   91.32%   91.41%   +0.08%     
==========================================
  Files          96       97       +1     
  Lines       11846    11898      +52     
  Branches     2675     2727      +52     
==========================================
+ Hits        10818    10876      +58     
+ Misses       1028     1022       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants