@@ -60,6 +60,7 @@ import Data.Hashable
6060#if __GLASGOW_HASKELL__ < 910
6161import Data.List (foldl' )
6262#endif
63+ import Data.Bits (Bits (.. ))
6364import Data.List.NonEmpty qualified as NE
6465import Data.Map.Strict qualified as Map
6566import Data.Text qualified as T
@@ -132,7 +133,8 @@ data OrRef a
132133
133134type RefCTree = CTreeRoot OrRef
134135
135- deriving instance Show (CTree OrRef )
136+ instance Show (CTree OrRef ) where
137+ show = showCTree
136138
137139deriving instance Show (CTreeRoot OrRef )
138140
@@ -340,11 +342,34 @@ data DistRef a
340342
341343instance Hashable a => Hashable (DistRef a )
342344
343- deriving instance Show (CTree DistRef )
344-
345- deriving instance Eq (CTree DistRef )
346-
347- instance Hashable (CTree DistRef )
345+ instance Show (CTree DistRef ) where
346+ show = showCTree
347+
348+ instance Eq (CTree DistRef ) where
349+ (==) = eqCTree
350+
351+ instance Hashable (CTree DistRef ) where
352+ hashWithSalt salt = \ case
353+ CTree. Literal x -> hashWithSalt salt $ hashWithSalt salt x
354+ CTree. Postlude x -> hashWithSalt (salt `xor` 1 ) $ hashWithSalt salt x
355+ CTree. Map x -> hashWithSalt (salt `xor` 2 ) $ hashWithSalt salt x
356+ CTree. Array x -> hashWithSalt (salt `xor` 3 ) $ hashWithSalt salt x
357+ CTree. Choice x -> hashWithSalt (salt `xor` 4 ) $ hashWithSalt salt x
358+ CTree. Group x -> hashWithSalt (salt `xor` 5 ) $ hashWithSalt salt x
359+ CTree. Enum x -> hashWithSalt (salt `xor` 6 ) $ hashWithSalt salt x
360+ CTree. Unwrap x -> hashWithSalt (salt `xor` 7 ) $ hashWithSalt salt x
361+ CTree. Occur x y -> hashWithSalt (salt `xor` 8 ) $ hashWithSalt salt x `xor` hashWithSalt (salt `xor` 1 ) y
362+ CTree. Tag x y -> hashWithSalt (salt `xor` 9 ) $ hashWithSalt salt x `xor` hashWithSalt (salt `xor` 1 ) y
363+ CTree. WithGen _ y -> hashWithSalt (salt `xor` 10 ) $ hashWithSalt (salt `xor` 1 ) y
364+ CTree. KV x y z ->
365+ hashWithSalt (salt `xor` 11 ) $
366+ hashWithSalt salt x `xor` hashWithSalt (salt `xor` 1 ) y `xor` hashWithSalt (salt `xor` 2 ) z
367+ CTree. Range x y z ->
368+ hashWithSalt (salt `xor` 12 ) $
369+ hashWithSalt salt x `xor` hashWithSalt (salt `xor` 1 ) y `xor` hashWithSalt (salt `xor` 2 ) z
370+ CTree. Control x y z ->
371+ hashWithSalt (salt `xor` 13 ) $
372+ hashWithSalt salt x `xor` hashWithSalt (salt `xor` 1 ) y `xor` hashWithSalt (salt `xor` 2 ) z
348373
349374deriving instance Show (CTreeRoot DistRef )
350375
@@ -400,7 +425,41 @@ data MonoRef a
400425 | MRuleRef Name
401426 deriving (Functor , Show )
402427
403- deriving instance Show (CTree MonoRef )
428+ showCTree :: Show (f (CTree f )) => CTree f -> String
429+ showCTree (CTree. Literal x) = " Literal " <> show x
430+ showCTree (CTree. Postlude x) = " Postlude " <> show x
431+ showCTree (CTree. Map x) = " Map " <> show x
432+ showCTree (CTree. Array x) = " Array " <> show x
433+ showCTree (CTree. Choice x) = " Choice " <> show x
434+ showCTree (CTree. Group x) = " Group " <> show x
435+ showCTree (CTree. KV x y z) = " KV " <> show x <> " " <> show y <> " " <> show z
436+ showCTree (CTree. Occur x y) = " Occur " <> show x <> " " <> show y
437+ showCTree (CTree. Range x y z) = " Range " <> show x <> " " <> show y <> " " <> show z
438+ showCTree (CTree. Control x y z) = " Control " <> show x <> " " <> show y <> " " <> show z
439+ showCTree (CTree. Enum x) = " Enum " <> show x
440+ showCTree (CTree. Unwrap x) = " Unwrap " <> show x
441+ showCTree (CTree. Tag x y) = " Tag " <> show x <> " " <> show y
442+ showCTree (CTree. WithGen _ y) = " WithGen " <> show y
443+
444+ eqCTree :: Eq (f (CTree f )) => CTree f -> CTree f -> Bool
445+ eqCTree (CTree. Literal x) (CTree. Literal x') = x == x'
446+ eqCTree (CTree. Postlude x) (CTree. Postlude x') = x == x'
447+ eqCTree (CTree. Map x) (CTree. Map x') = x == x'
448+ eqCTree (CTree. Array x) (CTree. Array x') = x == x'
449+ eqCTree (CTree. Choice x) (CTree. Choice x') = x == x'
450+ eqCTree (CTree. Group x) (CTree. Group x') = x == x'
451+ eqCTree (CTree. KV x y z) (CTree. KV x' y' z') = x == x' && y == y' && z == z'
452+ eqCTree (CTree. Occur x y) (CTree. Occur x' y') = x == x' && y == y'
453+ eqCTree (CTree. Range x y z) (CTree. Range x' y' z') = x == x' && y == y' && z == z'
454+ eqCTree (CTree. Control x y z) (CTree. Control x' y' z') = x == x' && y == y' && z == z'
455+ eqCTree (CTree. Enum x) (CTree. Enum x') = x == x'
456+ eqCTree (CTree. Unwrap x) (CTree. Unwrap x') = x == x'
457+ eqCTree (CTree. Tag x y) (CTree. Tag x' y') = x == x' && y == y'
458+ eqCTree (CTree. WithGen _ y) (CTree. WithGen _ y') = y == y'
459+ eqCTree _ _ = False
460+
461+ instance Show (CTree MonoRef ) where
462+ show = showCTree
404463
405464deriving instance
406465 Show (poly (CTree. Node MonoRef )) =>
0 commit comments