Advertisement
Tysonzero

Data/Diff.hs

Oct 17th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Data.Diff (Diff, Diffable, diff, patch) where
  2.  
  3. import Data.Flat.Instances ()
  4. import GHC.Generics (Generic)
  5. import Linear (V2(V2))
  6.  
  7. class Eq a => Diffable a where
  8.     data Diff a
  9.     diff :: a -> a -> Maybe (Diff a)
  10.     patch :: a -> Diff a -> a
  11.  
  12. instance Diffable a => Diffable (V2 a) where
  13.     newtype Diff (V2 a) = DiffV2 (V2 (Maybe (Diff a)))
  14.         deriving (Generic)
  15.  
  16.     diff v@(V2 x y) v'@(V2 x' y')
  17.        | v == v' = Nothing
  18.         | otherwise = Just . DiffV2 $ V2 (diff x x') (diff y y')
  19.  
  20.     patch (V2 x y) (DiffV2 (V2 xd yd)) = V2
  21.         ((maybe <*> patch) x xd)
  22.         ((maybe <*> patch) y yd)
  23.  
  24. instance Flat (Diff a) => Flat (Diff (V2 a))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement