Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

data library

Joshua Shinavier edited this page · 4 revisions

The data: library contains primitives for value comparison, type conversion, and literal reification.

Datatype and language

These two primitives deal with the datatype and language of RDF literals, and of native Ripple values equivalent to RDF literals.

lang

This primitive finds the language tag of an RDF plain literal. It expects a single argument at the top of the stack, which must be a language-tagged plain literal (otherwise, no solution will be produced). It pops the literal from the stack, then pushes the language tag onto the stack.

This primitive is identified by the URI http://www.w3.org/XML/1998/namespace#lang.

Example:

1)  "forty-two"@en lang.

  [1]  "en"

type

This primitive finds the datatype of an RDF literal. It expects one argument at the top of the stack, which must be a literal. It pops the literal from the stack, then pushes its datatype (if any) to the stack. If the argument is not a literal or does not have a datatype, no solution is produced.

This primitive is identified by the URI http://www.w3.org/2001/XMLSchema#type.

Examples:

1)  42 type.

  [1]  xsd:integer

2)  "plain" type.

3)  "plain" to-string. type.

  [1]  xsd:string

Value comparison

Ripple provides a total order which is used for sorting, memoizing and equality checking on values in the Ripple environment.

In general, Ripple values of the same or equivalent classes (for example, native numeric values in Ripple and certain numeric-typed RDF literals are considered equivalent) are compared according to their own internal rules, including:

  • numbers use the total order of real numbers
  • strings use string comparison
  • lists use recursive, entry-wise comparison

Values of non-equivalent classes use a global class comparator.

compare

This primitive compares two values according to Ripple's total order. It expects two arguments at the top of the stack. It pops the arguments from the stack and then pushes:

  • -1 if the first argument is less than the second
  • 0 if the arguments are equal
  • 1 if the first argument is greater than the second

Examples:

1)  0 0 compare.

  [1]  0

2)  0 1 compare.

  [1]  -1

3)  1 0 compare.

  [1]  1

4)  1 1.0 compare.

  [1]  0

5)  1 "1e0"^^xsd:double compare.

  [1]  0

6)  1 "one" compare.

  [1]  -1

7)  "one" "two" compare.

  [1]  -1

8)  "forty-two" "forty-three" compare.

  [1]  1

9)  (1 2 3) (1 2) compare.

  [1]  1

10)  (1 2 3) (1 2 3.0) compare.

  [1]  0

equal

This primitive determines whether two values are equal according to Ripple's total order. It expects two arguments at the top of the stack. It pops the arguments from the stack, compares them, then pushes true if they are equal, otherwise false.

This primitive produces true with exactly those arguments for which the compare primitive produces 0, and false for all others.

Examples:

1)  42 type. xsd:integer equal.

  [1]  true

2)  42 type. xsd:double equal. 

  [1]  false

3)  "42"^^xsd:integer "42e0"^^xsd:double equal.

  [1]  true

gt

This primitive compares two values according to Ripple's total order. It expects two arguments a and b at the top of the stack. It pops the arguments from the stack and pushes the value true if a is strictly greater than b, otherwise false.

This primitive is equivalent to the construction compare. 1 eq.

Examples:

1)  42 0 gt.

  [1]  true

2)  42e0 42 gt.

  [1]  false

3)  "42" 42 gt.

  [1]  true

gte

This primitive compares two values according to Ripple's total order. It expects two arguments a and b at the top of the stack. It pops the arguments from the stack and pushes the value true if a is greater than or equal to b, otherwise false.

This primitive is equivalent to the construction lt. not.

Examples:

1)  (1 2) (1) gte.

  [1]  true

2)  (1 2) (1 2) gte.

  [1]  true

3)  (1 2) (1 2 3) gte.

  [1]  false

lt

This primitive compares two values according to Ripple's total order. It expects two arguments a and b at the top of the stack. It pops the arguments from the stack and pushes the value true if a is strictly less than b, otherwise false.

This primitive is equivalent to the construction compare. -1 eq.

Examples:

1)  0 42e0 lt.

  [1]  true

2)  0 <http://example.org/42> lt.

  [1]  true

lte

