UNB/ CS/ David Bremner/ teaching/ cs4613/ tutorials/ Lab Tutorial 5


As usual, your solution should be submitted in to the handin-server by 11:30AM on 2024-02-21.

You are given a code skeleton skeleton.rkt for a simple language with numbers, strings and an overloaded product operator that takes either two numbers or a number and a string (and concatenates that many copies of the string). The various cases, including a runtime error, can be seen in the following tests:

(test (interp (prodE (numE 6) (numE 7))) (numV 42))
(test (interp (prodE (numE 3) (strE "ha"))) (strV "hahaha"))
(test (interp (prodE (numE 2) (prodE (numE 2) (strE "ha")))) (strV "hahahaha"))
(test (interp (prodE (strE "hello") (numE 3))) (strV "hellohellohello"))
(test/exn (interp (prodE (strE "hello") (strE "world"))) "mismatch")

Write the typechecker

Write a function typecheck to recursively typecheck Exp values. Your completed solution should pass the following tests

(test (typecheck (prodE (numE 6) (numE 7))) (numT))
(test (typecheck (prodE (numE 3)
                        (prodE (numE 6) (numE 7)))) (numT))
(test (typecheck (prodE (numE 3) (strE "ha"))) (strT))
(test (typecheck (prodE (strE "hello") (numE 3))) (strT))
(test (typecheck (prodE (prodE (numE 3) (numE 6))
                        (numE 7))) (numT))
(test/exn (typecheck (prodE (strE "hello") (strE "world"))) "mismatch")
(test/exn (typecheck (prodE (prodE (numE 2) (strE "hello")) (strE "world"))) "mismatch")
(test/exn (typecheck (prodE (prodE (numE 2) (strE "hello"))
                            (prodE (strE "world") (numE 3)))) "mismatch")
(test/exn (typecheck (prodE (strE "3") (prodE (numE 6) (strE "7")))) "mismatch")
(test/exn (typecheck (prodE (strE "3") (prodE (strE "6") (strE "7")))) "mismatch")