Skip to content

Commit 2d49f13

Browse files
authored
Merge pull request #5567 from unisonweb/share-3waydiff
2 parents e9d2a2a + c486c24 commit 2d49f13

File tree

21 files changed

+497
-165
lines changed

21 files changed

+497
-165
lines changed

Diff for: lib/unison-prelude/src/Unison/Prelude.hs

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Unison.Prelude
2525
whenJustM,
2626
eitherToMaybe,
2727
maybeToEither,
28+
eitherToThese,
2829
altSum,
2930
altMap,
3031
hoistMaybe,
@@ -82,6 +83,7 @@ import Data.Text as X (Text)
8283
import Data.Text qualified as Text
8384
import Data.Text.Encoding as X (decodeUtf8, encodeUtf8)
8485
import Data.Text.IO qualified as Text
86+
import Data.These (These (..))
8587
import Data.Traversable as X (for)
8688
import Data.Typeable as X (Typeable)
8789
import Data.Void as X (Void)
@@ -205,6 +207,9 @@ throwEitherM = throwEitherMWith id
205207
throwEitherMWith :: forall e e' m a. (MonadIO m, Exception e') => (e -> e') -> m (Either e a) -> m a
206208
throwEitherMWith f action = throwExceptT . withExceptT f $ (ExceptT action)
207209

210+
eitherToThese :: Either a b -> These a b
211+
eitherToThese = either This That
212+
208213
tShow :: (Show a) => a -> Text
209214
tShow = Text.pack . show
210215

Diff for: lib/unison-prelude/src/Unison/Util/Set.hs

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Unison.Util.Set
55
mapMaybe,
66
symmetricDifference,
77
Unison.Util.Set.traverse,
8+
Unison.Util.Set.for,
89
flatMap,
910
filterM,
1011
forMaybe,
@@ -51,6 +52,9 @@ forMaybe xs f =
5152
traverse :: (Applicative f, Ord b) => (a -> f b) -> Set a -> f (Set b)
5253
traverse f = fmap Set.fromList . Prelude.traverse f . Set.toList
5354

55+
for :: (Ord b, Applicative f) => Set a -> (a -> f b) -> f (Set b)
56+
for = flip Unison.Util.Set.traverse
57+
5458
flatMap :: (Ord b) => (a -> Set b) -> Set a -> Set b
5559
flatMap f = Set.unions . fmap f . Set.toList
5660

Diff for: unison-cli/src/Unison/Codebase/Editor/HandleInput/Merge2.hs

+20-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import Unison.Merge.ThreeWay qualified as ThreeWay
7878
import Unison.Name (Name)
7979
import Unison.NameSegment (NameSegment)
8080
import Unison.NameSegment qualified as NameSegment
81+
import Unison.Names (Names)
8182
import Unison.Parser.Ann (Ann)
8283
import Unison.Prelude
8384
import Unison.Project
@@ -241,6 +242,22 @@ doMerge info = do
241242

242243
let blob0 = Merge.makeMergeblob0 nametrees3 libdeps3
243244

245+
names3 :: Merge.ThreeWay Names <- do
246+
let causalHashes =
247+
Merge.TwoOrThreeWay
248+
{ alice = info.alice.causalHash,
249+
bob = info.bob.causalHash,
250+
lca = info.lca.causalHash
251+
}
252+
branches <- for causalHashes \ch -> do
253+
liftIO (Codebase.getBranchForHash env.codebase ch) >>= \case
254+
Nothing -> done (Output.CouldntLoadBranch ch)
255+
Just b -> pure b
256+
let names = fmap (Branch.toNames . Branch.head) branches
257+
pure Merge.ThreeWay {alice = names.alice, bob = names.bob, lca = fromMaybe mempty names.lca}
258+
259+
respondRegion (Output.Literal "Loading definitions...")
260+
244261
-- Hydrate
245262
hydratedDefns ::
246263
Merge.ThreeWay
@@ -260,14 +277,14 @@ doMerge info = do
260277
in bimap f g <$> blob0.defns
261278
)
262279

263-
respondRegion (Output.Literal "Computing diff between branches...")
280+
respondRegion (Output.Literal "Computing diffs...")
264281

265282
blob1 <-
266-
Merge.makeMergeblob1 blob0 hydratedDefns & onLeft \case
283+
Merge.makeMergeblob1 blob0 names3 hydratedDefns & onLeft \case
267284
Merge.Alice reason -> done (Output.IncoherentDeclDuringMerge mergeTarget reason)
268285
Merge.Bob reason -> done (Output.IncoherentDeclDuringMerge mergeSource reason)
269286

270-
liftIO (debugFunctions.debugDiffs blob1.diffs)
287+
liftIO (debugFunctions.debugDiffs blob1.diffsFromLCA)
271288

