The Semantic Functions of the Calculator


The denotational semantics for this calculator has a semantic function for each of the syntactic domains. Semantic equations specifying the functions are defined below, with one equation for each production rule in the abstract syntax. Inspection of the semantics for individual keystrokes will provide an understanding of the calculator operation.

   meaning: Program → Integer

meaning[[P]] = d where perform[[P]]( 0, nop, 0, 0 ) = ( a, op, d, m )
perform: ExprSequence → ( State → State )
perform[[E S]] = perform[[S]] º evaluate[[E]] perform[[E]] = evaluate[[E]]
evaluate: Expression → ( State → State )
evaluate[[N]]( a, op, d, m) = ( a, op, v, m ) where v = value[[N]] evaluate[[MR]]( a, op, d, m ) = ( a, op, m, m ) evaluate[[Clear]]( a, op, d, m ) = ( 0, nop, 0, 0 ) evaluate[[E1 O E2]] = evaluate[[E2]] º compute[[O]] º evaluate[[E1]] evaluate[[E A]] = calculate[[A]] º evaluate[[E]]
compute: Operator → ( State → State )
compute[[+]]( a, op, d, m ) = ( op( a, d ), plus, op( a, d ), m ) compute[[]]( a, op, d, m ) = ( op( a, d ), minus, op( a, d ), m ) compute[[×]]( a, op, d, m ) = ( op( a, d ), times, op( a, d ), m )
calculate: Answer → ( State → State )
calculate[[=]]( a, op, d, m ) = ( a, nop, op( a, d ), m ) calculate[[M+]]( a, op, d, m ) = ( a, nop, v, plus( m, v ) ) where v = op( a, d ) calculate[[+/-]]( a, op, d, m ) = ( a, op, minus( 0, d ), m )
value: Numeral → Integer
value[[ND]] = 10 * value[[N]] + value[[D]] value[[D]] = digit[[D]]
digit: Digit → Integer
digit[['0']] = 0, digit[['1']] = 1, ..., digit[['9']] = 9