首页 > 解决方案 > 在 Coq 中将临时列表转换为依赖类型列表

问题描述

我在 Coq 中有以下列表定义:

Variable A : Set.
Variable P : A -> Prop.
Hypothesis P_dec : forall x, {P x}+{~(P x)}.

Inductive plist : nat -> Set :=
   pnil : plist O
  | pcons : A -> forall n, plist n -> plist n
  | pconsp : forall (a:A) n, plist n -> P a -> plist (S n)
  .

A它描述了“其中至少n满足谓词的类型元素列表P”。

我的任务是创建将临时列表转换为plist(尽可能n)的函数。我的尝试是首先计算所有匹配P的元素,然后根据结果设置输出类型:

Fixpoint pcount (l : list A) : nat :=
  match l with
  | nil => O
  | h::t => if P_dec h then S(pcount t) else pcount t
  end.

Fixpoint plistIn (l : list A) : (plist (pcount l)) :=
  match l with
  | nil => pnil
  | h::t => match P_dec h with
          | left proof => pconsp h _ (plistIn t) proof
          | right _ => pcons h _ (plistIn t) 
          end
  end.

但是,我收到一条错误消息left proof

Error:
In environment
A : Set
P : A -> Prop
P_dec : forall x : A, {P x} + {~ P x}
plistIn : forall l : list A, plist (pcount l)
l : list A
h : A
t : list A
proof : P h
The term "pconsp h (pcount t) (plistIn t) proof" has type
 "plist (S (pcount t))" while it is expected to have type
 "plist (pcount (h :: t))".

问题是 Coq 看不到那S (pcount t)等于pcount (h :: t)知道P h,这已经被证明了。我不能让 Coq 知道这个真相。

如何正确定义这个函数?甚至有可能这样做吗?

标签: coqdependent-type

解决方案


您可以使用依赖模式匹配,因为结果类型plist (pcount (h :: t))取决于P_dec hisleftright.

下面,关键字as引入了一个新变量p,并return告诉了整个match表达式的类型,由 参数化p

Fixpoint plistIn (l : list A) : (plist (pcount l)) :=
  match l with
  | nil => pnil
  | h::t => match P_dec h as p return plist (if p then _ else _) with
          | left proof => pconsp h (pcount t) (plistIn t) proof
          | right _ => pcons h _ (plistIn t) 
          end
  end.

替换时类型plist (if p then _ else _)必须等于。然后在每个分支中,比如说,你需要产生(减少到左分支)。plist (pcount (h :: t))p := P_dec hleft proofplist (if left proof then _ else _)

Coq 可以在这里推断下划线中的内容有点神奇,但为了安全起见,您始终可以将其拼写出来:(if p then S (pcount t) else pcount t这意味着与 的定义完全匹配pcount)。


推荐阅读