问题
When I use value
to find out some value of a function that returns natural numbers, I always obtain the answer in the form of iterated Successor functions of 0, i.e., Suc(Suc( ... 0 ))
which can be hard to read sometimes.
Is there a way to output directly the number that Isabelle returns?
回答1:
This is something I wanted to fix some time ago but apparently I forgot. Carcigenate's guess is incorrect; this is indeed the fully evaluated result. The problem is just that natural numbers are printed in this unwieldy way.
You can do the following things:
The easiest way is to convert the number to an integer, i.e. instead of
value "foo x y z"
(wherefoo x y z
is the expression of typenat
that you want to eavluate) you writevalue "int (foo x y z)"
.You can add
~~/src/HOL/Library/Code_Target_Numeral
to your imports. This makes Isabelle's code generator use arbitrary-precision integers of the target language (in case ofvalue
, that's ML and its GMP-based integers) instead of the inefficient successor notation. As a side effect, this also prints natural numbers in a nicer way.You can add the following code to your theory, which changes the way the
value
command displays natural numbers:
Code:
lemma Suc_numeral_bit0: "Suc (numeral (Num.Bit0 n)) = numeral (Num.Bit1 n)"
by (subst Suc_numeral) simp
lemma Suc_numeral_bit1: "Suc (numeral (Num.Bit1 n)) = numeral (Num.Bit0 (n + Num.One))"
by (subst Suc_numeral) simp
lemmas [code_post] =
One_nat_def [symmetric] Suc_numeral_bit0 Suc_numeral_bit1 Num.Suc_1 Num.arith_simps
Note that the value
command is a diagnostic command only. It is mainly used for quick plausibility tests and debugging of code generation setups, and getting it to work can sometimes be tricky.
By default, value
relies on the code generator, i.e. Isabelle needs to know how to generate executable code for the expression, and if it cannot do that, value
will probably fail. (It can sometimes also fall back to some other strategies, normalisation by evaluation or evaluation by simplification, but those usually don't provide useful output)
I'm just telling you this so that you know what to expect from the value
command and don't get the impression that this is some integral part of Isabelle that people use all the time.
来源:https://stackoverflow.com/questions/42317474/isabelle-return-numbers-instead-of-sucsuc-0