如何处理 Coq 中 Program Fixpoint 生成的非常大的项?

2024-04-20

我试图在 Coq 中定义并证明正确的函数,该函数可以有效地比较两个排序列表。由于它并不总是在结构较小的项上递归(第一个或第二个列表较小),Fixpoint不会接受它,所以我尝试使用Program Fixpoint反而。

当尝试使用策略证明函数的属性时simpl or program_simpl,Coq 花费几分钟计算,然后生成一个长达数百行的巨大术语。我想知道我是否正在使用Program Fixpoint错误的方式,或者在推理时是否应该使用其他策略而不是简化?

我还想知道在像这样的参数中包含正确性所需的属性是否是一个好习惯,或者最好有一个单独的包装函数将正确性属性作为参数,并使该函数仅将两个列表进行比较?

请注意,我确实尝试定义一个更简单的版本make_diff,仅将 l1 和 l2 作为参数并固定类型A和关系R,但这仍然产生了一个巨大的术语program_simpl or simpl战术被应用。

*编辑:我的包含内容是(尽管这里可能并不需要全部):

Require Import Coq.Sorting.Sorted.
Require Import Coq.Lists.List.
Require Import Coq.Relations.Relation_Definitions.
Require Import Recdef.
Require Import Coq.Program.Wf.
Require Import Coq.Program.Tactics.

代码:

Definition is_decidable (A : Type) (R : relation A) := forall x y, {R x y} + {~(R x y)}.
Definition eq_decidable (A : Type) := forall (x y : A), { x = y } + { ~ (x = y) }.

Inductive diff (X: Type) : Type :=
  | add : X -> diff X
  | remove : X -> diff X 
  | update : X -> X -> diff X.

Program Fixpoint make_diff (A : Type) 
    (R : relation A)
    (dec : is_decidable A R)
    (eq_dec : eq_decidable A)
    (trans : transitive A R) 
    (lt_neq : (forall x y, R x y -> x <> y))
    (l1 l2 : list A)
     {measure (length l1 + length l2) } : list (diff A) :=
  match l1, l2 with
  | nil, nil => nil
  | nil, (new_h::new_t) => (add A new_h) :: (make_diff A R dec eq_dec trans lt_neq nil new_t)
  | (old_h::old_t), nil => (remove A old_h) :: (make_diff A R dec eq_dec trans lt_neq old_t nil)
  | (old_h::old_t) as old_l, (new_h::new_t) as new_l => 
    if dec old_h new_h 
      then (remove A old_h) :: make_diff A R dec eq_dec trans lt_neq old_t new_l
      else if eq_dec old_h new_h 
        then (update A old_h new_h) :: make_diff A R dec  eq_dec trans lt_neq old_t new_t
        else  (add A new_h) :: make_diff A R dec eq_dec trans lt_neq old_l new_t 
  end.
Next Obligation.
Proof.
  simpl.
  generalize dependent (length new_t).
  generalize dependent (length old_t).
  auto with arith.
Defined.
Next Obligation.
Proof.
  simpl.
  generalize dependent (length new_t).
  generalize dependent (length old_t).
  auto with arith.
Defined.

在这种特殊情况下,我们可以摆脱Program Fixpoint并使用简单的Fixpoint。因为在每次递归调用时我们都会调用make_diff无论是在第一个列表的尾部还是在第二个列表的尾部,我们都可以嵌套两个定点函数,如下所示。 (我已经使用了Section这里的机制是为了避免传递太多相同的参数)

Require Import Coq.Lists.List.
Import ListNotations.
Require Import Coq.Relations.Relations.

Section Make_diff.

Variable A : Type.
Variable R : relation A.
Variable dec : is_decidable A R.
Variable eq_dec : eq_decidable A.
Variable trans : transitive A R.
Variable lt_neq : forall x y, R x y -> x <> y.

Fixpoint make_diff (l1 l2 : list A) : list (diff A) :=
  let fix make_diff2 l2 :=
  match l1, l2 with
  | nil, nil => nil
  | nil, new_h::new_t => (add A new_h) :: make_diff2 new_t
  | old_h::old_t, nil => (remove A old_h) :: make_diff old_t nil
  | old_h::old_t, new_h::new_t =>
    if dec old_h new_h 
    then (remove A old_h) :: make_diff old_t l2
    else if eq_dec old_h new_h 
         then (update A old_h new_h) :: make_diff old_t new_t
         else (add A new_h) :: make_diff2 new_t
  end
  in make_diff2 l2.

End Make_diff.

观察到Section机制不会在生成的签名中包含未使用的参数。这是一个简单的测试:

(* make the first 2 arguments implicit *)    
Arguments make_diff [A R] _ _ _ _.

Require Import Coq.Arith.Arith.

