Homework #4
Due: Mon, Oct 26
How to Submit
Submit two files named
HW4Part1.hs
andHW4Part2.hs
through Canvas.Read the Homework Policy! This describes what to do if you cannot solve a problem.
Description
Consider the following abstract syntax, which describes a language that manipulates three integer registers named A
, B
, and R
.
int | ::= | (any integer) | integers |
reg | ::= | A | B | R |
register names |
expr | ::= | int | integer literal |
| | reg | load from register | |
| | expr + expr |
integer addition | |
| | expr <= expr |
less than or equal to | |
| | not expr |
boolean negation | |
stmt | ::= | reg := expr |
store to register |
| | if expr |
conditional statement | |
then prog |
|||
else prog |
|||
end |
|||
| | do prog |
loop until break | |
end |
|||
| | break |
break out of a loop | |
prog | ::= | ε | stmt ; prog |
sequence of statements |
Note that although the expression language may produce both booleans and integers, the registers may only contain integers.
Here is an example of a short program in this language that multiplies the numbers in A
and B
, storing the result in R
. Comments, which are not reflected in the abstract syntax, are included in curly braces {
… }
.
A := 7; { initialize the registers }
B := 9;
R := 0;
do
if A <= 0 then { loop until A is 0 }
break;
else
R := R + B; { ... add B to R }
A := A + -1; { ... decrement A }
end;
end;
Note that in this assignment you are not implementing the semantics of this language. You are only implementing and manipulating its syntax. This means that you have no way to run programs in this object language. I realize this makes testing difficult, so just do your best!
Part 1
In HW4Part1.hs
:
Encode the abstract syntax for the language as a set of Haskell types and data types.
Encode the AST of the example program above as a Haskell value.
Define a function
while :: Expr -> Prog -> Stmt
that defines a standard while loop as syntactic sugar.Write a Haskell function
sumFromTo :: Int -> Int -> Prog
that takes two integers x and y, and returns a program in the object language that sums all of the integers starting from x up to and including y, storing the result inR
. You can assume thatsumFromTo
is only called with arguments where x ≤ y. For example,sumFromTo 5 8
should generate an object language program that, if evaluated, would yield 26 (5+6+7+8) in registerR
.
Discussion prompt: There are at least two ways to approach #4, so spend some time thinking about alternatives.
Part 2
Observe that programs in our language may have type errors. For example, the following statements all have type errors:
A := not 3; { not can only be applied to booleans }
B := 4 + (5 <= 6); { can't add an integer and a boolean }
R := 8 <= 7; { registers may only contain integers }
if 2+3 then else end; { condition must be a boolean }
Note that our syntax allows empty then and else branches, so the fourth line is not a syntax error (only a type error).
In HW4Part2.hs
:
Refactor the syntax of the language to eliminate the possibility of type errors. The new syntax should be able to express all of the type correct programs that could be represented before and none of the type incorrect ones. Write the grammar of the new syntax in a comment in your file.
Encode the new abstract syntax as a set of Haskell types and data types.
Discussion prompt: What happens if you encode the above programs with type errors using the data types you defined in Part 1? What happens if you encode them using the data types you defined in Part 2? Think about the relationship between errors in your object language and errors in Haskell.