The applicative-fail package

[ Tags: bsd3, control, library ] [ Propose Tags ]

Applicative functor to perform parse-like actions and collect warnings/failures.

[Skip to Readme]


Versions 0.0.1, 0.0.2, 0.0.3, 1.0.0, 1.1.0, 1.1.1
Change log
Dependencies base (>=4.6 && <5), bifunctors, dlist, mtl, transformers, transformers-base [details]
License BSD3
Author Aleksey Uimanov
Category Control
Home page
Source repo head: git clone
Uploaded Mon Aug 3 07:25:18 UTC 2015 by AlekseyUymanov
Distributions NixOS:1.1.1
Downloads 1693 total (51 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-08-03 [all 1 reports]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees

Readme for applicative-fail-1.1.1

[back to package description]

What is it for?

Assume you have some type

data Animal = Animal
    { species :: String
    , weight  :: Double
    , age     :: Int
    } deriving (Show)

And you would like to produce this value from some data (e.g. query parameters). There can be some warnigns or value can not be produced at all. It would be great to have some simple tool to notify about warnings and/or fail computation.

Like that:

let spc = "Parastratiosphecomyia stratiosphecomyioides"
    w = 100
    a = 27234
    animal :: Fail [String] Animal
    animal = Animal
             <$> (if length spc > 20
                  then awarn "Name is too long" spc
                  else if spc == ""
                       then afail "Name can not be empty"
                       else pure spc)
             <*> (if w < 0
                  then afail "Weight can not be negative"
                  else pure w)
             <*> (if a < 0
                  then afail "Age can not be negative"
                  else pure a)

Now you can inspect the value we have got

>>> animal
Fail ["Name is too long"] (Just (Animal {species = "Parastratiosphecomyia stratiosphecomyioides", weight = 100.0, age = 27234}))

>>> getSucc animal
Just (Animal {species = "Parastratiosphecomyia stratiosphecomyioides", weight = 100.0, age = 27234})

>>> getFail animal
Just ["Name is too long"]

Now this example can be rewritten with monadic syntax inside field checkers using module Control.Monad.Fail:

let animal :: Fail [String] Animal
    animal = Animal
             <$> (runFailI $ do
                          when (length spc > 20) $ mwarn "Name is too long"
                          when (spc == "") $ mfail "Name can not be empty"
                          return spc)
             <*> (runFailI $ do
                          when (w < 0) $ mfail "Weight can not be negative"
                          return w)
             <*> (runFailI $ do
                          when (a < 0) $ mfail "Age can not be negative"
                          return a)