% CS 342 % General examples of CLU programs interval = cluster is create, contains, elements, equal, similar, copy % OVERVIEW: closed intervals of integers (immutable) % % A typical interval represents a set of all the integers % between two endpoints, inclusive. For example, [3, 5] is the set % of all ints greater than or equal to 3 and less than or equal to 5. rep = struct [lower, upper: int] % REP INVARIANT: For r: rep, r.lower <= r.upper. % % ABSTRACTION FUNCTION: r: rep respresents the interval [r.lower, r.upper]. create = proc(l,u: int) returns(cvt) signals(empty) % EFFECT: if l <= u, then return [l,u], otherwise signal empty if (l <= u) then return(rep${lower:l, upper:u}) else signal empty end end create contains = proc(iv: cvt, d: int) returns(bool) % EFFECT: return true if d is a element of iv return(iv.lower <= d & d <= iv.upper) end contains elements = iter(iv: cvt) yields(int) % EFFECT: yield the elements of iv, each once only i: int := iv.lower while i <= iv.upper do yield(i) i := i + 1 end end elements equal = proc(iv1, iv2: cvt) returns(bool) % EFFECT: return true if iv1 has same elements as iv2 return(iv1 = iv2) % same as return(rep$equal(iv1, iv2)) end equal similar = proc(iv1, iv2: interval) returns(bool) % EFFECT: return true if iv1 has same elements as iv2 return(iv1 = iv2) end similar copy = proc(iv: cvt) returns(cvt) % EFfECT: return iv return(iv) end copy end interval interval_test = proc() % EFFECT: print "3\n4\n5\n" on primary output po: stream := stream$primary_output() for i: int in interval$elements(interval$create(3,5)) do stream$putl(po, int$unparse(i)) end end interval_test traffic_light = cluster is create, change_red, change_amber, change_green, cycle, unparse % OVERVIEW: traffic lights (mutable) % % A traffic light is either red, amber (yellow), or green. rep = variant[red,amber,green: null] % REP INVARIANT: true % ABSTRACTION FUNCTION: r: rep represents a red light if rep$is_red(r), % an amber light if rep$is_amber(r), and a green light % if rep$is_green(r). create = proc(c: oneof[red, amber, green: null]) returns(cvt) % EFFECT: return a traffic light that has the color given by c tagcase c tag red: return(rep$make_red(nil)) tag green: return(rep$make_green(nil)) others: return(rep$make_amber(nil)) end end create change_red = proc(tl: cvt) % MODIFIES: tl % EFFECT: make the color of tl red rep$change_red(tl, nil) end change_red change_amber = proc(tl: cvt) % MODIFIES: tl % EFFECT: make the color of tl amber rep$change_amber(tl, nil) end change_amber change_green = proc(tl: cvt) % MODIFIES: tl % EFFECT: make the color of tl red rep$change_green(tl, nil) end change_green cycle = iter(tl: traffic_light) yields(traffic_light) % MODIFIES: tl % EFFECT: cycle tl through an infinite sequence of colors, % starting with the current color, % with amber following green, followed by red, % then green, amber, red, etc. while true do yield(tl) tagcase down(tl) tag amber: change_red(tl) tag red: change_green(tl) tag green: change_amber(tl) end end end cycle unparse = proc(tl: cvt) returns(string) % EFFECT: return "red" if tl is red, "green" if green, otherwise "amber" tagcase tl tag amber: return("amber") tag red: return("red") tag green: return("green") end end unparse end traffic_light