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