272289
liftIO (debugFunctions.debugCombinedDiff blob1.diff)
273290

Diff for: unison-core/src/Unison/Util/Defn.hs

+21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,28 @@ module Unison.Util.Defn
33
)
44
where
55

6+
import Data.Bifoldable (Bifoldable (..))
7+
import Data.Bifunctor (Bifunctor (..))
8+
import Data.Bitraversable (Bitraversable (..))
9+
import GHC.Generics (Generic)
10+
611
-- | A "definition" is either a term or a type.
712
data Defn term typ
813
= TermDefn term
914
| TypeDefn typ
15+
deriving stock (Generic, Functor, Foldable, Traversable, Show, Eq, Ord)
16+
17+
instance Bifunctor Defn where
18+
bimap f g = \case
19+
TermDefn x -> TermDefn (f x)
20+
TypeDefn y -> TypeDefn (g y)
21+
22+
instance Bifoldable Defn where
23+
bifoldMap f g = \case
24+
TermDefn x -> f x
25+
TypeDefn y -> g y
26+
27+
instance Bitraversable Defn where
28+
bitraverse f g = \case
29+
TermDefn x -> TermDefn <$> f x
30+
TypeDefn y -> TypeDefn <$> g y

Diff for: unison-core/src/Unison/Util/Defns.hs

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Unison.Util.Defns
44
DefnsF2,
55
DefnsF3,
66
DefnsF4,
7+
terms_,
8+
types_,
79
alignDefnsWith,
810
defnsAreEmpty,
911
fromTerms,
@@ -19,6 +21,7 @@ module Unison.Util.Defns
1921
)
2022
where
2123

24+
import Control.Lens (Lens)
2225
import Data.Align (Semialign, alignWith)
2326
import Data.Bifoldable (Bifoldable, bifoldMap)
2427
import Data.Bitraversable (Bitraversable, bitraverse)
@@ -31,7 +34,7 @@ data Defns terms types = Defns
3134
{ terms :: terms,
3235
types :: types
3336
}
34-
deriving stock (Generic, Functor, Show)
37+
deriving stock (Generic, Functor, Show, Eq, Ord)
3538
deriving (Monoid, Semigroup) via GenericSemigroupMonoid (Defns terms types)
3639

3740
instance Bifoldable Defns where
@@ -46,6 +49,12 @@ instance Bitraversable Defns where
4649
bitraverse f g (Defns x y) =
4750
Defns <$> f x <*> g y
4851

52+
terms_ :: Lens (Defns terms types) (Defns terms' types) terms terms'
53+
terms_ f (Defns x y) = (\x' -> Defns x' y) <$> f x
54+
55+
types_ :: Lens (Defns terms types) (Defns terms types') types types'
56+
types_ f (Defns x y) = (\y' -> Defns x y') <$> f y
57+
4958
-- | A common shape of definitions - terms and types are stored in the same structure.
5059
type DefnsF f terms types =
5160
Defns (f terms) (f types)

Diff for: unison-core/src/Unison/Util/Nametree.hs

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Unison.Util.Nametree
33
Nametree (..),
44
traverseNametreeWithName,
55
unfoldNametree,
6+
unionWith,
67

78
-- ** Flattening and unflattening
89
flattenNametree,
@@ -33,6 +34,16 @@ data Nametree a = Nametree
3334
}
3435
deriving stock (Functor, Foldable, Traversable, Generic, Show)
3536

37+
unionWith :: (a -> a -> a) -> Nametree a -> Nametree a -> Nametree a
38+
unionWith f (Nametree x xs) (Nametree y ys) =
39+
Nametree (f x y) (Map.unionWith (unionWith f) xs ys)
40+
41+
instance (Semigroup a) => Semigroup (Nametree a) where
42+
(<>) = unionWith (<>)
43+
44+
instance (Monoid a) => Monoid (Nametree a) where
45+
mempty = Nametree mempty mempty
46+
3647
instance Semialign Nametree where
3748
alignWith :: (These a b -> c) -> Nametree a -> Nametree b -> Nametree c
3849
alignWith f (Nametree x xs) (Nametree y ys) =

Diff for: unison-merge/package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ghc-options: -Wall
77
dependencies:
88
- base
99
- containers
10+
- either
1011
- lens
1112
- mtl
1213
- nonempty-containers

Diff for: unison-merge/src/Unison/Merge/DeclCoherencyCheck.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ data IncoherentDeclReason
132132
-- Foo.Bar#Foo
133133
IncoherentDeclReason'NestedDeclAlias !Name !Name -- shorter name, longer name
134134
| IncoherentDeclReason'StrayConstructor !TypeReferenceId !Name
135-
deriving stock (Show)
135+
deriving stock (Eq, Show)
136136

137137
checkDeclCoherency ::
138138
(HasCallStack) =>

0 commit comments

Comments
 (0)