简体   繁体   English

适用于将效果顺序颠倒的仿函数

[英]Applicative functor that reverses order of effects

Given an applicative functor f , I had an idea of making a new applicative functor Rev f like f but with the order of effects reversed. 给定一个应用程序的函子f ,我有一个想法,就像创建一个新的应用函数Rev f f但效果的顺序相反。 Here it is: 这里是:

import Control.Applicative

newtype Rev f a = Rev {unRev :: f a}

instance Functor f => Functor (Rev f) where
  fmap f (Rev fx) = Rev (fmap f fx)

instance Applicative f => Applicative (Rev f) where
  pure x = Rev (pure x)
  (Rev ff) <*> (Rev fx) = Rev (pure (\x f -> f x) <*> fx <*> ff)

My questions are 我的问题是

  1. Is that a valid Applicative instance (does it obey the Applicative laws)? 这是一个有效的Applicative实例(它是否遵守Applicative法律)?
  2. Does this construction have a name? 这个结构有名字吗? Is there a module somewhere this is hiding in? 这个模块隐藏在哪里?

The friendly folks on IRC pointed to the Backwards applicative offered by the transformers package. IRC的友好人员指出transformers包提供的Backwards应用程序。 You may also like the (<**>) operator available in the standard library. 您可能也喜欢标准库中提供的(<**>)运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM