// // Title: Concepts of Programming Languages // Number: CAS CS 320 // Semester: Fall 2012 // Class Time: TR 12:30-2:00 // Instructor: Hongwei Xi (hwxiATcsDOTbuDOTedu) // Teaching Fellow: Alex Ren (arenATcsDOTbuDOTedu) // (* // // Assignment #4 // Due Friday, February 15, 2013 at 11:59pm // *) (* ****** ****** *) // // Problem 1: 50 points // (* ****** ****** *) #include "BUCASCS320-6.hats" (* ****** ****** *) staload "GameOf24.sats" (* ****** ****** *) datatype exp = | Num of double | Add of (exp, exp) | Sub of (exp, exp) | Mul of (exp, exp) | Div of (exp, exp) // end of [exp] (* ****** ****** *) extern fun fprint_exp (out: FILEref, e: exp): void implement fprint_exp (out, e0) = let macdef prstr (s) = fprint_string (out, ,(s)) in // case+ e0 of | Num (v) => { val () = prstr "Num(" val () = fprintf (out, "%.f", @(v)) val () = prstr ")" } | Add (e1, e2) => { val () = prstr "Add(" val () = fprint_exp (out, e1) val () = prstr ", " val () = fprint_exp (out, e2) val () = prstr ")" } | Sub (e1, e2) => { val () = prstr "Sub(" val () = fprint_exp (out, e1) val () = prstr ", " val () = fprint_exp (out, e2) val () = prstr ")" } | Mul (e1, e2) => { val () = prstr "Mul(" val () = fprint_exp (out, e1) val () = prstr ", " val () = fprint_exp (out, e2) val () = prstr ")" } | Div (e1, e2) => { val () = prstr "Div(" val () = fprint_exp (out, e1) val () = prstr ", " val () = fprint_exp (out, e2) val () = prstr ")" } // end // end of [fprint_exp] (* ****** ****** *) fun eval_exp (e: exp): double = case+ e of | Num (a) => a | Add (e1, e2) => eval_exp e1 + eval_exp e2 | Sub (e1, e2) => eval_exp e1 - eval_exp e2 | Mul (e1, e2) => eval_exp e1 * eval_exp e2 | Div (e1, e2) => eval_exp e1 / eval_exp e2 // end of [eval_exp] (* ****** ****** *) typedef card = '{ card_exp= exp, card_val= double } // end of [card] assume card_type = card (* ****** ****** *) fun card_make_exp (e: exp): card = let val v = eval_exp (e) in '{ card_exp= e, card_val= v } end // end of [card_make_exp] (* ****** ****** *) implement card_make_int (i) = card_make_exp (Num (double_of (i))) (* ****** ****** *) implement card_get_val (x) = x.card_val (* ****** ****** *) implement add_card_card (x1, x2) = card_make_exp (Add (x1.card_exp, x2.card_exp)) // end of [add_card_card] implement sub_card_card (x1, x2) = card_make_exp (Sub (x1.card_exp, x2.card_exp)) // end of [sub_card_card] implement mul_card_card (x1, x2) = card_make_exp (Mul (x1.card_exp, x2.card_exp)) // end of [mul_card_card] implement div_card_card (x1, x2) = card_make_exp (Div (x1.card_exp, x2.card_exp)) // end of [div_card_card] (* ****** ****** *) implement fprint_card (out, x) = { val () = fprint_exp (out, x.card_exp) val () = fprint_string (out, " = ") val () = fprintf (out, "%.f", @(x.card_val)) } (* ****** ****** *) (* end of [GameOf24_card.dats] *)