Zulip Chat Archive

Stream: new members

Topic: Adding warnings in Lean4


Michael Wahlberg (Apr 20 2023 at 17:42):

How does one add warnings in a lean code when we know the user has inputted a false value? For example, I want my program to exit when someone inputs an empty list in this function :-

def non_empty(l : List ) : Bool :=
  match l with
  | [] =>
  panic! "list is empty"
  | head :: tail => true

I read somewhere that panic works, but how exactly does it work?

Adam Topaz (Apr 20 2023 at 19:42):

panic! will just return the default value of the required type (so it requires an Inhabited instance for the return type, which Bool has).

Adam Topaz (Apr 20 2023 at 19:43):

But when you evaluate it at the empty case, it will give an error.

Mario Carneiro (Apr 20 2023 at 19:43):

You should not use panic! like this. It is allowed to be optimized away, so you cannot use it for anything except as a best effort way to catch implementation bugs

Mario Carneiro (Apr 20 2023 at 19:45):

Functions in lean are required to be total. If you want to report an error, use a return type like Except String Bool or IO Bool that allows you to report errors


Last updated: Dec 20 2023 at 11:08 UTC