doc-src/IsarRef/Thy/HOL_Specific.thy
author blanchet
Fri, 27 May 2011 10:41:09 +0200
changeset 43881 665623e695ea
parent 43860 619f16bf2150
child 44141 bc72c1ccc89e
permissions -rw-r--r--
document new "try"
wenzelm@26840
     1
theory HOL_Specific
wenzelm@43522
     2
imports Base Main
wenzelm@26840
     3
begin
wenzelm@26840
     4
wenzelm@26852
     5
chapter {* Isabelle/HOL \label{ch:hol} *}
wenzelm@26849
     6
wenzelm@35757
     7
section {* Typedef axiomatization \label{sec:hol-typedef} *}
wenzelm@26849
     8
wenzelm@26849
     9
text {*
wenzelm@26849
    10
  \begin{matharray}{rcl}
wenzelm@35757
    11
    @{command_def (HOL) "typedef"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
wenzelm@26849
    12
  \end{matharray}
wenzelm@26849
    13
wenzelm@43467
    14
  @{rail "
wenzelm@43467
    15
    @@{command (HOL) typedef} altname? abstype '=' repset
wenzelm@26849
    16
    ;
wenzelm@26849
    17
wenzelm@43467
    18
    altname: '(' (@{syntax name} | @'open' | @'open' @{syntax name}) ')'
wenzelm@26849
    19
    ;
wenzelm@43576
    20
    abstype: @{syntax typespec_sorts} @{syntax mixfix}?
wenzelm@26849
    21
    ;
wenzelm@43467
    22
    repset: @{syntax term} (@'morphisms' @{syntax name} @{syntax name})?
wenzelm@43467
    23
  "}
wenzelm@26849
    24
wenzelm@28760
    25
  \begin{description}
wenzelm@42994
    26
wenzelm@35757
    27
  \item @{command (HOL) "typedef"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t = A"}
wenzelm@35757
    28
  axiomatizes a Gordon/HOL-style type definition in the background
wenzelm@35757
    29
  theory of the current context, depending on a non-emptiness result
wenzelm@35757
    30
  of the set @{text A} (which needs to be proven interactively).
wenzelm@35757
    31
wenzelm@35757
    32
  The raw type may not depend on parameters or assumptions of the
wenzelm@35757
    33
  context --- this is logically impossible in Isabelle/HOL --- but the
wenzelm@35757
    34
  non-emptiness property can be local, potentially resulting in
wenzelm@35757
    35
  multiple interpretations in target contexts.  Thus the established
wenzelm@35757
    36
  bijection between the representing set @{text A} and the new type
wenzelm@35757
    37
  @{text t} may semantically depend on local assumptions.
wenzelm@42994
    38
wenzelm@35757
    39
  By default, @{command (HOL) "typedef"} defines both a type @{text t}
wenzelm@35757
    40
  and a set (term constant) of the same name, unless an alternative
wenzelm@35757
    41
  base name is given in parentheses, or the ``@{text "(open)"}''
wenzelm@35757
    42
  declaration is used to suppress a separate constant definition
wenzelm@35757
    43
  altogether.  The injection from type to set is called @{text Rep_t},
wenzelm@35757
    44
  its inverse @{text Abs_t} --- this may be changed via an explicit
wenzelm@35757
    45
  @{keyword (HOL) "morphisms"} declaration.
wenzelm@42994
    46
wenzelm@26849
    47
  Theorems @{text Rep_t}, @{text Rep_t_inverse}, and @{text
wenzelm@26849
    48
  Abs_t_inverse} provide the most basic characterization as a
wenzelm@26849
    49
  corresponding injection/surjection pair (in both directions).  Rules
wenzelm@26849
    50
  @{text Rep_t_inject} and @{text Abs_t_inject} provide a slightly
wenzelm@26849
    51
  more convenient view on the injectivity part, suitable for automated
wenzelm@26894
    52
  proof tools (e.g.\ in @{attribute simp} or @{attribute iff}
wenzelm@26894
    53
  declarations).  Rules @{text Rep_t_cases}/@{text Rep_t_induct}, and
wenzelm@26894
    54
  @{text Abs_t_cases}/@{text Abs_t_induct} provide alternative views
wenzelm@26894
    55
  on surjectivity; these are already declared as set or type rules for
wenzelm@26849
    56
  the generic @{method cases} and @{method induct} methods.
wenzelm@42994
    57
wenzelm@35757
    58
  An alternative name for the set definition (and other derived
wenzelm@35757
    59
  entities) may be specified in parentheses; the default is to use
wenzelm@35757
    60
  @{text t} as indicated before.
wenzelm@26849
    61
wenzelm@28760
    62
  \end{description}
wenzelm@26849
    63
*}
wenzelm@26849
    64
wenzelm@26849
    65
wenzelm@26849
    66
section {* Adhoc tuples *}
wenzelm@26849
    67
wenzelm@26849
    68
text {*
wenzelm@26849
    69
  \begin{matharray}{rcl}
wenzelm@43467
    70
    @{attribute_def (HOL) split_format}@{text "\<^sup>*"} & : & @{text attribute} \\
wenzelm@26849
    71
  \end{matharray}
wenzelm@26849
    72
wenzelm@43467
    73
  @{rail "
wenzelm@43467
    74
    @@{attribute (HOL) split_format} ('(' 'complete' ')')?
wenzelm@43467
    75
  "}
wenzelm@26849
    76
wenzelm@28760
    77
  \begin{description}
wenzelm@42994
    78
krauss@40634
    79
  \item @{attribute (HOL) split_format}\ @{text "(complete)"} causes
wenzelm@28760
    80
  arguments in function applications to be represented canonically
wenzelm@28760
    81
  according to their tuple type structure.
wenzelm@26849
    82
krauss@40634
    83
  Note that this operation tends to invent funny names for new local
krauss@40634
    84
  parameters introduced.
wenzelm@26849
    85
wenzelm@28760
    86
  \end{description}
wenzelm@26849
    87
*}
wenzelm@26849
    88
wenzelm@26849
    89
wenzelm@26849
    90
section {* Records \label{sec:hol-record} *}
wenzelm@26849
    91
wenzelm@26849
    92
text {*
wenzelm@26849
    93
  In principle, records merely generalize the concept of tuples, where
wenzelm@26849
    94
  components may be addressed by labels instead of just position.  The
wenzelm@26849
    95
  logical infrastructure of records in Isabelle/HOL is slightly more
wenzelm@26849
    96
  advanced, though, supporting truly extensible record schemes.  This
wenzelm@26849
    97
  admits operations that are polymorphic with respect to record
wenzelm@26849
    98
  extension, yielding ``object-oriented'' effects like (single)
wenzelm@26849
    99
  inheritance.  See also \cite{NaraschewskiW-TPHOLs98} for more
wenzelm@26849
   100
  details on object-oriented verification and record subtyping in HOL.
wenzelm@26849
   101
*}
wenzelm@26849
   102
wenzelm@26849
   103
wenzelm@26849
   104
subsection {* Basic concepts *}
wenzelm@26849
   105
wenzelm@26849
   106
text {*
wenzelm@26849
   107
  Isabelle/HOL supports both \emph{fixed} and \emph{schematic} records
wenzelm@26849
   108
  at the level of terms and types.  The notation is as follows:
wenzelm@26849
   109
wenzelm@26849
   110
  \begin{center}
wenzelm@26849
   111
  \begin{tabular}{l|l|l}
wenzelm@26849
   112
    & record terms & record types \\ \hline
wenzelm@26849
   113
    fixed & @{text "\<lparr>x = a, y = b\<rparr>"} & @{text "\<lparr>x :: A, y :: B\<rparr>"} \\
wenzelm@26849
   114
    schematic & @{text "\<lparr>x = a, y = b, \<dots> = m\<rparr>"} &
wenzelm@26849
   115
      @{text "\<lparr>x :: A, y :: B, \<dots> :: M\<rparr>"} \\
wenzelm@26849
   116
  \end{tabular}
wenzelm@26849
   117
  \end{center}
wenzelm@26849
   118
wenzelm@26849
   119
  \noindent The ASCII representation of @{text "\<lparr>x = a\<rparr>"} is @{text
wenzelm@26849
   120
  "(| x = a |)"}.
wenzelm@26849
   121
wenzelm@26849
   122
  A fixed record @{text "\<lparr>x = a, y = b\<rparr>"} has field @{text x} of value
wenzelm@26849
   123
  @{text a} and field @{text y} of value @{text b}.  The corresponding
wenzelm@26849
   124
  type is @{text "\<lparr>x :: A, y :: B\<rparr>"}, assuming that @{text "a :: A"}
wenzelm@26849
   125
  and @{text "b :: B"}.
wenzelm@26849
   126
wenzelm@26849
   127
  A record scheme like @{text "\<lparr>x = a, y = b, \<dots> = m\<rparr>"} contains fields
wenzelm@26849
   128
  @{text x} and @{text y} as before, but also possibly further fields
wenzelm@26849
   129
  as indicated by the ``@{text "\<dots>"}'' notation (which is actually part
wenzelm@26849
   130
  of the syntax).  The improper field ``@{text "\<dots>"}'' of a record
wenzelm@26849
   131
  scheme is called the \emph{more part}.  Logically it is just a free
wenzelm@26849
   132
  variable, which is occasionally referred to as ``row variable'' in
wenzelm@26849
   133
  the literature.  The more part of a record scheme may be
wenzelm@26849
   134
  instantiated by zero or more further components.  For example, the
wenzelm@26849
   135
  previous scheme may get instantiated to @{text "\<lparr>x = a, y = b, z =
wenzelm@26852
   136
  c, \<dots> = m'\<rparr>"}, where @{text m'} refers to a different more part.
wenzelm@26849
   137
  Fixed records are special instances of record schemes, where
wenzelm@26849
   138
  ``@{text "\<dots>"}'' is properly terminated by the @{text "() :: unit"}
wenzelm@26849
   139
  element.  In fact, @{text "\<lparr>x = a, y = b\<rparr>"} is just an abbreviation
wenzelm@26849
   140
  for @{text "\<lparr>x = a, y = b, \<dots> = ()\<rparr>"}.
wenzelm@42994
   141
wenzelm@26849
   142
  \medskip Two key observations make extensible records in a simply
wenzelm@26849
   143
  typed language like HOL work out:
wenzelm@26849
   144
wenzelm@26849
   145
  \begin{enumerate}
wenzelm@26849
   146
wenzelm@26849
   147
  \item the more part is internalized, as a free term or type
wenzelm@26849
   148
  variable,
wenzelm@26849
   149
wenzelm@26852
   150
  \item field names are externalized, they cannot be accessed within
wenzelm@26852
   151
  the logic as first-class values.
wenzelm@26849
   152
wenzelm@26849
   153
  \end{enumerate}
wenzelm@26849
   154
wenzelm@26849
   155
  \medskip In Isabelle/HOL record types have to be defined explicitly,
wenzelm@26849
   156
  fixing their field names and types, and their (optional) parent
wenzelm@26849
   157
  record.  Afterwards, records may be formed using above syntax, while
wenzelm@26849
   158
  obeying the canonical order of fields as given by their declaration.
wenzelm@26849
   159
  The record package provides several standard operations like
wenzelm@26849
   160
  selectors and updates.  The common setup for various generic proof
wenzelm@26849
   161
  tools enable succinct reasoning patterns.  See also the Isabelle/HOL
wenzelm@26849
   162
  tutorial \cite{isabelle-hol-book} for further instructions on using
wenzelm@26849
   163
  records in practice.
wenzelm@26849
   164
*}
wenzelm@26849
   165
wenzelm@26849
   166
wenzelm@26849
   167
subsection {* Record specifications *}
wenzelm@26849
   168
wenzelm@26849
   169
text {*
wenzelm@26849
   170
  \begin{matharray}{rcl}
wenzelm@28761
   171
    @{command_def (HOL) "record"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@26849
   172
  \end{matharray}
wenzelm@26849
   173
wenzelm@43467
   174
  @{rail "
wenzelm@43576
   175
    @@{command (HOL) record} @{syntax typespec_sorts} '=' \\
wenzelm@43575
   176
      (@{syntax type} '+')? (@{syntax constdecl} +)
wenzelm@43467
   177
  "}
wenzelm@26849
   178
wenzelm@28760
   179
  \begin{description}
wenzelm@26849
   180
wenzelm@28760
   181
  \item @{command (HOL) "record"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t = \<tau> + c\<^sub>1 :: \<sigma>\<^sub>1
wenzelm@28760
   182
  \<dots> c\<^sub>n :: \<sigma>\<^sub>n"} defines extensible record type @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"},
wenzelm@26849
   183
  derived from the optional parent record @{text "\<tau>"} by adding new
wenzelm@26849
   184
  field components @{text "c\<^sub>i :: \<sigma>\<^sub>i"} etc.
wenzelm@26849
   185
wenzelm@26849
   186
  The type variables of @{text "\<tau>"} and @{text "\<sigma>\<^sub>i"} need to be
wenzelm@26849
   187
  covered by the (distinct) parameters @{text "\<alpha>\<^sub>1, \<dots>,
wenzelm@26849
   188
  \<alpha>\<^sub>m"}.  Type constructor @{text t} has to be new, while @{text
wenzelm@26849
   189
  \<tau>} needs to specify an instance of an existing record type.  At
wenzelm@26849
   190
  least one new field @{text "c\<^sub>i"} has to be specified.
wenzelm@26849
   191
  Basically, field names need to belong to a unique record.  This is
wenzelm@26849
   192
  not a real restriction in practice, since fields are qualified by
wenzelm@26849
   193
  the record name internally.
wenzelm@26849
   194
wenzelm@26849
   195
  The parent record specification @{text \<tau>} is optional; if omitted
wenzelm@26849
   196
  @{text t} becomes a root record.  The hierarchy of all records
wenzelm@26849
   197
  declared within a theory context forms a forest structure, i.e.\ a
wenzelm@26849
   198
  set of trees starting with a root record each.  There is no way to
wenzelm@26849
   199
  merge multiple parent records!
wenzelm@26849
   200
wenzelm@26849
   201
  For convenience, @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"} is made a
wenzelm@26849
   202
  type abbreviation for the fixed record type @{text "\<lparr>c\<^sub>1 ::
wenzelm@26849
   203
  \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n\<rparr>"}, likewise is @{text
wenzelm@26849
   204
  "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m, \<zeta>) t_scheme"} made an abbreviation for
wenzelm@26849
   205
  @{text "\<lparr>c\<^sub>1 :: \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n, \<dots> ::
wenzelm@26849
   206
  \<zeta>\<rparr>"}.
wenzelm@26849
   207
wenzelm@28760
   208
  \end{description}
wenzelm@26849
   209
*}
wenzelm@26849
   210
wenzelm@26849
   211
wenzelm@26849
   212
subsection {* Record operations *}
wenzelm@26849
   213
wenzelm@26849
   214
text {*
wenzelm@26849
   215
  Any record definition of the form presented above produces certain
wenzelm@26849
   216
  standard operations.  Selectors and updates are provided for any
wenzelm@26849
   217
  field, including the improper one ``@{text more}''.  There are also
wenzelm@26849
   218
  cumulative record constructor functions.  To simplify the
wenzelm@26849
   219
  presentation below, we assume for now that @{text "(\<alpha>\<^sub>1, \<dots>,
wenzelm@26849
   220
  \<alpha>\<^sub>m) t"} is a root record with fields @{text "c\<^sub>1 ::
wenzelm@26849
   221
  \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n"}.
wenzelm@26849
   222
wenzelm@26849
   223
  \medskip \textbf{Selectors} and \textbf{updates} are available for
wenzelm@26849
   224
  any field (including ``@{text more}''):
wenzelm@26849
   225
wenzelm@26849
   226
  \begin{matharray}{lll}
wenzelm@26852
   227
    @{text "c\<^sub>i"} & @{text "::"} & @{text "\<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<sigma>\<^sub>i"} \\
wenzelm@26852
   228
    @{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
   229
  \end{matharray}
wenzelm@26849
   230
wenzelm@26849
   231
  There is special syntax for application of updates: @{text "r\<lparr>x :=
wenzelm@26849
   232
  a\<rparr>"} abbreviates term @{text "x_update a r"}.  Further notation for
wenzelm@26849
   233
  repeated updates is also available: @{text "r\<lparr>x := a\<rparr>\<lparr>y := b\<rparr>\<lparr>z :=
wenzelm@26849
   234
  c\<rparr>"} may be written @{text "r\<lparr>x := a, y := b, z := c\<rparr>"}.  Note that
wenzelm@26849
   235
  because of postfix notation the order of fields shown here is
wenzelm@26849
   236
  reverse than in the actual term.  Since repeated updates are just
wenzelm@26849
   237
  function applications, fields may be freely permuted in @{text "\<lparr>x
wenzelm@26849
   238
  := a, y := b, z := c\<rparr>"}, as far as logical equality is concerned.
wenzelm@26849
   239
  Thus commutativity of independent updates can be proven within the
wenzelm@26849
   240
  logic for any two fields, but not as a general theorem.
wenzelm@26849
   241
wenzelm@26849
   242
  \medskip The \textbf{make} operation provides a cumulative record
wenzelm@26849
   243
  constructor function:
wenzelm@26849
   244
wenzelm@26849
   245
  \begin{matharray}{lll}
wenzelm@26852
   246
    @{text "t.make"} & @{text "::"} & @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26849
   247
  \end{matharray}
wenzelm@26849
   248
wenzelm@26849
   249
  \medskip We now reconsider the case of non-root records, which are
wenzelm@26849
   250
  derived of some parent.  In general, the latter may depend on
wenzelm@26849
   251
  another parent as well, resulting in a list of \emph{ancestor
wenzelm@26849
   252
  records}.  Appending the lists of fields of all ancestors results in
wenzelm@26849
   253
  a certain field prefix.  The record package automatically takes care
wenzelm@26849
   254
  of this by lifting operations over this context of ancestor fields.
wenzelm@26849
   255
  Assuming that @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"} has ancestor
wenzelm@26849
   256
  fields @{text "b\<^sub>1 :: \<rho>\<^sub>1, \<dots>, b\<^sub>k :: \<rho>\<^sub>k"},
wenzelm@26849
   257
  the above record operations will get the following types:
wenzelm@26849
   258
wenzelm@26852
   259
  \medskip
wenzelm@26852
   260
  \begin{tabular}{lll}
wenzelm@26852
   261
    @{text "c\<^sub>i"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<sigma>\<^sub>i"} \\
wenzelm@42994
   262
    @{text "c\<^sub>i_update"} & @{text "::"} & @{text "\<sigma>\<^sub>i \<Rightarrow>
wenzelm@26852
   263
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow>
wenzelm@26852
   264
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
wenzelm@26852
   265
    @{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
   266
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26852
   267
  \end{tabular}
wenzelm@26852
   268
  \medskip
wenzelm@26849
   269
wenzelm@26852
   270
  \noindent Some further operations address the extension aspect of a
wenzelm@26849
   271
  derived record scheme specifically: @{text "t.fields"} produces a
wenzelm@26849
   272
  record fragment consisting of exactly the new fields introduced here
wenzelm@26849
   273
  (the result may serve as a more part elsewhere); @{text "t.extend"}
wenzelm@26849
   274
  takes a fixed record and adds a given more part; @{text
wenzelm@26849
   275
  "t.truncate"} restricts a record scheme to a fixed record.
wenzelm@26849
   276
wenzelm@26852
   277
  \medskip
wenzelm@26852
   278
  \begin{tabular}{lll}
wenzelm@26852
   279
    @{text "t.fields"} & @{text "::"} & @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26852
   280
    @{text "t.extend"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr> \<Rightarrow>
wenzelm@26852
   281
      \<zeta> \<Rightarrow> \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
wenzelm@26852
   282
    @{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
   283
  \end{tabular}
wenzelm@26852
   284
  \medskip
wenzelm@26849
   285
wenzelm@26849
   286
  \noindent Note that @{text "t.make"} and @{text "t.fields"} coincide
wenzelm@26849
   287
  for root records.
wenzelm@26849
   288
*}
wenzelm@26849
   289
wenzelm@26849
   290
wenzelm@26849
   291
subsection {* Derived rules and proof tools *}
wenzelm@26849
   292
wenzelm@26849
   293
text {*
wenzelm@26849
   294
  The record package proves several results internally, declaring
wenzelm@26849
   295
  these facts to appropriate proof tools.  This enables users to
wenzelm@26849
   296
  reason about record structures quite conveniently.  Assume that
wenzelm@26849
   297
  @{text t} is a record type as specified above.
wenzelm@26849
   298
wenzelm@26849
   299
  \begin{enumerate}
wenzelm@42994
   300
wenzelm@26849
   301
  \item Standard conversions for selectors or updates applied to
wenzelm@26849
   302
  record constructor terms are made part of the default Simplifier
wenzelm@26849
   303
  context; thus proofs by reduction of basic operations merely require
wenzelm@26849
   304
  the @{method simp} method without further arguments.  These rules
wenzelm@26849
   305
  are available as @{text "t.simps"}, too.
wenzelm@42994
   306
wenzelm@26849
   307
  \item Selectors applied to updated records are automatically reduced
wenzelm@26849
   308
  by an internal simplification procedure, which is also part of the
wenzelm@26849
   309
  standard Simplifier setup.
wenzelm@26849
   310
wenzelm@26849
   311
  \item Inject equations of a form analogous to @{prop "(x, y) = (x',
wenzelm@26849
   312
  y') \<equiv> x = x' \<and> y = y'"} are declared to the Simplifier and Classical
wenzelm@26849
   313
  Reasoner as @{attribute iff} rules.  These rules are available as
wenzelm@26849
   314
  @{text "t.iffs"}.
wenzelm@26849
   315
wenzelm@26849
   316
  \item The introduction rule for record equality analogous to @{text
wenzelm@26849
   317
  "x r = x r' \<Longrightarrow> y r = y r' \<dots> \<Longrightarrow> r = r'"} is declared to the Simplifier,
wenzelm@26849
   318
  and as the basic rule context as ``@{attribute intro}@{text "?"}''.
wenzelm@26849
   319
  The rule is called @{text "t.equality"}.
wenzelm@26849
   320
wenzelm@26849
   321
  \item Representations of arbitrary record expressions as canonical
wenzelm@26849
   322
  constructor terms are provided both in @{method cases} and @{method
wenzelm@26849
   323
  induct} format (cf.\ the generic proof methods of the same name,
wenzelm@26849
   324
  \secref{sec:cases-induct}).  Several variations are available, for
wenzelm@26849
   325
  fixed records, record schemes, more parts etc.
wenzelm@42994
   326
wenzelm@26849
   327
  The generic proof methods are sufficiently smart to pick the most
wenzelm@26849
   328
  sensible rule according to the type of the indicated record
wenzelm@26849
   329
  expression: users just need to apply something like ``@{text "(cases
wenzelm@26849
   330
  r)"}'' to a certain proof problem.
wenzelm@26849
   331
wenzelm@26849
   332
  \item The derived record operations @{text "t.make"}, @{text
wenzelm@26849
   333
  "t.fields"}, @{text "t.extend"}, @{text "t.truncate"} are \emph{not}
wenzelm@26849
   334
  treated automatically, but usually need to be expanded by hand,
wenzelm@26849
   335
  using the collective fact @{text "t.defs"}.
wenzelm@26849
   336
wenzelm@26849
   337
  \end{enumerate}
wenzelm@26849
   338
*}
wenzelm@26849
   339
wenzelm@26849
   340
wenzelm@26849
   341
section {* Datatypes \label{sec:hol-datatype} *}
wenzelm@26849
   342
wenzelm@26849
   343
text {*
wenzelm@26849
   344
  \begin{matharray}{rcl}
wenzelm@28761
   345
    @{command_def (HOL) "datatype"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@41644
   346
    @{command_def (HOL) "rep_datatype"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@26849
   347
  \end{matharray}
wenzelm@26849
   348
wenzelm@43467
   349
  @{rail "
wenzelm@43575
   350
    @@{command (HOL) datatype} (spec + @'and')
wenzelm@26849
   351
    ;
wenzelm@43467
   352
    @@{command (HOL) rep_datatype} ('(' (@{syntax name} +) ')')? (@{syntax term} +)
wenzelm@26849
   353
    ;
wenzelm@26849
   354
wenzelm@43575
   355
    spec: @{syntax parname}? @{syntax typespec} @{syntax mixfix}? '=' (cons + '|')
wenzelm@26849
   356
    ;
wenzelm@43467
   357
    cons: @{syntax name} (@{syntax type} * ) @{syntax mixfix}?
wenzelm@43467
   358
  "}
wenzelm@26849
   359
wenzelm@28760
   360
  \begin{description}
wenzelm@26849
   361
wenzelm@28760
   362
  \item @{command (HOL) "datatype"} defines inductive datatypes in
wenzelm@26849
   363
  HOL.
wenzelm@26849
   364
wenzelm@28760
   365
  \item @{command (HOL) "rep_datatype"} represents existing types as
wenzelm@26849
   366
  inductive ones, generating the standard infrastructure of derived
wenzelm@26849
   367
  concepts (primitive recursion etc.).
wenzelm@26849
   368
wenzelm@28760
   369
  \end{description}
wenzelm@26849
   370
wenzelm@26849
   371
  The induction and exhaustion theorems generated provide case names
wenzelm@26849
   372
  according to the constructors involved, while parameters are named
wenzelm@26849
   373
  after the types (see also \secref{sec:cases-induct}).
wenzelm@26849
   374
wenzelm@26849
   375
  See \cite{isabelle-HOL} for more details on datatypes, but beware of
wenzelm@26849
   376
  the old-style theory syntax being used there!  Apart from proper
wenzelm@26849
   377
  proof methods for case-analysis and induction, there are also
wenzelm@26849
   378
  emulations of ML tactics @{method (HOL) case_tac} and @{method (HOL)
wenzelm@26849
   379
  induct_tac} available, see \secref{sec:hol-induct-tac}; these admit
wenzelm@26849
   380
  to refer directly to the internal structure of subgoals (including
wenzelm@26849
   381
  internally bound parameters).
wenzelm@26849
   382
*}
wenzelm@26849
   383
wenzelm@26849
   384
haftmann@41644
   385
section {* Functorial structure of types *}
haftmann@41644
   386
haftmann@41644
   387
text {*
haftmann@41644
   388
  \begin{matharray}{rcl}
haftmann@41752
   389
    @{command_def (HOL) "enriched_type"} & : & @{text "local_theory \<rightarrow> proof(prove)"}
haftmann@41644
   390
  \end{matharray}
haftmann@41644
   391
wenzelm@43467
   392
  @{rail "
wenzelm@43488
   393
    @@{command (HOL) enriched_type} (@{syntax name} ':')? @{syntax term}
haftmann@41644
   394
    ;
wenzelm@43488
   395
  "}
haftmann@41644
   396
haftmann@41644
   397
  \begin{description}
haftmann@41644
   398
wenzelm@43488
   399
  \item @{command (HOL) "enriched_type"}~@{text "prefix: m"} allows to
wenzelm@43488
   400
  prove and register properties about the functorial structure of type
wenzelm@43488
   401
  constructors.  These properties then can be used by other packages
wenzelm@43488
   402
  to deal with those type constructors in certain type constructions.
wenzelm@43488
   403
  Characteristic theorems are noted in the current local theory.  By
wenzelm@43488
   404
  default, they are prefixed with the base name of the type
wenzelm@43488
   405
  constructor, an explicit prefix can be given alternatively.
haftmann@41644
   406
haftmann@41644
   407
  The given term @{text "m"} is considered as \emph{mapper} for the
haftmann@41644
   408
  corresponding type constructor and must conform to the following
haftmann@41644
   409
  type pattern:
haftmann@41644
   410
haftmann@41644
   411
  \begin{matharray}{lll}
haftmann@41644
   412
    @{text "m"} & @{text "::"} &
haftmann@41644
   413
      @{text "\<sigma>\<^isub>1 \<Rightarrow> \<dots> \<sigma>\<^isub>k \<Rightarrow> (\<^vec>\<alpha>\<^isub>n) t \<Rightarrow> (\<^vec>\<beta>\<^isub>n) t"} \\
haftmann@41644
   414
  \end{matharray}
haftmann@41644
   415
haftmann@41644
   416
  \noindent where @{text t} is the type constructor, @{text
haftmann@41644
   417
  "\<^vec>\<alpha>\<^isub>n"} and @{text "\<^vec>\<beta>\<^isub>n"} are distinct
haftmann@41644
   418
  type variables free in the local theory and @{text "\<sigma>\<^isub>1"},
haftmann@41644
   419
  \ldots, @{text "\<sigma>\<^isub>k"} is a subsequence of @{text "\<alpha>\<^isub>1 \<Rightarrow>
haftmann@41644
   420
  \<beta>\<^isub>1"}, @{text "\<beta>\<^isub>1 \<Rightarrow> \<alpha>\<^isub>1"}, \ldots,
haftmann@41644
   421
  @{text "\<alpha>\<^isub>n \<Rightarrow> \<beta>\<^isub>n"}, @{text "\<beta>\<^isub>n \<Rightarrow>
haftmann@41644
   422
  \<alpha>\<^isub>n"}.
haftmann@41644
   423
haftmann@41644
   424
  \end{description}
haftmann@41644
   425
*}
haftmann@41644
   426
haftmann@41644
   427
wenzelm@26849
   428
section {* Recursive functions \label{sec:recursion} *}
wenzelm@26849
   429
wenzelm@26849
   430
text {*
wenzelm@26849
   431
  \begin{matharray}{rcl}
wenzelm@28761
   432
    @{command_def (HOL) "primrec"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   433
    @{command_def (HOL) "fun"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   434
    @{command_def (HOL) "function"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
wenzelm@28761
   435
    @{command_def (HOL) "termination"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
wenzelm@26849
   436
  \end{matharray}
wenzelm@26849
   437
wenzelm@43467
   438
  @{rail "
wenzelm@43467
   439
    @@{command (HOL) primrec} @{syntax target}? @{syntax \"fixes\"} @'where' equations
wenzelm@26849
   440
    ;
wenzelm@43467
   441
    (@@{command (HOL) fun} | @@{command (HOL) function}) @{syntax target}? functionopts?
wenzelm@43467
   442
      @{syntax \"fixes\"} \\ @'where' equations
krauss@40411
   443
    ;
wenzelm@43467
   444
wenzelm@43467
   445
    equations: (@{syntax thmdecl}? @{syntax prop} + '|')
wenzelm@26849
   446
    ;
krauss@42717
   447
    functionopts: '(' (('sequential' | 'domintros') + ',') ')'
wenzelm@26849
   448
    ;
wenzelm@43467
   449
    @@{command (HOL) termination} @{syntax term}?
wenzelm@43467
   450
  "}
wenzelm@26849
   451
wenzelm@28760
   452
  \begin{description}
wenzelm@26849
   453
wenzelm@28760
   454
  \item @{command (HOL) "primrec"} defines primitive recursive
wenzelm@26849
   455
  functions over datatypes, see also \cite{isabelle-HOL}.
wenzelm@26849
   456
wenzelm@28760
   457
  \item @{command (HOL) "function"} defines functions by general
wenzelm@26849
   458
  wellfounded recursion. A detailed description with examples can be
wenzelm@26849
   459
  found in \cite{isabelle-function}. The function is specified by a
wenzelm@26849
   460
  set of (possibly conditional) recursive equations with arbitrary
wenzelm@26849
   461
  pattern matching. The command generates proof obligations for the
wenzelm@26849
   462
  completeness and the compatibility of patterns.
wenzelm@26849
   463
wenzelm@26849
   464
  The defined function is considered partial, and the resulting
wenzelm@26849
   465
  simplification rules (named @{text "f.psimps"}) and induction rule
wenzelm@26849
   466
  (named @{text "f.pinduct"}) are guarded by a generated domain
wenzelm@26849
   467
  predicate @{text "f_dom"}. The @{command (HOL) "termination"}
wenzelm@26849
   468
  command can then be used to establish that the function is total.
wenzelm@26849
   469
wenzelm@28760
   470
  \item @{command (HOL) "fun"} is a shorthand notation for ``@{command
wenzelm@28760
   471
  (HOL) "function"}~@{text "(sequential)"}, followed by automated
wenzelm@28760
   472
  proof attempts regarding pattern matching and termination.  See
wenzelm@28760
   473
  \cite{isabelle-function} for further details.
wenzelm@26849
   474
wenzelm@28760
   475
  \item @{command (HOL) "termination"}~@{text f} commences a
wenzelm@26849
   476
  termination proof for the previously defined function @{text f}.  If
wenzelm@26849
   477
  this is omitted, the command refers to the most recent function
wenzelm@26849
   478
  definition.  After the proof is closed, the recursive equations and
wenzelm@26849
   479
  the induction principle is established.
wenzelm@26849
   480
wenzelm@28760
   481
  \end{description}
wenzelm@26849
   482
haftmann@27452
   483
  Recursive definitions introduced by the @{command (HOL) "function"}
haftmann@27452
   484
  command accommodate
wenzelm@26849
   485
  reasoning by induction (cf.\ \secref{sec:cases-induct}): rule @{text
wenzelm@26849
   486
  "c.induct"} (where @{text c} is the name of the function definition)
wenzelm@26849
   487
  refers to a specific induction rule, with parameters named according
krauss@33857
   488
  to the user-specified equations. Cases are numbered (starting from 1).
krauss@33857
   489
krauss@33857
   490
  For @{command (HOL) "primrec"}, the induction principle coincides
haftmann@27452
   491
  with structural recursion on the datatype the recursion is carried
haftmann@27452
   492
  out.
wenzelm@26849
   493
wenzelm@26849
   494
  The equations provided by these packages may be referred later as
wenzelm@26849
   495
  theorem list @{text "f.simps"}, where @{text f} is the (collective)
wenzelm@26849
   496
  name of the functions defined.  Individual equations may be named
wenzelm@26849
   497
  explicitly as well.
wenzelm@26849
   498
wenzelm@26849
   499
  The @{command (HOL) "function"} command accepts the following
wenzelm@26849
   500
  options.
wenzelm@26849
   501
wenzelm@28760
   502
  \begin{description}
wenzelm@26849
   503
wenzelm@28760
   504
  \item @{text sequential} enables a preprocessor which disambiguates
wenzelm@28760
   505
  overlapping patterns by making them mutually disjoint.  Earlier
wenzelm@28760
   506
  equations take precedence over later ones.  This allows to give the
wenzelm@28760
   507
  specification in a format very similar to functional programming.
wenzelm@28760
   508
  Note that the resulting simplification and induction rules
wenzelm@28760
   509
  correspond to the transformed specification, not the one given
wenzelm@26849
   510
  originally. This usually means that each equation given by the user
hoelzl@36137
   511
  may result in several theorems.  Also note that this automatic
wenzelm@26849
   512
  transformation only works for ML-style datatype patterns.
wenzelm@26849
   513
wenzelm@28760
   514
  \item @{text domintros} enables the automated generation of
wenzelm@26849
   515
  introduction rules for the domain predicate. While mostly not
wenzelm@26849
   516
  needed, they can be helpful in some proofs about partial functions.
wenzelm@26849
   517
wenzelm@28760
   518
  \end{description}
wenzelm@26849
   519
*}
wenzelm@26849
   520
wenzelm@26849
   521
wenzelm@26849
   522
subsection {* Proof methods related to recursive definitions *}
wenzelm@26849
   523
wenzelm@26849
   524
text {*
wenzelm@26849
   525
  \begin{matharray}{rcl}
wenzelm@28761
   526
    @{method_def (HOL) pat_completeness} & : & @{text method} \\
wenzelm@28761
   527
    @{method_def (HOL) relation} & : & @{text method} \\
wenzelm@28761
   528
    @{method_def (HOL) lexicographic_order} & : & @{text method} \\
krauss@33858
   529
    @{method_def (HOL) size_change} & : & @{text method} \\
wenzelm@26849
   530
  \end{matharray}
wenzelm@26849
   531
wenzelm@43467
   532
  @{rail "
wenzelm@43467
   533
    @@{method (HOL) relation} @{syntax term}
wenzelm@26849
   534
    ;
wenzelm@43467
   535
    @@{method (HOL) lexicographic_order} (@{syntax clasimpmod} * )
wenzelm@26849
   536
    ;
wenzelm@43467
   537
    @@{method (HOL) size_change} ( orders (@{syntax clasimpmod} * ) )
krauss@33858
   538
    ;
krauss@33858
   539
    orders: ( 'max' | 'min' | 'ms' ) *
wenzelm@43467
   540
  "}
wenzelm@26849
   541
wenzelm@28760
   542
  \begin{description}
wenzelm@26849
   543
wenzelm@28760
   544
  \item @{method (HOL) pat_completeness} is a specialized method to
wenzelm@26849
   545
  solve goals regarding the completeness of pattern matching, as
wenzelm@26849
   546
  required by the @{command (HOL) "function"} package (cf.\
wenzelm@26849
   547
  \cite{isabelle-function}).
wenzelm@26849
   548
wenzelm@28760
   549
  \item @{method (HOL) relation}~@{text R} introduces a termination
wenzelm@26849
   550
  proof using the relation @{text R}.  The resulting proof state will
wenzelm@26849
   551
  contain goals expressing that @{text R} is wellfounded, and that the
wenzelm@26849
   552
  arguments of recursive calls decrease with respect to @{text R}.
wenzelm@26849
   553
  Usually, this method is used as the initial proof step of manual
wenzelm@26849
   554
  termination proofs.
wenzelm@26849
   555
wenzelm@28760
   556
  \item @{method (HOL) "lexicographic_order"} attempts a fully
wenzelm@26849
   557
  automated termination proof by searching for a lexicographic
wenzelm@26849
   558
  combination of size measures on the arguments of the function. The
wenzelm@26849
   559
  method accepts the same arguments as the @{method auto} method,
wenzelm@26849
   560
  which it uses internally to prove local descents.  The same context
wenzelm@26849
   561
  modifiers as for @{method auto} are accepted, see
wenzelm@26849
   562
  \secref{sec:clasimp}.
wenzelm@26849
   563
wenzelm@26849
   564
  In case of failure, extensive information is printed, which can help
wenzelm@26849
   565
  to analyse the situation (cf.\ \cite{isabelle-function}).
wenzelm@26849
   566
krauss@33858
   567
  \item @{method (HOL) "size_change"} also works on termination goals,
krauss@33858
   568
  using a variation of the size-change principle, together with a
krauss@33858
   569
  graph decomposition technique (see \cite{krauss_phd} for details).
krauss@33858
   570
  Three kinds of orders are used internally: @{text max}, @{text min},
krauss@33858
   571
  and @{text ms} (multiset), which is only available when the theory
krauss@33858
   572
  @{text Multiset} is loaded. When no order kinds are given, they are
krauss@33858
   573
  tried in order. The search for a termination proof uses SAT solving
krauss@33858
   574
  internally.
krauss@33858
   575
krauss@33858
   576
 For local descent proofs, the same context modifiers as for @{method
krauss@33858
   577
  auto} are accepted, see \secref{sec:clasimp}.
krauss@33858
   578
wenzelm@28760
   579
  \end{description}
wenzelm@26849
   580
*}
wenzelm@26849
   581
krauss@40412
   582
subsection {* Functions with explicit partiality *}
krauss@40412
   583
krauss@40412
   584
text {*
krauss@40412
   585
  \begin{matharray}{rcl}
krauss@40412
   586
    @{command_def (HOL) "partial_function"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
krauss@40412
   587
    @{attribute_def (HOL) "partial_function_mono"} & : & @{text attribute} \\
krauss@40412
   588
  \end{matharray}
krauss@40412
   589
wenzelm@43467
   590
  @{rail "
wenzelm@43467
   591
    @@{command (HOL) partial_function} @{syntax target}?
wenzelm@43488
   592
      '(' @{syntax nameref} ')' @{syntax \"fixes\"} \\
wenzelm@43488
   593
      @'where' @{syntax thmdecl}? @{syntax prop}
wenzelm@43488
   594
  "}
krauss@40412
   595
krauss@40412
   596
  \begin{description}
krauss@40412
   597
wenzelm@43488
   598
  \item @{command (HOL) "partial_function"}~@{text "(mode)"} defines
wenzelm@43488
   599
  recursive functions based on fixpoints in complete partial
wenzelm@43488
   600
  orders. No termination proof is required from the user or
wenzelm@43488
   601
  constructed internally. Instead, the possibility of non-termination
wenzelm@43488
   602
  is modelled explicitly in the result type, which contains an
wenzelm@43488
   603
  explicit bottom element.
krauss@40412
   604
krauss@40412
   605
  Pattern matching and mutual recursion are currently not supported.
krauss@40412
   606
  Thus, the specification consists of a single function described by a
krauss@40412
   607
  single recursive equation.
krauss@40412
   608
krauss@40412
   609
  There are no fixed syntactic restrictions on the body of the
krauss@40412
   610
  function, but the induced functional must be provably monotonic
krauss@40412
   611
  wrt.\ the underlying order.  The monotonicitity proof is performed
krauss@40412
   612
  internally, and the definition is rejected when it fails. The proof
krauss@40412
   613
  can be influenced by declaring hints using the
krauss@40412
   614
  @{attribute (HOL) partial_function_mono} attribute.
krauss@40412
   615
krauss@40412
   616
  The mandatory @{text mode} argument specifies the mode of operation
krauss@40412
   617
  of the command, which directly corresponds to a complete partial
krauss@40412
   618
  order on the result type. By default, the following modes are
wenzelm@42994
   619
  defined:
krauss@40412
   620
krauss@40412
   621
  \begin{description}
krauss@40412
   622
  \item @{text option} defines functions that map into the @{type
krauss@40412
   623
  option} type. Here, the value @{term None} is used to model a
krauss@40412
   624
  non-terminating computation. Monotonicity requires that if @{term
krauss@40412
   625
  None} is returned by a recursive call, then the overall result
krauss@40412
   626
  must also be @{term None}. This is best achieved through the use of
krauss@40412
   627
  the monadic operator @{const "Option.bind"}.
wenzelm@42994
   628
krauss@40412
   629
  \item @{text tailrec} defines functions with an arbitrary result
krauss@40412
   630
  type and uses the slightly degenerated partial order where @{term
krauss@40412
   631
  "undefined"} is the bottom element.  Now, monotonicity requires that
krauss@40412
   632
  if @{term undefined} is returned by a recursive call, then the
krauss@40412
   633
  overall result must also be @{term undefined}. In practice, this is
krauss@40412
   634
  only satisfied when each recursive call is a tail call, whose result
krauss@40412
   635
  is directly returned. Thus, this mode of operation allows the
krauss@40412
   636
  definition of arbitrary tail-recursive functions.
krauss@40412
   637
  \end{description}
krauss@40412
   638
krauss@40412
   639
  Experienced users may define new modes by instantiating the locale
krauss@40412
   640
  @{const "partial_function_definitions"} appropriately.
krauss@40412
   641
krauss@40412
   642
  \item @{attribute (HOL) partial_function_mono} declares rules for
krauss@40412
   643
  use in the internal monononicity proofs of partial function
krauss@40412
   644
  definitions.
krauss@40412
   645
krauss@40412
   646
  \end{description}
krauss@40412
   647
krauss@40412
   648
*}
wenzelm@26849
   649
wenzelm@26849
   650
subsection {* Old-style recursive function definitions (TFL) *}
wenzelm@26849
   651
wenzelm@26849
   652
text {*
wenzelm@26849
   653
  The old TFL commands @{command (HOL) "recdef"} and @{command (HOL)
wenzelm@26849
   654
  "recdef_tc"} for defining recursive are mostly obsolete; @{command
wenzelm@26849
   655
  (HOL) "function"} or @{command (HOL) "fun"} should be used instead.
wenzelm@26849
   656
wenzelm@26849
   657
  \begin{matharray}{rcl}
wenzelm@28761
   658
    @{command_def (HOL) "recdef"} & : & @{text "theory \<rightarrow> theory)"} \\
wenzelm@28761
   659
    @{command_def (HOL) "recdef_tc"}@{text "\<^sup>*"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@26849
   660
  \end{matharray}
wenzelm@26849
   661
wenzelm@43467
   662
  @{rail "
wenzelm@43467
   663
    @@{command (HOL) recdef} ('(' @'permissive' ')')? \\
wenzelm@43467
   664
      @{syntax name} @{syntax term} (@{syntax prop} +) hints?
wenzelm@26849
   665
    ;
wenzelm@43467
   666
    recdeftc @{syntax thmdecl}? tc
wenzelm@26849
   667
    ;
wenzelm@43467
   668
    hints: '(' @'hints' ( recdefmod * ) ')'
wenzelm@26849
   669
    ;
wenzelm@43467
   670
    recdefmod: (('recdef_simp' | 'recdef_cong' | 'recdef_wf')
wenzelm@43467
   671
      (() | 'add' | 'del') ':' @{syntax thmrefs}) | @{syntax clasimpmod}
wenzelm@26849
   672
    ;
wenzelm@43467
   673
    tc: @{syntax nameref} ('(' @{syntax nat} ')')?
wenzelm@43467
   674
  "}
wenzelm@26849
   675
wenzelm@28760
   676
  \begin{description}
wenzelm@42994
   677
wenzelm@28760
   678
  \item @{command (HOL) "recdef"} defines general well-founded
wenzelm@26849
   679
  recursive functions (using the TFL package), see also
wenzelm@26849
   680
  \cite{isabelle-HOL}.  The ``@{text "(permissive)"}'' option tells
wenzelm@26849
   681
  TFL to recover from failed proof attempts, returning unfinished
wenzelm@26849
   682
  results.  The @{text recdef_simp}, @{text recdef_cong}, and @{text
wenzelm@26849
   683
  recdef_wf} hints refer to auxiliary rules to be used in the internal
wenzelm@26849
   684
  automated proof process of TFL.  Additional @{syntax clasimpmod}
wenzelm@26849
   685
  declarations (cf.\ \secref{sec:clasimp}) may be given to tune the
wenzelm@26849
   686
  context of the Simplifier (cf.\ \secref{sec:simplifier}) and
wenzelm@26849
   687
  Classical reasoner (cf.\ \secref{sec:classical}).
wenzelm@42994
   688
wenzelm@28760
   689
  \item @{command (HOL) "recdef_tc"}~@{text "c (i)"} recommences the
wenzelm@26849
   690
  proof for leftover termination condition number @{text i} (default
wenzelm@26849
   691
  1) as generated by a @{command (HOL) "recdef"} definition of
wenzelm@26849
   692
  constant @{text c}.
wenzelm@42994
   693
wenzelm@26849
   694
  Note that in most cases, @{command (HOL) "recdef"} is able to finish
wenzelm@26849
   695
  its internal proofs without manual intervention.
wenzelm@26849
   696
wenzelm@28760
   697
  \end{description}
wenzelm@26849
   698
wenzelm@26849
   699
  \medskip Hints for @{command (HOL) "recdef"} may be also declared
wenzelm@26849
   700
  globally, using the following attributes.
wenzelm@26849
   701
wenzelm@26849
   702
  \begin{matharray}{rcl}
wenzelm@28761
   703
    @{attribute_def (HOL) recdef_simp} & : & @{text attribute} \\
wenzelm@28761
   704
    @{attribute_def (HOL) recdef_cong} & : & @{text attribute} \\
wenzelm@28761
   705
    @{attribute_def (HOL) recdef_wf} & : & @{text attribute} \\
wenzelm@26849
   706
  \end{matharray}
wenzelm@26849
   707
wenzelm@43467
   708
  @{rail "
wenzelm@43467
   709
    (@@{attribute (HOL) recdef_simp} | @@{attribute (HOL) recdef_cong} |
wenzelm@43467
   710
      @@{attribute (HOL) recdef_wf}) (() | 'add' | 'del')
wenzelm@43467
   711
  "}
wenzelm@26849
   712
*}
wenzelm@26849
   713
wenzelm@26849
   714
wenzelm@26849
   715
section {* Inductive and coinductive definitions \label{sec:hol-inductive} *}
wenzelm@26849
   716
wenzelm@26849
   717
text {*
wenzelm@26849
   718
  An \textbf{inductive definition} specifies the least predicate (or
wenzelm@26849
   719
  set) @{text R} closed under given rules: applying a rule to elements
wenzelm@26849
   720
  of @{text R} yields a result within @{text R}.  For example, a
wenzelm@26849
   721
  structural operational semantics is an inductive definition of an
wenzelm@26849
   722
  evaluation relation.
wenzelm@26849
   723
wenzelm@26849
   724
  Dually, a \textbf{coinductive definition} specifies the greatest
wenzelm@26849
   725
  predicate~/ set @{text R} that is consistent with given rules: every
wenzelm@26849
   726
  element of @{text R} can be seen as arising by applying a rule to
wenzelm@26849
   727
  elements of @{text R}.  An important example is using bisimulation
wenzelm@26849
   728
  relations to formalise equivalence of processes and infinite data
wenzelm@26849
   729
  structures.
wenzelm@26849
   730
wenzelm@26849
   731
  \medskip The HOL package is related to the ZF one, which is
wenzelm@26849
   732
  described in a separate paper,\footnote{It appeared in CADE
wenzelm@26849
   733
  \cite{paulson-CADE}; a longer version is distributed with Isabelle.}
wenzelm@26849
   734
  which you should refer to in case of difficulties.  The package is
wenzelm@26849
   735
  simpler than that of ZF thanks to implicit type-checking in HOL.
wenzelm@26849
   736
  The types of the (co)inductive predicates (or sets) determine the
wenzelm@26849
   737
  domain of the fixedpoint definition, and the package does not have
wenzelm@26849
   738
  to use inference rules for type-checking.
wenzelm@26849
   739
wenzelm@26849
   740
  \begin{matharray}{rcl}
wenzelm@28761
   741
    @{command_def (HOL) "inductive"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   742
    @{command_def (HOL) "inductive_set"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   743
    @{command_def (HOL) "coinductive"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   744
    @{command_def (HOL) "coinductive_set"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28761
   745
    @{attribute_def (HOL) mono} & : & @{text attribute} \\
wenzelm@26849
   746
  \end{matharray}
wenzelm@26849
   747
wenzelm@43467
   748
  @{rail "
wenzelm@43467
   749
    (@@{command (HOL) inductive} | @@{command (HOL) inductive_set} |
wenzelm@43467
   750
      @@{command (HOL) coinductive} | @@{command (HOL) coinductive_set})
wenzelm@43467
   751
    @{syntax target}? @{syntax \"fixes\"} (@'for' @{syntax \"fixes\"})? \\
wenzelm@43467
   752
    (@'where' clauses)? (@'monos' @{syntax thmrefs})?
wenzelm@26849
   753
    ;
wenzelm@43467
   754
    clauses: (@{syntax thmdecl}? @{syntax prop} + '|')
wenzelm@26849
   755
    ;
wenzelm@43467
   756
    @@{attribute (HOL) mono} (() | 'add' | 'del')
wenzelm@43467
   757
  "}
wenzelm@26849
   758
wenzelm@28760
   759
  \begin{description}
wenzelm@26849
   760
wenzelm@28760
   761
  \item @{command (HOL) "inductive"} and @{command (HOL)
wenzelm@28760
   762
  "coinductive"} define (co)inductive predicates from the
wenzelm@26849
   763
  introduction rules given in the @{keyword "where"} part.  The
wenzelm@26849
   764
  optional @{keyword "for"} part contains a list of parameters of the
wenzelm@26849
   765
  (co)inductive predicates that remain fixed throughout the
wenzelm@26849
   766
  definition.  The optional @{keyword "monos"} section contains
wenzelm@26849
   767
  \emph{monotonicity theorems}, which are required for each operator
wenzelm@26849
   768
  applied to a recursive set in the introduction rules.  There
wenzelm@26849
   769
  \emph{must} be a theorem of the form @{text "A \<le> B \<Longrightarrow> M A \<le> M B"},
wenzelm@26849
   770
  for each premise @{text "M R\<^sub>i t"} in an introduction rule!
wenzelm@26849
   771
wenzelm@28760
   772
  \item @{command (HOL) "inductive_set"} and @{command (HOL)
wenzelm@28760
   773
  "coinductive_set"} are wrappers for to the previous commands,
wenzelm@26849
   774
  allowing the definition of (co)inductive sets.
wenzelm@26849
   775
wenzelm@28760
   776
  \item @{attribute (HOL) mono} declares monotonicity rules.  These
wenzelm@26849
   777
  rule are involved in the automated monotonicity proof of @{command
wenzelm@26849
   778
  (HOL) "inductive"}.
wenzelm@26849
   779
wenzelm@28760
   780
  \end{description}
wenzelm@26849
   781
*}
wenzelm@26849
   782
wenzelm@26849
   783
wenzelm@26849
   784
subsection {* Derived rules *}
wenzelm@26849
   785
wenzelm@26849
   786
text {*
wenzelm@26849
   787
  Each (co)inductive definition @{text R} adds definitions to the
wenzelm@26849
   788
  theory and also proves some theorems:
wenzelm@26849
   789
wenzelm@26849
   790
  \begin{description}
wenzelm@26849
   791
wenzelm@28760
   792
  \item @{text R.intros} is the list of introduction rules as proven
wenzelm@26849
   793
  theorems, for the recursive predicates (or sets).  The rules are
wenzelm@26849
   794
  also available individually, using the names given them in the
wenzelm@26849
   795
  theory file;
wenzelm@26849
   796
wenzelm@28760
   797
  \item @{text R.cases} is the case analysis (or elimination) rule;
wenzelm@26849
   798
wenzelm@28760
   799
  \item @{text R.induct} or @{text R.coinduct} is the (co)induction
wenzelm@26849
   800
  rule.
wenzelm@26849
   801
wenzelm@26849
   802
  \end{description}
wenzelm@26849
   803
wenzelm@26849
   804
  When several predicates @{text "R\<^sub>1, \<dots>, R\<^sub>n"} are
wenzelm@26849
   805
  defined simultaneously, the list of introduction rules is called
wenzelm@26849
   806
  @{text "R\<^sub>1_\<dots>_R\<^sub>n.intros"}, the case analysis rules are
wenzelm@26849
   807
  called @{text "R\<^sub>1.cases, \<dots>, R\<^sub>n.cases"}, and the list
wenzelm@26849
   808
  of mutual induction rules is called @{text
wenzelm@26849
   809
  "R\<^sub>1_\<dots>_R\<^sub>n.inducts"}.
wenzelm@26849
   810
*}
wenzelm@26849
   811
wenzelm@26849
   812
wenzelm@26849
   813
subsection {* Monotonicity theorems *}
wenzelm@26849
   814
wenzelm@26849
   815
text {*
wenzelm@26849
   816
  Each theory contains a default set of theorems that are used in
wenzelm@26849
   817
  monotonicity proofs.  New rules can be added to this set via the
wenzelm@26849
   818
  @{attribute (HOL) mono} attribute.  The HOL theory @{text Inductive}
wenzelm@26849
   819
  shows how this is done.  In general, the following monotonicity
wenzelm@26849
   820
  theorems may be added:
wenzelm@26849
   821
wenzelm@26849
   822
  \begin{itemize}
wenzelm@26849
   823
wenzelm@26849
   824
  \item Theorems of the form @{text "A \<le> B \<Longrightarrow> M A \<le> M B"}, for proving
wenzelm@26849
   825
  monotonicity of inductive definitions whose introduction rules have
wenzelm@26849
   826
  premises involving terms such as @{text "M R\<^sub>i t"}.
wenzelm@26849
   827
wenzelm@26849
   828
  \item Monotonicity theorems for logical operators, which are of the
wenzelm@26849
   829
  general form @{text "(\<dots> \<longrightarrow> \<dots>) \<Longrightarrow> \<dots> (\<dots> \<longrightarrow> \<dots>) \<Longrightarrow> \<dots> \<longrightarrow> \<dots>"}.  For example, in
wenzelm@26849
   830
  the case of the operator @{text "\<or>"}, the corresponding theorem is
wenzelm@26849
   831
  \[
wenzelm@26849
   832
  \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
   833
  \]
wenzelm@26849
   834
wenzelm@26849
   835
  \item De Morgan style equations for reasoning about the ``polarity''
wenzelm@26849
   836
  of expressions, e.g.
wenzelm@26849
   837
  \[
wenzelm@26849
   838
  @{prop "\<not> \<not> P \<longleftrightarrow> P"} \qquad\qquad
wenzelm@26849
   839
  @{prop "\<not> (P \<and> Q) \<longleftrightarrow> \<not> P \<or> \<not> Q"}
wenzelm@26849
   840
  \]
wenzelm@26849
   841
wenzelm@26849
   842
  \item Equations for reducing complex operators to more primitive
wenzelm@26849
   843
  ones whose monotonicity can easily be proved, e.g.
wenzelm@26849
   844
  \[
wenzelm@26849
   845
  @{prop "(P \<longrightarrow> Q) \<longleftrightarrow> \<not> P \<or> Q"} \qquad\qquad
wenzelm@26849
   846
  @{prop "Ball A P \<equiv> \<forall>x. x \<in> A \<longrightarrow> P x"}
wenzelm@26849
   847
  \]
wenzelm@26849
   848
wenzelm@26849
   849
  \end{itemize}
wenzelm@26849
   850
wenzelm@26849
   851
  %FIXME: Example of an inductive definition
wenzelm@26849
   852
*}
wenzelm@26849
   853
wenzelm@26849
   854
wenzelm@26849
   855
section {* Arithmetic proof support *}
wenzelm@26849
   856
wenzelm@26849
   857
text {*
wenzelm@26849
   858
  \begin{matharray}{rcl}
wenzelm@28761
   859
    @{method_def (HOL) arith} & : & @{text method} \\
nipkow@30863
   860
    @{attribute_def (HOL) arith} & : & @{text attribute} \\
wenzelm@28761
   861
    @{attribute_def (HOL) arith_split} & : & @{text attribute} \\
wenzelm@26849
   862
  \end{matharray}
wenzelm@26849
   863
wenzelm@26849
   864
  The @{method (HOL) arith} method decides linear arithmetic problems
wenzelm@26849
   865
  (on types @{text nat}, @{text int}, @{text real}).  Any current
wenzelm@26849
   866
  facts are inserted into the goal before running the procedure.
wenzelm@26849
   867
nipkow@30863
   868
  The @{attribute (HOL) arith} attribute declares facts that are
nipkow@30863
   869
  always supplied to the arithmetic provers implicitly.
nipkow@30863
   870
wenzelm@26894
   871
  The @{attribute (HOL) arith_split} attribute declares case split
wenzelm@30865
   872
  rules to be expanded before @{method (HOL) arith} is invoked.
wenzelm@26849
   873
nipkow@30863
   874
  Note that a simpler (but faster) arithmetic prover is
nipkow@30863
   875
  already invoked by the Simplifier.
wenzelm@26849
   876
*}
wenzelm@26849
   877
wenzelm@26849
   878
wenzelm@30169
   879
section {* Intuitionistic proof search *}
wenzelm@30169
   880
wenzelm@30169
   881
text {*
wenzelm@30169
   882
  \begin{matharray}{rcl}
wenzelm@30171
   883
    @{method_def (HOL) iprover} & : & @{text method} \\
wenzelm@30169
   884
  \end{matharray}
wenzelm@30169
   885
wenzelm@43467
   886
  @{rail "
wenzelm@43467
   887
    @@{method (HOL) iprover} ( @{syntax rulemod} * )
wenzelm@43467
   888
  "}
wenzelm@30169
   889
wenzelm@30171
   890
  The @{method (HOL) iprover} method performs intuitionistic proof
wenzelm@30171
   891
  search, depending on specifically declared rules from the context,
wenzelm@30171
   892
  or given as explicit arguments.  Chained facts are inserted into the
wenzelm@35613
   893
  goal before commencing proof search.
wenzelm@35613
   894
wenzelm@30169
   895
  Rules need to be classified as @{attribute (Pure) intro},
wenzelm@30169
   896
  @{attribute (Pure) elim}, or @{attribute (Pure) dest}; here the
wenzelm@30169
   897
  ``@{text "!"}'' indicator refers to ``safe'' rules, which may be
wenzelm@30169
   898
  applied aggressively (without considering back-tracking later).
wenzelm@30169
   899
  Rules declared with ``@{text "?"}'' are ignored in proof search (the
wenzelm@43497
   900
  single-step @{method (Pure) rule} method still observes these).  An
wenzelm@30169
   901
  explicit weight annotation may be given as well; otherwise the
wenzelm@30169
   902
  number of rule premises will be taken into account here.
wenzelm@30169
   903
*}
wenzelm@30169
   904
wenzelm@30169
   905
wenzelm@30171
   906
section {* Coherent Logic *}
wenzelm@30171
   907
wenzelm@30171
   908
text {*
wenzelm@30171
   909
  \begin{matharray}{rcl}
wenzelm@30171
   910
    @{method_def (HOL) "coherent"} & : & @{text method} \\
wenzelm@30171
   911
  \end{matharray}
wenzelm@30171
   912
wenzelm@43467
   913
  @{rail "
wenzelm@43467
   914
    @@{method (HOL) coherent} @{syntax thmrefs}?
wenzelm@43467
   915
  "}
wenzelm@30171
   916
wenzelm@30171
   917
  The @{method (HOL) coherent} method solves problems of
wenzelm@30171
   918
  \emph{Coherent Logic} \cite{Bezem-Coquand:2005}, which covers
wenzelm@30171
   919
  applications in confluence theory, lattice theory and projective
wenzelm@41048
   920
  geometry.  See @{file "~~/src/HOL/ex/Coherent.thy"} for some
wenzelm@30171
   921
  examples.
wenzelm@30171
   922
*}
wenzelm@30171
   923
wenzelm@30171
   924
blanchet@43082
   925
section {* Proving propositions *}
blanchet@43082
   926
blanchet@43082
   927
text {*
blanchet@43082
   928
  In addition to the standard proof methods, a number of diagnosis
blanchet@43082
   929
  tools search for proofs and provide an Isar proof snippet on success.
blanchet@43082
   930
  These tools are available via the following commands.
blanchet@43082
   931
blanchet@43082
   932
  \begin{matharray}{rcl}
blanchet@43082
   933
    @{command_def (HOL) "solve_direct"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@43881
   934
    @{command_def (HOL) "try"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@43857
   935
    @{command_def (HOL) "try_methods"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@43082
   936
    @{command_def (HOL) "sledgehammer"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@43082
   937
    @{command_def (HOL) "sledgehammer_params"} & : & @{text "theory \<rightarrow> theory"}
blanchet@43082
   938
  \end{matharray}
blanchet@43082
   939
wenzelm@43467
   940
  @{rail "
blanchet@43881
   941
    @@{command (HOL) try}
blanchet@43881
   942
    ;
blanchet@43881
   943
blanchet@43857
   944
    @@{command (HOL) try_methods} ( ( ( 'simp' | 'intro' | 'elim' | 'dest' ) ':' @{syntax thmrefs} ) + ) ?
wenzelm@43467
   945
      @{syntax nat}?
wenzelm@43467
   946
    ;
blanchet@43881
   947
wenzelm@43467
   948
    @@{command (HOL) sledgehammer} ( '[' args ']' )? facts? @{syntax nat}?
blanchet@43082
   949
    ;
blanchet@43082
   950
wenzelm@43467
   951
    @@{command (HOL) sledgehammer_params} ( ( '[' args ']' ) ? )
blanchet@43082
   952
    ;
blanchet@43082
   953
wenzelm@43467
   954
    args: ( @{syntax name} '=' value + ',' )
blanchet@43082
   955
    ;
blanchet@43082
   956
wenzelm@43467
   957
    facts: '(' ( ( ( ( 'add' | 'del' ) ':' ) ? @{syntax thmrefs} ) + ) ? ')'
blanchet@43082
   958
    ;
blanchet@43860
   959
  "} % FIXME check args "value"
blanchet@43082
   960
blanchet@43082
   961
  \begin{description}
blanchet@43082
   962
blanchet@43082
   963
  \item @{command (HOL) "solve_direct"} checks whether the current subgoals can
blanchet@43082
   964
    be solved directly by an existing theorem. Duplicate lemmas can be detected
blanchet@43082
   965
    in this way.
blanchet@43082
   966
blanchet@43857
   967
  \item @{command (HOL) "try_methods"} attempts to prove a subgoal using a combination
blanchet@43082
   968
    of standard proof methods (@{text auto}, @{text simp}, @{text blast}, etc.).
blanchet@43082
   969
    Additional facts supplied via @{text "simp:"}, @{text "intro:"},
blanchet@43082
   970
    @{text "elim:"}, and @{text "dest:"} are passed to the appropriate proof
blanchet@43082
   971
    methods.
blanchet@43082
   972
blanchet@43082
   973
  \item @{command (HOL) "sledgehammer"} attempts to prove a subgoal using external
blanchet@43082
   974
    automatic provers (resolution provers and SMT solvers). See the Sledgehammer
blanchet@43082
   975
    manual \cite{isabelle-sledgehammer} for details.
blanchet@43082
   976
blanchet@43082
   977
  \item @{command (HOL) "sledgehammer_params"} changes
blanchet@43082
   978
    @{command (HOL) "sledgehammer"} configuration options persistently.
blanchet@43082
   979
blanchet@43082
   980
  \end{description}
blanchet@43082
   981
*}
blanchet@43082
   982
blanchet@43082
   983
haftmann@31906
   984
section {* Checking and refuting propositions *}
haftmann@31906
   985
haftmann@31906
   986
text {*
haftmann@31906
   987
  Identifying incorrect propositions usually involves evaluation of
blanchet@43082
   988
  particular assignments and systematic counterexample search.  This
haftmann@31906
   989
  is supported by the following commands.
haftmann@31906
   990
haftmann@31906
   991
  \begin{matharray}{rcl}
haftmann@31906
   992
    @{command_def (HOL) "value"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@31906
   993
    @{command_def (HOL) "quickcheck"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@43082
   994
    @{command_def (HOL) "refute"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@43082
   995
    @{command_def (HOL) "nitpick"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@43082
   996
    @{command_def (HOL) "quickcheck_params"} & : & @{text "theory \<rightarrow> theory"} \\
blanchet@43082
   997
    @{command_def (HOL) "refute_params"} & : & @{text "theory \<rightarrow> theory"} \\
blanchet@43082
   998
    @{command_def (HOL) "nitpick_params"} & : & @{text "theory \<rightarrow> theory"}
haftmann@31906
   999
  \end{matharray}
haftmann@31906
  1000
wenzelm@43467
  1001
  @{rail "
wenzelm@43467
  1002
    @@{command (HOL) value} ( '[' name ']' )? modes? @{syntax term}
haftmann@31906
  1003
    ;
haftmann@31906
  1004
wenzelm@43467
  1005
    (@@{command (HOL) quickcheck} | @@{command (HOL) refute} | @@{command (HOL) nitpick})
wenzelm@43467
  1006
      ( '[' args ']' )? @{syntax nat}?
haftmann@31906
  1007
    ;
haftmann@31906
  1008
wenzelm@43467
  1009
    (@@{command (HOL) quickcheck_params} | @@{command (HOL) refute_params} |
wenzelm@43467
  1010
      @@{command (HOL) nitpick_params}) ( '[' args ']' )?
haftmann@31906
  1011
    ;
haftmann@31906
  1012
wenzelm@43467
  1013
    modes: '(' (@{syntax name} +) ')'
haftmann@31906
  1014
    ;
haftmann@31906
  1015
wenzelm@43467
  1016
    args: ( @{syntax name} '=' value + ',' )
haftmann@31906
  1017
    ;
wenzelm@43467
  1018
  "} % FIXME check "value"
haftmann@31906
  1019
haftmann@31906
  1020
  \begin{description}
haftmann@31906
  1021
haftmann@31906
  1022
  \item @{command (HOL) "value"}~@{text t} evaluates and prints a
haftmann@31906
  1023
    term; optionally @{text modes} can be specified, which are
haftmann@31906
  1024
    appended to the current print mode (see also \cite{isabelle-ref}).
haftmann@31906
  1025
    Internally, the evaluation is performed by registered evaluators,
haftmann@31906
  1026
    which are invoked sequentially until a result is returned.
haftmann@31906
  1027
    Alternatively a specific evaluator can be selected using square
haftmann@37419
  1028
    brackets; typical evaluators use the current set of code equations
haftmann@37419
  1029
    to normalize and include @{text simp} for fully symbolic evaluation
haftmann@37419
  1030
    using the simplifier, @{text nbe} for \emph{normalization by evaluation}
haftmann@37419
  1031
    and \emph{code} for code generation in SML.
haftmann@31906
  1032
haftmann@31906
  1033
  \item @{command (HOL) "quickcheck"} tests the current goal for
blanchet@43082
  1034
    counterexamples using a series of assignments for its
haftmann@31906
  1035
    free variables; by default the first subgoal is tested, an other
haftmann@31906
  1036
    can be selected explicitly using an optional goal index.
bulwahn@41162
  1037
    Assignments can be chosen exhausting the search space upto a given
bulwahn@41162
  1038
    size or using a fixed number of random assignments in the search space.
bulwahn@41162
  1039
    By default, quickcheck uses exhaustive testing.
haftmann@31906
  1040
    A number of configuration options are supported for
haftmann@31906
  1041
    @{command (HOL) "quickcheck"}, notably:
haftmann@31906
  1042
haftmann@31906
  1043
    \begin{description}
haftmann@31906
  1044
bulwahn@41162
  1045
    \item[@{text tester}] specifies how to explore the search space
bulwahn@41162
  1046
      (e.g. exhaustive or random).
bulwahn@41162
  1047
      An unknown configuration option is treated as an argument to tester,
bulwahn@41162
  1048
      making @{text "tester ="} optional.
wenzelm@40515
  1049
    \item[@{text size}] specifies the maximum size of the search space
wenzelm@40515
  1050
    for assignment values.
haftmann@31906
  1051
bulwahn@42956
  1052
    \item[@{text eval}] takes a term or a list of terms and evaluates
bulwahn@42956
  1053
      these terms under the variable assignment found by quickcheck.
wenzelm@42994
  1054
wenzelm@40515
  1055
    \item[@{text iterations}] sets how many sets of assignments are
wenzelm@40515
  1056
    generated for each particular size.
haftmann@31906
  1057
wenzelm@40515
  1058
    \item[@{text no_assms}] specifies whether assumptions in
wenzelm@40515
  1059
    structured proofs should be ignored.
blanchet@35331
  1060
wenzelm@40515
  1061
    \item[@{text timeout}] sets the time limit in seconds.
bulwahn@40480
  1062
wenzelm@40515
  1063
    \item[@{text default_type}] sets the type(s) generally used to
wenzelm@40515
  1064
    instantiate type variables.
bulwahn@40480
  1065
wenzelm@40515
  1066
    \item[@{text report}] if set quickcheck reports how many tests
wenzelm@40515
  1067
    fulfilled the preconditions.
bulwahn@40480
  1068
wenzelm@40515
  1069
    \item[@{text quiet}] if not set quickcheck informs about the
wenzelm@40515
  1070
    current size for assignment values.
bulwahn@40480
  1071
wenzelm@40515
  1072
    \item[@{text expect}] can be used to check if the user's
wenzelm@40515
  1073
    expectation was met (@{text no_expectation}, @{text
wenzelm@40515
  1074
    no_counterexample}, or @{text counterexample}).
bulwahn@40480
  1075
haftmann@31906
  1076
    \end{description}
haftmann@31906
  1077
haftmann@31906
  1078
    These option can be given within square brackets.
haftmann@31906
  1079
blanchet@43082
  1080
  \item @{command (HOL) "quickcheck_params"} changes
blanchet@43082
  1081
    @{command (HOL) "quickcheck"} configuration options persistently.
blanchet@43082
  1082
blanchet@43082
  1083
  \item @{command (HOL) "refute"} tests the current goal for
blanchet@43082
  1084
    counterexamples using a reduction to SAT. The following configuration
blanchet@43082
  1085
    options are supported:
blanchet@43082
  1086
blanchet@43082
  1087
    \begin{description}
blanchet@43082
  1088
blanchet@43082
  1089
    \item[@{text minsize}] specifies the minimum size (cardinality) of the
blanchet@43082
  1090
      models to search for.
blanchet@43082
  1091
blanchet@43082
  1092
    \item[@{text maxsize}] specifies the maximum size (cardinality) of the
blanchet@43082
  1093
      models to search for. Nonpositive values mean $\infty$.
blanchet@43082
  1094
blanchet@43082
  1095
    \item[@{text maxvars}] specifies the maximum number of Boolean variables
blanchet@43082
  1096
    to use when transforming the term into a propositional formula.
blanchet@43082
  1097
    Nonpositive values mean $\infty$.
blanchet@43082
  1098
blanchet@43082
  1099
    \item[@{text satsolver}] specifies the SAT solver to use.
blanchet@43082
  1100
blanchet@43082
  1101
    \item[@{text no_assms}] specifies whether assumptions in
blanchet@43082
  1102
    structured proofs should be ignored.
blanchet@43082
  1103
blanchet@43082
  1104
    \item[@{text maxtime}] sets the time limit in seconds.
blanchet@43082
  1105
blanchet@43082
  1106
    \item[@{text expect}] can be used to check if the user's
blanchet@43082
  1107
    expectation was met (@{text genuine}, @{text potential},
blanchet@43082
  1108
    @{text none}, or @{text unknown}).
blanchet@43082
  1109
blanchet@43082
  1110
    \end{description}
blanchet@43082
  1111
blanchet@43082
  1112
    These option can be given within square brackets.
blanchet@43082
  1113
blanchet@43082
  1114
  \item @{command (HOL) "refute_params"} changes
blanchet@43082
  1115
    @{command (HOL) "refute"} configuration options persistently.
blanchet@43082
  1116
blanchet@43082
  1117
  \item @{command (HOL) "nitpick"} tests the current goal for counterexamples
blanchet@43082
  1118
    using a reduction to first-order relational logic. See the Nitpick manual
blanchet@43082
  1119
    \cite{isabelle-nitpick} for details.
blanchet@43082
  1120
blanchet@43082
  1121
  \item @{command (HOL) "nitpick_params"} changes
blanchet@43082
  1122
    @{command (HOL) "nitpick"} configuration options persistently.
haftmann@31906
  1123
haftmann@31906
  1124
  \end{description}
haftmann@31906
  1125
*}
haftmann@31906
  1126
haftmann@31906
  1127
wenzelm@28752
  1128
section {* Unstructured case analysis and induction \label{sec:hol-induct-tac} *}
wenzelm@26849
  1129
wenzelm@26849
  1130
text {*
wenzelm@27123
  1131
  The following tools of Isabelle/HOL support cases analysis and
wenzelm@27123
  1132
  induction in unstructured tactic scripts; see also
wenzelm@27123
  1133
  \secref{sec:cases-induct} for proper Isar versions of similar ideas.
wenzelm@26849
  1134
wenzelm@26849
  1135
  \begin{matharray}{rcl}
wenzelm@28761
  1136
    @{method_def (HOL) case_tac}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  1137
    @{method_def (HOL) induct_tac}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  1138
    @{method_def (HOL) ind_cases}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  1139
    @{command_def (HOL) "inductive_cases"}@{text "\<^sup>*"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@26849
  1140
  \end{matharray}
wenzelm@26849
  1141
wenzelm@43467
  1142
  @{rail "
wenzelm@43576
  1143
    @@{method (HOL) case_tac} @{syntax goal_spec}? @{syntax term} rule?
wenzelm@26849
  1144
    ;
wenzelm@43576
  1145
    @@{method (HOL) induct_tac} @{syntax goal_spec}? (@{syntax insts} * @'and') rule?
wenzelm@26849
  1146
    ;
wenzelm@43467
  1147
    @@{method (HOL) ind_cases} (@{syntax prop}+) (@'for' (@{syntax name}+))?
wenzelm@26849
  1148
    ;
wenzelm@43467
  1149
    @@{command (HOL) inductive_cases} (@{syntax thmdecl}? (@{syntax prop}+) + @'and')
wenzelm@26849
  1150
    ;
wenzelm@26849
  1151
wenzelm@43467
  1152
    rule: 'rule' ':' @{syntax thmref}
wenzelm@43467
  1153
  "}
wenzelm@26849
  1154
wenzelm@28760
  1155
  \begin{description}
wenzelm@26849
  1156
wenzelm@28760
  1157
  \item @{method (HOL) case_tac} and @{method (HOL) induct_tac} admit
wenzelm@28760
  1158
  to reason about inductive types.  Rules are selected according to
wenzelm@28760
  1159
  the declarations by the @{attribute cases} and @{attribute induct}
wenzelm@28760
  1160
  attributes, cf.\ \secref{sec:cases-induct}.  The @{command (HOL)
wenzelm@28760
  1161
  datatype} package already takes care of this.
wenzelm@27123
  1162
wenzelm@27123
  1163
  These unstructured tactics feature both goal addressing and dynamic
wenzelm@26849
  1164
  instantiation.  Note that named rule cases are \emph{not} provided
wenzelm@27123
  1165
  as would be by the proper @{method cases} and @{method induct} proof
wenzelm@27123
  1166
  methods (see \secref{sec:cases-induct}).  Unlike the @{method
wenzelm@27123
  1167
  induct} method, @{method induct_tac} does not handle structured rule
wenzelm@27123
  1168
  statements, only the compact object-logic conclusion of the subgoal
wenzelm@27123
  1169
  being addressed.
wenzelm@42994
  1170
wenzelm@28760
  1171
  \item @{method (HOL) ind_cases} and @{command (HOL)
wenzelm@28760
  1172
  "inductive_cases"} provide an interface to the internal @{ML_text
wenzelm@26860
  1173
  mk_cases} operation.  Rules are simplified in an unrestricted
wenzelm@26860
  1174
  forward manner.
wenzelm@26849
  1175
wenzelm@26849
  1176
  While @{method (HOL) ind_cases} is a proof method to apply the
wenzelm@26849
  1177
  result immediately as elimination rules, @{command (HOL)
wenzelm@26849
  1178
  "inductive_cases"} provides case split theorems at the theory level
wenzelm@26849
  1179
  for later use.  The @{keyword "for"} argument of the @{method (HOL)
wenzelm@26849
  1180
  ind_cases} method allows to specify a list of variables that should
wenzelm@26849
  1181
  be generalized before applying the resulting rule.
wenzelm@26849
  1182
wenzelm@28760
  1183
  \end{description}
wenzelm@26849
  1184
*}
wenzelm@26849
  1185
wenzelm@26849
  1186
wenzelm@26849
  1187
section {* Executable code *}
wenzelm@26849
  1188
wenzelm@43498
  1189
text {* For validation purposes, it is often useful to \emph{execute}
wenzelm@43498
  1190
  specifications.  In principle, execution could be simulated by
wenzelm@43498
  1191
  Isabelle's inference kernel, i.e. by a combination of resolution and
wenzelm@43498
  1192
  simplification.  Unfortunately, this approach is rather inefficient.
wenzelm@43498
  1193
  A more efficient way of executing specifications is to translate
wenzelm@43498
  1194
  them into a functional programming language such as ML.
wenzelm@26849
  1195
wenzelm@43498
  1196
  Isabelle provides two generic frameworks to support code generation
wenzelm@43498
  1197
  from executable specifications.  Isabelle/HOL instantiates these
wenzelm@43498
  1198
  mechanisms in a way that is amenable to end-user applications.
wenzelm@43498
  1199
*}
wenzelm@43498
  1200
wenzelm@43498
  1201
wenzelm@43498
  1202
subsection {* The new code generator (F. Haftmann) *}
wenzelm@43498
  1203
wenzelm@43498
  1204
text {* This framework generates code from functional programs
haftmann@37397
  1205
  (including overloading using type classes) to SML \cite{SML}, OCaml
haftmann@39049
  1206
  \cite{OCaml}, Haskell \cite{haskell-revised-report} and Scala
wenzelm@43498
  1207
  \cite{scala-overview-tech-report}.  Conceptually, code generation is
wenzelm@43498
  1208
  split up in three steps: \emph{selection} of code theorems,
wenzelm@43498
  1209
  \emph{translation} into an abstract executable view and
wenzelm@43498
  1210
  \emph{serialization} to a specific \emph{target language}.
wenzelm@43498
  1211
  Inductive specifications can be executed using the predicate
wenzelm@43498
  1212
  compiler which operates within HOL.  See \cite{isabelle-codegen} for
wenzelm@43498
  1213
  an introduction.
haftmann@37397
  1214
haftmann@37397
  1215
  \begin{matharray}{rcl}
haftmann@37397
  1216
    @{command_def (HOL) "export_code"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@37397
  1217
    @{attribute_def (HOL) code} & : & @{text attribute} \\
haftmann@37397
  1218
    @{command_def (HOL) "code_abort"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1219
    @{command_def (HOL) "code_datatype"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1220
    @{command_def (HOL) "print_codesetup"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@37397
  1221
    @{attribute_def (HOL) code_inline} & : & @{text attribute} \\
haftmann@37397
  1222
    @{attribute_def (HOL) code_post} & : & @{text attribute} \\
haftmann@37397
  1223
    @{command_def (HOL) "print_codeproc"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@37397
  1224
    @{command_def (HOL) "code_thms"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@37397
  1225
    @{command_def (HOL) "code_deps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@37397
  1226
    @{command_def (HOL) "code_const"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1227
    @{command_def (HOL) "code_type"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1228
    @{command_def (HOL) "code_class"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1229
    @{command_def (HOL) "code_instance"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1230
    @{command_def (HOL) "code_reserved"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1231
    @{command_def (HOL) "code_monad"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1232
    @{command_def (HOL) "code_include"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@37397
  1233
    @{command_def (HOL) "code_modulename"} & : & @{text "theory \<rightarrow> theory"} \\
haftmann@39832
  1234
    @{command_def (HOL) "code_reflect"} & : & @{text "theory \<rightarrow> theory"}
haftmann@37397
  1235
  \end{matharray}
haftmann@37397
  1236
wenzelm@43467
  1237
  @{rail "
wenzelm@43467
  1238
    @@{command (HOL) export_code} ( constexpr + ) \\
wenzelm@43467
  1239
       ( ( @'in' target ( @'module_name' @{syntax string} ) ? \\
wenzelm@43467
  1240
        ( @'file' ( @{syntax string} | '-' ) ) ? ( '(' args ')' ) ?) + ) ?
haftmann@37397
  1241
    ;
haftmann@37397
  1242
wenzelm@43467
  1243
    const: @{syntax term}
haftmann@37397
  1244
    ;
haftmann@37397
  1245
haftmann@40959
  1246
    constexpr: ( const | 'name._' | '_' )
haftmann@37397
  1247
    ;
haftmann@37397
  1248
wenzelm@43467
  1249
    typeconstructor: @{syntax nameref}
haftmann@37397
  1250
    ;
haftmann@37397
  1251
wenzelm@43467
  1252
    class: @{syntax nameref}
haftmann@37397
  1253
    ;
haftmann@37397
  1254
haftmann@39049
  1255
    target: 'SML' | 'OCaml' | 'Haskell' | 'Scala'
haftmann@37397
  1256
    ;
haftmann@37397
  1257
wenzelm@43467
  1258
    @@{attribute (HOL) code} ( 'del' | 'abstype' | 'abstract' )?
haftmann@37397
  1259
    ;
haftmann@37397
  1260
wenzelm@43467
  1261
    @@{command (HOL) code_abort} ( const + )
haftmann@37397
  1262
    ;
haftmann@37397
  1263
wenzelm@43467
  1264
    @@{command (HOL) code_datatype} ( const + )
haftmann@37397
  1265
    ;
haftmann@37397
  1266
wenzelm@43467
  1267
    @@{attribute (HOL) code_inline} ( 'del' ) ?
haftmann@37397
  1268
    ;
haftmann@37397
  1269
wenzelm@43467
  1270
    @@{attribute (HOL) code_post} ( 'del' ) ?
haftmann@37397
  1271
    ;
haftmann@37397
  1272
wenzelm@43467
  1273
    @@{command (HOL) code_thms} ( constexpr + ) ?
haftmann@37397
  1274
    ;
haftmann@37397
  1275
wenzelm@43467
  1276
    @@{command (HOL) code_deps} ( constexpr + ) ?
haftmann@37397
  1277
    ;
haftmann@37397
  1278
wenzelm@43467
  1279
    @@{command (HOL) code_const} (const + @'and') \\
wenzelm@43467
  1280
      ( ( '(' target ( syntax ? + @'and' ) ')' ) + )
haftmann@37397
  1281
    ;
haftmann@37397
  1282
wenzelm@43467
  1283
    @@{command (HOL) code_type} (typeconstructor + @'and') \\
wenzelm@43467
  1284
      ( ( '(' target ( syntax ? + @'and' ) ')' ) + )
haftmann@37397
  1285
    ;
haftmann@37397
  1286
wenzelm@43467
  1287
    @@{command (HOL) code_class} (class + @'and') \\
wenzelm@43467
  1288
      ( ( '(' target \\ ( @{syntax string} ? + @'and' ) ')' ) + )
haftmann@37397
  1289
    ;
haftmann@37397
  1290
wenzelm@43467
  1291
    @@{command (HOL) code_instance} (( typeconstructor '::' class ) + @'and') \\
wenzelm@43467
  1292
      ( ( '(' target ( '-' ? + @'and' ) ')' ) + )
haftmann@37397
  1293
    ;
haftmann@37397
  1294
wenzelm@43467
  1295
    @@{command (HOL) code_reserved} target ( @{syntax string} + )
haftmann@37397
  1296
    ;
haftmann@37397
  1297
wenzelm@43467
  1298
    @@{command (HOL) code_monad} const const target
haftmann@37397
  1299
    ;
haftmann@37397
  1300
wenzelm@43467
  1301
    @@{command (HOL) code_include} target ( @{syntax string} ( @{syntax string} | '-') )
haftmann@37397
  1302
    ;
haftmann@37397
  1303
wenzelm@43467
  1304
    @@{command (HOL) code_modulename} target ( ( @{syntax string} @{syntax string} ) + )
haftmann@37397
  1305
    ;
haftmann@37397
  1306
wenzelm@43467
  1307
    @@{command (HOL) code_reflect} @{syntax string} \\
wenzelm@43467
  1308
      ( @'datatypes' ( @{syntax string} '=' ( '_' | ( @{syntax string} + '|' ) + @'and' ) ) ) ? \\
wenzelm@43467
  1309
      ( @'functions' ( @{syntax string} + ) ) ? ( @'file' @{syntax string} ) ?
haftmann@39832
  1310
    ;
haftmann@39832
  1311
wenzelm@43467
  1312
    syntax: @{syntax string} | ( @'infix' | @'infixl' | @'infixr' ) @{syntax nat} @{syntax string}
wenzelm@43467
  1313
  "}
haftmann@37397
  1314
haftmann@37397
  1315
  \begin{description}
haftmann@37397
  1316
haftmann@37397
  1317
  \item @{command (HOL) "export_code"} generates code for a given list
haftmann@39832
  1318
  of constants in the specified target language(s).  If no
haftmann@39832
  1319
  serialization instruction is given, only abstract code is generated
haftmann@39832
  1320
  internally.
haftmann@37397
  1321
haftmann@37397
  1322
  Constants may be specified by giving them literally, referring to
haftmann@37397
  1323
  all executable contants within a certain theory by giving @{text
haftmann@37397
  1324
  "name.*"}, or referring to \emph{all} executable constants currently
haftmann@37397
  1325
  available by giving @{text "*"}.
haftmann@37397
  1326
haftmann@37397
  1327
  By default, for each involved theory one corresponding name space
haftmann@37397
  1328
  module is generated.  Alternativly, a module name may be specified
haftmann@37397
  1329
  after the @{keyword "module_name"} keyword; then \emph{all} code is
haftmann@37397
  1330
  placed in this module.
haftmann@37397
  1331
haftmann@39832
  1332
  For \emph{SML}, \emph{OCaml} and \emph{Scala} the file specification
haftmann@39832
  1333
  refers to a single file; for \emph{Haskell}, it refers to a whole
haftmann@39832
  1334
  directory, where code is generated in multiple files reflecting the
haftmann@39832
  1335
  module hierarchy.  Omitting the file specification denotes standard
haftmann@37748
  1336
  output.
haftmann@37397
  1337
haftmann@37397
  1338
  Serializers take an optional list of arguments in parentheses.  For
haftmann@37397
  1339
  \emph{SML} and \emph{OCaml}, ``@{text no_signatures}`` omits
haftmann@37397
  1340
  explicit module signatures.
wenzelm@42994
  1341
haftmann@39832
  1342
  For \emph{Haskell} a module name prefix may be given using the
haftmann@39832
  1343
  ``@{text "root:"}'' argument; ``@{text string_classes}'' adds a
haftmann@39832
  1344
  ``@{verbatim "deriving (Read, Show)"}'' clause to each appropriate
haftmann@39832
  1345
  datatype declaration.
haftmann@37397
  1346
haftmann@37397
  1347
  \item @{attribute (HOL) code} explicitly selects (or with option
haftmann@38706
  1348
  ``@{text "del"}'' deselects) a code equation for code generation.
haftmann@38706
  1349
  Usually packages introducing code equations provide a reasonable
haftmann@38706
  1350
  default setup for selection.  Variants @{text "code abstype"} and
haftmann@38706
  1351
  @{text "code abstract"} declare abstract datatype certificates or
haftmann@38706
  1352
  code equations on abstract datatype representations respectively.
haftmann@37397
  1353
haftmann@37397
  1354
  \item @{command (HOL) "code_abort"} declares constants which are not
haftmann@39832
  1355
  required to have a definition by means of code equations; if needed
haftmann@39832
  1356
  these are implemented by program abort instead.
haftmann@37397
  1357
haftmann@37397
  1358
  \item @{command (HOL) "code_datatype"} specifies a constructor set
haftmann@37397
  1359
  for a logical type.
haftmann@37397
  1360
haftmann@37397
  1361
  \item @{command (HOL) "print_codesetup"} gives an overview on
haftmann@37397
  1362
  selected code equations and code generator datatypes.
haftmann@37397
  1363
haftmann@39832
  1364
  \item @{attribute (HOL) code_inline} declares (or with option
haftmann@39832
  1365
  ``@{text "del"}'' removes) inlining theorems which are applied as
haftmann@39832
  1366
  rewrite rules to any code equation during preprocessing.
haftmann@37397
  1367
haftmann@39832
  1368
  \item @{attribute (HOL) code_post} declares (or with option ``@{text
haftmann@39832
  1369
  "del"}'' removes) theorems which are applied as rewrite rules to any
haftmann@39832
  1370
  result of an evaluation.
haftmann@37397
  1371
haftmann@39832
  1372
  \item @{command (HOL) "print_codeproc"} prints the setup of the code
haftmann@39832
  1373
  generator preprocessor.
haftmann@37397
  1374
haftmann@37397
  1375
  \item @{command (HOL) "code_thms"} prints a list of theorems
haftmann@37397
  1376
  representing the corresponding program containing all given
haftmann@37397
  1377
  constants after preprocessing.
haftmann@37397
  1378
haftmann@37397
  1379
  \item @{command (HOL) "code_deps"} visualizes dependencies of
haftmann@37397
  1380
  theorems representing the corresponding program containing all given
haftmann@37397
  1381
  constants after preprocessing.
haftmann@37397
  1382
haftmann@37397
  1383
  \item @{command (HOL) "code_const"} associates a list of constants
haftmann@37397
  1384
  with target-specific serializations; omitting a serialization
haftmann@37397
  1385
  deletes an existing serialization.
haftmann@37397
  1386
haftmann@37397
  1387
  \item @{command (HOL) "code_type"} associates a list of type
haftmann@37397
  1388
  constructors with target-specific serializations; omitting a
haftmann@37397
  1389
  serialization deletes an existing serialization.
haftmann@37397
  1390
haftmann@37397
  1391
  \item @{command (HOL) "code_class"} associates a list of classes
haftmann@37397
  1392
  with target-specific class names; omitting a serialization deletes
haftmann@37397
  1393
  an existing serialization.  This applies only to \emph{Haskell}.
haftmann@37397
  1394
haftmann@37397
  1395
  \item @{command (HOL) "code_instance"} declares a list of type
haftmann@37397
  1396
  constructor / class instance relations as ``already present'' for a
haftmann@37397
  1397
  given target.  Omitting a ``@{text "-"}'' deletes an existing
haftmann@37397
  1398
  ``already present'' declaration.  This applies only to
haftmann@37397
  1399
  \emph{Haskell}.
haftmann@37397
  1400
haftmann@37397
  1401
  \item @{command (HOL) "code_reserved"} declares a list of names as
haftmann@37397
  1402
  reserved for a given target, preventing it to be shadowed by any
haftmann@37397
  1403
  generated code.
haftmann@37397
  1404
haftmann@37397
  1405
  \item @{command (HOL) "code_monad"} provides an auxiliary mechanism
haftmann@37397
  1406
  to generate monadic code for Haskell.
haftmann@37397
  1407
haftmann@37397
  1408
  \item @{command (HOL) "code_include"} adds arbitrary named content
haftmann@37397
  1409
  (``include'') to generated code.  A ``@{text "-"}'' as last argument
haftmann@37397
  1410
  will remove an already added ``include''.
haftmann@37397
  1411
haftmann@37397
  1412
  \item @{command (HOL) "code_modulename"} declares aliasings from one
haftmann@37397
  1413
  module name onto another.
haftmann@37397
  1414
haftmann@39832
  1415
  \item @{command (HOL) "code_reflect"} without a ``@{text "file"}''
haftmann@39832
  1416
  argument compiles code into the system runtime environment and
haftmann@39832
  1417
  modifies the code generator setup that future invocations of system
haftmann@39832
  1418
  runtime code generation referring to one of the ``@{text
haftmann@39832
  1419
  "datatypes"}'' or ``@{text "functions"}'' entities use these precompiled
haftmann@39832
  1420
  entities.  With a ``@{text "file"}'' argument, the corresponding code
haftmann@39832
  1421
  is generated into that specified file without modifying the code
haftmann@39832
  1422
  generator setup.
haftmann@39832
  1423
haftmann@37397
  1424
  \end{description}
wenzelm@43498
  1425
*}
haftmann@37397
  1426
wenzelm@43498
  1427
wenzelm@43498
  1428
subsection {* The old code generator (S. Berghofer) *}
wenzelm@43498
  1429
wenzelm@43498
  1430
text {* This framework generates code from both functional and
wenzelm@43498
  1431
  relational programs to SML, as explained below.
wenzelm@26849
  1432
wenzelm@26849
  1433
  \begin{matharray}{rcl}
wenzelm@43498
  1434
    @{command_def "code_module"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@43498
  1435
    @{command_def "code_library"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@43498
  1436
    @{command_def "consts_code"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@43498
  1437
    @{command_def "types_code"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@43497
  1438
    @{attribute_def code} & : & @{text attribute} \\
wenzelm@26849
  1439
  \end{matharray}
wenzelm@26849
  1440
wenzelm@43467
  1441
  @{rail "
wenzelm@43498
  1442
  ( @@{command code_module} | @@{command code_library} ) modespec? @{syntax name}? \\
wenzelm@43467
  1443
    ( @'file' name ) ? ( @'imports' ( @{syntax name} + ) ) ? \\
wenzelm@43467
  1444
    @'contains' ( ( @{syntax name} '=' @{syntax term} ) + | @{syntax term} + )
wenzelm@26849
  1445
  ;
wenzelm@26849
  1446
wenzelm@43467
  1447
  modespec: '(' ( @{syntax name} * ) ')'
wenzelm@26849
  1448
  ;
wenzelm@26849
  1449
wenzelm@43467
  1450
  @@{command (HOL) consts_code} (codespec +)
wenzelm@26849
  1451
  ;
wenzelm@26849
  1452
wenzelm@26849
  1453
  codespec: const template attachment ?
wenzelm@26849
  1454
  ;
wenzelm@26849
  1455
wenzelm@43467
  1456
  @@{command (HOL) types_code} (tycodespec +)
wenzelm@26849
  1457
  ;
wenzelm@26849
  1458
wenzelm@43467
  1459
  tycodespec: @{syntax name} template attachment ?
wenzelm@26849
  1460
  ;
wenzelm@26849
  1461
wenzelm@43467
  1462
  const: @{syntax term}
wenzelm@26849
  1463
  ;
wenzelm@26849
  1464
wenzelm@43467
  1465
  template: '(' @{syntax string} ')'
wenzelm@26849
  1466
  ;
wenzelm@26849
  1467
wenzelm@43467
  1468
  attachment: 'attach' modespec? '{' @{syntax text} '}'
wenzelm@26849
  1469
  ;
wenzelm@26849
  1470
wenzelm@43497
  1471
  @@{attribute code} name?
wenzelm@43467
  1472
  "}
wenzelm@26849
  1473
*}
wenzelm@26849
  1474
wenzelm@27045
  1475
wenzelm@43498
  1476
subsubsection {* Invoking the code generator *}
wenzelm@43498
  1477
wenzelm@43498
  1478
text {* The code generator is invoked via the @{command code_module}
wenzelm@43498
  1479
  and @{command code_library} commands, which correspond to
wenzelm@43498
  1480
  \emph{incremental} and \emph{modular} code generation, respectively.
wenzelm@43498
  1481
wenzelm@43498
  1482
  \begin{description}
wenzelm@43498
  1483
wenzelm@43498
  1484
  \item [Modular] For each theory, an ML structure is generated,
wenzelm@43498
  1485
  containing the code generated from the constants defined in this
wenzelm@43498
  1486
  theory.
wenzelm@43498
  1487
wenzelm@43498
  1488
  \item [Incremental] All the generated code is emitted into the same
wenzelm@43498
  1489
  structure.  This structure may import code from previously generated
wenzelm@43498
  1490
  structures, which can be specified via @{keyword "imports"}.
wenzelm@43498
  1491
  Moreover, the generated structure may also be referred to in later
wenzelm@43498
  1492
  invocations of the code generator.
wenzelm@43498
  1493
wenzelm@43498
  1494
  \end{description}
wenzelm@43498
  1495
wenzelm@43498
  1496
  After the @{command code_module} and @{command code_library}
wenzelm@43498
  1497
  keywords, the user may specify an optional list of ``modes'' in
wenzelm@43498
  1498
  parentheses. These can be used to instruct the code generator to
wenzelm@43498
  1499
  emit additional code for special purposes, e.g.\ functions for
wenzelm@43498
  1500
  converting elements of generated datatypes to Isabelle terms, or
wenzelm@43498
  1501
  test data generators. The list of modes is followed by a module
wenzelm@43498
  1502
  name.  The module name is optional for modular code generation, but
wenzelm@43498
  1503
  must be specified for incremental code generation.
wenzelm@43498
  1504
wenzelm@43498
  1505
  The code can either be written to a file, in which case a file name
wenzelm@43498
  1506
  has to be specified after the @{keyword "file"} keyword, or be loaded
wenzelm@43498
  1507
  directly into Isabelle's ML environment. In the latter case, the
wenzelm@43498
  1508
  @{command ML} theory command can be used to inspect the results
wenzelm@43498
  1509
  interactively, for example.
wenzelm@43498
  1510
wenzelm@43498
  1511
  The terms from which to generate code can be specified after the
wenzelm@43498
  1512
  @{keyword "contains"} keyword, either as a list of bindings, or just
wenzelm@43498
  1513
  as a list of terms. In the latter case, the code generator just
wenzelm@43498
  1514
  produces code for all constants and types occuring in the term, but
wenzelm@43498
  1515
  does not bind the compiled terms to ML identifiers.
wenzelm@43498
  1516
wenzelm@43498
  1517
  Here is an example:
wenzelm@43498
  1518
*}
wenzelm@43498
  1519
wenzelm@43498
  1520
code_module Test
wenzelm@43523
  1521
contains test = "foldl op + (0 :: int) [1, 2, 3, 4, 5]"
wenzelm@43498
  1522
wenzelm@43498
  1523
text {* \noindent This binds the result of compiling the given term to
wenzelm@43498
  1524
  the ML identifier @{ML Test.test}.  *}
wenzelm@43498
  1525
wenzelm@43498
  1526
ML {* @{assert} (Test.test = 15) *}
wenzelm@43498
  1527
wenzelm@43498
  1528
wenzelm@43498
  1529
subsubsection {* Configuring the code generator *}
wenzelm@43498
  1530
wenzelm@43498
  1531
text {* When generating code for a complex term, the code generator
wenzelm@43498
  1532
  recursively calls itself for all subterms.  When it arrives at a
wenzelm@43498
  1533
  constant, the default strategy of the code generator is to look up
wenzelm@43498
  1534
  its definition and try to generate code for it.  Constants which
wenzelm@43498
  1535
  have no definitions that are immediately executable, may be
wenzelm@43498
  1536
  associated with a piece of ML code manually using the @{command_ref
wenzelm@43498
  1537
  consts_code} command.  It takes a list whose elements consist of a
wenzelm@43498
  1538
  constant (given in usual term syntax -- an explicit type constraint
wenzelm@43498
  1539
  accounts for overloading), and a mixfix template describing the ML
wenzelm@43498
  1540
  code. The latter is very much the same as the mixfix templates used
wenzelm@43498
  1541
  when declaring new constants.  The most notable difference is that
wenzelm@43498
  1542
  terms may be included in the ML template using antiquotation
wenzelm@43498
  1543
  brackets @{verbatim "{"}@{verbatim "*"}~@{text "..."}~@{verbatim
wenzelm@43498
  1544
  "*"}@{verbatim "}"}.
wenzelm@43498
  1545
wenzelm@43498
  1546
  A similar mechanism is available for types: @{command_ref
wenzelm@43498
  1547
  types_code} associates type constructors with specific ML code.
wenzelm@43498
  1548
wenzelm@43498
  1549
  For example, the following declarations copied from @{file
wenzelm@43498
  1550
  "~~/src/HOL/Product_Type.thy"} describe how the product type of
wenzelm@43498
  1551
  Isabelle/HOL should be compiled to ML.  *}
wenzelm@43498
  1552
wenzelm@43498
  1553
typedecl ('a, 'b) prod
wenzelm@43498
  1554
consts Pair :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) prod"
wenzelm@43498
  1555
wenzelm@43498
  1556
types_code prod  ("(_ */ _)")
wenzelm@43498
  1557
consts_code Pair  ("(_,/ _)")
wenzelm@43498
  1558
wenzelm@43498
  1559
text {* Sometimes, the code associated with a constant or type may
wenzelm@43498
  1560
  need to refer to auxiliary functions, which have to be emitted when
wenzelm@43498
  1561
  the constant is used. Code for such auxiliary functions can be
wenzelm@43498
  1562
  declared using @{keyword "attach"}. For example, the @{const wfrec}
wenzelm@43498
  1563
  function can be implemented as follows:
wenzelm@43498
  1564
*}
wenzelm@43498
  1565
wenzelm@43498
  1566
consts_code wfrec  ("\<module>wfrec?")  (* FIXME !? *)
wenzelm@43523
  1567
attach {* fun wfrec f x = f (wfrec f) x *}
wenzelm@43498
  1568
wenzelm@43498
  1569
text {* If the code containing a call to @{const wfrec} resides in an
wenzelm@43498
  1570
  ML structure different from the one containing the function
wenzelm@43498
  1571
  definition attached to @{const wfrec}, the name of the ML structure
wenzelm@43498
  1572
  (followed by a ``@{text "."}'')  is inserted in place of ``@{text
wenzelm@43498
  1573
  "\<module>"}'' in the above template.  The ``@{text "?"}''  means that
wenzelm@43498
  1574
  the code generator should ignore the first argument of @{const
wenzelm@43498
  1575
  wfrec}, i.e.\ the termination relation, which is usually not
wenzelm@43498
  1576
  executable.
wenzelm@43498
  1577
wenzelm@43498
  1578
  \medskip Another possibility of configuring the code generator is to
wenzelm@43498
  1579
  register theorems to be used for code generation. Theorems can be
wenzelm@43498
  1580
  registered via the @{attribute code} attribute. It takes an optional
wenzelm@43498
  1581
  name as an argument, which indicates the format of the
wenzelm@43498
  1582
  theorem. Currently supported formats are equations (this is the
wenzelm@43498
  1583
  default when no name is specified) and horn clauses (this is
wenzelm@43498
  1584
  indicated by the name \texttt{ind}). The left-hand sides of
wenzelm@43498
  1585
  equations may only contain constructors and distinct variables,
wenzelm@43498
  1586
  whereas horn clauses must have the same format as introduction rules
wenzelm@43498
  1587
  of inductive definitions.
wenzelm@43498
  1588
wenzelm@43498
  1589
  The following example specifies three equations from which to
wenzelm@43498
  1590
  generate code for @{term "op <"} on natural numbers (see also
wenzelm@43498
  1591
  @{"file" "~~/src/HOL/Nat.thy"}).  *}
wenzelm@43498
  1592
wenzelm@43498
  1593
lemma [code]: "(Suc m < Suc n) = (m < n)"
wenzelm@43498
  1594
  and [code]: "((n::nat) < 0) = False"
wenzelm@43498
  1595
  and [code]: "(0 < Suc n) = True" by simp_all
wenzelm@43498
  1596
wenzelm@43498
  1597
wenzelm@43498
  1598
subsubsection {* Specific HOL code generators *}
wenzelm@43498
  1599
wenzelm@43498
  1600
text {* The basic code generator framework offered by Isabelle/Pure
wenzelm@43498
  1601
  has already been extended with additional code generators for
wenzelm@43498
  1602
  specific HOL constructs. These include datatypes, recursive
wenzelm@43498
  1603
  functions and inductive relations. The code generator for inductive
wenzelm@43498
  1604
  relations can handle expressions of the form @{text "(t\<^sub>1, \<dots>, t\<^sub>n) \<in>
wenzelm@43498
  1605
  r"}, where @{text "r"} is an inductively defined relation. If at
wenzelm@43498
  1606
  least one of the @{text "t\<^sub>i"} is a dummy pattern ``@{text "_"}'',
wenzelm@43498
  1607
  the above expression evaluates to a sequence of possible answers. If
wenzelm@43498
  1608
  all of the @{text "t\<^sub>i"} are proper terms, the expression evaluates
wenzelm@43498
  1609
  to a boolean value.
wenzelm@43498
  1610
wenzelm@43523
  1611
  The following example demonstrates this for beta-reduction on lambda
wenzelm@43523
  1612
  terms (see also @{"file" "~~/src/HOL/Proofs/Lambda/Lambda.thy"}).
wenzelm@43523
  1613
*}
wenzelm@43498
  1614
wenzelm@43523
  1615
datatype dB =
wenzelm@43523
  1616
    Var nat
wenzelm@43523
  1617
  | App dB dB  (infixl "\<degree>" 200)
wenzelm@43523
  1618
  | Abs dB
wenzelm@43523
  1619
wenzelm@43523
  1620
primrec lift :: "dB \<Rightarrow> nat \<Rightarrow> dB"
wenzelm@43523
  1621
where
wenzelm@43523
  1622
    "lift (Var i) k = (if i < k then Var i else Var (i + 1))"
wenzelm@43523
  1623
  | "lift (s \<degree> t) k = lift s k \<degree> lift t k"
wenzelm@43523
  1624
  | "lift (Abs s) k = Abs (lift s (k + 1))"
wenzelm@43523
  1625
wenzelm@43523
  1626
primrec subst :: "dB \<Rightarrow> dB \<Rightarrow> nat \<Rightarrow> dB"  ("_[_'/_]" [300, 0, 0] 300)
wenzelm@43523
  1627
where
wenzelm@43523
  1628
    "(Var i)[s/k] =
wenzelm@43523
  1629
      (if k < i then Var (i - 1) else if i = k then s else Var i)"
wenzelm@43523
  1630
  | "(t \<degree> u)[s/k] = t[s/k] \<degree> u[s/k]"
wenzelm@43523
  1631
  | "(Abs t)[s/k] = Abs (t[lift s 0 / k+1])"
wenzelm@43523
  1632
wenzelm@43523
  1633
inductive beta :: "dB \<Rightarrow> dB \<Rightarrow> bool"  (infixl "\<rightarrow>\<^sub>\<beta>" 50)
wenzelm@43523
  1634
where
wenzelm@43523
  1635
    beta: "Abs s \<degree> t \<rightarrow>\<^sub>\<beta> s[t/0]"
wenzelm@43523
  1636
  | appL: "s \<rightarrow>\<^sub>\<beta> t \<Longrightarrow> s \<degree> u \<rightarrow>\<^sub>\<beta> t \<degree> u"
wenzelm@43523
  1637
  | appR: "s \<rightarrow>\<^sub>\<beta> t \<Longrightarrow> u \<degree> s \<rightarrow>\<^sub>\<beta> u \<degree> t"
wenzelm@43523
  1638
  | abs: "s \<rightarrow>\<^sub>\<beta> t \<Longrightarrow> Abs s \<rightarrow>\<^sub>\<beta> Abs t"
wenzelm@43523
  1639
wenzelm@43523
  1640
code_module Test
wenzelm@43523
  1641
contains
wenzelm@43523
  1642
  test1 = "Abs (Var 0) \<degree> Var 0 \<rightarrow>\<^sub>\<beta> Var 0"
wenzelm@43523
  1643
  test2 = "Abs (Abs (Var 0 \<degree> Var 0) \<degree> (Abs (Var 0) \<degree> Var 0)) \<rightarrow>\<^sub>\<beta> _"
wenzelm@43523
  1644
wenzelm@43523
  1645
text {*
wenzelm@43523
  1646
  In the above example, @{ML Test.test1} evaluates to a boolean,
wenzelm@43523
  1647
  whereas @{ML Test.test2} is a lazy sequence whose elements can be
wenzelm@43523
  1648
  inspected separately.
wenzelm@43523
  1649
*}
wenzelm@43523
  1650
wenzelm@43523
  1651
ML {* @{assert} Test.test1 *}
wenzelm@43523
  1652
ML {* val results = DSeq.list_of Test.test2 *}
wenzelm@43523
  1653
ML {* @{assert} (length results = 2) *}
wenzelm@43523
  1654
wenzelm@43523
  1655
text {*
wenzelm@43498
  1656
  \medskip The theory underlying the HOL code generator is described
wenzelm@43498
  1657
  more detailed in \cite{Berghofer-Nipkow:2002}. More examples that
wenzelm@43498
  1658
  illustrate the usage of the code generator can be found e.g.\ in
wenzelm@43498
  1659
  @{"file" "~~/src/HOL/MicroJava/J/JListExample.thy"} and @{"file"
wenzelm@43498
  1660
  "~~/src/HOL/MicroJava/JVM/JVMListExample.thy"}.
wenzelm@43498
  1661
*}
wenzelm@43498
  1662
wenzelm@43498
  1663
wenzelm@27045
  1664
section {* Definition by specification \label{sec:hol-specification} *}
wenzelm@27045
  1665
wenzelm@27045
  1666
text {*
wenzelm@27045
  1667
  \begin{matharray}{rcl}
wenzelm@28761
  1668
    @{command_def (HOL) "specification"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@28761
  1669
    @{command_def (HOL) "ax_specification"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@27045
  1670
  \end{matharray}
wenzelm@27045
  1671
wenzelm@43467
  1672
  @{rail "
wenzelm@43467
  1673
  (@@{command (HOL) specification} | @@{command (HOL) ax_specification})
wenzelm@43467
  1674
    '(' (decl +) ')' \\ (@{syntax thmdecl}? @{syntax prop} +)
wenzelm@27045
  1675
  ;
wenzelm@43467
  1676
  decl: ((@{syntax name} ':')? @{syntax term} '(' @'overloaded' ')'?)
wenzelm@43467
  1677
  "}
wenzelm@27045
  1678
wenzelm@28760
  1679
  \begin{description}
wenzelm@27045
  1680
wenzelm@28760
  1681
  \item @{command (HOL) "specification"}~@{text "decls \<phi>"} sets up a
wenzelm@27045
  1682
  goal stating the existence of terms with the properties specified to
wenzelm@27045
  1683
  hold for the constants given in @{text decls}.  After finishing the
wenzelm@27045
  1684
  proof, the theory will be augmented with definitions for the given
wenzelm@27045
  1685
  constants, as well as with theorems stating the properties for these
wenzelm@27045
  1686
  constants.
wenzelm@27045
  1687
wenzelm@28760
  1688
  \item @{command (HOL) "ax_specification"}~@{text "decls \<phi>"} sets up
wenzelm@28760
  1689
  a goal stating the existence of terms with the properties specified
wenzelm@28760
  1690
  to hold for the constants given in @{text decls}.  After finishing
wenzelm@28760
  1691
  the proof, the theory will be augmented with axioms expressing the
wenzelm@28760
  1692
  properties given in the first place.
wenzelm@27045
  1693
wenzelm@28760
  1694
  \item @{text decl} declares a constant to be defined by the
wenzelm@27045
  1695
  specification given.  The definition for the constant @{text c} is
wenzelm@27045
  1696
  bound to the name @{text c_def} unless a theorem name is given in
wenzelm@27045
  1697
  the declaration.  Overloaded constants should be declared as such.
wenzelm@27045
  1698
wenzelm@28760
  1699
  \end{description}
wenzelm@27045
  1700
wenzelm@27045
  1701
  Whether to use @{command (HOL) "specification"} or @{command (HOL)
wenzelm@27045
  1702
  "ax_specification"} is to some extent a matter of style.  @{command
wenzelm@27045
  1703
  (HOL) "specification"} introduces no new axioms, and so by
wenzelm@27045
  1704
  construction cannot introduce inconsistencies, whereas @{command
wenzelm@27045
  1705
  (HOL) "ax_specification"} does introduce axioms, but only after the
wenzelm@27045
  1706
  user has explicitly proven it to be safe.  A practical issue must be
wenzelm@27045
  1707
  considered, though: After introducing two constants with the same
wenzelm@27045
  1708
  properties using @{command (HOL) "specification"}, one can prove
wenzelm@27045
  1709
  that the two constants are, in fact, equal.  If this might be a
wenzelm@27045
  1710
  problem, one should use @{command (HOL) "ax_specification"}.
wenzelm@27045
  1711
*}
wenzelm@27045
  1712
wenzelm@26840
  1713
end