简体   繁体   English

是否可以在伊莎贝尔的 function 的域中添加假设?

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

I'm not sure if it's okay to post a follow-up question like this, but I'll do it anyway.我不确定是否可以发布这样的后续问题,但无论如何我都会这样做。

So a few days ago I posted this question: How can I remove all occurrences of a sub-multiset in Isabelle?所以几天前我发布了这个问题: 如何删除 Isabelle 中所有出现的子多重集?

I thought the answer was great, but when trying to prove the lemma我认为答案很好,但是在试图证明引理时

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

I got really stuck.我真的卡住了。 I found that I couldn't simply do it after unfolding the def and applying some simple automations.我发现在展开 def 并应用一些简单的自动化之后我不能简单地做到这一点。 So I went back to my original function and made some tweaks to it such that it returns nothing if the input were to result in an infinite loop.所以我回到我原来的 function 并对其进行了一些调整,如果输入导致无限循环,它不会返回任何内容。 I thought it was going to work this time, but Isabelle still couldn't prove termination.我以为这次会奏效,但伊莎贝尔仍然无法证明终止。 I'm pretty sure it's obvious that size x is constantly decreasing by a factor of size y and cannot be negative, so it will have to ultimately terminate when size x = 0 or when y is no longer a subset of x.我很确定很明显, 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))"

Is it possible to convince Isabelle that this function terminates using function instead of fun ?是否有可能说服 Isabelle 这个 function 使用function而不是fun终止? Or are there other constraints I have to take into account?还是我必须考虑其他限制?

I'm really sorry if I shouldn't be posting questions like this.如果我不应该发布这样的问题,我真的很抱歉。 I'm still inexperienced with Isabelle and I do hope I'm upholding my purpose to learn about the language as best as I can.我对 Isabelle 仍然缺乏经验,我希望我能坚持自己的目标,尽可能地学习这门语言。 Thanks in advance!提前致谢!

I believe that looking at the documentation would have given you the correct syntax.我相信查看 文档会为您提供正确的语法。

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)

If the problem is the proof, sledgehammer would have found it for you.如果问题是证据,大锤会为你找到它。

However, I don't see how you intend to go from applied2 to the function you really want.但是,我看不出您打算如何将 go 从应用 2 转换为您真正想要的 function。 The real problem is determinism: you need an order to look at the subsets.真正的问题是确定性:您需要一个顺序来查看子集。 Manuel's solution was using Sup, but this is indeed not executable. Manuel 的解决方案是使用 Sup,但这确实是不可执行的。

If your only problem with the non-recursive definition is how to apply it to concrete inputs, I still think the alternative definition that I said was executable is the way to go.如果您对非递归定义的唯一问题是如何将其应用于具体输入,我仍然认为我所说的可执行的替代定义是 go 的方式。 Here's a prove that the two non-recursive definitions I gave are equivalent, and the application to the example you gave above:这是一个证明我给出的两个非递归定义是等价的,以及您上面给出的示例的应用程序:

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)

The value command does not work on that example because a , c , etc. are free variables. value命令不适用于该示例,因为ac等是自由变量。 But if you eg make an ad-hoc datatype for them, it works:但是,如果您例如为它们创建一个临时数据类型,它就可以工作:

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.

相关问题 在Isabelle中证明函数定义的正确性 - proving function definition correctness in Isabelle 列表量化功能:语法和类型错误(Isabelle) - Function for quantifying lists: syntax and type error (Isabelle) 在javascript中,是否可以在函数名称中添加字符串? - In javascript, is it possible to add a string to a function name? 是否可以在 PHP 的邮件功能中添加 CSS? - Is it possible to add CSS in the Mail function in PHP? Python函数可选参数 - 可以添加为条件? - Python function optional arguments - possible to add as condition? 将函数添加为数组本机对象的方法(是否可以将函数作为方法调用?) - Add function as method of Array native object (Is it possible to call a function as a method?) 请帮助说明该域的功能(使用函数在数组中添加斐波那契序列元素的程序) - Please help explain what this domain is doing (Program to add fibonacci sequence elements in array with a function) 最小化域上的功能 - Minimizing a function over a Domain 添加不是 function - add is not a function 是否可以将 timedelta Function 与输入连接,以便用户能够输入想要的秒数以添加到日期时间? - Is it possible to connect the timedelta Function with an Input, so that the User is able to enter wanted seconds to add to date time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM