Skip to content

Commit 919ad4a

Browse files
committed
Replace specialDiffVia with gspecialDiffNested, which uses functions from Generics.SOP.GGP
1 parent 1cb6efb commit 919ad4a

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

src/Generics/Diff/Class.hs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Generics.Diff.Class
1111
, gdiffWith
1212
, eqDiff
1313
, diffWithSpecial
14-
, specialDiffVia
14+
, gspecialDiffNested
1515

1616
-- * Special case: lists
1717
, diffListWith
@@ -20,9 +20,11 @@ where
2020

2121
import Data.SOP
2222
import Data.SOP.NP
23+
import qualified GHC.Generics as G
2324
import Generics.Diff.Render
2425
import Generics.Diff.Type
2526
import Generics.SOP as SOP
27+
import Generics.SOP.GGP as SOP
2628

2729
{- | A type with an instance of 'Diff' permits a more nuanced comparison than 'Eq' or 'Ord'.
2830
If two values are not equal, 'diff' will tell you exactly where they differ ("in this contructor,
@@ -207,51 +209,44 @@ gdiffWithPure ::
207209
DiffResult a
208210
gdiffWithPure ds = gdiffWith $ cpure_POP (Proxy @c) ds
209211

210-
{- | Helper function to implement 'specialDiff' for a type with @SpecialDiffError a = DiffErrorNested xss@.
212+
{- | Helper function to implement 'specialDiff' for an instance of "GHC.Generic", with
213+
@SpecialDiffError a = DiffErrorNested xss@.
211214
212215
For example, say we want to implement 'SpecialDiff' (and then 'Diff') for @Tree@ from @containers@.
213216
We'd ideally like to use a 'SOP.Generic' instance, but we don't have one. Nevertheless we can fake one,
214-
by providing a function to convert from @Tree@ to what __would__ be its 'SOP.Rep', and a set of 'ConstructorInfo's
215-
which __would__ have been derived.
217+
using 'G.Generic' from "GHC.Generics".
216218
217219
@
218220
data Tree a = Node
219221
{ rootLabel :: a
220222
, subForest :: [Tree a]
221223
}
222-
223-
type TreeCode a = '[ '[a, [Tree a]]]
224-
225-
fromTree :: Tree a -> 'NS' ('NP' 'I') (TreeCode a)
226-
fromTree (Node lbl frst) = 'Z' $ 'I' lbl ':*' 'I' frst ':*' 'Nil'
227-
228-
treeCons :: 'NP' 'ConstructorInfo' (TreeCode a)
229-
treeCons = 'Record' "Node" ('FieldInfo' "rootLabel" ':*' 'FieldInfo' "subForest" ':*' 'Nil') ':*' 'Nil'
224+
deriving ('G.Generic')
230225
231226
instance ('Diff' a) => 'SpecialDiff' (Tree a) where
232-
type 'SpecialDiffError' (Tree a) = 'DiffErrorNested' '[ '[a, [Tree a]]]
233-
'specialDiff' = 'specialDiffVia' fromTree treeCons
227+
type 'SpecialDiffError' (Tree a) = 'DiffErrorNested' ('GCode' (Tree a))
228+
'specialDiff' = 'gspecialDiffNested'
234229
235230
'renderSpecialDiffError' = 'diffErrorNestedDoc'
236231
237232
instance ('Diff' a) => 'Diff' (Tree a) where
238233
diff = 'diffWithSpecial'
239234
@
240235
-}
241-
specialDiffVia ::
242-
forall a code.
243-
(All2 Diff code) =>
244-
-- | Convert a type to an SOP representation
245-
(a -> NS (NP I) code) ->
246-
-- | Manual list of constructor info
247-
NP ConstructorInfo code ->
236+
gspecialDiffNested ::
237+
forall a.
238+
( G.Generic a
239+
, GFrom a
240+
, GDatatypeInfo a
241+
, All2 Diff (GCode a)
242+
) =>
248243
a ->
249244
a ->
250-
Maybe (DiffErrorNested code)
251-
specialDiffVia toCode constructors l r = gdiff' constructors differs (toCode l) (toCode r)
245+
Maybe (DiffErrorNested (GCode a))
246+
gspecialDiffNested l r = gdiff' constructors differs (unSOP $ gfrom l) (unSOP $ gfrom r)
252247
where
253-
differs :: NP (NP Differ) code
254248
differs = unPOP $ hcpure (Proxy @Diff) (Differ diff)
249+
constructors = constructorInfo $ gdatatypeInfo $ Proxy @a
255250

256251
------------------------------------------------------------
257252
-- Auxiliary functions

src/Generics/Diff/Special.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ instance ('Generics.Diff.Diff' a) => 'SpecialDiff' ('NE.NonEmpty' a) where
7474
module Generics.Diff.Special
7575
( SpecialDiff (..)
7676
, diffWithSpecial
77-
, specialDiffVia
77+
, gspecialDiffNested
7878

7979
-- * Lists
8080
, module List

0 commit comments

Comments
 (0)