Skip to content

Commit a8d75ac

Browse files
Expose FieldN classes and instances from Lens.Micro.Internal to Lens.Micro
Also add FieldN classes and instances up to Field9
1 parent 44c49b2 commit a8d75ac

File tree

4 files changed

+464
-218
lines changed

4 files changed

+464
-218
lines changed

microlens/microlens.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ library
6565
Lens.Micro.Extras
6666
Lens.Micro.Internal
6767
Lens.Micro.Type
68+
Lens.Micro.FieldN
6869
-- other-modules:
6970
-- other-extensions:
7071

microlens/src/Lens/Micro.hs

Lines changed: 215 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ module Lens.Micro
8080
Lens, Lens',
8181
lens,
8282
at,
83-
_1, _2, _3, _4, _5,
83+
-- _1, _2, _3, _4, _5,
84+
module Lens.Micro.FieldN,
8485

8586
-- * Iso: a lens that only changes the representation
8687
-- $isos-note
@@ -115,7 +116,7 @@ module Lens.Micro
115116
)
116117
where
117118

118-
119+
import Lens.Micro.FieldN
119120
import Lens.Micro.Type
120121
import Lens.Micro.Internal
121122

@@ -1623,3 +1624,215 @@ instance (Monad m) => Monad (StateT s m) where
16231624
instance (Fail.MonadFail m) => Fail.MonadFail (StateT s m) where
16241625
fail str = StateT $ \ _ -> Fail.fail str
16251626
#endif
1627+
1628+
1629+
-- class Field1 s t a b | s -> a, t -> b, s b -> t, t a -> s where
1630+
-- {- |
1631+
-- Gives access to the 1st field of a tuple (up to 5-tuples).
1632+
1633+
-- Getting the 1st component:
1634+
1635+
-- >>> (1,2,3,4,5) ^. _1
1636+
-- 1
1637+
1638+
-- Setting the 1st component:
1639+
1640+
-- >>> (1,2,3) & _1 .~ 10
1641+
-- (10,2,3)
1642+
1643+
-- Note that this lens is lazy, and can set fields even of 'undefined':
1644+
1645+
-- >>> set _1 10 undefined :: (Int, Int)
1646+
-- (10,*** Exception: Prelude.undefined
1647+
1648+
-- This is done to avoid violating a lens law stating that you can get back what you put:
1649+
1650+
-- >>> view _1 . set _1 10 $ (undefined :: (Int, Int))
1651+
-- 10
1652+
1653+
-- The implementation (for 2-tuples) is:
1654+
1655+
-- @
1656+
-- '_1' f t = (,) '<$>' f ('fst' t)
1657+
-- '<*>' 'pure' ('snd' t)
1658+
-- @
1659+
1660+
-- or, alternatively,
1661+
1662+
-- @
1663+
-- '_1' f ~(a,b) = (\\a' -> (a',b)) '<$>' f a
1664+
-- @
1665+
1666+
-- (where @~@ means a <https://wiki.haskell.org/Lazy_pattern_match lazy pattern>).
1667+
1668+
-- '_2', '_3', '_4', and '_5' are also available (see below).
1669+
-- -}
1670+
-- _1 :: Lens s t a b
1671+
1672+
-- instance Field1 (a,b) (a',b) a a' where
1673+
-- _1 k ~(a,b) = (\a' -> (a',b)) <$> k a
1674+
-- {-# INLINE _1 #-}
1675+
1676+
-- instance Field1 (a,b,c) (a',b,c) a a' where
1677+
-- _1 k ~(a,b,c) = (\a' -> (a',b,c)) <$> k a
1678+
-- {-# INLINE _1 #-}
1679+
1680+
-- instance Field1 (a,b,c,d) (a',b,c,d) a a' where
1681+
-- _1 k ~(a,b,c,d) = (\a' -> (a',b,c,d)) <$> k a
1682+
-- {-# INLINE _1 #-}
1683+
1684+
-- instance Field1 (a,b,c,d,e) (a',b,c,d,e) a a' where
1685+
-- _1 k ~(a,b,c,d,e) = (\a' -> (a',b,c,d,e)) <$> k a
1686+
-- {-# INLINE _1 #-}
1687+
1688+
-- {-
1689+
1690+
-- instance Field1 (a,b,c,d,e,f) (a',b,c,d,e,f) a a' where
1691+
-- _1 k ~(a,b,c,d,e,f) = (\a' -> (a',b,c,d,e,f)) <$> k a
1692+
-- {-# INLINE _1 #-}
1693+
1694+
-- instance Field1 (a,b,c,d,e,f,g) (a',b,c,d,e,f,g) a a' where
1695+
-- _1 k ~(a,b,c,d,e,f,g) = (\a' -> (a',b,c,d,e,f,g)) <$> k a
1696+
-- {-# INLINE _1 #-}
1697+
1698+
-- instance Field1 (a,b,c,d,e,f,g,h) (a',b,c,d,e,f,g,h) a a' where
1699+
-- _1 k ~(a,b,c,d,e,f,g,h) = (\a' -> (a',b,c,d,e,f,g,h)) <$> k a
1700+
-- {-# INLINE _1 #-}
1701+
1702+
-- instance Field1 (a,b,c,d,e,f,g,h,i) (a',b,c,d,e,f,g,h,i) a a' where
1703+
-- _1 k ~(a,b,c,d,e,f,g,h,i) = (\a' -> (a',b,c,d,e,f,g,h,i)) <$> k a
1704+
-- {-# INLINE _1 #-}
1705+
1706+
-- -}
1707+
1708+
-- class Field2 s t a b | s -> a, t -> b, s b -> t, t a -> s where
1709+
-- _2 :: Lens s t a b
1710+
1711+
-- instance Field2 (a,b) (a,b') b b' where
1712+
-- _2 k ~(a,b) = (\b' -> (a,b')) <$> k b
1713+
-- {-# INLINE _2 #-}
1714+
1715+
-- instance Field2 (a,b,c) (a,b',c) b b' where
1716+
-- _2 k ~(a,b,c) = (\b' -> (a,b',c)) <$> k b
1717+
-- {-# INLINE _2 #-}
1718+
1719+
-- instance Field2 (a,b,c,d) (a,b',c,d) b b' where
1720+
-- _2 k ~(a,b,c,d) = (\b' -> (a,b',c,d)) <$> k b
1721+
-- {-# INLINE _2 #-}
1722+
1723+
-- instance Field2 (a,b,c,d,e) (a,b',c,d,e) b b' where
1724+
-- _2 k ~(a,b,c,d,e) = (\b' -> (a,b',c,d,e)) <$> k b
1725+
-- {-# INLINE _2 #-}
1726+
1727+
-- {-
1728+
1729+
-- instance Field2 (a,b,c,d,e,f) (a,b',c,d,e,f) b b' where
1730+
-- _2 k ~(a,b,c,d,e,f) = (\b' -> (a,b',c,d,e,f)) <$> k b
1731+
-- {-# INLINE _2 #-}
1732+
1733+
-- instance Field2 (a,b,c,d,e,f,g) (a,b',c,d,e,f,g) b b' where
1734+
-- _2 k ~(a,b,c,d,e,f,g) = (\b' -> (a,b',c,d,e,f,g)) <$> k b
1735+
-- {-# INLINE _2 #-}
1736+
1737+
-- instance Field2 (a,b,c,d,e,f,g,h) (a,b',c,d,e,f,g,h) b b' where
1738+
-- _2 k ~(a,b,c,d,e,f,g,h) = (\b' -> (a,b',c,d,e,f,g,h)) <$> k b
1739+
-- {-# INLINE _2 #-}
1740+
1741+
-- instance Field2 (a,b,c,d,e,f,g,h,i) (a,b',c,d,e,f,g,h,i) b b' where
1742+
-- _2 k ~(a,b,c,d,e,f,g,h,i) = (\b' -> (a,b',c,d,e,f,g,h,i)) <$> k b
1743+
-- {-# INLINE _2 #-}
1744+
1745+
-- -}
1746+
1747+
-- class Field3 s t a b | s -> a, t -> b, s b -> t, t a -> s where
1748+
-- _3 :: Lens s t a b
1749+
1750+
-- instance Field3 (a,b,c) (a,b,c') c c' where
1751+
-- _3 k ~(a,b,c) = (\c' -> (a,b,c')) <$> k c
1752+
-- {-# INLINE _3 #-}
1753+
1754+
-- instance Field3 (a,b,c,d) (a,b,c',d) c c' where
1755+
-- _3 k ~(a,b,c,d) = (\c' -> (a,b,c',d)) <$> k c
1756+
-- {-# INLINE _3 #-}
1757+
1758+
-- instance Field3 (a,b,c,d,e) (a,b,c',d,e) c c' where
1759+
-- _3 k ~(a,b,c,d,e) = (\c' -> (a,b,c',d,e)) <$> k c
1760+
-- {-# INLINE _3 #-}
1761+
1762+
-- {-
1763+
1764+
-- instance Field3 (a,b,c,d,e,f) (a,b,c',d,e,f) c c' where
1765+
-- _3 k ~(a,b,c,d,e,f) = (\c' -> (a,b,c',d,e,f)) <$> k c
1766+
-- {-# INLINE _3 #-}
1767+
1768+
-- instance Field3 (a,b,c,d,e,f,g) (a,b,c',d,e,f,g) c c' where
1769+
-- _3 k ~(a,b,c,d,e,f,g) = (\c' -> (a,b,c',d,e,f,g)) <$> k c
1770+
-- {-# INLINE _3 #-}
1771+
1772+
-- instance Field3 (a,b,c,d,e,f,g,h) (a,b,c',d,e,f,g,h) c c' where
1773+
-- _3 k ~(a,b,c,d,e,f,g,h) = (\c' -> (a,b,c',d,e,f,g,h)) <$> k c
1774+
-- {-# INLINE _3 #-}
1775+
1776+
-- instance Field3 (a,b,c,d,e,f,g,h,i) (a,b,c',d,e,f,g,h,i) c c' where
1777+
-- _3 k ~(a,b,c,d,e,f,g,h,i) = (\c' -> (a,b,c',d,e,f,g,h,i)) <$> k c
1778+
-- {-# INLINE _3 #-}
1779+
1780+
-- -}
1781+
1782+
-- class Field4 s t a b | s -> a, t -> b, s b -> t, t a -> s where
1783+
-- _4 :: Lens s t a b
1784+
1785+
-- instance Field4 (a,b,c,d) (a,b,c,d') d d' where
1786+
-- _4 k ~(a,b,c,d) = (\d' -> (a,b,c,d')) <$> k d
1787+
-- {-# INLINE _4 #-}
1788+
1789+
-- instance Field4 (a,b,c,d,e) (a,b,c,d',e) d d' where
1790+
-- _4 k ~(a,b,c,d,e) = (\d' -> (a,b,c,d',e)) <$> k d
1791+
-- {-# INLINE _4 #-}
1792+
1793+
-- {-
1794+
1795+
-- instance Field4 (a,b,c,d,e,f) (a,b,c,d',e,f) d d' where
1796+
-- _4 k ~(a,b,c,d,e,f) = (\d' -> (a,b,c,d',e,f)) <$> k d
1797+
-- {-# INLINE _4 #-}
1798+
1799+
-- instance Field4 (a,b,c,d,e,f,g) (a,b,c,d',e,f,g) d d' where
1800+
-- _4 k ~(a,b,c,d,e,f,g) = (\d' -> (a,b,c,d',e,f,g)) <$> k d
1801+
-- {-# INLINE _4 #-}
1802+
1803+
-- instance Field4 (a,b,c,d,e,f,g,h) (a,b,c,d',e,f,g,h) d d' where
1804+
-- _4 k ~(a,b,c,d,e,f,g,h) = (\d' -> (a,b,c,d',e,f,g,h)) <$> k d
1805+
-- {-# INLINE _4 #-}
1806+
1807+
-- instance Field4 (a,b,c,d,e,f,g,h,i) (a,b,c,d',e,f,g,h,i) d d' where
1808+
-- _4 k ~(a,b,c,d,e,f,g,h,i) = (\d' -> (a,b,c,d',e,f,g,h,i)) <$> k d
1809+
-- {-# INLINE _4 #-}
1810+
1811+
-- -}
1812+
1813+
-- class Field5 s t a b | s -> a, t -> b, s b -> t, t a -> s where
1814+
-- _5 :: Lens s t a b
1815+
1816+
-- instance Field5 (a,b,c,d,e) (a,b,c,d,e') e e' where
1817+
-- _5 k ~(a,b,c,d,e) = (\e' -> (a,b,c,d,e')) <$> k e
1818+
-- {-# INLINE _5 #-}
1819+
1820+
-- {-
1821+
1822+
-- instance Field5 (a,b,c,d,e,f) (a,b,c,d,e',f) e e' where
1823+
-- _5 k ~(a,b,c,d,e,f) = (\e' -> (a,b,c,d,e',f)) <$> k e
1824+
-- {-# INLINE _5 #-}
1825+
1826+
-- instance Field5 (a,b,c,d,e,f,g) (a,b,c,d,e',f,g) e e' where
1827+
-- _5 k ~(a,b,c,d,e,f,g) = (\e' -> (a,b,c,d,e',f,g)) <$> k e
1828+
-- {-# INLINE _5 #-}
1829+
1830+
-- instance Field5 (a,b,c,d,e,f,g,h) (a,b,c,d,e',f,g,h) e e' where
1831+
-- _5 k ~(a,b,c,d,e,f,g,h) = (\e' -> (a,b,c,d,e',f,g,h)) <$> k e
1832+
-- {-# INLINE _5 #-}
1833+
1834+
-- instance Field5 (a,b,c,d,e,f,g,h,i) (a,b,c,d,e',f,g,h,i) e e' where
1835+
-- _5 k ~(a,b,c,d,e,f,g,h,i) = (\e' -> (a,b,c,d,e',f,g,h,i)) <$> k e
1836+
-- {-# INLINE _5 #-}
1837+
1838+
-- -}

0 commit comments

Comments
 (0)