Safe Haskell | None |
---|---|
Language | Haskell2010 |
Control.Optics.Linear.Iso
Description
This module provides linear isomorphisms.
An Iso a b s t
is equivalent to a (s %1-> a, b %1-> t)
. In the simple
case of an Iso' a s
, this is equivalent to inverse functions
(s %1-> a, a %1-> s)
. In the general case an Iso a b s t
means if you
have the isomorphisms (a %1-> b, b %1-> a)
and (s %1-> t, t %1-> s)
, then
you can form isomorphisms between s
, t
, a
and b
.
Example
{-# LANGUAGE LinearTypes #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE GADTs #-} import Control.Optics.Linear.Internal import Prelude.Linear import qualified Data.Functor.Linear as Data -- A toy example of operating over two isomorphic linear types closureFmap :: (a %1-> b) -> ClosureEither x a %1-> ClosureEither x b closureFmap f = over isoEithers (Data.fmap f) data ClosureEither a b where CLeft :: x %1-> (x %1-> a) %1-> ClosureEither a b CRight :: x %1-> (x %1-> b) %1-> ClosureEither a b isoEithers :: Iso (ClosureEither a b) (ClosureEither a b') (Either a b) (Either a b') isoEithers = iso fromClosure fromEither where fromEither :: Either a b %1-> ClosureEither a b fromEither (Left a) = CLeft () (() -> a) fromEither (Right b) = CRight () (() -> b) fromClosure :: ClosureEither a b %1-> Either a b fromClosure (CLeft x f) = Left (f x) fromClosure (CRight x f) = Right (f x)
Synopsis
- type Iso s t a b = Optic Profunctor s t a b
- type Iso' s a = Iso s s a a
- (.>) :: forall (arr :: Type -> Type -> Type) s t a b x y. Optic_ arr s t a b -> Optic_ arr a b x y -> Optic_ arr s t x y
- swap :: forall (m :: Type -> Type -> Type) u a b c d. SymmetricMonoidal m u => Iso (m a b) (m c d) (m b a) (m d c)
- assoc :: forall (m :: Type -> Type -> Type) u a b c d e f. SymmetricMonoidal m u => Iso (m a (m b c)) (m d (m e f)) (m (m a b) c) (m (m d e) f)
- withIso :: Optic_ (Exchange a b) s t a b -> ((s %1 -> a) -> (b %1 -> t) -> r) -> r
- iso :: (s %1 -> a) -> (b %1 -> t) -> Iso s t a b
Types
type Iso s t a b = Optic Profunctor s t a b Source #
Composing optics
(.>) :: forall (arr :: Type -> Type -> Type) s t a b x y. Optic_ arr s t a b -> Optic_ arr a b x y -> Optic_ arr s t x y infixr 9 Source #
Common optics
swap :: forall (m :: Type -> Type -> Type) u a b c d. SymmetricMonoidal m u => Iso (m a b) (m c d) (m b a) (m d c) Source #
assoc :: forall (m :: Type -> Type -> Type) u a b c d e f. SymmetricMonoidal m u => Iso (m a (m b c)) (m d (m e f)) (m (m a b) c) (m (m d e) f) Source #