Isabelle / HOL / Isar:如何使像“∃(​​x :: nat)。(x≠2∧(x≠2⟶x = 1))”这样的公式更容易证明?

时间:2019-06-25 03:36:50

标签: isabelle isar hol

我的故事很长,而我实际想要做的代码也很长。因此,首先,我将描述标题中使用公式进行的尝试,然后再描述在实践中如何遇到类似的情况,将我的实际代码放在最底下。

我了解也许我应该以完全不同的方式完成我想做的事情:我没有使用Isabelle的经验(甚至没有使用Haskell这样的功能编程语言的经验),所以我的代码可能不是惯用的

我做了什么

首先,我了解可以用by presburger来证明本节中的所有内容(正如try告诉我的那样)。但是,这不适用于我的实际问题,也可能不应该,因为在那里我处理的是包含整数的自定义数据类型,而不是整数本身。

在尝试将其简化为一个最小的完整可验证示例时,我注意到,即使对于"∃(x::nat). (x ≠ 2)"这样的简单公式,伊莎贝尔也很难证明。但是,我认为像x = 1这样的具体做法可能会有所帮助。确实,

lemma "∃(x::nat). (x ≠ 2 ∧ x = 1)"
  by auto

工作轻松,但是

lemma "∃(x::nat). (x ≠ 2 ∧ (x ≠ 2 ⟶ x = 1))"
  by auto

仍然失败。我想也许我应该把具体的部分放在第一位,但是当我尝试

lemma "∃(x::nat). ((x ≠ 2 ⟶ x = 1) ∧ x ≠ 2)"
  by auto

by auto行在jEdit中保持紫色,我认为这表示无限循环。

我怎么遇到的

我正在尝试使用Isabelle为Shogi建立模型,并最终证明某些Shogi问题解决方案的独特性。当我试图证明一个简单的配置是一个将军时,Isabelle陷入了困境(就上下文而言,我有datatype coord = Coord int int):

∃dst. dst ≠ Coord 5 3 ∧
          (dst ≠ Coord 5 3 ⟶
           dst ≠ Coord 5 2 ∧
           (dst ≠ Coord 5 2 ⟶
            (dst = Coord 5 1 ⟶
             (∃src. src ≠ Coord 5 3 ∧ (src ≠ Coord 5 3 ⟶ src = Coord 5 2))) ∧
            dst = Coord 5 1))

我知道这看起来很烂。让我稍微调整一下缩进量,以使其更易于解析为人眼。

∃dst. dst ≠ Coord 5 3 ∧
      (
          dst ≠ Coord 5 3 ⟶
          dst ≠ Coord 5 2 ∧
          (
              dst ≠ Coord 5 2 ⟶
              (
                  dst = Coord 5 1 ⟶
                  (∃src. src ≠ Coord 5 3 ∧ (src ≠ Coord 5 3 ⟶ src = Coord 5 2))
              ) ∧ dst = Coord 5 1
          )
      )

希望可以看到src = Coord 5 2src满足单行,然后dst = Coord 5 1满足了整个要求。

我试图将其作为引理并证明它by auto ---当然是行不通的。我终于尝试了by try,它告诉我:

"cvc4": Try this: by (smt coord.inject) (866 ms)

好的,那行得通。但是我真的很困惑,因为我什至不知道smt是什么。

无论如何,该方法(应用auto之后)成功证明了播放器2处于检查状态。我尝试使用相同的方法来证明它是一个将死者(至少,它证明在几个配置中Player 2仍在检查中),但这也导致了错误:

Solver z3: Solver "z3" failed -- enable tracing using the "smt_trace" option for details

甚至sledgehammer都没有帮助我。

由于支票和将军是Shogi问题的最基本规则,我认为我必须能够自动识别它们,然后才能执行平凡的事情。我愿意手动写下并证明一些引理,但现在我相信我可能做的是根本错误的事情。

我的代码

如果有什么需要澄清的,请在评论中提问。

theory Shogi
  imports Main "HOL-Library.Multiset"
begin

datatype coord = Coord int int
datatype vector = Vector int int
datatype piece_type = King | Gold
datatype move = Move coord coord | Drop piece_type coord
datatype player = Sente | Gote
type_synonym on_board =  "(coord, player * piece_type) map"
datatype board = Board on_board "(player * piece_type) multiset" player

fun opponent :: "player ⇒ player" where
  "opponent Sente = Gote" |
  "opponent Gote = Sente"

fun diff :: "coord ⇒ coord ⇒ vector" where
  "diff (Coord x1 y1) (Coord x0 y0) = Vector (x1 - x0) (y1 - y0)"

fun negate :: "vector ⇒ vector" where
  "negate (Vector x y) = (Vector (-x) (-y))"

fun to_hand :: "(player * piece_type) option ⇒ (player * piece_type) multiset" where
  "to_hand None = {#}" |
  "to_hand (Some (owner, piece)) = {#((opponent owner), piece)#}"

fun make_move :: "board ⇒ move ⇒ board" where
  "make_move (Board on_board in_hand to_move) (Move src dst) =
    (Board
      (on_board(src := None, dst := (on_board src)))
      (in_hand + (to_hand (on_board dst)))
      (opponent to_move)
    )" |
  "make_move (Board on_board in_hand to_move) (Drop piece pos) =
    (Board
      (on_board(pos := Some (to_move, piece)))
      (in_hand - {#(to_move, piece)#})
      (opponent to_move)
    )"

fun is_on_board :: "coord ⇒ bool" where
  "is_on_board (Coord file rank) = (1 ≤ file ∧ file ≤ 9 ∧ 1 ≤ rank ∧ rank ≤ 9)"

fun movement_vector :: "piece_type ⇒ vector ⇒ bool" where
  "movement_vector King (Vector x y) = ((x, y) ∈ {
    (1, -1), (0, -1), (-1, -1),
    (1,  0),          (-1,  0),
    (1,  1), (0,  1), (-1,  1)})" | 
  "movement_vector Gold (Vector x y) = ((x, y) ∈ {
    (1, -1), (0, -1), (-1, -1),
    (1,  0),          (-1,  0),
             (0,  1)          })"

fun negated_movement_vector :: "piece_type ⇒ vector ⇒ bool" where
  "negated_movement_vector piece vector = movement_vector piece (negate vector)"

fun absolute_movement_vector :: "player ⇒ piece_type ⇒ vector ⇒ bool" where
  "absolute_movement_vector Sente = movement_vector" |
  "absolute_movement_vector Gote = negated_movement_vector"

fun is_legal_for_piece :: "(player * piece_type) option ⇒ player ⇒ on_board
                                                        ⇒ coord ⇒ coord ⇒ bool" where
  "is_legal_for_piece None _ _ _ _ = False" |
  "is_legal_for_piece (Some (owner, piece)) to_move on_board src dst =
    (owner = to_move ∧ absolute_movement_vector owner piece (diff dst src))"

fun get_owner :: "(player * piece_type) option ⇒ player option" where
  "get_owner None = None" | "get_owner (Some (owner, piece)) = Some owner"

fun is_legal_physically :: "board ⇒ move ⇒ bool" where
  "is_legal_physically (Board on_board in_hand to_move) (Move src dst) =
    (
      (is_on_board dst) ∧
      (get_owner (on_board dst)) ≠ Some to_move ∧
      (is_legal_for_piece (on_board src) to_move on_board src dst)
    )" |
  "is_legal_physically (Board on_board in_hand to_move) (Drop piece pos) =
    ((is_on_board pos) ∧ ((on_board pos) = None))"

fun is_in_check :: "on_board ⇒ player ⇒ bool" where
  "is_in_check on_board player = 
    (∃dst src.
      (on_board dst) = Some (player, King) ∧
      (is_legal_for_piece (on_board src) (opponent player) on_board src dst)
    )"

fun is_legal_board :: "board ⇒ bool" where
  "is_legal_board (Board on_board in_hand to_move) = 
    (¬(is_in_check on_board (opponent to_move)))"

fun is_legal :: "board ⇒ move ⇒ bool" where
  "is_legal board move = 
    (
      (is_legal_physically board move) ∧ 
      (is_legal_board (make_move board move))
    )"

fun is_mate :: "board ⇒ bool" where "is_mate board = (¬(∃move. (is_legal board move)))"

fun is_checkmate :: "board ⇒ bool" where
  "is_checkmate (Board on_board in_hand to_move) = (
    (is_mate (Board on_board in_hand to_move)) ∧ (is_in_check on_board to_move))"

abbreviation board1 :: on_board where "board1 ≡ (map_of [
    ((Coord 5 1), (Gote, King)),
    ((Coord 5 2), (Sente, Gold)),
    ((Coord 5 3), (Sente, King))
  ])"

lemma "∃dst. dst ≠ Coord 5 3 ∧
          (dst ≠ Coord 5 3 ⟶
           dst ≠ Coord 5 2 ∧
           (dst ≠ Coord 5 2 ⟶
            (dst = Coord 5 1 ⟶
             (∃src. src ≠ Coord 5 3 ∧ (src ≠ Coord 5 3 ⟶ src = Coord 5 2))) ∧
            dst = Coord 5 1))"
  by (smt coord.inject)

lemma "is_in_check board1 Gote"
  apply auto
  apply (smt coord.inject)
  done

lemma "is_checkmate (Board board1 {#} Gote)"
  apply auto
  apply sledgehammer (* Both "cvc4" and "vampire" timed out *)
  apply (smt coord.inject) (* Solver z3: Solver "z3" failed -- enable tracing using the "smt_trace" option for details *)
  apply presburger (* Failed to apply proof method *)

end

0 个答案:

没有答案