module Darcs.Patch.Choices
(
PatchChoices
, Slot(..)
, patchChoices
, mkPatchChoices
, patchSlot
, getChoices
, separateFirstMiddleFromLast
, separateFirstFromMiddleLast
, forceMatchingFirst
, forceFirsts
, forceFirst
, forceMatchingLast
, forceLasts
, forceLast
, forceMiddle
, makeEverythingSooner
, makeEverythingLater
, selectAllMiddles
, refineChoices
, substitute
, LabelledPatch
, Label
, label
, unLabel
, labelPatches
, getLabelInt
) where
import Darcs.Prelude
import Darcs.Patch.Invert ( Invert, invert )
import Darcs.Patch.Commute ( Commute, commute, commuteRL )
import Darcs.Patch.Inspect ( PatchInspect, listTouchedFiles, hunkMatches )
import Darcs.Patch.Permutations ( commuteWhatWeCanRL, commuteWhatWeCanFL )
import Darcs.Patch.Witnesses.Eq ( EqCheck(..) )
import Darcs.Patch.Witnesses.Ordered
( FL(..), RL(..)
, (:>)(..), (:||:)(..)
, zipWithFL, mapFL_FL, concatFL
, (+>+), reverseRL, anyFL )
import Darcs.Patch.Witnesses.Sealed ( Sealed2(..) )
import Darcs.Patch.Witnesses.Unsafe ( unsafeCoerceP )
data Label = Label (Maybe Label) Int deriving Eq
data LabelledPatch p wX wY = LP Label (p wX wY)
data PatchChoice p wX wY = PC
{ pcPatch :: (LabelledPatch p wX wY)
, _pcIsLast :: Bool
}
pcSetLast :: Bool -> LabelledPatch p wX wY -> PatchChoice p wX wY
pcSetLast = flip PC
data PatchChoices p wX wY where
PCs :: { pcsFirsts :: FL (LabelledPatch p) wX wM
, pcsMiddleLasts :: FL (PatchChoice p) wM wY}
-> PatchChoices p wX wY
data Slot = InFirst | InMiddle | InLast
label :: LabelledPatch p wX wY -> Label
label (LP tg _) = tg
getLabelInt :: Label -> Int
getLabelInt (Label _ i) = i
unLabel :: LabelledPatch p wX wY -> p wX wY
unLabel (LP _ p) = p
compareLabels :: LabelledPatch p wA wB -> LabelledPatch p wC wD -> EqCheck (wA, wB) (wC, wD)
compareLabels (LP l1 _) (LP l2 _) = if l1 == l2 then unsafeCoerceP IsEq else NotEq
instance Invert p => Invert (LabelledPatch p) where
invert (LP t p) = LP t (invert p)
instance Commute p => Commute (LabelledPatch p) where
commute (LP l1 p1 :> LP l2 p2) = do
p2' :> p1' <- commute (p1 :> p2)
return (LP l2 p2' :> LP l1 p1')
instance PatchInspect p => PatchInspect (LabelledPatch p) where
listTouchedFiles = listTouchedFiles . unLabel
hunkMatches f = hunkMatches f . unLabel
instance Commute p => Commute (PatchChoice p) where
commute (PC p1 c1 :> PC p2 c2) = do
p2' :> p1' <- commute (p1 :> p2)
return (PC p2' c2 :> PC p1' c1)
instance PatchInspect p => PatchInspect (PatchChoice p) where
listTouchedFiles = listTouchedFiles . pcPatch
hunkMatches f = hunkMatches f . pcPatch
patchChoices :: FL p wX wY -> PatchChoices p wX wY
patchChoices = mkPatchChoices . labelPatches Nothing
labelPatches :: Maybe Label -> FL p wX wY -> FL (LabelledPatch p) wX wY
labelPatches tg ps = zipWithFL LP (map (Label tg) [1..]) ps
mkPatchChoices :: FL (LabelledPatch p) wX wY -> PatchChoices p wX wY
mkPatchChoices = PCs NilFL . mapFL_FL (pcSetLast False)
separateFirstFromMiddleLast :: PatchChoices p wX wZ
-> (FL (LabelledPatch p) :> FL (LabelledPatch p)) wX wZ
separateFirstFromMiddleLast (PCs f ml) = f :> mapFL_FL pcPatch ml
separateFirstMiddleFromLast :: Commute p
=> PatchChoices p wX wZ
-> (FL (LabelledPatch p) :> FL (LabelledPatch p)) wX wZ
separateFirstMiddleFromLast (PCs f l) =
case pushLasts l of
(m :> l') -> f +>+ m :> l'
getChoices :: Commute p
=> PatchChoices p wX wY
-> (FL (LabelledPatch p) :> FL (LabelledPatch p) :> FL (LabelledPatch p)) wX wY
getChoices (PCs f ml) =
case pushLasts ml of
(m :> l') -> f :> m :> l'
pushLasts :: Commute p
=> FL (PatchChoice p) wX wY
-> (FL (LabelledPatch p) :> FL (LabelledPatch p)) wX wY
pushLasts NilFL = NilFL :> NilFL
pushLasts (PC lp False :>: pcs) =
case pushLasts pcs of
(m :> l) -> (lp :>: m) :> l
pushLasts (PC lp True :>: pcs) =
case pushLasts pcs of
(m :> l) ->
case commuteWhatWeCanFL (lp :> m) of
(m' :> lp' :> deps) -> m' :> (lp' :>: deps +>+ l)
refineChoices :: (Commute p, Monad m)
=> (forall wU wV . FL (LabelledPatch p) wU wV ->
PatchChoices p wU wV -> m (PatchChoices p wU wV))
-> PatchChoices p wX wY -> m (PatchChoices p wX wY)
refineChoices act ps =
case getChoices ps of
(f :> m :> l) -> do
(PCs f' l') <- act m (mkPatchChoices m)
return . PCs (f +>+ f') $ l' +>+ mapFL_FL (pcSetLast True) l
patchSlot :: forall p wA wB wX wY. Commute p
=> LabelledPatch p wA wB
-> PatchChoices p wX wY
-> (Slot, PatchChoices p wX wY)
patchSlot (LP t _) pc@(PCs f ml)
| foundIn f = (InFirst, pc)
| otherwise = psLast f NilRL NilRL ml
where
foundIn = anyFL ((== t) . label)
psLast :: forall wM wC wL .
FL (LabelledPatch p) wX wM ->
RL (LabelledPatch p) wM wC ->
RL (LabelledPatch p) wC wL ->
FL (PatchChoice p) wL wY ->
(Slot, PatchChoices p wX wY)
psLast firsts middles bubble (PC lp True :>: ls)
| label lp == t = (InLast
, PCs { pcsFirsts = firsts
, pcsMiddleLasts = settleM middles
+>+ settleB bubble
+>+ PC lp True :>: ls})
psLast firsts middles bubble (PC lp False :>: ls)
| label lp == t =
case commuteRL (bubble :> lp) of
Just (lp' :> bubble') -> (InMiddle,
PCs { pcsFirsts = firsts
, pcsMiddleLasts = settleM middles
+>+ PC lp' False
:>: settleB bubble'
+>+ ls})
Nothing -> (InLast,
PCs { pcsFirsts = firsts
, pcsMiddleLasts = settleM middles
+>+ settleB bubble
+>+ PC lp True
:>: ls})
psLast firsts middles bubble (PC lp True :>: ls) =
psLast firsts middles (bubble :<: lp) ls
psLast firsts middles bubble (PC lp False :>: ls) =
case commuteRL (bubble :> lp) of
Just (lp' :> bubble') -> psLast firsts (middles :<: lp') bubble' ls
Nothing -> psLast firsts middles (bubble :<: lp) ls
psLast _ _ _ NilFL = error "impossible case"
settleM middles = mapFL_FL (\lp -> PC lp False) $ reverseRL middles
settleB bubble = mapFL_FL (\lp -> PC lp True) $ reverseRL bubble
forceMatchingFirst :: forall p wA wB. Commute p
=> ( forall wX wY . LabelledPatch p wX wY -> Bool)
-> PatchChoices p wA wB
-> PatchChoices p wA wB
forceMatchingFirst pred (PCs f0 ml) = fmfLasts f0 NilRL ml
where
fmfLasts :: FL (LabelledPatch p) wA wM
-> RL (PatchChoice p) wM wN
-> FL (PatchChoice p) wN wB
-> PatchChoices p wA wB
fmfLasts f l1 (a :>: l2)
| pred_pc a =
case commuteWhatWeCanRL (l1 :> a) of
(deps :> a' :> l1') ->
let
f' = f +>+ mapFL_FL pcPatch (reverseRL deps) +>+ (pcPatch a' :>: NilFL)
in fmfLasts f' l1' l2
fmfLasts f l1 (a :>: l2) = fmfLasts f (l1 :<: a) l2
fmfLasts f l1 NilFL = PCs { pcsFirsts = f
, pcsMiddleLasts = reverseRL l1 }
pred_pc :: forall wX wY . PatchChoice p wX wY -> Bool
pred_pc (PC lp _) = pred lp
forceFirsts :: Commute p
=> [Label] -> PatchChoices p wA wB -> PatchChoices p wA wB
forceFirsts ps = forceMatchingFirst ((`elem` ps) . label)
forceFirst :: Commute p
=> Label -> PatchChoices p wA wB -> PatchChoices p wA wB
forceFirst p = forceMatchingFirst ((== p) . label)
selectAllMiddles :: forall p wX wY. Commute p
=> Bool -> PatchChoices p wX wY -> PatchChoices p wX wY
selectAllMiddles True (PCs f l) = PCs f (mapFL_FL g l)
where g (PC lp _) = PC lp True
selectAllMiddles False (PCs f l) = samf f NilRL NilRL l
where
samf :: forall wM1 wM2 wM3 .
FL (LabelledPatch p) wX wM1 ->
RL (LabelledPatch p) wM1 wM2 ->
RL (PatchChoice p) wM2 wM3 ->
FL (PatchChoice p) wM3 wY ->
PatchChoices p wX wY
samf f1 f2 l1 (pc@(PC lp False) :>: l2) =
case commuteRL (l1 :> pc) of
Nothing -> samf f1 f2 (l1 :<: PC lp True) l2
Just ((PC lp' _) :> l1') -> samf f1 (f2 :<: lp') l1' l2
samf f1 f2 l1 (PC lp True :>: l2) = samf f1 f2 (l1 :<: PC lp True) l2
samf f1 f2 l1 NilFL = PCs (f1 +>+ reverseRL f2) (reverseRL l1)
forceMatchingLast :: Commute p => (forall wX wY . LabelledPatch p wX wY -> Bool)
-> PatchChoices p wA wB
-> PatchChoices p wA wB
forceMatchingLast pred (PCs f ml) =
forceMatchingMiddleOrLast pred True NilRL f ml
forceMatchingMiddleOrLast
:: forall p wA wB wM1 wM2 . Commute p
=> (forall wX wY . LabelledPatch p wX wY -> Bool)
-> Bool
-> RL (LabelledPatch p) wA wM1
-> FL (LabelledPatch p) wM1 wM2
-> FL (PatchChoice p) wM2 wB
-> PatchChoices p wA wB
forceMatchingMiddleOrLast pred b f1 (a :>: f2) ml
| pred a =
case commuteWhatWeCanFL (a :> f2) of
(f2' :> a' :> deps) ->
let
ml' = mapFL_FL (pcSetLast b) (a' :>: deps) +>+ ml
in
forceMatchingMiddleOrLast pred b f1 f2' ml'
forceMatchingMiddleOrLast pred b f1 (a :>: f2) ml =
forceMatchingMiddleOrLast pred b (f1 :<: a) f2 ml
forceMatchingMiddleOrLast pred b f1 NilFL ml =
PCs { pcsFirsts = reverseRL f1
, pcsMiddleLasts = mapFL_FL choose ml
}
where
choose (PC lp c) = (PC lp (if pred lp then b else c) )
forceLasts :: Commute p
=> [Label] -> PatchChoices p wA wB -> PatchChoices p wA wB
forceLasts ps = forceMatchingLast ((`elem` ps) . label)
forceLast :: Commute p
=> Label -> PatchChoices p wA wB -> PatchChoices p wA wB
forceLast p = forceMatchingLast ((== p) . label)
forceMiddle :: Commute p => Label -> PatchChoices p wA wB -> PatchChoices p wA wB
forceMiddle t (PCs f l) = forceMatchingMiddleOrLast ((== t) . label) False NilRL f l
makeEverythingLater :: PatchChoices p wX wY -> PatchChoices p wX wY
makeEverythingLater (PCs f ml) =
let m = mapFL_FL (pcSetLast False) f
ml' = mapFL_FL (\(PC lp _) -> PC lp True) ml
in PCs NilFL $ m +>+ ml'
makeEverythingSooner :: forall p wX wY. Commute p
=> PatchChoices p wX wY -> PatchChoices p wX wY
makeEverythingSooner (PCs f ml) =
case mes NilRL NilRL ml
of (m :> ml') ->
PCs (f +>+ m) ml'
where
mes :: forall wM1 wM2 wM3 .
RL (LabelledPatch p) wM1 wM2 ->
RL (LabelledPatch p) wM2 wM3 ->
FL (PatchChoice p) wM3 wY ->
(FL (LabelledPatch p) :> FL (PatchChoice p)) wM1 wY
mes middle bubble (PC lp True :>: mls) = mes middle (bubble :<: lp) mls
mes middle bubble (PC lp False :>: mls) =
case commuteRL (bubble :> lp) of
Nothing -> mes middle (bubble :<: lp) mls
Just (lp' :> bubble') -> mes (middle :<: lp') bubble' mls
mes middle bubble NilFL =
(reverseRL middle) :> mapFL_FL (\lp -> PC lp False) (reverseRL bubble)
substitute :: forall p wX wY .
Sealed2 (LabelledPatch p :||: FL (LabelledPatch p))
-> PatchChoices p wX wY
-> PatchChoices p wX wY
substitute (Sealed2 (lp :||: new_lps)) (PCs f l) =
PCs (concatFL $ mapFL_FL substLp f) (concatFL $ mapFL_FL substPc l)
where
substLp :: LabelledPatch p wA wB -> FL (LabelledPatch p) wA wB
substLp lp'
| IsEq <- compareLabels lp lp' = new_lps
| otherwise = lp' :>: NilFL
substPc :: PatchChoice p wA wB -> FL (PatchChoice p) wA wB
substPc (PC lp' c)
| IsEq <- compareLabels lp lp' = mapFL_FL (pcSetLast c) new_lps
| otherwise = PC lp' c :>: NilFL