effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Basic streaming, request for comments

Open phischu opened this issue 1 year ago • 8 comments

phischu avatar Jul 26 '24 13:07 phischu

It would be really nice to have a couple of examples showcasing the various features so that I can get an idea of how to use it :)

jiribenes avatar Jul 26 '24 15:07 jiribenes

By the way, here's Unison's Stream effect and Each effect (lazy nondeterminism from a stream)

Each.toList do
  use Nat <
  a = Each.range 0 5
  b = each [1, 2, 3]
  guard (a < b)
  (a, b)

jiribenes avatar Jul 26 '24 15:07 jiribenes

Can we maybe translate some Unison examples to Effekt to compare? That would be nice. @phischu could you please look into the Unison implementation as well?

b-studios avatar Jul 26 '24 15:07 b-studios

If you comment out the broken functions you can actually write an example which might look like this:

  val (_, l) = gatherList[Int, Unit] {
    for[Int, Unit] { range(0, 5) } { i =>
      for[Int, Unit] { range(1, 3) } { j =>
        do emit(i + j)
      }
    }
  }

  println(l)

Please note the explicit type arguments which are necessary because I need to say WHICH stream I want to handle.

This prints:

Cons(1, Cons(2, Cons(2, Cons(3, Cons(3, Cons(4, Cons(4, Cons(5, Cons(5, Cons(6, Nil()))))))))))

As you might guess, I really do not like having to annotate the types... I also don't like binding and ignoring unit in this example. It is very easy to get this example to something like:

val l = List[Int].collect {
  with i = Stream[Int].for { range(0, 5) }
  with j = Stream[Int].for { range(1, 3) }
  do emit(i + j)
}
println(l)

using a bit of phantom magic.

b-studios avatar Jul 26 '24 16:07 b-studios

Please note the explicit type arguments which are necessary because I need to say WHICH stream I want to handle.

How about

on[Int].for { ...

Or we try to fix this at the language level.

phischu avatar Jul 26 '24 17:07 phischu

(lazy nondeterminism from a stream)

This is planned in Phase 3

phischu avatar Jul 26 '24 17:07 phischu

could you please look into the Unison implementation as well?

I am looking at the streaming library, which is great.

phischu avatar Jul 26 '24 17:07 phischu

on the language level probably means fixing type inference which is planned but will take a while

b-studios avatar Jul 26 '24 18:07 b-studios

@phischu I took the liberty to add some doc comments.

b-studios avatar Nov 08 '24 17:11 b-studios

Interesting, the tests pass on my machine but apparently they now leak memory...

b-studios avatar Nov 08 '24 17:11 b-studios

To avoid having to rebase over and over again, I am in favor of just merging this and revising later. It is in a state that is already useful and we can continue improving it.

@phischu I'll give you the approve and if you deem it ready, just merge.

b-studios avatar Nov 08 '24 17:11 b-studios

Before merging, please rename the PR (just so that the autogenerated Changelog for the next release is a little more understandable 😇)

jiribenes avatar Nov 08 '24 17:11 jiribenes