-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Create a Dictionary abstraction over the raw Cons list. #327
Conversation
Would it be possible to go with something more like std::hash_map? I'm wondering if we can simplify things and use standard classes, essentially. |
In general I agree with going with std data structures when possible. Regarding std::hash_map... the problem is that the type checker and interpreter very often copy the dictionary (at every AST node) and then make independent changes to the two copies. The copies would be O(n) with a std::hash_map but are O(1) with the cons-based Dictionary. The Dictionary has O(n) lookup, but that only happens at variable nodes in the AST, and not every AST node. BTW, I think the Stack class could and probably should be replaced by std::stack. |
This Fixes #316 |
Maybe you can modify code to demonstrate how this is intended to be used? e.g., if you backed out the iterator code, would this still work? Is Part of my hesitant around this change is probably also based on FWIW, I've had some concerns over |
Regarding how it is used, I've got a branch named Regarding the naming of SetValueAt and SettingValueAt, I'm open to suggestions. Those names were suggestions from Dave. I was also thinking about operator[]...
|
Forgot to mention the purpose of the iterator... that's needed, for example, to print out a dictionary for tracing purposes. To replace PrintEnv and PrintTypeEnv with code that's not directly working with low-level stuff like Cons or AssocList. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListNode
would be appreciated (Node
alone sounds tree-ish). Not needed here though. Doing a pass of comments...
Regarding SettingValueAt, how about "CopyAndSetValue"? As you can probably tell, I'm happy with explicit names.
Regarding operator[]
overloading, style advice is to be judicious. In this case, I would be fine overloading it for Lookup and SetValueAt, although having the functions may be easier on the caller to read.
As an alternative, you might consider shortening names:
- Lookup -> Get
- SetValueAt -> Set
- SettingValueAt -> CopyAndSetValueAt (above suggestion) -> CopyAndSet
I went with the method names Set and Get. I ditched SettingValueAt because the same thing can be accomplished with a copy and a Set. |
On Tue, Mar 2, 2021 at 1:13 PM Jon Meow notifications@github.com wrote:
This is part of a pattern that comes up repeatedly in programming oriented
One often wants both versions of the operation, both for expressivity—it (Ideally, there would be language features so the operations could be
we had this feature in Swift for a week or two before somebody panicked In any case, the way we came up to relate and distinguish the two I'm certainly open to considering other conventions if you have a FWIW, I've had some concerns over Cons because it feels like an
Jeremy already answered about Cons; I'll just add that it's an established I very much suspect, though, even if we do use std components like stack,
will be much better. Method syntax is another important tool in clarifying I hope this begins to explain why we're headed this way /cc @geoffromer -- |
@jonmeow wrote:
FWIW, in lisp, cons cells are in fact used to build everything, including trees. I'm not defending the name or the usage. At the same time, there's limited benefit in spending the time to rename something we know we want to throw out soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the changes @jsiek.
@dabrahams I think this code is more understandable as it's currently implemented. I think the SettingValueAt method has simply been dropped, which I think eliminates that dissent.
* add command-line flag to enable/disable tracing output * adding missing exit for pattern variable in wrong context and a test case for it (#324) * Update executable_semantics/interpreter/interpreter.cpp comment on separate line as code Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com> * fix interpreter's handling of optional else of if statement (#323) * Update pattern_variable_fail.golden due to error (#334) * Use llvm's CommandLine for parsing (#332) * GitHub testing action (#331) Co-authored-by: Chandler Carruth <chandlerc@gmail.com> * add copyright * Create a Dictionary abstraction over the raw Cons list. (#327) * Create a Dictionary abstraction over the raw Cons list. * renamed Cons and some methods of Dictionary, various other cleanup * Update executable_semantics/tracing_flag.cpp added namespace comment Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com> * added a comment to cpp file Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com> Co-authored-by: Dave Abrahams <dabrahams@google.com> Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
* Create a Dictionary abstraction over the raw Cons list. * renamed Cons and some methods of Dictionary, various other cleanup
* add command-line flag to enable/disable tracing output * adding missing exit for pattern variable in wrong context and a test case for it (#324) * Update executable_semantics/interpreter/interpreter.cpp comment on separate line as code Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com> * fix interpreter's handling of optional else of if statement (#323) * Update pattern_variable_fail.golden due to error (#334) * Use llvm's CommandLine for parsing (#332) * GitHub testing action (#331) Co-authored-by: Chandler Carruth <chandlerc@gmail.com> * add copyright * Create a Dictionary abstraction over the raw Cons list. (#327) * Create a Dictionary abstraction over the raw Cons list. * renamed Cons and some methods of Dictionary, various other cleanup * Update executable_semantics/tracing_flag.cpp added namespace comment Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com> * added a comment to cpp file Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com> Co-authored-by: Dave Abrahams <dabrahams@google.com> Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
A subsequent PR will replace the uses of AssocList with this Dictionary class.