L={a^i b^j c^k d^l | i=k and j=l} i couldn't find the grammar of the given language - regular-language

I have tried
S-A|B
A-aCcD|aAc|ac
B-bBd|bd
C-b
D-d
this to only to output ac and bd's but I can't put b's between a and c's.

Consider the string a^p b^p c^p d^p. This string is surely in the language. If the language were regular, and there existed a regular grammar for it, then it would satisfy the requirements of the pumping lemma for regular languages: namely, the string given above could be written as uvx where |uv| <= p, |v| > 0 and for all natural numbers n, u(v^n)x is also in the language. There are seven cases for what v contains: just a; a and b; just b; b and c; just c; c and d; or just d. In any case, pumping v will change the number of instances of one kind of symbol but not the number of instances of the corresponding symbol (the one the language requires be exactly as frequent). So, the language cannot be regular.
Similarly, the pumping lemma for context-free languages would require that the string given above be written as uvxyz where |vxy| <= p, |vy| > 0 and for all natural numbers n, u(v^n)x(y^n)z is also a string of the language. We get exactly the same cases here as above and reach exactly the same conclusion: the language cannot be context-free.
Given all this, we might as well just target an unrestricted grammar (it might be possible to get a context-sensitive grammar for this language, but that is not requested). We want the same number of a and c, and the same number of b and d, and we want them to be in alphabetical order. We can start out like this:
S -> ACS | BDS | T
This lets us get strings with the same number of A as C, and the same number of B as D, ending with T. Next, we need some rules to allow putting the symbols A, B, C and D in the right order:
BA -> AB
CA -> AC
DA -> AD
CB -> BC
DB -> BD
DC -> CD
Finally, we need a way to convert the non-terminals A, B, C and D to terminals a, b, c and d in such a way that it can't work unless they're all in order. We can use T (and a few other symbols to be introduced soon) to sweep from right to left, converting as we go:
DT -> Td
CT -> Uc
BT -> Vb
AT -> Wa
T -> e
CU -> Uc
BU -> Vb
AU -> Wa
U -> e
BV -> Vb
AV -> Wa
V -> e
AW -> Wa
W -> e
Nonterminals T, U, V and W allow converting A-D, A-C, A-B and A to a-d, a-c, a-b and a, respectively. We can also get rid of this marker symbol at any time. The only way to eliminate all nonterminals and get a string of terminals (that is, to generate a string according to this grammar) is to sweep from right to left, converting all encountered symbols, and finally use whichever of the X -> e rules is needed to eliminate the marker. This can only work if the symbols were converted in descending order from right to left, so they must be in ascending order from left to right, as required. Because the productions for S introduce A, C and B, D in matched pairs, that requirement is satisfied as well.

Related

Let L be a language over {a,b} that contains the same number of occurrences of a and b. Which of the following language is regular?

(https://i.stack.imgur.com/lU278.png)
Why option C is also not correct?
Let's evaluate each one in turn. Let L be the language over {a, b} with #a = #b.
(a) L intersect ab. This is the language of strings of the form a^n b^n. This is not regular by the Myhill-Nerode theorem since each string a^k has a unique shortest string b^k which can be appended to get a string in the language, and there are clearly infinitely many such cases, so there can be no finite automaton for the language.
(b) (L intersect ab) union ab. This is regular because (L intersect ab) is clearly a subset of ab, but the union of a subset with any of its supersets is the superset; thus, (L intersect ab) union ab = ab which is regular since it has just three unique equivalence classes: those strings which can be followed by ab to get a string in the language; those that can only be followed by b* to get a string in the language; and those strings which cannot be followed by anything to get a string in the language.
(c) L union ab. This is not regular because strings of the form b^(k+1)a have unique shortest strings a^k which can be appended to get a string in the language, and there are clearly infinitely many such cases, so there can be no finite automaton to cover them all.
(d) (L intersect ab) union ba. This is not regular because strings of the form a^(k+1)b have unique shortest strings b^k which can be appended to get strings in the language, and there are infinitely many such cases, so no finite automaton can cover them all.

why there are |b|^|a| inhabitants in the function a->b.how it comes? why it is not just "a" inhabitants

why there are |b|^|a| inhabitants in the function a->b
I think it should be "a" inhabitants in it as a is any type. Can't understand why it is not.
Let's talk about a specific example, namely functions on the two specific types below:
data Three = A | B | C
data Four = OneFish | TwoFish | RedFish | BlueFish
A function of type Three -> Four can be fully specified by choosing one of the four possible values of type Four for each of the inputs A, B, and C; here's an example:
f A = TwoFish
f B = BlueFish
f C = OneFish
Here's another example; it's just like f except that it makes a different choice for C:
g A = TwoFish
g B = BlueFish
g C = BlueFish
Since there are four choices of output for A, four choices of output for B, and four choices of output for C, there are 4*4*4 = 64 possible choices of functions.
Now we can generalize a little bit and talk about total functions on other finite input types. As before, a function from a to b can be fully specified by telling which b it chooses for each input a. Where before we had 4 possible outputs for each input, there are now |b|-many possibilities for each of these choices. As before, since the choices are completely independent, we must multiply |b| by itself once for each possible input -- that is, |a|-many times. That is, we have |b|^|a| possible complete specifications.

What's the most efficient way to represent finite (non-recursive) algebraic type values?

What's the most efficient way to serialize finite (non-recursive) algebraic-data-types which are comprised only of constructors?
e.g.
p = A
| B q
q = C
| D r
| E
r = F
| G
Manually enumerating all valid combinations for this trivially small definition is possible:
A 0x00
B C 0x01
B D F 0x02
B D G 0x03
B E 0x04
Is there any broader theory here?
How about if we then add non-constructor types, such as ints, etc.?
How does Haskell represent these in memory (it allows recursion, so pointers/references will probably be necessary)?
There's no completely standard class that does this, but it's pretty easy to make one yourself. I'll sketch one way of doing this:
data P = A | B Q deriving Show
data Q = C | D R | E deriving Show
data R = F | G deriving Show
class Finite a where
allValues :: [a]
instance Finite P where
allValues = [A] ++ map B allValues
instance Finite Q where
allValues = [C] ++ map D allValues ++ [E]
instance Finite R where
allValues = [F] ++ [G]
I've written the instances this way to show that it's very easy and mechanical and could be done by a program (e.g. with generic programming or with Template Haskell). You could also add an instance to do some legwork for you, provided the type is Bounded and Enumerable:
instance (Bounded a, Enum a) => Finite a where
allValues = [minBound..maxBound]
If you now add deriving (Bounded, Show) to R, that's one less instance to write!
Anyway, now we can evaluate allValues :: [P] and get back [A,B C,B (D F),B (D G),B E] - which you can then zip with [0..] to get your encoding and so on.
But surely this has been done before! I'm not using serialization much (if ever), but a quick search shows that the binary package and the binary-derive package can do something similar for you, without having to write the instances yourself. I would see if those do what you want first.
As for in-memory haskell representations, we can't represent things fully packed typically since the structures are lazy, and that means we need an indirection at each level. That said, unpacking will let you crush these things together. But, as far as I know, it won't pack bits from nested constructors into the same word.
There is a pointer-tagging optimization that shoves some information about the constructor in the pointer that directs to it: http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/HaskellExecution/PointerTagging
For more on unpacking see this: http://www.haskell.org/haskellwiki/Performance/Data_types#Unpacking_strict_fields

Haskell starter questions... please explain it to me

I am supposed to write some haskell program but I don't really know where to start. I will be really really grateful if you can point me to some resources to read or explain me the question. I am sure this is something totally amateurish, but I really need a starting point.
data DFA q o = DFA (q -> o -> q) q [q]
data NFA q o = NFA (q -> o -> [q]) [q] [q]
-- I really realy don't understand the declarations here
-- I can guess that q is somewhat related to Q and o to E, but don't get what it really means
data Q = Q0 | Q1 | Q2
deriving (Eq, Enum, Bounded)
data E = A | B
-- what does n1 do ??
n1 :: NFA Q E
n1 = NFA d [Q0] [Q2] -- i see [Q0] refers to set of initial states and [Q2] refers to final states :)
where
d Q0 A = [Q0]
d Q0 B = [Q0, Q1]
d Q1 _ = [Q2]
d Q2 _ = []
-- the following functions are for me to write
starDFA :: Eq q => DFA q o -> [o] -> Bool
--for the above function, what are the arguments the function takes in ?
--how can we relate q with Q and [o] with [E] ??
Any explanations or references to proper starting points will be really helpful to me.
Sorry to ask such a dumb question, but I really don't know where to start :)
Thanks
Learn You a Haskell for Great Good! is probably the best Haskell tutorial at the moment. I recommend reading through the whole thing, but if you are in a hurry, I'll point out some of the more relevant sections for this assignment.
The main part in the code you are given are the data declarations, so once you're familiar with the basics (chapter 2 and the first section of chapter 3), a good place to dive in is the Algebraic data types intro, Type variables and Type parameters.
The above should be enough for deciphering the data declarations and understanding the relationship between q vs Q and o vs E.
Now to implement the actual function, you need to be familiar with how deterministic finite automata work and then know enough Haskell to write the actual implementation. Chapter 4 and chapter 5 are the most relevant chapters for this in the tutorial and you might also find the section on standard library list functions useful.
Once you get to this point, if you are stuck with the implementation, you can post another question with the code you have written so far.
In haskell we have three way to define new type, using three distinct keywords, type, newtype, and data.
In your example it's data keyword which is in use, let's focus a bit more on it.
It's better to start with the easiest one coming from your code
data E = A | B
Here, We have define a new type E which can take only two mode or state or value.
A type like this is what we call a sum type.
How can we use a it ?
Mostly with pattern matching.
useE :: E -> String
useE A = "This is A"
useE B = "This is B"
Now, a more complex data declaration from your code.
data Q = Q0 | Q1 | Q2 deriving (Eq, Enum, Bounded)
Again, as said previously we have a sum type which define a new type Q, taken three value, Q0, Q1 or Q2. But we have a deriving clause, which tell to the compiler that this new type implement the method (or function) deriving (or inherited) from Eq, Enum, Bounded class.
What's that mean ?
Let's take a look to a function.
Imagine you want to associate a number for each of the value of Q, how can we perform that ?
enumQ :: Q -> Int
enumQ x = fromEnum x
If you want more insight about this particular functionality provide by deriving clause, read the resources which have been indicated and try :info Enum under ghci. Note that the previous type could also derive from the same class. As these types are fully describe as the sum of an enumerable set of value (discriminated by |) we better understand why we call them sum type.
Finally the most difficult data declaration.
data DFA q o = DFA (q -> o -> q) q [q]
data NFA q o = NFA (q -> o -> [q]) [q] [q]
If fact they are almost the same data definition then I will go trough the first one and let you the analyses of the second one as an exercise.
data DFA q o = DFA (q -> o -> q) q [q]
This time we must talk about data constructor and type constructor.
On the left hand side of the equality, there is data constructor,
to built data and give it a name. On this side we have the required
parameter used to built this new data.
On the right hand side of the equality, there is type constructor, to
built this new type. On this side we have the explicit
plumbering which show to the reader how this new type (data) is built
using the existing type.
Now keeping in mind, that the following are type,
[x] ::: Type representing the polymorphic list, Example, [Int] =>
List of Int
x ::: A basic type, one of the existing one (Int, Char, String ...)
x -> y ::: Type which define a function taken a type x to produce a
type y.
x -> y -> z ::: Type which define a function taken a type x and a
type y to produce a type z. Which can be view as a function taking another function of type (x->y) and producing a type z. This is what we call an High-Order Function.
Then our data declaration, put in this context, is a data constructor, feed by two type parameter, q and o and as a result, it return a new type as the product of a high-order function a basic type and a list type. Which explain why we call this a product type.
It should be enough, now, infering by yourself, to answer your question what does n1 ?
Good luck.
From the little I understand about Haskell type declarations, the initial statements about DFA and NFA are saying something like (looking at NFA, for example):
(Left hand side:) NFA is a type that utilizes two types (q and o) in its construction.
(Right hand side:) an instance of NFA will be called NFA, and be composed of three parameters:
(1) "(q -> o -> [q])" , meaning a function that takes two parameters, one of type q and one of type o, and returns a list of q's, ([q])
(2) "[q]" , meaning one list of values of type q
(3) "[q]" , another list of values of type q
n1 seems like an instance construction of NFA, and we see
n1 = NFA d [Q0] [Q2]
So we can infer that:
(1) d is a function that takes two parameters, a 'q' and an 'o' and returns a list of q's
(2) [Q0] is a list of q's, and
(3) [Q2] is a list of q's.
And, indeed, the definition of d follows:d takes two parameters, a 'Q' and an 'E' and returns a list of Q's (which we know can be either Q0, Q1, or Q2) or an empty list.
I hope that helps a little and/or perhaps someone could clarify and correct my vague understanding as well.

Haskell: Numerically integrating using recursion

I'm giving Haskell a go at the moment and struggling to rewrite loops in terms of recursions.
I am trying to write a basic integrator which takes some function f(x) and integrates it over the range [a, b] via the Midpoint Method. The integrating function takes three parameters N, a, and b, where N is the number of rectangles being used to approximate the integral.
Wolfram Alpha link
When I try to compile this with GHCI I get a lot of abstract error messages and I don't really know where to start. Lots of 'Out of Scopes' and several 'Multiple Declarations of Main.a [or b]'.
Thanks
MPInt 1 a b = DELTA 1 -- Base case
MPInt N a b = (MPInt (N-1) a b) + DELTA
where
dX = (b - a) / N
DELTA = dX * f (a + dX * (N+0.5))
f :: (Num a) => a -> a
f x = x^2
You've named your function MPInt. Only the names of modules, classes, types, and constructors can begin with capital letters; values (including functions) must begin with lowercase letters (or an underscore, or certain punctuation marks). The same problem also applies to N and DELTA.
You use DELTA in the definition of the first case of MPInt, yet it's defined as part of the second case. A where clause only applies to the expression immediately before it (in this case, the MPInt N a b = ... definition). (Also, this first use of DELTA treats it as a function, yet the second use and its definition have it as a numeric value).

Resources