coq模块中的默认实现

时间:2014-06-18 04:59:03

标签: module coq

我有一个我想多次实现的界面:

Module Type I.
  Parameter a : A.
  Parameter b : B.
  Parameter c : C.
End I.

(并假设abc中的每一个实际上都有很多定义。)

实施将是

Module Imp1 <: I.
  Definition a : A := bar.
  Definition b : B := foo a.
  Definition c : C := baz a b.
End I.

现在事实证明,许多实现共享b的定义(需要a),但对c的定义不同。

如何集中b的定义?最好不改变I或复制其中的许多定义?

(我想编写一个模块仿函数BImp期待a:A作为某种参数,然后我可以Import (BImp a)。)

2 个答案:

答案 0 :(得分:1)

您可以将共享定义外包到模块更改部分(此处为outsourced)参数化的全局定义(此处为a)。我不知道是否有类似Haskell的默认实现。

Module Type I.
  Parameter a : A.
  Parameter b : B.
  Parameter c : C.
End I.

Definition outsourced (a:A) := foo a.

Module Imp1 <: I.
  Definition a : A := bar.
  Definition b : B := outsourced a.
  Definition c : C := baz a b.
End Imp1.

Module Imp2 <: I.
  Definition a : A := bar'.
  Definition b : B := outsourced a.
  Definition c : C := baz' a b.
End Imp2.

答案 1 :(得分:0)

您可以在其他模块中实现模块。这迫使你复制部分模块&#39;签名,但不是模块包含的证明。

Module Type PreorderSignature.
Parameter Inline type : Type.
Parameter Inline less : type -> type -> Prop.
Parameter Inline reflexivity : forall x1, less x1 x1.
Parameter Inline transitivity : forall x1 x2 x3, less x1 x2 -> less x2 x3 -> less x1 x3.
End PreorderSignature.

Module Preorder (PS : PreorderSignature).
Import PS.
(* Preorder facts. *)
End Preorder.

Module Type EquivalenceRelationSignature.
Parameter Inline type : Type.
Parameter Inline equal : type -> type -> Prop.
Parameter Inline reflexivity : forall x1, equal x1 x1.
Parameter Inline symmetry : forall x1 x2, equal x1 x2 -> equal x2 x1.
Parameter Inline transitivity : forall x1 x2 x3, equal x1 x2 -> equal x2 x3 -> equal x1 x3.
End EquivalenceRelationSignature.

Module EquivalenceRelation (ERS : EquivalenceRelationSignature).
Import ERS.
Module PreorderSignatureInstance <: PreorderSignature.
Definition type := type.
Definition less := equal.
Definition reflexivity := reflexivity.
Definition transitivity := transitivity.
End PreorderSignatureInstance.
Module PreorderInstance := Preorder PreorderSignatureInstance.
Import PreorderInstance.
(* Now your equivalence relations will inherit all the facts about preorders. *)
(* Other equivalence relation facts. *)
End EquivalenceRelation.

Module Type PartialOrderSignature.
Parameter Inline type : Type.
Parameter Inline less : type -> type -> Prop.
Parameter Inline reflexivity : forall x1, less x1 x1.
Parameter Inline antisymmetry : forall x1 x2, less x1 x2 -> less x2 x1 -> x1 = x2.
Parameter Inline transitivity : forall x1 x2 x3, less x1 x2 -> less x2 x3 -> less x1 x3.
End PartialOrderSignature.

Module PartialOrder (POS : PartialOrderSignature).
Import POS.
Module PreorderSignatureInstance <: PreorderSignature.
Definition type := type.
Definition less := less.
Definition reflexivity := reflexivity.
Definition transitivity := transitivity.
End PreorderSignatureInstance.
Module PreorderInstance := Preorder PreorderSignatureInstance.
Import PreorderInstance.
(* Now your partial orders will inherit all the facts about preorders. *)
(* Other partial order facts. *)
End PartialOrder.

要稍微展平模块层次结构,您可以使用ImportParameter Inline命令。