| 10561 |      1 | (*<*)
 | 
|  |      2 | theory Option2 = Main:
 | 
|  |      3 | hide const None Some
 | 
|  |      4 | hide type option
 | 
|  |      5 | (*>*)
 | 
|  |      6 | 
 | 
|  |      7 | text{*\indexbold{*option}\indexbold{*None}\indexbold{*Some}
 | 
|  |      8 | Our final datatype is very simple but still eminently useful:
 | 
|  |      9 | *}
 | 
|  |     10 | 
 | 
|  |     11 | datatype 'a option = None | Some 'a;
 | 
|  |     12 | 
 | 
|  |     13 | text{*\noindent
 | 
|  |     14 | Frequently one needs to add a distiguished element to some existing type.
 | 
|  |     15 | For example, type @{text"t option"} can model the result of a computation that
 | 
|  |     16 | may either terminate with an error (represented by @{term None}) or return
 | 
|  |     17 | some value @{term v} (represented by @{term"Some v"}).
 | 
|  |     18 | Similarly, @{typ nat} extended with $\infty$ can be modeled by type
 | 
|  |     19 | @{typ"nat option"}. In both cases one could define a new datatype with
 | 
|  |     20 | customized constructors like @{term Error} and @{term Infinity},
 | 
|  |     21 | but it is often simpler to use @{text option}. For an application see
 | 
|  |     22 | \S\ref{sec:Trie}.
 | 
|  |     23 | *}
 | 
|  |     24 | (*<*)
 | 
|  |     25 | (*
 | 
|  |     26 | constdefs
 | 
|  |     27 |  infplus :: "nat option \<Rightarrow> nat option \<Rightarrow> nat option"
 | 
|  |     28 | "infplus x y \<equiv> case x of None \<Rightarrow> None
 | 
|  |     29 |                | Some m \<Rightarrow> (case y of None \<Rightarrow> None | Some n \<Rightarrow> Some(m+n))"
 | 
|  |     30 | 
 | 
|  |     31 | *)
 | 
|  |     32 | end
 | 
|  |     33 | (*>*)
 |