This primitive compares two values according to Ripple's total order. It expects two arguments a and b at the top of the stack. It pops the arguments from the stack and pushes the value true if a is less than or equal to b, otherwise false.

This primitive is equivalent to the construction gt. not.

Examples:

1)  -1 1 lte.

  [1]  true

2)  1 1 lte.

  [1]  true

3)  1 -1 lte.

  [1]  false

Type conversion

These primitives convert between equivalence classes of Ripple values.

to-double

This primitive converts a value to a double-precision floating point value. It expects one argument at the top of the stack. It pops the argument from the stack, converts the argument to a string (see to-string), then parses the string according to xsd:double syntax. If type conversion succeeds, the resulting double value is pushed to the stack. Otherwise, no solution is produced.

Example:

1)  ("1" "2e0" "three" "4e0") each. to-double.

  [1]  1.0E0
  [2]  2.0E0
  [3]  4.0E0

to-integer

This primitive converts a value to an integer. It expects one argument at the top of the stack. It pops the argument from the stack, converts the argument to a string (see to-string), then parses the string according to xsd:integer syntax. If type conversion succeeds, the resulting double value is pushed to the stack. Otherwise, no solution is produced.

1)  ("1" "2e0" "3.0" "4") each. to-integer.

  [1]  1
  [2]  4

to-millis

This primitive converts a date/time value to an equivalent number of milliseconds since the Unix epoch. It expects a single value at the top of the stack, which is generally an xsd:dateTime-typed RDF literal. It pops the literal from the stack, converts it to a string (see to-string), parses the string using xsd:dateTime syntax, then pushes the millisecond value, as a long-typed literal, to the stack. If the conversion fails, no solution is produced.

Examples:

1)  <http://identi.ca/rss> rss:items. members. 5 limit. dc11:date.

  [1]  "2011-05-20T09:30:03+00:00"
  [2]  "2011-05-20T09:29:40+00:00"
  [3]  "2011-05-20T09:30:03+00:00"
  [4]  "2011-05-20T09:29:27+00:00"
  [5]  "2011-05-20T09:29:18+00:00"

2)  <http://identi.ca/rss> rss:items. members. 5 limit. dc11:date. dup. to-millis.

  [1]  "2011-05-20T09:29:18+00:00" "1305883758000"^^xsd:long
  [2]  "2011-05-20T09:30:03+00:00" "1305883803000"^^xsd:long
  [3]  "2011-05-20T09:30:03+00:00" "1305883803000"^^xsd:long
  [4]  "2011-05-20T09:29:40+00:00" "1305883780000"^^xsd:long
  [5]  "2011-05-20T09:29:27+00:00" "1305883767000"^^xsd:long

to-string

This primitive converts any value to a string. It expects a single argument at the top of the stack. It pops the argument from the stack and converts it to a string, then pushes this string to the stack.

Every Ripple value has a string representation, which is defined according the equivalence class of objects two which it belongs:

  • numeric values are serialized according to the syntax of the corresponding XML Schema datatype
  • (other) RDF literals use their label
  • URIs (i.e. resources identified by URIs) use a string representation of the URI
  • lists use any Ripple string representation which is valid (i.e. would be parsed to an object equivalent to the reference object) in the scripting environment. For example, "(1 2 3)" or "(foaf:knows. foaf:name)").

Other classes of objects exist in Ripple environments but have class-specific to-string behavior.

Examples:

1)  2 sqrt. to-string.

  [1]  "1.4142135623730951"^^xsd:string
  [2]  "-1.4142135623730951"^^xsd:string

2)  "foo" to-string.

  [1]  "foo"^^xsd:string

3)  foaf:Person to-string.

  [1]  "http://xmlns.com/foaf/0.1/Person"^^xsd:string

4)  (1 2 3) to-string.

  [1]  "(1 2 3)"^^xsd:string

to-uri

This primitive converts a value to an URI reference. It expects a single argument at the top of the stack. It pops the argument from the stack, converts it to a string (see to-string), then parses the resulting string as a URI. If parsing is successful, the URI is pushed to the stack. Otherwise, no solution is produced.

Example:

1)  "http://example.org/foo" to-uri.

  [1]  <http://example.org/foo>
Something went wrong with that request. Please try again.