Reflection and Sentantics in Lisp Brian Cantwell Smith XEROX Pale
Total Page:16
File Type:pdf, Size:1020Kb
Reflection and Sentantics in Lisp Brian Cantwell Smith XEROX Pale Alto Research Center 3333 Coyote Hill Road. Pale Alto. CA 94304; and Center for the Study of l.anguage and Information Stantbrd University. Stanlbrd. CA 94305 and continuation char;wtcrising the state of the computation at 1. Introduction that pui,~t. Thus, such constructs as ttmow and C,~TCII, which l"or three reasons, bi.';p's self-refi;rential properl.ies have not must otherwise be providt,d primitively, can in 3-Lisp be easily led to a general un(h:rst.auding of what it is fro" a cmuputational defined a:; user procedures (and defined, furthermore, in code system to reason, in substantial way~, about its; owe operations that is ~,!most isomorphic to the ~-calculus. equations one a,ul structures. First., there is more to reasoning than reference; normally writes, in the metalal'$,3~a!,'c, to describe them). And one also needs a theory, in terms of which to make .,~ense of the all this can be dolte wilhout writing the entire program in a referenced domain. A comln, ter system able to reason about centinuation-pas:;iz~g :~tyle, o!' the sort illu,;trated in [Steele it.:;elf- what I will call a reflective system -- will therefore 197til. The point is no!. to decide at the outset what should and need an account of itself embedded within it. Second, there what should not be explicit (in Steele's example, continuations most he a systematic relationship between that embedded must be passed arouml explicitly from the hcgim, ing). Rather, account and the system it describes. Without such a connection, the retlective architecture provides a method of making some the account would be useless -- as disconnected an the words of aspects of the computation explicit, right in the midst of a a haple~;s drunk who carries on about the evils of inebriation, computation, even if they were implicit a moment earlier. It without reali~iug that his story applies to himself. Tl'aditional provides a mech'mism, in other wo~'ds, of reaching up and embeddiugs of IAsp in Lisp are inadequate in just this way; they "pulling information out of the sky" when unexpected provide no means for the implicit state of the Lisp process to he circumstances warrant it, without having to worry about it reflected, moment by moment, in the explicit terms of the otherwise. embecbled account. Tlaird, a reflective system nmst be given an The overall claim is that retlection is simple to build on a appropriate vantage point at which to stand, far enough away to semantically sound hase, where 'semantically sound' means have itself in focus, and yet close enough to see the important more than that the semantics be earefl~lly formulated. Rather, I details. assume throughout that computational structures have a This paper presents a general architecture, called semantic significance that transcends their behavioural import procedurcd refh'ctio,, to support sell'directed reosoning in a -- or, to put this another way, that cmnputatinnal structures are serial programming lmaguage. Tim architecture, illustrated in a about something, over anti above the effects they have on the revamped dialect called 3-Lisp, solves all three problems with a systems they inhabit. Lisp's Nft. for example, not only single mechanism. The basic idea is to define an infinite tower ev~tluates to itself forever, but also (and somewhat of procedural self-nmdels, very much like mctacircular independently) stands for Falsehood. A reconstruction of Lisp interpreters [Steele and Sussman 1978b], except connected to semantics, therefore, must deal explicitly with both declarative each other in a simple but critical way. In such an architecture, and procedural ospects of the overall significance of any aspect of a procc~s's state that can be described in terms of computational structures. This distinction is different from One theory can be rendered explicit, in program accessihle (though I will coutrast it with) the distinction between structures. Furthermore, as we will see, this apparently infinite operalional and denotational semantics. It is a reconstruction architecture can be finitely implemented. has boca developed within a view that programming languages The architecture allows the user to define complex are properly to be understood in the same theoretical terms used programming constructs {such as escape operators, deviant to analyse not only other computer languages, but even natural variableqmssing protocols, and dehugging primitives), by writing languages. direct analogues of those metalinguistie semantical expressions This approach forces us to distinguish between a structure's that would normally be used to describe them. As is always wdue and what it returns, and to discriminate entities, like true in semantics, the metatheoretie descriptions must be numerals and numbers, that are isomorphic but not identical phrased in terms of some particular set of concepts; in this case (both instances of the general intellectual hygiene of avoiding I have used a theory of Lisp ba:;ed on environments and use/mention errors). Lisp's basic notion of evaluation, I will continuations. A 3-Lisp program, therefore, at any point during argue, is confused in this regard, and should be replaced with a computation, can obtain representations of the environment independent notions of designation and simplification. The result is illustrated in a semantically rationalised dialect, called Permission to copy without fee all or part of this material is granted 2-Lisp, based on a simplifying (designation-preserving) term- provided that the copi~ are not made or distributed for direct reducing processor. The point of defining 2-Lisp is that the commercial advantage, the ACM copyright notice and the title of the reflective 3-Lisp can be very simply defined on top of it, whereas publication and its date appear, and notice is given that copying is by defining a reflective version of a non-rationalised dialect would permission of the Association for Computing Machinery. To copy be more cmnplicated and more difficult to understand. otherwise, or to republish, requires a fee and/or specific permission. The strategy of presenting a general architecture by developing a concrete instance of it was selected on the grounds that a gemfine theory of reflection (perhaps analogous to the theory of rccursion) would be difficult to motivate or defend © 1983 ACM 0-89791-125-3/84/001/0023 $00.75 without taking this first, more pragtnatic, step. In section lO, 23 however, we will sketch a general "recipe" for adding reflective capabilities to any serial language; 3-Lisp is the result of applying this conversion process to the non-reflective 2-Lisp. It is sometimes said that there are only a few con'~truc~.s fi'om which languages are a,~sembled, ihcluding for example predicates, terms, functions, composition, recursion, abstraction, a branching eulnctor, end quantification. Though differellt from Ihe:~e notions (and not definable iJ~ terms of them), reflection is perhaps best viewed as a preposed addition to that family. Given this view, it is helpfid to understand relleci.ion by Figure l : A Serial Medel of Cemputation comparing it, ia particular, with L'ecursion -- a construct with | which it shares many features. Specifically, recursion can seem causal relationship, htrtherinore, must go both ways: fi'om event viciously circldar to the uninitiated, and can lead to confused to description, and from description back to event. (It is as if we implementations if poorly understood. The mathematical theory were creating a magic kingdom, where fl'om a cake you could ef recursion, however, underwrites our ability to usa reeursion automatically get a recipe, and from a recipe you could in programmiug languages without doubting its fundamental automatically get a cake.) In mathematical cases of self- soundness (in thct, for many programmers, without reference, including both self-referential statements, and models understanding much about the formal theory at all). Reflective of syntax and proof theory, there is of course no causation at all, systems, similarly, initially seem viciously circular (or at least since there is no temporality or behaviour (mathematical infinite), and are difficult to implement without an adequate systems don't run). Causation, however, is certainly part of any understanding. The intent of this paper, however, is to argue reflective agent. Suppose, for example, that you capsize while that reflection is as well-tamed a concept as recursion, and canoeing through dit/icult rapids, and swim to the shore to potentially as efficient to use. Tim long.range goal is not to figure out what you did wrong. You need a description of what force programmers to understand the intricacies of designing a you were doing at the moment the mishap occurred; merely reflective dialect, but rather to enable them to use reflection and having a name for yoursell, or even a general description of recursion with equal abandon. yourself, would be usele~. Also, your thinking must be able to have some effect; no good will come from your merely 2. Motivating Intuitions contemplating.a wonderful theory of an improved you. As well Before taking up technical details, it will help to lay out as stepping back and being able to think about your behaviour, seme motivations and assumptions. First, by 'reflection' in its in ether words, you must also be able to take a revised theory. most general sense, I mean tire ability of an agent to reason not and "dive back in under it", adjusting your behaviour so as to only introspectively, about its self and internal thought satisfy the new account. And finally, we mentioned that when processes, but ~.lso externally, about its behaviour and situation you take the step backwards, to reflect, you need a place to in the world.