( Regexp | XPath ) = Unification + Choicepoints + Syntax

jjore on 2007-08-02T06:03:24

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' )


ocaml edition

diakopter on 2007-08-03T02:01:49

http://www.cduce.org/