11{-# LANGUAGE DerivingVia #-}
22{-# OPTIONS_GHC -Wno-orphans #-}
33
4- {- | A worked example of implementing 'SpecialDiff' (and thereby 'Diff') for 'Tree's.
4+ {- | A worked example of implementing 'SpecialDiff' (and thereby 'Diff') for 'Tree.Tree 's.
55
66As with other 3rd-party types, there are different approaches we can take here. We'll show 2 of them:
77
@@ -22,6 +22,7 @@ import Generics.SOP.GGP
2222------------------------------------------------------------
2323-- Using gspecialDiffNested
2424
25+ -- | Generically-derived instance.
2526instance (Diff a ) => SpecialDiff (Tree. Tree a ) where
2627 type SpecialDiffError (Tree. Tree a ) = DiffErrorNested (GCode (Tree. Tree a ))
2728 specialDiff = gspecialDiffNested
@@ -33,17 +34,23 @@ instance (Diff a) => Diff (Tree.Tree a) where
3334------------------------------------------------------------
3435-- Using SpecialDiff
3536
37+ {- | A newtype wrapper around 'Tree.Tree' to demonstrate one alternate way we could hand-write
38+ a 'SpecialDiff' instance.
39+ -}
3640newtype CustomTree a = CustomTree (Tree. Tree a )
3741 deriving (Show ) via (Tree. Tree a )
3842
43+ -- | Where are we in the tree? Each element of the list says which child node we step to next.
3944newtype TreePath = TreePath [Int ]
4045 deriving (Show , Eq ) via [Int ]
4146
47+ -- | A custom error type for 'CustomTree'.
4248data CustomTreeDiffError a
4349 = DiffAtNode TreePath (DiffError a )
4450 | WrongLengthsOfChildren TreePath Int Int
4551 deriving (Show , Eq )
4652
53+ -- | Render a tree path as a 'TB.Builder'
4754renderTreePath :: TreePath -> TB. Builder
4855renderTreePath (TreePath [] ) = " <root>"
4956renderTreePath (TreePath (x : xs)) = mconcat $ showB x : [" ->" <> showB y | y <- xs]
0 commit comments