根据规范约束向量的类型

发布于 2025-01-20 04:38:11 字数 107 浏览 5 评论 0原文

在像 Haskell 或 Idris 这样的语言中,是否可以创建一种数据类型来检查函数的输入向量是否是单一的?也就是说,我可以创建一个数据类型 SumsToOne 来检查向量的总和是否等于 1 等吗?

In a language like Haskell or Idris, is it possible to create a data type which checks if an input vector for a function is, for instance, unitary? That is, can I create a data type SumsToOne which checks if a vector adds up to 1, etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2025-01-27 04:38:11

是的。在伊德里斯,就像你说的那样。您可以用证明包装一个列表,其中元素之和为一。

data SumsToOne : Type where
  STO : (xs : List Int) -> {auto prf : sum xs = 1} -> SumsToOne

但是,如果通过数据类型您指的是证明,而不是元素,则您可以只

go : (xs : List Int) -> {auto prf : sum xs = 1} -> <whatever you're returning>

证明是 sums xs = 1 类型prf 只是函数参数名称,auto 表示 Idris 会在可以的情况下为您找到证明。

我不知道这是否适用于浮点元素。

Yes. In Idris it's kind of just as you say. You could wrap a list with a proof the elements sum to one

data SumsToOne : Type where
  STO : (xs : List Int) -> {auto prf : sum xs = 1} -> SumsToOne

Though if by data type you are referring to the proof, rather than the elements as well, you can just

go : (xs : List Int) -> {auto prf : sum xs = 1} -> <whatever you're returning>

The proof is of type sums xs = 1. prf is just the function parameter name, and auto means Idris will find the proof for you if it can.

I don't know if this will work for floating point elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文