Isabelle return numbers instead of Suc(Suc( … 0 ))

自古美人都是妖i 提交于 2019-12-12 20:30:11

问题


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:

  1. The easiest way is to convert the number to an integer, i.e. instead of value "foo x y z" (where foo x y z is the expression of type nat that you want to eavluate) you write value "int (foo x y z)".

  2. 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 of value, 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.

  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!