preserves/representations.md

4.2 KiB

title
Representing Values in Programming Languages

NOT YET READY

We have given a definition of Value and its semantics, and proposed a concrete syntax for communicating and storing Values. We now turn to suggested representations of Values as programming-language values for various programming languages.

When designing a language mapping, an important consideration is roundtripping: serialization after deserialization, and vice versa, should both be identities.

Also, the presence or absence of annotations on a Value should not affect comparisons of that Value to others in any way.

JavaScript.

  • BooleanBoolean
  • Float and Double ↔ numbers
  • SignedInteger ↔ numbers or BigInt (see here and here)
  • String ↔ strings
  • ByteStringUint8Array
  • SymbolSymbol.for(...)
  • Record{ "_label": theLabel, "_fields": [field0, ..., fieldN] }, plus convenience accessors
    • (undefined) ↔ the undefined value
    • (rfc3339 F)Date, if F matches the date-time RFC 3339 production
  • SequenceArray
  • Set{ "_set": M } where M is a Map from the elements of the set to true
  • Dictionary ↔ a Map

Scheme/Racket.

  • Boolean ↔ booleans
  • Float and Double ↔ inexact numbers (Racket: single- and double-precision floats)
  • SignedInteger ↔ exact numbers
  • String ↔ strings
  • ByteString ↔ byte vector (Racket: "Bytes")
  • Symbol ↔ symbols
  • Record ↔ structures (Racket: prefab struct)
  • Sequence ↔ lists
  • Set ↔ Racket: sets
  • Dictionary ↔ Racket: hash-table

Java.

  • BooleanBoolean
  • Float and DoubleFloat and Double
  • SignedIntegerInteger, Long, BigInteger
  • StringString
  • ByteStringbyte[]
  • Symbol ↔ a simple data class wrapping a String
  • Record ↔ in a simple implementation, a generic Record class; else perhaps a bean mapping?
    • (mime T B) ↔ an implementation of javax.activation.DataSource?
  • Sequence ↔ an implementation of java.util.List
  • Set ↔ an implementation of java.util.Set
  • Dictionary ↔ an implementation of java.util.Map

Erlang.

  • Booleantrue and false
  • Float and Double ↔ floats (unsure how Erlang deals with single-precision)
  • SignedInteger ↔ integers
  • String ↔ pair of utf8 and a binary
  • ByteString ↔ a binary
  • Symbol ↔ pair of atom and a binary
  • Record ↔ triple of obj, label, and field list
  • Sequence ↔ a list
  • Set ↔ a sets set
  • Dictionary ↔ a [map][erlang-map] (new in Erlang/OTP R17)

This is a somewhat unsatisfactory mapping because: (a) Erlang doesn't garbage-collect its atoms, meaning that (a.1) representing Symbols as atoms could lead to denial-of-service and (a.2) representing Symbol-labelled Records as Erlang records must be rejected for the same reason; (b) even if it did, Erlang's boolean values are atoms, which would then clash with the Symbols true and false; and (c) Erlang has no distinct string type, making for a trilemma where Strings are in danger of clashing with ByteStrings, Sequences, or Records.

Python.

  • BooleanTrue and False
  • Float ↔ a Float wrapper-class for a double-precision value
  • Double ↔ float
  • SignedInteger ↔ int and long
  • Stringunicode
  • ByteStringbytes
  • Symbol ↔ a simple data class wrapping a unicode
  • Record ↔ something like namedtuple, but that doesn't care about class identity?
  • Sequencetuple (but accept list during encoding)
  • Setfrozenset (but accept set during encoding)
  • Dictionary ↔ a hashable (immutable) dictionary-like thing (but accept dict during encoding)

Squeak Smalltalk.

  • Booleantrue and false
  • Float ↔ perhaps a subclass of Float?
  • DoubleFloat
  • SignedIntegerInteger
  • StringWideString
  • ByteStringByteArray
  • SymbolWideSymbol
  • Record ↔ a simple data class
  • SequenceArrayedCollection (usually OrderedCollection)
  • SetSet
  • DictionaryDictionary