New submission from Ben Franksen <[email protected]>:

4 patches for repository https://darcs.net/screened:

patch 9869d614ff789fa57d554ad1996e85d99e6fc116
Author: Ben Franksen <[email protected]>
Date:   Fri Apr 22 08:51:13 CEST 2022
  * Prim.V1: inline isInDirectory and isSuperdir

patch cbcc171edd595d725ac377141d488d7688451e7b
Author: Ben Franksen <[email protected]>
Date:   Fri Apr 22 08:57:38 CEST 2022
  * Prim.V1: simplify some commute cases

  These simplifications are due to f == g => f `isPrefix` g. This refactor is
  pure boolean logic.

patch 91387a15fe3b42aa70c3f91d2b45fac0d4613114
Author: Ben Franksen <[email protected]>
Date:   Fri Apr 22 09:10:22 CEST 2022
  * Prim.V1: in commuteFiledir use guard style for all clauses

  Again a pure refactor for readability.

patch 55d1c8720e84d9d169cdb70f0614e10cdb808393
Author: Ben Franksen <[email protected]>
Date:   Tue Apr 26 11:58:58 CEST 2022
  * Prim.V1: list all cases explicitly

  This removes the Perhaps data type, speedyCommute, and
  everythingElseCommute, and renames cleverCommute to invertCommute.

  The complication introduced by the Perhaps data type with an extra Unknown
  constructor was only there to save a few case distinctions. Listing them
  explicitly is clearer and adds only a few lines of code which can be easily
  seen to be correct as they are all generically implemented in terms of
  invertCommute, exactly as before. The questionable speedyCommute is no
  longer used: it doesn't actually optimize anything since it merely
  duplicates parts of the first case of commuteFileDir. Finally, moving the
  two extra cases for ChangePref patches from everythingElseCommute into
  commuteFileDir allows us to avoid any fall-through wildcard case, making the
  code more robust and proving that all cases are handled. The extra cases for
  commuteFP (instead of a single fall-through case) are also meant to improve
  clarity and robustness; their common implementation (failed) follows
  logically from the combination of how the former cleverCommute and toMaybe
  functions handled Unknown results.

----------
files: patch-preview.txt, prim_v1_-inline-isindirectory-and-issuperdir.dpatch
messages: 24149
nosy: bfrk
status: needs-screening
title: Prim.V1: inline isInDirectory and isSupe... (and 3 more)

__________________________________
Darcs bug tracker <[email protected]>
<https://bugs.darcs.net/patch2443>
__________________________________
[Prim.V1: inline isInDirectory and isSuperdir
Ben Franksen <[email protected]>**20220422065113
 Ignore-this: 1a13395b57be0077d23d6eaa057b03773bf580b0a5a8e31d7e063651dc136ceaf25eaea79b822839
] hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 24
-isSuperdir :: AnchoredPath -> AnchoredPath -> Bool
-isSuperdir d1 d2 = isPrefix d1 d2 && d1 /= d2
-
-{-
-This is the original definition.
-Note that it explicitly excludes equality:
-
-isSuperdir d1 d2 = isd (fn2fp d1) (fn2fp d2)
-  where
-    isd s1 s2 =
-      length s2 >= length s1 + 1 && take (length s1 + 1) s2 == s1 ++ "/"
--}
-
-isInDirectory :: AnchoredPath -> AnchoredPath -> Bool
-isInDirectory = isPrefix
-{-
-Again, here is the orginial definition:
-isInDirectory d f = iid (fn2fp d) (fn2fp f)
-    where iid (cd:cds) (cf:cfs)
-              | cd /= cf = False
-              | otherwise = iid cds cfs
-          iid [] ('/':_) = True
-          iid [] [] = True -- Count directory itself as being in directory...
-          iid _ _ = False
--}
-
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 109
-  if not (isInDirectory d1 d2 || isInDirectory d2 d1) && d1 /= d2
+  if not (isPrefix d1 d2 || isPrefix d2 d1) && d1 /= d2
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 113
-    if not $ isInDirectory d f
+    if not $ isPrefix d f
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 116
-
--- FIXME using isSuperdir here makes no sense, should use just isPrefix
-
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 121
-    | isSuperdir d1 d' || isSuperdir d1 d = Failed
+    | (isPrefix d1 d' && d1 /= d') || (isPrefix d1 d && d1 /= d) = Failed
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 130
-    | d `isSuperdir` f && f' `isSuperdir` d' = Failed
+    | d `isPrefix` f && d /= f && f' `isPrefix` d' && f' /= d' = Failed

[Prim.V1: simplify some commute cases
Ben Franksen <[email protected]>**20220422065738
 Ignore-this: bdb00f4dfab5c5d4fa4942f8078a369e0fe8c9559e6e05e687fc6d3369c84d2da51b013b52067b1a
 
 These simplifications are due to f == g => f `isPrefix` g. This refactor is
 pure boolean logic.
] hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 109
-  if not (isPrefix d1 d2 || isPrefix d2 d1) && d1 /= d2
+  if not (isPrefix d1 d2 || isPrefix d2 d1)
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 121
-    | (isPrefix d1 d' && d1 /= d') || (isPrefix d1 d && d1 /= d) = Failed
-    | d == d1 = Failed  -- The exact guard is p1 == AddDir && d == d1
-                        -- but note d == d1 suffices because we know p1 != RmDir
-                        -- (and hence p1 == AddDir) since patches must be sequential.
-    | d1 == d' = Failed
+    | isPrefix d1 d' || isPrefix d1 d = Failed
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 126
-    | d `isPrefix` f && d /= f && f' `isPrefix` d' && f' /= d' = Failed
+    | d `isPrefix` f && f' `isPrefix` d' = Failed

[Prim.V1: in commuteFiledir use guard style for all clauses
Ben Franksen <[email protected]>**20220422071022
 Ignore-this: f3fe71699710d15230b94ee63616829fc6e2cae8e62cc276c26356bc79bd10d97c6267a3281255e1
 
 Again a pure refactor for readability.
] hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 105
-commuteFiledir (FP f1 p1 :> FP f2 p2) =
-  if f1 /= f2 then Succeeded ( FP f2 (unsafeCoerceP p2) :> FP f1 (unsafeCoerceP p1) )
-  else commuteFP f1 (p1 :> p2)
-commuteFiledir (DP d1 p1 :> DP d2 p2) =
-  if not (isPrefix d1 d2 || isPrefix d2 d1)
-  then Succeeded ( DP d2 (unsafeCoerceP p2) :> DP d1 (unsafeCoerceP p1) )
-  else Failed
-commuteFiledir (FP f fp :> DP d dp) =
-    if not $ isPrefix d f
-    then Succeeded (DP d (unsafeCoerceP dp) :> FP f (unsafeCoerceP fp))
-    else Failed
+commuteFiledir (FP f1 p1 :> FP f2 p2)
+    | f1 == f2 = commuteFP f1 (p1 :> p2)
+    | otherwise = Succeeded (FP f2 (unsafeCoerceP p2) :> FP f1 (unsafeCoerceP p1))
+commuteFiledir (DP d1 p1 :> DP d2 p2)
+    | isPrefix d1 d2 || isPrefix d2 d1 = Failed
+    | otherwise = Succeeded (DP d2 (unsafeCoerceP p2) :> DP d1 (unsafeCoerceP p1))
+commuteFiledir (FP f fp :> DP d dp)
+    | isPrefix d f = Failed
+    | otherwise = Succeeded (DP d (unsafeCoerceP dp) :> FP f (unsafeCoerceP fp))
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 117
-    | otherwise = Succeeded (Move d d' :> FP (movedirfilename d d' f1) (unsafeCoerceP p1))
+    | otherwise =
+        Succeeded (Move d d' :> FP (movedirfilename d d' f1) (unsafeCoerceP p1))
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 121
-    | otherwise = Succeeded (Move d d' :> DP (movedirfilename d d' d1) (unsafeCoerceP p1))
+    | otherwise =
+        Succeeded (Move d d' :> DP (movedirfilename d d' d1) (unsafeCoerceP p1))
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 130
-

[Prim.V1: list all cases explicitly
Ben Franksen <[email protected]>**20220426095858
 Ignore-this: a87f907a4d29d45be3f8756e846df28b0c6213bc9d4ee25fd2b5ccd3a6771d4813a60473498aebfe
 
 This removes the Perhaps data type, speedyCommute, and
 everythingElseCommute, and renames cleverCommute to invertCommute.
 
 The complication introduced by the Perhaps data type with an extra Unknown
 constructor was only there to save a few case distinctions. Listing them
 explicitly is clearer and adds only a few lines of code which can be easily
 seen to be correct as they are all generically implemented in terms of
 invertCommute, exactly as before. The questionable speedyCommute is no
 longer used: it doesn't actually optimize anything since it merely
 duplicates parts of the first case of commuteFileDir. Finally, moving the
 two extra cases for ChangePref patches from everythingElseCommute into
 commuteFileDir allows us to avoid any fall-through wildcard case, making the
 code more robust and proving that all cases are handled. The extra cases for
 commuteFP (instead of a single fall-through case) are also meant to improve
 clarity and robustness; their common implementation (failed) follows
 logically from the combination of how the former cleverCommute and toMaybe
 functions handled Unknown results.
] hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 6
-import Control.Monad ( MonadPlus, msum, mzero, mplus )
-import Control.Applicative ( Alternative(..) )
-
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 9
-import Darcs.Util.Path ( AnchoredPath, movedirfilename, isPrefix )
+import Darcs.Util.Path ( movedirfilename, isPrefix )
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 17
+import Darcs.Patch.CommuteFn ( CommuteFn )
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 22
-data Perhaps a = Unknown | Failed | Succeeded a
-
-instance Functor Perhaps where
-    fmap _ Unknown = Unknown
-    fmap _ Failed = Failed
-    fmap f (Succeeded x) = Succeeded (f x)
-
-instance Applicative Perhaps where
-    pure = Succeeded
-    _ <*> Failed = Failed
-    _ <*> Unknown = Unknown
-    Failed <*> _ = Failed
-    Unknown <*> _ = Unknown
-    Succeeded f <*> Succeeded x = Succeeded (f x)
-
-instance  Monad Perhaps where
-    (Succeeded x) >>= k =  k x
-    Failed   >>= _      =  Failed
-    Unknown  >>= _      =  Unknown
-    return              =  pure
-
-instance Alternative Perhaps where
-    empty = Unknown
-    Unknown <|> ys    = ys
-    Failed  <|> _     = Failed
-    (Succeeded x) <|> _ = Succeeded x
-
-instance  MonadPlus Perhaps where
-    mzero = Unknown
-    mplus = (<|>)
-
-toMaybe :: Perhaps a -> Maybe a
-toMaybe (Succeeded x) = Just x
-toMaybe _ = Nothing
-
-cleverCommute :: CommuteFunction -> CommuteFunction
-cleverCommute c (p1:>p2) =
-    case c (p1 :> p2) of
-    Succeeded x -> Succeeded x
-    Failed -> Failed
-    Unknown -> case c (invert p2 :> invert p1) of
-               Succeeded (p1' :> p2') -> Succeeded (invert p2' :> invert p1')
-               Failed -> Failed
-               Unknown -> Unknown
---cleverCommute c (p1,p2) = c (p1,p2) `mplus`
---    (case c (invert p2,invert p1) of
---     Succeeded (p1', p2') -> Succeeded (invert p2', invert p1')
---     Failed -> Failed
---     Unknown -> Unknown)
-
-speedyCommute :: CommuteFunction  -- Deal with common cases quickly!
-    -- Two file-patches modifying different files trivially commute.
-speedyCommute (p1@(FP f1 _) :> p2@(FP f2 _))
-  | f1 /= f2 = Succeeded (unsafeCoerceP p2 :> unsafeCoerceP p1)
-speedyCommute _other = Unknown
-
-everythingElseCommute :: CommuteFunction
-everythingElseCommute = eec
-  where
-    eec :: CommuteFunction
-    eec (p1 :> ChangePref p f t) = Succeeded (ChangePref p f t :> unsafeCoerceP p1)
-    eec (ChangePref p f t :> p2) = Succeeded (unsafeCoerceP p2 :> ChangePref p f t)
-    eec xx = cleverCommute commuteFiledir xx
-
-{-
-Note that it must be true that
-
-commutex (A^-1 A, P) = Just (P, A'^-1 A')
-
-and
-
-if commutex (A, B) == Just (B', A')
-then commutex (B^-1, A^-1) == Just (A'^-1, B'^-1)
--}
+failed :: Maybe a
+failed = Nothing
+
+type CommuteFunction p = CommuteFn p p
+
+-- | Use the invert-commute law.
+invertCommute :: Invert p => CommuteFunction p -> CommuteFunction p
+invertCommute c (p1:>p2) = do
+    ip1' :> ip2' <- c (invert p2 :> invert p1)
+    return (invert ip2' :> invert ip1')
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 34
-    commute x = toMaybe $ msum [speedyCommute x,
-                                everythingElseCommute x
-                               ]
-
-commuteFiledir :: CommuteFunction
+    commute = commuteFiledir
+
+commuteFiledir :: CommuteFunction Prim
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 38
-    | f1 == f2 = commuteFP f1 (p1 :> p2)
-    | otherwise = Succeeded (FP f2 (unsafeCoerceP p2) :> FP f1 (unsafeCoerceP p1))
+    | f1 == f2 = do
+        p2' :> p1' <- commuteFP (p1 :> p2)
+        return (FP f2 p2' :> FP f1 p1')
+    | otherwise = return (FP f2 (unsafeCoerceP p2) :> FP f1 (unsafeCoerceP p1))
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 43
-    | isPrefix d1 d2 || isPrefix d2 d1 = Failed
-    | otherwise = Succeeded (DP d2 (unsafeCoerceP p2) :> DP d1 (unsafeCoerceP p1))
+    | isPrefix d1 d2 || isPrefix d2 d1 = failed
+    | otherwise = return (DP d2 (unsafeCoerceP p2) :> DP d1 (unsafeCoerceP p1))
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 46
-    | isPrefix d f = Failed
-    | otherwise = Succeeded (DP d (unsafeCoerceP dp) :> FP f (unsafeCoerceP fp))
+    | isPrefix d f = failed
+    | otherwise = return (DP d (unsafeCoerceP dp) :> FP f (unsafeCoerceP fp))
+commuteFiledir pair@(DP _ _ :> FP _ _) = invertCommute commuteFiledir pair
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 50
-    | f1 == d' = Failed
-    | (p1 == AddFile || p1 == RmFile) && d == f1 = Failed
+    | f1 == d' = failed
+    | (p1 == AddFile || p1 == RmFile) && d == f1 = failed
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 53
-        Succeeded (Move d d' :> FP (movedirfilename d d' f1) (unsafeCoerceP p1))
+        return (Move d d' :> FP (movedirfilename d d' f1) (unsafeCoerceP p1))
+commuteFiledir pair@(Move _ _ :> FP _ _) = invertCommute commuteFiledir pair
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 56
-    | isPrefix d1 d' || isPrefix d1 d = Failed
+    | isPrefix d1 d' || isPrefix d1 d = failed
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 58
-        Succeeded (Move d d' :> DP (movedirfilename d d' d1) (unsafeCoerceP p1))
+        return (Move d d' :> DP (movedirfilename d d' d1) (unsafeCoerceP p1))
+commuteFiledir pair@(Move _ _ :> DP _ _) = invertCommute commuteFiledir pair
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 61
-    | f == d' || f' == d = Failed
-    | f == d || f' == d' = Failed
-    | d `isPrefix` f && f' `isPrefix` d' = Failed
+    | f == d' || f' == d = failed
+    | f == d || f' == d' = failed
+    | d `isPrefix` f && f' `isPrefix` d' = failed
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 65
-        Succeeded (Move (movedirfilename f' f d) (movedirfilename f' f d') :>
-                   Move (movedirfilename d d' f) (movedirfilename d d' f'))
-commuteFiledir _ = Unknown
-
-type CommuteFunction = forall wX wY . (Prim :> Prim) wX wY -> Perhaps ((Prim :> Prim) wX wY)
-
-commuteFP :: AnchoredPath -> (FilePatchType :> FilePatchType) wX wY
-          -> Perhaps ((Prim :> Prim) wX wY)
-commuteFP f (p1 :> Hunk line1 [] []) =
-    Succeeded (FP f (Hunk line1 [] []) :> FP f (unsafeCoerceP p1))
-commuteFP f (Hunk line1 [] [] :> p2) =
-    Succeeded (FP f (unsafeCoerceP p2) :> FP f (Hunk line1 [] []))
-commuteFP f (Hunk line1 old1 new1 :> Hunk line2 old2 new2) =
+      return
+        (Move (movedirfilename f' f d) (movedirfilename f' f d') :>
+         Move (movedirfilename d d' f) (movedirfilename d d' f'))
+commuteFiledir (p1 :> ChangePref p f t) =
+    return (ChangePref p f t :> unsafeCoerceP p1)
+commuteFiledir (ChangePref p f t :> p2) =
+    return (unsafeCoerceP p2 :> ChangePref p f t)
+
+commuteFP :: CommuteFunction FilePatchType
+commuteFP (p1 :> Hunk line1 [] []) =
+    return (Hunk line1 [] [] :> unsafeCoerceP p1)
+commuteFP (Hunk line1 [] [] :> p2) =
+    return (unsafeCoerceP p2 :> Hunk line1 [] [])
+commuteFP (Hunk line1 old1 new1 :> Hunk line2 old2 new2) =
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 81
-        Succeeded (FP f (Hunk line2' old2 new2) :> FP f (Hunk line1' old1 new1))
-      Nothing -> Failed
-commuteFP f (Hunk line1 old1 new1 :> TokReplace t o n) =
+        return (Hunk line2' old2 new2 :> Hunk line1' old1 new1)
+      Nothing -> failed
+commuteFP (Hunk line1 old1 new1 :> TokReplace t o n) =
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 86
-    Nothing -> Failed
+    Nothing -> failed
hunk ./src/Darcs/Patch/Prim/V1/Commute.hs 89
-        Nothing -> Failed
-        Just new1' -> Succeeded (FP f (TokReplace t o n) :>
-                                 FP f (Hunk line1 old1' new1'))
-commuteFP f (TokReplace t1 o1 n1 :> TokReplace t2 o2 n2)
-    | t1 /= t2 = Failed
-    | o1 == o2 = Failed
-    | n1 == o2 = Failed
-    | o1 == n2 = Failed
-    | n1 == n2 = Failed
-    | otherwise = Succeeded (FP f (TokReplace t2 o2 n2) :>
-                             FP f (TokReplace t1 o1 n1))
-commuteFP _ _ = Unknown
+        Nothing -> failed
+        Just new1' -> return (TokReplace t o n :> Hunk line1 old1' new1')
+commuteFP pair@(TokReplace {} :> Hunk {}) = invertCommute commuteFP pair
+commuteFP (TokReplace t1 o1 n1 :> TokReplace t2 o2 n2)
+    | t1 /= t2 = failed
+    | o1 == o2 = failed
+    | n1 == o2 = failed
+    | o1 == n2 = failed
+    | n1 == n2 = failed
+    | otherwise = return (TokReplace t2 o2 n2 :> TokReplace t1 o1 n1)
+commuteFP (AddFile :> _) = failed
+commuteFP (RmFile :> _) = failed
+commuteFP (Binary {} :> _) = failed
+commuteFP (_ :> AddFile) = failed
+commuteFP (_ :> RmFile) = failed
+commuteFP (_ :> Binary {}) = failed

Attachment: prim_v1_-inline-isindirectory-and-issuperdir.dpatch
Description: application/darcs-patch

_______________________________________________
darcs-devel mailing list
[email protected]
https://lists.darcs.net/mailman/listinfo/darcs-devel

Reply via email to