Coq:导入有关实例的信息

时间:2017-07-17 13:47:07

标签: typeclass implicit coq

Section Definitions.

  Definition eq_dec X := forall x y : X, {x=y} + {x <> y}.
  Existing Class eq_dec.

  (* Any function that uses eq_dec. Doesn't matter -- ↓ ↓ ↓ *)
  Definition f {X: Type} {DecX: eq_dec X} (x y: X) := x = y.

End Definitions.

Section MySection.

  Context {T: Type}.
  Hypothesis TEqDec: eq_dec T.

  Inductive myType :=
  | C: T -> myType.

  Instance myTypeEqDec : eq_dec myType.
  Proof. ... Defined.

  (* Everything is ok *)
  Example example1: forall (t1 t2: myType), f t1 t2.
  Proof. ... Qed.

End MySection.

Section AnotherSection.

  Context {T: Type}.
  Hypothesis TEqDec: eq_dec T.

  (*          Now I must explicitly specify this -- ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 
  Example example2: forall (t1 t2: @myType T), @f _ (@myTypeEqDec _ TEqDec) t1 t2.
  Proof. ... Qed.

End AnotherSection.

正如您在example1中看到的,Coq能够找到有关myType的实例信息。但是在我更改了部分之后,有关实例的所有信息都消失了,我必须明确指定它。因此,当我有许多类型类和实例时,代码会迅速变得混乱。显然,我应该以某种方式将这些信息反馈到上下文中。这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

只需将Global修饰符添加到您的实例声明中,如下所示:

Global Instance myTypeEqDec : eq_dec myType.
(* ... *)

reference manual中所述,

  

可以在节中声明的实例上使用Global修饰符,以便在节关闭后自动重新声明它们的泛化。