我认为流行的解决方案是将函子编码为“容器”,这篇论文的介绍是一个很好的起点:https ://arxiv.org/pdf/1805.08059.pdf这个想法要老得多(论文的意思是给出一个自包含的解释),你可以从那篇论文中寻找参考资料,但如果你不熟悉类型论或范畴论,我在粗略的搜索中发现的内容可能很难理解。
简而言之,Type -> Type我们使用以下类型来代替 :
Set Implicit Arguments.
Set Contextual Implicit.
Record container : Type := {
shape : Type;
pos : shape -> Type;
}.
F粗略地,如果你想象一个递归类型的“基本函子” Fix F,shape描述 的构造函数F,并且对于每个构造函数,pos枚举其中的“孔”。所以基函子List
data ListF a x
= NilF -- no holes
| ConsF a x -- one hole x
由这个容器给出:
Inductive list_shape a :=
| NilF : list_shape a
| ConsF : a -> list_shape a.
Definition list_pos a (s : list_shape a) : Type :=
match s with
| NilF => False (* no holes *)
| ConsF _ => True (* one hole x *)
end.
Definition list_container a : container := {|
shape := list_shape a;
pos := fun s => list_pos s;
|}.
关键是这个容器描述了一个严格的正函子:
Inductive ext (c : container) (a : Type) : Type := {
this_shape : shape c;
this_rec : pos c this_shape -> a;
}.
Definition listF a : Type -> Type := ext (list_container a).
因此Fix f = f (Fix f),fixpoint 构造可以取一个容器,而不是 :
Inductive Fix (c : container) : Type := MkFix : ext c (Fix c) -> Fix c.
并非所有仿函数都可以编码为容器(延续仿函数就是一个典型的例子),但你不会经常看到它们与Fix.
完整要点:https ://gist.github.com/Lysxia/21dd5fc7b79ced410b129f31ddf25c12