module Unison.Merge.TwoOrThreeWay
  ( TwoOrThreeWay (..),
    forgetLca,
    toThreeWay,
    toThreeWayA,
  )
where

import Unison.Merge.Internal.Types (ThreeWay (..), TwoOrThreeWay (..), TwoWay (..))
import Unison.Prelude

forgetLca :: TwoOrThreeWay a -> TwoWay a
forgetLca :: forall a. TwoOrThreeWay a -> TwoWay a
forgetLca TwoOrThreeWay {a
alice :: a
$sel:alice:TwoOrThreeWay :: forall a. TwoOrThreeWay a -> a
alice, a
bob :: a
$sel:bob:TwoOrThreeWay :: forall a. TwoOrThreeWay a -> a
bob} =
  TwoWay {a
alice :: a
$sel:alice:TwoWay :: a
alice, a
bob :: a
$sel:bob:TwoWay :: a
bob}

toThreeWay :: a -> TwoOrThreeWay a -> ThreeWay a
toThreeWay :: forall a. a -> TwoOrThreeWay a -> ThreeWay a
toThreeWay a
x TwoOrThreeWay {a
$sel:alice:TwoOrThreeWay :: forall a. TwoOrThreeWay a -> a
alice :: a
alice, a
$sel:bob:TwoOrThreeWay :: forall a. TwoOrThreeWay a -> a
bob :: a
bob, Maybe a
lca :: Maybe a
$sel:lca:TwoOrThreeWay :: forall a. TwoOrThreeWay a -> Maybe a
lca} =
  ThreeWay {a
alice :: a
$sel:alice:ThreeWay :: a
alice, a
bob :: a
$sel:bob:ThreeWay :: a
bob, $sel:lca:ThreeWay :: a
lca = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
x Maybe a
lca}

toThreeWayA :: (Applicative f) => f a -> TwoOrThreeWay a -> f (ThreeWay a)
toThreeWayA :: forall (f :: * -> *) a.
Applicative f =>
f a -> TwoOrThreeWay a -> f (ThreeWay a)
toThreeWayA f a
x TwoOrThreeWay a
y =
  case TwoOrThreeWay a
y.lca of
    Just a
lca -> ThreeWay a -> f (ThreeWay a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> ThreeWay a
g a
lca)
    Maybe a
Nothing -> a -> ThreeWay a
g (a -> ThreeWay a) -> f a -> f (ThreeWay a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
x
  where
    g :: a -> ThreeWay a
g a
lca =
      ThreeWay {$sel:alice:ThreeWay :: a
alice = TwoOrThreeWay a
y.alice, $sel:bob:ThreeWay :: a
bob = TwoOrThreeWay a
y.bob, a
$sel:lca:ThreeWay :: a
lca :: a
lca}