haskell - How to write an instance of Control.Lens.AT -
i have data structure can understood analogous data.map in maps keys of 1 type values of another. write instance of control.lens.at type cannot ever seem satisfy requirements.
given struct k v lookup, insert, update, , delete, must make instance @ (struct k v) work?
the at method should return indexed lens given index gets input structure , behaves this:
- on getting, if key not present, return
nothing, otherwise return value @ key in structure. - on setting, if new value
nothing, remove key structure, otherwise set (or create if it's not there) value injust. - the index key given
at.
this leads following code requirements:
instance @ (struct k v) @ key = ilens getter setter getter = (key, lookup key) setter s nothing = delete key s setter s (just newvalue) = insert key newvalue s i use lens construct lensilens construct indexed lens getter , setter. assume functions have following types:
lookup :: k -> struct k v -> maybe v delete :: k -> struct k v -> struct k v insert :: k -> v -> struct k v -> struct k v -- insert should override key when it's there you still have define ixvalue , index type family instances:
type instance ixvalue (struct k v) = v -- (struct k v) contains values of type v type instance index (struct k v) = k -- (struct k v) has keys of type k. edit: actually, @ must return indexed lens, not lens. confused order of arguments setter.
Comments
Post a Comment