Skip to content

Commit c303cc0

Browse files
committed
refactor(cabal-install, cabal-install-solver): avoid using "error" in validateSolverResult
- add step to Progress - add MonadFail Progress instance - refactor validateSolverResult
1 parent ac8916a commit c303cc0

File tree

3 files changed

+65
-47
lines changed

3 files changed

+65
-47
lines changed

cabal-install-solver/src/Distribution/Solver/Types/Progress.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{-# LANGUAGE DeriveFunctor #-}
2+
{-# LANGUAGE FlexibleInstances #-}
23
module Distribution.Solver.Types.Progress
34
( Progress(..)
45
, foldProgress
6+
, step
7+
, fail
58
) where
69

710
import Prelude ()
8-
import Distribution.Solver.Compat.Prelude hiding (fail)
11+
import Distribution.Solver.Compat.Prelude
912

1013
-- | A type to represent the unfolding of an expensive long running
1114
-- calculation that may fail. We may get intermediate steps before the final
@@ -16,6 +19,9 @@ data Progress step fail done = Step step (Progress step fail done)
1619
| Done done
1720
deriving (Functor)
1821

22+
step :: step -> Progress step fail ()
23+
step s = Step s (Done ())
24+
1925
-- | Consume a 'Progress' calculation. Much like 'foldr' for lists but with two
2026
-- base cases, one for a final result and one for failure.
2127
--
@@ -25,15 +31,18 @@ data Progress step fail done = Step step (Progress step fail done)
2531
--
2632
foldProgress :: (step -> a -> a) -> (fail -> a) -> (done -> a)
2733
-> Progress step fail done -> a
28-
foldProgress step fail done = fold
29-
where fold (Step s p) = step s (fold p)
30-
fold (Fail f) = fail f
31-
fold (Done r) = done r
34+
foldProgress step_ fail_ done_ = fold
35+
where fold (Step s p) = step_ s (fold p)
36+
fold (Fail f) = fail_ f
37+
fold (Done r) = done_ r
3238

3339
instance Monad (Progress step fail) where
3440
return = pure
3541
p >>= f = foldProgress Step Fail f p
3642

43+
instance MonadFail (Progress step String) where
44+
fail = Fail
45+
3746
instance Applicative (Progress step fail) where
3847
pure a = Done a
3948
p <*> x = foldProgress Step Fail (flip fmap x) p

cabal-install/src/Distribution/Client/Dependency.hs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
{-# LANGUAGE FlexibleContexts #-}
12
-----------------------------------------------------------------------------
2-
33
-----------------------------------------------------------------------------
4+
{-# LANGUAGE LambdaCase #-}
45

56
-- |
67
-- Module : Distribution.Client.Dependency
@@ -781,35 +782,42 @@ resolveDependencies
781782
-> Staged InstalledPackageIndex
782783
-> DepResolverParams
783784
-> Progress String String SolverInstallPlan
784-
resolveDependencies toolchains pkgConfigDB installedPkgIndex params =
785-
Step (showDepResolverParams finalparams) $
786-
fmap (validateSolverResult toolchains) $
787-
formatProgress $
788-
runSolver
789-
( SolverConfig
790-
reordGoals
791-
cntConflicts
792-
fineGrained
793-
minimize
794-
noReinstalls
795-
shadowing
796-
strFlags
797-
onlyConstrained_
798-
maxBkjumps
799-
enableBj
800-
solveExes
801-
order
802-
verbosity
803-
(PruneAfterFirstSuccess False)
804-
)
805-
toolchains
806-
pkgConfigDB
807-
(fmap installedPkgIndexM installedPkgIndex)
808-
sourcePkgIndex
809-
preferences
810-
constraints
811-
targets
785+
resolveDependencies toolchains pkgConfigDB installedPkgIndex params = do
786+
step (showDepResolverParams finalparams)
787+
pkgs <-
788+
formatProgress $
789+
runSolver
790+
config
791+
toolchains
792+
pkgConfigDB
793+
installedPkgIndex'
794+
sourcePkgIndex
795+
preferences
796+
constraints
797+
targets
798+
validateSolverResult toolchains pkgs
812799
where
800+
installedPkgIndex' = Staged $ \case
801+
Build -> getStage installedPkgIndex Build
802+
Host -> installedPkgIndexM (getStage installedPkgIndex Host)
803+
804+
config =
805+
SolverConfig
806+
reordGoals
807+
cntConflicts
808+
fineGrained
809+
minimize
810+
noReinstalls
811+
shadowing
812+
strFlags
813+
onlyConstrained_
814+
maxBkjumps
815+
enableBj
816+
solveExes
817+
order
818+
verbosity
819+
(PruneAfterFirstSuccess False)
820+
813821
finalparams@( DepResolverParams
814822
targets
815823
constraints
@@ -906,13 +914,14 @@ interpretPackagesPreference selected defaultPref prefs =
906914
validateSolverResult
907915
:: Staged (CompilerInfo, Platform)
908916
-> [ResolverPackage UnresolvedPkgLoc]
909-
-> SolverInstallPlan
917+
-> Progress String String SolverInstallPlan
910918
validateSolverResult toolchains pkgs =
911919
case planPackagesProblems toolchains pkgs of
912920
[] -> case SolverInstallPlan.new graph of
913-
Right plan -> plan
914-
Left problems -> error (formatPlanProblems problems)
915-
problems -> error (formatPkgProblems problems)
921+
Right plan -> return plan
922+
Left problems ->
923+
fail (formatPlanProblems problems)
924+
problems -> fail (formatPkgProblems problems)
916925
where
917926
graph :: Graph.Graph (ResolverPackage UnresolvedPkgLoc)
918927
graph = Graph.fromDistinctList pkgs

cabal-install/tests/UnitTests/Distribution/Solver/Modular/RetryLog.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ tests =
3535
\p (Blind f) ->
3636
toProgress (retry (fromProgress p) (fromProgress . f))
3737
=== (foldProgress Step f Done (p :: Log Int) :: Log Int)
38-
, testProperty "failWith" $ \step failure ->
39-
toProgress (failWith step failure)
40-
=== (Step step (Fail failure) :: Log Int)
41-
, testProperty "succeedWith" $ \step success ->
42-
toProgress (succeedWith step success)
43-
=== (Step step (Done success) :: Log Int)
44-
, testProperty "continueWith" $ \step p ->
45-
toProgress (continueWith step (fromProgress p))
46-
=== (Step step p :: Log Int)
38+
, testProperty "failWith" $ \step' failure ->
39+
toProgress (failWith step' failure)
40+
=== (Step step' (Fail failure) :: Log Int)
41+
, testProperty "succeedWith" $ \step' success ->
42+
toProgress (succeedWith step' success)
43+
=== (Step step' (Done success) :: Log Int)
44+
, testProperty "continueWith" $ \step' p ->
45+
toProgress (continueWith step' (fromProgress p))
46+
=== (Step step' p :: Log Int)
4747
, testCase "tryWith with failure" $
4848
let failure = Fail "Error"
4949
s = Step Success

0 commit comments

Comments
 (0)