generic-optics-lite (empty) → 0.1
raw patch · 4 files changed
+217/−0 lines, 4 filesdep +basedep +generic-lens-litedep +generic-optics-lite
Dependencies added: base, generic-lens-lite, generic-optics-lite, optics-core
Files
- LICENSE +30/−0
- example/Example.hs +65/−0
- generic-optics-lite.cabal +57/−0
- src/Data/Generics/Optics/Lite.hs +65/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2020 Oleg Grenrus++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Oleg Grenrus nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ example/Example.hs view
@@ -0,0 +1,65 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE OverloadedLabels #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE UndecidableInstances #-}+{-# OPTIONS_GHC -funfolding-keeness-factor=100 #-}+module Main (module Main) where++import Data.Generics.Optics.Lite (HasField, field)+import GHC.Generics (Generic)+import Optics.Core (A_Lens, LabelOptic (..), Lens')++main :: IO ()+main = return ()++-------------------------------------------------------------------------------+-- Big record+-------------------------------------------------------------------------------++data Ex = Ex+ { exA :: Int+ , exB :: Bool+ , ex0 :: Char+ , ex1 :: Char+ , ex2 :: Char+ , ex3 :: Char+ , ex4 :: Char+ , ex5 :: Char+ , ex6 :: Char+ , ex7 :: Char+ , ex8 :: Char+ , ex9 :: Char+ } deriving (Generic)++instance (HasField name Ex a, a ~ b) => LabelOptic name A_Lens Ex Ex a b where+ labelOptic = field @name++_exA :: Lens' Ex Int+_exA = field @"exA"++_exA' :: Lens' Ex Int+_exA' = #exA++_exB :: Lens' Ex Bool+_exB = field @"exB"++_ex4 :: Lens' Ex Char+_ex4 = field @"ex4"++-------------------------------------------------------------------------------+-- Sum of records+-------------------------------------------------------------------------------++data Ex2+ = Ex2A { exC :: Char }+ | Ex2B { exC :: Char }+ deriving (Generic)++_exC :: Lens' Ex2 Char+_exC = field @"exC"
+ generic-optics-lite.cabal view
@@ -0,0 +1,57 @@+cabal-version: 2.2+name: generic-optics-lite+version: 0.1+synopsis: Monomorphic field opics like with generic-lens+category: Lens, Optics, Generics+description:+ Derivation of (monomorphic, i.e. not type-changing) lens, like generic-lens.+ .+ The package have minimal dependencies and minimal API:+ .+ @+ class HasField (name :: Symbol) r a | name r -> a+ field :: HasField name r a => Lens' r a+ @++homepage: https://github.com/phadej/generic-lens-lite+license: BSD-3-Clause+license-file: LICENSE+author: Edward Kmett, Csongor Kiss, Oleg Grenrus+maintainer: Oleg Grenrus <oleg.grenrus@iki.fi>+copyright: Copyright (c) 2019 Edward Kmett, 2020 Oleg Grenrus+build-type: Simple+tested-with: GHC ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.1++source-repository head+ type: git+ location: https://github.com/phadej/generic-lens-lite+ subdir: generic-optics-lite++library+ default-language: Haskell2010+ hs-source-dirs: src+ ghc-options: -Wall+ exposed-modules: Data.Generics.Optics.Lite+ build-depends:+ , base >=4.9 && <4.14+ , generic-lens-lite >=0.1 && <0.1.1+ , optics-core ^>=0.2++ if impl(ghc >=8.4)+ ghc-options:+ -Wincomplete-uni-patterns -Wincomplete-record-updates+ -Wredundant-constraints -Widentities -Wmissing-export-lists++test-suite example+ type: exitcode-stdio-1.0+ default-language: Haskell2010+ hs-source-dirs: example+ main-is: Example.hs+ ghc-options: -Wall+ build-depends:+ , base+ , generic-optics-lite+ , optics-core++-- build-depends: dump-core+-- ghc-options: -fplugin=DumpCore
+ src/Data/Generics/Optics/Lite.hs view
@@ -0,0 +1,65 @@+{-# LANGUAGE AllowAmbiguousTypes #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE KindSignatures #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}+-- | Derive record field lenses generically.+--+-- == Usage with labels in "Optics.Label"+--+-- "Optics.Label" provides 'Optics.Label.LabelOptic' class which powers+-- the @OverloadedLabels@ instance for optics type.+--+-- It's possible to use 'L.HasField' and 'field' to derive instances+-- generically. If we have a simple record type which has 'GHC.Generics.Generic'+-- instance:+--+-- @+-- data Ex = Ex { exA :: Int, exB :: Char } deriving (Generic)+-- @+--+-- We can derive 'Optics.Label.LabelOptic' instances for all its fields+-- at once:+--+-- @+-- instance ('HasField' name Ex a, a ~ b) => LabelOptic name A_Lens Ex Ex a b where+-- labelOptic = 'field' @name+-- @+--+-- /Note:/ GHC will ask you to enable a lot of extensions, do it.+-- Youn need to enable @UndecidableInstances@ in particular to make+-- @FunctionalDependencies@ check pass.+--+module Data.Generics.Optics.Lite (+ field,+ L.HasField,+ ) where++import Data.Kind (Type)+import GHC.TypeLits (Symbol)+import Optics.Core (Lens', lensVL)++import qualified Data.Generics.Lens.Lite as L++-------------------------------------------------------------------------------+-- Public API+-------------------------------------------------------------------------------+++-- | A lens that focuses on a field with a given name.+-- Compatible with the optics package's 'Optics.Lens.Lens' type.+--+-- __Note:__ the lens is /simple/, i.e. doesn't allow type-changing updates.+-- This keeps the implementation small and quick.+--+-- You also may want to specify+-- @+-- {-\# OPTIONS_GHC -funfolding-keeness-factor=100 #-} (or some other arbitrarily large number)+-- @+-- for GHC to inline more aggressively.+--+field+ :: forall (name :: Symbol) (r :: Type) (a :: Type). L.HasField name r a+ => Lens' r a+field = lensVL (L.field @name)