Homework #3

Due: Thur, Feb 4

How to Submit

If submitting individually: Just upload your submission to the assignment in Canvas.

If submitting as part of a team:

  1. Assign yourself to a “Group” in Canvas. Select “People” from the menu, then select the “Groups” tab. Click “Join” on a group whose name starts with “HW3”.

    • You must coordinate with your team members to all join the same group. When choosing a group, please use the smallest available group number.

    • Do not add yourself to a group unless you have already coordinated and everyone has agreed to be in a group together.

  2. Upload a single file named HW3.hs for your group for this assignment in Canvas. Only one group member needs to upload the file.

    • Your file must contain the names and ID numbers of each member of your group in a comment at the top of your file. This is to ensure that nobody changed the composition of your group.

    • Your file must compile without errors in GHCi. If some of your code does not work, comment it out. If your code does not compile, the TAs will evaluate it harshly!

    • Do not change the existing doctest comments in the template (the lines starting with >>> and the results underneath). However, you are free to add your own doctest comments in addition to the ones that are there. The TAs will grade using both doctest and manual inspection of your file. If needed, they may also load your file in GHCi to run additional tests.

    • If you can’t solve a problem, you can get partial credit by describing in comments what you tried and where you got stuck. Often times, writing this out helps you get unstuck!

Description

This will be the first of three assignments using the MiniLogo language. MiniLogo is toy language inspired by Logo for programming simple 2D graphics.

Resources

For all of the following tasks, you are free to define whatever helper functions you need or use any functions in the standard libraries.

In GHCi, you can render a string with newline characters (\n) by applying the function putStrLn, as illustrated in the doctests in the template.

Part 1: MiniLogo Expressions

  1. Define the abstract syntax of MiniLogo expressions by defining the Haskell data type Expr. You should not include the grouping production in your abstract syntax since it is not needed (see the language description). I have provided the type synonym for variable names. You can use the built-in type Int for the int syntactic category directly.

  2. Define ASTs for the expressions given in concrete syntax in the template. Make sure that you take operator precedence and associativity into account.

  3. Define a pretty printer for MiniLogo expressions by implementing the function prettyExpr. You should try to make your output match the concrete syntax used in the doctests and on the language description page exactly. The doctests assume that you print parentheses only when strictly necessary. You should try to do this but since it’s a bit tricky you won’t be marked down as long as the concrete syntax that you generate is correct. For example, if you print (2 + (3 * x)) instead of 2 + 3 * x, that’s OK. However, if you print 2 + 3 * x when it should be (2 + 3) * x, that is an error. The upshot of this is that if you can’t get it just right, err on the side of including more parentheses. :-)

Part 2: MiniLogo Commands

  1. Define the abstract syntax of MiniLogo commands by defining a new Haskell data type Cmd. I have provided several more types and type synonyms that you should use in these definitions. Note that you do not need to define a separate synonym for cmds since cmds and block are equivalent in the abstract syntax, and cmds only appears in the grammar as part of block.

  2. Define the undefined ASTs of type Block corresponding to the bodies of the two macros in the final example from the language description (the program containing the for loop).

    The doctests show the concrete syntax of these blocks, but you won’t be able to pass the doctests until completing the next task.

  3. Define a pretty printer for MiniLogo commands by implementing the function prettyCmd. As before, you should try to make your output match the concte syntax used in the doctests and on the language description page exactly. You may want to add your own doctests while developing prettyCmd. I omitted my doctests from the template since your constructor names will likely be different from mine. The doctests from the previous tasks will serve to evaluate your pretty printer, but it might be helpful to have smaller test cases too.

Part 3: MiniLogo Programs

I have given you the syntax and pretty printer for MiniLogo programs. Your only job is to:

  1. Complete the definition of the example program. Be sure to reuse your definitions of the macro bodies from Part 2.


Back to course home page