doc-src/IsarImplementation/Thy/ML.thy
author haftmann
Sat, 10 Feb 2007 09:26:11 +0100
changeset 22293 3593a76c9ed3
parent 21502 7f3ea2b3bab6
child 22322 b9924abb8c66
permissions -rw-r--r--
added outline for Isabelle library description
     1 (* $Id$ *)
     2 
     3 theory "ML" imports base begin
     4 
     5 chapter {* Aesthetics of ML programming *}
     6 
     7 text FIXME
     8 
     9 text {* This style guide is loosely based on
    10   \url{http://caml.inria.fr/resources/doc/guides/guidelines.en.html}.
    11 %  FIMXE \url{http://www.cs.cornell.edu/Courses/cs312/2003sp/handouts/style.htm}
    12 
    13   Like any style guide, it should not be interpreted dogmatically.
    14   Instead, it forms a collection of recommendations which,
    15   if obeyed, result in code that is not considered to be
    16   obfuscated.  In certain cases, derivations are encouraged,
    17   as far as you know what you are doing.
    18 
    19   \begin{description}
    20 
    21     \item[fundamental law of programming]
    22       Whenever writing code, keep in mind: A program is
    23       written once, modified ten times, and read
    24       100 times.  So simplify its writing,
    25       always keep future modifications in mind,
    26       and never jeopardize readability.  Every second you hesitate
    27       to spend on making your code more clear you will
    28       have to spend ten times understanding what you have
    29       written later on.
    30 
    31     \item[white space matters]
    32       Treat white space in your code as if it determines
    33       the meaning of code.
    34 
    35       \begin{itemize}
    36 
    37         \item The space bar is the easiest key to find on the keyboard,
    38           press it as often as necessary. {\ttfamily 2 + 2} is better
    39           than {\ttfamily 2+2}, likewise {\ttfamily f (x, y)}
    40           better than {\ttfamily f(x,y)}.
    41 
    42         \item Restrict your lines to \emph{at most} 80 characters.
    43           This will allow you to keep the beginning of a line
    44           in view while watching its end.
    45 
    46         \item Ban tabs; they are a context-sensitive formatting
    47           feature and likely to confuse anyone not using your
    48           favourite editor.
    49 
    50         \item Get rid of trailing whitespace.  Instead, do not
    51           surpess a trailing newline at the end of your files.
    52 
    53         \item Choose a generally accepted style of indentation,
    54           then use it systematically throughout the whole
    55           application.  An indentation of two spaces is appropriate.
    56           Avoid dangling indentation.
    57 
    58       \end{itemize}
    59 
    60     \item[cut-and-paste succeeds over copy-and-paste]
    61       \emph{Never} copy-and-paste code when programming.  If you
    62         need the same piece of code twice, introduce a
    63         reasonable auxiliary function (if there is no
    64         such function, very likely you got something wrong).
    65         Any copy-and-paste will turn out to be painful 
    66         when something has to be changed or fixed later on.
    67 
    68     \item[comments]
    69       are a device which requires careful thinking before using
    70       it.  The best comment for your code should be the code itself.
    71       Prefer efforts to write clear, understandable code
    72       over efforts to explain nasty code.
    73 
    74     \item[functional programming is based on functions]
    75       Avoid ``constructivisms'', e.g. pass a table lookup function,
    76       rather than an actual table with lookup in body.  Accustom
    77       your way of codeing to the level of expressiveness
    78       a functional programming language is giving onto you.
    79 
    80     \item[tuples]
    81       are often in the way.  When there is no striking argument
    82       to tuple function arguments, just write your function curried.
    83 
    84     \item[telling names]
    85       Any name should tell its purpose as exactly as possible,
    86       while keeping its length to the absolutely neccessary minimum.
    87       Always give the same name to function arguments which
    88       have the same meaning. Separate words by underscores
    89       (``@{verbatim int_of_string}'', not ``@{verbatim intOfString}'')
    90 
    91   \end{description}
    92 *}
    93 
    94 
    95 chapter {* Basic library functions *}
    96 
    97 text {*
    98   Beyond the proposal of the SML/NJ basis library, Isabelle comes
    99   with its own library, from which selected parts are given here.
   100   See further files \emph{Pure/library.ML} and \emph{Pure/General/*.ML}.
   101 *}
   102 
   103 section {* Linear transformations *}
   104 
   105 text %mlref {*
   106   \begin{mldecls}
   107   @{index_ML "(op |>)": "'a * ('a -> 'b) -> 'b"} \\
   108   @{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
   109   @{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
   110   \end{mldecls}
   111 *}
   112 
   113 text FIXME
   114 
   115 text %mlref {*
   116   \begin{mldecls}
   117   @{index_ML "(op |->)": "('c * 'a) * ('c -> 'a -> 'b) -> 'b"} \\
   118   @{index_ML "(op |>>)": "('a * 'c) * ('a -> 'b) -> 'b * 'c"} \\
   119   @{index_ML "(op ||>)": "('c * 'a) * ('a -> 'b) -> 'c * 'b"} \\
   120   @{index_ML "(op ||>>)": "('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b"} \\
   121   @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
   122   \end{mldecls}
   123 *}
   124 
   125 text %mlref {*
   126   \begin{mldecls}
   127   @{index_ML "(op #>)": "('a -> 'b) * ('b -> 'c) -> 'a -> 'c"} \\
   128   @{index_ML "(op #->)": "('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd"} \\
   129   @{index_ML "(op #>>)": "('a -> 'c * 'b) * ('c -> 'd) -> 'a -> 'd * 'b"} \\
   130   @{index_ML "(op ##>)": "('a -> 'c * 'b) * ('b -> 'd) -> 'a -> 'c * 'd"} \\
   131   @{index_ML "(op ##>>)": "('a -> 'c * 'b) * ('b -> 'e * 'd) -> 'a -> ('c * 'e) * 'd"} \\
   132   \end{mldecls}
   133 *}
   134 
   135 text %mlref {*
   136   \begin{mldecls}
   137   @{index_ML "(op `)": "('b -> 'a) -> 'b -> 'a * 'b"} \\
   138   @{index_ML tap: "('b -> 'a) -> 'b -> 'b"} \\
   139   \end{mldecls}
   140 *}
   141 
   142 section {* Options and partiality *}
   143 
   144 text %mlref {*
   145   \begin{mldecls}
   146   @{index_ML is_some: "'a option -> bool"} \\
   147   @{index_ML is_none: "'a option -> bool"} \\
   148   @{index_ML the: "'a option -> 'a"} \\
   149   @{index_ML these: "'a list option -> 'a list"} \\
   150   @{index_ML the_list: "'a option -> 'a list"} \\
   151   @{index_ML the_default: "'a -> 'a option -> 'a"} \\
   152   @{index_ML try: "('a -> 'b) -> 'a -> 'b option"} \\
   153   @{index_ML can: "('a -> 'b) -> 'a -> bool"} \\
   154   \end{mldecls}
   155 *}
   156 
   157 text FIXME
   158 
   159 section {* Common data structures *}
   160 
   161 text "FIXME chronicle"
   162 
   163 subsection {* Lists (as set-like data structures) *}
   164 
   165 text {*
   166   \begin{mldecls}
   167   @{index_ML member: "('b * 'a -> bool) -> 'a list -> 'b -> bool"} \\
   168   @{index_ML insert: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
   169   @{index_ML remove: "('b * 'a -> bool) -> 'b -> 'a list -> 'a list"} \\
   170   @{index_ML merge: "('a * 'a -> bool) -> 'a list * 'a list -> 'a list"} \\
   171   \end{mldecls}
   172 *}
   173 
   174 text FIXME
   175 
   176 subsection {* Association lists *}
   177 
   178 text {*
   179   \begin{mldecls}
   180   @{index_ML_exc AList.DUP} \\
   181   @{index_ML AList.lookup: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option"} \\
   182   @{index_ML AList.defined: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool"} \\
   183   @{index_ML AList.update: "('a * 'a -> bool) -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list"} \\
   184   @{index_ML AList.default: "('a * 'a -> bool) -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list"} \\
   185   @{index_ML AList.delete: "('a * 'b -> bool) -> 'a -> ('b * 'c) list -> ('b * 'c) list"} \\
   186   @{index_ML AList.map_entry: "('a * 'b -> bool) -> 'a
   187     -> ('c -> 'c) -> ('b * 'c) list -> ('b * 'c) list"} \\
   188   @{index_ML AList.map_default: "('a * 'a -> bool) -> 'a * 'b -> ('b -> 'b)
   189     -> ('a * 'b) list -> ('a * 'b) list"} \\
   190   @{index_ML AList.join: "('a * 'a -> bool) -> ('a -> 'b * 'b -> 'b) (*exception DUP*)
   191     -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception AList.DUP*)"} \\
   192   @{index_ML AList.merge: "('a * 'a -> bool) -> ('b * 'b -> bool)
   193     -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception AList.DUP*)"}
   194   \end{mldecls}
   195 *}
   196 
   197 text FIXME
   198 
   199 subsection {* Tables *}
   200 
   201 text {*
   202   \begin{mldecls}
   203   @{index_ML_type Symtab.key} \\
   204   @{index_ML_type "'a Symtab.table"} \\
   205   @{index_ML_exc Symtab.DUP: Symtab.key} \\
   206   @{index_ML_exc Symtab.DUPS: "Symtab.key list"} \\
   207   @{index_ML_exc Symtab.SAME} \\
   208   @{index_ML_exc Symtab.UNDEF: Symtab.key} \\
   209   @{index_ML Symtab.empty: "'a Symtab.table"} \\
   210   @{index_ML Symtab.dest: "'a Symtab.table -> (Symtab.key * 'a) list"} \\
   211   @{index_ML Symtab.keys: "'a Symtab.table -> Symtab.key list"} \\
   212   @{index_ML Symtab.lookup: "'a Symtab.table -> Symtab.key -> 'a option"} \\
   213   @{index_ML Symtab.defined: "'a Symtab.table -> Symtab.key -> bool"} \\
   214   @{index_ML Symtab.update: "(Symtab.key * 'a) -> 'a Symtab.table -> 'a Symtab.table"} \\
   215   @{index_ML Symtab.default: "Symtab.key * 'a -> 'a Symtab.table -> 'a Symtab.table"} \\
   216   @{index_ML Symtab.delete: "Symtab.key
   217     -> 'a Symtab.table -> 'a Symtab.table (*exception Symtab.UNDEF*)"} \\
   218   @{index_ML Symtab.map_entry: "Symtab.key -> ('a -> 'a)
   219     -> 'a Symtab.table -> 'a Symtab.table"} \\
   220   @{index_ML Symtab.map_default: "(Symtab.key * 'a) -> ('a -> 'a)
   221     -> 'a Symtab.table -> 'a Symtab.table"} \\
   222   @{index_ML Symtab.join: "(Symtab.key -> 'a * 'a -> 'a) (*exception Symtab.DUP/Symtab.SAME*)
   223     -> 'a Symtab.table * 'a Symtab.table
   224     -> 'a Symtab.table (*exception Symtab.DUPS*)"} \\
   225   @{index_ML Symtab.merge: "('a * 'a -> bool)
   226     -> 'a Symtab.table * 'a Symtab.table
   227     -> 'a Symtab.table (*exception Symtab.DUPS*)"}
   228   \end{mldecls}
   229 *}
   230 
   231 text FIXME
   232 
   233 chapter {* Cookbook *}
   234 
   235 section {* A method that depends on declarations in the context *}
   236 
   237 text FIXME
   238 
   239 end