Skip to content

Instantly share code, notes, and snippets.

@gwils
Last active June 29, 2018 05:39
Show Gist options
  • Save gwils/5de85c77487061a270c61af8f777eb56 to your computer and use it in GitHub Desktop.
Save gwils/5de85c77487061a270c61af8f777eb56 to your computer and use it in GitHub Desktop.
data These a b = This a | That b | These a b
data These3 a b c = T3a a | T3b b | T3c c | T3ab a b | T3bc b c | T3ac a c | T3abc a b c
theseTo3 :: These a (These b c) -> These3 a b c
theseTo3 (This a) = T3a a
theseTo3 (That (This b)) = T3b b
theseTo3 (That (That c)) = T3c c
theseTo3 (These a (This b)) = T3ab a b
theseTo3 (That (These b c)) = T3bc b c
theseTo3 (These a (That c)) = T3ac a c
theseTo3 (These a (These b c)) = T3abc a b c
threeToThese :: These3 a b c -> These a (These b c)
threeToThese (T3a a) = This a
threeToThese (T3b b) = That (This b)
threeToThese (T3c c) = That (That c)
threeToThese (T3ab a b) = These a (This b)
threeToThese (T3bc b c) = That (These b c)
threeToThese (T3ac a c) = These a (That c)
threeToThese (T3abc a b c) = These a (These b c)
class (Functor f) => Align f where
nil :: f a
align :: f a -> f b -> f (These a b)
align = alignWith id
alignWith :: (These a b -> c) -> f a -> f b -> f c
alignWith f a b = f <$> align a b
alignWith3 :: Align f => (These3 a b c -> x) -> f a -> f b -> f c -> f x
alignWith3 f a b c =
let
f' = f . theseTo3
in
alignWith f' a (align b c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment