HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulemicrolens-0.4.14.0Haskell2010

Lens.Micro.Internal

This module is needed to give other packages from the microlens family (like microlens-ghc) access to functions and classes that don't need to be exported from Lens.Micro (because they just clutter the namespace). Also:

  • traversed is here because otherwise there'd be a dependency cycle

  • sets is here because it's used in RULEs

Classes like Each, Ixed, etc are provided for convenience – you're not supposed to export functions that work on all members of Ixed, for instance. Only microlens can do that. You mustn't declare instances of those classes for other types, either; these classes are incompatible with lens's classes, and by doing so you would divide the ecosystem.

If you absolutely need to define an instance (e.g. for internal use), only do it for your own types, because otherwise I might add an instance to one of the microlens packages later and if our instances are different it might lead to subtle bugs.

  • 1 type
  • 11 classes
  • 11 values
valuesets :: ((a -> b) -> s -> t) -> ASetter s t a b
#

sets creates an ASetter from an ordinary function. (The only thing it does is wrapping and unwrapping Identity.)

classclass Each s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Methods

Instances9Each, …
  • Each (Complex a) (Complex b) a bDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Each (NonEmpty a) (NonEmpty b) a bDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Each (Maybe a) (Maybe b) a bDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Each [a] [b] a bDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • (a ~ a', b ~ b') => Each (Either a a') (Either b b') a bDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • (a ~ b, q ~ r) => Each (a, b) (q, r) a qDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • (a ~ b, a ~ c, q ~ r, q ~ s) => Each (a, b, c) (q, r, s) a qDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • (a ~ b, a ~ c, a ~ d, q ~ r, q ~ s, q ~ t) => Each (a, b, c, d) (q, r, s, t) a qDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • (a ~ b, a ~ c, a ~ d, a ~ e, q ~ r, q ~ s, q ~ t, q ~ u) => Each (a, b, c, d, e) (q, r, s, t, u) a qDefined in microlens-0.4.14.0 · Lens.Micro.Internal
familytype family Index s
#
Instances3Index
  • type Index (NonEmpty a) = IntDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • type Index (e -> a) = eDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • type Index [a] = IntDefined in microlens-0.4.14.0 · Lens.Micro.Internal
familytype family IxValue m
#
Instances3IxValue
  • type IxValue (NonEmpty a) = aDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • type IxValue (e -> a) = aDefined in microlens-0.4.14.0 · Lens.Micro.Internal
  • type IxValue [a] = aDefined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Ixed m where
#

Methods

  • ix :: Index m -> Traversal' m (IxValue m)

    This traversal lets you access (and update) an arbitrary element in a list, array, Map, etc. (If you want to insert or delete elements as well, look at at.)

    An example for lists:

    Example1 expression
    [0..5] & ix 3 .~ 10[0,1,2,10,4,5]

    You can use it for getting, too:

    Example1 expression
    [0..5] ^? ix 3Just 3

    Of course, the element may not be present (which means that you can use ix as a safe variant of (!!)):

    Example1 expression
    [0..5] ^? ix 10Nothing

    Another useful instance is the one for functions – it lets you modify their outputs for specific inputs. For instance, here's maximum that returns 0 when the list is empty (instead of throwing an exception):

    maximum0 = maximum & ix [] .~ 0
    

    The following instances are provided in this package:

    ix :: Int -> Traversal' [a] a
    
    ix :: Int -> Traversal' (NonEmpty a) a
    
    ix :: (Eq e) => e -> Traversal' (e -> a) a
    

    You can also use ix with types from array, bytestring, and containers by using microlens-ghc, or additionally with types from vector, text, and unordered-containers by using microlens-platform.

Instances3Ixed
  • Ixed (NonEmpty a)Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Ixed [a]Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Eq e => Ixed (e -> a)Defined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Ixed m => At m where
#

Methods

  • at :: Index m -> Lens' m (Maybe (IxValue m))

    This lens lets you read, write, or delete elements in Map-like structures. It returns Nothing when the value isn't found, just like lookup:

    Data.Map.lookup k m = m ^. at k
    

    However, it also lets you insert and delete values by setting the value to Just value or Nothing:

    Data.Map.insert k a m = m & at k .~ Just a
    
    Data.Map.delete k m = m & at k .~ Nothing
    

    Or you could use (?~) instead of (.~):

    Data.Map.insert k a m = m & at k ?~ a
    

    Note that at doesn't work for arrays or lists. You can't delete an arbitrary element from an array (what would be left in its place?), and you can't set an arbitrary element in a list because if the index is out of list's bounds, you'd have to somehow fill the stretch between the last element and the element you just inserted (i.e. [1,2,3] & at 10 .~ 5 is undefined). If you want to modify an already existing value in an array or list, you should use ix instead.

    at is often used with non. See the documentation of non for examples.

    Note that at isn't strict for Map, even if you're using Data.Map.Strict:

    Example1 expression
    Data.Map.Strict.size (Data.Map.Strict.empty & at 1 .~ Just undefined)1

    The reason for such behavior is that there's actually no “strict Map” type; Data.Map.Strict just provides some strict functions for ordinary Maps.

    This package doesn't actually provide any instances for at, but there are instances for Map and IntMap in microlens-ghc and an instance for HashMap in microlens-platform.

classclass Field1 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Methods

  • _1 :: Lens s t a b

    Gives access to the 1st field of a tuple (up to 5-tuples).

    Getting the 1st component:

    Example1 expression
    (1,2,3,4,5) ^. _11

    Setting the 1st component:

    Example1 expression
    (1,2,3) & _1 .~ 10(10,2,3)

    Note that this lens is lazy, and can set fields even of undefined:

    Example1 expression
    set _1 10 undefined :: (Int, Int)(10,*** Exception: Prelude.undefined

    This is done to avoid violating a lens law stating that you can get back what you put:

    Example1 expression
    view _1 . set _1 10 $ (undefined :: (Int, Int))10

    The implementation (for 2-tuples) is:

    _1 f t = (,) <$> f    (fst t)
                 <*> pure (snd t)
    

    or, alternatively,

    _1 f ~(a,b) = (\a' -> (a',b)) <$> f a
    

    (where ~ means a lazy pattern).

    _2, _3, _4, and _5 are also available (see below).

Instances4Field1
  • Field1 (a, b) (a', b) a a'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field1 (a, b, c) (a', b, c) a a'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field1 (a, b, c, d) (a', b, c, d) a a'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field1 (a, b, c, d, e) (a', b, c, d, e) a a'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Field2 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Methods

Instances4Field2
  • Field2 (a, b) (a, b') b b'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field2 (a, b, c) (a, b', c) b b'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field2 (a, b, c, d) (a, b', c, d) b b'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field2 (a, b, c, d, e) (a, b', c, d, e) b b'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Field3 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Methods

Instances3Field3
  • Field3 (a, b, c) (a, b, c') c c'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field3 (a, b, c, d) (a, b, c', d) c c'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field3 (a, b, c, d, e) (a, b, c', d, e) c c'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Field4 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Methods

Instances2Field4
  • Field4 (a, b, c, d) (a, b, c, d') d d'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
  • Field4 (a, b, c, d, e) (a, b, c, d', e) d d'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Field5 s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Methods

Instances1Field5
  • Field5 (a, b, c, d, e) (a, b, c, d, e') e e'Defined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Cons s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Methods

Instances1Cons
  • Cons [a] [b] a bDefined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Snoc s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

Methods

Instances1Snoc
  • Snoc [a] [b] a bDefined in microlens-0.4.14.0 · Lens.Micro.Internal
classclass Strict lazy strict | lazy -> strict, strict -> lazy where
#

Methods

  • strict :: Lens' lazy strict

    strict lets you convert between strict and lazy versions of a datatype:

    Example2 expressions
    let someText = "hello" :: Lazy.TextsomeText ^. strict"hello" :: Strict.Text

    It can also be useful if you have a function that works on a strict type but your type is lazy:

    stripDiacritics :: Strict.Text -> Strict.Text
    stripDiacritics = ...
    
    Example2 expressions
    let someText = "Paul Erdős" :: Lazy.TextsomeText & strict %~ stripDiacritics"Paul Erdos" :: Lazy.Text

    strict works on ByteString and StateT/WriterT/RWST if you use microlens-ghc, and additionally on Text if you use microlens-platform.

  • lazy :: Lens' strict lazy

    lazy is like strict but works in opposite direction:

    Example2 expressions
    let someText = "hello" :: Strict.TextsomeText ^. lazy"hello" :: Lazy.Text

CallStack

1 declaration
typetype HasCallStack = IP "callStack" CallStack
#

Request a CallStack.

NOTE: The implicit parameter ?callStack :: CallStack is an implementation detail and should not be considered part of the CallStack API, we may decide to change the implementation in the future.

Coerce compatibility shim

1 declaration
valuecoerce :: Coercible a b => a -> b
#

The function coerce allows you to safely convert between values of types that have the same representation with no run-time overhead. In the simplest case you can use it instead of a newtype constructor, to go from the newtype's concrete type to the abstract type. But it also works in more complicated settings, e.g. converting a list of newtypes to a list of concrete types.

When used in conversions involving a newtype wrapper, make sure the newtype constructor is in scope.

This function is representation-polymorphic, but the RuntimeRep type argument is marked as Inferred, meaning that it is not available for visible type application. This means the typechecker will accept coerce @Int @Age 42.

Examples
Example5 expressions
newtype TTL = TTL Int deriving (Eq, Ord, Show)newtype Age = Age Int deriving (Eq, Ord, Show)coerce (Age 42) :: TTLTTL 42coerce (+ (1 :: Int)) (Age 42) :: TTLTTL 43coerce (map (+ (1 :: Int))) [Age 42, Age 24] :: [TTL][TTL 43,TTL 25]

Coerce-like composition

2 declarations
value(#.) :: Coercible c b => (b -> c) -> (a -> b) -> a -> c
#
value(.#) :: Coercible b a => (b -> c) -> (a -> b) -> a -> c
#