Adam Megacz megacz@{andrew.cmu.edu, cmu.edu, usa.net} Office hours: M 4:30-5:20, Weh3130 ML "Gotchas" ------------------------ VariableLand TypeLand ------------ -------- / 5 int C/C++ | 5.0 float \ "steam tunnels" char* / 5 int | 5. real | "steam tunnels" string ML | (5,5) (int * int) | fun x:int = x+3 int -> int \ fun x:int y = x + y int -> int -> int * Evaluation crap is key for last assignment on parsers (* * * Some stuff in the standard library: * * val String.size: string -> int * * val String.str: char -> str * // turns a char into a str * * val String.sub: string * int -> char * // selects the int-th character * * val String.substring: (string * int * int) -> string * // example: String.substring("Pfenning",3,3) = "nni" * * // also, ("foo" ^ "baz") evaluates to "foobaz" * *) val replace: string -> (char * string) -> string fun replace "" (from, to) = "" | replace buffer (from, to) = let val head = String.sub (buffer, 0) val tail = String.substring (buffer, 1, String.size(buffer) - 1) in if (head = from) then (to ^ (replace tail (from, to))) else (str(head) ^ (replace tail (from, to))) end replace "adam" (#"a", "A "); val it = "A dA m" : string ================================================================ Correctness proof: For Argument's Sake, let a[[n]] denote the LAST "n" characters of the string a. 1. replace a[[0]] (b,c) -> "" "" = a'[[0]] 2. Let the string a' be identical to the string a, but with all occurrences of b replaced by c. Assume that replace a[[n]] (b,c) evaluates to a'[[n]]. Now prove that replace a[[n+1]] (b,c) evaluates to a'[[n+1]]. * replace a[[n]] (b,c) ====> let val head = String.sub (a[[n+1]], 0) val tail = String.substring (a[[n+1]], 1, String.size(a[[n+1]]) - 1) in if (head = b) then (c ^ (replace tail (b, c))) else (str(head) ^ (replace tail (b, c))) end ====> let val head = a[size-n-1] val tail = a[[n]] in if (head = b) then (c ^ (replace tail (b, c))) else (str(head) ^ (replace tail (b, c))) end ====> if (a[size-n-1] = b) then (c ^ (replace a[[n]] (b, c))) else (str(a[n-1]) ^ (replace a[[n]] (b, c))) ====> if (a[size-n-1] = b) then (c ^ a'[[n]]) else (str(a[size-n-1]) ^ a'[[n]]) ====> (if (a[size-n-1] = b) then c else str(a[size-n-1])) ^ a'[[n]] ====> (str(a'[size-n-1])) ^ a'[[n]] ====> a'[[n]] Is this mathematical or complete induction? Mathematical. Mathematical = Dominoes Complete = Pyramid