Drasil
Drasil copied to clipboard
Add parenthesis to symbols in Expr
In the notebook, if we would like to print something like this
we have to add parenthesis to the unit because it has two ^ consecutively, so instead of
v_y^i^2
, it should be (v_y^i)^2
.
It's actually a printing thing, but if we add the parenthesis when printing, they will be added to all symbols, which I don't think is necessary for cases like y^2
. And we can't just hard code the ( ) like this square ((sy yVel))
in the expression.
Therefore, I think maybe we could add the parenthesis when we define the expression. I was thinking to add the constructor/operator in Expr. I don't think we have this now since this kind of case doesn't appear in the existing example, but I might be wrong.
That's an interesting issue. It looks like the "i" is a part of the symbol of yVel
. If this is the case, when printing, would it be possible to pattern match on the base terms to see if they're a symbol with a superscript? If they are, then we could blanket apply this as a fix for everything without having to manually designate the parentheses in the Expr
language (e.g., instead of always having to manually write square $ paren $ sy yVel
[assuming paren
would be the smart constructor for the potential new term Parentheses term in Expr], we would automatically be able to determine if/when to place the parentheses when printing, based on sy yVel
, writing just square $ sy yVel
).
It also seems like this is this related to us not yet having proper list types yet in Drasil, which is why the "i" appears in the symbol (though I'm not 100% sure on this instance).
Yes, the "i" is part of the symbol. I was thinking about the pattern matching, but we actually don't know whether the superscript is part of a symbol or an operation. We also don't know whether there is another superscript right after this one. We print the expression recursively and we only analyze what to print now.
Are we unable to check for both cases? If there's a superscript after the 2, I imagine it should be rolled into the 2 assuming it was also a power operation. Are there other cases where a superscript appears?
I think it is able to check if we define them differently since they both fall into the "Sup" category now.
When it encounters a superscript, the second expression will be attached to the first one. But we handle the symbols and operations separately. We could only know what it is in the Import
, we know nothing about it when we actually want to print them. I don't think we can check what is coming up next in the printer(? It seems to be too complicated for the printer.
Also, ^ is also used as a over hat symbol. But it is defined differently as a OverSymb
. I guess this won't be a problem because we print it differently, like \hat{}
not just ^
.
data OverSymb = Hat
Although it doesn't solve the problem for the long term, we could temporarily address the issue by replacing the vector components v_x and v_y with u and v. There will be cases where we want to use the subscript notation to show components, but we could postpone that.
@balacij, the superscript i isn't a list index in this case. The i is to represent initial, for the initial velocity.
Note that we should add some cleverness to the printing code (there is already some in there). For example, if we're doing a superscript of a +, we should put in parens, always. This says that if we're doing double-superscript (or double-subscript), we should add parens too. The case here might be trickier still: it looks like symbol-sup-sub-sup ! We don't want parens around sup-sub (or sub-sup), but we do if it's (sub-sup-sub) or (sup-sub-sup)!