矩阵应用型仿,这是不单子 [英] Matrix as Applicative functor, which is not Monad

查看:139
本文介绍了矩阵应用型仿,这是不单子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰上<一个href=\"http://stackoverflow.com/questions/7220436/good-examples-of-not-a-functor-functor-applicative-monad\">examples Applicatives的是不单子。我喜欢多维数组的例子,但我并没有完全得到它。

I run into examples of Applicatives that are not Monads. I like the multi-dimensional array example but I did not get it completely.

让我们一起来矩阵 M [A] 。你能证明 M [A] 应用型而不是单子使用Scala code?你有使用矩阵为 Applicatives

Let's take a matrix M[A]. Could you show that M[A] is an Applicative but not a Monad with Scala code ? Do you have any "real-world" examples of using matrices as Applicatives ?

推荐答案

类似 M [T]&LT; * GT; M [T =&GT; C] 是应用性:

val A = [[1,2],[1,2]] //let's assume such imaginary syntax for arrays
val B = [[*2, *3], [*5, *2]]

A <*> B === [[2,6],[5,4]]

有可能在信号处理更为复杂applicatives例如。使用applicatives允许你建立的一个函数矩阵(每做N以下元素的操作),并只做1矩阵运算,而不是N.

There may be more complex applicatives in signal processing for example. Using applicatives allows you to build one matrix of functions (each do N or less element-operations) and do only 1 matrix-operation instead of N.

Matrix是不是一个独异的定义 - 你必须定义+矩阵之间为(串联)(折叠更precisely)。而不是每一个(甚至monoidal)矩阵是一个单子 - 你必须additionaly定义 FMAP (不是 flatMap - 只要地图斯卡拉),使之成为函子(如果返回矩阵)内切仿函数。但默认情况下矩阵不是仿函数/含半幺群/单子(仿函数+含半幺群)。

Matrix is not a monoid by definition - you have to define "+" (concatenation) between matrixes for that (fold more precisely). And not every (even monoidal) matrix is a monad - you have to additionaly define fmap (not flatMap - just map in scala) to make it a Functor (endo-functor if it returns matrix). But by default Matrix isn't Functor/Monoid/Monad(Functor + Monoid).

关于一元矩阵。矩阵可以幺:您可以定义维度结合串联为沿垂直尺寸同样大小的矩阵。尺寸/尺寸无关级联会是这样的:

About monadic matrixes. Matrix can be monoid: you may define dimension-bound concatenation for matrixes that are same sized along the orthogonal dimension. Dimension/size-independent concatenation will be something like:

val A = [[11,12],[21,22]]; val B = [[11,12,13],[21,22,23],[31,32,33]]
A + B === [[11,12,0,0,0], [21,22,0,0,0], [0,0,11,12,13],[0,0,21,22,23],[0,0,31,32,33]

身份元素将是 []

所以,你也可以建立单子(伪$ C $再次C):

So you can also build the monad (pseudocode again):

def flatMap[T, U](a: M[T])(f: T => M[U]) = {
    val mapped = a.map(f)// M[M[U]] // map
    def normalize(xn: Int, yn: Int) = ... // complete matrix with zeros to strict xn * yn size
    a.map(normalize(a.max(_.xn), a.max(_.yn)))
     .reduceHorizontal(_ concat _)
     .reduceVertical(_ concat _) // flatten
}

val res = flatMap([[1,1],[2,1]], x => if(x == 1)[[2,2]] else [[3,3,3]])

res === [[2,2,0,2,2],[3,3,3,2,2]]

不幸的是,你必须有对于T零元素(或任何默认)(不仅是独异本身)。它没有将T本身某种岩浆(因为需要为这套没有明确的二元运算 - 只有一些常量为T定义),但可能会产生其他问题(根据您的挑战)。

Unfortunately, you must have zero-element (or any default) for T (not only for monoid itself). It doesn't make T itself some kind of magma (because no defined binary operation for this set is required - only some const defined for T), but may create additional problems (depending on your challenges).

这篇关于矩阵应用型仿,这是不单子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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