## Code to help with CS655 Assignment 7: Lambda and friends

As you implement subsitution, you may find it useful to use the following function, which a variable that is not free in any of a list of expressions: <<*>>= fun unused es v = if foldr (fn(e,p) => p orelse isfree v e) false es then unused e (v ^ "'") else v @ You also might find the following beta-reduction code useful. <<*>>= fun beta(APP(FN(x,M),N)) = SOME((N/x)M) | beta _ = NONE fun reduce t = case beta t of SOME t' => SOME t' | NONE => case t of FN(v,e) => (case reduce e of SOME e' => SOME(FN(v,e')) | NONE => NONE) | APP(a,b) => (case reduce a of SOME a' => SOME(APP(a',b)) | NONE => case reduce b of SOME b' => SOME(APP(a,b')) | NONE => NONE) | VAR v => NONE fun eval e = case reduce e of SOME e' => eval e' | NONE => e val add = FN("n",FN("m",FN("f",FN("x",APP(APP(VAR"n",VAR"f"), APP(APP(VAR"m",VAR"f"),VAR"x")))))) val three = FN("f",FN("x",APP(VAR"f",APP(VAR"f",APP(VAR"f",VAR"x"))))) val one = FN("f",FN("x",APP(VAR"f",VAR"x"))) val succ = APP(add,one) val four = eval(APP(APP(add,three),one)) @ Here's printing code: <<*>>= fun ? (VAR x) = x | ? (APP(M, N)) = "(" ^ ? M ^ " " ^ ? N ^ ")" | ? (FN(x, M)) = "(FN " ^ x ^ " => " ^ ? M ^ ")" @