定义无限树的相等关系

时间:2018-10-26 12:21:40

标签: 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)
               -> (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类型建立明确定义的相等关系?

3 个答案:

答案 0 :(得分:1)

我想我可能已经想出了如何在不使用互共生来修改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 answer中所述的方法相比,用这种方法构造Tree_eq的证明可能要困难得多。

答案 1 :(得分:1)

您的定义在很大程度上被拒绝,这是因为使用了让入式结构,阻止了检查器确定在构造器Tree_eq c1 c2tree_eq的出现是有效的。如果您删除它们或以其他方式写它们,则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的this answer)。

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.

答案 2 :(得分:0)

在析构函数下递归出现某个类型时,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.