Skip to content

Commit e6d2834

Browse files
committed
Top monoid and misc fixes
1 parent 7605336 commit e6d2834

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

src/Control/Optics/Linear/Internal.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ module Control.Optics.Linear.Internal
2424
, _Just, _Nothing
2525
, ptraversed, dtraversed
2626
, both, both'
27-
, get', gets', set'
2827
-- * Using optics
2928
, get, set, gets
29+
, get', gets', set', set''
3030
, match, match', build
3131
, preview
3232
, over, over'
@@ -42,15 +42,13 @@ module Control.Optics.Linear.Internal
4242
import qualified Control.Arrow as NonLinear
4343
import qualified Data.Bifunctor.Linear as Bifunctor
4444
import Data.Bifunctor.Linear (SymmetricMonoidal)
45-
import Data.Monoid
45+
import Data.Monoid (First(..), Sum(..))
4646
import Data.Functor.Const
4747
import Data.Functor.Linear
4848
import Data.Profunctor.Linear
49-
import Data.Functor.Linear
5049
import qualified Data.Profunctor.Kleisli.Linear as Linear
5150
import Data.Void
52-
import Prelude.Linear hiding ((<$>))
53-
-- ^ XXX: not entirely sure why the hiding is necessary here...
51+
import Prelude.Linear
5452
import qualified Prelude as P
5553

5654
-- TODO: documentation in this module
@@ -161,8 +159,8 @@ get l = gets l P.id
161159
gets :: Optic_ (NonLinear.Kleisli (Const r)) a b s t -> (a -> r) -> s -> r
162160
gets (Optical l) f s = getConst' (NonLinear.runKleisli (l (NonLinear.Kleisli (Const P.. f))) s)
163161

164-
preview :: Optic_ (NonLinear.Kleisli (Const (Maybe (First a)))) a b s t -> s -> Maybe a
165-
preview (Optical l) s = getFirst P.<$> (getConst (NonLinear.runKleisli (l (NonLinear.Kleisli (\a -> Const (Just (First a))))) s))
162+
preview :: Optic_ (NonLinear.Kleisli (Const (First a))) a b s t -> s -> Maybe a
163+
preview (Optical l) s = getFirst (getConst (NonLinear.runKleisli (l (NonLinear.Kleisli (\a -> Const (First (Just a))))) s))
166164

167165
get' :: Optic_ (Linear.Kleisli (Const (Top, a))) a b s t -> s ->. (Top, a)
168166
get' l = gets' l id

src/Data/Monoid/Linear.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
44
{-# LANGUAGE LinearTypes #-}
55
{-# LANGUAGE NoImplicitPrelude #-}
6+
{-# LANGUAGE ExistentialQuantification #-}
67
{-# LANGUAGE StandaloneDeriving #-}
78

89
-- | = The linear monoid hierarchy
@@ -13,6 +14,7 @@ module Data.Monoid.Linear
1314
( Semigroup(..)
1415
, Monoid(..)
1516
, Endo(..), appEndo
17+
, Top, throw
1618
, module Data.Semigroup
1719
)
1820
where
@@ -72,3 +74,15 @@ instance Semigroup Any where
7274
Any False <> Any True = Any True
7375
Any True <> Any False = Any True
7476
Any True <> Any True = Any True
77+
78+
data Top = forall x. Top x
79+
throw :: x ->. Top
80+
throw = Top
81+
82+
instance Prelude.Semigroup Top where
83+
Top x <> Top y = Top (x,y)
84+
instance Semigroup Top where
85+
Top x <> Top y = Top (x,y)
86+
instance Prelude.Monoid Top where
87+
mempty = Top ()
88+
instance Monoid Top where

src/Data/Profunctor/Kleisli/Linear.hs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ instance Control.Applicative f => DWandering (Kleisli f) where
4949
-- profunctorial properties still hold in this weaker setting.
5050
-- However stronger requirements on `f` are needed for profunctorial
5151
-- strength, so we have fewer instances.
52-
--
53-
-- Category theoretic remark: duality doesn't work in the obvious way, since
54-
-- (,) isn't the categorical product. Instead, we have a product (&), called
55-
-- "With", defined by
56-
-- > type With a b = forall r. Either (a ->. r) (b ->. r) ->. r
57-
-- which satisfies the universal property of the product of `a` and `b`.
58-
-- CoKleisli arrows are strong with respect to this monoidal structure,
59-
-- although this might not be useful...
6052
newtype CoKleisli w a b = CoKleisli { runCoKleisli :: w a ->. b }
6153

6254
instance Data.Functor f => Profunctor (CoKleisli f) where

src/Data/Profunctor/Linear.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ instance Strong Either Void LinearArrow where
9999
first (LA f) = LA $ either (Left . f) Right
100100
second (LA g) = LA $ either Left (Right . g)
101101

102+
instance DWandering LinearArrow where
103+
dwander (LA f) = LA (Data.fmap f)
104+
102105
instance Profunctor (->) where
103106
dimap f g h x = g (h (f x))
104107
instance Strong (,) () (->) where

src/Foreign/Marshal/Pure.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import Foreign.Marshal.Utils
6060
import Foreign.Ptr
6161
import Foreign.Storable
6262
import Foreign.Storable.Tuple ()
63-
import Prelude (($), return, (<*>))
63+
import Prelude (($), return, (<*>), (<$>))
6464
import Prelude.Linear hiding (($))
6565
import System.IO.Unsafe
6666
import qualified Unsafe.Linear as Unsafe

src/Prelude/Linear.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import Prelude hiding
4646
, either
4747
, maybe
4848
, (.)
49-
, Functor(..)
49+
, Functor(..), (<$>)
5050
, Applicative(..)
5151
, Monad(..)
5252
, Traversable(..)

src/System/IO/Linear.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import qualified Control.Monad.Linear as Control
4141
import qualified Data.Functor.Linear as Data
4242
import GHC.Exts (State#, RealWorld)
4343
import Prelude.Linear hiding (IO)
44+
import Prelude ((<$>))
4445
import qualified Unsafe.Linear as Unsafe
4546
import qualified System.IO as System
4647

0 commit comments

Comments
 (0)