Skip to content

Lets.Lens: type of modify incompatible with Prisms? #11

@endgame

Description

@endgame

In Lets.Lens, modify has the following type signature:

modify :: Lens s t a b -> (a -> b) -> s -> t

This is fine until the final exercise, which seems to want me to use a Prism with modify:

intOrLengthEven :: IntOr [a] -> IntOr Bool
intOrLengthEven = modify intOrP (even . length)

This fails with the following type error:

src/Lets/Lens.hs:756:26: error:
    • Could not deduce (Applicative f) arising from a use of ‘intOrP’
      from the context: Functor f
        bound by a type expected by the context:
                   Lens (IntOr [a]) (IntOr Bool) [a] Bool
        at src/Lets/Lens.hs:756:19-47
      Possible fix:
        add (Applicative f) to the context of
          a type expected by the context:
            Lens (IntOr [a]) (IntOr Bool) [a] Bool
    • In the first argument of ‘modify’, namely ‘intOrP’
      In the expression: modify intOrP (even . length)
      In an equation for ‘intOrLengthEven’:
          intOrLengthEven = modify intOrP (even . length)
    |
756 | intOrLengthEven = modify intOrP (even . length)
    |                          ^^^^^^

Changing the type of modify to modify :: Set s t a b -> (a -> b) -> s -> t fixes this problem. I note that lens uses this type for over :: ASetter s t a b -> (a -> b) -> s -> t (because type ASetter s t a b = (a -> Identity b) -> s -> Identity t ).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions