Is this a valid regular language expression for a binary string? Can it be condensed into something more concise? - regular-language

The regular expression is:
(10) U ((10(0 U 1))*10)
The language I am trying to write is this:(q2 is the accept state and {0,1} is the langauge)
(q0,0)=q3
(q0,1)=q1
(q1,0)=q2 — accept state
(q1,1)=q3
(q2,0)=q0
(q2,1)=q0
(q3,0)=q3
(q3,1)=q3

Related

Is L = {ww^Ru | w, u ∈ {0,1}+} regular language?

let L = {wwRu | w, u ∈ {0,1}+}. Is L regular language ? Note that w, u cannot be empty.
I've tried to prove it is not regular language by the pumping lemma, but I failed when w = 0^p1^p, 01^p, (01)^p. Once I take y = 0^p or 1^p, xyyz will be 00.../11.../01^n0... etc.
And I cannot draw its DFA/NFA or write its regular expression to prove it is regular language.
So is L regular or not ? How can I prove it ?
The language is not regular, and we can prove it using the Myhill-Nerode theorem.
Consider the sequence of strings 01, 0101, ..., (01)^n, ...
First, notice that none of these strings are in the language. Any prefix of any of these strings which has even length is of the form (01)^2m for some m, and therefore just a shorter string in the sequence; splitting such a prefix in two either has both substrings start with 0 and end with 1, or else it has the first substring start and end with 0 and the second start and end with 1. In either case, these strings are not of the form w(w^R)u for any w or u.
Next, notice that the shortest possible string which we can append to any of these strings, to produce a string in the language, is always the reverse of itself followed by either 0 or 1. That is, to turn 01 into a string in the language, we must append 100 or 101; there are no shorter strings we can append to 01 to get a string in the language. The same holds true for 0101: 10100 and 10101 are the shortest possible strings that take 0101 to a string in L. And so on for each string of the form (01)^n.
This means that each string of the form (01)^n is distinguishable with respect to the target language w(w^R)u. The Myhill-Nerode theorem tells us that a minimal DFA for a regular language has exactly as many states as there are equivalence classes under the indistinguishability relation. Because we have infinitely many distinguishable strings with respect to our language, a minimal DFA for this language must have infinitely many states. But, a DFA cannot have infinitely many states; this is a contradiction. This means that our language cannot be regular.
The language is REGULAR:
L = 00(0+1)+ + 11(0+1)+ + 0(11)+0(0+1)+ + 1(00)+1(0+1)+

Statements vs Expressions in Haskell, Ocaml, Javascript

In Haskell, afaik, there are no statements, just expressions. That is, unlike in an imperative language like Javascript, you cannot simply execute code line after line, i.e.
let a = 1
let b = 2
let c = a + b
print(c)
Instead, everything is an expression and nothing can simply modify state and return nothing (i.e. a statement). On top of that, everything would be wrapped in a function such that, in order to mimic such an action as above, you'd use the monadic do syntax and thereby hide the underlying nested functions.
Is this the same in OCAML/F# or can you just have imperative statements?
This is a bit of a complicated topic. Technically, in ML-style languages, everything is an expression. However, there is some syntactic sugar to make it read more like statements. For example, the sample you gave in F# would be:
let a = 1
let b = 2
let c = a + b
printfn "%d" c
However, the compiler silently turns those "statements" into the following expression for you:
let a = 1 in
let b = 2 in
let c = a + b in
printfn "%d" c
Now, the last line here is going to do IO, and unlike in Haskell, it won't change the type of the expression to IO. The type of the expression here is unit. unit is the F# way of expressing "this function doesn't really have result" in the type system. Of course, if the function doesn't have a result, in a purely functional language it would be pointless to call it. The only reason to call it would be for some side-effect, and since Haskell doesn't allow side-effects, they use the IO monad to encode the fact the function has an IO producing side-effect into the type system.
F# and other ML-based languages do allow side-effects like IO, so they have the unit type to represent functions that only do side-effects, like printing. When designing your application, you will generally want to avoid having unit-returning functions except for things like logging or printing. If you feel so inclined, you can even use F#'s moand-ish feature, Computation Expressions, to encapsulate your side-effects for you.
Not to be picky, but there's no language OCaml/F# :-)
To answer for OCaml: OCaml is not a pure functional language. It supports side effects directly through mutability, I/O, and exceptions. In many cases it treats such constructs as expressions with the value (), the single value of type unit.
Expressions of type unit can appear in a sequence separated by ;:
let s = ref 0 in
while !s < 10 do
Printf.printf "%d\n" !s; (* This has type unit *)
incr s (* This has type unit *)
done (* The while as a whole has type unit *)
Update
More specifically, ; ignores the value of the first expression and returns the value of the second expression. The first expression should have type unit but this isn't absolutely required.
# print_endline "hello"; 44 ;;
hello
- : int = 44
# 43 ; 44 ;;
Warning 10: this expression should have type unit.
- : int = 44
The ; operator is right associative, so you can write a ;-separated sequence of expressions without extra parentheses. It has the value of the last (rightmost) expression.
To answer the question we need to define what is an expression and what is a statement.
Distinction between expressions and statements
In layman terms, an expression is something that evaluates (reduces) to a value. It is basically something, that may occur on the right-hand side of the assignment operator. Contrary, a statement is some directive that doesn't produce directly a value.
For example, in Python, the ternary operator builds expressions, e.g.,
'odd' if x % 2 else 'even'
is an expression, so you can assign it to a variable, print, etc
While the following is a statement:
if x % 2:
'odd'
else:
'even'
It is not reduced to a value by Python, it couldn't be printed, assigned to a value, etc.
So far we were focusing more on the semantical differences between expressions and statements. But for a casual user, they are more noticeable on the syntactic level. I.e., there are places where a statement is expected and places where expressions are expected. For example, you can put a statement to the right of the assignment operator.
OCaml/Reason/Haskell/F# story
In OCaml, Reason, and F# such constructs as if, while, print etc are expressions. They all evaluate to values and can occur on the right-hand side of the assignment operator. So it looks like that there is no distinction between statements and expressions. Indeed, there are no statements in OCaml grammar at all. I believe, that F# and Reason are also not using word statement to exclude confusion. However, there are syntactic forms that are not expressions, for example:
open Core_kernel
it is not an expression, definitely, and
type students = student list
is not an expression.
So what is that? In the OCaml parlance, they are called definitions, and they are syntactic constructs that can appear in the module on the, so called, top-level. For example, in OCaml, there are value definitions, that look like this
let harry = student "Harry"
let larry = student "Larry"
let group = [harry; larry]
Every line above is a definition. And every line contains an expression on the right-hand side of the = symbol. In OCaml there is also a let expression, that has form let <v> = <exp> in <exp> that should not be confused with the top-level let definition.
Roughly the same is true for F# and Reason. It is also true for Haskell, that has a distinction between expressions and declarations. It actually should be true to probably every real-world language (i.e., excluding brainfuck and other toy languages).
Summary
So, all these languages have syntactic forms that are not expressions. They are not called statements per se, but we can treat them as statements. So there is a distinction between statements and expressions. The main difference from common imperative languages is that some well-known statements (e.g., if, while, for) are expressions in OCaml/F#/Reason/Haskell, and this is why people commonly say that there is no distinction between expressions and statements.

