Is the complement of any context free language context free? - complexity-theory

I read multiple answers that state if a language is not context free, then its complement is context free (correct me if I am wrong). Is this true for the opposite? the complement of a context free language is context free?

Neither statement is true. The complement of a context-free language can be context-free or not; the complement of a non-context free language can be context-free or not.
Every regular language is context-free. Regular languages are closed under complement, so the complement of a regular language is regular. Consequently, any regular language and its complement are a pair of complementary context-free languages.
The classic example of a non-context-free language whose complement is context-free is {ww|w∈{0,1}*}. (The proof that its complement is context-free is in the answer to this question.)
For a non-context-free language whose complement is also not context-free, a simple example is the language whose valid strings are all pairs {(i, x) i halts on input x} (where i is the description of a Turing machine). That language is not context-free, but it is recursively enumerable. Its complement is not even recursively-enumerable. (See Wikipedia)

Related

Challenge on Alphabet and Formal Grammar and Language

We know set A is countable if A is finite or in a one-to-one mapping to natural numbers.
Suppose ALPH be an arbitrary finite alphabet.
I summarize my inference:
a) Each arbitrary Language on ALPH is Countable. (I think this is True)
b) the set of all language from ALPH is Countable.(I think this is False)
c) for Each arbitrary Language on ALPH we have a generative formal grammar. (I think this is False)
d) Each arbitrary Language on ALPH that can be generated by formal grammar, is recursive. (I think this is True)
anyone could help me, and maybe correct me?
Without loss of generality, we can assume that ALPH is merely the set {0,1}. (Any other finite language can of course be encoded using the set {0,1}). Assuming that by a language L that you intend some arbitrary subset of ALPH*, we can assume that L is an arbitrary subset of {0,1}*.
Let S = {0,1}*.
a) The set S is countable. Since L is a subset of S, L is countable.
b) The set of all languages over S then is the powerset of S, which can be put into 1-1 correspondence with the Real numbers. Hence, not countable.
c) I believe that this is false, agreeing with your supposition. However, it depends on your definition of a 'generative formal grammar'. If you allow for formal grammars where individual rules of the grammar are undecidable, and/or allow for infinite generation rules, this becomes less clear. For any particular definition for 'generative formal grammar', where the collection of 'generative formal grammars' is enumerable, then of course, the answer is false.
d) In general, I believe that the answer to this is false. If you restrict yourself to formal grammars corresponding to context-free languages, then of course, your answer is true. However, consider http://en.wikipedia.org/wiki/Post_correspondence_problem The problem is undecidable, yet the generation rules are pretty clear.

Logic programming - Is subset with only one function symbol Turing - complete?

If I have a subset of logic programming which contains only one function symbol, am I able to do everything?
I think that I cannot but I am not sure at all.
A programming language can do anything user wants if it is a Turing-complete language. I was taught that this means it has to be able to execute if..then..else commands, recursion and that natural numbers should be defined.
Any help and opinions would be appreciated!
In classical predicate logic, there is a distinction between the formula level and the term level. Since an n-ary function can be represented as an (n+1)-ary predicate, restricting only the number of function symbols does not lessen the expressivity.
In prolog, there is no difference between the formula and the term level. You might pick an n-ary symbol p and try to encode turing machines or an equivalent notion(e.g. recursive functions) via nestings of p.
From my intution I would assume this is not possible: you can basically describe n-ary trees with variables as leaves, but then you can always unify these trees. This means that every rule head will match during recursive derivations and therefore you are unable to express any case distinction. Still, this is just an informal argument, not a proof.
P.S. you might also be interested in monadic logic, where only unary predicates are allowed. This fragment of first-order logic is decidable.

Prolog - what sort of sentences can't be expressed

I was wondering what sort of sentences can't you express in Prolog? I've been researching into logic programming in general and have learned that first-order logic is more expressive compared to definite clause logic (Horn clause) that Prolog is based on. It's a tough subject for me to get my head around.
So, for instance, can the following sentence be expressed:
For all cars, there does not exist at least 1 car without an engine
If so, are there any other sentences that CAN'T be expressed? If not, why?
You can express your sentence straightforward with Prolog using negation (\+).
E.g.:
car(bmw).
car(honda).
...
car(toyota).
engine(bmw, dohv).
engine(toyota, wenkel).
no_car_without_engine:-
\+(
car(Car),
\+(engine(Car, _))
).
Procedure no_car_without_engine/0 will succeed if every car has an engine, and fail otherwise.
The most problematic definitions in Prolog, are those which are left-recursive.
Definitions like
g(X) :- g(A), r(A,X).
are most likely to fail, due to Prolog's search algorithm, which is plain depth-first-search
and will run to infinity and beyond.
The general problem with Horn Clauses however is, that they're defined to have at most one positive element. That said, one can find a clause which is limited to those conditions,
for example:
A ∨ B
As a consequence, facts like ∀ X: cat(X) ∨ dog(X) can't be expressed directly.
There are ways to work around those and there are ways to allow such statements (see below).
Reading material:
These slides (p. 3) give an
example of which sentence you can't build using Prolog.
This work (p. 10) also explains Horn Clauses and their implications and introduces a method to allow 'invalid' Horn Clauses.
Prolog is a programming language, not a natural language interface.
The sentence you show is expressed in such a convoluted way that I had hard time attempting to understand it. Effectively, I must thanks gusbro that took the pain to express it in understandable way. But he entirely glossed over the knowledge representation problems that any programming language pose when applied to natural language, or even simply negation in first order logic. These problems are so urgent that the language selected is often perceived as 'unimportant'.
Relating to programming, Prolog lacks the ability to access in O(1) (constant time) any linear data structure (i.e. arrays). Then a QuickSort, for instance, that requires access to array elements in O(1), can't be implemented in efficient way.
But it's nevertheless a Turing complete language, for what is worth. Then there are no statements that can't be expressed in Prolog.
So you are looking for sentences that can't be expressed in clausal logic that can be expressed in first order logic.
Strictly speaking, there are many, simply because clausal logic is a restriction of FOL. So that's true by definition.
What you can do though is you can rewrite any set of FOL sentences into a logic program that is not equivalent but with good properties. So for example if you want to know if p is a consequence of your theory, you can use equivalently the transformed logic program.
A few notes on the other answers:
Negation in Prolog (\+) is negation as failure and not first order logic negation
Prolog is a programming language, as correctly pointed out, we should be talking about clausal logic instead.
Left recursion is not a problem. You can easily use a different selection rule, or some other inference mechanism.

Algorithm for regular expression intersection with cfg

Im looking for an algorithm which outputs if the intersection of a regular expression and a contex free grammar is empty or not. I know that this problem is decidable, however, I cannot find any example implementation (in pseudocode).
Can someone provide me with such an algorithm, in .NET if possible but this is not a must. This problem is also called "regular intersection". Googling for it only gives me the geometrical algorithm or the theory about it.
edit:
Anybody. Im really stuck on it, and cannot find anything yet.
Here is a sketch of an approach that occurs to me. I think this should work but it is probably not the best way to do it since it uses the terribly messy conversion from PDA to CFG.
Convert the regular expression into a nondeterministic finite automaton (NFA) and reduce it down to the minimal determinsitic finite automaton (DFA). Convert the context free grammar (CFG) into a pushdown automoton (PDA). These first steps are all well known and fairly simple algorithms.
Take the intersection of the DFA and PDA, which is also a PDA. We will say the DFA has states S1, start state s1, final states F1, and transitions delta1 of the form ((source,trigger),destination), and the PDA has states S2, start state s2, final states F2, and transitons delta2 of the form ((source,trigger,pop),(destination,push)). The new PDA has states S1 X S2, each state labeled by a pair. It has final states F1 X F2, and start state (s1,s2). Now for the transitions.
For each transition d an element of delta2, for each state s an element s1, find the transition t an element of delta1 of the form ((s,d.trigger),?). Make a new transition (((d.source, s), d.trigger, d.pop),((d.destination, t.destination),d.push)).
This new PDA should accept the intersection of the languages produced by the RE and the CFG. To test if the language is empty you will need to convert it back to a CFG. The algorithm for that is messy and large, but it works. Once you have done that, mark each terminal symbol. Then mark each symbol which has a rule where there are only marked symbols on the right hand side, and repeat until you can mark no more symbols. If you can mark the start symbol, the language is not empty. Otherwise, the language is empty.
In fact, there is a simpler algorithm for computing the intersection between a context-free grammar and a regular expression. It does not use push-down automata, which can be costly to obtain from CFG with several conversions.
This solution was presented in:
Y. Ba-Hillel, M. Prles, and E. Shamir. 1965. On formal properties of
simple phrase structure grammars. Z. Phonetik, Sprachwissen. Komm. 15
(I961), 143-172. Y. Bar-Hillel, Language and Information,
Addison-Wesley, Reading, Mass (1965), 116–150.
but you can find a simpler version in:
Richard Beigel and William Gasarch. .. A Proof that the intersection
of a context-free language and a regular language is Context-Free
Which Does Not use pushdown automata.
http://www.cs.umd.edu/~gasarch/BLOGPAPERS/ cfg.pdf (.).
If it help, this solution was implemented in Pyformlang (https://pyformlang.readthedocs.io/), and you can find it on Github for Python (https://github.com/Aunsiels/pyformlang/blob/master/pyformlang/cfg/cfg.py)

Recursive languages vs context-sensitive languages

In Chomsky's hierarchy, the set of recursive languages is not defined. I know that recursive languages are a subset of recursively enumerable languages and that all recursive languages are decidable.
What I'm curious about is how recursive languages compare to context-sensitive languages. Can I assume that context-sensitive languages are a strict subset of recursive languages, and therefore all context-sensitive languages are decidable?
To recognize a recursive language you need a kind of automaton named Decider . It is exactly a Turing Machine tricked by a limited control flow, that is, to ensure it will always halt.
Concerning context-sensitive languages, they are indeed a proper subset of recursive ones. It's trivial giving that the minimal automaton to recognize a context-sensitive language, a Linear bounded automaton is strictly less powerful than a decider. I guess that it would also be possible to demonstrate based on grammar restriction rules.
If your question is only if every context sensitive language is in the set of all recursive languages, you should try to prove it the classical way through formal automata. Ask yourself what formal automaton can simulate generation of context sensitive language and what is used to generate recursive language. Then just try to simulate one using the other. Once you look up the right automata in your textbook, you will sure be able to prove what you want.
set of context sensitive languages are a proper subset of recursive languages.
You dont have to assume this, refer to Peter Linz's book for proof.
According to Papadimitriou's book (3.4.2 (e)), context-sensitive grammars are equivalent to NSPACE(n), which is a proper subset of recursive languages. So, yes, your assumption is correct.
As per my references , I would also say that Context Sensitive Languages are a proper subset of a set of all Recursive Languages.You can find this proof in any Standard Textbook like
> An Introduction to Formal Languages and Automata (Edition 5) by Peter Linz

Resources