lens-labels 0.1.0.2 → 0.3.0.1
raw patch · 5 files changed
Files
- Changelog.md +21/−1
- lens-labels.cabal +41/−20
- src/Lens/Labels.hs +16/−20
- src/Lens/Labels/Prism.hs +80/−0
- src/Lens/Labels/Unwrapped.hs +30/−0
Changelog.md view
@@ -1,6 +1,26 @@ # Changelog for `lens-labels` -## Unreleased changes+## v0.3.0.1+- Bump the dependency on `base` to support `ghc-8.6.1`.++## v0.3.0.0+- Simplify instances to make them more readable. (#208)++## v0.2.0.2+- Bump the lower bound on `base` to indicate we require `ghc>=8.0`.++## v0.2.0.1+- Bump the dependency on `base` to support `ghc-8.4.2`.++## 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,43 @@-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.28.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: e53f8c044632779500255ae27a9bb17817d13a8d78b69828bd0f0a8af605ead0 +name: lens-labels+version: 0.3.0.1+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+ exposed-modules:+ Lens.Labels+ Lens.Labels.Unwrapped+ Lens.Labels.Prism+ other-modules:+ Paths_lens_labels+ hs-source-dirs:+ src+ build-depends:+ base >=4.9 && <4.13+ , ghc-prim >=0.4 && <0.6+ , profunctors >=5.2+ , tagged >=0.8+ default-language: Haskell2010
src/Lens/Labels.hs view
@@ -33,10 +33,11 @@ (&), (Category..), Lens,+ Lens', -- * HasLens- HasLens(..), Proxy#, proxy#,+ HasLens'(..), -- * Setters ASetter, (.~),@@ -50,47 +51,40 @@ 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---- | A type class for lens fields.-class HasLens (x :: Symbol) f s t 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+type Lens' s a = Lens s s a a -#if __GLASGOW_HASKELL__ >= 800 instance- (p ~ (a -> f b), q ~ (s -> f t), HasLens x f s t a b)+ (Functor f, p ~ (a -> f b), q ~ (s -> f t), s ~ t, a ~ b, HasLens' s x a) => IsLabel x (LensFn p q) where #if __GLASGOW_HASKELL__ >= 802- fromLabel = LensFn $ lensOf (proxy# :: Proxy# x)+ fromLabel = LensFn $ lensOf' (proxy# :: Proxy# x) #else- fromLabel p = LensFn $ lensOf p-#endif+ fromLabel p = LensFn $ lensOf' p #endif +-- | A type class for lens fields.+class HasLens' s (x :: Symbol) a | s x -> a where+ lensOf' :: Functor f => 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 +101,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