Introduction to Programming in ATS: | ||
---|---|---|
<<< Previous | Effectful Programming Features | Next >>> |
Handling I/O in ATS properly requires the availability of both dependent types and linear types, which I will cover elsewhere. In this section, I only present a means for allowing the programmer to access certain very basic I/O functionalities.
A file handle essentially associates a stream (of bytes) with a file identifier (represented as an integer). In ATS, the type for file handles is FILEref. There are three standard file handles, which are listed as follows:
stdin_ref: standard input
stdout_ref: standard output
stderr_ref: standard error output
fun open_file_exn (path: string, mode: file_mode): FILEref // end of [open_file_exn] fun close_file_exn (fil: FILEref): void |
file_mode_r: opening a file for reading and positioning the associated stream at the beginning of the file.
file_mode_rr: opening a file for both reading and and writing and positioning the associated stream at the beginning of the file.
file_mode_w: truncating a given file to zero length or creating a new one for writing and positioning the associated stream at the beginning of the file.
file_mode_ww: truncating a given file to zero length or creating a new one for both reading and writing and positioning the associated stream at the beginning of the file.
file_mode_a: opening a file for writing and positioning the associated stream at the end of the file.
file_mode_aa: opening a file for both reading and writing and positioning the associated stream at the beginning of the file for reading and at the end for writing.
// // The following line is needed for the compiler // to gain access to some library I/O functions: // staload _(*anon*) = "libc/SATS/stdio.sats" implement main () = () where { val out = open_file_exn ("hello.txt", file_mode_w) val () = fprint_string (out, "Hello, world!\n") val () = close_file_exn (out) } // end of [main] |
Another two common I/O functions are given the following interfaces:
The function input_line reads a line from the stream in a given file handle, and it returns a value of the type Stropt. For the moment, let us equate Stropt with the type option0(string). If the return value is constructed by option0_none, then the stream has reached the end when input_line is called. Otherwise, the return value is of the form option0_some(str), where str represents the line read from the stream minus the ending newline symbol. The function output_line writes its second argument, which is a string, and a newline symbol into the stream associated with its first argument, which is a file handle. As an example, the following short program echos each line received from the standard input onto the standard output:staload _(*anon*) = "libc/SATS/stdio.sats" implement main () = loop () where { fun loop (): void = let val line = input_line (stdin_ref) in if stropt_is_some (line) then let val () = output_line (stdout_ref, stropt_unsome (line)) in loop () end else () // loop exits as the end-of-file is reached // end of [if] end (* end of [loop] *) } // end of [main] |
<<< Previous | Home | Next >>> |
Example: Estimating the Constant Pi | Up | Convenience in Programming |