author | wenzelm |
Thu, 15 Feb 2018 12:11:00 +0100 | |
changeset 67613 | ce654b0e6d69 |
parent 67406 | 23307fd33906 |
child 69505 | cc2d676d5395 |
permissions | -rw-r--r-- |
10561 | 1 |
(*<*) |
16417 | 2 |
theory Option2 imports Main begin |
36176
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents:
35416
diff
changeset
|
3 |
hide_const None Some |
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents:
35416
diff
changeset
|
4 |
hide_type option |
10561 | 5 |
(*>*) |
6 |
||
67406 | 7 |
text\<open>\indexbold{*option (type)}\indexbold{*None (constant)}% |
11428 | 8 |
\indexbold{*Some (constant)} |
10561 | 9 |
Our final datatype is very simple but still eminently useful: |
67406 | 10 |
\<close> |
10561 | 11 |
|
58860 | 12 |
datatype 'a option = None | Some 'a |
10561 | 13 |
|
67406 | 14 |
text\<open>\noindent |
11310 | 15 |
Frequently one needs to add a distinguished element to some existing type. |
10561 | 16 |
For example, type @{text"t option"} can model the result of a computation that |
15905 | 17 |
may either terminate with an error (represented by @{const None}) or return |
10561 | 18 |
some value @{term v} (represented by @{term"Some v"}). |
19 |
Similarly, @{typ nat} extended with $\infty$ can be modeled by type |
|
20 |
@{typ"nat option"}. In both cases one could define a new datatype with |
|
21 |
customized constructors like @{term Error} and @{term Infinity}, |
|
22 |
but it is often simpler to use @{text option}. For an application see |
|
23 |
\S\ref{sec:Trie}. |
|
67406 | 24 |
\<close> |
10561 | 25 |
(*<*) |
26 |
(* |
|
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
16417
diff
changeset
|
27 |
definition infplus :: "nat option \<Rightarrow> nat option \<Rightarrow> nat option" where |
10561 | 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 |
(*>*) |