ϵ">]>
Literature.
[1] J. Komara. Specification and Verification of Programs. Online.
[2] Ján Kľuka. Úvod do deklaratívneho programovania. Online.
The symbolic data structures are usually defined in the functional programming languages with the help of union types which can be readily arithmetized. We have seen already in one of the previous chapters an example of union type defining binary trees. In this chapter we will use another union types to arithmetize certain classes of symbolic expressions.
Numeric terms are symbolic expressions formed from variables and constants by the application of numeric binary operators of addition and multiplication.
Constructors of numeric terms. Arithmetization of numeric expressions is done with the help of the following four pair constructors with pairwise different tags.
Discrimination on the constructors of numeric terms. The pattern matching style of definitions of functions operating over the codes of numeric terms is obtained with conditionals called discrimination on the constructors of numeric terms. Example:
Numeric terms. Discrimination on the constructors of numeric terms is used in the definition of the predicate holding of the codes of numeric terms. The predicate is defined by course of values recursion. We refer to this kind of recursive definition as structural recursion on numeric terms.
In the sequel we identify numeric terms with their codes and from now on we say the numeric term instead of the code of a numeric term.
[CL] Formatting the output. Use the format to display numbers as numeric terms. Try out the following simple queries
98041654 = t:Term
3,(2,(0,1),1,2),0,3 = t:Term
Mt(At(Vt(1),Ct(2)),Vt(3)) = t
Size of numeric terms. The function yields the size of the numeric term , i.e. the number of operations including variables needed to construct the term. Example:
The function is defined by structural recursion on numeric terms.
Denotation of numeric terms. (1 point) The function takes the numeric term and (the code of) the assignment and yields the value, i. e. denotation, of the term in the assignment. Example:
The function is defined by structural recursion on numeric terms.
Free variables. The function yields the list of the indices of variables occurring in the numeric term . Example:
The function is defined by structural recursion on numeric terms.
Multiplicative terms. The predicate is holding of multiplicative terms. These are numeric terms formed from variables and constants by the application of multiplication. Example:
The predicate is defined by structural recursion on numeric terms.
Terms in additive normal form. The predicate is holding of terms in additive normal form. These are either multiplicative terms or addition of two numeric terms in additive normal form. Example:
The predicate is defined by structural recursion on numeric terms.
In this part we give an example of a program which goes beyond structural recursion. We consider the problem of rearranging numeric terms so that the additions which they contain are associated to left.
Terms with left associated addition. The predicate is holding of numeric terms with left associated addition. Example:
The predicate is defined by structural recursion on numeric terms.
Rearranging terms into expressions with left associated addition. (1 point) Define the function such that
Do not use auxiliary functions!
Hint. Put
Define by recursion with the measure :
Numeric terms are compiled into programs of a postfix machine and then the programs are executed.
Instructions. The instructions of the postfix machine are defined with the help of four pair constructors with pairwise different tags.
Programs. The predicate holding of programs of the postfix machine is defined by list recursion.
[CL] Formatting the output. Use the format to display numbers as programs. Try out the following simple queries
4723455160 = p:Program
(0,1),(1,2),(2,0),(0,3),(3,0),0 = p:Program
Vi(1),Ci(2),Ai,Vi(3),Mi,0 = p
Compilation. (1 point) Numeric terms are compiled into programs of the postfix machine with the help of the operation :
Example:
The compilation function is defined by structural recursion on numeric terms.
One-step evaluation. (1 point) The operation of the postfix machine itself is described by the ternary function , where is a program, is an assignment (environment), and is a list of values (I/O stack). Specification:
Invariant:
The function is defined by structural recursion on the program .