Home Online Manual
Back: Flow control
Forward: Procedures
FastBack: The SINGULAR language
FastForward: Data types
Up: General concepts
Top: Singular Manual
Contents: Table of Contents
Index: Index
About: About this document

3.6 Input and output

SINGULAR's input and output (short, I/O) are realized using links. Links are the communication channels of SINGULAR, i.e., something SINGULAR can write to and read from. In this section, a short overview of the usage of links and of the different link types is given.

For loading of libraries, see LIB. For executing program scripts, see filecmd.


A special form of I/O is monitoring. When monitoring is enabled, SINGULAR makes a typescript of everything printed on your terminal to a file. This is useful to create a protocol of a SINGULAR session. The monitor command enables and disables this feature (see monitor).

How to use links

Recall that links are the communication channels of SINGULAR, i.e., something SINGULAR can write to and read from using the functions write and read. There are furthermore the functions dump and getdump which store resp. retrieve the content of an entire SINGULAR session to, resp. from, a link. The dump and getdump commands are not available for DBM links.

For more information, see write, read, dump, getdump.


  ring r; poly p = x+y;
  dump(":w test.sv");   // dump the session to the file test.sv
  kill r;               // kill the basering
  listvar();            // no output after killing the ring
  getdump(":r test.sv");// read the dump from the file
==> // r                              [0]  *ring
==> //      p                              [0]  poly

Specifying a link can be as easy as specifying a filename as a string. Links do not even need to be explicitly opened or closed before, resp. after, they are used. To explicitly open or close a link, the open, resp. close, commands may be used (see open, close).

Links have various properties which can be queried using the status function (see status).


  link l = "ssi:fork";
==> // type : ssi
==> // mode : fork
==> // name : 
==> // open : no
==> // read : not open
==> // write: not open
  status(l, "open");
==> yes
  status(l, "open");
==> no

ASCII links

Data that can be converted to a string can be written into files for storage or communication with other programs. The data are written in plain ASCII format. Reading from an ASCII link returns a string --- conversion into other data is up to the user. This can be done, for example, using the command execute (see execute).

ASCII links should primarily be used for storing small amounts of data, especially if it might become necessary to manually inspect or manipulate the data.

See ASCII links, for more information.


  // (over)write file test.ascii, link is specified as string
  write(":w test.ascii", "int i =", 3, ";");
  // reading simply returns the string
==> int i =
==> 3
==> ;
  // but now test.ascii is "executed"
==> 3

Ssi links

Data is communicated with other processes (e.g., SINGULAR processes) which may run on the same computer or on different ones. Data exchange is accomplished using TCP/IP links in the ssi format. Reading from an ssi link returns the written expressions (i.e., not a string, in general).

Ssi links should primarily be used for communicating with other programs or for parallel computations (see, for example, Parallelization with ssi links).

See Ssi links, for more information.


  ring r;
  link l = "ssi:tcp localhost:"+system("Singular"); // declare a link explicitly
  open(l);  // needs an open, launches another SINGULAR as a server
  write(l, x+y);
  kill r;
  def p = read(l);
  typeof(p); p;
==> poly
==> x+y
  close(l); // shuts down SINGULAR server

DBM links

Data is stored in and accessed from a data base. Writing is accomplished by a key and a value and associates the value with the key in the specified data base. Reading is accomplished w.r.t. a key, the value associated to it is returned. Both the key and the value have to be specified as strings. Hence, DBM links may be used only for data which may be converted to or from strings.

DBM links should primarily be used when data needs to be accessed not in a sequential way (like with files) but in an associative way (like with data bases).

See DBM links, for more information.


  ring r;
  // associate "x+y" with "mykey"
  write("DBM:w test.dbm", "mykey", string(x+y));
  // get from data base what is stored under "mykey"
  execute(read("DBM: test.dbm", "mykey"));
==> x+y