# Homework #4

**Due: Mon, Oct 30, 10:59pm**

## How to Submit

Submit two files named

`HW4Part1.<your-username>.hs`

and`HW4Part2.<your-username>.hs`

through TEACH.Your submission

*must compile without errors*in GHCi. Put all non-working parts of your solution in comments.**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 two integer registers named `A`

and `B`

.

int |
::= | (any integer) | integers |

bool |
::= | `true` | `false` |
booleans |

reg |
::= | `A` | `B` |
register names |

expr |
::= | int |
integer literal |

| | bool |
boolean literal | |

| | reg |
load from register | |

| | expr `+` expr |
integer addition | |

| | expr `<=` expr |
less than or equal to | |

| | `not` expr |
boolean negation | |

| | expr `&` expr |
boolean conjunction | |

stmt |
::= | reg `:=` expr |
store to register |

| | `if` expr |
conditional statement | |

`then` prog |
|||

`else` prog |
|||

`end` |
|||

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. Comments, which are not reflected in the abstract syntax, are included in curly braces `{`

… `}`

.

```
A := 3; { set register A to 3 }
B := A+2; { set register B to A+2, which will be 5 }
if A <= B then { if A is less than or equal to B ... }
A := A + A; { ... then double A }
else
B := B + B; { ... otherwise double B }
end;
B := A+B; { set B to A+B }
{ at the end of the program: A=6, B=11 }
```

Note that in this assignment you are *not* implementing the semantics of this language! You are only implementing and manipulating its syntax.

## Part 1

`HW4Part1.<your-username>.hs`

Encode the abstract syntax for the language as a set of Haskell types and data types.

Encode the example program above as a Haskell value.

Write a Haskell function

`sumFiveOrLess :: [Int] -> Prog`

that takes a list of integers and returns a program in our object language that sums up all of the integers in the list that are less than or equal to 5. The final result should be stored in register`A`

.

## Part 2

`HW4Part2.<your-username>.hs`

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 + true; { can't add an integer and a boolean }
A := true; { 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 third line is *not* a syntax error.)

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. Include the new syntax in a comment in your file. (Hint: you will need to split one of the syntactic categories into two.)

Encode the new abstract syntax as a set of Haskell types and data types.