Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No lens support #10

Open
Gurkenglas opened this issue Aug 24, 2016 · 10 comments
Open

No lens support #10

Gurkenglas opened this issue Aug 24, 2016 · 10 comments

Comments

@Gurkenglas
Copy link
Contributor

record uses lenses because, as he notes, they already solve the modification part of the record problem. Why don't you?

@jkarni
Copy link
Contributor

jkarni commented Aug 24, 2016

That's a good question. I mention the idea briefly in the second-to-last paragraph of the blog post I just wrote about this. The primary reason is that I still don't know how to deal neatly with deletion if we were to use lenses. Deleting could be modifying with const Void. But we'd have to add another step to the normalization - in addition to sorting the list and removing duplicates, remove Voids. And I think that'd make things a little ugly and inconsistent, but in all honestly, I haven't investigated this fully.

The secondary reason is that this seemed slightly easier to code, and the approaches aren't (EDIT: was originally 'are') mutually exclusive, so I thought I'd do this first. Happy to add lens support, though. Maybe this issue should be the place for discussing adding it.

@Gurkenglas
Copy link
Contributor Author

Gurkenglas commented Aug 24, 2016

You mean a lens Modifiable field val val' old new => Key field -> Lens old new val val, where val ~ () means that the field is being added and val' ~ () means that the field is being removed? Void is the type with no inhabitants. (Perhaps then you could even get rid of Key field and let hashtag-tokens stand directly for the lenses?)

@jkarni
Copy link
Contributor

jkarni commented Aug 24, 2016

Basically! Whether Void or () is largely irrelevant to me (for val'), since users should never see either one! That said, we probably want a type that's isomorphic to either, but our own and not exported, so users don't accidentally witness weird behavior.

@Gurkenglas
Copy link
Contributor Author

Gurkenglas commented Aug 24, 2016

If it's Void, how would you ever delete a value? You'd need to apply over the lens some a -> Void, which has no inhabitants.

@jkarni
Copy link
Contributor

jkarni commented Aug 24, 2016

which has no inhabitants.

Except bottom :). But if that sounds bad - sure, then (). As I said, I think if it ends up mattering it's because we screwed up.

@Gurkenglas
Copy link
Contributor Author

Gurkenglas commented Aug 24, 2016

Why not tell the user that all the fields already implicitly exist with a value of type ()? It's basically equivalent to non. (In fact maybe you can even use non in your source?)

@jkarni
Copy link
Contributor

jkarni commented Aug 24, 2016

Why not tell the user that all the fields already implicitly exist with a value of type ()?

That's an option, but then you still have to make sure that e.g. show behaves the same for deleted vs. never-created values. And in addition to show, ==, which is even worse because you'd like to compare values of different type!

As for non - it looks very promising, thanks for the link!

@Gurkenglas
Copy link
Contributor Author

Gurkenglas commented Aug 25, 2016

And then we add https://hackage.haskell.org/package/indexed-extras-0.2/docs/Control-Monad-Indexed-State.html and define a variant on %= which has type IxMonadState m => ASetter s t a b -> (a -> b) -> m s t () and possibly magically make the # tokens be the lenses and suddenly the jane example looks like

execBook :: IxStateT Identity (Book '[]) t () -> t
execBook = snd . runIdentity . flip runIxStateT emptyBook

jane :: Book '[ "name" :=> String, "age" :=> Int ]
jane = execBook $ do
  #name .= "Jane"
  #age .= 30

and then you can throw other state actions behind that into the same do block.

@jkarni
Copy link
Contributor

jkarni commented Aug 25, 2016

That is beautiful :)

@jsdw
Copy link

jsdw commented Sep 12, 2016

This looks amazing - I'd second the notion that having lenses work out of the box with it would be awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants