[previous] [up] [next]     [index]
Next: Variable Definitions Up: Intermezzo 1 Syntax and Semantics Previous: Errors

Boolean Expressions

Our current definition of the Beginning Student Scheme language omits two forms of expressions: and and or expressions. Adding them provides a case study of how to study new language construct. We must first understand their syntax, then their semantics, and finally their pragmatics.

Here is the revised grammar:

tabular8111

The grammar says that and and or are keywords, each followed by two expressions. At first glance, the two look like (primitive or function) applications. To understand why they are not, we must look the pragmatics of these expressions first.

Suppose we need to formulate a condition that determines whether the n-th fraction of 1 is m:

(and (not (= n 0)) 
     (= (/ 1 n) m)) 
We formulate the condition as an and combination of two boolean expressions, because we don't wish to divide by 0 accidentally. Next, assume n becomes 0 during the course of the evaluation. Then the expression becomes
(and (not (= 0 0)) 
     (= (/ 1 0) m)) 
Now, if and were an ordinary operation, we would have to evaluate both subexpressions, which would trigger an error in the second one. For this reason, and is not a primitive operation, but a special expression. In short, we use and and or expressions to combine boolean computations that may have to short-cut an evaluation.

Once we understand how and and or expressions should be evaluated, it is easy to formulate matching rules. Better still, we can formulate expressions in our first language that are equivalent to these expressions:

(and  tex2html_wrap_inline71959   tex2html_wrap_inline71961 )
 tex2html_wrap_inline71963  
(cond 
  [ tex2html_wrap_inline71959   tex2html_wrap_inline71961 ] 
  [else false]) 
and
(or  tex2html_wrap_inline71959   tex2html_wrap_inline71961 )
 tex2html_wrap_inline71963  
(cond 
  [ tex2html_wrap_inline71959  true] 
  [else  tex2html_wrap_inline71961 ]) 
These equivalences simplify what actually takes place in DrScheme but they are a perfectly appropriate model for now.


[previous] [up] [next]     [index]
Next: Variable Definitions Up: Intermezzo 1 Syntax and Semantics Previous: Errors

PLT