summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlouispan <>2018-04-23 09:48:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2018-04-23 09:48:00 (GMT)
commit1b4486518efacb65d8649f776e25fb91197b3467 (patch)
tree18e759dd3fb8278749e5d863297fc31236f1f0fa
parentc675e4b7adf802ae21e16109cb210ce9a579105c (diff)
version 3.1.0.03.1.0.0
-rw-r--r--README.md3
-rw-r--r--data-diverse-lens.cabal3
-rw-r--r--src/Data/Diverse/Lens/Many.hs64
-rw-r--r--src/Data/Diverse/Profunctor/Many.hs2
4 files changed, 38 insertions, 34 deletions
diff --git a/README.md b/README.md
index 4dd85f9..d0f2cc3 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,9 @@ Refer to [ManySpec.hs](https://github.com/louispan/data-diverse-lens/blob/master
# Changelog
+* 3.1.0.0
+ - Replaced `HasItem` with `Data.Has`.
+
* 3.0.0.2
- Fixed haddock.
diff --git a/data-diverse-lens.cabal b/data-diverse-lens.cabal
index 6d8841c..4292938 100644
--- a/data-diverse-lens.cabal
+++ b/data-diverse-lens.cabal
@@ -1,5 +1,5 @@
name: data-diverse-lens
-version: 3.0.0.2
+version: 3.1.0.0
synopsis: Isos & Lens for Data.Diverse.Many and Prisms for Data.Diverse.Which
description: Isos & Lens for Data.Diverse.Many and Prisms for Data.Diverse.Which
Refer to [ManySpec.hs](https://github.com/louispan/data-diverse-lens/blob/master/test/Data/Diverse/Lens/ManySpec.hs) and [WhichSpec.hs](https://github.com/louispan/data-diverse-lens/blob/master/test/Data/Diverse/Lens/WhichSpec.hs) for example usages.
@@ -29,6 +29,7 @@ library
, profunctors >= 5.2
, generic-lens >= 0.5.0.0
, lens >= 4
+ , data-has >= 0.3
ghc-options: -Wall
default-language: Haskell2010
diff --git a/src/Data/Diverse/Lens/Many.hs b/src/Data/Diverse/Lens/Many.hs
index a6c6b27..1846866 100644
--- a/src/Data/Diverse/Lens/Many.hs
+++ b/src/Data/Diverse/Lens/Many.hs
@@ -1,3 +1,5 @@
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
@@ -21,15 +23,16 @@ module Data.Diverse.Lens.Many (
-- * Single field
-- ** Lens for a single field
- , HasItem(..)
- , HasItem'(..)
- , HasItemL(..)
- , HasItemL'(..)
- , HasItemTag(..)
- , HasItemTag'(..)
+ , Has(..)
+ , item'
+ , Had(..)
+ , HasL(..)
+ , HadL(..)
+ , HasTag(..)
+ , HadTag(..)
-- , genericItemTag
- , HasItemN(..)
- , HasItemN'(..)
+ , HasN(..)
+ , HadN(..)
-- * Multiple fields
-- ** Lens for multiple fields
@@ -51,6 +54,7 @@ import Control.Lens
import Data.Diverse.Many
import Data.Diverse.TypeLevel
import Data.Generics.Product
+import Data.Has
import Data.Kind
import Data.Tagged
import GHC.TypeLits
@@ -65,29 +69,25 @@ _Many' = iso fromMany' toMany'
-----------------------------------------------------------------------
--- | 'grab' ('view' 'item') and 'replace'' ('set' 'item'') in 'Lens'' form.
+-- | Convient name for 'hasLens' to be consistent with 'Had' typeclass.
--
-- @
-- let x = (5 :: Int) './' False './' \'X' './' Just \'O' './' 'nil'
-- x '^.' 'item'' \@Int \`shouldBe` 5
-- (x '&' 'item'' \@Int .~ 6) \`shouldBe` (6 :: Int) './' False './' \'X' './' Just \'O' './' 'nil'
-- @
-class HasItem' a s where
- item' :: Lens' s a
-
- -- | Make it easy to create an instance of 'item' using 'Data.Generics.Product.Typed'
- default item' :: (HasType a s) => Lens' s a
- item' = typed
+item' :: Has a s => Lens' s a
+item' = hasLens
-instance UniqueMember x xs => HasItem' x (Many xs) where
- item' = lens grab replace'
+instance UniqueMember x xs => Has x (Many xs) where
+ hasLens = lens grab replace'
-- | Polymorphic version of 'item''
-class (HasItem' a s, Replaced a a s ~ s) => HasItem a s where
+class (Has a s, Replaced a a s ~ s) => Had a s where
type Replaced a b s
item :: Lens s (Replaced a b s) a b
-instance (UniqueMember x xs) => HasItem x (Many xs) where
+instance (UniqueMember x xs) => Had x (Many xs) where
type Replaced x b (Many xs) = Many (Replace x b xs)
item = lens grab (replace @x)
@@ -101,16 +101,16 @@ instance (UniqueMember x xs) => HasItem x (Many xs) where
--
-- A default implementation using generics is not provided as it make GHC think that @l@ must be type @Symbol@
-- when @l@ can actually be any kind.
--- Create instances of 'HasItemL'' using "Data.Generics.Product.Fields" as follows:
+-- Create instances of 'HasL' using "Data.Generics.Product.Fields" as follows:
-- @
-- instance HasField' l Foo a => itemL' l a Foo where
-- itemL' = field @l
-- default itemL' :: forall (l :: Symbol) a s. (HasField' l s a) => Lens' s a
-- itemL' = field @l
-class HasItemL' (l :: k) a s | s l -> a where
+class HasL (l :: k) a s | s l -> a where
itemL' :: Lens' s a
-instance (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => HasItemL' l x (Many xs) where
+instance (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => HasL l x (Many xs) where
itemL' = lens (grabL @l) (replaceL' @l)
-- | Polymorphic version of 'itemL''
@@ -119,28 +119,28 @@ instance (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => HasItemL' l x (Many x
-- let x = (5 :: Int) './' Tagged @Foo False './' Tagged \@Bar \'X' './' 'nil'
-- (x '&' 'itemL' \@Foo '.~' \"foo") \`shouldBe` (5 :: Int) './' \"foo" './' Tagged \@Bar \'X' './' 'nil'
-- @
-class (HasItemL' (l :: k) a s, ReplacedL l a a s ~ s) => HasItemL (l :: k) a s | s l -> a where
+class (HasL (l :: k) a s, ReplacedL l a a s ~ s) => HadL (l :: k) a s | s l -> a where
type ReplacedL l a b s
itemL :: Lens s (ReplacedL l a b s) a b
-instance (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => HasItemL l x (Many xs) where
+instance (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => HadL l x (Many xs) where
type ReplacedL l x b (Many xs) = Many (Replace (KindAtLabel l xs) b xs)
itemL = lens (grabL @l) (replaceL @l)
-- | Variation of 'itemL'' that automatically tags and untags a Tagged field.
-- @
-class HasItemL' l (Tagged l a) s => HasItemTag' (l :: k) a s where
+class HasL l (Tagged l a) s => HasTag (l :: k) a s where
itemTag' :: Lens' s a
-instance HasItemL' l (Tagged l a) s => HasItemTag' (l :: k) a s where
+instance HasL l (Tagged l a) s => HasTag (l :: k) a s where
itemTag' = itemL' @l . iso unTagged Tagged
-- | Polymorphic version of 'itemTag''
-- @
-class HasItemL l (Tagged l a) s => HasItemTag (l :: k) a s where
+class HadL l (Tagged l a) s => HadTag (l :: k) a s where
itemTag :: Lens s (ReplacedL l (Tagged l a) (Tagged l b) s) a b
-instance HasItemL l (Tagged l a) s => HasItemTag (l :: k) a s where
+instance HadL l (Tagged l a) s => HadTag (l :: k) a s where
itemTag = itemL @l . iso unTagged (Tagged @l)
-- | 'grabN' ('view' 'item') and 'replaceN'' ('set' 'item'') in 'Lens'' form.
@@ -150,14 +150,14 @@ instance HasItemL l (Tagged l a) s => HasItemTag (l :: k) a s where
-- x '^.' 'itemN'' \@0 \`shouldBe` 5
-- (x '&' 'itemN'' \@0 '.~' 6) \`shouldBe` (6 :: Int) './' False './' \'X' './' Just \'O' './' (6 :: Int) './' Just \'A' './' 'nil'
-- @
-class HasItemN' (n :: Nat) a s | s n -> a where
+class HasN (n :: Nat) a s | s n -> a where
itemN' :: Lens' s a
-instance (MemberAt n x xs) => HasItemN' n x (Many xs) where
+instance (MemberAt n x xs) => HasN n x (Many xs) where
itemN' = lens (grabN @n) (replaceN' @n)
-- | Polymorphic version of 'itemN''
-class (HasItemN' (n :: Nat) a s, ReplacedN n a a s ~ s) => HasItemN (n :: Nat) a s | s n -> a where
+class (HasN (n :: Nat) a s, ReplacedN n a a s ~ s) => HadN (n :: Nat) a s | s n -> a where
type ReplacedN n a b s
itemN :: Lens s (ReplacedN n a b s) a b
@@ -166,7 +166,7 @@ class (HasItemN' (n :: Nat) a s, ReplacedN n a a s ~ s) => HasItemN (n :: Nat) a
itemN = position @n
instance (MemberAt n x xs)
- => HasItemN n x (Many xs) where
+ => HadN n x (Many xs) where
type ReplacedN n x b (Many xs) = Many (ReplaceIndex n x b xs)
itemN = lens (grabN @n) (replaceN @n)
diff --git a/src/Data/Diverse/Profunctor/Many.hs b/src/Data/Diverse/Profunctor/Many.hs
index 2f769f5..29b19e9 100644
--- a/src/Data/Diverse/Profunctor/Many.hs
+++ b/src/Data/Diverse/Profunctor/Many.hs
@@ -34,7 +34,7 @@ import Data.Profunctor
-- | A friendlier constraint synonym for 'itemized'.
type Itemized a b s t =
- ( HasItem a s
+ ( Had a s
, t ~ Replaced a b s
)