on bfcd00a help sign in
76 lines 2.1 kB history raw
1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE FunctionalDependencies #-}
4{-# LANGUAGE MonoLocalBinds #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE PolyKinds #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE TypeApplications #-}
9{-# LANGUAGE UndecidableInstances #-}
11-----------------------------------------------------------------------------
12-- |
13-- Module : Data.Generics.Product.Any
14-- Copyright : (C) 2017 Csongor Kiss
15-- License : BSD3
16-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
17-- Stability : experimental
18-- Portability : non-portable
19--
20-- Derive a variety of lenses generically.
21--
22-----------------------------------------------------------------------------
24module Data.Generics.Product.Any
25 ( -- *Lenses
26 --
27 -- $example
28 HasAny (..)
29 ) where
31import Data.Generics.Internal.Lens
32import Data.Generics.Product.Fields
33import Data.Generics.Product.Positions
34import Data.Generics.Product.Typed
36-- $example
37-- @
38-- module Example where
39--
40-- import Data.Generics.Product
41-- import GHC.Generics
42--
43-- data Human = Human
44-- { name :: String
45-- , age :: Int
46-- , address :: String
47-- }
48-- deriving (Generic, Show)
49--
50-- human :: Human
51-- human = Human \"Tunyasz\" 50 \"London\"
52-- @
54-- |Records that have generic lenses.
55class HasAny (sel :: k) a s | s sel k -> a where
56 -- |A lens that focuses on a part of a product as identified by some
57 -- selector. Currently supported selectors are field names, positions and
58 -- unique types. Compatible with the lens package's 'Control.Lens.Lens'
59 -- type.
60 --
61 -- >>> human ^. the @Int
62 -- 50
63 -- >>> human ^. the @"name"
64 -- "Tunyasz"
65 -- >>> human ^. the @3
66 -- "London"
67 the :: Lens' s a
69instance HasPosition i a s => HasAny i a s where
70 the = position @i
72instance HasField field a s => HasAny field a s where
73 the = field @field
75instance HasType a s => HasAny a a s where
76 the = typed @a