RFC: better UX around `throw`?
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 9.2k
- Forks
- 990
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 175
Description
Proposal
Currently, throw "error" : IO A does not typecheck. I would like it to typecheck.
Reasoning:
- As a seasoned Lean programmer it is still hard for me to remember
.userErrorwhen trying to throw inIO. I suspect this is deeply unfriendly to beginners. - There is a canonical coercion from
StringtoIO.Error, via.userError. - User code in
IOalmost always wants to throw via this canonical coercion, rather than the more internal-to-IOerrors.
I have no ideas for how to do this without some major changes to MonadExcept. Suggestions appreciated.
One (bad) option would be to add a MonadExceptOf instance:
instance (priority := low) : MonadExceptOf String IO where
throw e := throw (.userError e)
tryCatch x f := tryCatch x (fun
| .userError e => f e
| e => throw e)
#check show IO Unit from throwThe String "hi"
#check show IO Unit from throw (.userError "hi")
but this requires throwThe String rather than throw, which I find no better than the status quo.
Community Feedback
Small bit of discussion here: https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Map.20of.20Lean's.20monads
Impact
Add 👍 to issues you consider important. If others benefit from the changes in this proposal being added, please ask them to add 👍 to it.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading MonadExcept, IO.Error, and the existing String-to-IO.Error coercion described in the proposal, then review the linked Zulip discussion. The design should make throw "error" : IO A typecheck while preserving behavior for other IO errors and avoiding a requirement to use throwThe String.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100