2019-05-08 17:51:23 +03:00
{- # LANGUAGE DeriveAnyClass, DefaultSignatures, DerivingStrategies, GeneralizedNewtypeDeriving, KindSignatures, TypeOperators, UndecidableInstances # -}
2018-07-19 21:45:04 +03:00
module Diffing.Algorithm
2018-10-29 18:01:41 +03:00
( Diff ( .. )
, Algorithm ( .. )
2018-07-19 21:45:04 +03:00
, Diffable ( .. )
, Equivalence ( .. )
, diff
, diffThese
, diffMaybe
, linearly
, byReplacing
, comparableTerms
, equivalentTerms
, algorithmForTerms
) where
2015-11-18 03:24:01 +03:00
2019-07-15 21:54:40 +03:00
import Control.Effect.Carrier hiding ( ( :+: ) )
import Control.Effect.NonDet
2018-10-29 18:01:41 +03:00
import qualified Data.Diff as Diff
2018-05-02 19:00:15 +03:00
import Data.Sum
2017-09-27 19:37:37 +03:00
import Data.Term
2018-10-13 00:54:57 +03:00
import GHC.Generics
2018-04-09 23:11:55 +03:00
import Prologue
2016-08-04 01:12:31 +03:00
2017-02-21 18:26:27 +03:00
-- | A single step in a diffing algorithm, parameterized by the types of terms, diffs, and the result of the applicable algorithm.
2018-10-29 18:01:41 +03:00
data Diff term1 term2 diff ( m :: * -> * ) k
2017-02-23 20:24:31 +03:00
-- | Diff two terms with the choice of algorithm left to the interpreter’ s discretion.
2019-07-06 18:27:12 +03:00
= Diff term1 term2 ( diff -> m k )
2017-02-21 18:38:10 +03:00
-- | Diff two terms recursively in O(n) time, resulting in a single diff node.
2019-07-06 18:27:12 +03:00
| Linear term1 term2 ( diff -> m k )
2017-02-21 18:38:10 +03:00
-- | Diff two lists of terms by each element’ s similarity in O(n³ log n), resulting in a list of diffs.
2019-07-06 18:27:12 +03:00
| RWS [ term1 ] [ term2 ] ( [ diff ] -> m k )
2017-09-19 17:02:26 +03:00
-- | Delete a term.
2019-07-06 18:27:12 +03:00
| Delete term1 ( diff -> m k )
2017-02-21 19:40:13 +03:00
-- | Insert a term.
2019-07-06 18:27:12 +03:00
| Insert term2 ( diff -> m k )
2017-02-21 19:40:13 +03:00
-- | Replace one term with another.
2019-07-06 18:27:12 +03:00
| Replace term1 term2 ( diff -> m k )
2019-07-06 17:52:38 +03:00
deriving stock ( Functor , Generic1 )
2019-05-08 17:51:23 +03:00
deriving anyclass ( HFunctor , Effect )
2018-10-29 18:01:41 +03:00
2019-07-06 18:27:12 +03:00
2019-03-04 20:00:19 +03:00
newtype Algorithm term1 term2 diff m a = Algorithm { runAlgorithm :: m a }
2019-05-08 17:51:23 +03:00
deriving newtype ( Applicative , Alternative , Functor , Monad )
2018-10-29 18:01:41 +03:00
instance Carrier sig m => Carrier sig ( Algorithm term1 term2 diff m ) where
2019-03-04 20:54:21 +03:00
eff = Algorithm . eff . handleCoercible
2017-02-21 18:44:48 +03:00
2016-08-04 19:08:34 +03:00
-- DSL
2017-02-23 20:24:31 +03:00
-- | Diff two terms without specifying the algorithm to be used.
2018-10-29 18:01:41 +03:00
diff :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => term1 -> term2 -> m diff
2019-03-04 20:54:21 +03:00
diff a1 a2 = send ( Diff a1 a2 pure )
2017-02-23 20:24:31 +03:00
2017-02-23 20:30:26 +03:00
-- | Diff a These of terms without specifying the algorithm to be used.
2018-10-29 18:01:41 +03:00
diffThese :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => These term1 term2 -> Algorithm term1 term2 diff m diff
2017-02-23 20:25:45 +03:00
diffThese = these byDeleting byInserting diff
2017-05-03 23:56:15 +03:00
-- | Diff a pair of optional terms without specifying the algorithm to be used.
2018-10-29 18:01:41 +03:00
diffMaybe :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => Maybe term1 -> Maybe term2 -> Algorithm term1 term2 diff m ( Maybe diff )
2017-10-03 19:20:52 +03:00
diffMaybe ( Just a1 ) ( Just a2 ) = Just <$> diff a1 a2
diffMaybe ( Just a1 ) _ = Just <$> byDeleting a1
diffMaybe _ ( Just a2 ) = Just <$> byInserting a2
diffMaybe _ _ = pure Nothing
2017-05-03 23:56:15 +03:00
2017-02-21 19:39:41 +03:00
-- | Diff two terms linearly.
2018-10-29 18:01:41 +03:00
linearly :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => term1 -> term2 -> Algorithm term1 term2 diff m diff
2019-03-04 20:54:21 +03:00
linearly f1 f2 = send ( Linear f1 f2 pure )
2016-08-04 01:38:35 +03:00
2017-02-21 19:39:41 +03:00
-- | Diff two terms using RWS.
2018-10-29 18:01:41 +03:00
byRWS :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => [ term1 ] -> [ term2 ] -> Algorithm term1 term2 diff m [ diff ]
2019-03-04 20:54:21 +03:00
byRWS as1 as2 = send ( RWS as1 as2 pure )
2017-02-21 19:40:13 +03:00
-- | Delete a term.
2018-10-29 18:01:41 +03:00
byDeleting :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => term1 -> Algorithm term1 term2 diff m diff
2019-03-04 20:54:21 +03:00
byDeleting a1 = sendDiff ( Delete a1 pure )
2017-02-21 19:40:13 +03:00
-- | Insert a term.
2018-10-29 18:01:41 +03:00
byInserting :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => term2 -> Algorithm term1 term2 diff m diff
2019-03-04 20:54:21 +03:00
byInserting a2 = sendDiff ( Insert a2 pure )
2017-02-21 19:40:13 +03:00
-- | Replace one term with another.
2018-10-29 18:01:41 +03:00
byReplacing :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => term1 -> term2 -> Algorithm term1 term2 diff m diff
2019-03-04 20:54:21 +03:00
byReplacing a1 a2 = send ( Replace a1 a2 pure )
2017-09-19 14:20:54 +03:00
2019-07-06 18:27:12 +03:00
sendDiff :: ( Carrier sig m , Member ( Diff term1 term2 diff ) sig ) => Diff term1 term2 diff m a -> Algorithm term1 term2 diff m a
2018-10-29 18:01:41 +03:00
sendDiff = Algorithm . send
2017-09-19 14:20:54 +03:00
2017-10-03 19:22:23 +03:00
-- | Diff two terms based on their 'Diffable' instances, performing substructural comparisons iff the initial comparison fails.
2019-03-04 20:54:21 +03:00
algorithmForTerms :: ( Carrier sig m , Diffable syntax , Member ( Diff ( Term syntax ann1 ) ( Term syntax ann2 ) ( Diff . Diff syntax ann1 ann2 ) ) sig , Member NonDet sig , Alternative m )
2017-09-14 16:41:52 +03:00
=> Term syntax ann1
-> Term syntax ann2
2018-10-29 18:01:41 +03:00
-> Algorithm ( Term syntax ann1 ) ( Term syntax ann2 ) ( Diff . Diff syntax ann1 ann2 ) m ( Diff . Diff syntax ann1 ann2 )
2017-09-19 21:21:37 +03:00
algorithmForTerms t1 @ ( Term ( In ann1 f1 ) ) t2 @ ( Term ( In ann2 f2 ) )
2017-09-19 23:13:23 +03:00
= mergeFor t1 t2
2018-10-29 18:01:41 +03:00
<|> Diff . deleteF . In ann1 <$> subalgorithmFor byDeleting ( ` mergeFor ` t2 ) f1
<|> Diff . insertF . In ann2 <$> subalgorithmFor byInserting ( mergeFor t1 ) f2
where mergeFor ( Term ( In ann1 f1 ) ) ( Term ( In ann2 f2 ) ) = Diff . merge ( ann1 , ann2 ) <$> algorithmFor f1 f2
2017-07-21 03:21:20 +03:00
2017-10-03 22:55:12 +03:00
-- | An O(1) relation on terms indicating their non-recursive comparability (i.e. are they of the same “kind” in a way that warrants comparison), defined in terms of the comparability of their respective syntax.
2017-10-03 21:58:14 +03:00
comparableTerms :: Diffable syntax
=> TermF syntax ann1 term1
-> TermF syntax ann2 term2
-> Bool
comparableTerms ( In _ syntax1 ) ( In _ syntax2 ) = comparableTo syntax1 syntax2
2017-10-03 22:58:52 +03:00
-- | An O(n) relation on terms indicating their recursive equivalence (i.e. are they _notionally_ “the same,” as distinct from literal equality), defined at each node in terms of the equivalence of their respective syntax, computed first on a nominated subterm (if any), falling back to substructural equivalence (e.g. equivalence of one term against the subject of the other, annotating term), and finally to equality.
2017-10-03 21:31:06 +03:00
equivalentTerms :: ( Diffable syntax , Eq1 syntax )
2017-10-03 20:27:02 +03:00
=> Term syntax ann1
-> Term syntax ann2
-> Bool
equivalentTerms term1 @ ( Term ( In _ syntax1 ) ) term2 @ ( Term ( In _ syntax2 ) )
2017-10-03 21:19:26 +03:00
= fromMaybe False ( equivalentTerms <$> equivalentBySubterm syntax1 <*> equivalentBySubterm syntax2 )
2017-10-03 22:37:42 +03:00
|| runEquivalence ( subalgorithmFor pure ( Equivalence . flip equivalentTerms term2 ) syntax1 )
|| runEquivalence ( subalgorithmFor pure ( Equivalence . equivalentTerms term1 ) syntax2 )
2017-10-03 21:31:06 +03:00
|| liftEq equivalentTerms syntax1 syntax2
2017-10-03 20:27:02 +03:00
2017-10-03 23:00:35 +03:00
-- | A constant 'Alternative' functor used by 'equivalentTerms' to compute the substructural equivalence of syntax.
2017-10-03 22:37:17 +03:00
newtype Equivalence a = Equivalence { runEquivalence :: Bool }
deriving ( Eq , Functor )
instance Applicative Equivalence where
pure _ = Equivalence True
Equivalence a <*> Equivalence b = Equivalence ( a && b )
instance Alternative Equivalence where
empty = Equivalence False
Equivalence a <|> Equivalence b = Equivalence ( a || b )
2017-07-21 03:21:20 +03:00
-- | A type class for determining what algorithm to use for diffing two terms.
2017-07-21 00:15:01 +03:00
class Diffable f where
2017-09-26 20:03:44 +03:00
-- | Construct an algorithm to diff a pair of @f@s populated with disjoint terms.
2019-03-04 20:54:21 +03:00
algorithmFor :: ( Alternative m , Carrier sig m , Member ( Diff term1 term2 diff ) sig , Member NonDet sig )
2018-10-29 18:01:41 +03:00
=> f term1
2017-09-23 05:24:50 +03:00
-> f term2
2018-10-29 18:01:41 +03:00
-> Algorithm term1 term2 diff m ( f diff )
2017-09-15 01:04:54 +03:00
default
2019-03-04 20:54:21 +03:00
algorithmFor :: ( Alternative m , Carrier sig m , Generic1 f , GDiffable ( Rep1 f ) , Member ( Diff term1 term2 diff ) sig , Member NonDet sig )
2017-09-23 05:24:50 +03:00
=> f term1
-> f term2
2018-10-29 18:01:41 +03:00
-> Algorithm term1 term2 diff m ( f diff )
2017-09-13 18:48:51 +03:00
algorithmFor = genericAlgorithmFor
2018-04-09 23:11:55 +03:00
tryAlignWith :: Alternative g => ( These a1 a2 -> g b ) -> f a1 -> f a2 -> g ( f b )
default tryAlignWith :: ( Alternative g , Generic1 f , GDiffable ( Rep1 f ) ) => ( These a1 a2 -> g b ) -> f a1 -> f a2 -> g ( f b )
tryAlignWith f a b = to1 <$> gtryAlignWith f ( from1 a ) ( from1 b )
2017-09-26 20:03:49 +03:00
-- | Construct an algorithm to diff against positions inside an @f@.
--
-- This is very like 'traverse', with two key differences:
--
-- 1. The traversal distributes through an 'Alternative' functor, not just an 'Applicative'.
-- 2. The traversal is mediated by two different functions, one for positions which should be ignored for substructural diffing, the other for positions which should be diffed substructurally.
--
-- These two functions allow us to say e.g. that comparisons against 'Data.Syntax.Context' should also be made against its subject, but not against any of the comments, resulting in the insertion of both comments and context when documenting an existing function.
--
-- By default, 'subalgorithmFor' produces 'empty', rejecting substructural comparisons. This is important for performance, as alternations with 'empty' are eliminated at construction time.
2018-10-29 18:01:41 +03:00
-- ^ The 'Alternative' instance will in general be 'Diff', but left opaque to make it harder to shoot oneself in the foot.
2018-02-28 20:07:56 +03:00
subalgorithmFor :: Alternative g
2017-09-26 20:03:49 +03:00
=> ( a -> g b ) -- ^ A “blur” function to traverse positions which should not be diffed against.
-> ( a -> g b ) -- ^ A “focus” function to traverse positions which should be diffed against.
-> f a -- ^ The syntax to diff inside of.
-> g ( f b ) -- ^ The resulting algorithm (or other 'Alternative' context), producing the traversed syntax.
2017-09-19 21:02:08 +03:00
subalgorithmFor _ _ _ = empty
2017-10-03 22:49:06 +03:00
-- | Syntax having a human-provided identifier, such as function/method definitions, can use equivalence of identifiers as a proxy for their overall equivalence, improving the quality & efficiency of the diff as a whole.
--
-- This can also be used for annotation nodes to ensure that their subjects’ equivalence is weighed appropriately.
--
-- Other syntax should use the default definition, and thus have equivalence computed piece-wise.
2017-10-03 21:18:55 +03:00
equivalentBySubterm :: f a -> Maybe a
equivalentBySubterm _ = Nothing
2017-10-03 22:51:56 +03:00
-- | A relation on syntax values indicating their In general this should be true iff both values have the same constructor (this is the relation computed by the default, generic definition).
--
-- For syntax with constant fields which serve as a classifier, this method can be overloaded to consider equality on that classifier in addition to/instead of the constructors themselves, and thus limit the comparisons accordingly.
2017-10-03 21:57:06 +03:00
comparableTo :: f term1 -> f term2 -> Bool
default comparableTo :: ( Generic1 f , GDiffable ( Rep1 f ) ) => f term1 -> f term2 -> Bool
comparableTo = genericComparableTo
2019-03-04 20:54:21 +03:00
genericAlgorithmFor :: ( Alternative m , Carrier sig m , Generic1 f , GDiffable ( Rep1 f ) , Member ( Diff term1 term2 diff ) sig , Member NonDet sig )
2017-09-25 19:24:42 +03:00
=> f term1
-> f term2
2018-10-29 18:01:41 +03:00
-> Algorithm term1 term2 diff m ( f diff )
2017-10-03 21:44:14 +03:00
genericAlgorithmFor a1 a2 = to1 <$> galgorithmFor ( from1 a1 ) ( from1 a2 )
2017-09-13 18:48:51 +03:00
2017-10-03 21:57:06 +03:00
genericComparableTo :: ( Generic1 f , GDiffable ( Rep1 f ) ) => f term1 -> f term2 -> Bool
genericComparableTo a1 a2 = gcomparableTo ( from1 a1 ) ( from1 a2 )
2017-07-21 00:15:01 +03:00
2018-05-02 19:00:15 +03:00
-- | 'Diffable' for 'Sum's of syntax functors is defined in general by straightforward lifting of each method into the functors in the 'Sum'.
instance Apply Diffable fs => Diffable ( Sum fs ) where
2018-05-02 19:13:01 +03:00
algorithmFor u1 u2 = fromMaybe empty ( apply2' @ Diffable ( \ inj f1 f2 -> inj <$> algorithmFor f1 f2 ) u1 u2 )
2017-07-21 00:15:01 +03:00
2018-05-29 15:53:37 +03:00
tryAlignWith f u1 u2 = fromMaybe empty ( apply2' @ Diffable ( \ inj t1 t2 -> inj <$> tryAlignWith f t1 t2 ) u1 u2 )
2018-04-09 23:11:55 +03:00
2018-05-02 19:13:01 +03:00
subalgorithmFor blur focus = apply' @ Diffable ( \ inj f -> inj <$> subalgorithmFor blur focus f )
2017-09-19 21:02:08 +03:00
2018-05-02 19:13:01 +03:00
equivalentBySubterm = apply @ Diffable equivalentBySubterm
2017-10-03 21:38:10 +03:00
2018-05-02 19:00:15 +03:00
-- | Comparability on 'Sum's is defined first by comparability of their contained functors (when they’ re the same), falling back to using 'subalgorithmFor' to opt substructurally-diffable syntax into comparisons (e.g. to allow annotating nodes to be compared against the kind of nodes they annotate).
2018-05-02 19:13:01 +03:00
comparableTo u1 u2 = fromMaybe False ( apply2 @ Diffable comparableTo u1 u2 <|> True <$ subalgorithmFor pure pure u1 <|> True <$ subalgorithmFor pure pure u2 )
2017-10-03 21:57:06 +03:00
2017-09-19 19:49:34 +03:00
-- | Diff two 'Maybe's.
instance Diffable Maybe where
2017-09-19 19:51:25 +03:00
algorithmFor = diffMaybe
2017-09-19 19:49:34 +03:00
2018-04-09 23:11:55 +03:00
tryAlignWith f ( Just a1 ) ( Just a2 ) = Just <$> f ( These a1 a2 )
tryAlignWith f ( Just a1 ) Nothing = Just <$> f ( This a1 )
tryAlignWith f Nothing ( Just a2 ) = Just <$> f ( That a2 )
tryAlignWith _ Nothing Nothing = pure Nothing
2017-09-19 19:46:15 +03:00
-- | Diff two lists using RWS.
2017-07-21 20:50:24 +03:00
instance Diffable [] where
2017-09-25 16:10:44 +03:00
algorithmFor = byRWS
2017-07-21 20:50:24 +03:00
2018-04-09 23:11:55 +03:00
tryAlignWith f ( a1 : as1 ) ( a2 : as2 ) = ( : ) <$> f ( These a1 a2 ) <*> tryAlignWith f as1 as2
tryAlignWith f [] as2 = traverse ( f . That ) as2
tryAlignWith f as1 [] = traverse ( f . This ) as1
2017-09-19 19:46:24 +03:00
-- | Diff two non-empty lists using RWS.
instance Diffable NonEmpty where
2018-05-31 01:56:50 +03:00
algorithmFor ( a1 :| as1 ) ( a2 :| as2 ) = nonEmpty <$> byRWS ( a1 : as1 ) ( a2 : as2 ) >>= maybeM empty
2017-09-19 19:46:24 +03:00
2018-04-09 23:11:55 +03:00
tryAlignWith f ( a1 :| as1 ) ( a2 :| as2 ) = ( :| ) <$> f ( These a1 a2 ) <*> tryAlignWith f as1 as2
2017-07-21 03:21:20 +03:00
-- | A generic type class for diffing two terms defined by the Generic1 interface.
2017-09-14 22:09:30 +03:00
class GDiffable f where
2019-03-04 20:54:21 +03:00
galgorithmFor :: ( Alternative m , Carrier sig m , Member ( Diff term1 term2 diff ) sig , Member NonDet sig ) => f term1 -> f term2 -> Algorithm term1 term2 diff m ( f diff )
2017-07-21 00:15:01 +03:00
2018-04-09 23:11:55 +03:00
gtryAlignWith :: Alternative g => ( These a1 a2 -> g b ) -> f a1 -> f a2 -> g ( f b )
2017-10-03 21:57:06 +03:00
gcomparableTo :: f term1 -> f term2 -> Bool
gcomparableTo _ _ = True
2017-07-21 20:50:53 +03:00
-- | Diff two constructors (M1 is the Generic1 newtype for meta-information (possibly related to type constructors, record selectors, and data types))
2017-09-14 22:09:30 +03:00
instance GDiffable f => GDiffable ( M1 i c f ) where
2017-10-03 19:20:52 +03:00
galgorithmFor ( M1 a1 ) ( M1 a2 ) = M1 <$> galgorithmFor a1 a2
2017-07-21 00:15:01 +03:00
2018-04-09 23:11:55 +03:00
gtryAlignWith f ( M1 a ) ( M1 b ) = M1 <$> gtryAlignWith f a b
2017-10-03 21:57:06 +03:00
gcomparableTo ( M1 a1 ) ( M1 a2 ) = gcomparableTo a1 a2
2017-07-21 20:50:53 +03:00
-- | Diff the fields of a product type.
-- i.e. data Foo a b = Foo a b (the 'Foo a b' is captured by 'a :*: b').
2017-09-14 22:09:30 +03:00
instance ( GDiffable f , GDiffable g ) => GDiffable ( f :*: g ) where
2017-09-19 17:48:26 +03:00
galgorithmFor ( a1 :*: b1 ) ( a2 :*: b2 ) = ( :*: ) <$> galgorithmFor a1 a2 <*> galgorithmFor b1 b2
2017-07-21 00:15:01 +03:00
2018-04-09 23:11:55 +03:00
gtryAlignWith f ( a1 :*: b1 ) ( a2 :*: b2 ) = ( :*: ) <$> gtryAlignWith f a1 a2 <*> gtryAlignWith f b1 b2
2017-07-21 20:50:53 +03:00
-- | Diff the constructors of a sum type.
-- i.e. data Foo a = Foo a | Bar a (the 'Foo a' is captured by L1 and 'Bar a' is R1).
2017-09-14 22:09:30 +03:00
instance ( GDiffable f , GDiffable g ) => GDiffable ( f :+: g ) where
2017-10-03 19:20:52 +03:00
galgorithmFor ( L1 a1 ) ( L1 a2 ) = L1 <$> galgorithmFor a1 a2
galgorithmFor ( R1 b1 ) ( R1 b2 ) = R1 <$> galgorithmFor b1 b2
2017-09-19 17:48:26 +03:00
galgorithmFor _ _ = empty
2017-07-21 00:15:01 +03:00
2018-04-09 23:11:55 +03:00
gtryAlignWith f a b = case ( a , b ) of
( L1 a , L1 b ) -> L1 <$> gtryAlignWith f a b
( R1 a , R1 b ) -> R1 <$> gtryAlignWith f a b
_ -> empty
2017-10-03 21:57:06 +03:00
gcomparableTo ( L1 _ ) ( L1 _ ) = True
gcomparableTo ( R1 _ ) ( R1 _ ) = True
gcomparableTo _ _ = False
2017-07-21 03:21:20 +03:00
-- | Diff two parameters (Par1 is the Generic1 newtype representing a type parameter).
2017-07-21 20:50:53 +03:00
-- i.e. data Foo a = Foo a (the 'a' is captured by Par1).
2017-09-14 22:09:30 +03:00
instance GDiffable Par1 where
2017-10-03 19:20:52 +03:00
galgorithmFor ( Par1 a1 ) ( Par1 a2 ) = Par1 <$> diff a1 a2
2017-07-21 00:15:01 +03:00
2018-04-09 23:11:55 +03:00
gtryAlignWith f ( Par1 a ) ( Par1 b ) = Par1 <$> f ( These a b )
2017-07-21 03:21:20 +03:00
-- | Diff two constant parameters (K1 is the Generic1 newtype representing type parameter constants).
2017-07-21 20:50:53 +03:00
-- i.e. data Foo = Foo Int (the 'Int' is a constant parameter).
2017-09-14 22:09:30 +03:00
instance Eq c => GDiffable ( K1 i c ) where
2018-03-20 17:41:53 +03:00
galgorithmFor ( K1 a1 ) ( K1 a2 ) = guard ( a1 == a2 ) $> K1 a1
2017-07-21 00:15:01 +03:00
2018-04-09 23:11:55 +03:00
gtryAlignWith _ ( K1 a ) ( K1 b ) = guard ( a == b ) $> K1 b
2017-07-21 03:21:20 +03:00
-- | Diff two terms whose constructors contain 0 type parameters.
-- i.e. data Foo = Foo.
2017-09-14 22:09:30 +03:00
instance GDiffable U1 where
2017-09-19 17:48:26 +03:00
galgorithmFor _ _ = pure U1
2017-07-21 00:15:01 +03:00
2018-04-09 23:11:55 +03:00
gtryAlignWith _ _ _ = pure U1
2017-09-19 19:46:40 +03:00
-- | Diff two 'Diffable' containers of parameters.
instance Diffable f => GDiffable ( Rec1 f ) where
2017-10-03 19:20:52 +03:00
galgorithmFor a1 a2 = Rec1 <$> algorithmFor ( unRec1 a1 ) ( unRec1 a2 )
2018-03-20 17:41:53 +03:00
2018-04-09 23:11:55 +03:00
gtryAlignWith f ( Rec1 a ) ( Rec1 b ) = Rec1 <$> tryAlignWith f a b