module Ditto.Proof where
import Control.Monad.Trans (lift)
import Ditto.Core (Form (..), Proved (..))
import Ditto.Result (Result (..))
import Numeric (readDec, readFloat, readSigned)
newtype Proof m error a b = Proof
{ proofFunction :: a -> m (Either error b)
}
prove
:: (Monad m)
=> Form m input error view a
-> Proof m error a b
-> Form m input error view b
prove (Form frm) (Proof f) =
Form $ do
(xml, mval) <- frm
val <- lift $ lift $ mval
case val of
(Error errs) -> pure (xml, pure $ Error errs)
(Ok (Proved posi a)) ->
do
r <- lift $ lift $ f a
case r of
(Left err) -> pure (xml, pure $ Error [(posi, err)])
(Right b) ->
pure
( xml
, pure $
Ok
( Proved
{ pos = posi
, unProved = b
}
)
)
transform
:: (Monad m)
=> Form m input error view a
-> Proof m error a b
-> Form m input error view b
transform frm proof = frm `prove` proof
transformEitherM
:: (Monad m)
=> Form m input error view a
-> (a -> m (Either error b))
-> Form m input error view b
transformEitherM frm func = frm `transform` (Proof func)
transformEither
:: (Monad m)
=> Form m input error view a
-> (a -> Either error b)
-> Form m input error view b
transformEither frm func = transformEitherM frm (pure . func)
notNullProof :: (Monad m) => error -> Proof m error [a] [a]
notNullProof errorMsg = Proof (pure . check)
where
check list =
if null list
then (Left errorMsg)
else (Right list)
decimal
:: (Monad m, Eq i, Num i)
=> (String -> error)
-> Proof m error String i
decimal mkError = Proof (pure . toDecimal)
where
toDecimal str =
case readDec str of
[(d, [])] -> (Right d)
_ -> (Left $ mkError str)
signedDecimal :: (Monad m, Eq i, Real i) => (String -> error) -> Proof m error String i
signedDecimal mkError = Proof (pure . toDecimal)
where
toDecimal str =
case (readSigned readDec) str of
[(d, [])] -> (Right d)
_ -> (Left $ mkError str)
realFrac :: (Monad m, RealFrac a) => (String -> error) -> Proof m error String a
realFrac mkError = Proof (pure . toRealFrac)
where
toRealFrac str =
case readFloat str of
[(f, [])] -> (Right f)
_ -> (Left $ mkError str)
realFracSigned :: (Monad m, RealFrac a) => (String -> error) -> Proof m error String a
realFracSigned mkError = Proof (pure . toRealFrac)
where
toRealFrac str =
case (readSigned readFloat) str of
[(f, [])] -> (Right f)
_ -> (Left $ mkError str)