(* Also see: http://www.csse.monash.edu.au/~lloyd/tildeFP/SML/1997/semantics.toy/toySyntax.sml *) (* ------------------------------------------------------------------------ *) type Ide = string; datatype Bopr = plus | minus | times | pwr; (* binary operators *) datatype Exp = binexp of Exp * Bopr * Exp | (* expressions *) unexp of Exp | (* there is only - *) varid of Ide | numeral of int (* the int >= 0 *) val zero = numeral 0; val one = numeral 1; (* ------------------------------------------------------------------------ *) fun priority plus = 1 | priority minus = 1 | priority times = 2 | priority pwr = 3 fun showBopr plus = "+" | showBopr minus = "-" | showBopr times = "*" | showBopr pwr = "**" fun show e = (* show *) let fun s (numeral n) _ = Int.toString n | s (varid x) _ = x | s (unexp e) _ = "-" ^ (s e 2) | s (binexp(a, opr, b)) p = let val po = priority opr; val par = po < p; val po' = (if opr = minus then 2 else po) (* ! *) in (if par then "(" else "") ^ s a po ^ showBopr opr ^ s b po' ^ (if par then ")" else "") end in s e 0 end; (* --------------------------------------------------------LA--csse--2005-- *) val two = numeral 2; val three = numeral 3; val four = numeral 4; val x = varid "x"; val xsq = binexp(x,pwr,two); val p = binexp(binexp(binexp(two,times,xsq), (* 2*x**2-3*x+4 *) minus,binexp(three,times,x)), plus, four); show p; (* E.g. Construct and print values representing polynomials *) (* --------------------------------------------CSSE--Monash-U.--.au--2005-- *)