Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of jjore (6662)

Thursday August 02, 2007
01:03 AM

( Regexp | XPath ) = Unification + Choicepoints + Syntax

[ #33956 ]

I didn't realize until today that the unifying idea behind regular expressions and XPath and all the other nifty pattern matching stuff available in neato-keen languages like Oz, Erlang, Haskell, Prolog, etc is that "regexps" and XPath were just convenient syntaxes to generate input to unification operations.

Consider the expression

'abc' =~ /^[a-z]+\z/

Expanded, the string is the list

[ 'a' 'b' 'c' ]

and conceptually, the regexp is a stream of

any( 'a' .. 'z' )

values which unify to any of the values a through z.

I gather that this idea that having unification so close to the programmer would let us all do "regexps" on arbitrary data structures more often because we'd just have to express the template streams ala HOP or similar. Apply your template to your data and viola! Destructuring!

%foo = (
    a => 1,
    b => 2,
    c => 3,
    d => 4,
# Now apply yon unification operator
\ %foo ~~ {
    $bar => 1,
    $baz => 2,
    c => $c,
# The following variables would then be populated.
assert( $bar eq 'a' )
assert( $baz eq 'b' )
assert( $c eq '3' )

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.