packages feed

data-as (empty) → 0.0.0.1

raw patch · 5 files changed

+181/−0 lines, 5 filesdep +basedep +profunctorssetup-changed

Dependencies added: base, profunctors

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for data-as++## 0.0.0.1++* initial release
+ Data/As.hs view
@@ -0,0 +1,126 @@+{-# LANGUAGE ExplicitForAll #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE RankNTypes #-}++{-|+Module: Data.As+Description: Simple extensible sum+Copyright: (c) incertia, 2020+License: MIT+Maintainer: incertia@incertia.net+Stability: experimental+Portability: portable++This module provides the 'As' class which is a multi parameter classy prism,+much like how 'Has' is a multi parameter classy lens.++We have the following primary use case for 'As'.++@+ -- some library code+ throwE :: (As e err, MonadError err m) => e -> m ()+ throwE = throwError . review asPrism+@+-}++module Data.As+  ( As(..)+  ) where++import Data.Functor.Const+  (Const(..))+import Data.Functor.Identity+  (Identity(..))+import Data.Monoid+  (First(..))+import Data.Profunctor+  (Profunctor, Choice(..), dimap, right')+import Data.Profunctor.Unsafe+  ((.#), (#.))+import Data.Void+  (Void, absurd)+import Text.Read+  (readMaybe)++-- inline our own Tagged+newtype Tagged s b = Tagged { unTagged :: b }++instance Profunctor Tagged where+  dimap _ f (Tagged s) = Tagged (f s)++instance Choice Tagged where+  left' (Tagged b) = Tagged (Left b)+  right' (Tagged b) = Tagged (Right b)++-- inline local Prism type so we don't depend on lens+-- unfortunately this means we need to depend on profunctor+type Prism t a = forall p f. (Choice p, Applicative f) => p a (f a) -> p t (f t)++-- these definitions are ripped directly from lens+-- | A typeclass for extensible sums.+--+-- The provided instances were inspired from the lens library.+--+-- Making your own instances when you actually depend on lens should be as easy+-- as @instance As Foo Bar where asPrism = _Foo@.+class As a t where+  {-# MINIMAL previewer, reviewer | asPrism #-}+  previewer :: t -> Maybe a+  previewer = getFirst #. getConst #. asPrism (Const #. First #. Just)++  reviewer :: a -> t+  reviewer = runIdentity #. unTagged #. asPrism .# Tagged .# Identity++  asPrism :: Prism t a+  asPrism = dimap (\a -> maybe (Left a) Right (previewer a)) (either pure (fmap reviewer)) . right'++  modifier :: (a -> a) -> t -> t+  modifier f = runIdentity . asPrism (Identity . f)++-- some instances based on the lens library+instance As a a where+  asPrism = id+  {-# INLINABLE asPrism #-}++instance As a (Maybe a) where+  previewer = id+  {-# INLINABLE previewer #-}+  reviewer = Just+  {-# INLINABLE reviewer #-}++instance As () (Maybe a) where+  previewer ma = case ma of+                   Nothing -> Just ()+                   Just _  -> Nothing+  {-# INLINABLE previewer #-}+  reviewer () = Nothing+  {-# INLINABLE reviewer #-}++instance As a (Either a b) where+  previewer eab = case eab of+                    Left a  -> Just a+                    Right _ -> Nothing+  {-# INLINABLE previewer #-}+  reviewer = Left+  {-# INLINABLE reviewer #-}++instance As b (Either a b) where+  previewer eab = case eab of+                    Right b -> Just b+                    Left _  -> Nothing+  {-# INLINABLE previewer #-}+  reviewer = Right+  {-# INLINABLE reviewer #-}++instance (Read a, Show a) => As a String where+  previewer = readMaybe+  {-# INLINABLE previewer #-}+  reviewer = show+  {-# INLINABLE reviewer #-}++instance As Void a where+  previewer _ = Nothing+  {-# INLINABLE previewer #-}+  reviewer = absurd+  {-# INLINABLE reviewer #-}
+ LICENSE view
@@ -0,0 +1,19 @@+Copyright (c) 2020 incertia++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ data-as.cabal view
@@ -0,0 +1,29 @@+cabal-version:       >=1.10+-- Initial package description 'data-as.cabal' generated by 'cabal init'.+-- For further documentation, see http://haskell.org/cabal/users-guide/++name:                data-as+version:             0.0.0.1+synopsis:            Simple extensible sum+description:         Simple extensible sum+-- bug-reports:+license:             MIT+license-file:        LICENSE+author:              Will Song+maintainer:          incertia@incertia.net+-- copyright:+category:            Data+build-type:          Simple+extra-source-files:  CHANGELOG.md+homepage:            https://github.com/incertia/data-as++source-repository head+  type:     git+  location: git://github.com/incertia/data-as.git++library+  build-depends:       base >=4.14 && <4.15+                     , profunctors+  exposed-modules:     Data.As+  hs-source-dirs:      .+  default-language:    Haskell2010