(sagittarius) - builtin library

Library (sagittarius)

This library has Sagittarius specific functionalities such as extra file system functions and so.

Builtin Syntax

Similar to the define however the define-constant binds variable as a constant value and the compiler try to fold it if it is constant value i.e. literal string, literal vector, literal number and so.

If the defined variable is being overwritten, the VM shows a warning message on the standard error. If overwriten is forbidden by #!no-overwite or other directives, then compiler raises an error.

[SRFI-8] formals and body are the same as lambda. Expression must be an expression.

receive binds values which are generated by expressions to formals.

The expressions in body are evaluated sequentially in the extended environment. The results of the last expression in the body are the values of the receive-expression.

[R7RS][SRFI-0] Compile time condition. The cond-expand resolves platform dependencies such as C's #ifdef preprocessor.

clauses must be one of these forms:

library form searches the given library-name and if it is found, then compiles body.

and, or and not are the same as usual syntax.

Users can check possible _feature-identifier_s via cond-featuresprocedure.

Macro transformer

Proc must be a procedure which accepts 3 arguments, form, rename and compare.

form

The input form of this macro. It is mere s-expression.

rename

A procedure. It takes an object as its argument and convert it to a syntax object. The procedure is similar with datum->syntax, it doesn't require template identifier.

compare

A procedure. It takes 2 arguments and compares if they are the same object in sense of free-identifier=?. If the given arguments are list or vector, then the procedure checks its elements.

The er-macro-transformer returns explicit renaming macro transformer, so you can write both hygine and non-hygine macro with it. For example:

