

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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.
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:
Make sure you explain your implementation in comments.
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.
You are encouraged to use the print statement as needed in your CLiter programs to make their input/output more legible.