gpt4 book ai didi

coq - 从 Coq 的定义中返回一条记录

转载 作者:行者123 更新时间:2023-12-04 21:23:40 26 4
gpt4 key购买 nike

假设我有一个包含两个 natsrecord

Record toy := {
num1 : nat;
num2 : nat
}.

我想建立一个定义,给定两个 nats 返回一个包含这两个 natsrecord

Definition args_to_toy_record (n1 : nat) (n2 : nat) : toy := 
(* {num1 = n1; num2 = n2} ?? *)

不幸的是,官方文档似乎只涵盖了返回类型为 boolnat 的简单情况。在 coq 中这样的事情是可能的吗?如果是,实现它的最佳方法是什么?

谢谢

最佳答案

你说得差不多了。你只需要稍微不同的语法:

Record toy := {
num1 : nat;
num2 : nat
}.

Definition args_to_toy_record (n1 : nat) (n2 : nat) : toy :=
{| num1 := n1; num2 := n2 |}.

或者,您可以使用常规构造函数语法。 Coq 中的每条记录 toto 都被声明为具有单个构造函数 Build_toto 的归纳(有时是协合)类型,其参数正是记录的字段:

Definition args_to_toy_record (n1 : nat) (n2 : nat) : toy :=
Build_toy n1 n2.

您也可以显式命名记录构造函数,如下所示:

Record toy := Toy {
num1 : nat;
num2 : nat
}.

Definition args_to_toy_record (n1 : nat) (n2 : nat) : toy :=
Toy n1 n2.

关于coq - 从 Coq 的定义中返回一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45650879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com