(define-syntax loop
  (er-macro-transformer
   (lambda (form rename compare)
     (let ((body (cdr form)))
       `(,(rename 'call/cc)
	 (,(rename 'lambda) (break)
	  (,(rename 'let) ,(rename 'f) () ,@body (,(rename 'f)))))))))

(let ((n 3) (ls '()))
  (loop
    (if (= n 0) (break ls))
    (set! ls (cons 'a ls))
    (set! n (- n 1))))
(a a a)

This example has the same functionality as the example written in datum->syntax description. The basic of er-macro-transformer is the opposite way of the syntax-case. The syntax-case always makes macro hygine, however the er-macro-transformer can make macro hygine. Moreover, if you do not use rename, it always makes it non-hygine.

Arithmetic operations

Function +. z ...
Function *. z ...
Function -. z ...
Function /. z ...

The same as +, *, - and /. The difference is these procedures converts given arguments inexact number.

x and m must be exact integer.

Returns _x_ ^ -1 mod _m_

x, e and m must be exact integer.

Returns _x_ ^ _e_ mod _m_

File system operations

Returns file size of filename in bytes. If filename does not exist, it raises &i/o condition.

Returns file type or permission of given filename.

Returns file statistics time in nano sec.

The file-stat-ctime procedure returns last change time of filename.

The file-stat-mtime returns last modified time of filename.

The file-stat-atime returns last accesse time of filename.

Changes given filename's access time and modify time.

atime specifies the access time of the filename.

mtime specifies the modify time of the filename.

If atime and/or mtime are time object, then the setting timestamp is the absolute time represented by the arguments.

If atime and/or mtime are real number, then the setting timestamp is the difference of the arguments from current time.

If atime and/or mtime are #f, then the timestamp is intact.

If atime and/or mtime are true value other then time or real number, then the timestamp is set to current time.

Changes given filename's file mode.

mode must be a fixnum encoded with POSIX file permission.

NOTE: On Windows, only the owner permission is considered.

Creates symbolic link of old-filename as new-filename.

Renames given old-filename to new-filename.

If old-filename does not exist, it raises &i/o.

If new-filename exists, it overwrite the existing file.

Creates/deletes given directory. If it fails, it raises condition &i/o.

Reads directory and returns contents as a string list. If path does not exist, it returns #f.

src and dst must be string and src must indicate existing file path.

Copies given src file to dst and returns #t if it's copied otherwise #f.

If optional argument overwrite is #t then it will over write the file even if it exists.

Returns current working directory.

If optional argument path is given, the current-directory sets current working directory to path and returns unspecified value.

Sets current working directory to path.

path1 and path2 must be string.

Concatenate given parameter with platform dependent path separator.

Hashtables

Creates a hashtable.

The same as make-eq-hashtable and make-eqv-hashtable. It uses equal? or string=? as comparing procedure, respectively.

Returns all hashtable's values. This procedure is for consistancy of hashtable-keys.

Returns given hashtable's keys or values, respectively.

The R6RS required procedure hashtable-keys and hashtable-valuesare implemented with these procedures.

Returns hashtable's hash type as a symbol. The possible return values are eq, eqv, equal, string and general.

Returns hashtable's hash weakness as a symbol if the given hashtable is weak hashtable, otherwise #f. The possible return values are key, value and both.

I/O

Returns #t if given port is closed, otherwise #f.

out must be binary output port. endian must be a value returned from endianness macro.

Write v to out as unsigned/signed 16/32 bit integer.

in must be binary input port. endian must be a value returned from endianness macro.

Read a number from in as unsigned/signed 16/32.

NOTE: above put-* and get-* have only 16 and 32 bit integers. This is because benchmark told us there's not much difference between C and Scheme implementation. We may add 64 bit integers and floting number versions in future if there's enough demand.

[SRFI-38] The read/ss reads a datum from given port.

The write/ss writes obj to given port.

These are the same as read and write procedure, but it can handle circular list.

[SRFI-28+] Formats arg accoding to string. _Port_specifies the destination; if it is an output port, the formatted result is written to it; if it is #t, the result is written to current output port; if it is #f, the formatted result is returned as a string. Port can be omitted and the result is the same as when #f is given.

String is a string that contains format directives. A format directive is a character sequence begins with tilda '~', and ends with some specific characters. A format directive takes the corresponding arg and formats it. The rest of string is copied to the output as is.

(format #f "the answer is ~a" 48)
the anser is 48

The format directive can take one or more parameters, separated by comma characters. A parameter may be an integer or a character; if it is a character, it should be preceded by a quote character. Parameter can be omitted, in such case the system default value is used. The interpretation of the parameters depends on the format directive.

Furthermore, a format directive can take two additional flags: atmark '@' and colon ':'. One or both of them may modify the behaviour of the format directive. Those flags must be placed immediately before the directive character.

The following complete list of the supported directives. Either upper case or lower case character can be used for the format directive; usually they have no distinction, except noted.

~mincol,colinc,minpad,padchar,maxcolA

Ascii output. The corresponding argument is printed by display. If an integer mincol is given, it specifies the minimum number of characters to be output; if the formatted result is shorter than mincol, a whitespace is padded to the right (i.e. the result is left justified). The colinc, minpad and padchar parameters control, if given, further padding. A character padchar replaces the padding character for the whitespace. If an integer minpad is given and greater than 0, at least minpad padding character is used, regardless of the resulting width. If an integer colinc is given, the padding character is added (after minpad) in chunk of colinc characters, until the entire width exceeds mincol. If atmark-flag '@' is given, the format result is right justified, i.e. padding is added to the left. The maxcol parameter, if given, limits the maximum number of characters to be written. If the length of formatted string exceeds maxcol, only maxcol characters are written. If colon-flag is given as well and the length of formatted string exceeds maxcol, maxcol - 4 characters are written and a string " ..." is attached after it.

(format #f "|~a|" "oops")
|oops|
(format #f "|~10a|" "oops")
|oops |
(format #f "|~10@a|" "oops")
| oops|
(format #f "|~10,,,'*@a|" "oops")
|\*\*\*\*\*\*oops|
(format #f "|~,,,,10a|" '(abc def ghi jkl))
|abc def gh|
(format #f "|~,,,,10:a|" '(abc def ghi jkl))
|abc de ...|

~mincol,colinc,minpad,padchar,maxcolS

S-expression output. The corresponding argument is printed by write. The semantics of parameters and flags are the same as ~A directive.

(format #f "|~s|" "oops")
|"oops"|
(format #f "|~10s|" "oops")
|"oops" |
(format #f "|~10@s|" "oops")
| "oops"|
(format #f "|~10,,,'*@s|" "oops")
|\*\*\*\*"oops"|

~mincol,padchar,commachar,intervalD

Decimal output. The argument is formatted as an decimal integer. If the argument is not an integer, all parameters are ignored and it is formatted by ~Adirective. If an integer parameter mincol is given, it specifies minimum width of the formatted result; if the result is shorter than it, padchar is padded on the left (i.e. the result is right justified). The default of padchar is a whitespace.

(format #f "|~d|" 12345)
|12345|
(format #f "|~10d|" 12345)
| 12345|
(format #f "|~10,'0d|" 12345)
|0000012345|
If atmark-flag is given, the sign '+' is printed for the positive argument. If colon-flag is given, every _interval_th digit of the result is grouped and commachar is inserted between them. The default of commachar is ',', and the default of interval is 3.
(format #f "|~:d|" 12345)
|12,345|
(format #f "|~,,'_,4:d|" -12345678)
|-1234_5678|

~mincol,padchar,commachar,intervalB

Binary output. The argument is formatted as a binary integer. The semantics of parameters and flags are the same as the ~D directive.

~mincol,padchar,commachar,intervalO

Octet output. The argument is formatted as a octal integer. The semantics of parameters and flags are the same as the ~D directive.

~mincol,padchar,commachar,intervalX
~mincol,padchar,commachar,intervalx

Hexadecimal output. The argument is formatted as a hexadecimal integer. If 'X' is used, upper case alphabets are used for the digits larger than 10. If 'x' is used, lower case alphabets are used. The semantics of parameters and flags are the same as the ~D directive.

(format #f "~8,'0x" 259847592)
0f7cf5a8
(format #f "~8,'0X" 259847592)
0F7CF5A8

Note: The format procedure's implementation and most of documentation is quoted from Gauche.

Returns #t when port data are ready, otherwise #f.

If the given port implementation does not support this functionality, the return value will be always #t. Following example describes when this always returns #t;

;; Assume read! is provided.
(define user-port (make-custom-binary-input-port "my-port" read! #f #f))
(port-ready user-port)
#t

port must be binary port.

Converts given port to buffered port if the port is not buffered port.

Buffered port is a type of port which does not read or write immediately but uses internal buffer. For example, file port with (buffer-mode block) or (buffer-mode line) uses the buffered port. This is useful when actual I/O is more expensive than memory access.

The buffer-mode must be a symbol the macro buffer-mode can return. If the buffer-mode is none, then the procedure does not convert the given port.

If the keyword argument buffer is specified, it must be a bytevector, then the converted buffered port uses specified bytevector as its internal buffer. If the bytevector size is zero or literal bytevector then &assertion is raised.

Creates a custom codec. Symbol is the name of the codec. _Getc_and putc must be procedures. Data is an user data used in _getc_and putc.

Getc must take 4 arguments, input-port, error-handling-mode, check-bom? and userdata. Input-port is binary input port. Error-handling-mode is symbol can be ignore, raise or replace depending on a transcoder which uses this custom codec. Check-bom? is boolean, if getc is being called first time, it is #t, otherwise #f. Userdata is user defined data which is given when the codec is created.

The basic process of getc is reading binary data from input-port and converting the data to UCS4. Returning UCS4 must be integer and does not have to be 4 byte.

Putc must take 4 arguments, output-port, char, error-handling-mode and userdata. Output-port is binary output port. Char is character object which needs to be converted from UCS4. Error-handling-mode is symbol can be ignore, raise or replace depending on a transcoder which uses this custom codec. Userdata is user defined data which is given when the codec is created.

The basic process of putc is converting given UCS4 charactner to target encoding data and putting it to output-port as binary data.

For sample implementation, see sitelib/encoding directory. You can find some custom codecs.

Symbols

Return #t if given symbol1 is less than, less than or equal to, greater than or greater than or equal to symbol2. If symbols are not null, then the procedures continue to compare until the given arguments are exhausted.

These procedures are analogy of string<?, string<=?, string>? and string>=?.

These can also be implemented as follows:

(define (symbol<? . syms) (apply string<? (map symbol->string syms)))

However, this build-in version won't converts given symbols to strings.

Keywords

Sagittarius has keyword objects which starts with ':'. It has almost the same feature as symbol, however it can not be bounded with any values. The keyword objects are self quoting so users don't have to put ' explicitly.

The keyword notation is *NOT* available on R6RS or R7RS reader mode. Thus #!r6rs or #!r7rs directive and -r6 or -r7command line option disable it.

Creates a new keyword from symbol.

Creates a new keyword from string.

Returns #t if obj is keyword, otherwise #f.

Returns a symbol representation of given keyword keyword.

Returns a string representation of given keyword keyword.

Returns the element after given keyword from given list.

The elements count of the list should be even number, otherwise the procedure might raise &error when keyword is not found in list.

If fallback is given and the procedure could not find the _keyword_from the list, the fallback will be return. Otherwise it raises &error.

(get-keyword :key '(a b c :key d))
d

(get-keyword :key '(a b c d e))
&error

(get-keyword :key '(a b c d e) 'fallback)
&error

(get-keyword :key '(a b c d e f) 'fallback)
fallback

Weak box

A weak box is a reference to an object that doesn’t prevent the object from being garbage-collected.

Returns #t if obj is weak box otherwise #f.

Returns a weak box whose value is obj.

Returns #t if the value of given weak box wb is garbage collocted. Otherwise #f.

Returns the value of given weak box wb if it's not garbage collocted. Otherwise returns fallback.

Replace the value of given wb with obj.

Weak vector

A weak vector is like a vector of objects, except each object can be garbage collected if it is not referenced from objects other than weak vectors. If the object is collected, the entry of the weak vector is replaced to #f.

Returns #t if obj is weak vector otherwise #f.

Creates and returns a weak vector of size size.

Returns the length of given weak vector wvec

Returns k-th element of a weak vector wvec.

By default, weak-vector-ref raise an &assertion if k is negative, or greater than or equal to the size of wvec. However, if an optional argument fallback is given, it is returned for such case.

If the element has been garbage collected, this procedure returns _fallback_if it is given, #f otherwise.

Sets k-th element of the weak vector wvec to value. It raises an &assertion if k is negative or greater than or equal to the size of wvec.

Weak hashtable

A weak hashtable contains references to an object that doesn't prevent the object from being garbage-collected the same as weak vector. A weak hashtable is like a hashtable, except each entry can be garbage collected if it is not referenced from objects other than weak hashtable according to the constructed condition.

Returns #t if obj is weak hashtable otherwise #f.

Make a weak hashtable with the hash procedure eq? and eqv?, respectively.

The keyword argument init-size specifies the initial size of the weak hashtable.

The kwyword argument weakness specifies the place where the weak pointer is. This must be one of the key, value and both. If the key is specified, then weak hashtable's weak pointer will be the key so is value. If the both is specified, then it will be both.

The keyword argument default specifies the default value when the entry is garbage collected. If this is not spceified, then undefined value will be used.

Returns the value in weak-hashtable associated with key. If weak-hashtable does not contain an association for key, default is returned.

Changes weak-hashtable to associate key with obj, adding a new association or replacing any existing association for key, and returns unspecified values.

Removes any association for key within weak-hashtable and returns unspecified values.

Returns a list of keys and values in the given weak-hashtable, respectively.

Returns a copy of weak-hashtable.

Shrink the given weak-hashtable and returns the number of removed entry. This is only for GC friendliness.

Bytevector operations

Converts given bytevector bytevector to exact integer. If optional argument start is given, conversion starts with index start. If optional argument end is given, convertion ends by index end. The conversion only happens in big endian.

The first form converts to signed integer and the rest convert to unsigned integer.

Ei must be exact integer. Converts exact integer ei to a bytevector.

The first form can accept signed integer and converts with two's complement form. The rest forms only accept unsigned integer and simply convert to bytes.

If optional argument size is given, then the procedure returns _size_size bytevector.

NOTE: The conversion is processed from right most byte so if the size is smaller than given ei bytes, then the rest of left bytes will be dropped.

NOTE: the endianness is always big endianness.

(integer->bytevector #x12345678 5)
#vu8(#x00 #x12 #x34 #x56 #x78)

(integer->bytevector #x12345678 3)
#vu8(#x34 #x56 #x78)

Returns a newly allocated bytevector that contains all elements in order from the subsequent locations in bvs ....

Appends each bytevectors in list-of-bytevectors. This is equivalent to:

(apply bytevector-append _list-of-bytevectors_)

List operations

[SRFI-1] Returns #t if list is circular or dotted list, respectively. Otherwise #f.

Returns (cons (cons _obj1_ _obj2_) _obj3_). Useful to put an entry at the head of an associative list.

[SRFI-1] Returns a list consisting of the elements of the first _list_followed by the elements of the other lists. The cells in the lists except the last one may be reused to construct the result. The last argument may be any object.

[SRFI-1] Returns a list consisting of the elements of list in reverse order. The cells of list may be reused to construct the returned list.

Vector operations

[SRFI-43] Copies a vector vector. Optional start and _end_arguments can be used to limit the range of vector to be copied. If the range specified by start and end falls outside of the original vector, the fill value is used to fill the result vector.

(vector-copy '#(0 1 2 3 4) 1 4)
#(1 2 3)

(vector-copy '#(0 1 2 3 4) 4 6 #f)
#(4 #f)

(vector-copy '#() 0 2 #t)
#(#t #t)

[SRFI-43] Returns a newly allocated vector that contains all elements in order from the subsequent locations in vector ....

(vector-append '#(1 2) '#(a b) '#(5))
#(1 2 a b 5)

[SRFI-43] Appends each vectors in list-of-vectors. This is equivalent to:

(apply vector-append _list-of-vectors_)

[SRFI-43] Reverse the given vector's elements.

The second form reverses the given vector destructively.

Optional arguments start and end must be non negative integer if it's given. And it restricts the range of the target elements.

String operations

Scan item (either a string or a character) in string.

The return argument specified what value should be returned when item is found in string. It must be one of the following symbols;

index

Returns the index in string if item is found, or #f. This is the default behaviour.

before

Returns a substring of string before item, or #f if item is not found.

after

Returns a substring of string after item, or #f if item is not found.

before*

Returns a substring of string before item, and the substring after it. If item is not found then return (values #f #f).

after*

Returns a substring of string up to the end of item, and the rest. after it. If item is not found then return (values #f #f).

both

Returns a substring of string before item and after item. If item is not found, return (values #f #f).

[SRFI-13] Appends each strings in list-of-strings. This is equivalent to:

(apply string-append _list-of-strings_)

Returns #t if the given obj is an immutable string, otherwise #f.

Immutable strings are one of the followings:

Returns copy of the given string. The returning string is converted to immutable string.

The optional argument start and end are given, then the returing string is contains only the range of given string.

Load path

Add given path to current loading path list.

If keyword argument append is #t, then given path is appended to the current loading path list, otherwise prepend to the list.

Add given suffix to current suffix list.

If keyword argument append is #t, then given suffix is appended to the current suffix list, otherwise prepend to the list.

The suffix should contain . as well.

Function load-path

Retrives/sets current loading path.

If the latter form is used, then the given path list paths is set to current loading path list. Otherwise retrieves the list.

The returning value is not copied, thus modifying the list may cause unexpected results.

System and platform

Function uname

Return a vector which contains the following information.

System name

Operating system name. E.g. "Linux"

Node name

The name of the computer.

Release

Release number of the system if availabel.

Version

Version number of the system if availabel.

Machine

Machine architecture of the system.

On POSIX environment, this is the result of uname(2).

On Windows environment, this is the result of couple of Win32 API calls and formatted to look like the result of uname(2).

Retrieves MAC address. The returning value is a bytevector which contains 6 elements.

If optional argument specifies which NIC's MAC address should be returned.

Returns a vector having 3 elements; user time, _system time_and tick.

The first procedure returns process CPU time, the second procedure returns thread CPU time. If the optional argument thread is given, then the procedure retrieves CPU time of given thread. Otherwise current thread.

tick is a clock tick. Users can compute CPU second Dividing user time or system time by this value.

NOTE: tick may not be the same even on the same platform. For example, get-process-times returns value of _SC_CLK_TCK, and get-thread-times returns 1000000 (on OSX), 1000000000 (on other POSIX environments which have pthread_getcpuclockid), or _SC_CLK_TCK(on other POSIX environments which don't have pthread_getcpuclockid). So users must not assume the value is one of them and calculate CPU second with assumed value.

Function gc

Invokes garbage collection manually.

Function cpu-count

Returns number of CPUs. The result contains hyperthreading, for example, if the CPU is Core i5 which have 2 cores and hyperthreading is enabled, then the returning value is 4.

On Linux environment, the procedure also considers taskset(1).

This procedure returns static value initialised during initialisation of Sagittarius process. Thus, if the process is restricted after its initialisation by taskset(1), for example the environment has 4 CPUs but restricted to 1, then this process, however, returns 4.

Debugging aid

Disassembles the compiled body of closure and print it.

Returns arity of given procedure.

It returns a pair whose car part is required arguments count and cdr part is boolean which indicates if the procedure accepts optional arguments or not.

Library (time)

Exports time macro

Evaluate expr and shows time usage.

The macro return the result of expr.