Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Created June 20, 2017 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjoerdvisscher/c84d6ae6ff6fdb80df9b772f5fdb421b to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/c84d6ae6ff6fdb80df9b772f5fdb421b to your computer and use it in GitHub Desktop.
Generic unification with one-liner
-- See https://www.reddit.com/r/haskell/comments/6htj7z/generic_unification/
default zipMatch :: (ADT1 t, Constraints01 t Eq Unifiable) => t a -> t a -> Maybe (t (Either a (a, a)))
zipMatch = runZip $ generic01 @Eq @Unifiable
(Zip $ \a b -> if a == b then Just a else Nothing)
(\(Zip f) -> Zip $ \a b -> zipMatch a b >>= traverse (either (\x -> f x x) (uncurry f)))
(Zip $ \a b -> Just $ Right (a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment