Error: no such theme PatternSkinTheme

Questions about the readings

Chapter 1

Q. Is there a reason that the predicates are consistently on the right in the define-type on page 6?

A. Yes, there is. A subexpression like (n number?) in a define-type expression isn't a procedure call; it's more like a field declaration in Java. n is the field name, and number? is the precondition that a value must meet in order to be stored in that field.


Q. Should the code example on page 8 in the book read (define parse (sexp) ...) instead of (define (parse sexp)...?

A. No, it's OK as it is. You may not have seen them in CSC 151, but Scheme provides "short form" procedure definitions: Any definition of the form

(define 
  (lambda ( ...)
    )

can be written more concisely as

(define (  ...)
  )

Variable-arity procedure definitions can be similarly abbreviated -- one places a dot just before the parameter that absorbs the leftover arguments.

Sometimes we omit short-form definitions from CSC 151 because they are redundant in principle, because they obscure the fact that procedures are values in Scheme, and because they are less general. There is no short form for procedures that manage static variables. For instance,

(define switch
  (let ((state 'off))
    (lambda ... )))

has no short form.


Q. What's the big deal about concrete vs. abstract syntax? Isn't a tree that represents only the abstract syntax of an expression or a program just a different way of arranging the same information that's in a full parse tree that uses the concrete syntax?

A. The abstract syntax prunes away redundancies. You keep all the information that is needed for executing the program, but you discard a lot of nodes that duplicate structural information or are needed only because we have to express a program structure that is naturally hierarchical in a linear sequence of characters.

Consider the AE program {+ {- 7 4} 9}, and compare the parse tree that we would get for the concrete syntax, which is

.-----.----------------add----------------.-----.
|     |                 |                 |     |
|     |     .-----.----sub----.-----.     |     |
|     |     |     |     |     |     |     |     |
|     |     |     |    num   num    |    num    |
|     |     |     |     |     |     |     |     |
{     +     {     -     7     4     }     9     }

with the abstract-syntax parse tree, which is

     .----add----.
     |           |
 .--sub--.       |
 |       |       |
num num num | | |
 7       4       9

Once parsing is completed, we don't need the braces any more, because the hierarchical data structure indicates the grouping; and we don't need the operator symbols any more, because we can recover the information they carried by inspecting which variant of AE is used at each internal node.


Original Source

==Chapter 1==

'''Q.''' Is there a reason that the predicates are consistently on the right in the <code>define-type</code> on page 6?

'''A.''' Yes, there is.  A subexpression like <code>(n number?)</code> in a [[CSC 302: define-type|<code>define-type</code>]] expression isn't a procedure call; it's more like a field declaration in Java.  <code>n</code> is the field name, and <code>number?</code> is the precondition that a value must meet in order to be stored in that field.

----

'''Q.''' Should the code example on page 8 in the book read <code>(define parse (sexp) ...)</code> instead of <code>(define (parse sexp)...</code>?

'''A.''' No, it's OK as it is.  You may not have seen them in CSC 151, but Scheme provides "short form" procedure definitions: Any definition of the form 

<pre>
(define <identifier>
  (lambda (<parameter> ...)
    <body>)
</pre>

can be written more concisely as

<pre>
(define (<identifier> <parameter> ...)
  <body>)
</pre>

Variable-arity procedure definitions can be similarly abbreviated -- one places a dot just before the parameter that absorbs the leftover arguments.

Sometimes we omit short-form definitions from CSC 151 because they are redundant in principle, because they obscure the fact that procedures are values in Scheme, and because they are less general.  There is no short form for procedures that manage static variables.   For instance,

<pre>
(define switch
  (let ((state 'off))
    (lambda ... )))
</pre>

has no short form.

----

'''Q.''' What's the big deal about concrete vs. abstract syntax?  Isn't a tree that represents only the abstract syntax of an expression or a program just a different way of arranging the same information that's in a full parse tree that uses the concrete syntax?

'''A.''' The abstract syntax prunes away redundancies.  You keep all the information that is needed for executing the program, but you discard a lot of nodes that duplicate structural information or are needed only because we have to express a program structure that is naturally hierarchical in a linear sequence of characters.

Consider the AE program {+ {- 7 4} 9}, and compare the parse tree that we would get for the concrete syntax, which is

<pre>
.-----.----------------add----------------.-----.
|     |                 |                 |     |
|     |     .-----.----sub----.-----.     |     |
|     |     |     |     |     |     |     |     |
|     |     |     |    num   num    |    num    |
|     |     |     |     |     |     |     |     |
{     +     {     -     7     4     }     9     }
</pre>

with the abstract-syntax parse tree, which is

<pre>
     .----add----.
     |           |
 .--sub--.       |
 |       |       |
num     num     num
 |       |       |
 7       4       9
</pre>

Once parsing is completed, we don't need the braces any more, because the hierarchical data structure indicates the grouping; and we don't need the operator symbols any more, because we can recover the information they carried by inspecting which variant of AE is used at each internal node.

----
Topic revision: r2 - 2010-07-14, JeffLeep
 

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback