Skip to content

Commit e7b2c3c

Browse files
committed
Remove state monad, and store state at the end of the log.
1 parent 0c12435 commit e7b2c3c

File tree

1 file changed

+46
-46
lines changed
  • cabal-install/Distribution/Solver/Modular

1 file changed

+46
-46
lines changed

cabal-install/Distribution/Solver/Modular/Explore.hs

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Distribution.Solver.Modular.Explore
44
, backjumpAndExplore
55
) where
66

7-
import Control.Monad.State.Lazy
87
import Data.Foldable as F
98
import Data.List as L (foldl')
109
import Data.Map as M
@@ -20,8 +19,6 @@ import Distribution.Solver.Types.PackagePath
2019
import Distribution.Solver.Types.Settings (EnableBackjumping(..), CountConflicts(..))
2120
import qualified Distribution.Solver.Types.Progress as P
2221

23-
type Explore = State ConflictMap
24-
2522
-- | This function takes the variable we're currently considering, an
2623
-- initial conflict set and a
2724
-- list of children's logs. Each log yields either a solution or a
@@ -46,29 +43,29 @@ type Explore = State ConflictMap
4643
-- variable. See also the comments for 'avoidSet'.
4744
--
4845
backjump :: EnableBackjumping -> Var QPN
49-
-> ConflictSet QPN -> P.PSQ k (Explore (ConflictSetLog a))
50-
-> Explore (ConflictSetLog a)
46+
-> ConflictSet QPN -> P.PSQ k (ConflictMap -> ConflictSetLog a)
47+
-> ConflictMap -> ConflictSetLog a
5148
backjump (EnableBackjumping enableBj) var initial xs =
5249
F.foldr combine logBackjump xs initial
5350
where
54-
combine :: Explore (ConflictSetLog a)
55-
-> (ConflictSet QPN -> Explore (ConflictSetLog a))
56-
-> ConflictSet QPN -> Explore (ConflictSetLog a)
57-
combine x f csAcc = do
58-
l <- x
59-
case l of
60-
P.Done d -> return (P.Done d)
61-
P.Fail cs
62-
| enableBj && not (var `CS.member` cs) -> logBackjump cs
63-
| otherwise -> f (csAcc `CS.union` cs)
64-
P.Step m ms -> do
65-
l' <- combine (return ms) f csAcc
66-
return (P.Step m l')
51+
combine :: (ConflictMap -> ConflictSetLog a)
52+
-> (ConflictSet QPN -> ConflictMap -> ConflictSetLog a)
53+
-> ConflictSet QPN -> ConflictMap -> ConflictSetLog a
54+
combine x f csAcc cm =
55+
let l = x cm
56+
in case l of
57+
P.Done d -> P.Done d
58+
P.Fail (cs, cm')
59+
| enableBj && not (var `CS.member` cs) -> logBackjump cs cm'
60+
| otherwise -> f (csAcc `CS.union` cs) cm'
61+
P.Step m ms ->
62+
let l' = combine (\ _ -> ms) f csAcc cm
63+
in P.Step m l'
6764

68-
logBackjump :: ConflictSet QPN -> Explore (ConflictSetLog a)
69-
logBackjump cs = return (failWith (Failure cs Backjump) cs)
65+
logBackjump :: ConflictSet QPN -> ConflictMap -> ConflictSetLog a
66+
logBackjump cs cm = failWith (Failure cs Backjump) (cs, cm)
7067

71-
type ConflictSetLog = P.Progress Message (ConflictSet QPN)
68+
type ConflictSetLog = P.Progress Message (ConflictSet QPN, ConflictMap)
7269

7370
type ConflictMap = Map (Var QPN) Int
7471

@@ -95,51 +92,54 @@ updateCM cs cm =
9592
-- | A tree traversal that simultaneously propagates conflict sets up
9693
-- the tree from the leaves and creates a log.
9794
exploreLog :: EnableBackjumping -> CountConflicts -> Tree QGoalReason
98-
-> (Assignment -> Explore (ConflictSetLog (Assignment, RevDepMap)))
95+
-> (Assignment -> ConflictMap -> ConflictSetLog (Assignment, RevDepMap))
9996
exploreLog enableBj (CountConflicts countConflicts) = cata go
10097
where
101-
updateCM' :: ConflictSet QPN -> Explore a -> Explore a
98+
updateCM' :: ConflictSet QPN -> ConflictMap -> ConflictMap
10299
updateCM'
103-
| countConflicts = \ c k -> modify' (updateCM c) >> k
104-
| otherwise = \ _ k -> k
100+
| countConflicts = \ c cm -> updateCM c cm
101+
| otherwise = \ _ cm -> cm
105102

106-
getBestGoal' :: P.PSQ (Goal QPN) a -> Explore (Goal QPN, a)
103+
getBestGoal' :: P.PSQ (Goal QPN) a -> ConflictMap -> (Goal QPN, a)
107104
getBestGoal'
108-
| countConflicts = \ ts -> get >>= \ cm -> return (getBestGoal cm ts)
109-
| otherwise = return . getFirstGoal
105+
| countConflicts = \ ts cm -> getBestGoal cm ts
106+
| otherwise = \ ts _ -> getFirstGoal ts
110107

111-
go :: TreeF QGoalReason (Assignment -> Explore (ConflictSetLog (Assignment, RevDepMap)))
112-
-> (Assignment -> Explore (ConflictSetLog (Assignment, RevDepMap)))
113-
go (FailF c fr) _ = updateCM' c (return (failWith (Failure c fr) c))
114-
go (DoneF rdm) a = return (succeedWith Success (a, rdm))
108+
go :: TreeF QGoalReason (Assignment -> ConflictMap -> ConflictSetLog (Assignment, RevDepMap))
109+
-> (Assignment -> ConflictMap -> ConflictSetLog (Assignment, RevDepMap))
110+
go (FailF c fr) _ = \ cm -> let failure = failWith (Failure c fr)
111+
in if countConflicts
112+
then failure (c, updateCM' c cm)
113+
else failure (c, cm)
114+
go (DoneF rdm) a = \ _ -> succeedWith Success (a, rdm)
115115
go (PChoiceF qpn gr ts) (A pa fa sa) =
116116
backjump enableBj (P qpn) (avoidSet (P qpn) gr) $ -- try children in order,
117117
P.mapWithKey -- when descending ...
118-
(\ i@(POption k _) r -> do
119-
l <- r (A (M.insert qpn k pa) fa sa)
120-
return (tryWith (TryP qpn i) l)
118+
(\ i@(POption k _) r cm ->
119+
let l = r (A (M.insert qpn k pa) fa sa) cm
120+
in tryWith (TryP qpn i) l
121121
)
122122
ts
123123
go (FChoiceF qfn gr _ _ ts) (A pa fa sa) =
124124
backjump enableBj (F qfn) (avoidSet (F qfn) gr) $ -- try children in order,
125125
P.mapWithKey -- when descending ...
126-
(\ k r -> do
127-
l <- r (A pa (M.insert qfn k fa) sa)
128-
return (tryWith (TryF qfn k) l)
126+
(\ k r cm ->
127+
let l = r (A pa (M.insert qfn k fa) sa) cm
128+
in tryWith (TryF qfn k) l
129129
)
130130
ts
131131
go (SChoiceF qsn gr _ ts) (A pa fa sa) =
132132
backjump enableBj (S qsn) (avoidSet (S qsn) gr) $ -- try children in order,
133133
P.mapWithKey -- when descending ...
134-
(\ k r -> do
135-
l <- r (A pa fa (M.insert qsn k sa))
136-
return (tryWith (TryS qsn k) l)
134+
(\ k r cm ->
135+
let l = r (A pa fa (M.insert qsn k sa)) cm
136+
in tryWith (TryS qsn k) l
137137
)
138138
ts
139-
go (GoalChoiceF ts) a = do
140-
(k, v) <- getBestGoal' ts
141-
l <- v a
142-
return (continueWith (Next k) l)
139+
go (GoalChoiceF ts) a = \ cm ->
140+
let (k, v) = getBestGoal' ts cm
141+
l = v a cm
142+
in continueWith (Next k) l
143143

144144
-- | Build a conflict set corresponding to the (virtual) option not to
145145
-- choose a solution for a goal at all.
@@ -173,7 +173,7 @@ backjumpAndExplore :: EnableBackjumping
173173
-> CountConflicts
174174
-> Tree QGoalReason -> Log Message (Assignment, RevDepMap)
175175
backjumpAndExplore enableBj countConflicts t =
176-
toLog $ fst $ runState (exploreLog enableBj countConflicts t (A M.empty M.empty M.empty)) M.empty
176+
toLog $ (exploreLog enableBj countConflicts t (A M.empty M.empty M.empty)) M.empty
177177
where
178178
toLog :: P.Progress step fail done -> Log step done
179179
toLog = P.foldProgress P.Step (const (P.Fail ())) P.Done

0 commit comments

Comments
 (0)