6. Scheme Procedures in Psyche
All procedures for Scheme described in R5RS[1] are implemented
in Python in the module psyche.schemefct. Since Scheme
identifiers are a superset of Python identifiers, some name mangling
was necessary. Since the complete list of Scheme procedures is already
covered in R5RS, this Chapter will focus on the name mangling.
This Section describes the algorithm used for name mangling. In this
discussion, ``Scheme procedure'' refers to the procedure from R5RS
while ``Python function'' refers to the implementation of a Scheme
procedure in psyche.schemefct.
Table 6.1 contains some examples.
- case
- Scheme procedures are all in lower case, with separate
words divided by dashes. Python functions use mixed case, where the
first word is lowercase and the first character of each following word
is capitalized.
- operators
- In Scheme, all operators are actually identifiers or
parts of identifiers. The Python functions use the same naming
convention as the module operators.
- ?
- Scheme procedures implementing predicates all end with
a question mark. Python functions use the standard Python naming
scheme by prepending the name with is (and using appropriate
capitalization).
- !
- Scheme procedures use the exclamation mark to specify
state-changing operations. Python functions omit the exclamation mark.
- ->
- Scheme procedures use -> for functions that
convert one type to another. Python uses the word to (with
appropriate capitalization).
- reserved names
- Sometimes, after applying the previous mangling
steps, the resulting name is not appropriate in Python. It can be a
keyword or the name of a builtin or the name of a well-known
module. These words are suffixed with an underscore. So the Scheme
procedures not, list and string are implemented
by the Python functions not_, list_ and
string_
Table 6.1:
Some name mangling examples
Scheme procedure |
Python function |
current-input-port |
currentInputPort |
+ |
add |
number? |
isNumber |
string-set! |
stringSet |
string->number |
stringToNumber |
char-ci<=? |
isCharCiLe |
|
If there is need to call one of the Scheme procedures in Python, there
are two possibilities: the first option is to do the name mangling
yourself. This is not too difficult, but quite error-prone.
The alternative is the procedures variable in
psyche.schemefct. This is a dictionary mapping all Scheme
procedure names to the corresponding Python function.
While it is possible to modify the procedures dictionary, you
are advised not to. This variable is used to initialize instances of
SchemeEnvironment5, the initial environment for most
Interpreters.
Example 6..1
Using number? in Python
from psyche import schemefct, interpreter
i = interpreter.Interpreter()
obj = i.eval("5")
result = schemefct.isNumber(obj)
# or
result = schemefct.procedures["number?"](obj)
Y. Duppen