Trouble with Coq type classes when defining recursive dependent fields

时间:2017-08-30 20:34:40

标签: recursion typeclass coq

I am trying to define a set of descriptions and their interpretation into Coq Types, and this is what I came up with so far:

currentContext

When declaring the definesPresentationContext instance, I initially wanted to define it as:

(need some text here so that SO will format the next code block :(...)

.custom

But Coq would not let me. It seems to me that, it tries to resolve those calls to denote as some other instance that it can't find, while really they were meant to be a recursive call to the instance being defined.

Is there any convincing that will let me write this instance without the explicit Class Desc (D : Type) := { denotesInto : D -> Type ; denote : forall (d : D), denotesInto d }. Notation "⟦ d ⟧" := (denote d). Inductive TypeD : Type := | ArrowD : TypeD -> TypeD -> TypeD | ListD : TypeD -> TypeD | NatD : TypeD . Global Instance Desc_TypeD : Desc TypeD := { denotesInto := fun _ => Type ; denote := fix go d := match d with | ArrowD dL dR => (go dL) -> (go dR) | ListD dT => list (go dT) | NatD => nat end }. ?

Thanks!

1 个答案:

答案 0 :(得分:2)

很难知道fix为什么在不了解更多关于你的背景的情况下困扰你。一种接近你想要的东西的方法是打开你的TypeD,但是,这肯定会有其他缺点:

Class Desc (D : Type) :=
  { denote : forall (d : D), Type }.

Notation "⟦ d ⟧" := (denote d).

Inductive TypeD (D : Type) : Type :=
| ArrowD : D -> D -> TypeD D
| ListD  : D -> TypeD D
| NatD   : TypeD D
.

Global Instance Desc_TypeD D `{DI : Desc D} : Desc (TypeD D) :=
  { denote := fun d =>
      match d with
      | ArrowD _ dL dR => ⟦dL⟧ -> ⟦dR⟧
      | ListD _ dT     => list ⟦dT⟧
      | NatD _         => nat
      end
  }.

请注意,我们还需要使denote类型更加通用,因为我们无法获得有关参数D的足够信息。