Coq 定义无限树的等式关系
在coq中,我可以为组件为成对的共导类型定义相等关系:Coq 定义无限树的等式关系,coq,coinduction,Coq,Coinduction,在coq中,我可以为组件为成对的共导类型定义相等关系: Section Pairs. Variable (A:Type). CoInductive Stream := cons : (A * Stream) -> Stream. CoInductive Stream_eq : Stream -> Stream -> Prop := stream_eq : forall t1 t2 b1 b2, Stream_eq (t1) (t2) -
Section Pairs.
Variable (A:Type).
CoInductive Stream :=
cons : (A * Stream) -> Stream.
CoInductive Stream_eq : Stream -> Stream -> Prop :=
stream_eq : forall t1 t2 b1 b2, Stream_eq (t1) (t2)
-> (b1 = b2)
-> Stream_eq (cons (b1,t1)) (cons (b2,t2)).
End Pairs.
对于组件为函数的类型,我也可以这样做:
Section Functions.
Variable (A:Type).
CoInductive Useless :=
cons_useless : (A -> Useless) -> Useless.
CoInductive Useless_eq : Useless -> Useless -> Prop :=
useless_eq : forall t1 t2, (forall b, Useless_eq (t1 b) (t2 b))
-> Useless_eq (cons_useless t1) (cons_useless t2).
End Functions.
但我似乎无法为组件为函数对的类型定义类似关系:
Section FunctionsToPairs.
Variable (A:Type).
Variable (B:Type).
CoInductive InfiniteTree :=
cons_tree : (A -> B * InfiniteTree) -> InfiniteTree.
CoInductive Tree_eq : InfiniteTree -> InfiniteTree -> Prop :=
tree_eq : forall (t1:A -> B*InfiniteTree) (t2:A -> B*InfiniteTree),
(forall b, let (a1, c1) := (t1 b) in
let (a2, c2) := (t2 b) in Tree_eq c1 c2 /\ a1 = a2)
-> Tree_eq (cons_tree t1) (cons_tree t2).
End FunctionsToPairs.
我得到一个错误:
Non strictly positive occurrence of "Tree_eq" in
"forall t1 t2 : A -> B * InfiniteTree,
(forall b : A, let (a1, c1) := t1 b in let (a2, c2) := t2 b in Tree_eq c1 c2 /\ a1 = a2) ->
Tree_eq (cons_tree t1) (cons_tree t2)".
有没有办法为InfiniteTree类型定义一个定义良好的相等关系?Coq在析构函数下递归出现一个类型时会感到困惑。您可以通过稍微更改树类型的定义来解决此问题:
Section FunctionsToPairs.
Variable (A:Type).
Variable (B:Type).
CoInductive InfiniteTree :=
cons_tree : (A -> B) -> (A -> InfiniteTree) -> InfiniteTree.
CoInductive Tree_eq : InfiniteTree -> InfiniteTree -> Prop :=
tree_eq : forall (f1 f2 : A -> B) (t1 t2 : A -> InfiniteTree),
(forall x, f1 x = f2 x) ->
(forall x, Tree_eq (t1 x) (t2 x)) ->
Tree_eq (cons_tree f1 t1) (cons_tree f2 t2).
End FunctionsToPairs.
我想我可能已经知道了如何在不使用互归纳修改InfiniteTree类型的情况下实现这一点
CoInductive Tree_eq : InfiniteTree -> InfiniteTree -> Prop :=
tree_eq : forall (t1:A -> B*InfiniteTree) (t2:A -> B*InfiniteTree),
(forall b, Pair_eq (t1 b) (t1 b))
-> Tree_eq (cons_tree t1) (cons_tree t2)
with Pair_eq : B*InfiniteTree -> B*InfiniteTree -> Prop :=
pair_eq : forall b1 b2 t1 t2, b1 = b2 -> Tree_eq t1 t2 -> Pair_eq (b1, t1) (b2, t2).
一个缺点是,这种方法可能比使用Arthur所述的方法更难构造Tree_eq的证明。您的定义大部分被拒绝,因为使用了let-in构造,阻止了检查者确定构造函数中出现的
Tree_eq c1 c2
是有效的。如果您删除它们,或以不同的方式编写它们,Coq将接受该定义
例如,以下工作:
CoInductive Tree_eq : InfiniteTree -> InfiniteTree -> Prop :=
tree_eq : forall (t1:A -> B*InfiniteTree) (t2:A -> B*InfiniteTree),
(forall b, let x1 := (t1 b) in
let x2 := (t2 b) in Tree_eq (snd x1) (snd x2) /\ fst x1 = fst x2)
-> Tree_eq (cons_tree t1) (cons_tree t2).
请注意,启用了原始投影后,您的原始定义就生效了(这个想法来自@JasonGross)
这是次优的,因为我计划从中提取Haskell程序,虽然a->(b,c)和(a->b,a->c)可能(几乎)同构,但它们在性能方面表现不同-在第一种情况下,函数只被调用一次
Set Primitive Projections.
Record prod {A B} := pair { fst : A ; snd : B }.
Arguments prod : clear implicits.
Arguments pair {A B}.
Add Printing Let prod.
Notation "x * y" := (prod x y) : type_scope.
Notation "( x , y , .. , z )" := (pair .. (pair x y) .. z) : core_scope.
Hint Resolve pair : core.
Section FunctionsToPairs.
Variable (A:Type).
Variable (B:Type).
CoInductive InfiniteTree :=
cons_tree : (A -> B * InfiniteTree) -> InfiniteTree.
CoInductive Tree_eq : InfiniteTree -> InfiniteTree -> Prop :=
tree_eq : forall (t1:A -> B*InfiniteTree) (t2:A -> B*InfiniteTree),
(forall b, let (a1, c1) := (t1 b) in
let (a2, c2) := (t2 b) in Tree_eq c1 c2 /\ a1 = a2)
-> Tree_eq (cons_tree t1) (cons_tree t2).
End FunctionsToPairs.