我想定義一個帶有變量扇出的加權樹,它在類型上是多態的。我想出了這個:Coq中的子類型多態性
(* Weighted tree with topological ordering on the nodes. *)
Inductive wtree (A : Type) : Type :=
LNode : A->wtree A
| INode : A->list (R*wtree A) -> wtree A.
不過,我寧願重量存儲在一個類型,是這樣的:
Inductive Wtype (A : Type) : Type := W : R->A->Wtype A.
Inductive wtree (A : Wtype) : Type :=
LNode : A->wtree A
| INode : A->list (wtree A) -> wtree A.
其中R
是標準庫中的實數集。
這不起作用,因爲Wtype
是Type->Type
,而不是Type
,但我無法弄清楚如何做到這一點。不幸的是,我仍然住在面向對象的土地上,我真的只想給A
一個更具限制性的超類型,而不是Type
,但只是不知道如何在Coq中做到這一點。