TH CALCULATOR After looking at the basics of the denotational method with the binary numerals we will take chmidt s example of a calculator to bring us one step closer to looking at the semantics of a real language. The calculator we shall study is essentially a two-function calculator that operates on natural numbers only (no negatives). It is unusual in that it has a conditional built into it, and it can handle numbers of any size. How it does this is a mystery, but we are only interested in semantics, not in engineering. It looks like this: < > O OFF 7 8 9 4 5 6 1 2 3 0 ( ) LATAWR + IF, TOTAL At the top is a display with two scroll buttons (for those long numbers). There are O and OFF buttons, the usual digit buttons, 0-9, and two operator buttons for addition and multiplication. The IF and the comma buttons work together as we shall see. TOTAL makes the calculator compute the result of the previously input expression. The LATAWR button recalls the value of the last calculation and inserts the value into the current one. YTAX A real calculator has all kinds of problems to solve when the user pushes the wrong buttons. For instance what happens the + button is pushed twice in a row? What happens if the parentheses are not matched? What happens if the IF form is not used correctly? We are going to ignore all of these complications, and instead only allow correctly formed calculations. We can do this because our first step in syntax is to describe correct forms, not the incorrect ones. The abstract syntax is then: P Program xpression-sequence xpression umeral igit P :: O :: TOTAL TOTAL OFF :: 1 + 2 1 2 ( ) IF 1, 2, 3 LATAWR :: :: 0 1 2 3 4 5 6 7 8 9 0
Clearly in this grammar the terminal symbols are given by what is on the button (O, LATAWR, etc.); P,, and are the main non-terminals. A typical program would be: O 1 + 2 TOTAL 3 LATAWR TOTAL OFF, which would show 3 and then 9 in the display. A program can thus contain many calculations; something we will have to show in the semantics. MATIC ALGBRA This calculator only deals with natural numbers, so clearly the domain at used in the binary numeral example is also used here. In addition to arithmetic, we will also need an equality operation for the IF test. The domain is then: omain: at zero, one, two, : at plus : at at at times : at at at equals : at at Tr We will need the Boolean domain for the last operation: omain: Tr true, false : Tr :Tr A A A, for any domain A. This last operation is the conditional form that we used to extend the lambda calculus. otice that we have written all the functions out in curried form, without the cross product. VALUATIO FUCTIO Following the method used for the binary numerals, each non-terminal in the grammar has a corresponding function, overloaded with each of the alternatives that define the non-terminal. We need to define the functionality of each before we start, so that each alternative equation is correctly defined. tarting with the syntactic category, Program, we need to decide what its function will return. We have three choices. We could have it return nothing, since the calculator has done its job when the OFF button is pushed, or we could have it return the last value calculated. either of these choices capture the whole story, so we will choose to have it return a list of all the values calculated. The denotation of our program will then be at, or a list of all the values calculated. o, we have: P: Program at The list is actually a domain with the following definition: omain: List A, where A is any domain nil : List hd : List A hd( a cons a nil) a tl : List List 1 2 1 tl( a cons a nil) a nil 1 2 2 cons : A List List cons( a, a cons a nil) a cons a nil 0 1 2 0 1
This is reminiscent of the language Lisp, or cheme, which, of course, have lambda calculus as their origin. We can define these operations in pure lambda calculus, but the expressions then become overly complicated, so we will omit them, just as we omit the definitions of arithmetic. hd returns the first item of the list, tl returns the list without the first item, and cons adds an item to the front of an existing list. nil is the empty list. We will only need cons and nil for our example, in order to form the list of values to be returned. The expression sequence,, actually produces this list of values, but it has a hidden parameter, since any one of the expression in the sequence can use the value from LATAWR. Thus we have: : xpression-sequence at at or, : xpression-sequence at at in curried form. The parentheses surround the output, while the rest are inputs. The expression function is easy now, since it only produces one value: : xpression at at Again, the extra parameters are for LATAWR. umerals are easy, and the function for digits is trivial: : umeral at : igit at Let us finish up the equations for the constants first: 0 zero 9 nine ( ten times ) plus xpressions can be simple: ( n) n ( ) ( ) ( n) The argument n is the value of the calculator s memory that can be used by pressing LATAWR: LATAWR ( n) n Arithmetic is now easy: 1+ 2 1 2 1 2 n 1 n times 2 ( n) ( n) plus ( n) ( ) ( ) ( n) The conditional form IF 1, 2, 3 will be given the semantics that if the first expression evaluates to zero, then the value of the expression will be the second expression, otherwise the value will be the third one. This is where the equals operations comes in: 1 2 3 ( 1 ) 2 3 IF,, ( n) ( n) equals zero ( n) ( n) We finish up with the expression sequence: n n n n cons TOTAL OFF ( n) ( n) TOTAL ( ) let ( ) in ( n )
otice how the result of calculating the first expression in the sequence is passed to the rest of the sequence, but is remembered as the head of the list. The final one in the sequence need not be passed on since the OFF button has been pushed. Finally the program itself: P O ( zero) where the memory of the calculator is initialized to zero for the first expression in the sequence. UMMARY OF MATIC FIITIO It is instructive to collect all the definitions together in one place to see the whole picture. We do this below: 1. yntax P Program xpression-sequence xpression umeral igit P :: O :: TOTAL TOTAL OFF :: 1 + 2 1 2 ( ) IF 1, 2, 3 LATAWR :: :: 0 1 2 3 4 5 6 7 8 9 0 2. emantic algebras omain: at zero, one, two, : at plus : at at at times : at at at equals : at at Tr omain: Tr true, false : Tr :Tr A A A,for any domain A. omain: List A, where A is any domain nil : List hd : List A tl : List List cons : A List List 3. Valuation functions
P: Program at P O ( zero) : xpression-sequence at at n n n n cons TOTAL OFF ( n) ( n) TOTAL ( ) let ( ) in ( n ) : xpression at at 1+ 2 1 2 1 2 n 1 n times 2 ( n) ( n) plus ( n) ( ) ( ) ( n) ( n) n ( ) ( ) ( n) 1 2 3 ( 1 ) 2 3 IF,, ( n) ( n) equals zero ( n) ( n) LATAWR ( n) n : umeral at : igit at ( ten times ) plus 0 9 XAMPL RIVATIO zero nine In order to show how the functions work, let us take the example program O 1 + 2 TOTAL 3 LATAWR TOTAL OFF and derive its denotation: P zero O 1 + 2 TOTAL 3 LATAWR TOTAL OFF 1 + 2 TOTAL 3 LATAWR TOTAL OFF ( ) let n 1 + 2 ( zero) in n cons 3 LATAWR TOTAL OFF ( n ) At this point we can work on the first expression to find what 1 + 2 ( zero) 1( zero) plus 2( zero) 1 plus 1 1 plus 1 one plus two We could reduce this to three at this point or leave it unevaluated. This is the difference between the normal order of reduction and the applicative order. Here we will use the applicative order, and pass three to the rest of the sequence: n is:
three cons ( 3 LATAWR ( )) 3 LATAWR TOTAL OFF ( three) three cons three Again, we work on the expression separately: 3 LATAWR ( three) three times 3 times three 3 times three 3( ) LATAWR( three) three times three nine The whole derivation thus reduces to: three cons nine which is the program s static denotation. If we had left the domain algebra expression unevaluated, we would have had: ( ) ( one plus two) cons three times ( one plus two) In a sense this is like the compiled version of the program. The syntax has disappeared and the result is a more primitive program running on the calculator s virtual machine, which is purely abstract in this case. otice that, denotationally speaking, three and one plus two cons three times one plus two are ( ) cons nine ( ) ( ) identical, since they both denote the same list. The difference is purely in the level of representation. LAMBA FORM FOR VALUATIO FUCTIO very valuation function has an alternative expansion of a syntactic category as its first argument. Where there are other arguments necessary, as in the functions for and, then we can write these using the lambda notation: λ TOTAL OFF λn. ( n) and TOTAL n.let n ( n) in n cons ( n ) 1+ 2 λn 1 n plus 2 1 2 λ 1 2. ( ) ( n) n. ( n) times ( n) ometimes the lambda version is easier to read than the other, but it is a choice we can make if you we want to. Where the argument is present on both sides, it is technically possible to omit it. e.g. ( ) This is possible because of the extensionality of functions. still has functionality xpression at at, so both side of the equation have functionality at at. Thus they both need at to produce their final result.