lens-labels 0.1.0.2 → 0.2.0.0
raw patch · 5 files changed
+189/−34 lines, 5 filesdep +profunctorsdep +taggeddep ~basePVP ok
version bump matches the API change (PVP)
Dependencies added: profunctors, tagged
Dependency ranges changed: base
API changes (from Hackage documentation)
- Lens.Labels: data Proxy# :: forall k. k -> TYPE VoidRep
- Lens.Labels: instance (p ~ (a -> f b), q ~ (s -> f t), Lens.Labels.HasLens x f s t a b) => GHC.OverloadedLabels.IsLabel x (Lens.Labels.LensFn p q)
- Lens.Labels: newtype Const k a (b :: k) :: forall k. * -> k -> *
+ Lens.Labels: class HasLens f s s x a a => HasLens' f s x a | x s -> a
+ Lens.Labels: data Proxy# :: forall k0. () => k0 -> TYPE TupleRep [] RuntimeRep
+ Lens.Labels: infixr 9 .
+ Lens.Labels: instance (p ~ (a -> f b), q ~ (s -> f t), Lens.Labels.HasLens f s t x a b) => GHC.OverloadedLabels.IsLabel x (Lens.Labels.LensFn p q)
+ Lens.Labels: lensOf' :: HasLens' f s x a => Proxy# x -> (a -> f a) -> s -> f s
+ Lens.Labels: newtype Const k a (b :: k) :: forall k. () => * -> k -> *
+ Lens.Labels: type Lens' s a = Lens s s a a
+ Lens.Labels.Prism: (#) :: AReview t b -> b -> t
+ Lens.Labels.Prism: _Just :: Prism (Maybe a) (Maybe b) a b
+ Lens.Labels.Prism: _Left :: Prism (Either a c) (Either b c) a b
+ Lens.Labels.Prism: _Nothing :: Prism' (Maybe a) ()
+ Lens.Labels.Prism: _Right :: Prism (Either c a) (Either c b) a b
+ Lens.Labels.Prism: infixr 8 #
+ Lens.Labels.Prism: prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
+ Lens.Labels.Prism: prism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b
+ Lens.Labels.Prism: type AReview t b = Optic' Tagged Identity t b
+ Lens.Labels.Prism: type Prism' s a = Prism s s a a
+ Lens.Labels.Prism: type Prism s t a b = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)
+ Lens.Labels.Unwrapped: instance GHC.OverloadedLabels.IsLabel x (Lens.Labels.LensFn p q) => GHC.OverloadedLabels.IsLabel x (p -> q)
- Lens.Labels: (&) :: a -> (a -> b) -> b
+ Lens.Labels: (&) :: () => a -> (a -> b) -> b
- Lens.Labels: (.) :: Category k cat => forall (b :: k) (c :: k) (a :: k). cat b c -> cat a b -> cat a c
+ Lens.Labels: (.) :: Category k cat => forall (b :: k) (c :: k) (a :: k). () => cat b c -> cat a b -> cat a c
- Lens.Labels: class HasLens (x :: Symbol) f s t a b | x s -> a, x t -> b, x s b -> t, x t a -> s
+ Lens.Labels: class HasLens f s t (x :: Symbol) a b | x s -> a, x t -> b, x s b -> t, x t a -> s
- Lens.Labels: lensOf :: HasLens x f s t a b => Proxy# x -> (a -> f b) -> s -> f t
+ Lens.Labels: lensOf :: HasLens f s t x a b => Proxy# x -> (a -> f b) -> s -> f t
- Lens.Labels: proxy# :: Proxy# k a
+ Lens.Labels: proxy# :: () => Proxy# k0 a
- Lens.Labels: view :: s -> Getting a s t a b -> a
+ Lens.Labels: view :: Getting a s t a b -> s -> a
Files
- Changelog.md +9/−1
- lens-labels.cabal +42/−20
- src/Lens/Labels.hs +28/−13
- src/Lens/Labels/Prism.hs +80/−0
- src/Lens/Labels/Unwrapped.hs +30/−0
Changelog.md view
@@ -1,6 +1,14 @@ # Changelog for `lens-labels` -## Unreleased changes+## v0.2.0.0+- Improve readability of `HasLens` instances. (#118)+- Remove support for `ghc-7.10`. (#136)+- Use a `.cabal` file that's auto-generated from `hpack`. (#138)+- Export a `Lens'` type synonym. (#141)+- Swap the order of parameters for `view` to match other packages. (#141)+- Add `Lens.Labels.Unwrapped`, allowing easier interoperation with+ other lens libraries. (#157)+- Add `Lens.Labels.Prism`. (#160) ## v0.1.0.2 - Bump the dependency on `base` to support `ghc-8.2.1`.
lens-labels.cabal view
@@ -1,22 +1,44 @@-name: lens-labels-version: 0.1.0.2-synopsis: Integration of lenses with OverloadedLabels.-description: Provides a framework to integrate lenses with GHC's- OverloadedLabels extension.-homepage: https://github.com/google/proto-lens-license: BSD3-license-file: LICENSE-author: Judah Jacobson-maintainer: proto-lens@googlegroups.com-copyright: Google Inc.-category: Data-build-type: Simple-cabal-version: >=1.10-extra-source-files: Changelog.md+-- This file has been generated from package.yaml by hpack version 0.20.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: df0afffbdb7c438238f17625c906f5225edd023a14ebf41e3526bbfb5769c4d4 +name: lens-labels+version: 0.2.0.0+synopsis: Integration of lenses with OverloadedLabels.+description: Provides a framework to integrate lenses with GHC's OverloadedLabels extension.+category: Data+homepage: https://github.com/google/proto-lens#readme+bug-reports: https://github.com/google/proto-lens/issues+author: Judah Jacobson+maintainer: proto-lens@googlegroups.com+copyright: Google Inc.+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ Changelog.md++source-repository head+ type: git+ location: https://github.com/google/proto-lens+ subdir: lens-labels+ library- exposed-modules: Lens.Labels- build-depends: base >= 4.8 && < 4.11- , ghc-prim >=0.4 && <0.6- hs-source-dirs: src- default-language: Haskell2010+ hs-source-dirs:+ src+ build-depends:+ base >=4.8 && <4.11+ , ghc-prim >=0.4 && <0.6+ , profunctors >=5.2+ , tagged >=0.8+ exposed-modules:+ Lens.Labels+ Lens.Labels.Unwrapped+ Lens.Labels.Prism+ other-modules:+ Paths_lens_labels+ default-language: Haskell2010
src/Lens/Labels.hs view
@@ -33,10 +33,12 @@ (&), (Category..), Lens,+ Lens', -- * HasLens HasLens(..), Proxy#, proxy#,+ HasLens'(..), -- * Setters ASetter, (.~),@@ -50,47 +52,58 @@ view, ) where + import qualified Control.Category as Category import GHC.Prim (Proxy#, proxy#)-#if __GLASGOW_HASKELL__ >= 800 import GHC.OverloadedLabels (IsLabel(..))-#endif import GHC.TypeLits (Symbol) import Data.Function ((&))-#if __GLASGOW_HASKELL__ >= 800 import Data.Functor.Const (Const(..))-#else-import Control.Applicative (Const(..))-#endif import Data.Functor.Identity(Identity(..)) -- | A newtype for defining lenses. Can be composed using--- `(Control.Category..)` (also exported from this module).+-- '(Control.Category..)', which is exported from this module. newtype LensFn a b = LensFn {runLens :: a -> b} deriving Category.Category type LensLike f s t a b = LensFn (a -> f b) (s -> f t) type LensLike' f s a = LensLike f s s a a type Lens s t a b = forall f . Functor f => LensLike f s t a b+type Lens' s a = Lens s s a a -- | A type class for lens fields.-class HasLens (x :: Symbol) f s t a b+class HasLens f s t (x :: Symbol) a b | x s -> a, x t -> b, x s b -> t, x t a -> s where lensOf :: Proxy# x -> (a -> f b) -> s -> f t -#if __GLASGOW_HASKELL__ >= 800 instance- (p ~ (a -> f b), q ~ (s -> f t), HasLens x f s t a b)+ (p ~ (a -> f b), q ~ (s -> f t), HasLens f s t x a b) => IsLabel x (LensFn p q) where #if __GLASGOW_HASKELL__ >= 802 fromLabel = LensFn $ lensOf (proxy# :: Proxy# x) #else fromLabel p = LensFn $ lensOf p #endif-#endif +-- | A type class for lens fields of monomorphic types (i.e., where the lens+-- doesn't change the outer type).+--+-- This class can be used to simplify instance declarations and type+-- errors, by "forwarding" 'HasLens' to simpler instances. For example:+--+-- @+-- instance (HasLens' f Foo x a, a ~ b) => HasLens f Foo Foo x a b where+-- where lensOf = lensOf'+-- instance Functor f => HasLens' f Foo "a" Int where ...+-- instance Functor f => HasLens' f Foo "b" Double where ...+-- instance Functor f => HasLens' f Foo "c" [Float] where ...+-- ...+-- @+class HasLens f s s x a a => HasLens' f s x a | x s -> a where+ lensOf' :: Proxy# x -> (a -> f a) -> s -> f s+ type ASetter s t a b = LensLike Identity s t a b (.~), set :: ASetter s t a b -> b -> s -> t@@ -107,8 +120,10 @@ type Getting r s t a b = LensLike (Const r) s t a b -(^.), view :: s -> Getting a s t a b -> a+(^.) :: s -> Getting a s t a b -> a s ^. f = getConst $ runLens f Const s-view = (^.)++view :: Getting a s t a b -> s -> a+view = flip (^.) infixl 8 ^.
+ src/Lens/Labels/Prism.hs view
@@ -0,0 +1,80 @@+-- Copyright 2016 Google Inc. All Rights Reserved.+--+-- Use of this source code is governed by a BSD-style+-- license that can be found in the LICENSE file or at+-- https://developers.google.com/open-source/licenses/bsd++-- | This module defines the 'Prism' type and combinators. Used for building+-- 'Prism's for oneof fields.+{-# LANGUAGE RankNTypes #-}++module Lens.Labels.Prism+ ( Prism+ , Prism'+ , AReview+ , (#)+ , prism+ , prism'+ , _Left+ , _Right+ , _Just+ , _Nothing+ ) where++import Data.Tagged (Tagged (..))+import Data.Functor.Identity (Identity (..))+import Data.Profunctor (dimap)+import Data.Profunctor.Choice+import Data.Profunctor.Unsafe ((#.), (.#))+++------------------------------------------------------------------------------+-- Prism Internals+------------------------------------------------------------------------------+type Prism s t a b = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)++type Prism' s a = Prism s s a a++type Optic p f s t a b = p a (f b) -> p s (f t)++type Optic' p f s a = Optic p f s s a a++type AReview t b = Optic' Tagged Identity t b++-- | Used for constructing 'Prism' values.+--+-- @'_Just' '#' 5 == 'Just' 5@+( # ) :: AReview t b -> b -> t+( # ) p = runIdentity #. unTagged #. p .# Tagged .# Identity+infixr 8 #+++------------------------------------------------------------------------------+-- Prism Combinators+------------------------------------------------------------------------------++-- | Build a 'Control.Lens.Prism.Prism'.+--+-- @'Either' t a@ is used instead of @'Maybe' a@ to permit the types of @s@ and @t@ to differ.+--+prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b+prism bt seta = dimap seta (either pure (fmap bt)) . right'+{-# INLINE prism #-}++-- | This is usually used to build a 'Prism'', when you have to use an operation like+-- 'Data.Typeable.cast' which already returns a 'Maybe'.+prism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b+prism' bs sma = prism bs (\s -> maybe (Left s) Right (sma s))+{-# INLINE prism' #-}++_Left :: Prism (Either a c) (Either b c) a b+_Left = prism Left $ either Right (Left . Right)++_Right :: Prism (Either c a) (Either c b) a b+_Right = prism Right $ either (Left . Left) Right++_Just :: Prism (Maybe a) (Maybe b) a b+_Just = prism Just $ maybe (Left Nothing) Right++_Nothing :: Prism' (Maybe a) ()+_Nothing = prism' (const Nothing) $ maybe (Just ()) (const Nothing)
+ src/Lens/Labels/Unwrapped.hs view
@@ -0,0 +1,30 @@+{-# LANGUAGE CPP #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE UndecidableInstances #-}+#if __GLASGOW_HASKELL__ >= 802+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}+{-# OPTIONS_GHC -fno-warn-simplifiable-class-constraints #-}+#endif+{-# OPTIONS_GHC -fno-warn-orphans #-}++------------------------------------------------------------------------------+-- | This module provides OverloadedLabels 'IsLabel' support via an+-- orphan instance. This means a @Lens.Family.Lens@ can be referenced+-- as @#foo@ whenever we have an instance of @Lens.Labels.HasLens@+-- with the label @"foo"@."+--+-- This can eliminate the need to call 'runLens' when working with libraries+-- like @lens@, @microlens@, or @lens-family@.+module Lens.Labels.Unwrapped where++import GHC.OverloadedLabels (IsLabel (..))+import Lens.Labels (LensFn (..))++instance IsLabel x (LensFn p q) => IsLabel x (p -> q) where+#if __GLASGOW_HASKELL__ >= 802+ fromLabel = runLens $ fromLabel @x+#else+ fromLabel x = runLens $ fromLabel x+#endif