不同类型的转换

时间:2019-10-16 06:30:04

标签: coq

 Inductive color: Type :=
 | red 
 | green
 | blue.
Inductive listw : Type :=
  | nil : listw
  | cons : web -> listw -> listw.
Definition colorlist (c:color) : listw :=
  match w with 
  | red => (cons red nil)
  | green => (cons green nil)
  | blue => (cons blue nil)
end.
Notation "x ++ y" := (app x y)
                     (right associativity, at level 60).
Definition bag:=listw.
Definition bag:=((cons red nil)++(cons green nil)++(cons blue nil)).
Bag = cons red (cons green (cons blue nil)).

我已经定义了颜色列表(归纳类型)。想要用特定的自然数替换每种颜色。将自然数分配给其他归纳类型列表时遇到问题。

1 个答案:

答案 0 :(得分:1)

我很难弄清楚你到底想要什么,所以我会尽力写出答案。

您有一种颜色:

Inductive color := 
| red
| green
| blue.

然后,您要从中获取自然数列表。因此,您将数字与每种颜色相关联:

Definition nat_of_color (c : color) : nat :=
  match c with
  | red => 0
  | green => 1
  | blue => 2
  end.

现在,如果您要使用coq提供的list类型,则所需的功能是:

Definition nat_list_of_color_list (l : list color) : list nat :=
  List.map nat_of_color l.

换句话说,List.map nat_of_color是您似乎想要的功能。

请注意,排序算法和出现次数可能更通用,可以立即使用颜色。