Redis output conversion

The redis(Server, Command, Reply as Type) works quite well (still have to deal with complex types such as streams as discussed). There are also a few issues with converting Prolog values to Redis strings. Currently

  • atomic types are emitted verbatim (unquoted)
  • prolog(Term) is emitted as \x00T\x00<canonical form>
  • A:B:C… where A, B, … are atomic is transformed into a key
  • Anything else is emitted as the output of write/1.


  • When dealing with atoms and strings, we need to specify the encoding. This is now hard-wired UTF-8, but this prevents emitting a binary blob or as text in an encoding agreed upon by the other clients of the Redis server.
  • The prolog(Term) is an outlier
  • Using write/1 for anything else is dubious as type and instantiation errors just result in some Prolog term in the Redis DB, which is unlikely to be useful.
  • Several commands take arrays or alternating key/value arrays as argument. Would be nice if we could pass lists, dicts or lists of pairs to these.

I guess that using the same ... as Type is the most sensible notation to guide these conversions. For the encoding we could use as string(Encoding). The string is not really needed as anything in Redis is a string, but it remains consistent with the input types. Alternatively would be to use the plain encoding name, as e.g. one of these:

redis(default, set(a, String as string(iso_latin_1)))
redis(default, set(a, String as iso_latin_1))

The prolog(Term) is probably better Term as prolog, which in the future could also allow for Term as prolog(Options) to set additional write options. Default would be to use write_canonical/1. Issue is whether or not to prefix the term with the magic \x00T\x00 and have fetching the value automatically parse it or require using

redis(default, get(key), Term as prolog).

Both make sense to me. The fact that I can put arbitrary Prolog terms in the database and get them out without worries is surely practical.

For arrays we probably want ... as list and ... as pairs where the first requires a Prolog list and the second either a dict or a list of pairs. Arguments could be used to guide conversion of the individual arguments.


1 Like