5. Scheme Types in Psyche

Warning: Unfinished

Scheme has a number of well-defined types; unfortunately, those types do not map exactly on the Python types. In order to return meaningful results from the eval method, Psyche wraps some of those types in Python objects. These objects can be found in the psyche.types module.

Table 5.1 shows how Scheme types are mapped on Python types and Psyche objects.


Table 5.1: Scheme types, Python types and Psyche objects
Scheme type Python type Psyche object
Number int, long, float, complex Fraction
Boolean   Boolean
Pair   Pair
Symbol   Symbol
Character   Character
String string MString
Vector   Vector



5.1 Symbols

Scheme supports the notion of symbols, something that does not exist in Python. Therefore, Psyche provides the class Symbol that models Scheme symbols.

There are two kinds of symbols in Scheme:

  1. Symbol literals
  2. Symbols created by string->symbol

Every symbol also has a string representation, which can be obtained by symbol->string. Two symbols are identical if and only if they have an identical string representation.

The string representation differs between literal symbols and symbols created by string->symbol: the former are always represented in lower case5.1, while the latter are represented exactly like the string they were derived from.

The Psyche class Symbol implements this by providing a constructor with two arguments: name and the optional fromString. Two symbols are equal iff

  1. both Symbols have the same lowercased name
  2. neither Symbol has a fromString or both Symbols have the same fromString

Since symbols occur frequently in Scheme programs, literal Symbols are interned using the Flyweight pattern. The Flyweight pool uses weak references, assuring that Symbols are garbage collected as soon as there are no other references left.

The result of this is that the eqv? on literals is identical to using the Python is comparison keyword.


5.2 Strings

In Scheme, there are three kinds of strings:

  1. Strings literals
  2. Strings returned by symbol->string
  3. Strings returned by other Scheme functions

String literals are immutable and are represented by the Python type str.

Strings returned symbol->string are immutable as well; however, they have the added functionality that calling string->symbol on such a string returns an interned symbol (see 5.1 for more information on such symbols). Strings returned by symbol->string are represented by the Psyche class SymbolString, a subclass of str.

SymbolStrings are identical to str in all respects, apart from an extra field used by the string->symbol function.

All other strings in Scheme are mutable. They are represented by the Psyche class MString, also a subclass of str. MStrings behave exactly like str, with the added functionality of supporting __setitem__.

MString.__setitem__ only accepts single character strings. All other values raise a TypeError.

5.3 Pairs and Lists

Scheme has pairs as a built-in datatype. Pairs are used to implement lists, by setting the cdr of a pair to another pair or the empty list.



Footnotes

... case5.1
This is more specific than R5RS, which only requires a standard case
Y. Duppen