Docsity

Homework 7: Implementing Denotational Semantics of CLiter Programs in OCaml - Prof. Cesare, Assignments of Programming Languages

Toggle sidebar
bg1
22c:111 Programming Language Concepts
Fall 2008
Homework 7
Due: Friday, December 12, by 11:30pm
This is an pair programming assignment in OCaml. Please check the course website to see who
your programming partner is.
Type your solutions in a plain text file called hw7.ml contained in the the archive file hw7.zip
together with other necessary files.
Submit one copy of hw7.ml per pair through ICON. Your hw7.ml file must compile with no
errors. You may receive a zero for a problem if its requested code does not compile correctly.
In this assignment we revisit the CLiter language seen in Homeworks 4 and 6, so you may want
to review those homeworks, their solution, and instructions on how to compile all the needed files.
In Homework 4, we developed a parser and a type checker for CLiter programs. In this assign-
ment you will write code implementing the denotational semantics of CLiter programspresented
seen in class. This implementation will effectively yield an interpreter for CLiter programs, which
you will then run on some sample programs.
1 Implementing the denotational semantics
For better testing of the interpreter, we will consider here a small extension of CLiter with a string
type, a conversion operator from integers to strings, a read statement, and a print statement.
In the concrete syntax, the set of types is extended with the type string and the set of unary
operators is extended with the operator to string. Expressions of type strings are sequences of
characters in double quotes, as in OCaml and with the same escaped characters1, or variables
(identifiers) of type string, or expressions of the form to string(e)where eis an expression of
type int. The meaning of to string(e)is the same as that of the function string of int in
Ocaml. The print statement has the form
print e
where eis an expression of type string. The effect of the statement is to print the value of eon
the standard output. The read statement has the form
read x
1With \nrepresenting a new line character, and so on.
1
pf2

Partial preview of the text

Download Homework 7: Implementing Denotational Semantics of CLiter Programs in OCaml - Prof. Cesare and more Assignments Programming Languages in PDF only on Docsity!

22c:111 Programming Language Concepts

Fall 2008

Homework 7

Due: Friday, December 12, by 11:30pm

This is an pair programming assignment in OCaml. Please check the course website to see who your programming partner is.

Type your solutions in a plain text file called hw7.ml contained in the the archive file hw7.zip together with other necessary files. Submit one copy of hw7.ml per pair through ICON. Your hw7.ml file must compile with no errors. You may receive a zero for a problem if its requested code does not compile correctly.

In this assignment we revisit the CLiter language seen in Homeworks 4 and 6, so you may want to review those homeworks, their solution, and instructions on how to compile all the needed files.

In Homework 4, we developed a parser and a type checker for CLiter programs. In this assign- ment you will write code implementing the denotational semantics of CLiter programspresented seen in class. This implementation will effectively yield an interpreter for CLiter programs, which you will then run on some sample programs.

1 Implementing the denotational semantics

For better testing of the interpreter, we will consider here a small extension of CLiter with a string type, a conversion operator from integers to strings, a read statement, and a print statement. In the concrete syntax, the set of types is extended with the type string and the set of unary operators is extended with the operator to string. Expressions of type strings are sequences of characters in double quotes, as in OCaml and with the same escaped characters^1 , or variables (identifiers) of type string, or expressions of the form to string(e) where e is an expression of type int. The meaning of to string(e) is the same as that of the function string of int in Ocaml. The print statement has the form

print e

where e is an expression of type string. The effect of the statement is to print the value of e on the standard output. The read statement has the form

read x (^1) With \n representing a new line character, and so on.

where x is a variable of type int. The effect of the statement is to read an integer from the standard input and assign that to x. The OCaml types implementing CLiter’s abstract syntax from the previous homework have been modified and extended to account for these extensions of the language. See the enclosed file types.ml. That file contains also contains a new value type, implementing the values of CLiter’s type—that is, the mathematical entities CLiter expressions evaluate to. Finally, it contains the signature of the state abstract data type seen in class. The new file hw7.ml contains a partial implementation of the meaning functions seen in class (p meaning, s meaning, and so on).

You are to do the following:

  1. Provide an implementation of your choice for the state ADT conforming to the following specification: - empty:state is the empty state, one in which every variable is undefined. - update:state -> var -> value -> state takes a state q, a variable x and a value v, and returns a new state q′^ that is identical to q except that the value of x in q′^ is v. - get value:state -> var -> value takes a state q and a variable x and returns the value of x in q if x is defined in q; otherwise it raises the exception (UndefinedVar x).

Make sure you explain your implementation in comments.

  1. Complete the implementation of the meaning functions according to the specification of CLiter’s denotational semantics discussed in class.

2 Testing the interpreter

Once you complete Part 1, you can test your implementation by evaluating CLiter programs with an interpreter that uses your functions. One version of the interpreter is the function eval:string -> state which takes the name of a file containing a CLiter program, parses it, type checks it, and then evaluates it, returning its final state. The other version evals:string -> state is similar except that it reads the CLiter program directly from its input string.

A few test programs are enclosed in tests.zip. You are also to implement and test the following CLiter programs. Include the programs at the end of hw7.ml in an OCaml comment.

  1. This program asks the user for the value of two variables, x and y, swaps their values, and then prints their new values.
  2. This program asks the user for two values m and n, computes the value mn^ and then prints it provided that n is non-negative; otherwise it prints an error message.
  3. This program reads in four values and prints them back in increasing order.

You are encouraged to use the print statement as needed in your CLiter programs to make their input/output more legible.