Discussion:
[isabelle] ML - How to handle ERROR exception
Janik Benzin
2018-11-08 19:25:51 UTC
Permalink
Hi all,

I would like to learn a bit more about the ML basis of Isabelle.
Recently, I have tried to catch and handle the user exception "ERROR of
string" defined in exn.ML in the following example:

ML {*
type isar_record = {context: Proof.context, facts: thm list, goal:
thm}

fun goal_display ctxt (isar_goal:isar_record) =
Pretty.writeln(Goal_Display.pretty_goal ctxt (#goal(isar_goal)))
handle Exn.ERROR _ => ();
*}

Now I would like to insert the function using ML_val and antiquotations
anywhere in a theory file, i.e. inside and outside of proofs:

ML_val {*
goal_display @{context} @{Isar.goal}
*}

However, the exception ERROR "No proof state" is printed as an error
message in Isabelle, although I’ve installed the corresponding error
handler. Am I doing something wrong here?

Thank you for your time and help!

Best regards,
Janik Benzin
Makarius
2018-11-09 11:58:26 UTC
Permalink
Post by Janik Benzin
I would like to learn a bit more about the ML basis of Isabelle.
Recently, I have tried to catch and handle the user exception "ERROR of
The "implementation" manual contains a lot of explanations about
Isabelle/ML in chapter 0. Section 0.5 is about exceptions in particular:
these clear guidelines help to work with various kinds of errors robustly.
Post by Janik Benzin
ML {*
thm}
fun goal_display ctxt (isar_goal:isar_record) =
Pretty.writeln(Goal_Display.pretty_goal ctxt (#goal(isar_goal)))
handle Exn.ERROR _ => ();
*}
BTW, when you find yourself defining record-type aliases such as
isar_record above there is something wrong with your function
signatures. In the Isabelle/Pure implementation, types are defined in a
manner to minimize redundant type constraints: ML is essentially
implicitly typed LISP, and the Isabelle/ML IDE tells you what the
inferred types are -- you rarely write anything in the source.

So here is the example written in a more standard way:

ML ‹
fun goal_display ctxt goal =
Pretty.writeln (Goal_Display.pretty_goal ctxt goal)
handle ERROR _ => ();

Post by Janik Benzin
ML_val {*
*}
However, the exception ERROR "No proof state" is printed as an error
message in Isabelle, although I’ve installed the corresponding error
handler. Am I doing something wrong here?
The error is produced statically by the @{Isar.goal} antiquotation,
because the context of the ML_val command lacks a goal state.

Such confusions about the origin of exceptions belong to the nature of
exceptions: it requires care to write programs correctly in that respect
(section 0.5 of the "implementation" manual and the body of
Isabelle/Pure ML sources should provide some idea how to do it).


Makarius

Loading...