packages feed

cautious (empty) → 0.0.0.0

raw patch · 12 files changed

+297/−0 lines, 12 filesdep +QuickCheckdep +aesondep +basesetup-changed

Dependencies added: QuickCheck, aeson, base, cautious, genvalidity, genvalidity-hspec, genvalidity-hspec-aeson, hspec, hspec-discover, transformers, validity

Files

+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for cautious++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Author name here nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,4 @@+# Cautious++Cautious provides a monad Cautious w e, which keeps track of warnings `w` and errors `e` during calculations.+CautiousT w e m is a MonadTransform based on Cautious.
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ cautious.cabal view
@@ -0,0 +1,67 @@+name: cautious+version: 0.0.0.0+cabal-version: >=1.10+build-type: Simple+license: BSD3+license-file: LICENSE+copyright: 2018 Nick Van den Broeck+maintainer: nick.van.den.broeck666@gmail.com+homepage: https://github.com/Nickske666/cautious#readme+bug-reports: https://github.com/Nickske666/cautious/issues+synopsis: Keep track of warnings and errors during calculations.+description:+    A Cautious monad "Monoid w => Cautious w e a" which keeps track of the success of a task. The options are "CautiousWarning w a" (where "CautiousWarning mempty a" represents "success") and "CautiousError e". In addition, there is a monadtransformer "Monad m, Monoid w => CautiousT w e m a"+category: Testing+author: Nick Van den Broeck+extra-source-files:+    ChangeLog.md+    README.md++source-repository head+    type: git+    location: https://github.com/Nickske666/cautious++library+    exposed-modules:+        Import+        Cautious.Cautious+        Cautious.CautiousT+    build-depends:+        QuickCheck >=2.9 && <2.11,+        aeson -any,+        base >=4.10 && <5,+        genvalidity-hspec-aeson -any,+        hspec -any,+        hspec-discover -any,+        transformers -any,+        validity -any+    default-language: Haskell2010+    default-extensions: NoImplicitPrelude+    hs-source-dirs: src/+    other-modules:+        Paths_cautious+    ghc-options: -Wall++test-suite cautious-test+    type: exitcode-stdio-1.0+    main-is: Spec.hs+    build-depends:+        QuickCheck -any,+        aeson -any,+        base >=4.10 && <5,+        cautious -any,+        genvalidity -any,+        genvalidity-hspec -any,+        genvalidity-hspec-aeson -any,+        hspec -any,+        hspec-discover -any,+        transformers -any,+        validity -any+    default-language: Haskell2010+    hs-source-dirs: test/+    other-modules:+        Cautious.CautiousSpec+        Cautious.Gen+        TestImport+        Paths_cautious+    ghc-options: -threaded -rtsopts -with-rtsopts=-N
+ src/Cautious/Cautious.hs view
@@ -0,0 +1,46 @@+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveFunctor #-}++module Cautious.Cautious where++import Import++import qualified Data.Aeson as JSON++data Cautious w e a+    = CautiousWarning w+                      a+    | CautiousError e+    deriving (Generic, Functor)++instance Monoid w => Applicative (Cautious w e) where+    pure = CautiousWarning mempty+    (<*>) (CautiousWarning w f) (CautiousWarning w2 a) =+        CautiousWarning (mappend w w2) $ f a+    (<*>) (CautiousError e) _ = CautiousError e+    (<*>) _ (CautiousError e) = CautiousError e++instance Monoid w => Monad (Cautious w e) where+    (>>=) (CautiousWarning w a) f =+        case f a of+            CautiousWarning w2 x -> CautiousWarning (mappend w w2) x+            CautiousError e -> CautiousError e+    (>>=) (CautiousError e) _ = CautiousError e++instance (Validity a, Validity w, Validity e) => Validity (Cautious w e a)++instance (Show w, Show e, Show a) => Show (Cautious w e a) where+    show (CautiousWarning w a) =+        "CautiousWarnings: " ++ show w ++ "\nResult: " ++ show a+    show (CautiousError e) = "CautiousError: " ++ show e++instance (Eq w, Eq e, Eq a) => Eq (Cautious w e a) where+    (==) (CautiousWarning w a) (CautiousWarning w' a') = w == w' && a == a'+    (==) (CautiousError e) (CautiousError e') = e == e'+    _ == _ = False++instance (JSON.FromJSON a, JSON.FromJSON w, JSON.FromJSON e) =>+         JSON.FromJSON (Cautious w e a)++instance (JSON.ToJSON a, JSON.ToJSON w, JSON.ToJSON e) =>+         JSON.ToJSON (Cautious w e a)
+ src/Cautious/CautiousT.hs view
@@ -0,0 +1,68 @@+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE KindSignatures #-}+{-# LANGUAGE UndecidableInstances #-}++module Cautious.CautiousT where++import Import++import Cautious.Cautious++import Control.Monad.Trans.Class++newtype CautiousT w e (m :: * -> *) a = CautiousT+    { runCautiousT :: m (Cautious w e a)+    } deriving (Generic, Functor)++cautiousWarning :: Monad m => w -> a -> CautiousT w e m a+cautiousWarning w a = CautiousT . pure $ CautiousWarning w a++cautiousWarningIfNothing ::+       (Monoid w, Monad m) => w -> Maybe a -> CautiousT w e m (Maybe a)+cautiousWarningIfNothing w Nothing = cautiousWarning w Nothing+cautiousWarningIfNothing _ (Just a) = cautiousWarning mempty $ Just a++cautiousWarningM :: Monad m => w -> m a -> CautiousT w e m a+cautiousWarningM w ma = CautiousT $ CautiousWarning w <$> ma++cautiousError :: Monad m => e -> CautiousT w e m a+cautiousError e = CautiousT . pure $ CautiousError e++cautiousErrorIfNothing ::+       (Monoid w, Monad m) => Maybe a -> e -> CautiousT w e m a+cautiousErrorIfNothing Nothing e = CautiousT . pure $ CautiousError e+cautiousErrorIfNothing (Just a) _ = pure a++instance (Applicative m, Monoid w) => Applicative (CautiousT w e m) where+    pure = CautiousT . pure . pure+    CautiousT mf <*> CautiousT ma = CautiousT $ liftA2 (<*>) mf ma++instance (Monad m, Monoid w) => Monad (CautiousT w e m) where+    CautiousT ma >>= f =+        CautiousT $ do+            ra <- ma+            case ra of+                CautiousWarning w a -> do+                    rb <- runCautiousT $ f a+                    case rb of+                        CautiousWarning w' b ->+                            pure $ CautiousWarning (w <> w') b+                        CautiousError e -> pure $ CautiousError e+                CautiousError e -> pure $ CautiousError e+    a >> b = a *> b++instance (MonadIO m, Monoid w) => MonadIO (CautiousT w e m) where+    liftIO f = CautiousT $ pure <$> liftIO f++instance Monoid w => MonadTrans (CautiousT w e) where+    lift ma = CautiousT $ CautiousWarning mempty <$> ma++instance Validity (m (Cautious e w a)) => Validity (CautiousT e w m a) where+    validate = validate . runCautiousT++instance Show (m (Cautious e w a)) => Show (CautiousT e w m a) where+    show = show . runCautiousT++instance Eq (m (Cautious e w a)) => Eq (CautiousT e w m a) where+    CautiousT x == CautiousT y = x == y
+ src/Import.hs view
@@ -0,0 +1,16 @@+module Import+    ( module X+    ) where++import Prelude as X++import Data.Monoid as X++import Data.Validity as X++import GHC.Generics as X++import Control.Monad as X+import Control.Monad.IO.Class as X++import Control.Applicative as X
+ test/Cautious/CautiousSpec.hs view
@@ -0,0 +1,27 @@+{-# LANGUAGE TypeApplications #-}++module Cautious.CautiousSpec+    ( spec+    ) where++import TestImport++import Cautious.Cautious+import Cautious.CautiousT+import Cautious.Gen ()++type CautiousExample = Cautious String String++type CautiousExampleT = CautiousT String String Maybe++spec :: Spec+spec = do+    genValidSpec @(CautiousExample Int)+    eqSpec @(CautiousExample Int)+    jsonSpecOnValid @(CautiousExample Int)+    functorSpecOnValid @CautiousExample+    applicativeSpecOnValid @CautiousExample+    monadSpecOnValid @CautiousExample+    functorSpecOnValid @CautiousExampleT+    applicativeSpecOnValid @CautiousExampleT+    monadSpecOnValid @CautiousExampleT
+ test/Cautious/Gen.hs view
@@ -0,0 +1,19 @@+{-# LANGUAGE UndecidableInstances #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++module Cautious.Gen where++import TestImport++import Cautious.Cautious+import Cautious.CautiousT++instance (GenUnchecked a, GenUnchecked w, GenUnchecked e) =>+         GenUnchecked (Cautious w e a)++instance (GenValid a, GenValid w, GenValid e) => GenValid (Cautious w e a)++instance GenUnchecked (m (Cautious e w a)) =>+         GenUnchecked (CautiousT e w m a)++instance GenValid (m (Cautious e w a)) => GenValid (CautiousT e w m a)
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
+ test/TestImport.hs view
@@ -0,0 +1,13 @@+module TestImport+    ( module X+    ) where++import Prelude as X++import Data.GenValidity as X++import Test.Hspec as X+import Test.QuickCheck as X++import Test.Validity as X+import Test.Validity.Aeson as X