英文:
Set type Variable to a concrete type inside a definition
问题
在Coq中,我们定义如下:
Section bin_trees.
Variable T: Type.
Inductive bin_tree :=
| Leaf : bin_tree
| Node : T -> bin_tree -> bin_tree -> bin_tree.
在bin_trees
部分内,我想定义一个示例树:
Definition my_nat_bin_tree := Node 3 Leaf Leaf.
但这是不允许的:
Error: In environment
T : Type
The term "3" has type "nat"
while it is expected to have type
"T".
我是否可以将T
设置为某个具体的类型(在这种情况下是nat
)以用于单个定义的目的?类似这样的方式:
Definition my_nat_bin_tree := @Node (T:=nat) 3 Leaf Leaf.
或者也许有一种技巧可以在定义后关闭并重新打开部分,以便我可以引用T
变量而无需重新声明它?
英文:
Say that we define in Coq:
Section bin_trees.
Variable T: Type.
Inductive bin_tree :=
| Leaf : bin_tree
| Node : T -> bin_tree -> bin_tree -> bin_tree.
Within bin_trees
section, I would like to define some sample tree:
Definition my_nat_bin_tree := Node 3 Leaf Leaf.
But this is not allowed:
Error: In environment
T : Type
The term "3" has type "nat"
while it is expected to have type
"T".
Can I set T
to some concrete type (in this case nat
) for the purposes of a single definition? Something like:
Definition my_nat_bin_tree := @Node (T:=nat) 3 Leaf Leaf.
Or maybe there is a trick to close and reopen the section after the definitions so that I can refer to T
variable without redeclaring it?
答案1
得分: 1
你可能想关闭这一节。但这并不意味着你必须在每个地方都提供显式的 T。
这段代码展示了如何使用 Arguments
命令将 T
设为隐式。
Section bin_trees.
Variable T : Type.
Inductive bin_tree :=
| Leaf : bin_tree
| Node : T -> bin_tree -> bin_tree -> bin_tree.
End bin_trees.
(* 在 section 外部,T 是 Leaf、Node 和 bin_tree 的参数。 *)
Check Leaf : forall T : Type, bin_tree T.
Check Node : forall T : Type, T -> bin_tree T -> bin_tree T -> bin_tree T.
(* 你可以告诉 Coq 从上下文中推断出 T。 *)
Arguments Node {T}.
Arguments Leaf {T}.
(* 由于 3 是 nat,所以 T 没有其他选择。 *)
Definition my_nat_bin_tree := Node 3 Leaf Leaf.
Check my_nat_bin_tree : bin_tree nat.
Definition my_bool_bin_tree := Node false (Node true Leaf Leaf) Leaf.
Check my_bool_bin_tree : bin_tree bool.
(* 现在如果想要一个类型为 nat 的 Leaf *)
Fail Definition my_nat_leaf := Leaf. (* 不工作:
- ?T: 无法推断出 Leaf 的隐式参数 T,其类型为 "Type"。 *)
(* 但我们可以在本地使用 @ 操作符使 T 变为非显式 *)
Definition my_nat_leaf := @Leaf nat.
Check my_nat_leaf : bin_tree nat.
英文:
You probably want to close the section anyway. But that does not mean you have to provide an explicit T everywhere.
This code shows how to use the Arguments
command to make T
implicit.
Section bin_trees.
Variable T : Type.
Inductive bin_tree :=
| Leaf : bin_tree
| Node : T -> bin_tree -> bin_tree -> bin_tree.
End bin_trees.
(* Outside the section, T is a parameter for Leaf, Node and bin_tree. *)
Check Leaf : forall T : Type, bin_tree T.
Check Node : forall T : Type, T -> bin_tree T -> bin_tree T -> bin_tree T.
(* You can tell coq to deduce T from the context. *)
Arguments Node {T}.
Arguments Leaf {T}.
(* Since 3 is in nat, there is no other choice for T. *)
Definition my_nat_bin_tree := Node 3 Leaf Leaf.
Check my_nat_bin_tree : bin_tree nat.
Definition my_bool_bin_tree := Node false (Node true Leaf Leaf) Leaf.
Check my_bool_bin_tree : bin_tree bool.
(* Now if one really wants some Leaf of type, say nat *)
Fail Definition my_nat_leaf := Leaf. (* does not work :
- ?T: Cannot infer the implicit parameter T of Leaf whose type is "Type". *)
(* But we can "unexplicit" T locally with the @ operator *)
Definition my_nat_leaf := @Leaf nat.
Check my_nat_leaf : bin_tree nat.
About the section, actually if T is not that much used you can also directly give the type as a parameter as in :
Inductive bin_tree' (T : Type) :=
| Leaf' : bin_tree' T
| Node' : T -> bin_tree' T -> bin_tree' T -> bin_tree' T.
答案2
得分: 0
一个你可以使用的技巧是使用符号来在本地实例化一个定义。
Section Top.
Inductive bin_tree (T : Type) :=
| Leaf : bin_tree T
| Node : T -> bin_tree T -> bin_tree T -> bin_tree T.
Section bin_trees.
Variable T : Type.
Notation bin_tree := (bin_tree T).
Notation Leaf := (Leaf T).
Notation Node := (Node T).
Check bin_tree.
Check Leaf.
Check Node.
Definition my_nat_bin_tree := Top.Node _ 3 (Top.Leaf _) (Top.Leaf _).
Print my_nat_bin_tree.
End bin_trees.
Print my_nat_bin_tree.
End Top.
Print my_nat_bin_tree.
英文:
A trick you can use is to use notation to locally instanciate a definition.
Section Top.
Inductive bin_tree (T : Type) :=
| Leaf : bin_tree T
| Node : T -> bin_tree T -> bin_tree T -> bin_tree T.
Section bin_trees.
Variable T : Type.
Notation bin_tree := (bin_tree T).
Notation Leaf := (Leaf T).
Notation Node := (Node T).
Check bin_tree.
Check Leaf.
Check Node.
Definition my_nat_bin_tree := Top.Node _ 3 (Top.Leaf _) (Top.Leaf _).
Print my_nat_bin_tree.
End bin_trees.
Print my_nat_bin_tree.
End Top.
Print my_nat_bin_tree.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论