I have a data structure that can be understood as analogous to Data.Map
in that it maps keys of one type to values of another. I would like to write an instance of Control.Lens.At
for this type but I cannot ever seem to satisfy all the requirements.
Given Struct k v
with lookup
, insert
, update
, and delete
, what must I do to make instance At (Struct k v)
work?
The
at
method should return an indexed lens for a given index gets as input your structure and behaves like this:Nothing
, otherwise return the value at the key in the structure.Nothing
, remove the key from the structure, otherwise set it (or create it if it's not already there) to the value in theJust
.at
.This leads to the following code for your requirements:
I use
lens
to construct a lensilens
to construct an indexed lens from a getter and a setter. I also assume that your functions have the following types:You still have to define the
IxValue
andIndex
type family instances:EDIT: Actually, at must return an indexed lens, not just a lens. I also confused the order of the arguments to the setter.