Hasklet #4

Due: Mon, May 17

Read the general information on Hasklets!

Submit your solution through Canvas.

Important grading and support notes

This Hasklet is a bit more involved than previous ones and so will be worth a few more points. You should plan to spend correspondingly more time on it (and if get stuck, your documentation of where and how should reflect this).

Description

Haskell template

In this Hasklet you’ll be completing the implementation of a monad that combines state and failure, then using that to implement a monadic semantics for a stack-based programming language.

The abstract syntax of the language is defined by the Cmd and Prog data types in the template. There are brief explanations of how each command works in the comments, but you will probably also need to refer to the example programs below to figure out the details of how each command should be implemented.

The template also provides a data type definition for a variant of the state monad we used in class. This monad differs in two ways: First, the state is fixed to be a Stack, which is a list of integers. Second, computations may fail, indicated by Maybe attached to the type of the result.

The template file contains a minimal set of start-to-finish doctests based on the example programs. You may want to supplement these with additional test cases for each function to help your development.

Tasks

Part 1: Monad implementation

In this part, you’ll be finishing the implementation of the StackM monad. The dig primitive is provided for you since its intended behavior is less obvious than the other primitives.

  1. Instantiate the Monad type class for StackM.

  2. Implement the three unimplemented primitive operations for pushing, popping, at peeking at integers on the stack.

Part 2: Language semantics

In this part, you’ll be implementing the semantics of the stack language using the StackM monad.

Note that the return type of both the cmd and prog functions are StackM (), indicating that they are monadic computations in the StackM monad that return a unit value. The unit value reflects that these functions don’t return interesting results, they’re just executed for their effect on the underlying stack.

The implementations of the Push, Drop, and Dig commands are provided to get you started.

  1. In cmd, implement the semantics of the Dup, Neg, Add, Mul, and LEq commands.

  2. Implement prog, the semantics of executing a program. Hint: There is a function in Control.Monad that makes this implementation trivial, but it’s also just fine to implement it directly.

  3. Return to cmd and implement the semantics of If and While.


Back to course home page