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)).
我已经定义了颜色列表(感应类型)。想用特定的自然数替换每种颜色。我在将自然数分配给不同的归纳类型列表时遇到问题。
解决方案
我很难弄清楚你到底想要什么,所以我会试着为我所知道的写一个答案。
你有一种颜色:
Inductive color :=
| red
| green
| blue.
然后你想从中得到一个自然数列表。因此,您将数字与每种颜色相关联:
Definition nat_of_color (c : color) : nat :=
match c with
| red => 0
| green => 1
| blue => 2
end.
现在,如果你使用list
coq 提供的类型,你想要的函数是:
Definition nat_list_of_color_list (l : list color) : list nat :=
List.map nat_of_color l.
换句话说,List.map nat_of_color
是您似乎想要的功能。
请注意,排序算法和出现计数可能会变得更通用,以便立即使用颜色。
推荐阅读
- java - Spring Data:: 使存储库方法在服务接口中可用
- javascript - 简化 forEach 语句以使用嵌套对象和数组更新 mongoDB 文档
- unity3d - 统一所有对象具有相同的位置
- php - Laravel 5.7 上传到公用文件夹不起作用
- docker - .net core web api 应用程序与 docker 中的 https
- css - :active 和 :focus 是否应用于鼠标单击?-CSS
- javascript - 如何在每行颜色不同的情况下进行html文本输入
- jquery - IE 11 WMV 视频在导航上留下黑框
- c# - 如何将事件从基于 Laravel 的服务器广播到基于 .net C# 的客户端?
- c# - C# 运算符“==”不能应用于泛型类中“TKey”和“TKey”类型的操作数