2 * Simple structure for cleanly handling input parameters
3 * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu>
4 * Annotations: Alex Vaynberg <alv@andrew.cmu.edu>
11 val flag : string -> flag (* create a new flag that is not set *)
12 val not : flag -> flag (* reverses the meaning of flag being set *)
14 val set : flag -> unit (* set a flag *)
15 val unset : flag -> unit (* unset a flag *)
16 val isset : flag -> bool (* check if the flag is set *)
18 val guard : flag -> ('a -> unit) -> 'a -> unit
19 (* return a function that runs only if flag is set *)
20 val guards : flag list -> ('a -> unit) -> 'a -> unit
21 (* return a function that runs only if all flags are set *)
22 val someguards : flag list -> ('a -> unit) -> 'a -> unit
23 (* return a func that runs if one of flags is set *)
25 (* get a function that runs the first one if flag is set, or second one if it is not *)
26 val branch : flag -> ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
28 val show : flag -> string (* returns string that contains the setting of the flag *)
31 structure Flag :> FLAG =
33 datatype flag = FLAG of {name : string, value : bool ref, post : bool -> bool}
35 fun flag name = FLAG {name = name, value = ref false, post = fn b => b}
37 fun set (FLAG f) = #value f := true
38 fun unset (FLAG f) = #value f := false
39 fun not (FLAG {name, value, post}) =
40 FLAG {name = name, value = value, post = fn b => Bool.not (post b)}
42 fun isset (FLAG f) = (#post f) (! (#value f))
44 fun guard fl f x = if isset fl then f x else ()
45 fun guards fls f x = if List.all isset fls then f x else ()
46 fun someguards fls f x = if List.exists isset fls then f x else ()
48 fun branch fl (f, g) = if isset fl then f else g
50 fun show (FLAG f) = "flag " ^ #name f ^ " = " ^ Bool.toString (! (#value f))