module Unison.Merge.DiffOp
  ( DiffOp (..),
    DiffOp2 (..),
    Unison.Merge.DiffOp.map,
    Unison.Merge.DiffOp.traverse,
  )
where

import Unison.Merge.Updated (Updated)
import Unison.Merge.Updated qualified as Updated

-- | A diff operation is one of:
--
--   * An add (where nothing was)
--   * A delete (of the thing that was)
--   * An update (from old to new)
data DiffOp a
  = DiffOp'Add !a
  | DiffOp'Delete !a
  | DiffOp'Update !(Updated a)
  deriving stock (Int -> DiffOp a -> ShowS
[DiffOp a] -> ShowS
DiffOp a -> String
(Int -> DiffOp a -> ShowS)
-> (DiffOp a -> String) -> ([DiffOp a] -> ShowS) -> Show (DiffOp a)
forall a. Show a => Int -> DiffOp a -> ShowS
forall a. Show a => [DiffOp a] -> ShowS
forall a. Show a => DiffOp a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> DiffOp a -> ShowS
showsPrec :: Int -> DiffOp a -> ShowS
$cshow :: forall a. Show a => DiffOp a -> String
show :: DiffOp a -> String
$cshowList :: forall a. Show a => [DiffOp a] -> ShowS
showList :: [DiffOp a] -> ShowS
Show)

map :: (a -> b) -> DiffOp a -> DiffOp b
map :: forall a b. (a -> b) -> DiffOp a -> DiffOp b
map a -> b
f = \case
  DiffOp'Add a
x -> b -> DiffOp b
forall a. a -> DiffOp a
DiffOp'Add (a -> b
f a
x)
  DiffOp'Delete a
x -> b -> DiffOp b
forall a. a -> DiffOp a
DiffOp'Delete (a -> b
f a
x)
  DiffOp'Update Updated a
x -> Updated b -> DiffOp b
forall a. Updated a -> DiffOp a
DiffOp'Update ((a -> b) -> Updated a -> Updated b
forall a b. (a -> b) -> Updated a -> Updated b
Updated.map a -> b
f Updated a
x)

traverse :: (Applicative f) => (a -> f b) -> DiffOp a -> f (DiffOp b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> DiffOp a -> f (DiffOp b)
traverse a -> f b
f = \case
  DiffOp'Add a
x -> b -> DiffOp b
forall a. a -> DiffOp a
DiffOp'Add (b -> DiffOp b) -> f b -> f (DiffOp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
  DiffOp'Delete a
x -> b -> DiffOp b
forall a. a -> DiffOp a
DiffOp'Delete (b -> DiffOp b) -> f b -> f (DiffOp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
  DiffOp'Update Updated a
x -> Updated b -> DiffOp b
forall a. Updated a -> DiffOp a
DiffOp'Update (Updated b -> DiffOp b) -> f (Updated b) -> f (DiffOp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> Updated a -> f (Updated b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Updated a -> f (Updated b)
Updated.traverse a -> f b
f Updated a
x

-- | Like 'DiffOp', but updates are tagged as propagated (True) or not (False).
--
-- This could be cleaned up a bit, but eh, it works for now. Historical context: the concept of a propagated upddate was
-- not in the initial version of merge (which was only concerned with the merge algorithm and producing the correct
-- output). However, it is now being incorporated, because when e.g. viewing a diff, we do want to see propagated, not
-- drop them.
data DiffOp2 a
  = DiffOp2'Add !a
  | DiffOp2'Delete !a
  | DiffOp2'Update !(Updated a) !Bool {- is propagated? -}
  deriving stock (Int -> DiffOp2 a -> ShowS
[DiffOp2 a] -> ShowS
DiffOp2 a -> String
(Int -> DiffOp2 a -> ShowS)
-> (DiffOp2 a -> String)
-> ([DiffOp2 a] -> ShowS)
-> Show (DiffOp2 a)
forall a. Show a => Int -> DiffOp2 a -> ShowS
forall a. Show a => [DiffOp2 a] -> ShowS
forall a. Show a => DiffOp2 a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> DiffOp2 a -> ShowS
showsPrec :: Int -> DiffOp2 a -> ShowS
$cshow :: forall a. Show a => DiffOp2 a -> String
show :: DiffOp2 a -> String
$cshowList :: forall a. Show a => [DiffOp2 a] -> ShowS
showList :: [DiffOp2 a] -> ShowS
Show)