{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
module Propellor.Types.ResultCheck (
UncheckedProperty,
unchecked,
checkResult,
check,
Checkable,
assume,
) where
import Propellor.Types
import Propellor.Exception
import Utility.Monad
import Data.Monoid
import Prelude
data UncheckedProperty i = UncheckedProperty (Property i)
instance TightenTargets UncheckedProperty where
tightenTargets :: forall (untightened :: [MetaType]) (tightened :: [MetaType]).
(TightenTargetsAllowed untightened tightened, SingI tightened) =>
UncheckedProperty (MetaTypes untightened)
-> UncheckedProperty (MetaTypes tightened)
tightenTargets (UncheckedProperty Property (MetaTypes untightened)
p) = forall i. Property i -> UncheckedProperty i
UncheckedProperty (forall (p :: * -> *) (untightened :: [MetaType])
(tightened :: [MetaType]).
(TightenTargets p, TightenTargetsAllowed untightened tightened,
SingI tightened) =>
p (MetaTypes untightened) -> p (MetaTypes tightened)
tightenTargets Property (MetaTypes untightened)
p)
unchecked :: Property i -> UncheckedProperty i
unchecked :: forall i. Property i -> UncheckedProperty i
unchecked = forall i. Property i -> UncheckedProperty i
UncheckedProperty
checkResult
:: (Checkable p i, LiftPropellor m)
=> m a
-> (a -> m Result)
-> p i
-> Property i
checkResult :: forall (p :: * -> *) i (m :: * -> *) a.
(Checkable p i, LiftPropellor m) =>
m a -> (a -> m Result) -> p i -> Property i
checkResult m a
precheck a -> m Result
postcheck p i
p = forall metatypes.
Property metatypes
-> (Propellor Result -> Propellor Result) -> Property metatypes
adjustPropertySatisfy (forall (p :: * -> *) i. Checkable p i => p i -> Property i
checkedProp p i
p) forall a b. (a -> b) -> a -> b
$ \Propellor Result
satisfy -> do
a
a <- forall (m :: * -> *) a. LiftPropellor m => m a -> Propellor a
liftPropellor m a
precheck
Result
r <- forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
m Result -> m Result
catchPropellor Propellor Result
satisfy
Result
r' <- forall (m :: * -> *) a. LiftPropellor m => m a -> Propellor a
liftPropellor forall a b. (a -> b) -> a -> b
$ a -> m Result
postcheck a
a
forall (m :: * -> *) a. Monad m => a -> m a
return (Result
r forall a. Semigroup a => a -> a -> a
<> Result
r')
check :: (Checkable p i, LiftPropellor m) => m Bool -> p i -> Property i
check :: forall (p :: * -> *) i (m :: * -> *).
(Checkable p i, LiftPropellor m) =>
m Bool -> p i -> Property i
check m Bool
test p i
p = forall metatypes.
Property metatypes
-> (Propellor Result -> Propellor Result) -> Property metatypes
adjustPropertySatisfy (forall (p :: * -> *) i. Checkable p i => p i -> Property i
preCheckedProp p i
p) forall a b. (a -> b) -> a -> b
$ \Propellor Result
satisfy ->
forall (m :: * -> *) a. Monad m => m Bool -> (m a, m a) -> m a
ifM (forall (m :: * -> *) a. LiftPropellor m => m a -> Propellor a
liftPropellor m Bool
test)
( Propellor Result
satisfy
, forall (m :: * -> *) a. Monad m => a -> m a
return Result
NoChange
)
class Checkable p i where
checkedProp :: p i -> Property i
preCheckedProp :: p i -> Property i
instance Checkable Property i where
checkedProp :: Property i -> Property i
checkedProp = forall a. a -> a
id
preCheckedProp :: Property i -> Property i
preCheckedProp = forall a. a -> a
id
instance Checkable UncheckedProperty i where
checkedProp :: UncheckedProperty i -> Property i
checkedProp (UncheckedProperty Property i
p) = Property i
p
preCheckedProp :: UncheckedProperty i -> Property i
preCheckedProp (UncheckedProperty Property i
p) = Property i
p forall (p :: * -> *) i.
Checkable p i =>
p i -> Result -> Property i
`assume` Result
MadeChange
assume :: Checkable p i => p i -> Result -> Property i
assume :: forall (p :: * -> *) i.
Checkable p i =>
p i -> Result -> Property i
assume p i
p Result
result = forall metatypes.
Property metatypes
-> (Propellor Result -> Propellor Result) -> Property metatypes
adjustPropertySatisfy (forall (p :: * -> *) i. Checkable p i => p i -> Property i
checkedProp p i
p) forall a b. (a -> b) -> a -> b
$ \Propellor Result
satisfy -> do
Result
r <- Propellor Result
satisfy
forall (m :: * -> *) a. Monad m => a -> m a
return (Result
r forall a. Semigroup a => a -> a -> a
<> Result
result)