|
1 | 1 | [](https://github.com/fpringle/generic-diff/actions/workflows/haskell.yml) |
2 | 2 |
|
3 | | -# Generic structural diffs |
| 3 | +`generic-diff` provides a typeclass-based way to structurally compare values of the same type. |
4 | 4 |
|
5 | | -`generic-diff` lets us pinpoint exactly where two values differ, which can be very useful, for example for debugging failing tests. |
6 | | -This functionality is provided by the `Diff` typeclass, for which instances can be derived automatically using `Generic` from |
7 | | -[generics-sop](https://github.com/well-typed/generics-sop). |
8 | | - |
9 | | -For detailed information, see the [Hackage docs](https://hackage.haskell.org/package/generic-diff/docs/Generics-Diff.html). |
10 | | - |
11 | | -## Example |
12 | | - |
13 | | -```haskell |
14 | | -{-# LANGUAGE DerivingStrategies #-} |
15 | | -{-# LANGUAGE DeriveGeneric #-} |
16 | | -{-# LANGUAGE DeriveAnyClass #-} |
17 | | - |
18 | | -import Generics.Diff |
19 | | -import Generics.Diff.Render |
20 | | - |
21 | | -import qualified GHC.Generics as G |
22 | | -import qualified Generics.SOP as SOP |
23 | | - |
24 | | -data BinOp = Plus | Minus |
25 | | - deriving stock (Show, G.Generic) |
26 | | - deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo, Diff) |
27 | | - |
28 | | -data Expr |
29 | | - = Atom Int |
30 | | - | Bin {left :: Expr, op :: BinOp, right :: Expr} |
31 | | - deriving stock (Show, G.Generic) |
32 | | - deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo, Diff) |
33 | | - |
34 | | -expr1, expr2 :: Expr |
35 | | -expr1 = Bin (Atom 1) Plus (Bin (Atom 1) Plus (Atom 1)) |
36 | | -expr2 = Bin (Atom 1) Plus (Bin (Atom 1) Plus (Atom 2)) |
37 | | -``` |
38 | | - |
39 | | -```haskell |
40 | | -ghci> printDiffResult $ diff expr1 expr2 |
41 | | -In field right: |
42 | | - Both values use constructor Bin but fields don't match |
43 | | - In field right: |
44 | | - Both values use constructor Atom but fields don't match |
45 | | - In field 0 (0-indexed): |
46 | | - Not equal |
47 | | -``` |
| 5 | +See [generic-diff](./generic-diff#readme) for the core package, or [generic-diff-instances](./generic-diff-instances#readme) for a more "batteries-included" set of instances. |
0 commit comments