dep-t-advice 0.2.0.1 → 0.3.0.0
raw patch · 5 files changed
+32/−36 lines, 5 filesdep −constraintsPVP ok
version bump matches the API change (PVP)
Dependencies removed: constraints
API changes (from Hackage documentation)
- Control.Monad.Dep.Advice: Sub :: (a => Dict b) -> (:-) a b
- Control.Monad.Dep.Advice: infixr 9 :-
- Control.Monad.Dep.Advice: newtype a :- b
- Control.Monad.Dep.Advice: [Dict] :: forall a. a => Dict a
+ Control.Monad.Dep.Advice: [Dict] :: forall k (c :: k -> Constraint) (a :: k). c a => Dict c a
- Control.Monad.Dep.Advice: data Dict a
+ Control.Monad.Dep.Advice: data Dict (c :: k -> Constraint) (a :: k)
- Control.Monad.Dep.Advice: restrictArgs :: forall more less e m r. (forall x. more x :- less x) -> Advice less e m r -> Advice more e m r
+ Control.Monad.Dep.Advice: restrictArgs :: forall more less e m r. (forall x. Dict more x -> Dict less x) -> Advice less e m r -> Advice more e m r
Files
- CHANGELOG.md +7/−2
- README.md +1/−1
- dep-t-advice.cabal +1/−2
- lib/Control/Monad/Dep/Advice.hs +20/−28
- test/tests.hs +3/−3
CHANGELOG.md view
@@ -1,10 +1,15 @@ # Revision history for dep-t-advice +## 0.3.0.0 + +* BREAKING CHANGE. Removed the dependency on "constraints" to reduce the + dependency footprint. This changes the signature of restrictArgs. + ## 0.2.0.0 * BREAKING CHANGE. The Advice type is no longer parameterized by the `cem` and - `cr` constraints. Instead, it's directly parameterized by the types @e@ of - the environment, @m@ of the base monad and @r@ of the result, which are left + `cr` constraints. Instead, it's directly parameterized by the types `e` of + the environment, `m` of the base monad and `r` of the result, which are left polymorphic when needed. This simplifies a bunch of things. The `ca` constraint paramter remains however.
README.md view
@@ -78,7 +78,7 @@ - Change the result value of the function. -- Sidestep the execution of the function altogeher, providing al alternative result. +- Sidestep the execution of the function altogether, providing al alternative result. Here's how a `printArgs` advice might be defined:
dep-t-advice.cabal view
@@ -1,7 +1,7 @@ cabal-version: 3.0 name: dep-t-advice -version: 0.2.0.1 +version: 0.3.0.0 synopsis: Giving good advice to functions in a DepT environment. description: Companion to the dep-t package. Easily add behaviour to functions living in a DepT environment, whatever the number of arguments they might have. @@ -23,7 +23,6 @@ common common build-depends: base >=4.10.0.0 && < 5, sop-core ^>= 0.5.0.0, - constraints ^>= 0.12, dep-t ^>= 0.1.3.0, default-language: Haskell2010
lib/Control/Monad/Dep/Advice.hs view
@@ -101,19 +101,14 @@ NP (..), I (..), cfoldMap_NP, - - -- * "constraints" re-exports - -- $constraints - type (:-) (..), - Dict (..), + Dict (..) ) where import Control.Monad.Dep -import Data.Constraint import Data.Kind import Data.SOP -import Data.SOP.Dict qualified as SOP +import Data.SOP.Dict import Data.SOP.NP -- $setup @@ -131,7 +126,6 @@ -- >>> import Control.Monad.Dep -- >>> import Control.Monad.Dep.Advice -- >>> import Control.Monad.Dep.Advice.Basic (printArgs,returnMempty) --- >>> import Data.Constraint -- >>> import Data.Kind -- >>> import Data.SOP -- >>> import Data.SOP.NP @@ -484,30 +478,34 @@ -- normal function constraints—the @ca@ constraints of an 'Advice' aren't -- automatically "collected" during composition. -- --- Instead, we need to harmonize the @ca@ constraints of each 'Advice' by turning them --- into the combination of all constraints. 'restrictArgs' helps with that. +-- Instead, we need to harmonize the @ca@ constraints of each 'Advice' by +-- turning them into the combination of all constraints. 'restrictArgs' +-- helps with that. -- --- 'restrictArgs' takes as parameter evidence of entailment between @ca@ --- constraints, using the type '(:-)' from the \"constraints\" package. But --- how to construct such evidence? By using the 'Sub' and the 'Dict' --- constructors, either with an explicit type signature: +-- 'restrictArgs' takes as parameter value-level "\evidence\" that one +-- constraint implies another. But how to construct such evidence? By using +-- the 'Dict' GADT, more precisely the deceptively simple-looking term +-- @\\Dict -> Dict@. That function "absorbs" some constraint present in the +-- ambient context and re-packages it a a new constraint that is implied by +-- the former. We can't rely on type inference here; we need to provide +-- enough type information to the GADT, be it as an explicit signature: -- -- >>> :{ -- stricterPrintArgs :: forall e m r. MonadIO m => Advice (Show `And` Eq `And` Ord) NilEnv m r --- stricterPrintArgs = restrictArgs (Sub Dict) (printArgs stdout "foo") +-- stricterPrintArgs = restrictArgs (\Dict -> Dict) (printArgs stdout "foo") -- :} -- -- or with a type application to 'restrictArgs': -- --- >>> stricterPrintArgs = restrictArgs @(Show `And` Eq `And` Ord) (Sub Dict) (printArgs stdout "foo") +-- >>> stricterPrintArgs = restrictArgs @(Show `And` Eq `And` Ord) (\Dict -> Dict) (printArgs stdout "foo") -- -- -- | Makes the constraint on the arguments more restrictive. restrictArgs :: forall more less e m r. - -- | Evidence that one constraint implies the other. - (forall x. more x :- less x) -> + -- | Evidence that one constraint implies the other. Every @x@ that has a @more@ instance also has a @less@ instance. + (forall x. Dict more x -> Dict less x) -> -- | Advice with less restrictive constraint on the args. Advice less e m r -> -- | Advice with more restrictive constraint on the args. @@ -523,7 +521,7 @@ restrictArgs evidence (Advice proxy tweakArgs tweakExecution) = let captureExistential :: forall more less e m r u. - (forall x. more x :- less x) -> + (forall x. Dict more x -> Dict less x) -> Proxy u -> ( forall as. All less as => @@ -540,19 +538,13 @@ Advice (Proxy @u) ( let tweakArgs'' :: forall as. All more as => NP I as -> DepT e m (u, NP I as) - tweakArgs'' = case SOP.mapAll @more @less (translateEvidence @more @less evidence') of - f -> case f (SOP.Dict @(All more) @as) of - SOP.Dict -> \args -> tweakArgs' @as args + tweakArgs'' = case Data.SOP.Dict.mapAll @more @less evidence' of + f -> case f (Dict @(All more) @as) of + Dict -> \args -> tweakArgs' @as args in tweakArgs'' ) tweakExecution' in captureExistential evidence proxy tweakArgs tweakExecution - - -translateEvidence :: forall more less a. (forall x. more x :- less x) -> SOP.Dict more a -> SOP.Dict less a -translateEvidence evidence SOP.Dict = - case evidence @a of - Sub Dict -> SOP.Dict @less @a -- $sop -- Some useful definitions re-exported the from \"sop-core\" package.
test/tests.hs view
@@ -29,6 +29,7 @@ import Test.Tasty.HUnit import Prelude hiding (log) import Data.Proxy +import System.IO -- Some helper typeclasses. -- @@ -254,9 +255,8 @@ returnMempty'' :: forall ca e m r. (Monad m, Monoid r, Show r, Read r) => Advice ca e m r returnMempty'' = returnMempty <> justAResultConstraint --- --- returnMempty'' = restrictResult @(Monoid `And` Show) (Sub Dict) returnMempty --- +printArgs' = restrictArgs @(Eq `And` Ord `And` Show) (\Dict -> Dict) (printArgs @NilEnv @IO stdout "foo") + -- does EnvConstraint compile? -- type FooAdvice = Advice Top (EnvConstraint (MustBe NilEnv)) Top