What is the Set defined by this Regular Expression?

I am trying to go through regular expression and language questions however, this one seems to have gotten me stuck.
Can somebody help?
I am trying to write out the set that is defined by this regular expression:
To understand this regular expression, lets consider its three parts separately:
( a | Ɛ ) abb (a | b)
\---1---- --2--- ---3---
this regular expression is defined in three groups/parts using parenthesis
Part-1: Ɛ is a null symbol in regular expression, if it appears with some other symbol (or a group of symbols) with union operator | that means that symbol(or group) is option e.g. can be appear or not appear in some strings of language ( Ɛ symbols in FA as edge label defines 'null-transition' — which allows a transformation to a new state without consuming any input symbols).
In your regular expression, first 'a' is written with Ɛ — ( a | Ɛ ) so it is option - it can appear in some string or absent in other. Hence strings generated with using this regular expression either starts with two 'a' or one 'a'.
Part-2: Sub-string 'aab' always appears in all possible string using this regular expression.
so strings can be in two possible forms:
aabb(a|b)
abb(a|b)
Part-3: (a | b) string either ends with symbol 'a' or symbol 'b'.
if both above forms ends with 'a'
aabba
abba
if both above forms ends with 'b'
aabbb
abbb
Of-course it is a finite language and its DFA does not contain any loop. Its DFA for this language { aabba, abba, aabbb, abbb } would be as following:

Is this language regular? {0^n 1^m | m != n}, I don't understand the direct proof by pumping length

There is a direct way to prove it: If p is the pumping length and we take the string s = 0p1p+p!, then no matter what the decomposition s = xyz is the string xy1+p!/|y|z will equal 0p+p!1p+p! which is not in the language.
I don't understand the value y given here.
y is some substring that can be "pumped" - repeated * times - and still keep the language regular. Basically, we have to find a loop in there somewhere, and that loop is what y represents.
Basically, if the language is of the form 0m1m! (m zeroes followed by m! ones) then there can't be a loop in there.
In this case, y represents "the hypothetical pump string for the subset language {0m1m!}" - hypothetical because it cannot exist! Clearly, no pumping is possible for this smaller language, since repetition will take us out of the language immediately. (consider the example 00111111 - can we find a pump string for this?) Therefore, we have a special case of the language which is not regular, therefore the language generally is not regular. (though it certainly contains special cases which are regular, but this is not in dispute)

design NFA which accepts specific length of strings

Im looking forward to design a FA which accepts some kind of string that accept some A and B.
First a string which the number of A is five more times higher than B.
i mean L={w∈{A,B}* and (nA(W)-nB(W) mod 5=0)
And also a FA which accept different number of character in a string:
L={A^n B^m C^k | n,k>0 and m>3}
I design some FAs But they did not work perfectly on this complicated strings.
Any help on how should i design this ?
Unfortunately, your questions are confusing as the english text doesn't agree with the mathematical formula. I will try to answer to these four questions then:
A language which consists of string over {a,b} that the number of a (= #a(w))
is five times as the number of b ( #b(w)),
L = { w in {a,b}* : #a(w)>#b(w) and #a(w)=#b(w)mod5 }
This cannot be done by an NFA. The proof is simple by using the pumping lemma (P.L) with the string a^pb^5p, where p is the constant of P.L.
For the language: L={w∈{A,B}* : (nA(W)-nB(W)) mod 5=0} that you wrote,
you can do it with an DFA that consists of a cycle of 5 states.
The transitions are, if you read a go clockwise if you read b go counter-clocwise. Choose at random one state to be initial state and the same state will be the final state.
For the language L={A^n B^m C^k | n,k>0 and m>3}, it should be easy to find out
if you read L as L=A(A)* B(B)* c^4(C)*
For the language that accepts different number of character in the string (let's say over a,b). The language should be R={ w in {a,b}* : #a(w) not equal #b(w)}
This language again it cannot be recognized by an NFA. If this language was regular (recognzied by an NFA) so would be this language:
L=a*b* intersection (R complement). The language L is {a^n b^n/ n non-negative integer}.
The language L is the first example of most books when they speak about languages that are non-regular.
Hopefully, you will find this answer helpful.

Resources