我刚看到有人用不熟悉的语法在Coq中定义了一个归纳类型,如下所示:
Inductive nat_tree : Type :=
| NatLeaf
| NatNode of color & nat_tree & nat & nat_tree.
我习惯的语法看起来像this:
Inductive ident : sort :=
ident1 : type1
| …
| identn : typen
任何人都可以解释一下新语法是什么吗?
顺便说一下,这是来自ssrflect教程。而且我想知道它是否是ssr的补充。答案 0 :(得分:3)
是的,你是对的:这个语法是由Ssreflect定义的。两者都被定义为用于声明匿名参数的语法糖:of T
和& T
表示(_ : T)
;也就是说,T
类型的未命名参数。因此,nat_tree
的定义等同于下面的定义。
Inductive nat_tree :=
| NatLeaf
| NatNode (_ : color) (_ : nat_tree) (_ : nat) (_ : nat_tree).
您也可以为每个参数指定名称:
Inductive nat_tree :=
| NatLeaf
| NatNode (c : color) (t1 : nat_tree) (n : nat) (t2 : nat_tree).
正如加莱指出的那样,这使得Coq中数据类型声明的语法更类似于OCaml' s。请注意,上面的声明不提供每个构造函数的返回类型。在标准Coq中,当使用此语法给出所有参数时,以及当定义的类型为 uniform 时,指定返回类型是可选的。这意味着我们可以将list
类型定义为
Inductive list (T : Type) :=
| nil
| cons (t : T) (l : list T).
但是需要定义长度索引列表的类型,如下所示(因为nat
索引):
Inductive vector (T : Type) : nat -> Type :=
| vnil : vector T O
| vcons (n : nat) (t : T) (v : vector T n) : vector T (S n).