簡體   English   中英

Coq:設置默認的隱式參數

[英]Coq: set default implicit parameters

假設我有一個包含許多模塊和部分的代碼。 在其中一些中,有多態定義。

Module MyModule.

   Section MyDefs.

      (* Implicit. *)
      Context {T: Type}. 

      Inductive myIndType: Type :=
      | C : T -> myIndType.

   End MyDefs.

End MyModule.

Module AnotherModule.

   Section AnotherSection.

      Context {T: Type}.
      Variable P: Type -> Prop.

      (*              ↓↓         ↓↓ - It's pretty annoying. *)
      Lemma lemma: P (@myIndType T).

   End AnotherSection.

End AnotherModule.

通常,Coq可以推斷出類型,但是通常我仍然會遇到輸入錯誤。 在這種情況下,您必須使用@顯式指定隱式類型,這會破壞可讀性。

無法推斷類型為“類型”的_的隱式參數_。

有辦法避免這種情況嗎? 是否可以指定類似默認參數的內容,每次Coq不能猜測類型時都將替換該默認參數?

您可以使用類型類來實現此默認值概念:

Class Default (A : Type) (a : A) :=
  withDefault { value : A }.

Arguments withDefault {_} {_}.
Arguments value {_} {_}.

Instance default (A : Type) (a : A) : Default A a :=
  withDefault a.

Definition myNat `{dft : Default nat 3} : nat :=
  value dft.

Eval cbv in myNat.
(* = 3 : nat *)
Eval cbv in (@myNat (withDefault 5)).
(* = 5 : nat *)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM