Construct the NPDA for the language - automata

Construct the NPDA for the language:
L={w:w∈{a,b}^*,the number of a' s is at least the number of b' s }

Our strategy can be this:
input a, stack a/Z: push a
input a, stack b: pop
input b, stack a: pop
input b, stack b/Z: push b
accept if no additional input and stack a/Z
Why does this work? If we have more a than b, we end up with a on the stack. If the numbers of a and b are the same, we end up with Z on the stack. If there are more b than a, we end up with b on the stack. So we accept either a or Z on top when the input is exhausted.
q e s q' s'
q0 a Z q0 aZ
q0 a a q0 aa
q0 a b q0 -
q0 b Z q0 bZ
q0 b a q0 -
q0 b b q0 bb
q0 - a q1 a
q0 - Z q1 Z
q1 - a q1 -
This PDA ends in q1 with an empty stack if the input string has at least as many a as b.

L = {w:w∈{a,b}^*}
So here in this case the Stack would start with z and then afterwards pushing the symbol 'a' and then popping the symbol 'b'.
And, also the minimal string would be string 'ab';
So the transitions function in this case would be as follows:
δ(q0, a, z) = (q0, az) => a,z/az
δ(q0, b, a) = (q1, ε) => b,a/ε
δ(q1, ε, z) = (q2, z) => ε/z
Here the state q0 would be the start state and state q2 would be the final state.

Related

Why cannot define function of type 'Set -> Set' in Agda?

Assume there are four types , D, Q, P, C
data Q : Set where
q1 : Q
q2 : Q
data P : Set where
p1 : P
p2 : P
data C : Set where
c1 : C
c2 : C
data D : Set where
d1 : D
d2 : D
When trying to define function
f : Set -> Set
f D = Q
f P = C
I get the warning unreachable clause .
I assume it is because domains of Agda function are defined on sets but not category of sets.
What if I want a mapping relation which behaves like a close type family in Haskell ?
Because the Agda compiler erases types during compilation, it is not allowed to pattern match on Set directly. (The error 'unreachable clause' is a bit confusing but it results from Agda interpreting D as a pattern variable rather than the datatype.)
The usual way to work around this problem is to define your own universe, i.e. a datatype whose elements are interpreted as specific sets. Here is an example:
data Q : Set where
q1 : Q
q2 : Q
data P : Set where
p1 : P
p2 : P
data C : Set where
c1 : C
c2 : C
data D : Set where
d1 : D
d2 : D
-- Our little universe
data U : Set where
d : U
p : U
-- The interpretation function
⟦_⟧ : U → Set
⟦ d ⟧ = D
⟦ p ⟧ = P
f : U → Set
f d = Q
f p = C
-- An example of how to use f:
omo : {u : U} → ⟦ u ⟧ → f u
omo {d} d1 = q1
omo {d} d2 = q2
omo {p} p1 = c1
omo {p} p2 = c2

Construct a new DFA B from A where L(B) = L(A) - {w | w∈E* }

Im having a bit of difficulty with the following question:
Given a DFA A = (E = {a,b,c} , Q ,q0 ,F , l) (where l is the transition function), build a new DFA B such that L(B) = L(A) - {a}.
Now I understand that Eb = Ea, but how can I define B transition function, or accepting states, without knowing the accepting states of A?
Thank you.
First, let us construct a DFA called C which accepts w. This DFA has |w| + 2 states: one initial state, one dead state, one accepting state.
Second, let us construct a DFA called D which accepts everything except w. Simply change all non-accepting states to accepting, and vice versa.
Third, let us construct B using the Cartesian Product Machine construction on input DFAs A and D with intersection as the operator. This DFA will hav |Q| x (|w| + 2) states in total.
The language of B is everything accepted by A and D simultaneously. D accepts anything that isn't w; so, B accepts anything in L(A) that isn't w, as required.
EDIT: Some more detail about what B ends up looking like.
Let the states of A be QA and the states of D be QD. Let the accepting states of A be FA and the accepting states of D be FD. Let dA be the transition function for A and dD be the transition function for D. From our construction above, we have:
Q = {(x, y) for x in QA y in QD}
E = the same alphabet as A and D, assumed to be the same. If w contains only symbols in A's alphabet then just use A's alphabet. If w contains symbols not in A's alphabet then w is not in L(A) and we can just let B = A.
q0 = (q0, q0')
F = {(x, y) in Q | x in FA and y in FD}
d((x, y), s) = (dA(x, s), dD(y, s))
As for what D looks like:
QD = {q0', q1', …, q(|w|+1)'}
E = same as A's
q0' is the initial state
FD = QD \ {q(|w|)}
d(qi, s) = q(i+1) if s is the (i+1)th symbol of w, or q(|w|+1) otherwise

why this code is not working in agda?

I am trying to prove commutative property over natural number on multiplication operation.
--proving comm over *
*comm : ∀ a b → (a * b) ≡ (b * a)
*comm zero b = sym (rightId* b)
*comm (suc a) b = {!!}
when i check goal I found that it is b + a * b ≡ b * suc a. So i proved this.
lemma*-swap : ∀ a b → a + a * b ≡ a * suc b
Now when i tried :
*comm : ∀ a b → (a * b) ≡ (b * a)
*comm zero b = sym (rightId* b)
*comm (suc a) b = lemma*-swap b a
This should work as it satisfied the goal but why this is not working?? Please suggest me where I am wrong.
b + a * b (the expression in the goal) and a + a * b (the expression in lemma*-swap) are distinct so applying lemma*-swap does not satisfy the goal.
You need to rewrite the induction hypothesis *comm a b to turn a * b into b * a in the goal so that the expression lemma*-swap b a can be used to discharge the goal.

semantic web rule use "all"

Assume that I have the following statements:
A p B, A p C, B p C ( p is a symmetric property, i.e. B p A, C p A and C p B)
A v 2, B v 1, C v 1,
I want to use a rule to do something like:
?a p all(?b)
if ?b v 1
than ?a q 'Yes'
that means that you can infer (A q 'Yes'), but B can't since B p A and A v 2(although B p C and C v 1).
[rule: (?a eg:p ?b), (?b eg:v 1) -> (?a eg:q 'Yes')]
I've used the above rule in Jena, but I got A,B,C eg:q 'Yes', which is wrong.
Any help will be greatly appreciated.
Update (originally posted as an answer)
the meaning of (?a p all(?b)) is that I like to get a set which all ?mem in this set fulfill the (?a p ?mem). And all member must fulfill (?mem v 1) to infer (?a q 'Yes').
For example,
A p B and A p C,so I get a set which contains (B, C).since B and C v 1,so A q 'Yes.
B p A and B p C,so I get a set(A, C),but A v 2,so can't infer that B q 'Yes'.
Problem Solved
Thanks to Joshua Taylor.
Firstly, these two rules can't use at the same time.The rule2 should be used after rule1.
And, the rule2 should be [rule2: (?s ?p ?o) noValue(?s, connectedToNonOne) -> (?s q 'Yes')].
but I got A,B,C eg:q 'Yes', which is wrong.
The rule you have actually written in Jena says
For any two individuals X and Y, if (X p Y) and (Y v 1) then (X q 'Yes').
From the rule you've written, this is correct, by:
(A p C), (C v 1) → (A q 'Yes')
(B p C), (C v 1) → (B q 'Yes')
(C p B), (B v 1) → (C q 'Yes')
What you're actually trying to say is:
For any individual X, if for every individual Y, (X p Y) implies (Y v 1), then (X q 'Yes').
In first order logic, your original rule could be written as:
∀ x,y ([p(x,y) ∧ v(y,1)] → q(x,'yes')
What you're actually trying to capture would be:
∀x[(∀y[p(x,y) → v(y,1)]) → q(x,'yes')]
That's harder to capture in Jena rules, because to check whether (∀y[p(x,y) → v(y,1)]) holds or not, all Jena can do is check whether there are currently any counterexamples. If one were added later, you might have incorrect inferences.
Using the builtins available in the rule reasoner, you could do something with noValue and notEqual along the lines of:
#-- If an individual is disqualified by being
#-- connected to a something that is connected
#-- to something that is not equal to 1, then
#-- add a connectedToNonOne triple.
[rule1:
(?x p ?y), (?y v ?z), notEqual(?z,1)
->
(?x connectedToNonOne true)]
#-- Mark everything that is *not* disqualified
#-- with `q 'Yes'`.
[rule2:
noValue(?x, connectedToNonOne)
->
(?x q 'Yes')

Follow sets Top-Down parsing

I have a question for the Follow sets of the following rules:
L -> CL'
L' -> epsilon
| ; L
C -> id:=G
|if GC
|begin L end
I have computed that the Follow(L) is in the Follow(L'). Also Follow(L') is in the Follow(L) so they both will contain: {end, $}. However, as L' is Nullable will the Follow(L) contain also the Follow(C)?
I have computed that the Follow(C) = First(L') and also Follow(C) subset Follow(L) = { ; $ end}.
In the answer the Follow(L) and Follow(L') contain only {end, $}, but shouldn't it contain ; as well from the Follow(C) as L' can be null?
Thanks
However, as L' is Nullable will the Follow(L) contain also the Follow(C)?
The opposite. Follow(C) will contain Follow(L). Think of the following sentence:
...Lx...
where X is some terminal and thus is in Follow(L). This could be expanded to:
...CL'x...
and further to:
...Cx...
So what follows L, can also follow C. The opposite is not necessarily true.
To calculate follows, think of a graph, where the nodes are (NT, n) which means non-terminal NT with the length of tokens as follow (in LL(1), n is either 1 or 0). The graph for yours would look like this:
_______
|/_ \
(L, 1)----->(L', 1) _(C, 1)
| \__________|____________/| |
| | |
| | |
| _______ | |
V |/_ \ V V
(L, 0)----->(L', 0) _(C, 0)
\_______________________/|
Where (X, n)--->(Y, m) means the follows of length n of X, depend on follows of length m of Y (of course, m <= n). That is to calculate (X, n), first you should calculate (Y, m), and then you should look at every rule that contains X on the right hand side and Y on the left hand side e.g.:
Y -> ... X REST
take what REST expands to with length n - m for every m in [0, n) and then concat every result with every follow from the (Y, m) set. You can calculate what REST expands to while calculating the firsts of REST, simply by holding a flag saying whether REST completely expands to that first, or partially. Furthermore, add firsts of REST with length n as follows of X too. For example:
S -> A a b c
A -> B C d
C -> epsilon | e | f g h i
Then to find follows of B with length 3 (which are e d a, d a b and f g h), we look at the rule:
A -> B C d
and we take the sentence C d, and look at what it can produce:
"C d" with length 0 (complete):
"C d" with length 1 (complete):
d
"C d" with length 2 (complete):
e d
"C d" with length 3 (complete or not):
f g h
Now we take these and merge with follow(A, m):
follow(A, 0):
epsilon
follow(A, 1):
a
follow(A, 2):
a b
follow(A, 3):
a b c
"C d" with length 0 (complete) concat follow(A, 3):
"C d" with length 1 (complete) concat follow(A, 2):
d a b
"C d" with length 2 (complete) concat follow(A, 1):
e d a
"C d" with length 3 (complete or not) concat follow(A, 0) (Note: follow(X, 0) is always epsilon):
f g h
Which is the set we were looking for. So in short, the algorithm becomes:
Create the graph of follow dependencies
Find the connected components and create a DAG out of it.
Traverse the DAG from the end (from the nodes that don't have any dependency) and calculate the follows with the algorithm above, having calculated firsts beforehand.
It's worth noting that the above algorithm is for any LL(K). For LL(1), the situation is much simpler.

Resources