现在我有一个相互定义的归纳类型a和t:
Inductive a : Type :=
| basic : string -> (string * string) -> a
| complex : string -> list a -> nat -> list t -> (string * string) -> a
| complex2 : string -> list a -> a
with t : Type :=
| t_intro : string * a * (set string) * (set string) -> t.
我的问题是如何证明每个人的可判定等式定义?
Definition a_dec : forall (x y : a), {x = y} + {x <> y}.
Definition t_dec : forall (x y : t), {x = y} + {x <> y}.
答案 0 :(得分:3)
这里有两个不同的问题:
你有互感类型,所以你需要声明一个相互的固定点,例如
Fixpoint a_dec (x y : a) : { x = y } + { x <> y }
with b_dec (x y : t) : { x = y } + { x <> y }.
这样Coq可以生成一个相互固定点,你可以使用两种诱导假设(谨防) 保护条件虽然)。还有其他方法可以定义此修复点,但这是最简单的方法。
在Coq意义上,您的类型不是“结构上”递归的:a
依赖于List.list
类型,
所以Coq将无法单独找到应用归纳所需的结构关系。如果您只需要关于list
的引理而根本没有引导,那么您就没有问题。否则,您可能必须定义自己的递归方案,或重新定义相互块中的列表,以便Coq了解列表中的元素是您的类型的子类。
对于前一种方法,我建议您阅读this page(搜索嵌套的归纳类型)。后一种解决方案可能如下:
Inductive a : Type :=
| basic : string -> (string * string) -> a
| complex : string -> list_a -> nat -> list_t -> (string * string) -> a
| complex2 : string -> list_a -> a
with t : Type :=
| t_intro : string * a * (set string) * (set string) -> t.
with list_a : Type
| anil : list_a
| acons : a -> list_a -> list_a
with list_t : Type
| tnil : list_t
| tcons : t -> list_t -> list_t
.
有了这个,您将无法直接使用List.list
库,但您仍然可以在list_a
和list a
之间建立双向翻译(resp。{{ 1}})在不需要归纳时使用该库。
希望它有所帮助, 诉
答案 1 :(得分:2)
您的类型具有嵌套归纳,它们是归纳类型的递归出现,作为其他归纳类型的参数(在您的情况下,对和列表)。
与其他情况一样,您需要感应才能显示您想要的内容。不幸的是,与更简单的类型不同,Coq没有自动生成这种类型的归纳原理的烘焙机制,所以你必须自己推出。关于一般问题here有一些有趣的链接。以下是如何通过组合嵌套和相互递归来解决您的情况。请注意Forall'
辅助归纳法以说明归纳假设
Require Import String.
Require Import List.
Require Import ListSet.
Import ListNotations.
Set Implicit Arguments.
Inductive a : Type :=
| basic : string -> (string * string) -> a
| complex : string -> list a -> nat -> list t -> (string * string) -> a
| complex2 : string -> list a -> a
with t : Type :=
| t_intro : string * a * (set string) * (set string) -> t.
Inductive Forall' A (T : A -> Type) : list A -> Type :=
| Forall_nil' : Forall' T []
| Forall_cons' : forall x l, T x -> Forall' T l -> Forall' T (x :: l).
Definition build_forall A (T : A -> Type) (f : forall x, T x) :=
fix F l : Forall' T l :=
match l with
| [] => Forall_nil' _
| x :: l' => Forall_cons' x (f x) (F l')
end.
Lemma forall_eq_dec A (l : list A) (H : Forall' (fun x => forall y, {x = y} + {x <> y}) l) :
forall l', {l = l'} + {l <> l'}.
Proof.
induction H as [| x l Hx Hl IH]; intros [|y l']; try (right; congruence); eauto.
specialize (IH l').
destruct IH; subst; try (right; congruence).
destruct (Hx y); subst; try (right; congruence).
eauto.
Qed.
Fixpoint a_ind' (Ta : a -> Type) (Tt : t -> Type)
(H1 : forall s p, Ta (basic s p))
(H2 : forall s la, Forall' Ta la ->
forall n lt, Forall' Tt lt ->
forall p, Ta (complex s la n lt p))
(H3 : forall s la, Forall' Ta la -> Ta (complex2 s la))
(H4 : forall s x, Ta x ->
forall ss1 ss2, Tt (t_intro (s, x, ss1, ss2))) x {struct x} : Ta x :=
match x with
| basic s p => H1 s p
| complex s la n lt p => H2 s la (build_forall _ (a_ind' H1 H2 H3 H4) la)
n lt (build_forall _ (t_ind' H1 H2 H3 H4) lt)
p
| complex2 s la => H3 s la (build_forall _ (a_ind' H1 H2 H3 H4) la)
end
with t_ind' (Ta : a -> Type) (Tt : t -> Type)
(H1 : forall s p, Ta (basic s p))
(H2 : forall s la, Forall' Ta la ->
forall n lt, Forall' Tt lt ->
forall p, Ta (complex s la n lt p))
(H3 : forall s la, Forall' Ta la -> Ta (complex2 s la))
(H4 : forall s x, Ta x ->
forall ss1 ss2, Tt (t_intro (s, x, ss1, ss2))) x {struct x} : Tt x :=
match x with
| t_intro (s, x, ss1, ss2) => H4 s x (a_ind' H1 H2 H3 H4 x) ss1 ss2
end.
Definition a_and_t_ind (Ta : a -> Type) (Tt : t -> Type)
(H1 : forall s p, Ta (basic s p))
(H2 : forall s la, Forall' Ta la ->
forall n lt, Forall' Tt lt ->
forall p, Ta (complex s la n lt p))
(H3 : forall s la, Forall' Ta la -> Ta (complex2 s la))
(H4 : forall s x, Ta x ->
forall ss1 ss2, Tt (t_intro (s, x, ss1, ss2))) :
(forall x, Ta x) * (forall x, Tt x) :=
(a_ind' H1 H2 H3 H4, t_ind' H1 H2 H3 H4).
Lemma pair_dec A B (HA : forall x y : A, {x = y} + {x <> y})
(HB : forall x y : B, {x = y} + {x <> y}) :
forall x y : A * B, {x = y} + {x <> y}.
Proof. decide equality. Qed.
Definition a_and_t_dec : (forall x y : a, {x = y} + {x <> y}) *
(forall x y : t, {x = y} + {x <> y}).
Proof.
apply a_and_t_ind.
- intros s p [s' p'| |]; try (right; congruence).
destruct (string_dec s s'); try (right; congruence).
subst.
destruct (pair_dec string_dec string_dec p p'); try (right; congruence).
subst. eauto.
- intros s la Hla n lt Hlt p y.
destruct y as [|s' la' n' lt' p'|]; try (right; congruence).
destruct (string_dec s s'); subst; try (right; congruence).
destruct (forall_eq_dec Hla la'); subst; try (right; congruence).
destruct (NPeano.Nat.eq_dec n n'); subst; try (right; congruence).
destruct (forall_eq_dec Hlt lt'); subst; try (right; congruence).
destruct (pair_dec string_dec string_dec p p'); subst; try (right; congruence).
eauto.
- intros s la Hla [| |s' la']; try (right; congruence).
destruct (string_dec s s'); subst; try (right; congruence).
destruct (forall_eq_dec Hla la'); subst; try (right; congruence).
eauto.
- intros s x Hx ss1 ss2 [[[[s' x'] ss1'] ss2']].
destruct (string_dec s s'); subst; try (right; congruence).
destruct (Hx x'); subst; try (right; congruence).
destruct (list_eq_dec string_dec ss1 ss1'); subst; try (right; congruence).
destruct (list_eq_dec string_dec ss2 ss2'); subst; try (right; congruence).
eauto.
Qed.
Definition a_dec := fst a_and_t_dec.
Definition t_dec := snd a_and_t_dec.