29.01.2014 Views

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 8. DEPENDANT TYPE QUALIFIERS 60<br />

8.1 tut8.ml<br />

The module SM is constructed using the functorial interface to the OCaml standard library Map<br />

module. With string as the key type, we'll use it as the context when translating attribute<br />

parameters to expressions.<br />

module SM = Map.Make(struct<br />

type t = string<br />

let compare = Pervasives.compare<br />

end)<br />

8.1.1 Types and printing<br />

Here, we dene a type to represent the color type qualier. There are three constructors. One<br />

identies an exact color, and the other two represent upper and lower bounds on the color <strong>of</strong> a type.<br />

A type can have either one exact color, or at most one upper and one lower bound.<br />

type rgb = exp × exp × exp<br />

A color qualier can be an exact color (ExactRGB), a lower bound (LowerRGB), or an upper bound<br />

(UpperRGB). The qualiers are parameterized by three <strong>CIL</strong> expressions representing the amounts <strong>of</strong><br />

red, blue, green, and blue that are a part <strong>of</strong> each color.<br />

type color =<br />

| ExactRGB <strong>of</strong> rgb<br />

| LowerRGB <strong>of</strong> rgb<br />

| UpperRGB <strong>of</strong> rgb<br />

type colors = color list<br />

As in Chapter 7 it is useful to dene global variables for string constants, as well as a list <strong>of</strong> the<br />

strings.<br />

OCaml Style Note: It may be good to think <strong>of</strong> having a personal upper<br />

limit for the arity <strong>of</strong> tuple types. After some point, remembering the meanings<br />

<strong>of</strong> the elements becomes dicult, and it becomes more readable to dene record<br />

types. The tuple rgb is a triple <strong>of</strong> three expressions: the amounts <strong>of</strong> red, green,<br />

and bluehopefully easy enough to remember, so we don't make a record type<br />

for it.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!