Compute make_diff lt_dec Nat.eq_dec [1;2;3] [4;5;6].
(* = [remove nat 1; remove nat 2; remove nat 3; add nat 4; add nat 5; add nat 6] 
      : list (diff nat) *)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何处理 Coq 中 Program Fixpoint 生成的非常大的项? 的相关文章

  • 对 Coq 导入感到困惑

    有人可以告诉我之间的区别吗 Require Name Require Import Name Import Name Require 加载外部库 通常来自标准库或user contribs 文件夹 Import 导入模块中的名称 例如 如果
  • 当目标是类型时,为什么 Coq 不允许反转、析构等?

    When refine正在运行一个程序 我试图通过以下方式结束证明inversion on a False假设当目标是Type 这是我尝试做的证明的简化版本 Lemma strange1 forall T Type 0 gt 0 gt T
  • Coq 中的 `destruct` 和 `case_eq` 策略有什么区别?

    我明白了destruct因为它将归纳定义分解为其构造函数 我最近看到case eq我不明白它有什么不同 1 subgoals n nat k nat m M t nat H match M find elt nat n m with Som
  • 如何切换到 Coq 的特定版本——尤其是在使用 Opam 管理 Coq 版本时?

    我目前使用的是标准方式 可能通过网站 安装的标准方式 但我想用tcoq 我相信我已经正确安装了它 因为我有一个 bin 文件 并且所有常见的 Coq 内容似乎都在那里 pinno gamepad tcoq ls bin coq tex co
  • 在 Coq 中使用我自己的 == 运算符重写策略

    我试图直接从字段的公理证明简单的字段属性 经过对 Coq 原生现场支持的一些实验 像这个 我决定最好简单地写下 10 条公理并使其自成一体 我在需要使用的时候遇到了困难rewrite与我自己的 运算符自然不起作用 我意识到我必须添加一些我的
  • Coq:定义子类型

    我有一个类型 比如说 Inductive Tt a b c 定义它的子类型的最简单和 或最好的方法是什么 假设我希望子类型仅包含构造函数a and b 一种方法是对二元素类型进行参数化 例如布尔 Definition filt x bool
  • 匹配中的冗余子句

    当我运行以下脚本时 Definition inv a Prop Prop match a with False gt True True gt False end 我收到 错误 该子句是多余的 知道为什么会发生这种情况吗 谢谢 马库斯 这件
  • 用约翰·梅杰的等式重写

    约翰 梅杰的等式带有以下重写引理 Check JMeq ind r JMeq ind r forall A Type x A P A gt Prop P x gt forall y A JMeq y x gt P y 很容易将其概括为 Le
  • Coq - 在 if ... then ... else 中使用 Prop (True | False)

    我对 Coq 有点陌生 我正在尝试实现插入排序的通用版本 我正在实现一个以比较器作为参数的模块 该 Comparator 实现了比较运算符 如 is eq is le is neq 等 在插入排序中 为了插入 我必须比较输入列表中的两个元素
  • 证明匹配语句

    为了解决一个练习 我有以下代表整数的定义 Inductive bin Type Zero bin Twice bin gt bin TwiceOne bin gt bin 这个想法是 Twice x is 2 x 两次一x is 2 x 1
  • 在 Coq 中,重写适用于 = 但不适用于 <-> (iff)

    我在证明期间有以下内容 我需要替换normal form step t with value t因为有一个已证明的定理存在等价 H1 t1 gt t1 normal form step t1 t2 tm H2 t2 gt t2 normal
  • 如何在 Coq 中禁用我的自定义符号?

    我定义了一个符号来模拟命令式编程 Notation a gt gt b b a at level 50 然而之后 所有函数应用表达式都表示为 gt gt 样式 例如 在 Coq Toplevel 的证明模式下 我可以看到 bs nat gt
  • 如何在 Coq 中切换当前目标?

    是否可以切换当前目标或子目标来在 Coq 中进行证明 例如 我有一个这样的目标 来自 eexists 1 1 s gt 0 r1 r1 s1 s r3 r3 s2 我想做的是split并首先证明正确的连接 我认为这将给出存在变量的值 s 并
  • 如何暂时禁用 Coq 中的符号

    当您熟悉项目时 符号很方便 但当您刚刚开始使用代码库时 符号可能会令人困惑 我知道你可以用白话关闭所有符号Set Printing All 但是 我想保留一些打印 例如隐式参数 全部打印如下 Require Import Utf8 core
  • 将假设中的 ~exists 转换为 forall

    我陷入了假设的境地 exists k k lt n 1 f k f n 2 并希望将其转换为等效的 我希望如此 假设forall k k lt n 1 gt f k lt gt f n 2 这是一个小例子 Require Import Co
  • 在 Coq 中证明可逆列表是回文

    这是我对回文的归纳定义 Inductive pal X Type list X gt Prop pal0 pal pal1 forall x X pal x pal2 forall x X l list X pal l gt pal x l
  • Coq :> 符号

    这可能是非常微不足道的 但我找不到任何关于 gt 符号在 Coq 中含义的信息 有什么区别 U 类型 和 W gt 类型 这取决于符号出现的位置 例如 如果它位于记录声明内 它会指示 Coq 添加相应的记录投影作为强制 具体来说 假设我们有
  • Coq:添加“强归纳”策略

    对自然数的 强 或 完全 归纳意味着当证明 n 上的归纳步骤时 您可以假设该属性对于任何 k 都成立 Theorem strong induction forall P nat gt Prop forall n nat forall k n
  • 可以在 Coq 的蕴涵中使用 destruct 吗?

    destruct可以用来分割and or在柯克 不过好像也可以用暗示 例如我想证明 P gt P Lemma test P P gt P Proof unfold not intro pffpf apply pffpf intro pff
  • 在 Coq 中,“if then else”允许非布尔第一个参数?

    我读过一些教程if a then b else c代表match a with true gt b false gt c end 然而 很奇怪的是 前者不检查类型a 而后者当然确保a是一个布尔值 例如 Coq lt Check if nil

随机推荐