Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Last active August 29, 2015 13:58
Show Gist options
  • Save tonymorris/10003660 to your computer and use it in GitHub Desktop.
Save tonymorris/10003660 to your computer and use it in GitHub Desktop.
Dual to FieldN?
{-
Is this even sensible?
Does this exist already?
-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
import Control.Lens(Choice, prism)
import Control.Applicative(Applicative)
class Constructor1 s t a b | s -> a, t -> b, s b -> t, t a -> s where
_1_ ::
(Choice p, Applicative f) =>
p a (f b)
-> p s (f t)
class Constructor2 s t a b | s -> a, t -> b, s b -> t, t a -> s where
_2_ ::
(Choice p, Applicative f) =>
p a (f b)
-> p s (f t)
instance Constructor1 (Maybe a) (Maybe b) a b where
_1_ =
prism Just (maybe (Left Nothing) Right)
instance Constructor1 (Either a b) (Either x b) a x where
_1_ =
prism Left (either Right (Left . Right))
instance Constructor2 (Either a b) (Either a x) b x where
_2_ =
prism Right (either (Left . Left) Right)
data Either3 a b c =
Left3 a
| Middle3 b
| Right3 c
deriving (Eq, Show)
either3 ::
(a -> x)
-> (b -> x)
-> (c -> x)
-> Either3 a b c
-> x
either3 l _ _ (Left3 a) =
l a
either3 _ m _ (Middle3 b) =
m b
either3 _ _ r (Right3 c) =
r c
instance Constructor1 (Either3 a b c) (Either3 x b c) a x where
_1_ =
prism Left3 (either3 Right (Left . Middle3) (Left . Right3))
instance Constructor2 (Either3 a b c) (Either3 a x c) b x where
_2_ =
prism Middle3 (either3 (Left . Left3) Right (Left . Right3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment