packages feed

generic-lens 0.4.0.1 → 0.4.1.0

raw patch · 11 files changed

+50/−25 lines, 11 filesdep +inspection-testingdep −ghc-proofsdep ~basePVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependencies added: inspection-testing

Dependencies removed: ghc-proofs

Dependency ranges changed: base

API changes (from Hackage documentation)

- Data.Generics.Product.Positions: instance (GHC.Generics.Generic s, Data.Generics.Product.Positions.ErrorUnless i s ((0 Data.Generics.Product.Internal.Positions.<? i) Data.Type.Bool.&& (i GHC.TypeNats.<=? Data.Generics.Product.Internal.Positions.Size (GHC.Generics.Rep s))), Data.Generics.Product.Internal.Positions.GHasPosition 1 i (GHC.Generics.Rep s) a) => Data.Generics.Product.Positions.HasPosition i a s
+ Data.Generics.Internal.Lens: mLens :: Lens' (M1 i c f p) (f p)
+ Data.Generics.Internal.Lens: repLens :: Generic a => Lens' a (Rep a x)
+ Data.Generics.Product.Positions: instance (GHC.Generics.Generic s, Data.Generics.Product.Positions.ErrorUnless i s ((0 Data.Generics.Product.Internal.Positions.<? i) Data.Type.Bool.&& (i GHC.TypeLits.<=? Data.Generics.Product.Internal.Positions.Size (GHC.Generics.Rep s))), Data.Generics.Product.Internal.Positions.GHasPosition 1 i (GHC.Generics.Rep s) a) => Data.Generics.Product.Positions.HasPosition i a s
+ Data.Generics.Sum: injectTyped :: AsType a s => a -> s
+ Data.Generics.Sum: projectTyped :: AsType a s => s -> Maybe a
+ Data.Generics.Sum.Typed: injectTyped :: AsType a s => a -> s
+ Data.Generics.Sum.Typed: projectTyped :: AsType a s => s -> Maybe a
- Data.Generics.Product: class HasField (field :: Symbol) a s | s field -> a
+ Data.Generics.Product: class HasField (field :: Symbol) a s | s field -> a where field f s = fmap (flip (setField @field) s) (f (getField @field s)) getField s = s ^. field @field setField = set (field @field)
- Data.Generics.Product: class HasPosition (i :: Nat) a s | s i -> a
+ Data.Generics.Product: class HasPosition (i :: Nat) a s | s i -> a where position f s = fmap (flip (setPosition @i) s) (f (getPosition @i s)) getPosition s = s ^. position @i setPosition = set (position @i)
- Data.Generics.Product: class HasType a s
+ Data.Generics.Product: class HasType a s where typed f t = fmap (flip (setTyped @a) t) (f (getTyped @a t)) getTyped s = s ^. typed @a setTyped = set (typed @a)
- Data.Generics.Product: class Subtype sup sub
+ Data.Generics.Product: class Subtype sup sub where super f sub = fmap (`smash` sub) (f (upcast sub)) upcast s = s ^. super @sup smash = set (super @sup)
- Data.Generics.Product.Fields: class HasField (field :: Symbol) a s | s field -> a
+ Data.Generics.Product.Fields: class HasField (field :: Symbol) a s | s field -> a where field f s = fmap (flip (setField @field) s) (f (getField @field s)) getField s = s ^. field @field setField = set (field @field)
- Data.Generics.Product.Positions: class HasPosition (i :: Nat) a s | s i -> a
+ Data.Generics.Product.Positions: class HasPosition (i :: Nat) a s | s i -> a where position f s = fmap (flip (setPosition @i) s) (f (getPosition @i s)) getPosition s = s ^. position @i setPosition = set (position @i)
- Data.Generics.Product.Subtype: class Subtype sup sub
+ Data.Generics.Product.Subtype: class Subtype sup sub where super f sub = fmap (`smash` sub) (f (upcast sub)) upcast s = s ^. super @sup smash = set (super @sup)
- Data.Generics.Product.Typed: class HasType a s
+ Data.Generics.Product.Typed: class HasType a s where typed f t = fmap (flip (setTyped @a) t) (f (getTyped @a t)) getTyped s = s ^. typed @a setTyped = set (typed @a)
- Data.Generics.Sum: class AsSubtype sub sup
+ Data.Generics.Sum: class AsSubtype sub sup where _Sub = prism injectSub projectSub
- Data.Generics.Sum.Subtype: class AsSubtype sub sup
+ Data.Generics.Sum.Subtype: class AsSubtype sub sup where _Sub = prism injectSub projectSub

Files

README.md view
@@ -2,6 +2,7 @@  [![Build Status](https://travis-ci.org/kcsongor/generic-lens.svg?branch=master)](https://travis-ci.org/kcsongor/generic-lens)+[![Hackage](https://img.shields.io/hackage/v/generic-lens.svg)](https://hackage.haskell.org/package/generic-lens)  Generically derive lenses and prisms for data types. 
generic-lens.cabal view
@@ -1,5 +1,5 @@ name:                 generic-lens-version:              0.4.0.1+version:              0.4.1.0 synopsis:             Generic data-structure operations exposed as lenses. description:          This package uses the GHC 8 Generic representation to derive various operations on data structures with a lens interface, including structural subtype relationship between records and positional indexing into arbitrary product types. @@ -68,7 +68,7 @@    build-depends:      base          >= 4.9 && <= 5.0                     , generic-lens-                    , ghc-proofs >= 0.1.1+                    , inspection-testing >= 0.1    default-language:   Haskell2010   ghc-options:        -Wall
src/Data/Generics/Internal/Lens.hs view
@@ -77,6 +77,13 @@ mIso :: Iso' (M1 i c f p) (f p) mIso = dimap unM1 (fmap M1) +-- These are specialised versions of the Isos above. On GHC 8.0.2, having+-- these functions eta-expanded allows the optimiser to inline these functions.+mLens :: Lens' (M1 i c f p) (f p)+mLens f s = mIso f s++repLens :: Generic a => Lens' a (Rep a x)+repLens f s = repIso f s  sumIso :: Iso' ((a :+: b) x) (Either (a x) (b x)) sumIso = dimap f (fmap t)
src/Data/Generics/Product/Fields.hs view
@@ -91,7 +91,7 @@   , GHasField field (Rep s) a   ) => HasField field a s where -  field = ravel (repIso . gfield @field)+  field = ravel (repLens . gfield @field)  type family ErrorUnless (field :: Symbol) (s :: Type) (contains :: Bool) :: Constraint where   ErrorUnless field s 'False
src/Data/Generics/Product/Internal/Fields.hs view
@@ -53,13 +53,13 @@   gfield f (K1 x) = fmap K1 (f x)  instance GHasField field (S1 ('MetaSel ('Just field) upkd str infstr) (Rec0 a)) a where-  gfield = mIso . gfield @field+  gfield = mLens . gfield @field  instance GHasField field f a => GHasField field (M1 D meta f) a where-  gfield = mIso . gfield @field+  gfield = mLens . gfield @field  instance GHasField field f a => GHasField field (M1 C meta f) a where-  gfield = mIso . gfield @field+  gfield = mLens . gfield @field  class GProductHasField (field :: Symbol) l r a (left :: Bool) | left field l r -> a where   gproductField :: Lens' ((l :*: r) x) a
src/Data/Generics/Product/Internal/Positions.hs view
@@ -46,7 +46,7 @@   gposition f (K1 x) = fmap K1 (f x)  instance GHasPosition offset i f a => GHasPosition offset i (M1 m meta f) a where-  gposition = mIso . gposition @offset @i+  gposition = mLens . gposition @offset @i  instance   ( goLeft  ~ (i <? (offset + Size l))
src/Data/Generics/Product/Internal/Typed.hs view
@@ -50,7 +50,7 @@   gtyped f (K1 x) = fmap K1 (f x)  instance GHasType f a => GHasType (M1 m meta f) a where-  gtyped = mIso . gtyped+  gtyped = mLens . gtyped  class GProductHasType l r a (contains :: Bool) where   gproductTyped :: Lens' ((l :*: r) x) a
src/Data/Generics/Product/Positions.hs view
@@ -93,7 +93,7 @@   , GHasPosition 1 i (Rep s) a   ) => HasPosition i a s where -  position = ravel (repIso . gposition @1 @i)+  position = ravel (repLens . gposition @1 @i)  type family ErrorUnless (i :: Nat) (s :: Type) (hasP :: Bool) :: Constraint where   ErrorUnless i s 'False
src/Data/Generics/Product/Typed.hs view
@@ -83,7 +83,7 @@   , GHasType (Rep s) a   ) => HasType a s where -  typed = ravel (repIso . gtyped)+  typed = ravel (repLens . gtyped)  type family ErrorUnlessOne (a :: Type) (s :: Type) (count :: Count) :: Constraint where   ErrorUnlessOne a s 'None
src/Data/Generics/Sum/Typed.hs view
@@ -81,13 +81,24 @@   --  Just 2   _Typed :: Prism' s a +  -- |Inject by type.+  injectTyped :: a -> s++  -- |Project by type.+  projectTyped :: s -> Maybe a+ instance   ( Generic s   , ErrorUnlessOne a s (CountPartialType a (Rep s))   , GAsType (Rep s) a   ) => AsType a s where -  _Typed = repIso . _GTyped+  _Typed+    = repIso . _GTyped+  injectTyped+    = to . ginjectTyped+  projectTyped+    = either (const Nothing) Just . gprojectTyped . from  type family ErrorUnlessOne (a :: Type) (s :: Type) (count :: Count) :: Constraint where   ErrorUnlessOne a s 'None
test/Spec.hs view
@@ -1,4 +1,4 @@-{-# OPTIONS_GHC -O -fplugin GHC.Proof.Plugin #-}+{-# OPTIONS_GHC -O -fplugin Test.Inspection.Plugin #-}  {-# LANGUAGE AllowAmbiguousTypes             #-} {-# LANGUAGE DataKinds                       #-}@@ -8,12 +8,13 @@ {-# LANGUAGE RankNTypes                      #-} {-# LANGUAGE ScopedTypeVariables             #-} {-# LANGUAGE TypeApplications                #-}+{-# LANGUAGE TemplateHaskell                 #-}  module Main where  import GHC.Generics import Data.Generics.Product-import GHC.Proof+import Test.Inspection  main :: IO () main = putStrLn "Hello world"@@ -42,19 +43,24 @@  -------------------------------------------------------------------------------- -- * Tests--- The ghc-proofs plugin checks that the following equalities hold, by checking--- that the LHSs and the RHSs are CSEd. This also means that the runtime--- characteristics of the derived lenses is the same as the manually written--- ones above.+-- The inspection-testing plugin checks that the following equalities hold, by+-- checking that the LHSs and the RHSs are CSEd. This also means that the+-- runtime characteristics of the derived lenses is the same as the manually+-- written ones above. -fieldP :: Proof-fieldP = L fieldALensManual === L (field @"fieldA")+fieldALensName :: Lens' Record Int+fieldALensName = field @"fieldA" -typedP :: Proof-typedP = L fieldALensManual === L (typed @Int)+fieldALensType :: Lens' Record Int+fieldALensType = typed @Int -posP :: Proof-posP = L fieldALensManual === L (position @1)+fieldALensPos :: Lens' Record Int+fieldALensPos = position @1 -subtypeP :: Proof-subtypeP = L subtypeLensManual === L super+subtypeLensGeneric :: Lens' Record Record2+subtypeLensGeneric = super++inspect $ 'fieldALensManual === 'fieldALensName+inspect $ 'fieldALensManual === 'fieldALensType+inspect $ 'fieldALensManual === 'fieldALensPos+inspect $ 'subtypeLensManual === 'subtypeLensGeneric