CS 3520 Homework 9   - Due November 16

This homework is due November 16, 8:00 AM.

Submit your work using DrScheme's Handin button, even though it's OCaml code. The handin server will not run any tests.

Exercise 9.1, True, False, equals, and if

Start with the TFAE interpreter from lecture. The implementation (spread across several slides) already includes a boolean type, but no expressions of boolean type.

Add support for true, false, {= ... ...}, and {if ... ... ...} expressions, where = produces a boolean given two numbers, and if requires a boolean expression for the test. Use True, False, Eq, and If as the names of new fae constructors.

Examples:

  interp(Num(10), MTSub);; (* produces NumV(10) *)
  interp(Eq(Num(13),
            If(Eq(Num(1),Add(Num(-1), Num(2))),
               Num(12),
               Num(13))),
         MTSub) ;; (* produces BoolV(false) *)

  typecheck(Eq(Num(13),
               If(Eq(Num(1),Add(Num(-1), Num(2))),
                  Num(12),
                  Num(13))),
            MTEnv);;    (* succeeds *)

  typecheck(Add(Num(1), If(True, True, False)), MTEnv);; (* fails *)

Exercise 9.2, Pairs

Implement {pair ... ...}, {fst ...}, and {snd ...} expressions as shown in lecture for TPRCFAE (but not implemented in lecture). Call the new fae constructors Pair, Fst, and Snd, and call the new te constructor CrossTE.

Examples:

  interp(Fst(Pair(Num(10), Num(8))), MTSub);; (* produces NumV(10) *)
  interp(Snd(Pair(Num(10), Num(8))), MTSub);; (* produces NumV(8) *)

  typecheck(Pair(Num(10), Num(8)), MTEnv);; (* succeeds *)
  typecheck(Add(Num(1), Snd(Pair(False, Num(8)))), MTEnv);; (* succeeds *)
  typecheck(Fun("x", CrossTE(NumTE, BoolTE),
                If(Snd(Id("x")), Num(0), Fst(Id("x")))),
            MTEnv)                                        (* succeeds *)

  typecheck(Fst(Num(10)), MTEnv);; (* fails *)
  typecheck(Add(Num(1), Fst(Pair(False, Num(8)))), MTEnv);; (* fails *)
  typecheck(Fun("x", CrossTE(NumTE, BoolTE),
                If(Fst(Id("x")), Num(0), Fst(Id("x")))),
            MTEnv)                                        (* fails *)

Exercise 9.3, Functions that Accept Multiple Arguments, Yet Again

With pairs, functions can accept multiple arguments by accepting paired values, but we can also add direct support for multiple arguments.

Change the interpreter to allow multiple function arguments and multiple arguments at function calls. The grammar of the language is now as follows:

  <TMFAE> = <number>
          | true
          | false
          | {+ <TMFAE> <TMFAE>}
          | {- <TMFAE> <TMFAE>}
          | {= <TMFAE> <TMFAE>}
          | <id>
          | {if <TMFAE> <TMFAE> <TMFAE>}
          | {fun {[<id> : <tyexp>]*} <TMFAE>}
          | {<TMFAE> <TMFAE>*}
          | {pair <TMFAE> <TMFAE>}
          | {fst <TMFAE>}
          | {snd <TMFAE>}

To support this grammar, change the Fun and App constructors in fae, and the ArrowTE constructor in te (plus other constructors that do not represent the input program):

  type fae = 
      ...
    | Fun of string list * te list * fae
    | App of fae * fae list
      ...

  type te =
      ...
    | ArrowTE of te list * te

In OCaml, for any type t, t list is the type of a list of values of type t using OCaml's built-in list operations. The empty list is written [ ]; a list containing three integers 1, 2, and 3 is written [1; 2; 3]; a double colon is the cons operator, so that 0::[1; 2; 3] produces [0; 1; 2; 3], and this operator can be used in patterns. Thus,

  let rec sum : (int list -> int) = function
        [] -> 0
      | a::rest -> a + sum(rest);;
  sum [1; 2; 3];;

produces 6.

The map function is List.map:

  List.map (fun x -> x+1) [1;2;3];; (* produces [2;3;4] *)

Examples:

  interp(App(Fun([], [], Num(10)), []), MTSub);; (* produces NumV(10) *)
  interp(App(Fun(["x"; "y"], [NumTE; NumTE], Sub(Id("x"), Id("y"))), 
             [Num(10); Num(20)]), 
         MTSub);;                (* produces NumV(-10) *)

  typecheck(App(Fun(["x"; "y"], [NumTE; BoolTE], Id("y")), 
                [Num(10); False]), 
            MTEnv);;             (* succeeds *)
  typecheck(App(Fun(["x"; "y"], [NumTE; BoolTE], Id("y")), 
                [Num(10)]), 
            MTEnv);;             (* fails *)


Last update: Wednesday, November 23rd, 2005
mflatt@cs.utah.edu