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

CN: Can sandbox be replaced with pure? #752

Open
dc-mak opened this issue Dec 10, 2024 · 2 comments
Open

CN: Can sandbox be replaced with pure? #752

dc-mak opened this issue Dec 10, 2024 · 2 comments
Labels
cn technical debt Something for internal cleanup

Comments

@dc-mak
Copy link
Contributor

dc-mak commented Dec 10, 2024

let pure (m : 'a t) : 'a t =
fun s ->
Solver.push (Option.get s.solver);
let outcome = match m s with Ok (a, _) -> Ok (a, s) | Error e -> Error e in
Solver.pop (Option.get s.solver) 1;
outcome
let sandbox (m : 'a t) : 'a Resultat.t t =
fun s ->
let n = Solver.num_scopes (Option.get s.solver) in
Solver.push (Option.get s.solver);
let outcome =
match m s with
| Ok (a, _s') ->
assert (Solver.num_scopes (Option.get s.solver) = n + 1);
Solver.pop (Option.get s.solver) 1;
Ok a
| Error e ->
let n' = Solver.num_scopes (Option.get s.solver) in
assert (n' > n);
Solver.pop (Option.get s.solver) (n' - n);
Error e
in
Ok (outcome, s)

I feel like this could be

let sandbox (m : 'a t) : 'a Resultat.t t =
  fun s ->
  Ok (Result.map fst (pure m s), s)

But I am not sure.

@dc-mak dc-mak added cn technical debt Something for internal cleanup labels Dec 10, 2024
@samcowger
Copy link
Collaborator

I am also not sure - I didn't write either of those functions, and I don't have a great understanding of solver scopes, so I don't think I'm the best person to address this. Perhaps @cp526 or @yav could weigh in?

For what it's worth, looking at them now, these two functions do seem to me to make different assumptions about how many new solver scopes an erroring action might introduce. pure seems to think that number is always 1, while sandbox seems to think that it could be 1 or more. I don't think both of these assumptions can hold.

@dc-mak
Copy link
Contributor Author

dc-mak commented Dec 10, 2024

I think that makes sense actually - I think a reasonable guess, and it might come down to intent: that pure is only for actions which are expected to succeed (not allowed to run anything afterwards) but sandbox is for actions which may succeed or fail.

In which case, I think it is worth strengthening the definition of pure to still leave the the typing monad/solver usable after a failed action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cn technical debt Something for internal cleanup
Projects
None yet
Development

No branches or pull requests

2 participants