(* // // Course: BU CAS CS 520 // Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) // This file is for Assignment 3, BU CAS CS 520, Fall, 2010 // *) (* ****** ****** *) datatype Utm = (* for untyped alpha-normal forms *) | UTMind of int | UTMvar of string | UTMlam of Utm | UTMapp of (Utm, Utm) | UTMlet of (Utm, Utm) | UTMfix of Utm | UTMint of int | UTMbool of bool | UTMstr of string | UTMopr of (string, Utmlst) | UTMif of (Utm, Utm, Utm) // end of [Utm] where Utmlst = list0 Utm datatype Val = | VALint of int | VALbool of bool | VALstr of string | VALvoid of () | VALclo of (Utm, Env) // for representing closures | VALfix of (Utm, Env) // for handling fix // fix f(x).e => lam x.e[f->fix...] // end of [Val] where Env = list0 (Val) (* ****** ****** *) // // As an example, the following term defines a function that // doubles its argument (which represents an integer), that is // lam x. x + x: // UTMlam (UTMopr ("+", cons (UTMind 1, cons (UTMind 1, nil)))) // // As another example, the following term represents fix f(x). f(x): // UTMfix (UTMapp (UTMind(2), UTMind(1))) // (* ****** ****** *) // // 10 points // Please construct a term [TALLY] which represents the tally // function presented in class // (* // concrete syntax: TALLY = fix f(x). if x > 0 then x + f(x-1) else 0 *) val TALLY : Utm (* ****** ****** *) // // 10 points // Please construct a term [ISPRIME] which represents a function // that checks whether a given natural number is a prime. // val ISPRIME : Utm (* ****** ****** *) // // 40 points // Please implement a function [utmeval] which evaluates a term // to a value according to the call-by-value semantics // fun utmeval (u: Utm): Val (* ****** ****** *) (* end of [utmeval.sats] *)