[antlr-interest] question about continuation based i/o in Andrew w
Appel's book
=?gb2312?q?=CB=EF=BC=CD=B8=D5=20Jigang=20(Robert)=20Sun?=
sunjigang1965 at yahoo.com.cn
Sun May 14 04:50:44 PDT 2006
The following is extracted from Andrew W Appel's book Modern Compiler
Implementation in Java, Second Edition, section 15.3 page 304. I do not
understand the built-in type "answer". Besides the following
paragraphs, no other explanation was given in this book. It seems that
continuation i/o and the type answer is supposed known to his readers
as backgroud knowledge; but I have no ideas about it.
Could anyone tell me relevant links or tutorials, course notes, etc
talking about the type. I want to know what it is and how it is
implemented.
Thanks in advance.
Robert
CONTINUATION-BASED I/O
Producing new data structures instead of updating old ones makes it
possible to obey the "no assignments" rules, but how is the program to
do input/output? The technique of continuation-based I/O expresses
input/output in a functional framework. As shown in Program 15.4, the
predefined types and functions in PureFunJava rely on the notion of an
answer: This is the "result" returned by the entire program.
PROGRAM 15.4: Built-in types and functions for PureFunJava.
type answer // a special, built-in type
type intConsumer = int -> answer
type cont = () -> answer
class ContIO
{
public answer readByte (intConsumer c);
public answer putByte (int i, cont c);
public answer exit();
}
MiniJava doesn't have an input function, but if it did, the type would
be straightforward: something like int readByte(). To express this
without side effects, PureFunJava's readByte takes an argument that is
a int-Consumer and passes the newly read integer to that consumer.
Whatever answer the consumer produces will also be the answer of the
readByte.
Similarly, putByte takes a character to print as well as a continuation
(cont); putByte outputs a character and then calls the cont to produce
an answer.
The point of these arrangements is to allow input/output while
preserving equational reasoning. Interestingly, input/output is now
"visible" to the typechecker: Any function which does I/O will have
answer in its result type.
___________________________________________________________
ÇÀ×¢ÑÅ»¢Ãâ·ÑÓÊÏä-3.5GÈÝÁ¿£¬20M¸½¼þ£¡
http://cn.mail.yahoo.com
More information about the antlr-interest
mailing list