简体   繁体   English

伊莎贝尔(Isabelle):linord证明

[英]Isabelle: linord proof

My attempt to create a custom linear order for a custom data type failed, Below is my code: 我尝试为自定义数据类型创建自定义线性顺序失败,以下是我的代码:

theory Scratch
imports Main 
begin

  datatype st  = Str "string"

  fun solf_str_int:: "string ⇒ int" where
    "solf_str_int str = (if (size str) > 0
                       then int(nat_of_char (hd str) + 1) + 100 *     (solf_str_int (tl str))
                       else 0)"

  fun soflord:: "st ⇒ st ⇒ bool" where
    "soflord s1 s2 = (case s1 of Str ss1 ⇒ (case s2 of Str ss2 ⇒ 
                        (solf_str_int ss1) ≤ (solf_str_int ss2)))"

instantiation st :: linorder
begin
  definition nleq: "less_eq n1 n2 == soflord n1 n2"  
  definition neq: "eq n1 n2 == (n1 ≤ n2) ∧ (n2 ≤ n1)"
  definition nle:  "less n1 n2 == (n1 ≤ n2) ∧ (¬(n1 = n2))" (* ++ *)

  instance proof
    fix n1 n2 x y :: st
    show "n1 ≤ n1" by (simp add:nleq split:st.split)
    show "(n1 ≤ n2) ∨ (n2 ≤ n1)" by (simp add:nleq split:st.split)     (*why is 'by ()' highlited?*)
    (*this fail if I comment line ++ out*)
    show "(x < y) = (x ≤ y ∧ (¬ (y ≤ x)))" by (simp add:nleq neq split:node.split)

  qed
end
end

The definition marked with (* ++ *) is not right and if delete it the last show give problems. 标有(* ++ *)的定义不正确,如果删除该定义,则最后一个显示会出现问题。

How do I correct the prove? 我该如何纠正证明? Why is the second last show partially highlighted? 为什么倒数第二个节目部分突出?

When you define the operations of a type class ( less_eq and less in the case of linorder ), the name of the overloaded operation can only be used if the inferred type of the operation matches exactly the overloaded instance that is being defined. 当定义一个类型的类(的操作less_eqless在的情况下linorder ),只能用于过载操作的名称,如果推断出的类型的动作的恰好正在定义重载实例相匹配。 In particular, the type is not specialised if it turns out to be too general. 特别是,如果类型过于通用,则该类型不是专用的。

The definition for less_eq works because soflord restricts the types of n1 and n2 to st , so less_eq is used with type st => st => bool , which is precisely what is needed here. less_eq的定义有效,因为soflordn1n2的类型限制为st ,因此less_eqst => st => bool类型less_eq使用,这正是此处所需要的。 For less , type inference computes the most general type 'b :: ord => 'b => bool . less ,类型推断就计算出最通用的类​​型'b :: ord => 'b => bool As this is not of the expected type st => st => bool , Isabelle does not recognize the definition as a definition of an overloaded operation and consequently complains that you want to redefine an existing operation in its full generality. 由于这不是st => st => bool的预期类型,因此Isabelle无法将该定义识别为重载操作的定义,因此抱怨您想完全重新定义一个现有操作。 If you restrict the types as necessary, then the definition works as expected. 如果根据需要限制类型,则定义将按预期工作。

definition nle:  "less n1 (n2 :: st) == (n1 ≤ n2) ∧ (¬(n1 = n2))"

However, your definitions do not define a linear order on st . 但是,您的定义未在st上定义线性顺序。 The problem is that antisymmetry is violated. 问题是违反了反对称性。 For example, the two strings Str ''d'' and Str [Char Nibble0 Nibble0, Char Nibble0 Nibble0] (ie, the string consisting of two characters at codepoint 0 ) are "equivalent" in your order, although they are different values. 例如,两个字符串Str ''d''Str [Char Nibble0 Nibble0, Char Nibble0 Nibble0] (即,在代码点0处由两个字符组成的字符串)在您的顺序中是“等效的”,尽管它们是不同的值。 You attempt to define equality on st , too, but in higher-order logic, equality cannot be defined. 您也尝试在st上定义相等性,但是在高阶逻辑中,无法定义相等性。 It is determined by the way you constructed your type. 这取决于您构造类型的方式。 If you really want to identify strings that are equivalent according to your order, you have to construct a quotient first, eg, using the quotient package. 如果您确实想根据您的订单标识等效的字符串,则必须首先构造一个商,例如,使用商包。

The purple highlighting of by(simp ...) indicates that the proof method simp is still running. by(simp ...)的紫色突出显示表示证明方法simp仍在运行。 In your case, it will not terminate, because simp will keep unfolding the defining equation for solf_str_int : its right-hand side contains an instance of the left-hand side. 在您的情况下,它不会终止,因为simp会不断展开solf_str_int的定义式:它的右侧包含左侧的一个实例。 I recommend that you define your functions by pattern-matching on the left-hand side of = . 我建议您通过=左侧的模式匹配来定义函数。 Then, the equations are only used when they can consume a pattern. 然后,仅在方程式可以使用某种模式时才使用它们。 Thus, you have to trigger case distinctions yourself (eg using cases ), but you also get more control over the automated tactics. 因此,您必须自己触发区分大小写(例如,使用cases ),但是您还可以更好地控制自动化策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM