packages feed

linear 1.3.1.1 → 1.4

raw patch · 4 files changed

+53/−12 lines, 4 filesdep ~base

Dependency ranges changed: base

Files

CHANGELOG.markdown view
@@ -1,3 +1,7 @@+1.4+---+* Renamed `incore` to `column` and added an example.+ 1.3.1.1 ------- * Build bugfix
linear.cabal view
@@ -1,6 +1,6 @@ name:          linear category:      Math, Algebra-version:       1.3.1.1+version:       1.4 license:       BSD3 cabal-version: >= 1.8 license-file:  LICENSE
src/Linear/Affine.hs view
@@ -109,7 +109,7 @@ -- type level newtype Point f a = P (f a)   deriving ( Eq, Ord, Show, Read, Monad, Functor, Applicative, Foldable-           , Traversable, Apply, Bind, Additive, Metric, Core, R1, R2, R3, R4+           , Traversable, Apply, Additive, Metric            , Fractional , Num, Ix, Storable, Epsilon #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 702            , Generic@@ -118,6 +118,30 @@            , Generic1 #endif            )++lensP :: Functor f => (g a -> f (g a)) -> Point g a -> f (Point g a)+lensP afb (P a) = (\b -> P b) <$> afb a++instance Bind f => Bind (Point f) where+  join (P m) = P $ join $ fmap (\(P m')->m') m++instance Core f => Core (Point f) where+  core f = P $ core (\l->f (lensP . l))++instance R1 f => R1 (Point f) where+  _x = lensP . _x++instance R2 f => R2 (Point f) where+  _y = lensP . _y+  _xy = lensP . _xy++instance R3 f => R3 (Point f) where+  _z = lensP . _z+  _xyz = lensP . _xyz++instance R4 f => R4 (Point f) where+  _w = lensP . _w+  _xyzw = lensP . _xyzw  instance Additive f => Affine (Point f) where   type Diff (Point f) = f
src/Linear/Core.hs view
@@ -16,7 +16,7 @@ ---------------------------------------------------------------------------- module Linear.Core   ( Core(..)-  , incore+  , column   ) where  import Control.Applicative@@ -27,6 +27,14 @@ import GHC.Generics (Generic1) #endif +-- $setup+-- >>> import Linear+-- >>> import Control.Lens++type LensLike f s t a b = (a -> f b) -> s -> f t+type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t+type Lens' s a = forall f. Functor f => (a -> f a) -> s -> f s+ -- | -- A 'Functor' @f@ is corepresentable if it is isomorphic to @(x -> a)@ -- for some x. Nearly all such functors can be represented by choosing @x@ to be@@ -35,11 +43,7 @@ -- 'Representable' 'Functor'. class Functor f => Core f where   -- | Form a structure by applying the given function to lenses focused on its holes.-  ---  -- @-  -- 'core' :: ((forall x. 'Control.Lens.Lens' (f x) x) -> a) -> f a-  -- @-  core :: ((forall g x. Functor g => (x -> g x) -> f x -> g (f x)) -> a) -> f a+  core :: ((forall x. Lens' (f x) x) -> a) -> f a  data Context a b t = Context { peek :: b -> t, pos :: a } #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706@@ -51,16 +55,25 @@ instance Functor (Context a b) where   fmap f (Context bt a) = Context (f.bt) a -view :: ((a -> Const a b) -> s -> Const a t) -> s -> a+view :: LensLike (Const a) s t a b -> s -> a view l = getConst . l Const  -- | This is a generalization of 'Control.Lens.inside' to work over any corepresentable 'Functor'. -- -- @--- 'incore' :: 'Core' f => 'Lens' s t a b -> 'Lens' (f s) (f t) (f a) (f b)+-- 'column' :: 'Core' f => 'Lens' s t a b -> 'Lens' (f s) (f t) (f a) (f b) -- @-incore :: (Functor g, Core f) => ((a -> Context a b b) -> s -> Context a b t) -> (f a -> g (f b)) -> f s -> g (f t)-incore l f es = o <$> f i where+--+-- In practice it is used to access a column of a matrix.+--+--+-- >>> V2 (V3 1 2 3) (V3 4 5 6) ^._x+-- V3 1 2 3+--+-- >>> V2 (V3 1 2 3) (V3 4 5 6) ^.column _x+-- V2 1 4+column :: Core f => LensLike (Context a b) s t a b -> Lens (f s) (f t) (f a) (f b)+column l f es = o <$> f i where    go = l (Context id)    i = core $ \ e -> pos $ go (view e es)    o eb = core $ \ e -> peek (go (view e es)) (view e eb)