Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: check for square-number with a regex

by moritz (Cardinal)
on Oct 23, 2009 at 15:03 UTC ( #802914=note: print w/replies, xml ) Need Help??


in reply to Re: check for square-number with a regex
in thread check for square-number with a regex

When we talk about a regex in the context of Perl, we don't mean Regular Expression in the sense that a computer scientist would use (ie Typ-3 language of the Chomsky hierarchy), but rather "the thing that you but in m/... in Perl".

These regexes are much more powerful than regular expressions, in fact they can do things that even Typ-2 langauges (context-free languages) can't. I've not yet seen a proof that they are Turing complete, so I don't know if they are.

Update: fixed Chomsky classification, thanks jakobi

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^3: check for square-number with a regex
by JadeNB (Chaplain) on Oct 23, 2009 at 15:11 UTC
    These regexes are much more powerful than regular expressions, in fact they can do things that even Typ-1 langauges (context-free languages) can't. I've not yet seen a proof that they are Turing complete, so I don't know if they are.
    In a sense they're ‘obviously’ Turing-complete, since they can contain embedded Perl code; but I assume that's not what you meant. What's another example of Perl regexes recognising a language that a CFG can't? (It sounds vaguely like a challenge, but I'm just politely curious.)

      First of all sorry for getting the numbering wrong, I misremembered REG as being Typ-0 and Turing as being Typ-3, it's the other way round.

      What's another example of Perl regexes recognising a language that a CFG can't?
      m/^(\w+)\1$/

      Is already outside CFG.

      CFG is parsed by a non-deterministic finite automaton which has access to a stack of infinite size.

      Since it can only access the top element of the stack, it has only access to the capture in reverse order, or with a finite look-behind on the stack (implemented by storing some values in the state of the NFA). A more formal proof can be made with the Pumping lemma for context-free languages.

      I also think that the regex primality test is outside of CFG, but I've never proven that, so don't rely on it ;-)

        I also think that the regex primality test is outside of CFG, but I've never proven that, so don't rely on it ;-)
        Doesn't the pumping lemma that you cited also show what you claim? Specifically, if there were a CFG that recognised exactly the strings of 1s of prime length, then we would have the assertion that any sufficiently large prime s could be written as s = u + v + x + y + z, with everyone non-negative and v + y at least 1, in such a way that u + x + i*(v + y) + z was also prime for every non-negative i. However, in that case,
        • if u + x + z = 0, then we can take i to be composite, so that u + x + z + i*(v + y) = i*(v + y) is composite;
        • or
        • otherwise, we may take i = (v + y + 2)*(u + x + z), so that u + x + z + i*(v + y) = (v + y + 1)^2*(u + x + z) is composite.
        This is a contradiction.

        UPDATE: Forgot to handle the case u + x + z = 0.
        UPDATE 2: Oops, and I was quite sloppy in my choice of i even when u + x + z is positive. I think that it's OK now.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://802914]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2020-09-27 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?