Zulip Chat Archive
Stream: lean4
Topic: pattern for multiple guards before error?
Thomas Murrills (Feb 07 2023 at 06:41):
In MetaM
(well, TacticM
) I want to write something like
try
guard ... -- easy check
guard ... -- harder check
guard ... -- hardest check
throwError "all checks passed :("
catch _ =>
foo
but, of course, this doesn't work the way I naively expect it to, because throwError
is an error and thus gets caught by the catch
.
I can use a .internal
exception, but that seems a bit hacky for what should be a "nice" procedure: try a bunch of things in order; if they fail, do one thing, if they succeed, do another. (A little worried about simply using &&
in if
because I'm not sure it short-circuits at the elaboration stage.)
Any advice to write this cleanly?
Mario Carneiro (Feb 07 2023 at 06:49):
I use the try?
function for this
Mario Carneiro (Feb 07 2023 at 06:51):
it internally catches the exception and presents the result as an Option
Mario Carneiro (Feb 07 2023 at 06:51):
also, &&
does short-circuit
Thomas Murrills (Feb 07 2023 at 07:09):
oh, cool. this is the function in Mathlib.Control.Basic
? so here the pattern would be something like...
let .none := try?
guard ...
guard ...
guard ...
| throwError "checks passed :("
foo
up to syntax?
Thomas Murrills (Feb 07 2023 at 07:10):
(unfortunately I think I'm upstream of that file unless I reorganize, which, wouldn't be the worst thing)
Thomas Murrills (Feb 07 2023 at 07:30):
(oh, I think I can use return
in my particular case, I think, since I do in fact want to foo; return ()
)
Last updated: Dec 20 2023 at 11:08 UTC