doc-src/IsarImplementation/Thy/ML.thy
author haftmann
Fri, 30 Mar 2007 16:18:59 +0200
changeset 22550 c5039bee2602
parent 22503 d53664118418
child 23652 94eeb79be496
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18538
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
     1
(* $Id$ *)
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
     2
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
     3
theory "ML" imports base begin
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
     4
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
     5
chapter {* Aesthetics of ML programming *}
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
     6
21502
7f3ea2b3bab6 prefer antiquotations over LaTeX macros;
wenzelm
parents: 21148
diff changeset
     7
text FIXME
7f3ea2b3bab6 prefer antiquotations over LaTeX macros;
wenzelm
parents: 21148
diff changeset
     8
21148
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
     9
text {* This style guide is loosely based on
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    10
  \url{http://caml.inria.fr/resources/doc/guides/guidelines.en.html}.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    11
%  FIMXE \url{http://www.cs.cornell.edu/Courses/cs312/2003sp/handouts/style.htm}
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    12
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    13
  Like any style guide, it should not be interpreted dogmatically.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    14
  Instead, it forms a collection of recommendations which,
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    15
  if obeyed, result in code that is not considered to be
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    16
  obfuscated.  In certain cases, derivations are encouraged,
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    17
  as far as you know what you are doing.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    18
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    19
  \begin{description}
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    20
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    21
    \item[fundamental law of programming]
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    22
      Whenever writing code, keep in mind: A program is
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    23
      written once, modified ten times, and read
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    24
      100 times.  So simplify its writing,
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    25
      always keep future modifications in mind,
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    26
      and never jeopardize readability.  Every second you hesitate
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    27
      to spend on making your code more clear you will
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    28
      have to spend ten times understanding what you have
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    29
      written later on.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    30
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    31
    \item[white space matters]
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    32
      Treat white space in your code as if it determines
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    33
      the meaning of code.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    34
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    35
      \begin{itemize}
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    36
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    37
        \item The space bar is the easiest key to find on the keyboard,
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    38
          press it as often as necessary. {\ttfamily 2 + 2} is better
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    39
          than {\ttfamily 2+2}, likewise {\ttfamily f (x, y)}
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    40
          better than {\ttfamily f(x,y)}.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    41
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    42
        \item Restrict your lines to \emph{at most} 80 characters.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    43
          This will allow you to keep the beginning of a line
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    44
          in view while watching its end.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    45
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    46
        \item Ban tabs; they are a context-sensitive formatting
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    47
          feature and likely to confuse anyone not using your
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    48
          favourite editor.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    49
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    50
        \item Get rid of trailing whitespace.  Instead, do not
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    51
          surpess a trailing newline at the end of your files.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    52
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    53
        \item Choose a generally accepted style of indentation,
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    54
          then use it systematically throughout the whole
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    55
          application.  An indentation of two spaces is appropriate.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    56
          Avoid dangling indentation.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    57
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    58
      \end{itemize}
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    59
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    60
    \item[cut-and-paste succeeds over copy-and-paste]
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    61
      \emph{Never} copy-and-paste code when programming.  If you
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    62
        need the same piece of code twice, introduce a
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    63
        reasonable auxiliary function (if there is no
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    64
        such function, very likely you got something wrong).
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    65
        Any copy-and-paste will turn out to be painful 
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    66
        when something has to be changed or fixed later on.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    67
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    68
    \item[comments]
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    69
      are a device which requires careful thinking before using
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    70
      it.  The best comment for your code should be the code itself.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    71
      Prefer efforts to write clear, understandable code
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    72
      over efforts to explain nasty code.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    73
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    74
    \item[functional programming is based on functions]
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    75
      Avoid ``constructivisms'', e.g. pass a table lookup function,
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    76
      rather than an actual table with lookup in body.  Accustom
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    77
      your way of codeing to the level of expressiveness
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    78
      a functional programming language is giving onto you.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    79
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    80
    \item[tuples]
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    81
      are often in the way.  When there is no striking argument
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    82
      to tuple function arguments, just write your function curried.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    83
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    84
    \item[telling names]
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    85
      Any name should tell its purpose as exactly as possible,
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    86
      while keeping its length to the absolutely neccessary minimum.
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    87
      Always give the same name to function arguments which
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    88
      have the same meaning. Separate words by underscores
21502
7f3ea2b3bab6 prefer antiquotations over LaTeX macros;
wenzelm
parents: 21148
diff changeset
    89
      (``@{verbatim int_of_string}'', not ``@{verbatim intOfString}'')
21148
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    90
3a64d58a9f49 first version of style guide
haftmann
parents: 20520
diff changeset
    91
  \end{description}
18554
bff7a1466fe4 more stuff;
wenzelm
parents: 18538
diff changeset
    92
*}
18538
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
    93
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
    94
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
    95
chapter {* Basic library functions *}
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
    96
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
    97
text {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
    98
  Beyond the proposal of the SML/NJ basis library, Isabelle comes
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
    99
  with its own library, from which selected parts are given here.
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   100
  See further files \emph{Pure/library.ML} and \emph{Pure/General/*.ML}.
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   101
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   102
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   103
section {* Linear transformations *}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   104
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   105
text %mlref {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   106
  \begin{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   107
  @{index_ML "(op |>)": "'a * ('a -> 'b) -> 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   108
  @{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   109
  @{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   110
  \end{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   111
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   112
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   113
(*<*)
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   114
typedecl foo
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   115
consts foo :: foo
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   116
ML {*
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   117
val dummy_const = ("bar", @{typ foo}, NoSyn)
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   118
val dummy_def = ("bar", @{term foo})
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   119
val thy = Theory.copy @{theory}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   120
*}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   121
(*>*)
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   122
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   123
text {*
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   124
  Many problems in functional programming can be thought of
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   125
  as linear transformations, i.e.~a caluclation starts with a
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   126
  particular value @{text "x \<Colon> foo"} which is then transformed
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   127
  by application of a function @{text "f \<Colon> foo \<Rightarrow> foo"},
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   128
  continued by an application of a function @{text "g \<Colon> foo \<Rightarrow> bar"},
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   129
  and so on.  As a canoncial example, take primitive functions enriching
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   130
  theories by constants and definitions:
22550
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   131
  @{ML "Sign.add_consts_i: (string * typ * mixfix) list -> theory
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   132
-> theory"}
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   133
  and @{ML "Theory.add_defs_i: bool -> bool
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   134
-> (bstring * term) list -> theory -> theory"}.
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   135
  Written with naive application, an addition of a constant with
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   136
  a corresponding definition would look like:
22550
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   137
  @{ML "Theory.add_defs_i false false [dummy_def]
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   138
  (Sign.add_consts_i [dummy_const] thy)"}.
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   139
  With increasing numbers of applications, this code gets quite unreadable.
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   140
  Using composition, at least the nesting of brackets may be reduced:
22550
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   141
  @{ML "(Theory.add_defs_i false false [dummy_def] o Sign.add_consts_i
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   142
  [dummy_const]) thy"}.
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   143
  What remains unsatisfactory is that things are written down in the opposite order
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   144
  as they actually ``happen''.
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   145
*}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   146
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   147
(*<*)
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   148
ML {*
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   149
val thy = Theory.copy @{theory}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   150
*}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   151
(*>*)
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   152
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   153
text {*
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   154
  At this stage, Isabelle offers some combinators which allow for more convenient
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   155
  notation, most notably reverse application:
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   156
  @{ML "
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   157
thy
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   158
|> Sign.add_consts_i [dummy_const]
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   159
|> Theory.add_defs_i false false [dummy_def]"}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   160
*}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   161
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   162
text {*
22550
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   163
  \noindent When iterating over a list of parameters @{text "[x\<^isub>1, x\<^isub>2, \<dots> x\<^isub>n] \<Colon> 'a list"},
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   164
  the @{ML fold} combinator lifts a single function @{text "f \<Colon> 'a -> 'b -> 'b"}:
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   165
  @{text "y |> fold f [x\<^isub>1, x\<^isub>2, \<dots> x\<^isub>n] \<equiv> y |> f x\<^isub>1 |> f x\<^isub>2 |> \<dots> |> f x\<^isub>n"}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   166
*}
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   167
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   168
text %mlref {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   169
  \begin{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   170
  @{index_ML "(op |->)": "('c * 'a) * ('c -> 'a -> 'b) -> 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   171
  @{index_ML "(op |>>)": "('a * 'c) * ('a -> 'b) -> 'b * 'c"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   172
  @{index_ML "(op ||>)": "('c * 'a) * ('a -> 'b) -> 'c * 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   173
  @{index_ML "(op ||>>)": "('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   174
  @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   175
  \end{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   176
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   177
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   178
text {*
22550
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   179
  \noindent FIXME transformations involving side results
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   180
*}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   181
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   182
text %mlref {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   183
  \begin{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   184
  @{index_ML "(op #>)": "('a -> 'b) * ('b -> 'c) -> 'a -> 'c"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   185
  @{index_ML "(op #->)": "('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   186
  @{index_ML "(op #>>)": "('a -> 'c * 'b) * ('c -> 'd) -> 'a -> 'd * 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   187
  @{index_ML "(op ##>)": "('a -> 'c * 'b) * ('b -> 'd) -> 'a -> 'c * 'd"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   188
  @{index_ML "(op ##>>)": "('a -> 'c * 'b) * ('b -> 'e * 'd) -> 'a -> ('c * 'e) * 'd"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   189
  \end{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   190
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   191
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   192
text {*
22550
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   193
  \noindent All those linear combinators also exist in higher-order
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   194
  variants which do not expect a value on the left hand side
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   195
  but a function.
22322
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   196
*}
b9924abb8c66 continued
haftmann
parents: 22293
diff changeset
   197
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   198
text %mlref {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   199
  \begin{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   200
  @{index_ML "(op `)": "('b -> 'a) -> 'b -> 'a * 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   201
  @{index_ML tap: "('b -> 'a) -> 'b -> 'b"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   202
  \end{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   203
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   204
22550
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   205
text {*
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   206
  \noindent FIXME
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   207
*}
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   208
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   209
section {* Options and partiality *}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   210
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   211
text %mlref {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   212
  \begin{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   213
  @{index_ML is_some: "'a option -> bool"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   214
  @{index_ML is_none: "'a option -> bool"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   215
  @{index_ML the: "'a option -> 'a"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   216
  @{index_ML these: "'a list option -> 'a list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   217
  @{index_ML the_list: "'a option -> 'a list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   218
  @{index_ML the_default: "'a -> 'a option -> 'a"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   219
  @{index_ML try: "('a -> 'b) -> 'a -> 'b option"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   220
  @{index_ML can: "('a -> 'b) -> 'a -> bool"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   221
  \end{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   222
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   223
22550
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   224
text {*
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   225
  Standard selector functions on @{text option}s are provided.
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   226
  The @{ML try} and @{ML can} functions provide a convenient
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   227
  interface for handling exceptions -- both take as arguments
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   228
  a function @{text f} together with a parameter @{text x}
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   229
  and catch any exception during the evaluation of the application
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   230
  of @{text f} to @{text x}, either return a lifted result
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   231
  (@{ML NONE} on failure) or a boolean value (@{ML false} on failure).
c5039bee2602 updated
haftmann
parents: 22503
diff changeset
   232
*}
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   233
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   234
section {* Common data structures *}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   235
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   236
subsection {* Lists (as set-like data structures) *}
18538
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
   237
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   238
text {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   239
  \begin{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   240
  @{index_ML member: "('b * 'a -> bool) -> 'a list -> 'b -> bool"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   241
  @{index_ML insert: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   242
  @{index_ML remove: "('b * 'a -> bool) -> 'b -> 'a list -> 'a list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   243
  @{index_ML merge: "('a * 'a -> bool) -> 'a list * 'a list -> 'a list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   244
  \end{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   245
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   246
22503
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   247
text {*
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   248
  Lists are often used as set-like data structures -- set-like in
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   249
  then sense that they support notion of @{ML member}-ship,
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   250
  @{ML insert}-ing and @{ML remove}-ing, but are order-sensitive.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   251
  This is convenient when implementing a history-like mechanism:
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   252
  @{ML insert} adds an element \emph{to the front} of a list,
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   253
  if not yet present; @{ML remove} removes \emph{all} occurences
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   254
  of a particular element.  Correspondingly @{ML merge} implements a 
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   255
  a merge on two lists suitable for merges of context data
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   256
  (\secref{sec:context-theory}).
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   257
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   258
  Functions are parametrized by an explicit equality function
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   259
  to accomplish overloaded equality;  in most cases of monomorphic
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   260
  equality, writing @{ML "(op =)"} should suffice.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   261
*}
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   262
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   263
subsection {* Association lists *}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   264
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   265
text {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   266
  \begin{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   267
  @{index_ML_exc AList.DUP} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   268
  @{index_ML AList.lookup: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   269
  @{index_ML AList.defined: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   270
  @{index_ML AList.update: "('a * 'a -> bool) -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   271
  @{index_ML AList.default: "('a * 'a -> bool) -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   272
  @{index_ML AList.delete: "('a * 'b -> bool) -> 'a -> ('b * 'c) list -> ('b * 'c) list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   273
  @{index_ML AList.map_entry: "('a * 'b -> bool) -> 'a
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   274
    -> ('c -> 'c) -> ('b * 'c) list -> ('b * 'c) list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   275
  @{index_ML AList.map_default: "('a * 'a -> bool) -> 'a * 'b -> ('b -> 'b)
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   276
    -> ('a * 'b) list -> ('a * 'b) list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   277
  @{index_ML AList.join: "('a * 'a -> bool) -> ('a -> 'b * 'b -> 'b) (*exception DUP*)
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   278
    -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception AList.DUP*)"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   279
  @{index_ML AList.merge: "('a * 'a -> bool) -> ('b * 'b -> bool)
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   280
    -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception AList.DUP*)"}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   281
  \end{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   282
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   283
22503
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   284
text {*
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   285
  Association lists can be seens as an extension of set-like lists:
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   286
  on the one hand, they may be used to implement finite mappings,
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   287
  on the other hand, they remain order-sensitive and allow for
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   288
  multiple key-value-pair with the same key: @{ML AList.lookup}
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   289
  returns the \emph{first} value corresponding to a particular
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   290
  key, if present.  @{ML AList.update} updates
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   291
  the \emph{first} occurence of a particular key; if no such
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   292
  key exists yet, the key-value-pair is added \emph{to the front}.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   293
  @{ML AList.delete} only deletes the \emph{first} occurence of a key.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   294
  @{ML AList.merge} provides an operation suitable for merges of context data
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   295
  (\secref{sec:context-theory}), where an equality parameter on
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   296
  values determines whether a merge should be considered a conflict.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   297
  A slightly generalized operation if implementend by the @{ML AList.join}
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   298
  function which allows for explicit conflict resolution.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   299
*}
22293
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   300
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   301
subsection {* Tables *}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   302
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   303
text {*
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   304
  \begin{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   305
  @{index_ML_type Symtab.key} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   306
  @{index_ML_type "'a Symtab.table"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   307
  @{index_ML_exc Symtab.DUP: Symtab.key} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   308
  @{index_ML_exc Symtab.DUPS: "Symtab.key list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   309
  @{index_ML_exc Symtab.SAME} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   310
  @{index_ML_exc Symtab.UNDEF: Symtab.key} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   311
  @{index_ML Symtab.empty: "'a Symtab.table"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   312
  @{index_ML Symtab.dest: "'a Symtab.table -> (Symtab.key * 'a) list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   313
  @{index_ML Symtab.keys: "'a Symtab.table -> Symtab.key list"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   314
  @{index_ML Symtab.lookup: "'a Symtab.table -> Symtab.key -> 'a option"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   315
  @{index_ML Symtab.defined: "'a Symtab.table -> Symtab.key -> bool"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   316
  @{index_ML Symtab.update: "(Symtab.key * 'a) -> 'a Symtab.table -> 'a Symtab.table"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   317
  @{index_ML Symtab.default: "Symtab.key * 'a -> 'a Symtab.table -> 'a Symtab.table"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   318
  @{index_ML Symtab.delete: "Symtab.key
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   319
    -> 'a Symtab.table -> 'a Symtab.table (*exception Symtab.UNDEF*)"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   320
  @{index_ML Symtab.map_entry: "Symtab.key -> ('a -> 'a)
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   321
    -> 'a Symtab.table -> 'a Symtab.table"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   322
  @{index_ML Symtab.map_default: "(Symtab.key * 'a) -> ('a -> 'a)
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   323
    -> 'a Symtab.table -> 'a Symtab.table"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   324
  @{index_ML Symtab.join: "(Symtab.key -> 'a * 'a -> 'a) (*exception Symtab.DUP/Symtab.SAME*)
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   325
    -> 'a Symtab.table * 'a Symtab.table
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   326
    -> 'a Symtab.table (*exception Symtab.DUPS*)"} \\
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   327
  @{index_ML Symtab.merge: "('a * 'a -> bool)
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   328
    -> 'a Symtab.table * 'a Symtab.table
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   329
    -> 'a Symtab.table (*exception Symtab.DUPS*)"}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   330
  \end{mldecls}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   331
*}
3593a76c9ed3 added outline for Isabelle library description
haftmann
parents: 21502
diff changeset
   332
22503
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   333
text {*
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   334
  Tables are an efficient representation of finite mappings without
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   335
  any notion of order;  due to their efficiency they should be used
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   336
  whenever such pure finite mappings are neccessary.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   337
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   338
  The key type of tables must be given explicitly by instantiating
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   339
  the @{ML_functor TableFun} functor which takes the key type
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   340
  together with its @{ML_type order}; for convience, we restrict
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   341
  here to the @{ML_struct Symtab} instance with @{ML_type string}
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   342
  as key type.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   343
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   344
  Most table functions correspond to those of association lists.
d53664118418 added some sketches about library functions
haftmann
parents: 22322
diff changeset
   345
*}
20489
a684fc70d04e tentative appendix C;
wenzelm
parents: 18554
diff changeset
   346
a684fc70d04e tentative appendix C;
wenzelm
parents: 18554
diff changeset
   347
chapter {* Cookbook *}
a684fc70d04e tentative appendix C;
wenzelm
parents: 18554
diff changeset
   348
20491
wenzelm
parents: 20489
diff changeset
   349
section {* A method that depends on declarations in the context *}
20489
a684fc70d04e tentative appendix C;
wenzelm
parents: 18554
diff changeset
   350
a684fc70d04e tentative appendix C;
wenzelm
parents: 18554
diff changeset
   351
text FIXME
a684fc70d04e tentative appendix C;
wenzelm
parents: 18554
diff changeset
   352
18538
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
   353
end