doc-src/IsarRef/Thy/HOL_Specific.thy
author blanchet
Tue Feb 23 14:11:36 2010 +0100 (2010-02-23)
changeset 35331 450ab945c451
parent 34172 4301e9ea1c54
child 35351 7425aece4ee3
permissions -rw-r--r--
document Quickcheck's "no_assms" option
wenzelm@26840
     1
theory HOL_Specific
wenzelm@26849
     2
imports Main
wenzelm@26840
     3
begin
wenzelm@26840
     4
wenzelm@26852
     5
chapter {* Isabelle/HOL \label{ch:hol} *}
wenzelm@26849
     6
wenzelm@26849
     7
section {* Primitive types \label{sec:hol-typedef} *}
wenzelm@26849
     8
wenzelm@26849
     9
text {*
wenzelm@26849
    10
  \begin{matharray}{rcl}
wenzelm@28761
    11
    @{command_def (HOL) "typedecl"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
    12
    @{command_def (HOL) "typedef"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@26849
    13
  \end{matharray}
wenzelm@26849
    14
wenzelm@26849
    15
  \begin{rail}
wenzelm@26849
    16
    'typedecl' typespec infix?
wenzelm@26849
    17
    ;
wenzelm@26849
    18
    'typedef' altname? abstype '=' repset
wenzelm@26849
    19
    ;
wenzelm@26849
    20
wenzelm@26849
    21
    altname: '(' (name | 'open' | 'open' name) ')'
wenzelm@26849
    22
    ;
wenzelm@26849
    23
    abstype: typespec infix?
wenzelm@26849
    24
    ;
wenzelm@26849
    25
    repset: term ('morphisms' name name)?
wenzelm@26849
    26
    ;
wenzelm@26849
    27
  \end{rail}
wenzelm@26849
    28
wenzelm@28760
    29
  \begin{description}
wenzelm@26849
    30
  
wenzelm@28760
    31
  \item @{command (HOL) "typedecl"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} is similar
wenzelm@28760
    32
  to the original @{command "typedecl"} of Isabelle/Pure (see
wenzelm@28760
    33
  \secref{sec:types-pure}), but also declares type arity @{text "t ::
wenzelm@28760
    34
  (type, \<dots>, type) type"}, making @{text t} an actual HOL type
wenzelm@28760
    35
  constructor.  %FIXME check, update
wenzelm@26849
    36
  
wenzelm@28760
    37
  \item @{command (HOL) "typedef"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t = A"} sets up
wenzelm@28760
    38
  a goal stating non-emptiness of the set @{text A}.  After finishing
wenzelm@28760
    39
  the proof, the theory will be augmented by a Gordon/HOL-style type
wenzelm@28760
    40
  definition, which establishes a bijection between the representing
wenzelm@28760
    41
  set @{text A} and the new type @{text t}.
wenzelm@26849
    42
  
wenzelm@26849
    43
  Technically, @{command (HOL) "typedef"} defines both a type @{text
wenzelm@26849
    44
  t} and a set (term constant) of the same name (an alternative base
wenzelm@26849
    45
  name may be given in parentheses).  The injection from type to set
wenzelm@26849
    46
  is called @{text Rep_t}, its inverse @{text Abs_t} (this may be
wenzelm@26849
    47
  changed via an explicit @{keyword (HOL) "morphisms"} declaration).
wenzelm@26849
    48
  
wenzelm@26849
    49
  Theorems @{text Rep_t}, @{text Rep_t_inverse}, and @{text
wenzelm@26849
    50
  Abs_t_inverse} provide the most basic characterization as a
wenzelm@26849
    51
  corresponding injection/surjection pair (in both directions).  Rules
wenzelm@26849
    52
  @{text Rep_t_inject} and @{text Abs_t_inject} provide a slightly
wenzelm@26849
    53
  more convenient view on the injectivity part, suitable for automated
wenzelm@26894
    54
  proof tools (e.g.\ in @{attribute simp} or @{attribute iff}
wenzelm@26894
    55
  declarations).  Rules @{text Rep_t_cases}/@{text Rep_t_induct}, and
wenzelm@26894
    56
  @{text Abs_t_cases}/@{text Abs_t_induct} provide alternative views
wenzelm@26894
    57
  on surjectivity; these are already declared as set or type rules for
wenzelm@26849
    58
  the generic @{method cases} and @{method induct} methods.
wenzelm@26849
    59
  
wenzelm@26849
    60
  An alternative name may be specified in parentheses; the default is
wenzelm@26849
    61
  to use @{text t} as indicated before.  The ``@{text "(open)"}''
wenzelm@26849
    62
  declaration suppresses a separate constant definition for the
wenzelm@26849
    63
  representing set.
wenzelm@26849
    64
wenzelm@28760
    65
  \end{description}
wenzelm@26849
    66
wenzelm@26849
    67
  Note that raw type declarations are rarely used in practice; the
wenzelm@26849
    68
  main application is with experimental (or even axiomatic!) theory
wenzelm@26849
    69
  fragments.  Instead of primitive HOL type definitions, user-level
wenzelm@26849
    70
  theories usually refer to higher-level packages such as @{command
wenzelm@26849
    71
  (HOL) "record"} (see \secref{sec:hol-record}) or @{command (HOL)
wenzelm@26849
    72
  "datatype"} (see \secref{sec:hol-datatype}).
wenzelm@26849
    73
*}
wenzelm@26849
    74
wenzelm@26849
    75
wenzelm@26849
    76
section {* Adhoc tuples *}
wenzelm@26849
    77
wenzelm@26849
    78
text {*
wenzelm@26849
    79
  \begin{matharray}{rcl}
wenzelm@28761
    80
    @{attribute (HOL) split_format}@{text "\<^sup>*"} & : & @{text attribute} \\
wenzelm@26849
    81
  \end{matharray}
wenzelm@26849
    82
wenzelm@26849
    83
  \begin{rail}
haftmann@31912
    84
    'split\_format' ((( name * ) + 'and') | ('(' 'complete' ')'))
wenzelm@26849
    85
    ;
wenzelm@26849
    86
  \end{rail}
wenzelm@26849
    87
wenzelm@28760
    88
  \begin{description}
wenzelm@26849
    89
  
wenzelm@28760
    90
  \item @{attribute (HOL) split_format}~@{text "p\<^sub>1 \<dots> p\<^sub>m \<AND> \<dots>
wenzelm@28760
    91
  \<AND> q\<^sub>1 \<dots> q\<^sub>n"} puts expressions of low-level tuple types into
wenzelm@28760
    92
  canonical form as specified by the arguments given; the @{text i}-th
wenzelm@28760
    93
  collection of arguments refers to occurrences in premise @{text i}
wenzelm@28760
    94
  of the rule.  The ``@{text "(complete)"}'' option causes \emph{all}
wenzelm@28760
    95
  arguments in function applications to be represented canonically
wenzelm@28760
    96
  according to their tuple type structure.
wenzelm@26849
    97
wenzelm@26849
    98
  Note that these operations tend to invent funny names for new local
wenzelm@26849
    99
  parameters to be introduced.
wenzelm@26849
   100
wenzelm@28760
   101
  \end{description}
wenzelm@26849
   102
*}
wenzelm@26849
   103
wenzelm@26849
   104
wenzelm@26849
   105
section {* Records \label{sec:hol-record} *}
wenzelm@26849
   106
wenzelm@26849
   107
text {*
wenzelm@26849
   108
  In principle, records merely generalize the concept of tuples, where
wenzelm@26849
   109
  components may be addressed by labels instead of just position.  The
wenzelm@26849
   110
  logical infrastructure of records in Isabelle/HOL is slightly more
wenzelm@26849
   111
  advanced, though, supporting truly extensible record schemes.  This
wenzelm@26849
   112
  admits operations that are polymorphic with respect to record
wenzelm@26849
   113
  extension, yielding ``object-oriented'' effects like (single)
wenzelm@26849
   114
  inheritance.  See also \cite{NaraschewskiW-TPHOLs98} for more
wenzelm@26849
   115
  details on object-oriented verification and record subtyping in HOL.
wenzelm@26849
   116
*}
wenzelm@26849
   117
wenzelm@26849
   118
wenzelm@26849
   119
subsection {* Basic concepts *}
wenzelm@26849
   120
wenzelm@26849
   121
text {*
wenzelm@26849
   122
  Isabelle/HOL supports both \emph{fixed} and \emph{schematic} records
wenzelm@26849
   123
  at the level of terms and types.  The notation is as follows:
wenzelm@26849
   124
wenzelm@26849
   125
  \begin{center}
wenzelm@26849
   126
  \begin{tabular}{l|l|l}
wenzelm@26849
   127
    & record terms & record types \\ \hline
wenzelm@26849
   128
    fixed & @{text "\<lparr>x = a, y = b\<rparr>"} & @{text "\<lparr>x :: A, y :: B\<rparr>"} \\
wenzelm@26849
   129
    schematic & @{text "\<lparr>x = a, y = b, \<dots> = m\<rparr>"} &
wenzelm@26849
   130
      @{text "\<lparr>x :: A, y :: B, \<dots> :: M\<rparr>"} \\
wenzelm@26849
   131
  \end{tabular}
wenzelm@26849
   132
  \end{center}
wenzelm@26849
   133
wenzelm@26849
   134
  \noindent The ASCII representation of @{text "\<lparr>x = a\<rparr>"} is @{text
wenzelm@26849
   135
  "(| x = a |)"}.
wenzelm@26849
   136
wenzelm@26849
   137
  A fixed record @{text "\<lparr>x = a, y = b\<rparr>"} has field @{text x} of value
wenzelm@26849
   138
  @{text a} and field @{text y} of value @{text b}.  The corresponding
wenzelm@26849
   139
  type is @{text "\<lparr>x :: A, y :: B\<rparr>"}, assuming that @{text "a :: A"}
wenzelm@26849
   140
  and @{text "b :: B"}.
wenzelm@26849
   141
wenzelm@26849
   142
  A record scheme like @{text "\<lparr>x = a, y = b, \<dots> = m\<rparr>"} contains fields
wenzelm@26849
   143
  @{text x} and @{text y} as before, but also possibly further fields
wenzelm@26849
   144
  as indicated by the ``@{text "\<dots>"}'' notation (which is actually part
wenzelm@26849
   145
  of the syntax).  The improper field ``@{text "\<dots>"}'' of a record
wenzelm@26849
   146
  scheme is called the \emph{more part}.  Logically it is just a free
wenzelm@26849
   147
  variable, which is occasionally referred to as ``row variable'' in
wenzelm@26849
   148
  the literature.  The more part of a record scheme may be
wenzelm@26849
   149
  instantiated by zero or more further components.  For example, the
wenzelm@26849
   150
  previous scheme may get instantiated to @{text "\<lparr>x = a, y = b, z =
wenzelm@26852
   151
  c, \<dots> = m'\<rparr>"}, where @{text m'} refers to a different more part.
wenzelm@26849
   152
  Fixed records are special instances of record schemes, where
wenzelm@26849
   153
  ``@{text "\<dots>"}'' is properly terminated by the @{text "() :: unit"}
wenzelm@26849
   154
  element.  In fact, @{text "\<lparr>x = a, y = b\<rparr>"} is just an abbreviation
wenzelm@26849
   155
  for @{text "\<lparr>x = a, y = b, \<dots> = ()\<rparr>"}.
wenzelm@26849
   156
  
wenzelm@26849
   157
  \medskip Two key observations make extensible records in a simply
wenzelm@26849
   158
  typed language like HOL work out:
wenzelm@26849
   159
wenzelm@26849
   160
  \begin{enumerate}
wenzelm@26849
   161
wenzelm@26849
   162
  \item the more part is internalized, as a free term or type
wenzelm@26849
   163
  variable,
wenzelm@26849
   164
wenzelm@26852
   165
  \item field names are externalized, they cannot be accessed within
wenzelm@26852
   166
  the logic as first-class values.
wenzelm@26849
   167
wenzelm@26849
   168
  \end{enumerate}
wenzelm@26849
   169
wenzelm@26849
   170
  \medskip In Isabelle/HOL record types have to be defined explicitly,
wenzelm@26849
   171
  fixing their field names and types, and their (optional) parent
wenzelm@26849
   172
  record.  Afterwards, records may be formed using above syntax, while
wenzelm@26849
   173
  obeying the canonical order of fields as given by their declaration.
wenzelm@26849
   174
  The record package provides several standard operations like
wenzelm@26849
   175
  selectors and updates.  The common setup for various generic proof
wenzelm@26849
   176
  tools enable succinct reasoning patterns.  See also the Isabelle/HOL
wenzelm@26849
   177
  tutorial \cite{isabelle-hol-book} for further instructions on using
wenzelm@26849
   178
  records in practice.
wenzelm@26849
   179
*}
wenzelm@26849
   180
wenzelm@26849
   181
wenzelm@26849
   182
subsection {* Record specifications *}
wenzelm@26849
   183
wenzelm@26849
   184
text {*
wenzelm@26849
   185
  \begin{matharray}{rcl}
wenzelm@28761
   186
    @{command_def (HOL) "record"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@26849
   187
  \end{matharray}
wenzelm@26849
   188
wenzelm@26849
   189
  \begin{rail}
wenzelm@26849
   190
    'record' typespec '=' (type '+')? (constdecl +)
wenzelm@26849
   191
    ;
wenzelm@26849
   192
  \end{rail}
wenzelm@26849
   193
wenzelm@28760
   194
  \begin{description}
wenzelm@26849
   195
wenzelm@28760
   196
  \item @{command (HOL) "record"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t = \<tau> + c\<^sub>1 :: \<sigma>\<^sub>1
wenzelm@28760
   197
  \<dots> c\<^sub>n :: \<sigma>\<^sub>n"} defines extensible record type @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"},
wenzelm@26849
   198
  derived from the optional parent record @{text "\<tau>"} by adding new
wenzelm@26849
   199
  field components @{text "c\<^sub>i :: \<sigma>\<^sub>i"} etc.
wenzelm@26849
   200
wenzelm@26849
   201
  The type variables of @{text "\<tau>"} and @{text "\<sigma>\<^sub>i"} need to be
wenzelm@26849
   202
  covered by the (distinct) parameters @{text "\<alpha>\<^sub>1, \<dots>,
wenzelm@26849
   203
  \<alpha>\<^sub>m"}.  Type constructor @{text t} has to be new, while @{text
wenzelm@26849
   204
  \<tau>} needs to specify an instance of an existing record type.  At
wenzelm@26849
   205
  least one new field @{text "c\<^sub>i"} has to be specified.
wenzelm@26849
   206
  Basically, field names need to belong to a unique record.  This is
wenzelm@26849
   207
  not a real restriction in practice, since fields are qualified by
wenzelm@26849
   208
  the record name internally.
wenzelm@26849
   209
wenzelm@26849
   210
  The parent record specification @{text \<tau>} is optional; if omitted
wenzelm@26849
   211
  @{text t} becomes a root record.  The hierarchy of all records
wenzelm@26849
   212
  declared within a theory context forms a forest structure, i.e.\ a
wenzelm@26849
   213
  set of trees starting with a root record each.  There is no way to
wenzelm@26849
   214
  merge multiple parent records!
wenzelm@26849
   215
wenzelm@26849
   216
  For convenience, @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"} is made a
wenzelm@26849
   217
  type abbreviation for the fixed record type @{text "\<lparr>c\<^sub>1 ::
wenzelm@26849
   218
  \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n\<rparr>"}, likewise is @{text
wenzelm@26849
   219
  "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m, \<zeta>) t_scheme"} made an abbreviation for
wenzelm@26849
   220
  @{text "\<lparr>c\<^sub>1 :: \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n, \<dots> ::
wenzelm@26849
   221
  \<zeta>\<rparr>"}.
wenzelm@26849
   222
wenzelm@28760
   223
  \end{description}
wenzelm@26849
   224
*}
wenzelm@26849
   225
wenzelm@26849
   226
wenzelm@26849
   227
subsection {* Record operations *}
wenzelm@26849
   228
wenzelm@26849
   229
text {*
wenzelm@26849
   230
  Any record definition of the form presented above produces certain
wenzelm@26849
   231
  standard operations.  Selectors and updates are provided for any
wenzelm@26849
   232
  field, including the improper one ``@{text more}''.  There are also
wenzelm@26849
   233
  cumulative record constructor functions.  To simplify the
wenzelm@26849
   234
  presentation below, we assume for now that @{text "(\<alpha>\<^sub>1, \<dots>,
wenzelm@26849
   235
  \<alpha>\<^sub>m) t"} is a root record with fields @{text "c\<^sub>1 ::
wenzelm@26849
   236
  \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n"}.
wenzelm@26849
   237
wenzelm@26849
   238
  \medskip \textbf{Selectors} and \textbf{updates} are available for
wenzelm@26849
   239
  any field (including ``@{text more}''):
wenzelm@26849
   240
wenzelm@26849
   241
  \begin{matharray}{lll}
wenzelm@26852
   242
    @{text "c\<^sub>i"} & @{text "::"} & @{text "\<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<sigma>\<^sub>i"} \\
wenzelm@26852
   243
    @{text "c\<^sub>i_update"} & @{text "::"} & @{text "\<sigma>\<^sub>i \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
wenzelm@26849
   244
  \end{matharray}
wenzelm@26849
   245
wenzelm@26849
   246
  There is special syntax for application of updates: @{text "r\<lparr>x :=
wenzelm@26849
   247
  a\<rparr>"} abbreviates term @{text "x_update a r"}.  Further notation for
wenzelm@26849
   248
  repeated updates is also available: @{text "r\<lparr>x := a\<rparr>\<lparr>y := b\<rparr>\<lparr>z :=
wenzelm@26849
   249
  c\<rparr>"} may be written @{text "r\<lparr>x := a, y := b, z := c\<rparr>"}.  Note that
wenzelm@26849
   250
  because of postfix notation the order of fields shown here is
wenzelm@26849
   251
  reverse than in the actual term.  Since repeated updates are just
wenzelm@26849
   252
  function applications, fields may be freely permuted in @{text "\<lparr>x
wenzelm@26849
   253
  := a, y := b, z := c\<rparr>"}, as far as logical equality is concerned.
wenzelm@26849
   254
  Thus commutativity of independent updates can be proven within the
wenzelm@26849
   255
  logic for any two fields, but not as a general theorem.
wenzelm@26849
   256
wenzelm@26849
   257
  \medskip The \textbf{make} operation provides a cumulative record
wenzelm@26849
   258
  constructor function:
wenzelm@26849
   259
wenzelm@26849
   260
  \begin{matharray}{lll}
wenzelm@26852
   261
    @{text "t.make"} & @{text "::"} & @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26849
   262
  \end{matharray}
wenzelm@26849
   263
wenzelm@26849
   264
  \medskip We now reconsider the case of non-root records, which are
wenzelm@26849
   265
  derived of some parent.  In general, the latter may depend on
wenzelm@26849
   266
  another parent as well, resulting in a list of \emph{ancestor
wenzelm@26849
   267
  records}.  Appending the lists of fields of all ancestors results in
wenzelm@26849
   268
  a certain field prefix.  The record package automatically takes care
wenzelm@26849
   269
  of this by lifting operations over this context of ancestor fields.
wenzelm@26849
   270
  Assuming that @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"} has ancestor
wenzelm@26849
   271
  fields @{text "b\<^sub>1 :: \<rho>\<^sub>1, \<dots>, b\<^sub>k :: \<rho>\<^sub>k"},
wenzelm@26849
   272
  the above record operations will get the following types:
wenzelm@26849
   273
wenzelm@26852
   274
  \medskip
wenzelm@26852
   275
  \begin{tabular}{lll}
wenzelm@26852
   276
    @{text "c\<^sub>i"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<sigma>\<^sub>i"} \\
wenzelm@26849
   277
    @{text "c\<^sub>i_update"} & @{text "::"} & @{text "\<sigma>\<^sub>i \<Rightarrow> 
wenzelm@26852
   278
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow>
wenzelm@26852
   279
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
wenzelm@26852
   280
    @{text "t.make"} & @{text "::"} & @{text "\<rho>\<^sub>1 \<Rightarrow> \<dots> \<rho>\<^sub>k \<Rightarrow> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow>
wenzelm@26852
   281
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26852
   282
  \end{tabular}
wenzelm@26852
   283
  \medskip
wenzelm@26849
   284
wenzelm@26852
   285
  \noindent Some further operations address the extension aspect of a
wenzelm@26849
   286
  derived record scheme specifically: @{text "t.fields"} produces a
wenzelm@26849
   287
  record fragment consisting of exactly the new fields introduced here
wenzelm@26849
   288
  (the result may serve as a more part elsewhere); @{text "t.extend"}
wenzelm@26849
   289
  takes a fixed record and adds a given more part; @{text
wenzelm@26849
   290
  "t.truncate"} restricts a record scheme to a fixed record.
wenzelm@26849
   291
wenzelm@26852
   292
  \medskip
wenzelm@26852
   293
  \begin{tabular}{lll}
wenzelm@26852
   294
    @{text "t.fields"} & @{text "::"} & @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26852
   295
    @{text "t.extend"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr> \<Rightarrow>
wenzelm@26852
   296
      \<zeta> \<Rightarrow> \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
wenzelm@26852
   297
    @{text "t.truncate"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26852
   298
  \end{tabular}
wenzelm@26852
   299
  \medskip
wenzelm@26849
   300
wenzelm@26849
   301
  \noindent Note that @{text "t.make"} and @{text "t.fields"} coincide
wenzelm@26849
   302
  for root records.
wenzelm@26849
   303
*}
wenzelm@26849
   304
wenzelm@26849
   305
wenzelm@26849
   306
subsection {* Derived rules and proof tools *}
wenzelm@26849
   307
wenzelm@26849
   308
text {*
wenzelm@26849
   309
  The record package proves several results internally, declaring
wenzelm@26849
   310
  these facts to appropriate proof tools.  This enables users to
wenzelm@26849
   311
  reason about record structures quite conveniently.  Assume that
wenzelm@26849
   312
  @{text t} is a record type as specified above.
wenzelm@26849
   313
wenzelm@26849
   314
  \begin{enumerate}
wenzelm@26849
   315
  
wenzelm@26849
   316
  \item Standard conversions for selectors or updates applied to
wenzelm@26849
   317
  record constructor terms are made part of the default Simplifier
wenzelm@26849
   318
  context; thus proofs by reduction of basic operations merely require
wenzelm@26849
   319
  the @{method simp} method without further arguments.  These rules
wenzelm@26849
   320
  are available as @{text "t.simps"}, too.
wenzelm@26849
   321
  
wenzelm@26849
   322
  \item Selectors applied to updated records are automatically reduced
wenzelm@26849
   323
  by an internal simplification procedure, which is also part of the
wenzelm@26849
   324
  standard Simplifier setup.
wenzelm@26849
   325
wenzelm@26849
   326
  \item Inject equations of a form analogous to @{prop "(x, y) = (x',
wenzelm@26849
   327
  y') \<equiv> x = x' \<and> y = y'"} are declared to the Simplifier and Classical
wenzelm@26849
   328
  Reasoner as @{attribute iff} rules.  These rules are available as
wenzelm@26849
   329
  @{text "t.iffs"}.
wenzelm@26849
   330
wenzelm@26849
   331
  \item The introduction rule for record equality analogous to @{text
wenzelm@26849
   332
  "x r = x r' \<Longrightarrow> y r = y r' \<dots> \<Longrightarrow> r = r'"} is declared to the Simplifier,
wenzelm@26849
   333
  and as the basic rule context as ``@{attribute intro}@{text "?"}''.
wenzelm@26849
   334
  The rule is called @{text "t.equality"}.
wenzelm@26849
   335
wenzelm@26849
   336
  \item Representations of arbitrary record expressions as canonical
wenzelm@26849
   337
  constructor terms are provided both in @{method cases} and @{method
wenzelm@26849
   338
  induct} format (cf.\ the generic proof methods of the same name,
wenzelm@26849
   339
  \secref{sec:cases-induct}).  Several variations are available, for
wenzelm@26849
   340
  fixed records, record schemes, more parts etc.
wenzelm@26849
   341
  
wenzelm@26849
   342
  The generic proof methods are sufficiently smart to pick the most
wenzelm@26849
   343
  sensible rule according to the type of the indicated record
wenzelm@26849
   344
  expression: users just need to apply something like ``@{text "(cases
wenzelm@26849
   345
  r)"}'' to a certain proof problem.
wenzelm@26849
   346
wenzelm@26849
   347
  \item The derived record operations @{text "t.make"}, @{text
wenzelm@26849
   348
  "t.fields"}, @{text "t.extend"}, @{text "t.truncate"} are \emph{not}
wenzelm@26849
   349
  treated automatically, but usually need to be expanded by hand,
wenzelm@26849
   350
  using the collective fact @{text "t.defs"}.
wenzelm@26849
   351
wenzelm@26849
   352
  \end{enumerate}
wenzelm@26849
   353
*}
wenzelm@26849
   354
wenzelm@26849
   355
wenzelm@26849
   356
section {* Datatypes \label{sec:hol-datatype} *}
wenzelm@26849
   357
wenzelm@26849
   358
text {*
wenzelm@26849
   359
  \begin{matharray}{rcl}
wenzelm@28761
   360
    @{command_def (HOL) "datatype"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
   361
  @{command_def (HOL) "rep_datatype"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@26849
   362
  \end{matharray}
wenzelm@26849
   363
wenzelm@26849
   364
  \begin{rail}
wenzelm@26849
   365
    'datatype' (dtspec + 'and')
wenzelm@26849
   366
    ;
haftmann@27452
   367
    'rep\_datatype' ('(' (name +) ')')? (term +)
wenzelm@26849
   368
    ;
wenzelm@26849
   369
wenzelm@26849
   370
    dtspec: parname? typespec infix? '=' (cons + '|')
wenzelm@26849
   371
    ;
haftmann@31912
   372
    cons: name ( type * ) mixfix?
wenzelm@26849
   373
  \end{rail}
wenzelm@26849
   374
wenzelm@28760
   375
  \begin{description}
wenzelm@26849
   376
wenzelm@28760
   377
  \item @{command (HOL) "datatype"} defines inductive datatypes in
wenzelm@26849
   378
  HOL.
wenzelm@26849
   379
wenzelm@28760
   380
  \item @{command (HOL) "rep_datatype"} represents existing types as
wenzelm@26849
   381
  inductive ones, generating the standard infrastructure of derived
wenzelm@26849
   382
  concepts (primitive recursion etc.).
wenzelm@26849
   383
wenzelm@28760
   384
  \end{description}
wenzelm@26849
   385
wenzelm@26849
   386
  The induction and exhaustion theorems generated provide case names
wenzelm@26849
   387
  according to the constructors involved, while parameters are named
wenzelm@26849
   388
  after the types (see also \secref{sec:cases-induct}).
wenzelm@26849
   389
wenzelm@26849
   390
  See \cite{isabelle-HOL} for more details on datatypes, but beware of
wenzelm@26849
   391
  the old-style theory syntax being used there!  Apart from proper
wenzelm@26849
   392
  proof methods for case-analysis and induction, there are also
wenzelm@26849
   393
  emulations of ML tactics @{method (HOL) case_tac} and @{method (HOL)
wenzelm@26849
   394
  induct_tac} available, see \secref{sec:hol-induct-tac}; these admit
wenzelm@26849
   395
  to refer directly to the internal structure of subgoals (including
wenzelm@26849
   396
  internally bound parameters).
wenzelm@26849
   397
*}
wenzelm@26849
   398
wenzelm@26849
   399
wenzelm@26849
   400
section {* Recursive functions \label{sec:recursion} *}
wenzelm@26849
   401
wenzelm@26849
   402
text {*
wenzelm@26849
   403
  \begin{matharray}{rcl}
wenzelm@28761
   404
    @{command_def (HOL) "primrec"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   405
    @{command_def (HOL) "fun"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   406
    @{command_def (HOL) "function"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
wenzelm@28761
   407
    @{command_def (HOL) "termination"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
wenzelm@26849
   408
  \end{matharray}
wenzelm@26849
   409
wenzelm@26849
   410
  \begin{rail}
wenzelm@26849
   411
    'primrec' target? fixes 'where' equations
wenzelm@26849
   412
    ;
wenzelm@26849
   413
    equations: (thmdecl? prop + '|')
wenzelm@26849
   414
    ;
wenzelm@26985
   415
    ('fun' | 'function') target? functionopts? fixes 'where' clauses
wenzelm@26849
   416
    ;
wenzelm@26849
   417
    clauses: (thmdecl? prop ('(' 'otherwise' ')')? + '|')
wenzelm@26849
   418
    ;
wenzelm@26985
   419
    functionopts: '(' (('sequential' | 'domintros' | 'tailrec' | 'default' term) + ',') ')'
wenzelm@26849
   420
    ;
wenzelm@26849
   421
    'termination' ( term )?
wenzelm@26849
   422
  \end{rail}
wenzelm@26849
   423
wenzelm@28760
   424
  \begin{description}
wenzelm@26849
   425
wenzelm@28760
   426
  \item @{command (HOL) "primrec"} defines primitive recursive
wenzelm@26849
   427
  functions over datatypes, see also \cite{isabelle-HOL}.
wenzelm@26849
   428
wenzelm@28760
   429
  \item @{command (HOL) "function"} defines functions by general
wenzelm@26849
   430
  wellfounded recursion. A detailed description with examples can be
wenzelm@26849
   431
  found in \cite{isabelle-function}. The function is specified by a
wenzelm@26849
   432
  set of (possibly conditional) recursive equations with arbitrary
wenzelm@26849
   433
  pattern matching. The command generates proof obligations for the
wenzelm@26849
   434
  completeness and the compatibility of patterns.
wenzelm@26849
   435
wenzelm@26849
   436
  The defined function is considered partial, and the resulting
wenzelm@26849
   437
  simplification rules (named @{text "f.psimps"}) and induction rule
wenzelm@26849
   438
  (named @{text "f.pinduct"}) are guarded by a generated domain
wenzelm@26849
   439
  predicate @{text "f_dom"}. The @{command (HOL) "termination"}
wenzelm@26849
   440
  command can then be used to establish that the function is total.
wenzelm@26849
   441
wenzelm@28760
   442
  \item @{command (HOL) "fun"} is a shorthand notation for ``@{command
wenzelm@28760
   443
  (HOL) "function"}~@{text "(sequential)"}, followed by automated
wenzelm@28760
   444
  proof attempts regarding pattern matching and termination.  See
wenzelm@28760
   445
  \cite{isabelle-function} for further details.
wenzelm@26849
   446
wenzelm@28760
   447
  \item @{command (HOL) "termination"}~@{text f} commences a
wenzelm@26849
   448
  termination proof for the previously defined function @{text f}.  If
wenzelm@26849
   449
  this is omitted, the command refers to the most recent function
wenzelm@26849
   450
  definition.  After the proof is closed, the recursive equations and
wenzelm@26849
   451
  the induction principle is established.
wenzelm@26849
   452
wenzelm@28760
   453
  \end{description}
wenzelm@26849
   454
haftmann@27452
   455
  Recursive definitions introduced by the @{command (HOL) "function"}
haftmann@27452
   456
  command accommodate
wenzelm@26849
   457
  reasoning by induction (cf.\ \secref{sec:cases-induct}): rule @{text
wenzelm@26849
   458
  "c.induct"} (where @{text c} is the name of the function definition)
wenzelm@26849
   459
  refers to a specific induction rule, with parameters named according
krauss@33857
   460
  to the user-specified equations. Cases are numbered (starting from 1).
krauss@33857
   461
krauss@33857
   462
  For @{command (HOL) "primrec"}, the induction principle coincides
haftmann@27452
   463
  with structural recursion on the datatype the recursion is carried
haftmann@27452
   464
  out.
wenzelm@26849
   465
wenzelm@26849
   466
  The equations provided by these packages may be referred later as
wenzelm@26849
   467
  theorem list @{text "f.simps"}, where @{text f} is the (collective)
wenzelm@26849
   468
  name of the functions defined.  Individual equations may be named
wenzelm@26849
   469
  explicitly as well.
wenzelm@26849
   470
wenzelm@26849
   471
  The @{command (HOL) "function"} command accepts the following
wenzelm@26849
   472
  options.
wenzelm@26849
   473
wenzelm@28760
   474
  \begin{description}
wenzelm@26849
   475
wenzelm@28760
   476
  \item @{text sequential} enables a preprocessor which disambiguates
wenzelm@28760
   477
  overlapping patterns by making them mutually disjoint.  Earlier
wenzelm@28760
   478
  equations take precedence over later ones.  This allows to give the
wenzelm@28760
   479
  specification in a format very similar to functional programming.
wenzelm@28760
   480
  Note that the resulting simplification and induction rules
wenzelm@28760
   481
  correspond to the transformed specification, not the one given
wenzelm@26849
   482
  originally. This usually means that each equation given by the user
wenzelm@26849
   483
  may result in several theroems.  Also note that this automatic
wenzelm@26849
   484
  transformation only works for ML-style datatype patterns.
wenzelm@26849
   485
wenzelm@28760
   486
  \item @{text domintros} enables the automated generation of
wenzelm@26849
   487
  introduction rules for the domain predicate. While mostly not
wenzelm@26849
   488
  needed, they can be helpful in some proofs about partial functions.
wenzelm@26849
   489
wenzelm@28760
   490
  \item @{text tailrec} generates the unconstrained recursive
wenzelm@26849
   491
  equations even without a termination proof, provided that the
wenzelm@26849
   492
  function is tail-recursive. This currently only works
wenzelm@26849
   493
wenzelm@28760
   494
  \item @{text "default d"} allows to specify a default value for a
wenzelm@26849
   495
  (partial) function, which will ensure that @{text "f x = d x"}
wenzelm@26849
   496
  whenever @{text "x \<notin> f_dom"}.
wenzelm@26849
   497
wenzelm@28760
   498
  \end{description}
wenzelm@26849
   499
*}
wenzelm@26849
   500
wenzelm@26849
   501
wenzelm@26849
   502
subsection {* Proof methods related to recursive definitions *}
wenzelm@26849
   503
wenzelm@26849
   504
text {*
wenzelm@26849
   505
  \begin{matharray}{rcl}
wenzelm@28761
   506
    @{method_def (HOL) pat_completeness} & : & @{text method} \\
wenzelm@28761
   507
    @{method_def (HOL) relation} & : & @{text method} \\
wenzelm@28761
   508
    @{method_def (HOL) lexicographic_order} & : & @{text method} \\
krauss@33858
   509
    @{method_def (HOL) size_change} & : & @{text method} \\
wenzelm@26849
   510
  \end{matharray}
wenzelm@26849
   511
wenzelm@26849
   512
  \begin{rail}
wenzelm@26849
   513
    'relation' term
wenzelm@26849
   514
    ;
haftmann@31912
   515
    'lexicographic\_order' ( clasimpmod * )
wenzelm@26849
   516
    ;
krauss@33858
   517
    'size\_change' ( orders ( clasimpmod * ) )
krauss@33858
   518
    ;
krauss@33858
   519
    orders: ( 'max' | 'min' | 'ms' ) *
wenzelm@26849
   520
  \end{rail}
wenzelm@26849
   521
wenzelm@28760
   522
  \begin{description}
wenzelm@26849
   523
wenzelm@28760
   524
  \item @{method (HOL) pat_completeness} is a specialized method to
wenzelm@26849
   525
  solve goals regarding the completeness of pattern matching, as
wenzelm@26849
   526
  required by the @{command (HOL) "function"} package (cf.\
wenzelm@26849
   527
  \cite{isabelle-function}).
wenzelm@26849
   528
wenzelm@28760
   529
  \item @{method (HOL) relation}~@{text R} introduces a termination
wenzelm@26849
   530
  proof using the relation @{text R}.  The resulting proof state will
wenzelm@26849
   531
  contain goals expressing that @{text R} is wellfounded, and that the
wenzelm@26849
   532
  arguments of recursive calls decrease with respect to @{text R}.
wenzelm@26849
   533
  Usually, this method is used as the initial proof step of manual
wenzelm@26849
   534
  termination proofs.
wenzelm@26849
   535
wenzelm@28760
   536
  \item @{method (HOL) "lexicographic_order"} attempts a fully
wenzelm@26849
   537
  automated termination proof by searching for a lexicographic
wenzelm@26849
   538
  combination of size measures on the arguments of the function. The
wenzelm@26849
   539
  method accepts the same arguments as the @{method auto} method,
wenzelm@26849
   540
  which it uses internally to prove local descents.  The same context
wenzelm@26849
   541
  modifiers as for @{method auto} are accepted, see
wenzelm@26849
   542
  \secref{sec:clasimp}.
wenzelm@26849
   543
wenzelm@26849
   544
  In case of failure, extensive information is printed, which can help
wenzelm@26849
   545
  to analyse the situation (cf.\ \cite{isabelle-function}).
wenzelm@26849
   546
krauss@33858
   547
  \item @{method (HOL) "size_change"} also works on termination goals,
krauss@33858
   548
  using a variation of the size-change principle, together with a
krauss@33858
   549
  graph decomposition technique (see \cite{krauss_phd} for details).
krauss@33858
   550
  Three kinds of orders are used internally: @{text max}, @{text min},
krauss@33858
   551
  and @{text ms} (multiset), which is only available when the theory
krauss@33858
   552
  @{text Multiset} is loaded. When no order kinds are given, they are
krauss@33858
   553
  tried in order. The search for a termination proof uses SAT solving
krauss@33858
   554
  internally.
krauss@33858
   555
krauss@33858
   556
 For local descent proofs, the same context modifiers as for @{method
krauss@33858
   557
  auto} are accepted, see \secref{sec:clasimp}.
krauss@33858
   558
wenzelm@28760
   559
  \end{description}
wenzelm@26849
   560
*}
wenzelm@26849
   561
wenzelm@26849
   562
wenzelm@26849
   563
subsection {* Old-style recursive function definitions (TFL) *}
wenzelm@26849
   564
wenzelm@26849
   565
text {*
wenzelm@26849
   566
  The old TFL commands @{command (HOL) "recdef"} and @{command (HOL)
wenzelm@26849
   567
  "recdef_tc"} for defining recursive are mostly obsolete; @{command
wenzelm@26849
   568
  (HOL) "function"} or @{command (HOL) "fun"} should be used instead.
wenzelm@26849
   569
wenzelm@26849
   570
  \begin{matharray}{rcl}
wenzelm@28761
   571
    @{command_def (HOL) "recdef"} & : & @{text "theory \<rightarrow> theory)"} \\
wenzelm@28761
   572
    @{command_def (HOL) "recdef_tc"}@{text "\<^sup>*"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@26849
   573
  \end{matharray}
wenzelm@26849
   574
wenzelm@26849
   575
  \begin{rail}
wenzelm@26849
   576
    'recdef' ('(' 'permissive' ')')? \\ name term (prop +) hints?
wenzelm@26849
   577
    ;
wenzelm@26849
   578
    recdeftc thmdecl? tc
wenzelm@26849
   579
    ;
haftmann@31912
   580
    hints: '(' 'hints' ( recdefmod * ) ')'
wenzelm@26849
   581
    ;
wenzelm@26849
   582
    recdefmod: (('recdef\_simp' | 'recdef\_cong' | 'recdef\_wf') (() | 'add' | 'del') ':' thmrefs) | clasimpmod
wenzelm@26849
   583
    ;
wenzelm@26849
   584
    tc: nameref ('(' nat ')')?
wenzelm@26849
   585
    ;
wenzelm@26849
   586
  \end{rail}
wenzelm@26849
   587
wenzelm@28760
   588
  \begin{description}
wenzelm@26849
   589
  
wenzelm@28760
   590
  \item @{command (HOL) "recdef"} defines general well-founded
wenzelm@26849
   591
  recursive functions (using the TFL package), see also
wenzelm@26849
   592
  \cite{isabelle-HOL}.  The ``@{text "(permissive)"}'' option tells
wenzelm@26849
   593
  TFL to recover from failed proof attempts, returning unfinished
wenzelm@26849
   594
  results.  The @{text recdef_simp}, @{text recdef_cong}, and @{text
wenzelm@26849
   595
  recdef_wf} hints refer to auxiliary rules to be used in the internal
wenzelm@26849
   596
  automated proof process of TFL.  Additional @{syntax clasimpmod}
wenzelm@26849
   597
  declarations (cf.\ \secref{sec:clasimp}) may be given to tune the
wenzelm@26849
   598
  context of the Simplifier (cf.\ \secref{sec:simplifier}) and
wenzelm@26849
   599
  Classical reasoner (cf.\ \secref{sec:classical}).
wenzelm@26849
   600
  
wenzelm@28760
   601
  \item @{command (HOL) "recdef_tc"}~@{text "c (i)"} recommences the
wenzelm@26849
   602
  proof for leftover termination condition number @{text i} (default
wenzelm@26849
   603
  1) as generated by a @{command (HOL) "recdef"} definition of
wenzelm@26849
   604
  constant @{text c}.
wenzelm@26849
   605
  
wenzelm@26849
   606
  Note that in most cases, @{command (HOL) "recdef"} is able to finish
wenzelm@26849
   607
  its internal proofs without manual intervention.
wenzelm@26849
   608
wenzelm@28760
   609
  \end{description}
wenzelm@26849
   610
wenzelm@26849
   611
  \medskip Hints for @{command (HOL) "recdef"} may be also declared
wenzelm@26849
   612
  globally, using the following attributes.
wenzelm@26849
   613
wenzelm@26849
   614
  \begin{matharray}{rcl}
wenzelm@28761
   615
    @{attribute_def (HOL) recdef_simp} & : & @{text attribute} \\
wenzelm@28761
   616
    @{attribute_def (HOL) recdef_cong} & : & @{text attribute} \\
wenzelm@28761
   617
    @{attribute_def (HOL) recdef_wf} & : & @{text attribute} \\
wenzelm@26849
   618
  \end{matharray}
wenzelm@26849
   619
wenzelm@26849
   620
  \begin{rail}
wenzelm@26849
   621
    ('recdef\_simp' | 'recdef\_cong' | 'recdef\_wf') (() | 'add' | 'del')
wenzelm@26849
   622
    ;
wenzelm@26849
   623
  \end{rail}
wenzelm@26849
   624
*}
wenzelm@26849
   625
wenzelm@26849
   626
wenzelm@26849
   627
section {* Inductive and coinductive definitions \label{sec:hol-inductive} *}
wenzelm@26849
   628
wenzelm@26849
   629
text {*
wenzelm@26849
   630
  An \textbf{inductive definition} specifies the least predicate (or
wenzelm@26849
   631
  set) @{text R} closed under given rules: applying a rule to elements
wenzelm@26849
   632
  of @{text R} yields a result within @{text R}.  For example, a
wenzelm@26849
   633
  structural operational semantics is an inductive definition of an
wenzelm@26849
   634
  evaluation relation.
wenzelm@26849
   635
wenzelm@26849
   636
  Dually, a \textbf{coinductive definition} specifies the greatest
wenzelm@26849
   637
  predicate~/ set @{text R} that is consistent with given rules: every
wenzelm@26849
   638
  element of @{text R} can be seen as arising by applying a rule to
wenzelm@26849
   639
  elements of @{text R}.  An important example is using bisimulation
wenzelm@26849
   640
  relations to formalise equivalence of processes and infinite data
wenzelm@26849
   641
  structures.
wenzelm@26849
   642
wenzelm@26849
   643
  \medskip The HOL package is related to the ZF one, which is
wenzelm@26849
   644
  described in a separate paper,\footnote{It appeared in CADE
wenzelm@26849
   645
  \cite{paulson-CADE}; a longer version is distributed with Isabelle.}
wenzelm@26849
   646
  which you should refer to in case of difficulties.  The package is
wenzelm@26849
   647
  simpler than that of ZF thanks to implicit type-checking in HOL.
wenzelm@26849
   648
  The types of the (co)inductive predicates (or sets) determine the
wenzelm@26849
   649
  domain of the fixedpoint definition, and the package does not have
wenzelm@26849
   650
  to use inference rules for type-checking.
wenzelm@26849
   651
wenzelm@26849
   652
  \begin{matharray}{rcl}
wenzelm@28761
   653
    @{command_def (HOL) "inductive"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   654
    @{command_def (HOL) "inductive_set"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   655
    @{command_def (HOL) "coinductive"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   656
    @{command_def (HOL) "coinductive_set"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   657
    @{attribute_def (HOL) mono} & : & @{text attribute} \\
wenzelm@26849
   658
  \end{matharray}
wenzelm@26849
   659
wenzelm@26849
   660
  \begin{rail}
wenzelm@26849
   661
    ('inductive' | 'inductive\_set' | 'coinductive' | 'coinductive\_set') target? fixes ('for' fixes)? \\
wenzelm@26849
   662
    ('where' clauses)? ('monos' thmrefs)?
wenzelm@26849
   663
    ;
wenzelm@26849
   664
    clauses: (thmdecl? prop + '|')
wenzelm@26849
   665
    ;
wenzelm@26849
   666
    'mono' (() | 'add' | 'del')
wenzelm@26849
   667
    ;
wenzelm@26849
   668
  \end{rail}
wenzelm@26849
   669
wenzelm@28760
   670
  \begin{description}
wenzelm@26849
   671
wenzelm@28760
   672
  \item @{command (HOL) "inductive"} and @{command (HOL)
wenzelm@28760
   673
  "coinductive"} define (co)inductive predicates from the
wenzelm@26849
   674
  introduction rules given in the @{keyword "where"} part.  The
wenzelm@26849
   675
  optional @{keyword "for"} part contains a list of parameters of the
wenzelm@26849
   676
  (co)inductive predicates that remain fixed throughout the
wenzelm@26849
   677
  definition.  The optional @{keyword "monos"} section contains
wenzelm@26849
   678
  \emph{monotonicity theorems}, which are required for each operator
wenzelm@26849
   679
  applied to a recursive set in the introduction rules.  There
wenzelm@26849
   680
  \emph{must} be a theorem of the form @{text "A \<le> B \<Longrightarrow> M A \<le> M B"},
wenzelm@26849
   681
  for each premise @{text "M R\<^sub>i t"} in an introduction rule!
wenzelm@26849
   682
wenzelm@28760
   683
  \item @{command (HOL) "inductive_set"} and @{command (HOL)
wenzelm@28760
   684
  "coinductive_set"} are wrappers for to the previous commands,
wenzelm@26849
   685
  allowing the definition of (co)inductive sets.
wenzelm@26849
   686
wenzelm@28760
   687
  \item @{attribute (HOL) mono} declares monotonicity rules.  These
wenzelm@26849
   688
  rule are involved in the automated monotonicity proof of @{command
wenzelm@26849
   689
  (HOL) "inductive"}.
wenzelm@26849
   690
wenzelm@28760
   691
  \end{description}
wenzelm@26849
   692
*}
wenzelm@26849
   693
wenzelm@26849
   694
wenzelm@26849
   695
subsection {* Derived rules *}
wenzelm@26849
   696
wenzelm@26849
   697
text {*
wenzelm@26849
   698
  Each (co)inductive definition @{text R} adds definitions to the
wenzelm@26849
   699
  theory and also proves some theorems:
wenzelm@26849
   700
wenzelm@26849
   701
  \begin{description}
wenzelm@26849
   702
wenzelm@28760
   703
  \item @{text R.intros} is the list of introduction rules as proven
wenzelm@26849
   704
  theorems, for the recursive predicates (or sets).  The rules are
wenzelm@26849
   705
  also available individually, using the names given them in the
wenzelm@26849
   706
  theory file;
wenzelm@26849
   707
wenzelm@28760
   708
  \item @{text R.cases} is the case analysis (or elimination) rule;
wenzelm@26849
   709
wenzelm@28760
   710
  \item @{text R.induct} or @{text R.coinduct} is the (co)induction
wenzelm@26849
   711
  rule.
wenzelm@26849
   712
wenzelm@26849
   713
  \end{description}
wenzelm@26849
   714
wenzelm@26849
   715
  When several predicates @{text "R\<^sub>1, \<dots>, R\<^sub>n"} are
wenzelm@26849
   716
  defined simultaneously, the list of introduction rules is called
wenzelm@26849
   717
  @{text "R\<^sub>1_\<dots>_R\<^sub>n.intros"}, the case analysis rules are
wenzelm@26849
   718
  called @{text "R\<^sub>1.cases, \<dots>, R\<^sub>n.cases"}, and the list
wenzelm@26849
   719
  of mutual induction rules is called @{text
wenzelm@26849
   720
  "R\<^sub>1_\<dots>_R\<^sub>n.inducts"}.
wenzelm@26849
   721
*}
wenzelm@26849
   722
wenzelm@26849
   723
wenzelm@26849
   724
subsection {* Monotonicity theorems *}
wenzelm@26849
   725
wenzelm@26849
   726
text {*
wenzelm@26849
   727
  Each theory contains a default set of theorems that are used in
wenzelm@26849
   728
  monotonicity proofs.  New rules can be added to this set via the
wenzelm@26849
   729
  @{attribute (HOL) mono} attribute.  The HOL theory @{text Inductive}
wenzelm@26849
   730
  shows how this is done.  In general, the following monotonicity
wenzelm@26849
   731
  theorems may be added:
wenzelm@26849
   732
wenzelm@26849
   733
  \begin{itemize}
wenzelm@26849
   734
wenzelm@26849
   735
  \item Theorems of the form @{text "A \<le> B \<Longrightarrow> M A \<le> M B"}, for proving
wenzelm@26849
   736
  monotonicity of inductive definitions whose introduction rules have
wenzelm@26849
   737
  premises involving terms such as @{text "M R\<^sub>i t"}.
wenzelm@26849
   738
wenzelm@26849
   739
  \item Monotonicity theorems for logical operators, which are of the
wenzelm@26849
   740
  general form @{text "(\<dots> \<longrightarrow> \<dots>) \<Longrightarrow> \<dots> (\<dots> \<longrightarrow> \<dots>) \<Longrightarrow> \<dots> \<longrightarrow> \<dots>"}.  For example, in
wenzelm@26849
   741
  the case of the operator @{text "\<or>"}, the corresponding theorem is
wenzelm@26849
   742
  \[
wenzelm@26849
   743
  \infer{@{text "P\<^sub>1 \<or> P\<^sub>2 \<longrightarrow> Q\<^sub>1 \<or> Q\<^sub>2"}}{@{text "P\<^sub>1 \<longrightarrow> Q\<^sub>1"} & @{text "P\<^sub>2 \<longrightarrow> Q\<^sub>2"}}
wenzelm@26849
   744
  \]
wenzelm@26849
   745
wenzelm@26849
   746
  \item De Morgan style equations for reasoning about the ``polarity''
wenzelm@26849
   747
  of expressions, e.g.
wenzelm@26849
   748
  \[
wenzelm@26849
   749
  @{prop "\<not> \<not> P \<longleftrightarrow> P"} \qquad\qquad
wenzelm@26849
   750
  @{prop "\<not> (P \<and> Q) \<longleftrightarrow> \<not> P \<or> \<not> Q"}
wenzelm@26849
   751
  \]
wenzelm@26849
   752
wenzelm@26849
   753
  \item Equations for reducing complex operators to more primitive
wenzelm@26849
   754
  ones whose monotonicity can easily be proved, e.g.
wenzelm@26849
   755
  \[
wenzelm@26849
   756
  @{prop "(P \<longrightarrow> Q) \<longleftrightarrow> \<not> P \<or> Q"} \qquad\qquad
wenzelm@26849
   757
  @{prop "Ball A P \<equiv> \<forall>x. x \<in> A \<longrightarrow> P x"}
wenzelm@26849
   758
  \]
wenzelm@26849
   759
wenzelm@26849
   760
  \end{itemize}
wenzelm@26849
   761
wenzelm@26849
   762
  %FIXME: Example of an inductive definition
wenzelm@26849
   763
*}
wenzelm@26849
   764
wenzelm@26849
   765
wenzelm@26849
   766
section {* Arithmetic proof support *}
wenzelm@26849
   767
wenzelm@26849
   768
text {*
wenzelm@26849
   769
  \begin{matharray}{rcl}
wenzelm@28761
   770
    @{method_def (HOL) arith} & : & @{text method} \\
nipkow@30863
   771
    @{attribute_def (HOL) arith} & : & @{text attribute} \\
wenzelm@28761
   772
    @{attribute_def (HOL) arith_split} & : & @{text attribute} \\
wenzelm@26849
   773
  \end{matharray}
wenzelm@26849
   774
wenzelm@26849
   775
  The @{method (HOL) arith} method decides linear arithmetic problems
wenzelm@26849
   776
  (on types @{text nat}, @{text int}, @{text real}).  Any current
wenzelm@26849
   777
  facts are inserted into the goal before running the procedure.
wenzelm@26849
   778
nipkow@30863
   779
  The @{attribute (HOL) arith} attribute declares facts that are
nipkow@30863
   780
  always supplied to the arithmetic provers implicitly.
wenzelm@26849
   781
nipkow@30863
   782
  The @{attribute (HOL) arith_split} attribute declares case split
wenzelm@30865
   783
  rules to be expanded before @{method (HOL) arith} is invoked.
nipkow@30863
   784
nipkow@30863
   785
  Note that a simpler (but faster) arithmetic prover is
nipkow@30863
   786
  already invoked by the Simplifier.
wenzelm@26849
   787
*}
wenzelm@26849
   788
wenzelm@26849
   789
wenzelm@30169
   790
section {* Intuitionistic proof search *}
wenzelm@30169
   791
wenzelm@30169
   792
text {*
wenzelm@30169
   793
  \begin{matharray}{rcl}
wenzelm@30171
   794
    @{method_def (HOL) iprover} & : & @{text method} \\
wenzelm@30169
   795
  \end{matharray}
wenzelm@30169
   796
wenzelm@30169
   797
  \begin{rail}
haftmann@31912
   798
    'iprover' ('!' ?) ( rulemod * )
wenzelm@30169
   799
    ;
wenzelm@30169
   800
  \end{rail}
wenzelm@30169
   801
wenzelm@30171
   802
  The @{method (HOL) iprover} method performs intuitionistic proof
wenzelm@30171
   803
  search, depending on specifically declared rules from the context,
wenzelm@30171
   804
  or given as explicit arguments.  Chained facts are inserted into the
wenzelm@30171
   805
  goal before commencing proof search; ``@{method (HOL) iprover}@{text
wenzelm@30171
   806
  "!"}''  means to include the current @{fact prems} as well.
wenzelm@30169
   807
  
wenzelm@30169
   808
  Rules need to be classified as @{attribute (Pure) intro},
wenzelm@30169
   809
  @{attribute (Pure) elim}, or @{attribute (Pure) dest}; here the
wenzelm@30169
   810
  ``@{text "!"}'' indicator refers to ``safe'' rules, which may be
wenzelm@30169
   811
  applied aggressively (without considering back-tracking later).
wenzelm@30169
   812
  Rules declared with ``@{text "?"}'' are ignored in proof search (the
wenzelm@30169
   813
  single-step @{method rule} method still observes these).  An
wenzelm@30169
   814
  explicit weight annotation may be given as well; otherwise the
wenzelm@30169
   815
  number of rule premises will be taken into account here.
wenzelm@30169
   816
*}
wenzelm@30169
   817
wenzelm@30169
   818
wenzelm@30171
   819
section {* Coherent Logic *}
wenzelm@30171
   820
wenzelm@30171
   821
text {*
wenzelm@30171
   822
  \begin{matharray}{rcl}
wenzelm@30171
   823
    @{method_def (HOL) "coherent"} & : & @{text method} \\
wenzelm@30171
   824
  \end{matharray}
wenzelm@30171
   825
wenzelm@30171
   826
  \begin{rail}
wenzelm@30171
   827
    'coherent' thmrefs?
wenzelm@30171
   828
    ;
wenzelm@30171
   829
  \end{rail}
wenzelm@30171
   830
wenzelm@30171
   831
  The @{method (HOL) coherent} method solves problems of
wenzelm@30171
   832
  \emph{Coherent Logic} \cite{Bezem-Coquand:2005}, which covers
wenzelm@30171
   833
  applications in confluence theory, lattice theory and projective
wenzelm@30171
   834
  geometry.  See @{"file" "~~/src/HOL/ex/Coherent.thy"} for some
wenzelm@30171
   835
  examples.
wenzelm@30171
   836
*}
wenzelm@30171
   837
wenzelm@30171
   838
haftmann@31912
   839
section {* Checking and refuting propositions *}
haftmann@31912
   840
haftmann@31912
   841
text {*
haftmann@31912
   842
  Identifying incorrect propositions usually involves evaluation of
haftmann@31912
   843
  particular assignments and systematic counter example search.  This
haftmann@31912
   844
  is supported by the following commands.
haftmann@31912
   845
haftmann@31912
   846
  \begin{matharray}{rcl}
haftmann@31912
   847
    @{command_def (HOL) "value"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@31912
   848
    @{command_def (HOL) "quickcheck"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
haftmann@31912
   849
    @{command_def (HOL) "quickcheck_params"} & : & @{text "theory \<rightarrow> theory"}
haftmann@31912
   850
  \end{matharray}
haftmann@31912
   851
haftmann@31912
   852
  \begin{rail}
haftmann@31912
   853
    'value' ( ( '[' name ']' ) ? ) modes? term
haftmann@31912
   854
    ;
haftmann@31912
   855
haftmann@31912
   856
    'quickcheck' ( ( '[' args ']' ) ? ) nat?
haftmann@31912
   857
    ;
haftmann@31912
   858
haftmann@31912
   859
    'quickcheck_params' ( ( '[' args ']' ) ? )
haftmann@31912
   860
    ;
haftmann@31912
   861
haftmann@31912
   862
    modes: '(' (name + ) ')'
haftmann@31912
   863
    ;
haftmann@31912
   864
haftmann@31912
   865
    args: ( name '=' value + ',' )
haftmann@31912
   866
    ;
haftmann@31912
   867
  \end{rail}
haftmann@31912
   868
haftmann@31912
   869
  \begin{description}
haftmann@31912
   870
haftmann@31912
   871
  \item @{command (HOL) "value"}~@{text t} evaluates and prints a
haftmann@31912
   872
    term; optionally @{text modes} can be specified, which are
haftmann@31912
   873
    appended to the current print mode (see also \cite{isabelle-ref}).
haftmann@31912
   874
    Internally, the evaluation is performed by registered evaluators,
haftmann@31912
   875
    which are invoked sequentially until a result is returned.
haftmann@31912
   876
    Alternatively a specific evaluator can be selected using square
haftmann@31912
   877
    brackets; available evaluators include @{text nbe} for
haftmann@31912
   878
    \emph{normalization by evaluation} and \emph{code} for code
haftmann@31912
   879
    generation in SML.
haftmann@31912
   880
haftmann@31912
   881
  \item @{command (HOL) "quickcheck"} tests the current goal for
haftmann@31912
   882
    counter examples using a series of arbitrary assignments for its
haftmann@31912
   883
    free variables; by default the first subgoal is tested, an other
haftmann@31912
   884
    can be selected explicitly using an optional goal index.
haftmann@31912
   885
    A number of configuration options are supported for
haftmann@31912
   886
    @{command (HOL) "quickcheck"}, notably:
haftmann@31912
   887
haftmann@31912
   888
    \begin{description}
haftmann@31912
   889
haftmann@31912
   890
      \item[size] specifies the maximum size of the search space for
haftmann@31912
   891
        assignment values.
haftmann@31912
   892
haftmann@31912
   893
      \item[iterations] sets how many sets of assignments are
haftmann@31912
   894
        generated for each particular size.
haftmann@31912
   895
blanchet@35331
   896
      \item[no\_assms] specifies whether assumptions in
blanchet@35331
   897
        structured proofs should be ignored.
blanchet@35331
   898
haftmann@31912
   899
    \end{description}
haftmann@31912
   900
haftmann@31912
   901
    These option can be given within square brackets.
haftmann@31912
   902
haftmann@31912
   903
  \item @{command (HOL) "quickcheck_params"} changes quickcheck
haftmann@31912
   904
    configuration options persitently.
haftmann@31912
   905
haftmann@31912
   906
  \end{description}
haftmann@31912
   907
*}
haftmann@31912
   908
haftmann@31912
   909
wenzelm@28603
   910
section {* Invoking automated reasoning tools -- The Sledgehammer *}
wenzelm@28603
   911
wenzelm@28603
   912
text {*
wenzelm@28603
   913
  Isabelle/HOL includes a generic \emph{ATP manager} that allows
wenzelm@28603
   914
  external automated reasoning tools to crunch a pending goal.
wenzelm@28603
   915
  Supported provers include E\footnote{\url{http://www.eprover.org}},
wenzelm@28603
   916
  SPASS\footnote{\url{http://www.spass-prover.org/}}, and Vampire.
wenzelm@28603
   917
  There is also a wrapper to invoke provers remotely via the
wenzelm@28603
   918
  SystemOnTPTP\footnote{\url{http://www.cs.miami.edu/~tptp/cgi-bin/SystemOnTPTP}}
wenzelm@28603
   919
  web service.
wenzelm@28603
   920
wenzelm@28603
   921
  The problem passed to external provers consists of the goal together
wenzelm@28603
   922
  with a smart selection of lemmas from the current theory context.
wenzelm@28603
   923
  The result of a successful proof search is some source text that
wenzelm@28603
   924
  usually reconstructs the proof within Isabelle, without requiring
wenzelm@28603
   925
  external provers again.  The Metis
wenzelm@28603
   926
  prover\footnote{\url{http://www.gilith.com/software/metis/}} that is
wenzelm@28603
   927
  integrated into Isabelle/HOL is being used here.
wenzelm@28603
   928
wenzelm@28603
   929
  In this mode of operation, heavy means of automated reasoning are
wenzelm@28603
   930
  used as a strong relevance filter, while the main proof checking
wenzelm@28603
   931
  works via explicit inferences going through the Isabelle kernel.
wenzelm@28603
   932
  Moreover, rechecking Isabelle proof texts with already specified
wenzelm@28603
   933
  auxiliary facts is much faster than performing fully automated
wenzelm@28603
   934
  search over and over again.
wenzelm@28603
   935
wenzelm@28603
   936
  \begin{matharray}{rcl}
wenzelm@28761
   937
    @{command_def (HOL) "sledgehammer"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
wenzelm@28761
   938
    @{command_def (HOL) "print_atps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28761
   939
    @{command_def (HOL) "atp_info"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
wenzelm@28761
   940
    @{command_def (HOL) "atp_kill"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
wenzelm@29112
   941
    @{command_def (HOL) "atp_messages"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
wenzelm@28761
   942
    @{method_def (HOL) metis} & : & @{text method} \\
wenzelm@28603
   943
  \end{matharray}
wenzelm@28603
   944
wenzelm@28603
   945
  \begin{rail}
haftmann@31912
   946
  'sledgehammer' ( nameref * )
wenzelm@28603
   947
  ;
wenzelm@29112
   948
  'atp\_messages' ('(' nat ')')?
wenzelm@29114
   949
  ;
wenzelm@28603
   950
wenzelm@28603
   951
  'metis' thmrefs
wenzelm@28603
   952
  ;
wenzelm@28603
   953
  \end{rail}
wenzelm@28603
   954
wenzelm@28760
   955
  \begin{description}
wenzelm@28603
   956
wenzelm@28760
   957
  \item @{command (HOL) sledgehammer}~@{text "prover\<^sub>1 \<dots> prover\<^sub>n"}
wenzelm@28760
   958
  invokes the specified automated theorem provers on the first
wenzelm@28760
   959
  subgoal.  Provers are run in parallel, the first successful result
wenzelm@28760
   960
  is displayed, and the other attempts are terminated.
wenzelm@28603
   961
wenzelm@28603
   962
  Provers are defined in the theory context, see also @{command (HOL)
wenzelm@28603
   963
  print_atps}.  If no provers are given as arguments to @{command
wenzelm@28603
   964
  (HOL) sledgehammer}, the system refers to the default defined as
wenzelm@28603
   965
  ``ATP provers'' preference by the user interface.
wenzelm@28603
   966
wenzelm@28603
   967
  There are additional preferences for timeout (default: 60 seconds),
wenzelm@28603
   968
  and the maximum number of independent prover processes (default: 5);
wenzelm@28603
   969
  excessive provers are automatically terminated.
wenzelm@28603
   970
wenzelm@28760
   971
  \item @{command (HOL) print_atps} prints the list of automated
wenzelm@28603
   972
  theorem provers available to the @{command (HOL) sledgehammer}
wenzelm@28603
   973
  command.
wenzelm@28603
   974
wenzelm@28760
   975
  \item @{command (HOL) atp_info} prints information about presently
wenzelm@28603
   976
  running provers, including elapsed runtime, and the remaining time
wenzelm@28603
   977
  until timeout.
wenzelm@28603
   978
wenzelm@28760
   979
  \item @{command (HOL) atp_kill} terminates all presently running
wenzelm@28603
   980
  provers.
wenzelm@28603
   981
wenzelm@29112
   982
  \item @{command (HOL) atp_messages} displays recent messages issued
wenzelm@29112
   983
  by automated theorem provers.  This allows to examine results that
wenzelm@29112
   984
  might have got lost due to the asynchronous nature of default
wenzelm@29112
   985
  @{command (HOL) sledgehammer} output.  An optional message limit may
wenzelm@29112
   986
  be specified (default 5).
wenzelm@29112
   987
wenzelm@28760
   988
  \item @{method (HOL) metis}~@{text "facts"} invokes the Metis prover
wenzelm@28760
   989
  with the given facts.  Metis is an automated proof tool of medium
wenzelm@28760
   990
  strength, but is fully integrated into Isabelle/HOL, with explicit
wenzelm@28760
   991
  inferences going through the kernel.  Thus its results are
wenzelm@28603
   992
  guaranteed to be ``correct by construction''.
wenzelm@28603
   993
wenzelm@28603
   994
  Note that all facts used with Metis need to be specified as explicit
wenzelm@28603
   995
  arguments.  There are no rule declarations as for other Isabelle
wenzelm@28603
   996
  provers, like @{method blast} or @{method fast}.
wenzelm@28603
   997
wenzelm@28760
   998
  \end{description}
wenzelm@28603
   999
*}
wenzelm@28603
  1000
wenzelm@28603
  1001
wenzelm@28752
  1002
section {* Unstructured case analysis and induction \label{sec:hol-induct-tac} *}
wenzelm@26849
  1003
wenzelm@26849
  1004
text {*
wenzelm@27123
  1005
  The following tools of Isabelle/HOL support cases analysis and
wenzelm@27123
  1006
  induction in unstructured tactic scripts; see also
wenzelm@27123
  1007
  \secref{sec:cases-induct} for proper Isar versions of similar ideas.
wenzelm@26849
  1008
wenzelm@26849
  1009
  \begin{matharray}{rcl}
wenzelm@28761
  1010
    @{method_def (HOL) case_tac}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  1011
    @{method_def (HOL) induct_tac}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  1012
    @{method_def (HOL) ind_cases}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  1013
    @{command_def (HOL) "inductive_cases"}@{text "\<^sup>*"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@26849
  1014
  \end{matharray}
wenzelm@26849
  1015
wenzelm@26849
  1016
  \begin{rail}
wenzelm@26849
  1017
    'case\_tac' goalspec? term rule?
wenzelm@26849
  1018
    ;
wenzelm@26849
  1019
    'induct\_tac' goalspec? (insts * 'and') rule?
wenzelm@26849
  1020
    ;
wenzelm@26849
  1021
    'ind\_cases' (prop +) ('for' (name +)) ?
wenzelm@26849
  1022
    ;
wenzelm@26849
  1023
    'inductive\_cases' (thmdecl? (prop +) + 'and')
wenzelm@26849
  1024
    ;
wenzelm@26849
  1025
wenzelm@26849
  1026
    rule: ('rule' ':' thmref)
wenzelm@26849
  1027
    ;
wenzelm@26849
  1028
  \end{rail}
wenzelm@26849
  1029
wenzelm@28760
  1030
  \begin{description}
wenzelm@26849
  1031
wenzelm@28760
  1032
  \item @{method (HOL) case_tac} and @{method (HOL) induct_tac} admit
wenzelm@28760
  1033
  to reason about inductive types.  Rules are selected according to
wenzelm@28760
  1034
  the declarations by the @{attribute cases} and @{attribute induct}
wenzelm@28760
  1035
  attributes, cf.\ \secref{sec:cases-induct}.  The @{command (HOL)
wenzelm@28760
  1036
  datatype} package already takes care of this.
wenzelm@27123
  1037
wenzelm@27123
  1038
  These unstructured tactics feature both goal addressing and dynamic
wenzelm@26849
  1039
  instantiation.  Note that named rule cases are \emph{not} provided
wenzelm@27123
  1040
  as would be by the proper @{method cases} and @{method induct} proof
wenzelm@27123
  1041
  methods (see \secref{sec:cases-induct}).  Unlike the @{method
wenzelm@27123
  1042
  induct} method, @{method induct_tac} does not handle structured rule
wenzelm@27123
  1043
  statements, only the compact object-logic conclusion of the subgoal
wenzelm@27123
  1044
  being addressed.
wenzelm@26849
  1045
  
wenzelm@28760
  1046
  \item @{method (HOL) ind_cases} and @{command (HOL)
wenzelm@28760
  1047
  "inductive_cases"} provide an interface to the internal @{ML_text
wenzelm@26860
  1048
  mk_cases} operation.  Rules are simplified in an unrestricted
wenzelm@26860
  1049
  forward manner.
wenzelm@26849
  1050
wenzelm@26849
  1051
  While @{method (HOL) ind_cases} is a proof method to apply the
wenzelm@26849
  1052
  result immediately as elimination rules, @{command (HOL)
wenzelm@26849
  1053
  "inductive_cases"} provides case split theorems at the theory level
wenzelm@26849
  1054
  for later use.  The @{keyword "for"} argument of the @{method (HOL)
wenzelm@26849
  1055
  ind_cases} method allows to specify a list of variables that should
wenzelm@26849
  1056
  be generalized before applying the resulting rule.
wenzelm@26849
  1057
wenzelm@28760
  1058
  \end{description}
wenzelm@26849
  1059
*}
wenzelm@26849
  1060
wenzelm@26849
  1061
wenzelm@26849
  1062
section {* Executable code *}
wenzelm@26849
  1063
wenzelm@26849
  1064
text {*
wenzelm@26849
  1065
  Isabelle/Pure provides two generic frameworks to support code
wenzelm@26849
  1066
  generation from executable specifications.  Isabelle/HOL
wenzelm@26849
  1067
  instantiates these mechanisms in a way that is amenable to end-user
wenzelm@26849
  1068
  applications.
wenzelm@26849
  1069
wenzelm@26849
  1070
  One framework generates code from both functional and relational
wenzelm@26849
  1071
  programs to SML.  See \cite{isabelle-HOL} for further information
wenzelm@26849
  1072
  (this actually covers the new-style theory format as well).
wenzelm@26849
  1073
wenzelm@26849
  1074
  \begin{matharray}{rcl}
wenzelm@28761
  1075
    @{command_def (HOL) "code_module"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1076
    @{command_def (HOL) "code_library"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1077
    @{command_def (HOL) "consts_code"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1078
    @{command_def (HOL) "types_code"} & : & @{text "theory \<rightarrow> theory"} \\  
wenzelm@28761
  1079
    @{attribute_def (HOL) code} & : & @{text attribute} \\
wenzelm@26849
  1080
  \end{matharray}
wenzelm@26849
  1081
wenzelm@26849
  1082
  \begin{rail}
wenzelm@26849
  1083
  ( 'code\_module' | 'code\_library' ) modespec ? name ? \\
wenzelm@26849
  1084
    ( 'file' name ) ? ( 'imports' ( name + ) ) ? \\
wenzelm@26849
  1085
    'contains' ( ( name '=' term ) + | term + )
wenzelm@26849
  1086
  ;
wenzelm@26849
  1087
wenzelm@26849
  1088
  modespec: '(' ( name * ) ')'
wenzelm@26849
  1089
  ;
wenzelm@26849
  1090
wenzelm@26849
  1091
  'consts\_code' (codespec +)
wenzelm@26849
  1092
  ;
wenzelm@26849
  1093
wenzelm@26849
  1094
  codespec: const template attachment ?
wenzelm@26849
  1095
  ;
wenzelm@26849
  1096
wenzelm@26849
  1097
  'types\_code' (tycodespec +)
wenzelm@26849
  1098
  ;
wenzelm@26849
  1099
wenzelm@26849
  1100
  tycodespec: name template attachment ?
wenzelm@26849
  1101
  ;
wenzelm@26849
  1102
wenzelm@26849
  1103
  const: term
wenzelm@26849
  1104
  ;
wenzelm@26849
  1105
wenzelm@26849
  1106
  template: '(' string ')'
wenzelm@26849
  1107
  ;
wenzelm@26849
  1108
wenzelm@26849
  1109
  attachment: 'attach' modespec ? verblbrace text verbrbrace
wenzelm@26849
  1110
  ;
wenzelm@26849
  1111
wenzelm@26849
  1112
  'code' (name)?
wenzelm@26849
  1113
  ;
wenzelm@26849
  1114
  \end{rail}
wenzelm@26849
  1115
wenzelm@26849
  1116
  \medskip The other framework generates code from functional programs
wenzelm@26849
  1117
  (including overloading using type classes) to SML \cite{SML}, OCaml
wenzelm@26849
  1118
  \cite{OCaml} and Haskell \cite{haskell-revised-report}.
wenzelm@26849
  1119
  Conceptually, code generation is split up in three steps:
wenzelm@26849
  1120
  \emph{selection} of code theorems, \emph{translation} into an
wenzelm@26849
  1121
  abstract executable view and \emph{serialization} to a specific
wenzelm@26849
  1122
  \emph{target language}.  See \cite{isabelle-codegen} for an
wenzelm@26849
  1123
  introduction on how to use it.
wenzelm@26849
  1124
wenzelm@26849
  1125
  \begin{matharray}{rcl}
wenzelm@28761
  1126
    @{command_def (HOL) "export_code"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28761
  1127
    @{command_def (HOL) "code_thms"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28761
  1128
    @{command_def (HOL) "code_deps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28761
  1129
    @{command_def (HOL) "code_datatype"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1130
    @{command_def (HOL) "code_const"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1131
    @{command_def (HOL) "code_type"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1132
    @{command_def (HOL) "code_class"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1133
    @{command_def (HOL) "code_instance"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1134
    @{command_def (HOL) "code_monad"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1135
    @{command_def (HOL) "code_reserved"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1136
    @{command_def (HOL) "code_include"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1137
    @{command_def (HOL) "code_modulename"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1138
    @{command_def (HOL) "code_abort"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28761
  1139
    @{command_def (HOL) "print_codesetup"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@31254
  1140
    @{command_def (HOL) "print_codeproc"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28761
  1141
    @{attribute_def (HOL) code} & : & @{text attribute} \\
wenzelm@26849
  1142
  \end{matharray}
wenzelm@26849
  1143
wenzelm@26849
  1144
  \begin{rail}
haftmann@34172
  1145
    'export\_code' ( constexpr + ) \\
wenzelm@26849
  1146
      ( ( 'in' target ( 'module\_name' string ) ? \\
wenzelm@26849
  1147
        ( 'file' ( string | '-' ) ) ? ( '(' args ')' ) ?) + ) ?
wenzelm@26849
  1148
    ;
wenzelm@26849
  1149
wenzelm@26849
  1150
    'code\_thms' ( constexpr + ) ?
wenzelm@26849
  1151
    ;
wenzelm@26849
  1152
wenzelm@26849
  1153
    'code\_deps' ( constexpr + ) ?
wenzelm@26849
  1154
    ;
wenzelm@26849
  1155
wenzelm@26849
  1156
    const: term
wenzelm@26849
  1157
    ;
wenzelm@26849
  1158
wenzelm@26849
  1159
    constexpr: ( const | 'name.*' | '*' )
wenzelm@26849
  1160
    ;
wenzelm@26849
  1161
wenzelm@26849
  1162
    typeconstructor: nameref
wenzelm@26849
  1163
    ;
wenzelm@26849
  1164
wenzelm@26849
  1165
    class: nameref
wenzelm@26849
  1166
    ;
wenzelm@26849
  1167
wenzelm@26849
  1168
    target: 'OCaml' | 'SML' | 'Haskell'
wenzelm@26849
  1169
    ;
wenzelm@26849
  1170
wenzelm@26849
  1171
    'code\_datatype' const +
wenzelm@26849
  1172
    ;
wenzelm@26849
  1173
wenzelm@26849
  1174
    'code\_const' (const + 'and') \\
wenzelm@26849
  1175
      ( ( '(' target ( syntax ? + 'and' ) ')' ) + )
wenzelm@26849
  1176
    ;
wenzelm@26849
  1177
wenzelm@26849
  1178
    'code\_type' (typeconstructor + 'and') \\
wenzelm@26849
  1179
      ( ( '(' target ( syntax ? + 'and' ) ')' ) + )
wenzelm@26849
  1180
    ;
wenzelm@26849
  1181
wenzelm@26849
  1182
    'code\_class' (class + 'and') \\
haftmann@28687
  1183
      ( ( '(' target \\ ( string ? + 'and' ) ')' ) + )
wenzelm@26849
  1184
    ;
wenzelm@26849
  1185
wenzelm@26849
  1186
    'code\_instance' (( typeconstructor '::' class ) + 'and') \\
wenzelm@26849
  1187
      ( ( '(' target ( '-' ? + 'and' ) ')' ) + )
wenzelm@26849
  1188
    ;
wenzelm@26849
  1189
wenzelm@26849
  1190
    'code\_monad' const const target
wenzelm@26849
  1191
    ;
wenzelm@26849
  1192
wenzelm@26849
  1193
    'code\_reserved' target ( string + )
wenzelm@26849
  1194
    ;
wenzelm@26849
  1195
wenzelm@26849
  1196
    'code\_include' target ( string ( string | '-') )
wenzelm@26849
  1197
    ;
wenzelm@26849
  1198
wenzelm@26849
  1199
    'code\_modulename' target ( ( string string ) + )
wenzelm@26849
  1200
    ;
wenzelm@26849
  1201
haftmann@27452
  1202
    'code\_abort' ( const + )
wenzelm@26849
  1203
    ;
wenzelm@26849
  1204
wenzelm@26849
  1205
    syntax: string | ( 'infix' | 'infixl' | 'infixr' ) nat string
wenzelm@26849
  1206
    ;
wenzelm@26849
  1207
haftmann@31998
  1208
    'code' ( 'del' ) ?
haftmann@31998
  1209
    ;
haftmann@31998
  1210
haftmann@31998
  1211
    'code_unfold' ( 'del' ) ?
haftmann@31998
  1212
    ;
haftmann@31998
  1213
haftmann@31998
  1214
    'code_post' ( 'del' ) ?
wenzelm@26849
  1215
    ;
wenzelm@26849
  1216
  \end{rail}
wenzelm@26849
  1217
wenzelm@28760
  1218
  \begin{description}
wenzelm@26849
  1219
wenzelm@28760
  1220
  \item @{command (HOL) "export_code"} is the canonical interface for
wenzelm@28760
  1221
  generating and serializing code: for a given list of constants, code
haftmann@34172
  1222
  is generated for the specified target languages.  If no serialization
haftmann@34172
  1223
  instruction is given, only abstract code is generated internally.
wenzelm@26849
  1224
wenzelm@26849
  1225
  Constants may be specified by giving them literally, referring to
wenzelm@26849
  1226
  all executable contants within a certain theory by giving @{text
wenzelm@26849
  1227
  "name.*"}, or referring to \emph{all} executable constants currently
wenzelm@26849
  1228
  available by giving @{text "*"}.
wenzelm@26849
  1229
wenzelm@26849
  1230
  By default, for each involved theory one corresponding name space
wenzelm@26849
  1231
  module is generated.  Alternativly, a module name may be specified
wenzelm@26849
  1232
  after the @{keyword "module_name"} keyword; then \emph{all} code is
wenzelm@26849
  1233
  placed in this module.
wenzelm@26849
  1234
wenzelm@26849
  1235
  For \emph{SML} and \emph{OCaml}, the file specification refers to a
wenzelm@26849
  1236
  single file; for \emph{Haskell}, it refers to a whole directory,
wenzelm@26849
  1237
  where code is generated in multiple files reflecting the module
wenzelm@26849
  1238
  hierarchy.  The file specification ``@{text "-"}'' denotes standard
wenzelm@26849
  1239
  output.  For \emph{SML}, omitting the file specification compiles
wenzelm@26849
  1240
  code internally in the context of the current ML session.
wenzelm@26849
  1241
wenzelm@26849
  1242
  Serializers take an optional list of arguments in parentheses.  For
haftmann@34172
  1243
  \emph{SML} and \emph{OCaml}, ``@{text no_signatures}`` omits
haftmann@34172
  1244
  explicit module signatures.
haftmann@34172
  1245
  
haftmann@34172
  1246
  For \emph{Haskell} a module name prefix may be given using the ``@{text
wenzelm@26849
  1247
  "root:"}'' argument; ``@{text string_classes}'' adds a ``@{verbatim
wenzelm@26849
  1248
  "deriving (Read, Show)"}'' clause to each appropriate datatype
wenzelm@26849
  1249
  declaration.
wenzelm@26849
  1250
wenzelm@28760
  1251
  \item @{command (HOL) "code_thms"} prints a list of theorems
wenzelm@26849
  1252
  representing the corresponding program containing all given
haftmann@34172
  1253
  constants.
wenzelm@26849
  1254
wenzelm@28760
  1255
  \item @{command (HOL) "code_deps"} visualizes dependencies of
wenzelm@26849
  1256
  theorems representing the corresponding program containing all given
haftmann@34172
  1257
  constants.
wenzelm@26849
  1258
wenzelm@28760
  1259
  \item @{command (HOL) "code_datatype"} specifies a constructor set
wenzelm@26849
  1260
  for a logical type.
wenzelm@26849
  1261
wenzelm@28760
  1262
  \item @{command (HOL) "code_const"} associates a list of constants
wenzelm@26849
  1263
  with target-specific serializations; omitting a serialization
wenzelm@26849
  1264
  deletes an existing serialization.
wenzelm@26849
  1265
wenzelm@28760
  1266
  \item @{command (HOL) "code_type"} associates a list of type
wenzelm@26849
  1267
  constructors with target-specific serializations; omitting a
wenzelm@26849
  1268
  serialization deletes an existing serialization.
wenzelm@26849
  1269
wenzelm@28760
  1270
  \item @{command (HOL) "code_class"} associates a list of classes
wenzelm@28760
  1271
  with target-specific class names; omitting a serialization deletes
wenzelm@28760
  1272
  an existing serialization.  This applies only to \emph{Haskell}.
wenzelm@26849
  1273
wenzelm@28760
  1274
  \item @{command (HOL) "code_instance"} declares a list of type
wenzelm@26849
  1275
  constructor / class instance relations as ``already present'' for a
wenzelm@26849
  1276
  given target.  Omitting a ``@{text "-"}'' deletes an existing
wenzelm@26849
  1277
  ``already present'' declaration.  This applies only to
wenzelm@26849
  1278
  \emph{Haskell}.
wenzelm@26849
  1279
wenzelm@28760
  1280
  \item @{command (HOL) "code_monad"} provides an auxiliary mechanism
wenzelm@28760
  1281
  to generate monadic code for Haskell.
wenzelm@26849
  1282
wenzelm@28760
  1283
  \item @{command (HOL) "code_reserved"} declares a list of names as
wenzelm@26849
  1284
  reserved for a given target, preventing it to be shadowed by any
wenzelm@26849
  1285
  generated code.
wenzelm@26849
  1286
wenzelm@28760
  1287
  \item @{command (HOL) "code_include"} adds arbitrary named content
haftmann@27706
  1288
  (``include'') to generated code.  A ``@{text "-"}'' as last argument
wenzelm@26849
  1289
  will remove an already added ``include''.
wenzelm@26849
  1290
wenzelm@28760
  1291
  \item @{command (HOL) "code_modulename"} declares aliasings from one
wenzelm@28760
  1292
  module name onto another.
wenzelm@26849
  1293
wenzelm@28760
  1294
  \item @{command (HOL) "code_abort"} declares constants which are not
haftmann@29560
  1295
  required to have a definition by means of code equations; if
wenzelm@28760
  1296
  needed these are implemented by program abort instead.
wenzelm@26849
  1297
wenzelm@28760
  1298
  \item @{attribute (HOL) code} explicitly selects (or with option
haftmann@29560
  1299
  ``@{text "del"}'' deselects) a code equation for code
haftmann@29560
  1300
  generation.  Usually packages introducing code equations provide
wenzelm@28760
  1301
  a reasonable default setup for selection.
wenzelm@26849
  1302
haftmann@31998
  1303
  \item @{attribute (HOL) code_inline} declares (or with
haftmann@28562
  1304
  option ``@{text "del"}'' removes) inlining theorems which are
haftmann@29560
  1305
  applied as rewrite rules to any code equation during
wenzelm@26849
  1306
  preprocessing.
wenzelm@26849
  1307
haftmann@31998
  1308
  \item @{attribute (HOL) code_post} declares (or with
haftmann@31998
  1309
  option ``@{text "del"}'' removes) theorems which are
haftmann@31998
  1310
  applied as rewrite rules to any result of an evaluation.
haftmann@31998
  1311
wenzelm@28760
  1312
  \item @{command (HOL) "print_codesetup"} gives an overview on
haftmann@31254
  1313
  selected code equations and code generator datatypes.
haftmann@31254
  1314
haftmann@31254
  1315
  \item @{command (HOL) "print_codeproc"} prints the setup
haftmann@31254
  1316
  of the code generator preprocessor.
wenzelm@26849
  1317
wenzelm@28760
  1318
  \end{description}
wenzelm@26849
  1319
*}
wenzelm@26849
  1320
wenzelm@27045
  1321
wenzelm@27045
  1322
section {* Definition by specification \label{sec:hol-specification} *}
wenzelm@27045
  1323
wenzelm@27045
  1324
text {*
wenzelm@27045
  1325
  \begin{matharray}{rcl}
wenzelm@28761
  1326
    @{command_def (HOL) "specification"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@28761
  1327
    @{command_def (HOL) "ax_specification"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@27045
  1328
  \end{matharray}
wenzelm@27045
  1329
wenzelm@27045
  1330
  \begin{rail}
wenzelm@27045
  1331
  ('specification' | 'ax\_specification') '(' (decl +) ')' \\ (thmdecl? prop +)
wenzelm@27045
  1332
  ;
wenzelm@27045
  1333
  decl: ((name ':')? term '(' 'overloaded' ')'?)
wenzelm@27045
  1334
  \end{rail}
wenzelm@27045
  1335
wenzelm@28760
  1336
  \begin{description}
wenzelm@27045
  1337
wenzelm@28760
  1338
  \item @{command (HOL) "specification"}~@{text "decls \<phi>"} sets up a
wenzelm@27045
  1339
  goal stating the existence of terms with the properties specified to
wenzelm@27045
  1340
  hold for the constants given in @{text decls}.  After finishing the
wenzelm@27045
  1341
  proof, the theory will be augmented with definitions for the given
wenzelm@27045
  1342
  constants, as well as with theorems stating the properties for these
wenzelm@27045
  1343
  constants.
wenzelm@27045
  1344
wenzelm@28760
  1345
  \item @{command (HOL) "ax_specification"}~@{text "decls \<phi>"} sets up
wenzelm@28760
  1346
  a goal stating the existence of terms with the properties specified
wenzelm@28760
  1347
  to hold for the constants given in @{text decls}.  After finishing
wenzelm@28760
  1348
  the proof, the theory will be augmented with axioms expressing the
wenzelm@28760
  1349
  properties given in the first place.
wenzelm@27045
  1350
wenzelm@28760
  1351
  \item @{text decl} declares a constant to be defined by the
wenzelm@27045
  1352
  specification given.  The definition for the constant @{text c} is
wenzelm@27045
  1353
  bound to the name @{text c_def} unless a theorem name is given in
wenzelm@27045
  1354
  the declaration.  Overloaded constants should be declared as such.
wenzelm@27045
  1355
wenzelm@28760
  1356
  \end{description}
wenzelm@27045
  1357
wenzelm@27045
  1358
  Whether to use @{command (HOL) "specification"} or @{command (HOL)
wenzelm@27045
  1359
  "ax_specification"} is to some extent a matter of style.  @{command
wenzelm@27045
  1360
  (HOL) "specification"} introduces no new axioms, and so by
wenzelm@27045
  1361
  construction cannot introduce inconsistencies, whereas @{command
wenzelm@27045
  1362
  (HOL) "ax_specification"} does introduce axioms, but only after the
wenzelm@27045
  1363
  user has explicitly proven it to be safe.  A practical issue must be
wenzelm@27045
  1364
  considered, though: After introducing two constants with the same
wenzelm@27045
  1365
  properties using @{command (HOL) "specification"}, one can prove
wenzelm@27045
  1366
  that the two constants are, in fact, equal.  If this might be a
wenzelm@27045
  1367
  problem, one should use @{command (HOL) "ax_specification"}.
wenzelm@27045
  1368
*}
wenzelm@27045
  1369
wenzelm@26840
  1370
end