Coq中的从属类型列表连接

时间:2014-11-12 16:32:29

标签: coq

我有一个带有对象和箭头的图形,从中我已经定义了箭头路径的概念。我想定义这些路径的连接。以下代码是我天真的尝试。但是p'条款emptyPathmatch时出错:'术语“p”的类型为“路径A B”,而预期类型为“路径A B”  “路径A C”。'从概念上讲,应该没有问题,因为在这种情况下B等于C。我怎样才能让它发挥作用?

Parameter Object : Set.
Parameter Arrow : Object -> Object -> Set.

Inductive path : Object -> Object -> Set :=
  emptyPath : forall X, path X X
| cons : forall {A B C} (p : path A B) (f : Arrow B C), path A C.

Fixpoint concat {A B C : Object} (p : path A B) (p' : path B C) :  path A C :=
  match p' return path A C with
    | emptyPath _ => p
    | cons _ _ _ p0 f => cons (concat p p0) f
  end. 

2 个答案:

答案 0 :(得分:1)

每当你有"不同的指数"在您的定义中,您需要应用所谓的"护航模式"让你的定义工作。基本思想是重新构建一些参数,以便依赖模式匹配可以“改变”#34;他们的类型。

Parameter Object : Set.
Parameter Arrow : Object -> Object -> Set.

Inductive path : Object -> Object -> Set :=
  emptyPath : forall X, path X X
| cons : forall {A B C} (p : path A B) (f : Arrow B C), path A C.

Fixpoint concat {A B C : Object} (p : path A B) (p' : path B C) :  path A C :=
  match p' in path B C return path A B -> path A C with
    | emptyPath _ => fun p => p
    | cons _ _ _ p0 f => fun p => cons (concat p p0) f
  end p.

您可以在Adam Chlipala的CPDT书中了解有关护航模式的更多信息。

答案 1 :(得分:1)

(* You could use Program *)

Parameter Object : Set.
Parameter Arrow : Object -> Object -> Set.

Inductive path : Object -> Object -> Set :=
  emptyPath : forall X, path X X
| full : forall {A B C} (p : path A B) (f : Arrow B C), path A C.

Require Import Coq.Program.Program.

Program Fixpoint concat {A B C : Object} (p : path A B) (p' : path B C) :  path A C :=
  match p' with
    | emptyPath _ => p
    | full _ _ _ p0 f => full (concat p p0) f
  end. 

(* Unfortunately, I don't think Program produces an easy way to unfold
or simpl the definitions it produces, so programs about them are
difficult. *)

(* You could also switch to an unyped representation. I find this
method much easier to reason about. *)

Definition sArrow := prod Object Object.

Definition spath := list sArrow.

Fixpoint bound (xs:spath) a b :=
  match xs with
    | nil => a = b
    | cons y ys =>
      a = fst y /\
      bound ys (snd y) b
  end.

Lemma concatBound : forall xs a b c ys (top:bound xs a b), bound ys b c -> bound (xs ++ ys)%list a c.
induction xs; intros.
{
  unfold app; unfold bound in *; subst; assumption.
}
{
  simpl in *.
  destruct top; split; subst; auto.
  eapply IHxs; solve [eauto].
}
Qed.