Homework #4

Due: Fri, Feb 12

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 “HW4”.

    • 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 HW4.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!

Files

Template: HW4.template.hs

Support files (don’t edit or submit these):

You should download all three of the above linked files and put them in the same directory. Rename the template to HW4.hs and enter your solutions directly in that file.

You must launch GHCi from within the directory containing these files so that GHCi can find them all when you’re working on your solution.

Description

This is the second of three assignments using the MiniLogo language. In this assignment, you’ll be implementing the semantics of a subset of MiniLogo called MiniMiniLogo.

MiniMiniLogo is just MiniLogo without macros or for-loops. The concrete syntax of MiniMiniLogo is given below.

int ::= (any integer)
 
cmds ::= ε   |   cmd ; cmds command sequence
block ::= { cmds } command block
 
mode ::= down   |   up pen status
 
expr ::= int literal integer
| expr + expr addition
| expr * expr multiplication
| ( expr ) grouping
 
cmd ::= pen mode change pen mode
| move ( expr , expr ) move pen to a new position
 
prog ::= main() block MiniMiniLogo program

Any MiniLogo program that does not contain macro definitions, macro calls, or for-loops is a valid MiniMiniLogo program.

You should refer to the MiniLogo Language Description for an explanation of how the commands in MiniMiniLogo work.

A Haskell implementation of the abstract syntax of MiniMiniLogo is provided for you in the file MiniMiniLogo.hs. It also provides a pretty printer and some functions to generate example MiniMiniLogo programs. Finally, this file provides some types that you’ll use in your semantics, such as Point, Line, and State. Make sure you study these types closely!

In this assignment, you will be implementing the semantics of MiniMiniLogo. The meaning of a MiniMiniLogo is a list of the lines that it draws. Be sure to re-read the section in the language description about the canvas and pen of the execution environment, since you’ll need to understand these to implement the semantics.

The support file Render.hs provides a library for rendering the output of a MiniMiniLogo program as an SVG image in an HTML5 file. You don’t need to understand the contents of this file and can just use the draw function in the template.

Tasks

I have left the types of expr, cmd, and prog undefined so that you can try to come up with the semantic domains on your own. However, you can reverse engineer these from the test cases, if you get stuck. I’ll also give you the semantic domains in class on Monday, Feb 8.

  1. Implement expr, the semantic function for MiniMiniLogo expressions (Expr).

  2. Implement cmd, the semantic function for MiniMiniLogo commands (Cmd). Note that a command updates the state of the pen and possibly draws a line.

  3. Implement block, the semantic function for MiniMiniLogo blocks (Block). A program changes the state of the pen and may draw several lines.

  4. Implement the function optimize that optimizes a MiniMiniLogo program by evaluating all of the expressions it contains to literal integers. For example, a program containing the command move(2 + 3 * 4, 5 + 6) should be optimized to move(14, 11).

    Note that this kind of optimization is possible in MiniMiniLogo since we don’t have variable references in expressions. In the full version of MiniLogo, expressions with variable references cannot be reduced to literal integers (although they may still be able to be partially evaluated.

I have provided the semantics of programs (prog), which is trivial after you’ve implemented the semantics of block. It just executes the main block with the initial pen state and returns any lines produced.

After you have implemented block in Step 3, you can use the draw function in the template to run a MiniMiniLogo program and render its output to HTML, which you can then load in your browser. To see if your semantics is working correctly, you can compare the result of running draw shebang with this image: shebang.png


Back to course home page