1

I was browsing the 'lens' documentation concerning prisms, and I noticed a line in Control.Lens.Review stating that the infix review operator # 'is commonly used when using a Prism as a smart constructor.'.

This got my a bit intrigued, because the example with _Left, one of constructor for the 'Either' data type didn't seem like a smart constructor as far as I understand smart constructors (adding extra restrictions on argument values). So I googled around a bit for prisms as smart constructors but I didn't really find any example.

Is there someone who knows what exactly is meant with this statement and might give an example? Or maybe tell me that my definition of smart constructor is not correct, of course.

Kasper
  • 1,205
  • 12
  • 22

1 Answers1

2

One view of the Prism type is as a near-isomorphism. In fact, the lens documentation says "It may help to think of this as a Iso that can be partial in one direction."

And isn't the partial direction a pretty good analogy for a smart constructor? You can hand it any set of arguments (that match the type), but some of them will fail to create a value.

Carl
  • 26,500
  • 4
  • 65
  • 86
  • I'm missing something I think. For example, in the Either String Int type, the _Left prism will never fail as long as you hand it a correct string type. They will always create a value, I think. I don't see how _Left can protect against invalid argument values, so I don't really see how some argument value will fail to create a correct Either. Or shouldn't I focus on _Left? Or am I misinterpreting your use of 'value'? – Kasper Nov 30 '14 at 21:21
  • 1
    @Kasper You're right that the direction of partiality in `Prism`s doesn't align with using `review` as a constructor that can fail. Maybe it's best to say that it's using a broader definition of smart constructor, just that of a function that returns a value of a specific type. That meaning almost useless broad, but it isn't unheard-of. – Carl Nov 30 '14 at 21:43