3

Recently, a question I asked was closed for lacking in focus: Simplifying a Python AST for an arithmetic expression [closed] The help page for "focus" says:

If your question has many valid answers (but no way to determine which, if any, are correct), then it probably needs to be more focused to be successful in our format. This question currently includes multiple questions in one. It should focus on one problem only.

Since I want to ask the same question again (as the closed question banner suggests), I need help to improve the focus of the question so that it is not closed again.

9
  • 17
    Do NOT ask the question again. EDIT the existing question based on the feedback you receive here.
    – Paulie_D
    Jun 2, 2022 at 20:03
  • 6
    Then the banner is ambiguous: "You can edit the question or post a new one." Jun 2, 2022 at 20:13
  • 1
    Maybe it's because I'm not familiar with Python's ast library, but I don't see why this would have been closed as Needs more focus. I could see Needs debugging details making sense since there doesn't appear to be an attempt at the desired output, or the current result of the code in the question.
    – codewario
    Jun 2, 2022 at 20:15
  • 21
    @BjörnLindqvist You're absolutely right, the banner is very ambiguous, we've been trying to get it improved for a really long time... the second phrase should be interpreted as "ask a different one"; reposting the same question to the same site is essentially always ill-advised. Your move after a question closure should always be to edit it into shape if you still want an answer.
    – zcoop98
    Jun 2, 2022 at 20:20
  • 14
    The banner is an invitation to get a question ban. Re-ask a question and it will be closed as a duplicate of the first and probably collect you more ban-contributing downvotes. Looks like you're safe, but I don't know (or want to know) how many new users have been shot in the foot by that <expletive deleted>y advice. Jun 2, 2022 at 20:50
  • I don't see anything about asking a new question? For me the banner just says "Update the question so it focuses on one problem only. This will help others answer the question. You can edit the question.". Is it different for the OP vs. others? Jun 2, 2022 at 21:07
  • 3
    @KarlKnechtel the close banners are now personalized, so that the OP and others may see different messages.
    – Andrew T.
    Jun 2, 2022 at 21:14
  • 4
    @BendertheGreatest there is no code to debug (beyond code to create the original AST), since OP doesn't know where to start. There are therefore no debugging details to request. The question is too broad because of the amount of work that needs to be done to solve the problem: it has not been analyzed and broken down into parts. The code is only a few lines that can be run without modification and don't cause an error, so I don't see the point in expecting the result of the print to be pasted into the question. Jun 3, 2022 at 4:21
  • @BjörnLindqvist I think you'll find more texts on Stack Overflow are needlessly ambiguous, so when in doubt... please ask (although meta usually already has the answer, so do try to search first or you might make people grumpy :) ).
    – Gimby
    Jun 3, 2022 at 7:32

1 Answer 1

8

The question is considered "not focused" because it asks to do too much. I assume that what you have in mind is something like: "recursively, process the tree according to the following rule: if the current node is a BinOp and the children are both numeric constants, perform the operation and replace this node with the result".

There is nothing built-in that would do all of that. (There is, however, a NodeTransformer class that can help with the graph-traversal aspect of the problem.) If you hadn't thought of an algorithm along those lines, then you either were hoping there would be such a built-in (in which case the answer is a simple "no") or you simply didn't take the appropriate steps to figure out the actual problem (i.e., where do you get stuck, trying to write the code for that algorithm?). If you had, then you should have said so, and shown your work so far.

However, even an algorithm like that wouldn't work for the example you showed. There is a well-known third-party library that does that kind of symbolic manipulation of expressions: it's called Sympy, and it's very heavyweight. I've never heard of simplified versions of that, because the application is too niche.

If you restrict yourself to + - / * operators and integer constants, I believe it can be shown that the result will always be a polynomial in those constants (as would, necessarily, the result at each node). You theoretically could define a polynomial class that maps a tuple of (variable, exponent) tuples to the coefficient for the corresponding term of the polynomial, and use that to store intermediate results in the kind of traversal I described. If you restrict to a single variable with a given name, it could just be a list of coefficients for x^0, x^1 etc. Either way, you would want to use a rational number class to avoid floating-point inaccuracies (since integers are not closed under division). I hope you get the point, though: this would be way too much to ask for in a single question.

It is also good to do research before asking. I tried putting python ast simplify into a search engine and readily found this previous question. site:docs.python.org ast immediately finds the documentation, which allows for a quick scan for useful functionality. python symbolic computation will tell you all about SymPy - admittedly, you have to have the concept in mind first.

6
  • I indeed found that question before asking. The difference is that they are asking about evaluating asts. From "3 + 2*(3 + 1)" to "11". I'm asking about algebraically manipulating asts. From "3 + x - 2*(y + x) - y" to "3 - x - 3*y". I think there are algorithms and libraries (other than SymPy which I don't want to use cause it doesn't operate on asts) for this. Idk how to focus my question more than "An algorithm/implementation for simplifying algebraic integer expressions that is compatible with the Python ast module". Jun 2, 2022 at 22:12
  • 1
    That's because it's a difficult and broad subject, in the same way that "transform an AST into compiled code and apply optimizations" is. Books on compiler theory may help. Jun 2, 2022 at 22:13
  • I.e the question "How to simplify an algebraic integer expression using an algorithm/implementation compatible with the Python ast module?" is not focused enough? Then can you suggest a way to focus the question or break it down into subquestions? Or is the question not allowed on SO at all? Jun 2, 2022 at 22:25
  • I cannot easily pick out a sub-question. If you do the research described, you may be able to figure something out. Jun 2, 2022 at 22:27
  • 2
    "There is nothing built-in that would do [constant folding of an AST]." There kind of is - the CPython bytecode compiler itself will do it, and the compile function will accept an AST. However, I don't think there's anything built-in which will convert the resulting code object back into an AST. (This comment is just for my amusement, really.)
    – kaya3
    Jun 3, 2022 at 4:16
  • @KarlKnechtel Then I cannot accept your answer since it does not tell me how to focus my question. Jun 3, 2022 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .