{-# LANGUAGE
    BangPatterns
  , DeriveFoldable
  , DeriveFunctor
  , DeriveTraversable
  , GeneralizedNewtypeDeriving
  , MultiParamTypeClasses
  , OverloadedStrings
  , PatternSynonyms
  , ExplicitForAll
#-}

-- | Types relevant to forms and their validation.
module Ditto.Types (
  -- * FormId
    FormId(..)
  , FormRange(..)
  , encodeFormId
  , formIdentifier
  -- * Form result types
  , Value(..)
  , View(..)
  , Proved(..)
  , Result(.., Error, Ok)
  ) where

import Control.Applicative (Alternative(..))
import Data.List.NonEmpty (NonEmpty(..))
import Data.String (IsString(..))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.List.NonEmpty as NE

------------------------------------------------------------------------------
-- FormId
------------------------------------------------------------------------------

-- | An ID used to identify forms
data FormId
  = FormId
      {-# UNPACK #-} !Text           -- ^ Global prefix for the form
      {-# UNPACK #-} !(NonEmpty Int) -- ^ Stack indicating field. Head is most specific to this item
  | FormIdName
      {-# UNPACK #-} !Text -- ^ Local name of the input
      {-# UNPACK #-} !Int  -- ^ Index of the input
  deriving (FormId -> FormId -> Bool
(FormId -> FormId -> Bool)
-> (FormId -> FormId -> Bool) -> Eq FormId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FormId -> FormId -> Bool
$c/= :: FormId -> FormId -> Bool
== :: FormId -> FormId -> Bool
$c== :: FormId -> FormId -> Bool
Eq, Eq FormId
Eq FormId
-> (FormId -> FormId -> Ordering)
-> (FormId -> FormId -> Bool)
-> (FormId -> FormId -> Bool)
-> (FormId -> FormId -> Bool)
-> (FormId -> FormId -> Bool)
-> (FormId -> FormId -> FormId)
-> (FormId -> FormId -> FormId)
-> Ord FormId
FormId -> FormId -> Bool
FormId -> FormId -> Ordering
FormId -> FormId -> FormId
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FormId -> FormId -> FormId
$cmin :: FormId -> FormId -> FormId
max :: FormId -> FormId -> FormId
$cmax :: FormId -> FormId -> FormId
>= :: FormId -> FormId -> Bool
$c>= :: FormId -> FormId -> Bool
> :: FormId -> FormId -> Bool
$c> :: FormId -> FormId -> Bool
<= :: FormId -> FormId -> Bool
$c<= :: FormId -> FormId -> Bool
< :: FormId -> FormId -> Bool
$c< :: FormId -> FormId -> Bool
compare :: FormId -> FormId -> Ordering
$ccompare :: FormId -> FormId -> Ordering
$cp1Ord :: Eq FormId
Ord, Int -> FormId -> ShowS
[FormId] -> ShowS
FormId -> String
(Int -> FormId -> ShowS)
-> (FormId -> String) -> ([FormId] -> ShowS) -> Show FormId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FormId] -> ShowS
$cshowList :: [FormId] -> ShowS
show :: FormId -> String
$cshow :: FormId -> String
showsPrec :: Int -> FormId -> ShowS
$cshowsPrec :: Int -> FormId -> ShowS
Show)

instance IsString FormId where
  fromString :: String -> FormId
fromString String
x = Text -> Int -> FormId
FormIdName (String -> Text
T.pack String
x) Int
0

-- | Encoding a @FormId@: use this instead of @show@ for
-- the name of the input / query string parameter
encodeFormId :: FormId -> Text
encodeFormId :: FormId -> Text
encodeFormId (FormId Text
p NonEmpty Int
xs) =
  let ids :: [Text]
ids = (Int -> Text) -> [Int] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Text
T.pack (String -> Text) -> (Int -> String) -> Int -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show) (NonEmpty Int -> [Int]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty Int
xs)
  in Text
p Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-val-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
"." [Text]
ids
encodeFormId (FormIdName Text
x Int
_) = Text
x

-- | get the head 'Int' from a 'FormId'
formIdentifier :: FormId -> Int
formIdentifier :: FormId -> Int
formIdentifier (FormId Text
_ (Int
x :| [Int]
_)) = Int
x
formIdentifier (FormIdName Text
_ Int
x) = Int
x

-- | A range of ID's to specify a group of forms
data FormRange
  = FormRange FormId FormId
  deriving (FormRange -> FormRange -> Bool
(FormRange -> FormRange -> Bool)
-> (FormRange -> FormRange -> Bool) -> Eq FormRange
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FormRange -> FormRange -> Bool
$c/= :: FormRange -> FormRange -> Bool
== :: FormRange -> FormRange -> Bool
$c== :: FormRange -> FormRange -> Bool
Eq, Int -> FormRange -> ShowS
[FormRange] -> ShowS
FormRange -> String
(Int -> FormRange -> ShowS)
-> (FormRange -> String)
-> ([FormRange] -> ShowS)
-> Show FormRange
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FormRange] -> ShowS
$cshowList :: [FormRange] -> ShowS
show :: FormRange -> String
$cshow :: FormRange -> String
showsPrec :: Int -> FormRange -> ShowS
$cshowsPrec :: Int -> FormRange -> ShowS
Show)

------------------------------------------------------------------------------
-- Form result types
------------------------------------------------------------------------------

-- | views, values as a result of the environment, etc.

-- | Function which creates the form view
newtype View err v = View { View err v -> [(FormRange, err)] -> v
unView :: [(FormRange, err)] -> v }
  deriving (b -> View err v -> View err v
NonEmpty (View err v) -> View err v
View err v -> View err v -> View err v
(View err v -> View err v -> View err v)
-> (NonEmpty (View err v) -> View err v)
-> (forall b. Integral b => b -> View err v -> View err v)
-> Semigroup (View err v)
forall b. Integral b => b -> View err v -> View err v
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall err v. Semigroup v => NonEmpty (View err v) -> View err v
forall err v. Semigroup v => View err v -> View err v -> View err v
forall err v b.
(Semigroup v, Integral b) =>
b -> View err v -> View err v
stimes :: b -> View err v -> View err v
$cstimes :: forall err v b.
(Semigroup v, Integral b) =>
b -> View err v -> View err v
sconcat :: NonEmpty (View err v) -> View err v
$csconcat :: forall err v. Semigroup v => NonEmpty (View err v) -> View err v
<> :: View err v -> View err v -> View err v
$c<> :: forall err v. Semigroup v => View err v -> View err v -> View err v
Semigroup, Semigroup (View err v)
View err v
Semigroup (View err v)
-> View err v
-> (View err v -> View err v -> View err v)
-> ([View err v] -> View err v)
-> Monoid (View err v)
[View err v] -> View err v
View err v -> View err v -> View err v
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall err v. Monoid v => Semigroup (View err v)
forall err v. Monoid v => View err v
forall err v. Monoid v => [View err v] -> View err v
forall err v. Monoid v => View err v -> View err v -> View err v
mconcat :: [View err v] -> View err v
$cmconcat :: forall err v. Monoid v => [View err v] -> View err v
mappend :: View err v -> View err v -> View err v
$cmappend :: forall err v. Monoid v => View err v -> View err v -> View err v
mempty :: View err v
$cmempty :: forall err v. Monoid v => View err v
$cp1Monoid :: forall err v. Monoid v => Semigroup (View err v)
Monoid, a -> View err b -> View err a
(a -> b) -> View err a -> View err b
(forall a b. (a -> b) -> View err a -> View err b)
-> (forall a b. a -> View err b -> View err a)
-> Functor (View err)
forall a b. a -> View err b -> View err a
forall a b. (a -> b) -> View err a -> View err b
forall err a b. a -> View err b -> View err a
forall err a b. (a -> b) -> View err a -> View err b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> View err b -> View err a
$c<$ :: forall err a b. a -> View err b -> View err a
fmap :: (a -> b) -> View err a -> View err b
$cfmap :: forall err a b. (a -> b) -> View err a -> View err b
Functor)

-- | used to represent whether a value was found in the form
-- submission data, missing from the form submission data, or expected
-- that the default value should be used
data Value a
  = Default
  | Missing
  | Found a
  deriving (Value a -> Value a -> Bool
(Value a -> Value a -> Bool)
-> (Value a -> Value a -> Bool) -> Eq (Value a)
forall a. Eq a => Value a -> Value a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Value a -> Value a -> Bool
$c/= :: forall a. Eq a => Value a -> Value a -> Bool
== :: Value a -> Value a -> Bool
$c== :: forall a. Eq a => Value a -> Value a -> Bool
Eq, Int -> Value a -> ShowS
[Value a] -> ShowS
Value a -> String
(Int -> Value a -> ShowS)
-> (Value a -> String) -> ([Value a] -> ShowS) -> Show (Value a)
forall a. Show a => Int -> Value a -> ShowS
forall a. Show a => [Value a] -> ShowS
forall a. Show a => Value a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Value a] -> ShowS
$cshowList :: forall a. Show a => [Value a] -> ShowS
show :: Value a -> String
$cshow :: forall a. Show a => Value a -> String
showsPrec :: Int -> Value a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Value a -> ShowS
Show, a -> Value b -> Value a
(a -> b) -> Value a -> Value b
(forall a b. (a -> b) -> Value a -> Value b)
-> (forall a b. a -> Value b -> Value a) -> Functor Value
forall a b. a -> Value b -> Value a
forall a b. (a -> b) -> Value a -> Value b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Value b -> Value a
$c<$ :: forall a b. a -> Value b -> Value a
fmap :: (a -> b) -> Value a -> Value b
$cfmap :: forall a b. (a -> b) -> Value a -> Value b
Functor, Functor Value
Foldable Value
Functor Value
-> Foldable Value
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Value a -> f (Value b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Value (f a) -> f (Value a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Value a -> m (Value b))
-> (forall (m :: * -> *) a. Monad m => Value (m a) -> m (Value a))
-> Traversable Value
(a -> f b) -> Value a -> f (Value b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Value (m a) -> m (Value a)
forall (f :: * -> *) a. Applicative f => Value (f a) -> f (Value a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Value a -> m (Value b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Value a -> f (Value b)
sequence :: Value (m a) -> m (Value a)
$csequence :: forall (m :: * -> *) a. Monad m => Value (m a) -> m (Value a)
mapM :: (a -> m b) -> Value a -> m (Value b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Value a -> m (Value b)
sequenceA :: Value (f a) -> f (Value a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Value (f a) -> f (Value a)
traverse :: (a -> f b) -> Value a -> f (Value b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Value a -> f (Value b)
$cp2Traversable :: Foldable Value
$cp1Traversable :: Functor Value
Traversable, Value a -> Bool
(a -> m) -> Value a -> m
(a -> b -> b) -> b -> Value a -> b
(forall m. Monoid m => Value m -> m)
-> (forall m a. Monoid m => (a -> m) -> Value a -> m)
-> (forall m a. Monoid m => (a -> m) -> Value a -> m)
-> (forall a b. (a -> b -> b) -> b -> Value a -> b)
-> (forall a b. (a -> b -> b) -> b -> Value a -> b)
-> (forall b a. (b -> a -> b) -> b -> Value a -> b)
-> (forall b a. (b -> a -> b) -> b -> Value a -> b)
-> (forall a. (a -> a -> a) -> Value a -> a)
-> (forall a. (a -> a -> a) -> Value a -> a)
-> (forall a. Value a -> [a])
-> (forall a. Value a -> Bool)
-> (forall a. Value a -> Int)
-> (forall a. Eq a => a -> Value a -> Bool)
-> (forall a. Ord a => Value a -> a)
-> (forall a. Ord a => Value a -> a)
-> (forall a. Num a => Value a -> a)
-> (forall a. Num a => Value a -> a)
-> Foldable Value
forall a. Eq a => a -> Value a -> Bool
forall a. Num a => Value a -> a
forall a. Ord a => Value a -> a
forall m. Monoid m => Value m -> m
forall a. Value a -> Bool
forall a. Value a -> Int
forall a. Value a -> [a]
forall a. (a -> a -> a) -> Value a -> a
forall m a. Monoid m => (a -> m) -> Value a -> m
forall b a. (b -> a -> b) -> b -> Value a -> b
forall a b. (a -> b -> b) -> b -> Value a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Value a -> a
$cproduct :: forall a. Num a => Value a -> a
sum :: Value a -> a
$csum :: forall a. Num a => Value a -> a
minimum :: Value a -> a
$cminimum :: forall a. Ord a => Value a -> a
maximum :: Value a -> a
$cmaximum :: forall a. Ord a => Value a -> a
elem :: a -> Value a -> Bool
$celem :: forall a. Eq a => a -> Value a -> Bool
length :: Value a -> Int
$clength :: forall a. Value a -> Int
null :: Value a -> Bool
$cnull :: forall a. Value a -> Bool
toList :: Value a -> [a]
$ctoList :: forall a. Value a -> [a]
foldl1 :: (a -> a -> a) -> Value a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Value a -> a
foldr1 :: (a -> a -> a) -> Value a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Value a -> a
foldl' :: (b -> a -> b) -> b -> Value a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Value a -> b
foldl :: (b -> a -> b) -> b -> Value a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Value a -> b
foldr' :: (a -> b -> b) -> b -> Value a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Value a -> b
foldr :: (a -> b -> b) -> b -> Value a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Value a -> b
foldMap' :: (a -> m) -> Value a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Value a -> m
foldMap :: (a -> m) -> Value a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Value a -> m
fold :: Value m -> m
$cfold :: forall m. Monoid m => Value m -> m
Foldable)

instance Applicative Value where
  pure :: a -> Value a
pure = a -> Value a
forall a. a -> Value a
Found
  (Found a -> b
f) <*> :: Value (a -> b) -> Value a -> Value b
<*> (Found a
x) = b -> Value b
forall a. a -> Value a
Found (a -> b
f a
x)
  Value (a -> b)
Default <*> Value a
_ = Value b
forall a. Value a
Default
  Value (a -> b)
Missing <*> Value a
_ = Value b
forall a. Value a
Missing
  Found{} <*> Value a
Default = Value b
forall a. Value a
Default
  Found{} <*> Value a
Missing = Value b
forall a. Value a
Default

instance Alternative Value where
  empty :: Value a
empty = Value a
forall a. Value a
Missing
  x :: Value a
x@Found{} <|> :: Value a -> Value a -> Value a
<|> Value a
_ = Value a
x
  Value a
Default <|> Value a
_ = Value a
forall a. Value a
Default
  Value a
Missing <|> Value a
x = Value a
x

instance Semigroup a => Semigroup (Value a) where
  Value a
Missing <> :: Value a -> Value a -> Value a
<> Value a
Missing = Value a
forall a. Value a
Missing
  Value a
Missing <> Value a
Default = Value a
forall a. Value a
Default
  Value a
Default <> Value a
Missing = Value a
forall a. Value a
Default
  Value a
Default <> Value a
Default = Value a
forall a. Value a
Default
  Found a
x <> Found a
y = a -> Value a
forall a. a -> Value a
Found (a
x a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
y)
  Found a
x <> Value a
_ = a -> Value a
forall a. a -> Value a
Found a
x
  Value a
_ <> Found a
y = a -> Value a
forall a. a -> Value a
Found a
y

-- | Type for failing computations
-- Similar to @Either@ but with an accumilating @Applicative@ instance
newtype Result e ok = Result { Result e ok -> Either [(FormRange, e)] ok
getResult :: Either [(FormRange, e)] ok }
  deriving (Int -> Result e ok -> ShowS
[Result e ok] -> ShowS
Result e ok -> String
(Int -> Result e ok -> ShowS)
-> (Result e ok -> String)
-> ([Result e ok] -> ShowS)
-> Show (Result e ok)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall e ok. (Show e, Show ok) => Int -> Result e ok -> ShowS
forall e ok. (Show e, Show ok) => [Result e ok] -> ShowS
forall e ok. (Show e, Show ok) => Result e ok -> String
showList :: [Result e ok] -> ShowS
$cshowList :: forall e ok. (Show e, Show ok) => [Result e ok] -> ShowS
show :: Result e ok -> String
$cshow :: forall e ok. (Show e, Show ok) => Result e ok -> String
showsPrec :: Int -> Result e ok -> ShowS
$cshowsPrec :: forall e ok. (Show e, Show ok) => Int -> Result e ok -> ShowS
Show, Result e ok -> Result e ok -> Bool
(Result e ok -> Result e ok -> Bool)
-> (Result e ok -> Result e ok -> Bool) -> Eq (Result e ok)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall e ok. (Eq e, Eq ok) => Result e ok -> Result e ok -> Bool
/= :: Result e ok -> Result e ok -> Bool
$c/= :: forall e ok. (Eq e, Eq ok) => Result e ok -> Result e ok -> Bool
== :: Result e ok -> Result e ok -> Bool
$c== :: forall e ok. (Eq e, Eq ok) => Result e ok -> Result e ok -> Bool
Eq, a -> Result e b -> Result e a
(a -> b) -> Result e a -> Result e b
(forall a b. (a -> b) -> Result e a -> Result e b)
-> (forall a b. a -> Result e b -> Result e a)
-> Functor (Result e)
forall a b. a -> Result e b -> Result e a
forall a b. (a -> b) -> Result e a -> Result e b
forall e a b. a -> Result e b -> Result e a
forall e a b. (a -> b) -> Result e a -> Result e b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Result e b -> Result e a
$c<$ :: forall e a b. a -> Result e b -> Result e a
fmap :: (a -> b) -> Result e a -> Result e b
$cfmap :: forall e a b. (a -> b) -> Result e a -> Result e b
Functor, a -> Result e a -> Bool
Result e m -> m
Result e a -> [a]
Result e a -> Bool
Result e a -> Int
Result e a -> a
Result e a -> a
Result e a -> a
Result e a -> a
(a -> m) -> Result e a -> m
(a -> m) -> Result e a -> m
(a -> b -> b) -> b -> Result e a -> b
(a -> b -> b) -> b -> Result e a -> b
(b -> a -> b) -> b -> Result e a -> b
(b -> a -> b) -> b -> Result e a -> b
(a -> a -> a) -> Result e a -> a
(a -> a -> a) -> Result e a -> a
(forall m. Monoid m => Result e m -> m)
-> (forall m a. Monoid m => (a -> m) -> Result e a -> m)
-> (forall m a. Monoid m => (a -> m) -> Result e a -> m)
-> (forall a b. (a -> b -> b) -> b -> Result e a -> b)
-> (forall a b. (a -> b -> b) -> b -> Result e a -> b)
-> (forall b a. (b -> a -> b) -> b -> Result e a -> b)
-> (forall b a. (b -> a -> b) -> b -> Result e a -> b)
-> (forall a. (a -> a -> a) -> Result e a -> a)
-> (forall a. (a -> a -> a) -> Result e a -> a)
-> (forall a. Result e a -> [a])
-> (forall a. Result e a -> Bool)
-> (forall a. Result e a -> Int)
-> (forall a. Eq a => a -> Result e a -> Bool)
-> (forall a. Ord a => Result e a -> a)
-> (forall a. Ord a => Result e a -> a)
-> (forall a. Num a => Result e a -> a)
-> (forall a. Num a => Result e a -> a)
-> Foldable (Result e)
forall a. Eq a => a -> Result e a -> Bool
forall a. Num a => Result e a -> a
forall a. Ord a => Result e a -> a
forall m. Monoid m => Result e m -> m
forall a. Result e a -> Bool
forall a. Result e a -> Int
forall a. Result e a -> [a]
forall a. (a -> a -> a) -> Result e a -> a
forall e a. Eq a => a -> Result e a -> Bool
forall e a. Num a => Result e a -> a
forall e a. Ord a => Result e a -> a
forall m a. Monoid m => (a -> m) -> Result e a -> m
forall e m. Monoid m => Result e m -> m
forall e a. Result e a -> Bool
forall e a. Result e a -> Int
forall e a. Result e a -> [a]
forall b a. (b -> a -> b) -> b -> Result e a -> b
forall a b. (a -> b -> b) -> b -> Result e a -> b
forall e a. (a -> a -> a) -> Result e a -> a
forall e m a. Monoid m => (a -> m) -> Result e a -> m
forall e b a. (b -> a -> b) -> b -> Result e a -> b
forall e a b. (a -> b -> b) -> b -> Result e a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Result e a -> a
$cproduct :: forall e a. Num a => Result e a -> a
sum :: Result e a -> a
$csum :: forall e a. Num a => Result e a -> a
minimum :: Result e a -> a
$cminimum :: forall e a. Ord a => Result e a -> a
maximum :: Result e a -> a
$cmaximum :: forall e a. Ord a => Result e a -> a
elem :: a -> Result e a -> Bool
$celem :: forall e a. Eq a => a -> Result e a -> Bool
length :: Result e a -> Int
$clength :: forall e a. Result e a -> Int
null :: Result e a -> Bool
$cnull :: forall e a. Result e a -> Bool
toList :: Result e a -> [a]
$ctoList :: forall e a. Result e a -> [a]
foldl1 :: (a -> a -> a) -> Result e a -> a
$cfoldl1 :: forall e a. (a -> a -> a) -> Result e a -> a
foldr1 :: (a -> a -> a) -> Result e a -> a
$cfoldr1 :: forall e a. (a -> a -> a) -> Result e a -> a
foldl' :: (b -> a -> b) -> b -> Result e a -> b
$cfoldl' :: forall e b a. (b -> a -> b) -> b -> Result e a -> b
foldl :: (b -> a -> b) -> b -> Result e a -> b
$cfoldl :: forall e b a. (b -> a -> b) -> b -> Result e a -> b
foldr' :: (a -> b -> b) -> b -> Result e a -> b
$cfoldr' :: forall e a b. (a -> b -> b) -> b -> Result e a -> b
foldr :: (a -> b -> b) -> b -> Result e a -> b
$cfoldr :: forall e a b. (a -> b -> b) -> b -> Result e a -> b
foldMap' :: (a -> m) -> Result e a -> m
$cfoldMap' :: forall e m a. Monoid m => (a -> m) -> Result e a -> m
foldMap :: (a -> m) -> Result e a -> m
$cfoldMap :: forall e m a. Monoid m => (a -> m) -> Result e a -> m
fold :: Result e m -> m
$cfold :: forall e m. Monoid m => Result e m -> m
Foldable, Functor (Result e)
Foldable (Result e)
Functor (Result e)
-> Foldable (Result e)
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Result e a -> f (Result e b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Result e (f a) -> f (Result e a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Result e a -> m (Result e b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Result e (m a) -> m (Result e a))
-> Traversable (Result e)
(a -> f b) -> Result e a -> f (Result e b)
forall e. Functor (Result e)
forall e. Foldable (Result e)
forall e (m :: * -> *) a.
Monad m =>
Result e (m a) -> m (Result e a)
forall e (f :: * -> *) a.
Applicative f =>
Result e (f a) -> f (Result e a)
forall e (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Result e a -> m (Result e b)
forall e (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Result e a -> f (Result e b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Result e (m a) -> m (Result e a)
forall (f :: * -> *) a.
Applicative f =>
Result e (f a) -> f (Result e a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Result e a -> m (Result e b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Result e a -> f (Result e b)
sequence :: Result e (m a) -> m (Result e a)
$csequence :: forall e (m :: * -> *) a.
Monad m =>
Result e (m a) -> m (Result e a)
mapM :: (a -> m b) -> Result e a -> m (Result e b)
$cmapM :: forall e (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Result e a -> m (Result e b)
sequenceA :: Result e (f a) -> f (Result e a)
$csequenceA :: forall e (f :: * -> *) a.
Applicative f =>
Result e (f a) -> f (Result e a)
traverse :: (a -> f b) -> Result e a -> f (Result e b)
$ctraverse :: forall e (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Result e a -> f (Result e b)
$cp2Traversable :: forall e. Foldable (Result e)
$cp1Traversable :: forall e. Functor (Result e)
Traversable, Applicative (Result e)
a -> Result e a
Applicative (Result e)
-> (forall a b. Result e a -> (a -> Result e b) -> Result e b)
-> (forall a b. Result e a -> Result e b -> Result e b)
-> (forall a. a -> Result e a)
-> Monad (Result e)
Result e a -> (a -> Result e b) -> Result e b
Result e a -> Result e b -> Result e b
forall e. Applicative (Result e)
forall a. a -> Result e a
forall e a. a -> Result e a
forall a b. Result e a -> Result e b -> Result e b
forall a b. Result e a -> (a -> Result e b) -> Result e b
forall e a b. Result e a -> Result e b -> Result e b
forall e a b. Result e a -> (a -> Result e b) -> Result e b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> Result e a
$creturn :: forall e a. a -> Result e a
>> :: Result e a -> Result e b -> Result e b
$c>> :: forall e a b. Result e a -> Result e b -> Result e b
>>= :: Result e a -> (a -> Result e b) -> Result e b
$c>>= :: forall e a b. Result e a -> (a -> Result e b) -> Result e b
$cp1Monad :: forall e. Applicative (Result e)
Monad)

pattern Error :: forall e ok. [(FormRange, e)] -> Result e ok
pattern $bError :: [(FormRange, e)] -> Result e ok
$mError :: forall r e ok.
Result e ok -> ([(FormRange, e)] -> r) -> (Void# -> r) -> r
Error e = Result (Left e)
pattern Ok :: forall e ok. ok -> Result e ok
pattern $bOk :: ok -> Result e ok
$mOk :: forall r e ok. Result e ok -> (ok -> r) -> (Void# -> r) -> r
Ok ok = Result (Right ok)
{-# COMPLETE Error, Ok #-}

instance Applicative (Result e) where
  pure :: a -> Result e a
pure = a -> Result e a
forall e a. a -> Result e a
Ok
  Error [(FormRange, e)]
x <*> :: Result e (a -> b) -> Result e a -> Result e b
<*> Error [(FormRange, e)]
y = [(FormRange, e)] -> Result e b
forall e ok. [(FormRange, e)] -> Result e ok
Error ([(FormRange, e)] -> Result e b) -> [(FormRange, e)] -> Result e b
forall a b. (a -> b) -> a -> b
$ [(FormRange, e)]
x [(FormRange, e)] -> [(FormRange, e)] -> [(FormRange, e)]
forall a. [a] -> [a] -> [a]
++ [(FormRange, e)]
y
  Error [(FormRange, e)]
x <*> Ok a
_ = [(FormRange, e)] -> Result e b
forall e ok. [(FormRange, e)] -> Result e ok
Error [(FormRange, e)]
x
  Ok a -> b
_ <*> Error [(FormRange, e)]
y = [(FormRange, e)] -> Result e b
forall e ok. [(FormRange, e)] -> Result e ok
Error [(FormRange, e)]
y
  Ok a -> b
x <*> Ok a
y = b -> Result e b
forall e a. a -> Result e a
Ok (b -> Result e b) -> b -> Result e b
forall a b. (a -> b) -> a -> b
$ a -> b
x a
y

-- | Proved records a value, the location that value came from, and something that was proved about the value.
data Proved a = Proved
  { Proved a -> FormRange
pos :: FormRange
  , Proved a -> a
unProved :: a
  } deriving (Int -> Proved a -> ShowS
[Proved a] -> ShowS
Proved a -> String
(Int -> Proved a -> ShowS)
-> (Proved a -> String) -> ([Proved a] -> ShowS) -> Show (Proved a)
forall a. Show a => Int -> Proved a -> ShowS
forall a. Show a => [Proved a] -> ShowS
forall a. Show a => Proved a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Proved a] -> ShowS
$cshowList :: forall a. Show a => [Proved a] -> ShowS
show :: Proved a -> String
$cshow :: forall a. Show a => Proved a -> String
showsPrec :: Int -> Proved a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Proved a -> ShowS
Show, a -> Proved b -> Proved a
(a -> b) -> Proved a -> Proved b
(forall a b. (a -> b) -> Proved a -> Proved b)
-> (forall a b. a -> Proved b -> Proved a) -> Functor Proved
forall a b. a -> Proved b -> Proved a
forall a b. (a -> b) -> Proved a -> Proved b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Proved b -> Proved a
$c<$ :: forall a b. a -> Proved b -> Proved a
fmap :: (a -> b) -> Proved a -> Proved b
$cfmap :: forall a b. (a -> b) -> Proved a -> Proved b
Functor, Proved a -> Bool
(a -> m) -> Proved a -> m
(a -> b -> b) -> b -> Proved a -> b
(forall m. Monoid m => Proved m -> m)
-> (forall m a. Monoid m => (a -> m) -> Proved a -> m)
-> (forall m a. Monoid m => (a -> m) -> Proved a -> m)
-> (forall a b. (a -> b -> b) -> b -> Proved a -> b)
-> (forall a b. (a -> b -> b) -> b -> Proved a -> b)
-> (forall b a. (b -> a -> b) -> b -> Proved a -> b)
-> (forall b a. (b -> a -> b) -> b -> Proved a -> b)
-> (forall a. (a -> a -> a) -> Proved a -> a)
-> (forall a. (a -> a -> a) -> Proved a -> a)
-> (forall a. Proved a -> [a])
-> (forall a. Proved a -> Bool)
-> (forall a. Proved a -> Int)
-> (forall a. Eq a => a -> Proved a -> Bool)
-> (forall a. Ord a => Proved a -> a)
-> (forall a. Ord a => Proved a -> a)
-> (forall a. Num a => Proved a -> a)
-> (forall a. Num a => Proved a -> a)
-> Foldable Proved
forall a. Eq a => a -> Proved a -> Bool
forall a. Num a => Proved a -> a
forall a. Ord a => Proved a -> a
forall m. Monoid m => Proved m -> m
forall a. Proved a -> Bool
forall a. Proved a -> Int
forall a. Proved a -> [a]
forall a. (a -> a -> a) -> Proved a -> a
forall m a. Monoid m => (a -> m) -> Proved a -> m
forall b a. (b -> a -> b) -> b -> Proved a -> b
forall a b. (a -> b -> b) -> b -> Proved a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Proved a -> a
$cproduct :: forall a. Num a => Proved a -> a
sum :: Proved a -> a
$csum :: forall a. Num a => Proved a -> a
minimum :: Proved a -> a
$cminimum :: forall a. Ord a => Proved a -> a
maximum :: Proved a -> a
$cmaximum :: forall a. Ord a => Proved a -> a
elem :: a -> Proved a -> Bool
$celem :: forall a. Eq a => a -> Proved a -> Bool
length :: Proved a -> Int
$clength :: forall a. Proved a -> Int
null :: Proved a -> Bool
$cnull :: forall a. Proved a -> Bool
toList :: Proved a -> [a]
$ctoList :: forall a. Proved a -> [a]
foldl1 :: (a -> a -> a) -> Proved a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Proved a -> a
foldr1 :: (a -> a -> a) -> Proved a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Proved a -> a
foldl' :: (b -> a -> b) -> b -> Proved a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Proved a -> b
foldl :: (b -> a -> b) -> b -> Proved a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Proved a -> b
foldr' :: (a -> b -> b) -> b -> Proved a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Proved a -> b
foldr :: (a -> b -> b) -> b -> Proved a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Proved a -> b
foldMap' :: (a -> m) -> Proved a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Proved a -> m
foldMap :: (a -> m) -> Proved a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Proved a -> m
fold :: Proved m -> m
$cfold :: forall m. Monoid m => Proved m -> m
Foldable, Functor Proved
Foldable Proved
Functor Proved
-> Foldable Proved
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Proved a -> f (Proved b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Proved (f a) -> f (Proved a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Proved a -> m (Proved b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Proved (m a) -> m (Proved a))
-> Traversable Proved
(a -> f b) -> Proved a -> f (Proved b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Proved (m a) -> m (Proved a)
forall (f :: * -> *) a.
Applicative f =>
Proved (f a) -> f (Proved a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Proved a -> m (Proved b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Proved a -> f (Proved b)
sequence :: Proved (m a) -> m (Proved a)
$csequence :: forall (m :: * -> *) a. Monad m => Proved (m a) -> m (Proved a)
mapM :: (a -> m b) -> Proved a -> m (Proved b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Proved a -> m (Proved b)
sequenceA :: Proved (f a) -> f (Proved a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Proved (f a) -> f (Proved a)
traverse :: (a -> f b) -> Proved a -> f (Proved b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Proved a -> f (Proved b)
$cp2Traversable :: Foldable Proved
$cp1Traversable :: Functor Proved
Traversable)