Regular language, L1 and L2 - regular-language

Let there be two languages L1 and L2 with the property that L1 ⊆ L2 and L2 ∈ REG then L1 ∈ REG. I have searched everywhere and I can't find anything, how do you solve this ? Can you please provide ample explanation, thank you,

I'm taking this as the question:
If L1 is a subset of L2, and L2 is regular, does it follow that L1 is regular also?
The answer is no. The proof is by counterexample. Let L2 be the following regular language: all strings over the alphabet. Let L1 be the following subset of L2: any non-regular language of the alphabet. Then L1 is a subset of L2, L2 is regular, and L1 is non-regular.

Related

Why does Coq rename variables in induction?

I'm trying to prove the following theorem.
Theorem subseq_trans : forall (l1 l2 l3 : list nat),
subseq l1 l2 -> subseq l2 l3 -> subseq l1 l3.
Proof.
intros l1 l2 l3 H12 H23. generalize dependent l1. (* here l2 l3 *) induction H23.
- intros. inversion H12. apply empty.
- (* here l1 l2 *) rename l2 into l3. rename l1 into l2. ...
In line 3 after generalize dependent l1. I have the following context:
l2, l3 : list nat
H23 : subseq l2 l3
But in line 5 before renames I have
l1, l2 : list nat
H23 : subseq l1 l2
IHsubseq : ...
So l2 and l3 became l1 and l2. Why did it happen? How can I prevent this from happening?
I don't think it's important, but subseq is defined like this:
Inductive subseq : list nat -> list nat -> Prop :=
| empty l : subseq [] l
| first x l1 l2 (H : subseq l1 l2) : subseq (x :: l1) (x :: l2)
| skip x l1 l2 (H : subseq l1 l2) : subseq l1 (x :: l2).
My Coq version:
The Coq Proof Assistant, version 8.8.2 (January 2019)
It's renamed to l1 and l2 because that's the name given in the definition of subseq. To fix this you can name the variables explicitly when doing the induction:
induction H23 as [ | ? l2 l3 | ].
Here there are 3 cases for subseq, so you need 3 branches. I've only named the lists for the first case, so I've left the names blank for the other cases and used ? once, both of which which tell Coq to use the default naming.

Find out which is a regular language

My Argument/Answer is if y is aregular set then there exits a DFA which accepts y. In the L1 there is a condition that y=x^n, that x will belong to L1,as y is accepted by DFA. So is x^n and so is x so L1 is regular. Now L2 --> here the condition is x=y^n. Here y is accepted by DFA so is y^n so which is equal to x so x can be accepted by DFA. This makes both L1,L2 regular
Is my argument right?
This question seems like it's poorly-posed. For example, if we take A = {a}, then L1 is the language {a} and L2 is the language a*, both of which are regular. If we pick A = a*b, then L1 = a *b (which is regular) and L2 = { (anb)m | m, n ≥ 0 }, which is not regular (using the pumping lemma). In other words, the answer depends on the choice of A.

Haskell, combining Do and if

Hi I was wondering whether there was a way to use do if in haskell in this kind of way
doIfFunction :: Eq a => [a]->[a] ->[a]
doIfFunction l1 l2 = do
if l /= something then (l2++something) else l2
if l /= something2 then (l2++something2) else l2
l2
Basically if this something function returns a different value I want it to add it to l2 then return l2. I keep getting l2 as being empty at the end and it shouldn't be, is this because of the else l2 does it reset the value of l2 to what it was at the start?
If so could I do something like this?
doIfFunction :: Eq a => [a]->[a] ->[a]
doIfFunction l1 l2 = do
if l /= something then (let l2 = l2++something) else l2
if l /= something2 then (let l2 = l2++something2) else l2
l2
This gives errors, but I was wondering if this is on the right lines.
When the doIfFunction is being called it will always be doIfFunction l [] where l contains values and [] is empty
Generally, you should avoid to think about modifying anything in Haskell. In fact, you can't, the language doesn't allow it. At all†!
Instead of modifying the list l2 itself, you think about creating additional lists which are modified versions of l2. (The immediate benefit is that you can still use the original l2 anywhere else; in particular it's fine if some other function / thread still needs this old version and you weren't aware of it.)
I.e., instead of
do
modify_something_about_l2
modify_something_else_about_l2
yield_some_result
you want to just evaluate the result of a function-application chain, like
f l2 = some_other_modification (some_modification (l2))
or, as we prefer to write it,
f = some_other_modification . some_modification
In your particular case:
doIfFunction l1
= (\l2 -> if l /= something2 then (l2++something2) else l2)
. (\l2 -> if l /= something then (l2++something) else l2)
If you don't like the “backwards style“ you can also replace the composition operator . with its flipped version:
import Control.Arrow
doIfFunction l1
= (\l2 -> if l /= something then (l2++something) else l2)
>>> (\l2 -> if l /= something2 then (l2++something2) else l2)
Also, you can, via eta-reduction, avoid mentioning the intermediate lists at all:
doIfFunction l1
= (if l /= something then (++something) else id)
>>> (if l /= something2 then (++something2) else id)
That said...
You're probably thinking: it's mighty inefficient to always create new modified versions of everything, instead of just modifying in-place. Well, sometimes it is, often it is actually no problem at all.
You example is in fact one where it is a problem: to append something to l2, a copy of the entire list needs to be made. Maybe you can avoid this problem very easily: if instead of appending, you prepend:
doIfFunction l1
= (if l /= something then (something++) else id)
>>> (if l /= something2 then (something2++) else id)
then there's no performance penalty. The reason being: a list is just a chain of elements (heads), each with references to the rest of the list. Pre-pending an element is just a matter of making a new head and linking it to the already existing list!
†Even with clever lazy prepending, sometimes the pure-functional style is significantly more time-expensive. Don't worry: although Haskell does not allow modifying values, there are types which encapsulate the concept of destructive modifications, namely the ST monad. So you can still implement algorithms that need destructive updates efficiently, but basically you don't do it in Haskell but in an embedded “domain-specific imperative language”, that integrates seamlessly with Haskell.
Just put the if clause in the let statement:
doIfFunction :: Eq a => [a] -> [a] -> [a]
doIfFunction l1 l2 = do
let l2' = if l /= something then l2 ++ something else l2
let l2'' = if l /= something2 then l2' ++ something2 else l2'
l2''
But in this case you don't really need the do notation since you don't use any monadic operation. I believe you want to use the do notation to simulate imperative-style programming. Consider using a where if you think it's more readable:
doIfFunction :: Eq a => [a] -> [a] -> [a]
doIfFunction l1 l2 = result
where newL2 = if l /= something then l2 ++ something else l2
result = if l /= something2 then newL2 ++ something2 else newL2
Alternatively, when you will be more familiar with Haskell, you could use leftaroundabout's answer (that would be the most idiomatic way to write what you want to do).

Is the union of regular languages regular?

If the langages L1,...,Ln are regular, is the union of them regular too?
We know that the union of two regular languages is a regular language. How to prove that the union of many regular languages also is regular?
You can use induction. Here's a very very rusty sketch of the proof.
Given -
Union of two regular languages is regular.
Let f(n) be a function representing the union of n regular languages.
Question
Is f(n) a regular language?
Base Case -
if n = 1, then the union of a single regular language is regular.
if n = 2, then, by the given hypothesis, we know that f(2) is regular.
Inductive Hypothesis-
Assume for all n <= k that f(n) is regular.
Inductive Step -
Let n = k+1. We know by the inductive hypothesis that f(k) is a regular language. So . . .
f(n) = f(k+1) = Lk+1 U f(k)
Where Lk+1 is the k+1st regular language. Since f(k) and Lk+1 are regular, then f(n) = f(k+1) is regular by the given hypothesis.
QED
For More on Inductive Proofs
Wiki - http://en.wikipedia.org/wiki/Mathematical_induction
Khan Academy - https://www.khanacademy.org/math/precalculus/seq_induction/proof_by_induction/v/proof-by-induction

Are those two regular languages the same?

Given an alphabet of {a, b} where Na denotes the number of occurrences of a, and Nb the number of occurrences of b:
L1 = {xy | Na(x) = Nb(y)}
L2 = {w | Na(w) and Nb(w) are even number}
Wouldn't a single DFA with four states and using mod be able to accept both languages?
No, because both languages are different so you can't draw single DFA for both languages.
An automaton uniquely defined a language, but yes of-course for a language more than one automata are possible called 'equivalent automata'.
Language L1 = A = {xy | Na(x) = Nb(y)} is a regular language. Regular expression for this language is:
(a + b)*a(a + b)*b(a + b)* + ^
To understand this language and regular expression read: "Show that the following set over {a, b} is regular".
Language L2 = A = {w | Na(w) and Nb(w) are even number} is also a regular language. Regular expression for this language is:
((a + b(aa)*ab)(bb)*(ba(aa)*ab(bb)*)*a + (b + a(bb)*ba)(aa)*(ab(bb)*ba(aa)*)*b)*
To understand this language and regular expression read: "Need Regular Expression for Finite Automata".
But both languages are not equal because there are some strings in language L1 those are not belongs to language L2 e.g. ab is a string in L1 but doesn't not consist of even number of a and b hence doesn't belongs to language L2.
Note: Language L2 is either not a subset of language L1, because in L2 a strings of even length and single symbol is possible like aa, aaaa, bb, bbbb but these strings are not member in L1.
Both languages are different hence single DFA is not possible for both languages.
Both the languages L1 = {xy | Na(x) = Nb(y)} and
L2 = {w | Na(w) and Nb(w) are even number}are different so we cannot draw a single DFA for both languages.
For Language L1 :
A = {xy | Na(x) = Nb(y)} is a regular language.
Regular expression for this language is:
(a + b)*a(a + b)b(a + b)
Language L2 :
A = {w | Na(w) and Nb(w) are even number} is also a regular language. Regular expression for this language is:
((a + b(aa)ab)(bb)(ba(aa)ab(bb))*a + (b + a(bb)ba)(aa)(ab(bb)ba(aa))b)
Both languages are not equal because there are some strings in language L1 which doesnt belong to language L2. ab is a string in L1 but doesn't not consist of even number of a and b hence doesn't belongs to language L2.
As Both languages are different,single DFA cannot be constructed that accepts both the languages.

Resources