6

A type like Maybe (Lens' a b) doesn't work because Lens' is under the hood a Rank-2 type, that can't be wrapped in a type constructor without the -XImpredicativeTypes extension (which is not really supported in GHC).

What is thus the best type to give a function that would morally have type

foo :: A -> Maybe (Lens' B C)

A possibility would be to defer the Maybe into a passed continuation

foo' :: ∀ y . A -> (Lens' B C -> y) -> Maybe y

but I don't particularly like that.

leftaroundabout
  • 101,764
  • 3
  • 156
  • 291
  • Use [`ALens`](https://hackage.haskell.org/package/lens-4.15.1/docs/Control-Lens-Lens.html#t:ALens) maybe? – András Kovács Jan 07 '17 at 15:15
  • There's also `ReifiedLens` which is just the usual newtype wrapper.around a `Lens`. When you're returning a lens it might be more efficient to use `ReifiedLens` than `ALens`, and it seems about equally convenient for the consumer. – Reid Barton Jan 07 '17 at 15:31

1 Answers1

7

This is what the Control.Lens.Reified module is for. It contains newtype wrappers for the lens hierarchy.

foo :: A -> Maybe (ReifiedLens' B C)
Benjamin Hodgson
  • 37,496
  • 16
  • 98
  • 147