packages feed

lens-labels (empty) → 0.1.0.0

raw patch · 4 files changed

+160/−0 lines, 4 filesdep +basedep +ghc-primsetup-changed

Dependencies added: base, ghc-prim

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2016, Google Inc.++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 Google Inc. 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ lens-labels.cabal view
@@ -0,0 +1,21 @@+name:                lens-labels+version:             0.1.0.0+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++library+  exposed-modules:     Lens.Labels+  build-depends:       base >= 4.8 && < 4.10+                     , ghc-prim >=0.4 && <0.6+  hs-source-dirs:      src+  default-language:    Haskell2010
+ src/Lens/Labels.hs view
@@ -0,0 +1,107 @@+{- | A lens library that integrates with OverloadedLabels.++Unlike the `lens` package (and others), lenses are defined as a newtype+instead of a type synonym, to avoid overlapping with other IsLabel+instances.  However, the `LensFn` and `runLens` functions allow converting+between the two types; for example:++> LensFn :: Control.Lens.LensLike f s t a b -> Lens.Labels.LensLike f s t a b+> runLens :: Lens.Labels.LensLike f s t a b -> Control.Lens.LensLike f s t a b++TODO: support more general optic types (e.g., prisms).+-}+{-# LANGUAGE CPP #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE ExplicitNamespaces #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE KindSignatures #-}+{-# LANGUAGE MagicHash #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE UndecidableInstances #-}+module Lens.Labels (+    -- * Lenses+    LensFn(..),+    LensLike,+    LensLike',+    (&),+    (Category..),+    Lens,+    -- * HasLens+    HasLens(..),+    Proxy#(..),+    proxy#,+    -- * Setters+    ASetter,+    (.~),+    (%~),+    set,+    over,+    -- * Getters+    Const(..),+    Getting,+    (^.),+    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).+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++#if __GLASGOW_HASKELL__ >= 800+instance+    (p ~ (a -> f b), q ~ (s -> f t), HasLens x f s t a b)+    => IsLabel x (LensFn p q) where+    fromLabel p = LensFn $ lensOf p+#endif++type ASetter s t a b = LensLike Identity s t a b++(.~), set :: ASetter s t a b -> b -> s -> t+f .~ x = f %~ const x+set = (.~)++infixr 4 .~++(%~), over :: ASetter s t a b -> (a -> b) -> s -> t+f %~ g = \s -> runIdentity $ runLens f (Identity . g) s+over = (%~)++infixr 4 %~++type Getting r s t a b = LensLike (Const r) s t a b++(^.), view :: s -> Getting a s t a b -> a+s ^. f = getConst $ runLens f Const s+view = (^.)++infixl 8 ^.