This post will introduce Ripple’s application and regular expression syntax. The very first releases of Ripple (as in the screencast) included a single, infix symbol, “/” for the application of mappings. For example, to map the numbers 2 and 3 to their sum, you would have used (2 3 /add). While the slash is still supported for the sake of backwards compatibility, Ripple’s preferred syntax is now entirely postfix-based, and includes various constructions for “forward” and “backward” application of mappings, as well as for regular expressions. These are:
- “>>” — forward application. Simply applies a mapping, exactly once. For instance, the example above should be written:
(2 3 add >>)using the preferred syntax. - “<<” — backward application. Applies the inverse of a mapping, insofar as this is defined. For instance, the expression
(2 3 add <<)yields (-1), as the inverse mapping ofaddis defined to besub, the subtraction primitive. In RDF applications, backward application is useful for traversing links from head to tail rather than from tail to head. For example, if(:timbl foaf:knows >>)yields all individuals known by Tim Berners-Lee, then(:timbl foaf:knows <<)yields all individuals who know Tim Berners-Lee, according to Ripple’s knowledge base. In the current application, you can even traverse backwards from literal values. For example,("Timothy Berners-Lee" foaf:name <<)yields(:timbl)himself. - “?” — optional quantifier. This and the following constructions provide POSIX-style regular expressions in Ripple. When it stands before an application operator, “?” applies the operator both once and not at all. For instance, the expression
(:timbl foaf:knows? >>)yields both Tim Berners-Lee and the individuals he knows. The expression(42 neg? >>)yields both(42)and(-42). - “*” — star quantifier. When it stands before an application operator, “*” applies the operator zero or more times. This is particularly useful when working with recursive data structures such as lists. For example, the expression
((10 20 30) rdf:rest* >> rdf:first >>)yields(10),(20), and(30). - “+” — plus quantifier. Like “*”, but applies its operator at least once. Thus, the expression
((10 20 30) rdf:rest+ >> rdf:first >>)yields only(20)and(30), as therdf:restmapping is applied once, then twice before the end of the list is reached. - “{n}” — numeric quantifier. Applies its operator a single, specified number
nof times. For instance,((10 20 30) rdf:rest{2} >> rdf:first >>)yields(30). The expression(:timbl foaf:knows{2} >>)yields all individuals known by Tim Berners-Lee, in transitive fashion for two degrees. This is the same as(:timbl foaf:knows >> foaf:knows >>). - “{n,m}” — range quantifier. Applies its operator at least
ntimes and at mostmtimes. For instance,((10 20 30) rdf:rest{0,1} >> rdf:first >>)yields(10)and(20).(:timbl foaf:knows{2,3} <<)yields all individuals from whom Tim Berners-Lee is two or three degrees removed, according to thefoaf:knowsmapping.
Note that despite this diversity of syntax, there is and always has been only one true application operator in Ripple, still called op. Apart from the forward application symbol “>>” which is simply an alias for op, all of the above constructions are merely syntactic sugar for expressions involving op together with one primitive mapping or another. For example, the expression (:timbl foaf:knows{2} >>) parses to the same Ripple program as (:timbl foaf:knows 2 timesApply op).
0 Responses to “Arrows and regex”