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

Random effect #465

Open
danieldjohnson opened this issue Jan 15, 2021 · 3 comments
Open

Random effect #465

danieldjohnson opened this issue Jan 15, 2021 · 3 comments
Labels
language / type system Type system extensions and bugs

Comments

@danieldjohnson
Copy link
Collaborator

Dex's random number generation is currently quite similar to JAX's. The implementation is a great fit for a functional language, but it's a bit annoying to have to manually split keys.

If Dex had a built-in Random effect, we could make things much more ergonomic while still compiling to the same implementation. Suppose we exposed

runRandom : Key -> (() -> {Random} a) -> a
getKey : () -> {Random} Key

Then, we could desugar this to an efficient functional RNG splitting process:

  • For a sequence of calls, either split the RNG sort of like a state effect, or use ixkey with the (lexical) position of this call in the block
  • Inside for loops, split the RNG using ixkey based on the index

Note that you can "duplicate" keys by doing getKey and then calling an inner runRandom twice with the same action. So I think this would be equally powerful as manually passing keys, but much more convenient.

@danieldjohnson
Copy link
Collaborator Author

Open question: are there other types of effect that act like this? In other words, would it be better to implement some sort of effect Splittable a and then provide an implementation of Splittable Key?

@srush
Copy link
Contributor

srush commented Jan 15, 2021

Relevant discussion as well #401

(Btw, that response mentions a Monadic implementation, but we don't have a Monad class in the prelude nor does the current type system support it. Might be a nice target of #460 ).

@dougalm
Copy link
Collaborator

dougalm commented Jan 15, 2021

Yeah, I really hope we can solve this with effects! As I mention in that comment on #401, my concern is with parallelism. Under a for, we want to use ixkey-based splitting instead of state, as you say. But that means that a for isn't equivalent to the unrolled version. But maybe that's fine?

Sasha, sorry for confusing things by bringing up monads. I wasn't suggesting we actually use them. I was just using that standard interface as a way to describe a possible PRNG API. Haskell's Monad type class gives you a clear set of functions to implement and laws those functions should satisfy when you want to implement a new instance. The problem with Dex's effects is that they're all compiler built-ins, and there isn't a clear set of steps for defining a new one. (On the plus side, that does give us a lot of freedom.)

@dan-zheng dan-zheng added the language / type system Type system extensions and bugs label Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language / type system Type system extensions and bugs
Projects
None yet
Development

No branches or pull requests

4 participants