Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

[Merged by Bors] - refactor(tactic/polyrith): use the autogenerated json parser #15429

Closed
wants to merge 4 commits into from

Conversation

eric-wieser
Copy link
Member

This acts as a nice demonstration of how to use @[derive non_null_json_serializable]


Open in Gitpod

@eric-wieser eric-wieser added awaiting-review The author would like community review of the PR awaiting-CI The author would like to see what CI has to say before doing more work. labels Jul 16, 2022
@eric-wieser eric-wieser requested a review from robertylewis July 16, 2022 16:58
@hrmacbeth hrmacbeth added the t-meta Tactics, attributes or user commands label Jul 16, 2022
@robertylewis
Copy link
Member

Thanks for this! This will conflict with #15425 and should be merged first. I'm closing Lean for a bit but will review soon -- it looks like a huge improvement!

@github-actions github-actions bot removed the awaiting-CI The author would like to see what CI has to say before doing more work. label Jul 17, 2022
Comment on lines 306 to 316
let e : exceptional (sage_json_success ⊕ sage_json_failure) :=
-- try the error format first, so that if both fail we get the message from the success parser
(sum.inr <$> of_json sage_json_failure j) <|> (sum.inl <$> of_json sage_json_success j),
match e with
| exceptional.exception f := exceptional.exception (λ s, format!"internal json error: " ++ f s)
| exceptional.success (sum.inr f) :=
fail!"polyrith failed to retrieve a solution from Sage! {f.error_name}: {f.error_value}"
| exceptional.success (sum.inl s) := do
do { some t ← pure s.trace | skip, tactic.trace t},
pure s.data
end
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let e : exceptional (sage_json_success ⊕ sage_json_failure) :=
-- try the error format first, so that if both fail we get the message from the success parser
(sum.inr <$> of_json sage_json_failure j) <|> (sum.inl <$> of_json sage_json_success j),
match e with
| exceptional.exception f := exceptional.exception (λ s, format!"internal json error: " ++ f s)
| exceptional.success (sum.inr f) :=
fail!"polyrith failed to retrieve a solution from Sage! {f.error_name}: {f.error_value}"
| exceptional.success (sum.inl s) := do
do { some t ← pure s.trace | skip, tactic.trace t},
pure s.data
end
(do f ← of_json sage_json_failure j,
fail!"polyrith failed to retrieve a solution from Sage! {f.error_name}: {f.error_value}") <|>
(do s ← decorate_ex "internal json error: " $ of_json sage_json_success j,
s.trace.mmap trace,
pure s.data)

I think this is a slicker version of the same thing!

Copy link
Member Author

Choose a reason for hiding this comment

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

That doesn't work correctly, because if an error message is recieved the failure is treated as a parsing failure, and then it tries to reparse as a success message. Still a helpful comment though, I didn't know about decorate_ex and didn't think of option.mmap!

/-- A schema for failure messages from the python script -/
@[derive [non_null_json_serializable, inhabited]]
structure sage_json_failure :=
(success : {b : bool // b = ff})
Copy link
Member

Choose a reason for hiding this comment

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

I trust this is right (also because it works), but what's the significance of this weird subtype? Is the idea that only json data with success: false should be parsed into this type? Is the non_null_json_serializable derive handler the part that's taking this into account?

Copy link
Member Author

@eric-wieser eric-wieser Jul 18, 2022

Choose a reason for hiding this comment

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

The subtype.non_null_json_serializable parser is the one that handles this; it parses the value, and uses decidability for the proof.

I could also have used

Suggested change
(success : {b : bool // b = ff})
(success : bool)
(success_eq_ff : success = ff)

and then the derive handler would have handled it; but then the inhabited derive handler (which I'm using only to appease the linter) wouldn't have worked.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

@robertylewis
Copy link
Member

bors merge

@github-actions github-actions bot added ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.) and removed awaiting-review The author would like community review of the PR labels Jul 18, 2022
bors bot pushed a commit that referenced this pull request Jul 18, 2022
This acts as a nice demonstration of how to use `@[derive non_null_json_serializable]`
@bors
Copy link

bors bot commented Jul 18, 2022

Build failed (retrying...):

bors bot pushed a commit that referenced this pull request Jul 18, 2022
This acts as a nice demonstration of how to use `@[derive non_null_json_serializable]`
@bors
Copy link

bors bot commented Jul 18, 2022

This PR was included in a batch that was canceled, it will be automatically retried

bors bot pushed a commit that referenced this pull request Jul 18, 2022
This acts as a nice demonstration of how to use `@[derive non_null_json_serializable]`
@bors
Copy link

bors bot commented Jul 19, 2022

Pull request successfully merged into master.

Build succeeded:

@bors bors bot changed the title refactor(tactic/polyrith): use the autogenerated json parser [Merged by Bors] - refactor(tactic/polyrith): use the autogenerated json parser Jul 19, 2022
@bors bors bot closed this Jul 19, 2022
@bors bors bot deleted the eric-wieser/polyrith-use-json branch July 19, 2022 01:01
joelriou pushed a commit that referenced this pull request Jul 23, 2022
This acts as a nice demonstration of how to use `@[derive non_null_json_serializable]`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.) t-meta Tactics, attributes or user commands
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants