{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FlexibleInstances #-} module Ahc.Data.Lens.Tuple where import Ahc.Data.Lens import Data.Functor.Identity class Field1 s t a b | s -> a, t -> b, s b -> t, t a -> s where _1 :: Lens s t a b instance Field1 (Identity a) (Identity a) a a where _1 = lens runIdentity (\_ x -> Identity x) instance Field1 (a, b) (a', b) a a' where _1 = lens (\(~(x, _)) -> x) (\(~(_, y)) x -> (x, y)) instance Field1 (a, b, c) (a', b, c) a a' where _1 = lens (\(~(x, _, _)) -> x) (\(~(_, y, z)) x -> (x, y, z)) instance Field1 (a, b, c, d) (a', b, c, d) a a' where _1 = lens (\(~(x, _, _, _)) -> x) (\(~(_, y, z, a)) x -> (x, y, z, a)) class Field2 s t a b | s -> a, t -> b, s b -> t, t a -> s where _2 :: Lens s t a b instance Field2 (a, b) (a, b') b b' where _2 = lens (\(~(_, y)) -> y) (\(~(x, _)) y -> (x, y)) instance Field2 (a, b, c) (a, b', c) b b' where _2 = lens (\(~(_, x, _)) -> x) (\(~(x, _, z)) y -> (x, y, z)) instance Field2 (a, b, c, d) (a, b', c, d) b b' where _2 = lens (\(~(_, x, _, _)) -> x) (\(~(x, _, z, a)) y -> (x, y, z, a)) class Field3 s t a b | s -> a, t -> b, s b -> t, t a -> s where _3 :: Lens s t a b instance Field3 (a, b, c) (a, b, c') c c' where _3 = lens (\(~(_, _, x)) -> x) (\(~(x, y, _)) z -> (x, y, z)) instance Field3 (a, b, c, d) (a, b, c', d) c c' where _3 = lens (\(~(_, _, x, _)) -> x) (\(~(x, y, _, a)) z -> (x, y, z, a))