簡體   English   中英

是否可以在伊莎貝爾的 function 的域中添加假設?

[英]Is it possible to add assumptions to the domain of a function in Isabelle?

我不確定是否可以發布這樣的后續問題,但無論如何我都會這樣做。

所以幾天前我發布了這個問題: 如何刪除 Isabelle 中所有出現的子多重集?

我認為答案很好,但是在試圖證明引理時

lemma "applied1 {#''a'',''a'',''d'',''c'',''a'',''a'',''d'',''c''#} {#''a'',''a'',''c''#} ''f'' = {#''f'',''f'',''d'',''d''#}"

我真的卡住了。 我發現在展開 def 並應用一些簡單的自動化之后我不能簡單地做到這一點。 所以我回到我原來的 function 並對其進行了一些調整,如果輸入導致無限循環,它不會返回任何內容。 我以為這次會奏效,但伊莎貝爾仍然無法證明終止。 我很確定很明顯, size xsize y的倍數不斷減小並且不能為負數,因此當size x = 0或 y 不再是 x 的子集時,它最終必須終止。

fun applied2 :: "'a multiset ⇒ 'a multiset ⇒ 'a ⇒ 'a multiset option" where
"applied2 x y z = (if z ∈# y ∨ y = {#} then None else (if y ⊆# x then Some (plus {#z#} (the (applied2 (x - y) y z))) else Some x))"

是否有可能說服 Isabelle 這個 function 使用function而不是fun終止? 還是我必須考慮其他限制?

如果我不應該發布這樣的問題,我真的很抱歉。 我對 Isabelle 仍然缺乏經驗,我希望我能堅持自己的目標,盡可能地學習這門語言。 提前致謝!

我相信查看 文檔會為您提供正確的語法。

function applied2 :: "'a multiset ⇒ 'a multiset ⇒ 'a ⇒ 'a multiset option" where
"applied2 x y z = (if z ∈# y ∨ y = {#} then None else (if y ⊆# x then Some (plus {#z#} (the (applied2 (x - y) y z))) else Some x))"
  by pat_completeness auto
termination
  by (relation "measure (λ(x,y,z). size x)")
    (auto simp: mset_subset_eq_exists_conv nonempty_has_size)

如果問題是證據,大錘會為你找到它。

但是,我看不出您打算如何將 go 從應用 2 轉換為您真正想要的 function。 真正的問題是確定性:您需要一個順序來查看子集。 Manuel 的解決方案是使用 Sup,但這確實是不可執行的。

如果您對非遞歸定義的唯一問題是如何將其應用於具體輸入,我仍然認為我所說的可執行的替代定義是 go 的方式。 這是一個證明我給出的兩個非遞歸定義是等價的,以及您上面給出的示例的應用程序:

definition applied :: "'a multiset ⇒ 'a multiset ⇒ 'a ⇒ 'a multiset" where
  "applied ms xs y = (if xs = {#} then ms else
     (let n = Max {n. repeat_mset n xs ⊆# ms}
      in ms - repeat_mset n xs + replicate_mset n y))"

lemma count_le_size: "count M x ≤ size M"
  by (induction M) auto

lemma applied_code [code]:
  "applied ms xs y = (if xs = {#} then ms else
     (let n = (MIN x ∈set_mset xs. count ms x div count xs x)
      in ms - repeat_mset n xs + replicate_mset n y))"
  unfolding applied_def
proof (intro if_cong let_cong refl)
  assume ne: "xs ≠ {#}"
  have subset: "{n. repeat_mset n xs ⊆# ms} ⊆ {..size ms}"
  proof safe
    fix n assume n: "repeat_mset n xs ⊆# ms"
    from ne obtain x where x: "x ∈# xs"
      by auto
    have "n * 1 ≤ n * count xs x"
      using x by (intro mult_left_mono) auto
    also have "… = count (repeat_mset n xs) x"
      by simp
    also have "… ≤ count ms x"
      using n by (intro mset_subset_eq_count)
    also have "… ≤ size ms"
      by (rule count_le_size)
    finally show "n ≤ size ms" by simp
  qed
  hence finite: "finite {n. repeat_mset n xs ⊆# ms}"
    by (rule finite_subset) auto

  show "Max {n. repeat_mset n xs ⊆# ms} = (MIN x∈set_mset xs. count ms x div count xs x)"
  proof (intro antisym)
    show "Max {n. repeat_mset n xs ⊆# ms} ≤ (MIN x∈set_mset xs. count ms x div count xs x)"
    proof (rule Max.boundedI)
      show "{n. repeat_mset n xs ⊆# ms} ≠ {}"
        by (auto intro: exI[of _ 0])
    next
      fix n assume n: "n ∈ {n. repeat_mset n xs ⊆# ms}"
      show "n ≤ (MIN x∈set_mset xs. count ms x div count xs x)"
      proof (safe intro!: Min.boundedI)
        fix x assume x: "x ∈# xs"
        have "count (repeat_mset n xs) x ≤ count ms x"
          using n by (intro mset_subset_eq_count) auto
        also have "count (repeat_mset n xs) x = n * count xs x"
          by simp
        finally show "n ≤ count ms x div count xs x"
          by (metis count_eq_zero_iff div_le_mono nonzero_mult_div_cancel_right x)
      qed (use ne in auto)
    qed (fact finite)
  next
    define m where "m = (MIN x∈set_mset xs. count ms x div count xs x)"
    show "m ≤ Max {n. repeat_mset n xs ⊆# ms}"
    proof (rule Max.coboundedI[OF finite], safe)
      show "repeat_mset m xs ⊆# ms"
      proof (rule mset_subset_eqI)
        fix x
        show "count (repeat_mset m xs) x ≤ count ms x"
        proof (cases "x ∈# xs")
          case True
          have "count (repeat_mset m xs) x = m * count xs x"
            by simp
          also have "… ≤ (count ms x div count xs x) * count xs x"
            unfolding m_def using ‹x ∈# xs› by (intro mult_right_mono Min.coboundedI) auto
          also have "… ≤ count ms x"
            by simp
          finally show ?thesis .
        next
          case False
          hence "count xs x = 0"
            by (meson not_in_iff)
          thus ?thesis by simp
        qed
      qed
    qed
  qed
qed

lemma replicate_mset_unfold:
  assumes "n > 0"
  shows   "replicate_mset n x = {#x#} + replicate_mset (n - 1) x"
  using assms by (cases n) auto

lemma
  assumes "a ≠ c" "a ≠ f" "c ≠ f"
  shows   "applied {#a,a,c,a,a,c#} {#a,a,c#} f = mset [f, f]"
  using assms
  by (simp add: applied_code replicate_mset_unfold flip: One_nat_def)

value命令不適用於該示例,因為ac等是自由變量。 但是,如果您例如為它們創建一個臨時數據類型,它就可以工作:

datatype test = a | b | c | f

value "applied {#a,a,c,a,a,c#} {#a,a,c#} f"
(* "mset [f, f]" :: "test multiset" *)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM