7

私は「純粋に応用的」と私が呼んでいるものEither、つまりEitherApplicativeインスタンスを実装しない限りインスタンスが利用可能であるという用途を頻繁に見つけますMonad

newtype AEither e a = AEither { unAEither :: Either e a }
  deriving Functor

-- technically we only need Semigroup
instance Monoid e => Applicative (AEither e) where
  pure a = AEither (pure a)
  AEither e1 <*> AEither e2 = AEither (combine e1 e2) where
    combine (Right f) (Right a) = Right (f a)
    combine (Left m1) (Left m2) = Left (m1 <> m2)
    combine (Left m ) _         = Left m
    combine _         (Left m ) = Left m

のインスタンスApplicativeよりも強力な「エラーの要約」の概念を提供するため、非常に便利です。そのために、何度も何度も実装しています。EitherMonad

どこかに標準インスタンスはありますか?標準的な名前もありますか?

4

1 に答える 1

7

validationこれは、パッケージの AccValidation タイプとよく似ています: http://hackage.haskell.org/package/validation-0.3.1/docs/Data-Validation.html

編集:

特に、次のインスタンス宣言:

instance Semigroup err => Apply (AccValidation err) where
  AccFailure e1 <.> AccFailure e2 =
    AccFailure (e1 <> e2)
  AccFailure e1 <.> AccSuccess _  =
    AccFailure e1
  AccSuccess _  <.> AccFailure e2 =
    AccFailure e2
  AccSuccess f  <.> AccSuccess a  =
    AccSuccess (f a)
于 2014-03-08T03:04:17.350 に答える