Download 2.3 Case Study: Functional Embedded Telephony in Camelot

Transcript
Chapter 2
diamond type for heap-space allocation, self-defined classes, threads for
concurrency, and so on. Therefore, it’s a good opportunity to help test and the
Camelot compiler.
(1) Using the Diamond Type
Diamond type in Camelot enables in-place operations and precise control of heap
usage. A freelist for the storage of unused diamonds is provided for the case where
one may not always want to reuse a diamond value immediately. Since a safe and
static automation of the decision about whether a pattern match is destructive or not
is not available, it is our programmers’ responsibility to ensure that the datatype we
are going to deconstruct will not be used anymore because its heap space will be
reclaimed and its contents will be overwritten subsequently. We have mentioned in
the previous section that there are two kinds of destructive match patterns in
Camelot, one uses “@d” annotation and the other uses “@_”. For the inference
implementation of the static prediction of heap space usage (introduced in the next
section), both of them are treated as @_, as this will not affect the inference in any
way. So it is not a big matter whether to use @d or @_, but, it is a matter of
distinguishing whether to use a destructive pattern match or a read-only pattern
match. Following codes are the examples of their usage in this project.
type card = ! NullCard | Card of int* string * string * int* int* int* string
let attAbility c =
match c with Weapon (_, _, _, attak , _, _, _) → attak
| NullWeapon → 0
Figure 2.6(a): A Method in the Project Using Read-Only Match
let co n ca tL ist ( l1 : ca rd p a irlist ) ( l 2 : ca rd p a irlist ) =
m a tch l1 w ith N il 6 → ()
| C ons6(a, b, t ) @ d →
co n ca tL ist t ( C o n s 6 ( a , b , l 2 ) @ d )
Figure 2.6(b): A Method in the Project
Using Deconstructive Pattern Match with “@d”
28