Infix to postfix for negative numbers - infix-notation

How do I convert negative numbers from infix to postfix ?
Suppose I have a expression
a = - b - (-c-d)
In some places I read that you can parathesize the negative numbers like
a = (-b) - (-c-d)
But here if I do that, I will get a term like "ab-" at the beginning of the postfix expression which means a-b and is incorrect.
How do I convert this ?

In infix notation, you must distinguish between the binary subtraction operator sub and the unary negation operator neg. Both are represented by a minus sign, but the context tells you which is which.
You've got a negation, when the minus as at the beginning of the expression, or after an opening parenthesis or after a binary operator:
    − (x + y)   →   x y add neg
    4 × − x   →   4 x neg mult
    2 × (− x + y)   →   2 x neg y add mult
You've got a subtraction when the minus is after a closing parenthesis or after a symbol, i.e. after a variable or number:
    1 − x   →   1 x sub
    (4 ∗ x) − 1   →   4 x mult 1 sub
Take care that the unary operator neg just takes one argument off the stack. If you want to stick with binary operators, you can push a zero before the second operand and use binary sub:
    − (x + y)   →   0 x y add sub
    4 x neg mult   →  4 0 x sub mult
    2 x neg y add mult   →   2 0 x sub y add mult
Finally, you can apply a similar logic to unary plus, which you can just ignore:
    + x   →   x
    + (x + y)   →   x y add

Related

Writing the production rules of this finite state machine

Consider the following state diagram which accepts the alphabet {0,1} and accepts if the input string has two consecutive 0's or 1's:
01001 --> Accept
101 --> Reject
How would I write the production rules to show this? Is it just:
D -> C0 | B1 | D0 | D1
C -> A0 | B0
B -> A1 | C1
And if so, how would the terminals (0,1) be differentiated from the states (A,B,C) ? And should the state go before or after the input? That is, should it be A1 or 1A for example?
The grammar you suggest has no A: it's not a non-terminal because it has no production rules, and it's not a terminal because it's not present in the input. You could make that work by writing, for example, C → 0 | B 0, but a more general solution is to make A into a non-terminal using an ε-rule: A → ε and then
C → A 0 | B 0.
B0 is misleading, because it looks like a single thing. But it's two grammatical symbols, a non-terminal (B) and a terminal 0.
With those modifications, your grammar is fine. It's a left linear grammar; a right linear grammar can also be constructed from the FSA by considering in-transitions rather than out-transitions. In this version, the epsilon production corresponds to final states rather than initial states.
A → 1 B | 0 C
B → 0 C | 1 D
C → 1 B | 0 D
D → 0 D | 1 D | ε
If it's not obvious why the FSM corresponds to these two grammars, it's probably worth grabbing a pad of paper and constructing a derivation with each grammar for a few sample sentences. Compare the derivations you produce with the progress through the FSM for the same input.

Interacting in agda-mode with agda?

It feels super awkward to interact with agda.
Consider the proof state:
_ = begin
5 ∸ 3
≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ { 2 <cursor-goes-here> }0
When I type C-c C-l (type-check), it says
?0 : 2 ∸ 0 ≡ _y_131
_y_131 : ℕ [ at /home/bollu/work/plfa/src/plfa/part1/Naturals.lagda.md:586,5-10 ]
which doesn't seem like a great error? Nor does a refine (C-c C-r) give me a good error message: It only tells me:
cannot refine
How do I get adga to tell me:
You've finished the proof, except for a missing \qed
In general, what is the "preferred mode of interaction" when building proofs?
The overall issue
Your post starts by the following assumption:
It feels super awkward to interact with agda.
The reason that could explain your feeling is that you seem to assume that Agda can both infer a term and its type, in other words, both the property you wish to prove and a proof of it. Agda can often do one of these, but asking for both does not make much sense. As a comparison, imagine being on a bench in a park, when a complete strangers comes and sits next to you, saying nothing. You can see he would very much enjoy to ask you something, but, despite your efforts at making him speak, he remains silent. After a few minutes, the stranger yells at you that, despite him being thirsty, you did not bring the drink he was expected. In this metaphor, the stranger is you, and you are Agda. There is no way you could have known he was thirsty, and even less bring him his drink.
Concretely
You gave the following piece of code:
_ = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ { 2 <cursor-goes-here> }0
This piece of code lacks a type signature which will allow Agda to help you more. Agda tells you so when you type check by providing you with the inferred type of the goal:
?0 : 2 ∸ 0 ≡ _y_131
_y_131 : ℕ [ at /home/bollu/work/plfa/src/plfa/part1/Naturals.lagda.md:586,5-10 ]
Here Agda says that your proof goal is that 2 ∸ 0 is equal to some unknown natural number y. This number being unknown there is very little chance Agda can help you go further in your proof effort because it does not even know what you wish to prove. As far as it knows, your goal could turn out to be 5 ∸ 3 ≡ 3 for wish there exists no proof term.
Getting back to our metaphor, you lacks the statement "I am thirsty". Should the stranger provide this piece of information, you could - possibly - react, which means Agda can try and help.
The solution
I'm assuming you wish to prove that the result of your subtraction is two, in which case the code is as follows:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ {!!}
In this case, you can interact with Agda in various ways, which all lead to Agda providing you with a sound proof term:
You can call Agsy to solve the problem for you (CTRL-c CTRL-a), which leads to:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ refl
You can try and refine the goal directly (CTRL-c CTRL-r), asking Agda if there exists any unique constructor which has the right type, which leads to the same:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ refl
If you wish to wrap up your proof using \qed you can try and input _∎ into the hole after which refining (CTRL-c CTRL-r) gives:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ {!!} ∎
Calling Agsy in the resulting goal naturally gives:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ 2 ∎

Not able to apply guard expression in query list comprehension

I am trying a query list comprehension:
> (set xs '(1 2 3 4 5 6 7 8))
> (lc ((<- x xs) (when (> x 5))) x)
But I am getting the error exception error: undefined function when/1.
Is it possible to apply guard statements to lc?
According to the LFE User Guide, within a list comprehension qualifier, the guard must precede the list expression:
(<- pat {{guard}} list-expr)
This means your example should be written as follows:
lfe> (set xs '(1 2 3 4 5 6 7 8))
(1 2 3 4 5 6 7 8)
lfe> (lc ((<- x (when (> x 5)) xs)) x)
(6 7 8)
You could alternatively treat the greater-than expression as a normal boolean expression qualifier:
lfe> (lc ((<- x xs) (> x 5)) x)
(6 7 8)

How to determine if the language is LL(1)?

P → PL | L
L → N; | M; | C
N → print E
M → print "W"
W → TW | ε
C → if E {P} | if E {P} else {P}
E → (EOE) | V (note: this has a variable O)
O → + | - | *
V → 0 | 1 | 2 | 3 (note: this has a terminal 0 (zero))
T → a | b | c | d
For the above grammar G, is it not LL(1) because it evokes FIRST/FIRST conflict when trying to predict the production of P?
I am really struggling on how to prove that it is not LL(1)...
Any help or advice would be very thankful!
A left-recursive grammar cannot be LL(k) for any k. P → P L is left-recursive.
In addition, L has two productions starting with the same terminal, so it is impossible to choose between them with only one symbol of lookahead.

Is this grammar LL(1)

I have been asked to convert:
S → Sa | bSb | bc
to LL(1) so far I have:
S → bY
Y → SbF | cF
F → aF | ε
Is this LL(1)? If not would this be LL(1):
S → bY
Y → bYbF | cF
F → aF | ε
if neither of these would somebody please give me the correct answer and why thanks in advance!
This is what I would do:
S → Sa | bSb | bc
Remove left recursion:
F -> aF | EPSILON```
Now left factor:
F -> aF | EPSILON
X -> SbF | cF```
Check the First and Follows:
S: b
X: b, c
F: a, EPSILON```
```Follows():
S: $, b
X: $, b
F: $, b```
Everything checks out so it is LL(1) parsable.

Resources