optics Haskell Library

repository·master·Indexed 18 days ago

https://github.com/well-typed/optics

A high-performance optics library for Haskell providing lenses, prisms, and other optical traversals. Includes a CLI for visualizing optics hierarchies and migration guides for transitioning indexed type class instances to optics-0.4 via the indexed-traversable package.

Tokens
812
Snippets
2
Records
3
Agent score
14%

What's inside optics

  1. Migrate indexed type class instances to optics-0.4

    master

    In optics-0.4, the FunctorWithIndex, FoldableWithIndex, and TraversableWithIndex type classes have been moved to the indexed-traversable package.

    If you define your own instances for these classes, you should now depend directly on indexed-traversable.

    Warning on Duplicates: The lens package (versions < 5) defines similar classes and is also migrating to indexed-traversable. If your package defines both the old optics versions and the new indexed-traversable versions, you may encounter duplicate instance errors.

  2. Maintain backward compatibility for optics-0.3 indexed instances

    master

    If your package must support both optics-0.4 (and newer) and optics-0.3 users, you should implement your indexed instances using the indexed-traversable package and then provide re-exports/wrappers for the older optics-core classes using CPP (C Pre-Processor) macros. This ensures you only define the logic once in indexed-traversable while satisfying the requirements of older optics versions.

    -- from indexed-traversable
    import Data.Functor.WithIndex
    
    -- from optics-core
    import qualified Optics.Core as O
    
    -- your (indexed) container
    data MySeq a = ...
    
    -- indexed-traversable instance (the primary source of truth)
    instance FunctorWithIndex     Int MySeq where imap = ...
    instance FoldableWithIndex    Int MySeq where ifoldMap = ...
    instance TraversableWithIndex Int MySeq where itraverse = ...
    
    -- optics-core <0.4 instance, note the ! to avoid conflicts in 0.4+
    #if !MIN_VERSION_optics_core(0,4,0)
    instance O.FunctorWithIndex     Int MySeq where imap = imap
    instance O.FoldableWithIndex    Int MySeq where ifoldMap = ifoldMap
    instance O.TraversableWithIndex Int MySeq where itraverse = itraverse
    #endif
  3. Use the MMP.Optics CLI to visualize optics hierarchies

    master

    The optics CLI allows you to visualize the hierarchy and structure of different optics types (hierarchy, reoptics, indexedoptics) as diagrams.

    By default, running the command without arguments or with an unrecognized argument prints the general hierarchy diagram. You can also target specific optics by providing their name as an argument.

    # Print the default hierarchy diagram
    optics
    
    # Print specific diagrams
    optics hierarchy
    optics reoptics
    optics indexedoptics
    
    # Print a diagram for a specific optic name (e.g., 'myOptic')
    optics myOptic