类型安全的矩阵乘法 [英] Type-safe matrix multiplication

查看:158
本文介绍了类型安全的矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长篇大论的讨论之后,在Haskell中写下这个Scala矩阵乘法,我仍然想知道......类型安全的矩阵乘法是什么样的?所以这里是你的挑战:或者链接到一个Haskell实现,或者实现你自己,如下所示:

 数据Matrix ... =。 .. 

matrixMult :: Matrix ... - >矩阵... - >矩阵...
matrixMult ... = ...

其中<$ c $如果您尝试将两个基数乘以不兼容的维,c> matrixMult 会在编译时 时产生类型错误。布朗尼指出,如果你链接到讨论这个确切主题的论文或书籍,并且/或者讨论自己这个功能是多么有用/无用。

有很多实现这个功能的软件包:



非常好的讨论设计空间和选择: http://repa.ouroborus.net/



历史利益是McBride的欺骗它从2001年描述强类型的载体。他采用的技术与上述软件包中使用的技术非常相似。他们很明显是在做依赖类型编程的圈子里,但我的印象是,伪造它这篇文章是早期在Haskell中使用它的例子之一。 Oleg的 2005 Monad Reader中关于数字参数化类型的文章有一些很好的讨论这些技术的历史也是如此。

After the long-winded discussion at Write this Scala Matrix multiplication in Haskell, I was left wondering...what would a type-safe matrix multiplication look like? So here's your challenge: either link to a Haskell implementation, or implement yourself, the following:

data Matrix ... = ...

matrixMult :: Matrix ... -> Matrix ... -> Matrix ...
matrixMult ... = ...

Where matrixMult produces a type error at compile time if you try to multiply two matricies with incompatible dimensions. Brownie points if you link to papers or books that discuss this precise topic, and/or discuss yourself how useful/useless this functionality is.

解决方案

There are a number of packages that implement this:

The Repa papers in particular have a really nice discussion of the design space and choices made: http://repa.ouroborus.net/

Of historical interest is McBride's "Faking It" from 2001 which describes strongly typed vectors. The techniques he employs are fairly similar to those used in the above packages. They were obviously known in circles doing dependently typed programming, but my impression is that the "Faking It" paper is one of the earlier instances where these were used in Haskell. Oleg's 2005 Monad Reader article on number-parameterized types has some good discussion on the history of these techniques as well.

这篇关于类型安全的矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