ShenDoc 30




Streams in Shen were introduced to encode some of the functions that were hard-wired into Qi II like 'write-file' and 'read-file'. The goal was to capture just as much in the way of primitives as is necessary to reproduce the functionality of Qi II. From Shen 13 onwards only byte streams are supported by the standard. Any other kind of stream will be found in a library. See the side link Bytes and Characters' for more on reading and writing to Shen streams.

The basic functions for streams are

  • open
  • close
  • pr
  • read-byte
  • write-byte
  • stinput
  • stoutput

The function 'open' creates a stream and the first argument is a string designating the file which is the basis of the stream and the second argument is either 'in' or 'out' determines whether the resulting stream is a source or sink . Material written to a file overwrites the contents already in it. The function 'close' closes a stream and returns the empty list.

The 'open' function works in hand with the 'cd' command in Shen which fixes the home directory. All files are opened relative to the value for the home directory, which is held in the global *home-directory*. The value of this global is changed by the cd function common to Qi and Shen.

(stinput) returns the standard input as a byte source. If 'read-byte' is applied to the standard input then the user's input is echoed to the terminal under the printing convention that the character printed has the same code point as the byte (as in UTF-8). Effectively this means that (stinput) may handle extended ASCII, although only ASCII is required of the Shen reader.

'write-byte' writes a byte as a number n from 0 to 255 to a stream and returns n as a value. If the stream is omitted then 'write-byte' defaults to the standard output. If 'write-byte' is applied to the standard output then the user's input is echoed to the terminal under the printing convention that the character printed has the same code point as the byte. (stoutput) returns the standard output.

Both 'stinput' and 'stoutput' are defined in Shen within the Shen kernel in terms of the global variables *stinput* and *stoutput* to which the Shen standard input and the Shen standard output are bound.

The type 'stream' is a parametric dependent type in Shen and has only two possible parameters - 'in' or 'out'. The type of the 'open' function is a dependent type which cannot be given a type in arrow notation but requires the following sequent calculus definition.

if (element? Direction [in out])
File : string;
(open File Direction) : (stream Direction);

where the values of Direction are 'in' and 'out'.

In Shen 16 the zero-place function 'it' was introduced; (it) returns the last (unevaluated) expression entered to the standard input as a string. The type of it is (--> string).


Basic Types in Shen and Kλ
The Primitive Functions of Kλ
The Syntax of Kλ
Notes on the Implementation of Kλ
Boolean Operators
The Syntax of Symbols
The Semantics of Symbols in Shen and Kλ
Strings and Pattern Matching
Character Streams and Byte Streams
Bytes and Unicode
Reader Macros
Standard Vectors and Pattern Matching
Non-standard Vectors and Tuples
Generic Functions
Type Declarations
External Global Variables
Property Lists and Hashing
Error Handling
Floats and Integers
The Timer
Special Forms

Built by Shen Technology (c) Mark Tarver, September 2021