src/Doc/Isar_Ref/HOL_Specific.thy
author blanchet
Thu Sep 11 18:54:36 2014 +0200 (2014-09-11)
changeset 58306 117ba6cbe414
parent 58305 57752a91eec4
child 58310 91ea607a34d8
permissions -rw-r--r--
renamed 'rep_datatype' to 'old_rep_datatype' (HOL)
wenzelm@26840
     1
theory HOL_Specific
Christian@52895
     2
imports Base Main "~~/src/HOL/Library/Old_Recdef" "~~/src/Tools/Adhoc_Overloading"
wenzelm@26840
     3
begin
wenzelm@26840
     4
wenzelm@50109
     5
chapter {* Higher-Order Logic *}
wenzelm@42914
     6
wenzelm@42914
     7
text {* Isabelle/HOL is based on Higher-Order Logic, a polymorphic
wenzelm@42914
     8
  version of Church's Simple Theory of Types.  HOL can be best
wenzelm@42914
     9
  understood as a simply-typed version of classical set theory.  The
wenzelm@42914
    10
  logic was first implemented in Gordon's HOL system
wenzelm@42914
    11
  \cite{mgordon-hol}.  It extends Church's original logic
wenzelm@42914
    12
  \cite{church40} by explicit type variables (naive polymorphism) and
wenzelm@42914
    13
  a sound axiomatization scheme for new types based on subsets of
wenzelm@42914
    14
  existing types.
wenzelm@42914
    15
wenzelm@42914
    16
  Andrews's book \cite{andrews86} is a full description of the
wenzelm@42914
    17
  original Church-style higher-order logic, with proofs of correctness
wenzelm@42914
    18
  and completeness wrt.\ certain set-theoretic interpretations.  The
wenzelm@42914
    19
  particular extensions of Gordon-style HOL are explained semantically
wenzelm@42914
    20
  in two chapters of the 1993 HOL book \cite{pitts93}.
wenzelm@42914
    21
wenzelm@42914
    22
  Experience with HOL over decades has demonstrated that higher-order
wenzelm@42914
    23
  logic is widely applicable in many areas of mathematics and computer
wenzelm@42914
    24
  science.  In a sense, Higher-Order Logic is simpler than First-Order
wenzelm@42914
    25
  Logic, because there are fewer restrictions and special cases.  Note
wenzelm@42914
    26
  that HOL is \emph{weaker} than FOL with axioms for ZF set theory,
wenzelm@42914
    27
  which is traditionally considered the standard foundation of regular
wenzelm@42914
    28
  mathematics, but for most applications this does not matter.  If you
wenzelm@42914
    29
  prefer ML to Lisp, you will probably prefer HOL to ZF.
wenzelm@42914
    30
wenzelm@42914
    31
  \medskip The syntax of HOL follows @{text "\<lambda>"}-calculus and
wenzelm@42914
    32
  functional programming.  Function application is curried.  To apply
wenzelm@42914
    33
  the function @{text f} of type @{text "\<tau>\<^sub>1 \<Rightarrow> \<tau>\<^sub>2 \<Rightarrow> \<tau>\<^sub>3"} to the
wenzelm@42914
    34
  arguments @{text a} and @{text b} in HOL, you simply write @{text "f
wenzelm@42914
    35
  a b"} (as in ML or Haskell).  There is no ``apply'' operator; the
wenzelm@42914
    36
  existing application of the Pure @{text "\<lambda>"}-calculus is re-used.
wenzelm@42914
    37
  Note that in HOL @{text "f (a, b)"} means ``@{text "f"} applied to
wenzelm@42914
    38
  the pair @{text "(a, b)"} (which is notation for @{text "Pair a
wenzelm@42914
    39
  b"}).  The latter typically introduces extra formal efforts that can
wenzelm@42914
    40
  be avoided by currying functions by default.  Explicit tuples are as
wenzelm@42914
    41
  infrequent in HOL formalizations as in good ML or Haskell programs.
wenzelm@42914
    42
wenzelm@42914
    43
  \medskip Isabelle/HOL has a distinct feel, compared to other
wenzelm@42914
    44
  object-logics like Isabelle/ZF.  It identifies object-level types
wenzelm@42914
    45
  with meta-level types, taking advantage of the default
wenzelm@42914
    46
  type-inference mechanism of Isabelle/Pure.  HOL fully identifies
wenzelm@42914
    47
  object-level functions with meta-level functions, with native
wenzelm@42914
    48
  abstraction and application.
wenzelm@42914
    49
wenzelm@42914
    50
  These identifications allow Isabelle to support HOL particularly
wenzelm@42914
    51
  nicely, but they also mean that HOL requires some sophistication
wenzelm@42914
    52
  from the user.  In particular, an understanding of Hindley-Milner
wenzelm@42914
    53
  type-inference with type-classes, which are both used extensively in
wenzelm@42914
    54
  the standard libraries and applications.  Beginners can set
wenzelm@42914
    55
  @{attribute show_types} or even @{attribute show_sorts} to get more
wenzelm@42914
    56
  explicit information about the result of type-inference.  *}
wenzelm@42914
    57
wenzelm@42914
    58
wenzelm@50109
    59
chapter {* Derived specification elements *}
wenzelm@50109
    60
wenzelm@42908
    61
section {* Inductive and coinductive definitions \label{sec:hol-inductive} *}
wenzelm@42908
    62
wenzelm@46280
    63
text {*
wenzelm@46280
    64
  \begin{matharray}{rcl}
wenzelm@46280
    65
    @{command_def (HOL) "inductive"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@46280
    66
    @{command_def (HOL) "inductive_set"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@46280
    67
    @{command_def (HOL) "coinductive"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@46280
    68
    @{command_def (HOL) "coinductive_set"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@50302
    69
    @{command_def "print_inductives"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@46280
    70
    @{attribute_def (HOL) mono} & : & @{text attribute} \\
wenzelm@46280
    71
  \end{matharray}
wenzelm@46280
    72
wenzelm@46280
    73
  An \emph{inductive definition} specifies the least predicate or set
wenzelm@46280
    74
  @{text R} closed under given rules: applying a rule to elements of
wenzelm@46280
    75
  @{text R} yields a result within @{text R}.  For example, a
wenzelm@46280
    76
  structural operational semantics is an inductive definition of an
wenzelm@46280
    77
  evaluation relation.
wenzelm@42908
    78
wenzelm@42913
    79
  Dually, a \emph{coinductive definition} specifies the greatest
wenzelm@42913
    80
  predicate or set @{text R} that is consistent with given rules:
wenzelm@42913
    81
  every element of @{text R} can be seen as arising by applying a rule
wenzelm@42913
    82
  to elements of @{text R}.  An important example is using
wenzelm@42913
    83
  bisimulation relations to formalise equivalence of processes and
wenzelm@42913
    84
  infinite data structures.
wenzelm@47859
    85
wenzelm@42913
    86
  Both inductive and coinductive definitions are based on the
wenzelm@42913
    87
  Knaster-Tarski fixed-point theorem for complete lattices.  The
wenzelm@42913
    88
  collection of introduction rules given by the user determines a
wenzelm@42913
    89
  functor on subsets of set-theoretic relations.  The required
wenzelm@42913
    90
  monotonicity of the recursion scheme is proven as a prerequisite to
wenzelm@42913
    91
  the fixed-point definition and the resulting consequences.  This
wenzelm@42913
    92
  works by pushing inclusion through logical connectives and any other
wenzelm@42913
    93
  operator that might be wrapped around recursive occurrences of the
wenzelm@42913
    94
  defined relation: there must be a monotonicity theorem of the form
wenzelm@42913
    95
  @{text "A \<le> B \<Longrightarrow> \<M> A \<le> \<M> B"}, for each premise @{text "\<M> R t"} in an
wenzelm@42913
    96
  introduction rule.  The default rule declarations of Isabelle/HOL
wenzelm@42913
    97
  already take care of most common situations.
wenzelm@42907
    98
wenzelm@55112
    99
  @{rail \<open>
wenzelm@42908
   100
    (@@{command (HOL) inductive} | @@{command (HOL) inductive_set} |
wenzelm@42908
   101
      @@{command (HOL) coinductive} | @@{command (HOL) coinductive_set})
wenzelm@55029
   102
    @{syntax target}? \<newline>
wenzelm@55112
   103
    @{syntax "fixes"} (@'for' @{syntax "fixes"})? (@'where' clauses)? \<newline>
wenzelm@42913
   104
    (@'monos' @{syntax thmrefs})?
wenzelm@42908
   105
    ;
wenzelm@42908
   106
    clauses: (@{syntax thmdecl}? @{syntax prop} + '|')
wenzelm@42908
   107
    ;
wenzelm@42908
   108
    @@{attribute (HOL) mono} (() | 'add' | 'del')
wenzelm@55112
   109
  \<close>}
wenzelm@42908
   110
wenzelm@42908
   111
  \begin{description}
wenzelm@42908
   112
wenzelm@42908
   113
  \item @{command (HOL) "inductive"} and @{command (HOL)
wenzelm@42913
   114
  "coinductive"} define (co)inductive predicates from the introduction
wenzelm@42913
   115
  rules.
wenzelm@42913
   116
wenzelm@42913
   117
  The propositions given as @{text "clauses"} in the @{keyword
wenzelm@42913
   118
  "where"} part are either rules of the usual @{text "\<And>/\<Longrightarrow>"} format
wenzelm@42913
   119
  (with arbitrary nesting), or equalities using @{text "\<equiv>"}.  The
wenzelm@42913
   120
  latter specifies extra-logical abbreviations in the sense of
wenzelm@42913
   121
  @{command_ref abbreviation}.  Introducing abstract syntax
wenzelm@42913
   122
  simultaneously with the actual introduction rules is occasionally
wenzelm@42913
   123
  useful for complex specifications.
wenzelm@42913
   124
wenzelm@42913
   125
  The optional @{keyword "for"} part contains a list of parameters of
wenzelm@42913
   126
  the (co)inductive predicates that remain fixed throughout the
wenzelm@42913
   127
  definition, in contrast to arguments of the relation that may vary
wenzelm@42913
   128
  in each occurrence within the given @{text "clauses"}.
wenzelm@42913
   129
wenzelm@42913
   130
  The optional @{keyword "monos"} declaration contains additional
wenzelm@42908
   131
  \emph{monotonicity theorems}, which are required for each operator
wenzelm@42913
   132
  applied to a recursive set in the introduction rules.
wenzelm@42908
   133
wenzelm@42908
   134
  \item @{command (HOL) "inductive_set"} and @{command (HOL)
wenzelm@42913
   135
  "coinductive_set"} are wrappers for to the previous commands for
wenzelm@42913
   136
  native HOL predicates.  This allows to define (co)inductive sets,
wenzelm@42913
   137
  where multiple arguments are simulated via tuples.
wenzelm@42908
   138
wenzelm@50302
   139
  \item @{command "print_inductives"} prints (co)inductive definitions and
wenzelm@50302
   140
  monotonicity rules.
wenzelm@50302
   141
wenzelm@42913
   142
  \item @{attribute (HOL) mono} declares monotonicity rules in the
wenzelm@42913
   143
  context.  These rule are involved in the automated monotonicity
wenzelm@42913
   144
  proof of the above inductive and coinductive definitions.
wenzelm@42908
   145
wenzelm@42908
   146
  \end{description}
wenzelm@42908
   147
*}
wenzelm@42908
   148
wenzelm@42908
   149
wenzelm@42908
   150
subsection {* Derived rules *}
wenzelm@42908
   151
wenzelm@42913
   152
text {* A (co)inductive definition of @{text R} provides the following
wenzelm@42913
   153
  main theorems:
wenzelm@42908
   154
wenzelm@42908
   155
  \begin{description}
wenzelm@42908
   156
wenzelm@42908
   157
  \item @{text R.intros} is the list of introduction rules as proven
wenzelm@42908
   158
  theorems, for the recursive predicates (or sets).  The rules are
wenzelm@42908
   159
  also available individually, using the names given them in the
wenzelm@42908
   160
  theory file;
wenzelm@42908
   161
wenzelm@42908
   162
  \item @{text R.cases} is the case analysis (or elimination) rule;
wenzelm@42908
   163
wenzelm@42908
   164
  \item @{text R.induct} or @{text R.coinduct} is the (co)induction
bulwahn@44273
   165
  rule;
bulwahn@44273
   166
bulwahn@44273
   167
  \item @{text R.simps} is the equation unrolling the fixpoint of the
bulwahn@44273
   168
  predicate one step.
wenzelm@47859
   169
wenzelm@42908
   170
  \end{description}
wenzelm@42908
   171
wenzelm@42908
   172
  When several predicates @{text "R\<^sub>1, \<dots>, R\<^sub>n"} are
wenzelm@42908
   173
  defined simultaneously, the list of introduction rules is called
wenzelm@42908
   174
  @{text "R\<^sub>1_\<dots>_R\<^sub>n.intros"}, the case analysis rules are
wenzelm@42908
   175
  called @{text "R\<^sub>1.cases, \<dots>, R\<^sub>n.cases"}, and the list
wenzelm@42908
   176
  of mutual induction rules is called @{text
wenzelm@42908
   177
  "R\<^sub>1_\<dots>_R\<^sub>n.inducts"}.
wenzelm@42908
   178
*}
wenzelm@42908
   179
wenzelm@42908
   180
wenzelm@42908
   181
subsection {* Monotonicity theorems *}
wenzelm@42908
   182
wenzelm@42913
   183
text {* The context maintains a default set of theorems that are used
wenzelm@42913
   184
  in monotonicity proofs.  New rules can be declared via the
wenzelm@42913
   185
  @{attribute (HOL) mono} attribute.  See the main Isabelle/HOL
wenzelm@42913
   186
  sources for some examples.  The general format of such monotonicity
wenzelm@42913
   187
  theorems is as follows:
wenzelm@42908
   188
wenzelm@42908
   189
  \begin{itemize}
wenzelm@42908
   190
wenzelm@42913
   191
  \item Theorems of the form @{text "A \<le> B \<Longrightarrow> \<M> A \<le> \<M> B"}, for proving
wenzelm@42908
   192
  monotonicity of inductive definitions whose introduction rules have
wenzelm@42913
   193
  premises involving terms such as @{text "\<M> R t"}.
wenzelm@42908
   194
wenzelm@42908
   195
  \item Monotonicity theorems for logical operators, which are of the
wenzelm@42908
   196
  general form @{text "(\<dots> \<longrightarrow> \<dots>) \<Longrightarrow> \<dots> (\<dots> \<longrightarrow> \<dots>) \<Longrightarrow> \<dots> \<longrightarrow> \<dots>"}.  For example, in
wenzelm@42908
   197
  the case of the operator @{text "\<or>"}, the corresponding theorem is
wenzelm@42908
   198
  \[
wenzelm@42908
   199
  \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@42908
   200
  \]
wenzelm@42908
   201
wenzelm@42908
   202
  \item De Morgan style equations for reasoning about the ``polarity''
wenzelm@42908
   203
  of expressions, e.g.
wenzelm@42908
   204
  \[
wenzelm@42908
   205
  @{prop "\<not> \<not> P \<longleftrightarrow> P"} \qquad\qquad
wenzelm@42908
   206
  @{prop "\<not> (P \<and> Q) \<longleftrightarrow> \<not> P \<or> \<not> Q"}
wenzelm@42908
   207
  \]
wenzelm@42908
   208
wenzelm@42908
   209
  \item Equations for reducing complex operators to more primitive
wenzelm@42908
   210
  ones whose monotonicity can easily be proved, e.g.
wenzelm@42908
   211
  \[
wenzelm@42908
   212
  @{prop "(P \<longrightarrow> Q) \<longleftrightarrow> \<not> P \<or> Q"} \qquad\qquad
wenzelm@42908
   213
  @{prop "Ball A P \<equiv> \<forall>x. x \<in> A \<longrightarrow> P x"}
wenzelm@42908
   214
  \]
wenzelm@42908
   215
wenzelm@42908
   216
  \end{itemize}
wenzelm@42913
   217
*}
wenzelm@42908
   218
wenzelm@42913
   219
subsubsection {* Examples *}
wenzelm@42913
   220
wenzelm@42913
   221
text {* The finite powerset operator can be defined inductively like this: *}
wenzelm@42913
   222
wenzelm@42913
   223
inductive_set Fin :: "'a set \<Rightarrow> 'a set set" for A :: "'a set"
wenzelm@42913
   224
where
wenzelm@42913
   225
  empty: "{} \<in> Fin A"
wenzelm@42913
   226
| insert: "a \<in> A \<Longrightarrow> B \<in> Fin A \<Longrightarrow> insert a B \<in> Fin A"
wenzelm@42913
   227
wenzelm@42913
   228
text {* The accessible part of a relation is defined as follows: *}
wenzelm@42913
   229
wenzelm@42913
   230
inductive acc :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> bool"
wenzelm@42913
   231
  for r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"  (infix "\<prec>" 50)
wenzelm@42913
   232
where acc: "(\<And>y. y \<prec> x \<Longrightarrow> acc r y) \<Longrightarrow> acc r x"
wenzelm@42913
   233
wenzelm@42913
   234
text {* Common logical connectives can be easily characterized as
wenzelm@42913
   235
non-recursive inductive definitions with parameters, but without
wenzelm@42913
   236
arguments. *}
wenzelm@42913
   237
wenzelm@42913
   238
inductive AND for A B :: bool
wenzelm@42913
   239
where "A \<Longrightarrow> B \<Longrightarrow> AND A B"
wenzelm@42913
   240
wenzelm@42913
   241
inductive OR for A B :: bool
wenzelm@42913
   242
where "A \<Longrightarrow> OR A B"
wenzelm@42913
   243
  | "B \<Longrightarrow> OR A B"
wenzelm@42913
   244
wenzelm@42913
   245
inductive EXISTS for B :: "'a \<Rightarrow> bool"
wenzelm@42913
   246
where "B a \<Longrightarrow> EXISTS B"
wenzelm@42913
   247
wenzelm@42913
   248
text {* Here the @{text "cases"} or @{text "induct"} rules produced by
wenzelm@42913
   249
  the @{command inductive} package coincide with the expected
wenzelm@42913
   250
  elimination rules for Natural Deduction.  Already in the original
wenzelm@42913
   251
  article by Gerhard Gentzen \cite{Gentzen:1935} there is a hint that
wenzelm@42913
   252
  each connective can be characterized by its introductions, and the
wenzelm@42913
   253
  elimination can be constructed systematically. *}
wenzelm@42908
   254
wenzelm@42908
   255
wenzelm@42908
   256
section {* Recursive functions \label{sec:recursion} *}
wenzelm@42908
   257
wenzelm@42908
   258
text {*
wenzelm@42908
   259
  \begin{matharray}{rcl}
wenzelm@42908
   260
    @{command_def (HOL) "primrec"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@42908
   261
    @{command_def (HOL) "fun"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@42908
   262
    @{command_def (HOL) "function"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
wenzelm@42908
   263
    @{command_def (HOL) "termination"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
krauss@54017
   264
    @{command_def (HOL) "fun_cases"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@42908
   265
  \end{matharray}
wenzelm@42908
   266
wenzelm@55112
   267
  @{rail \<open>
wenzelm@55112
   268
    @@{command (HOL) primrec} @{syntax target}? @{syntax "fixes"} @'where' equations
wenzelm@42908
   269
    ;
wenzelm@42908
   270
    (@@{command (HOL) fun} | @@{command (HOL) function}) @{syntax target}? functionopts?
wenzelm@55112
   271
      @{syntax "fixes"} \<newline> @'where' equations
wenzelm@26849
   272
    ;
wenzelm@26849
   273
wenzelm@42908
   274
    equations: (@{syntax thmdecl}? @{syntax prop} + '|')
wenzelm@26849
   275
    ;
wenzelm@42908
   276
    functionopts: '(' (('sequential' | 'domintros') + ',') ')'
wenzelm@26849
   277
    ;
wenzelm@42908
   278
    @@{command (HOL) termination} @{syntax term}?
krauss@54017
   279
    ;
krauss@54017
   280
    @@{command (HOL) fun_cases} (@{syntax thmdecl}? @{syntax prop} + @'and')
wenzelm@55112
   281
  \<close>}
wenzelm@26849
   282
wenzelm@28760
   283
  \begin{description}
wenzelm@42123
   284
wenzelm@42908
   285
  \item @{command (HOL) "primrec"} defines primitive recursive
blanchet@58305
   286
  functions over datatypes (see also @{command_ref (HOL) datatype_new}).
blanchet@58305
   287
  The given @{text equations} specify reduction rules that are produced
blanchet@58305
   288
  by instantiating the generic combinator for primitive recursion that
blanchet@58305
   289
  is available for each datatype.
wenzelm@42912
   290
wenzelm@42912
   291
  Each equation needs to be of the form:
wenzelm@42912
   292
wenzelm@42912
   293
  @{text [display] "f x\<^sub>1 \<dots> x\<^sub>m (C y\<^sub>1 \<dots> y\<^sub>k) z\<^sub>1 \<dots> z\<^sub>n = rhs"}
wenzelm@42912
   294
wenzelm@42912
   295
  such that @{text C} is a datatype constructor, @{text rhs} contains
wenzelm@42912
   296
  only the free variables on the left-hand side (or from the context),
wenzelm@42912
   297
  and all recursive occurrences of @{text "f"} in @{text "rhs"} are of
wenzelm@42912
   298
  the form @{text "f \<dots> y\<^sub>i \<dots>"} for some @{text i}.  At most one
wenzelm@42912
   299
  reduction rule for each constructor can be given.  The order does
wenzelm@42912
   300
  not matter.  For missing constructors, the function is defined to
wenzelm@42912
   301
  return a default value, but this equation is made difficult to
wenzelm@42912
   302
  access for users.
wenzelm@42912
   303
wenzelm@42912
   304
  The reduction rules are declared as @{attribute simp} by default,
wenzelm@42912
   305
  which enables standard proof methods like @{method simp} and
wenzelm@42912
   306
  @{method auto} to normalize expressions of @{text "f"} applied to
wenzelm@42912
   307
  datatype constructions, by simulating symbolic computation via
wenzelm@42912
   308
  rewriting.
wenzelm@35744
   309
wenzelm@42908
   310
  \item @{command (HOL) "function"} defines functions by general
wenzelm@42908
   311
  wellfounded recursion. A detailed description with examples can be
wenzelm@42908
   312
  found in \cite{isabelle-function}. The function is specified by a
wenzelm@42908
   313
  set of (possibly conditional) recursive equations with arbitrary
wenzelm@42908
   314
  pattern matching. The command generates proof obligations for the
wenzelm@42908
   315
  completeness and the compatibility of patterns.
wenzelm@42907
   316
wenzelm@42908
   317
  The defined function is considered partial, and the resulting
wenzelm@42908
   318
  simplification rules (named @{text "f.psimps"}) and induction rule
wenzelm@42908
   319
  (named @{text "f.pinduct"}) are guarded by a generated domain
wenzelm@42908
   320
  predicate @{text "f_dom"}. The @{command (HOL) "termination"}
wenzelm@42908
   321
  command can then be used to establish that the function is total.
wenzelm@42123
   322
wenzelm@42908
   323
  \item @{command (HOL) "fun"} is a shorthand notation for ``@{command
wenzelm@42908
   324
  (HOL) "function"}~@{text "(sequential)"}, followed by automated
wenzelm@42908
   325
  proof attempts regarding pattern matching and termination.  See
wenzelm@42908
   326
  \cite{isabelle-function} for further details.
wenzelm@42123
   327
wenzelm@42908
   328
  \item @{command (HOL) "termination"}~@{text f} commences a
wenzelm@42908
   329
  termination proof for the previously defined function @{text f}.  If
wenzelm@42908
   330
  this is omitted, the command refers to the most recent function
wenzelm@42908
   331
  definition.  After the proof is closed, the recursive equations and
wenzelm@42908
   332
  the induction principle is established.
wenzelm@26849
   333
krauss@54017
   334
  \item @{command (HOL) "fun_cases"} generates specialized elimination
krauss@54017
   335
  rules for function equations. It expects one or more function equations
krauss@54017
   336
  and produces rules that eliminate the given equalities, following the cases
krauss@54017
   337
  given in the function definition.
wenzelm@28760
   338
  \end{description}
wenzelm@42907
   339
wenzelm@42908
   340
  Recursive definitions introduced by the @{command (HOL) "function"}
wenzelm@42912
   341
  command accommodate reasoning by induction (cf.\ @{method induct}):
wenzelm@42912
   342
  rule @{text "f.induct"} refers to a specific induction rule, with
wenzelm@42912
   343
  parameters named according to the user-specified equations. Cases
wenzelm@42912
   344
  are numbered starting from 1.  For @{command (HOL) "primrec"}, the
wenzelm@42912
   345
  induction principle coincides with structural recursion on the
wenzelm@42912
   346
  datatype where the recursion is carried out.
wenzelm@42908
   347
wenzelm@42908
   348
  The equations provided by these packages may be referred later as
wenzelm@42908
   349
  theorem list @{text "f.simps"}, where @{text f} is the (collective)
wenzelm@42908
   350
  name of the functions defined.  Individual equations may be named
wenzelm@42908
   351
  explicitly as well.
wenzelm@42908
   352
wenzelm@42908
   353
  The @{command (HOL) "function"} command accepts the following
wenzelm@42908
   354
  options.
wenzelm@42908
   355
wenzelm@42908
   356
  \begin{description}
wenzelm@42908
   357
wenzelm@42908
   358
  \item @{text sequential} enables a preprocessor which disambiguates
wenzelm@42908
   359
  overlapping patterns by making them mutually disjoint.  Earlier
wenzelm@42908
   360
  equations take precedence over later ones.  This allows to give the
wenzelm@42908
   361
  specification in a format very similar to functional programming.
wenzelm@42908
   362
  Note that the resulting simplification and induction rules
wenzelm@42908
   363
  correspond to the transformed specification, not the one given
wenzelm@42908
   364
  originally. This usually means that each equation given by the user
wenzelm@42908
   365
  may result in several theorems.  Also note that this automatic
wenzelm@42908
   366
  transformation only works for ML-style datatype patterns.
wenzelm@42908
   367
wenzelm@42908
   368
  \item @{text domintros} enables the automated generation of
wenzelm@42908
   369
  introduction rules for the domain predicate. While mostly not
wenzelm@42908
   370
  needed, they can be helpful in some proofs about partial functions.
wenzelm@42908
   371
wenzelm@42908
   372
  \end{description}
wenzelm@26849
   373
*}
wenzelm@26849
   374
wenzelm@42912
   375
subsubsection {* Example: evaluation of expressions *}
wenzelm@42912
   376
wenzelm@42912
   377
text {* Subsequently, we define mutual datatypes for arithmetic and
wenzelm@42912
   378
  boolean expressions, and use @{command primrec} for evaluation
wenzelm@42912
   379
  functions that follow the same recursive structure. *}
wenzelm@42912
   380
blanchet@58305
   381
datatype_new 'a aexp =
wenzelm@42912
   382
    IF "'a bexp"  "'a aexp"  "'a aexp"
wenzelm@42912
   383
  | Sum "'a aexp"  "'a aexp"
wenzelm@42912
   384
  | Diff "'a aexp"  "'a aexp"
wenzelm@42912
   385
  | Var 'a
wenzelm@42912
   386
  | Num nat
wenzelm@42912
   387
and 'a bexp =
wenzelm@42912
   388
    Less "'a aexp"  "'a aexp"
wenzelm@42912
   389
  | And "'a bexp"  "'a bexp"
wenzelm@42912
   390
  | Neg "'a bexp"
wenzelm@42912
   391
wenzelm@42912
   392
wenzelm@42912
   393
text {* \medskip Evaluation of arithmetic and boolean expressions *}
wenzelm@42912
   394
wenzelm@42912
   395
primrec evala :: "('a \<Rightarrow> nat) \<Rightarrow> 'a aexp \<Rightarrow> nat"
wenzelm@42912
   396
  and evalb :: "('a \<Rightarrow> nat) \<Rightarrow> 'a bexp \<Rightarrow> bool"
wenzelm@42912
   397
where
wenzelm@42912
   398
  "evala env (IF b a1 a2) = (if evalb env b then evala env a1 else evala env a2)"
wenzelm@42912
   399
| "evala env (Sum a1 a2) = evala env a1 + evala env a2"
wenzelm@42912
   400
| "evala env (Diff a1 a2) = evala env a1 - evala env a2"
wenzelm@42912
   401
| "evala env (Var v) = env v"
wenzelm@42912
   402
| "evala env (Num n) = n"
wenzelm@42912
   403
| "evalb env (Less a1 a2) = (evala env a1 < evala env a2)"
wenzelm@42912
   404
| "evalb env (And b1 b2) = (evalb env b1 \<and> evalb env b2)"
wenzelm@42912
   405
| "evalb env (Neg b) = (\<not> evalb env b)"
wenzelm@42912
   406
wenzelm@42912
   407
text {* Since the value of an expression depends on the value of its
wenzelm@42912
   408
  variables, the functions @{const evala} and @{const evalb} take an
wenzelm@42912
   409
  additional parameter, an \emph{environment} that maps variables to
wenzelm@42912
   410
  their values.
wenzelm@42912
   411
wenzelm@42912
   412
  \medskip Substitution on expressions can be defined similarly.  The
wenzelm@42912
   413
  mapping @{text f} of type @{typ "'a \<Rightarrow> 'a aexp"} given as a
wenzelm@42912
   414
  parameter is lifted canonically on the types @{typ "'a aexp"} and
wenzelm@42912
   415
  @{typ "'a bexp"}, respectively.
wenzelm@42912
   416
*}
wenzelm@42912
   417
wenzelm@42912
   418
primrec substa :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a aexp \<Rightarrow> 'b aexp"
wenzelm@42912
   419
  and substb :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a bexp \<Rightarrow> 'b bexp"
wenzelm@42912
   420
where
wenzelm@42912
   421
  "substa f (IF b a1 a2) = IF (substb f b) (substa f a1) (substa f a2)"
wenzelm@42912
   422
| "substa f (Sum a1 a2) = Sum (substa f a1) (substa f a2)"
wenzelm@42912
   423
| "substa f (Diff a1 a2) = Diff (substa f a1) (substa f a2)"
wenzelm@42912
   424
| "substa f (Var v) = f v"
wenzelm@42912
   425
| "substa f (Num n) = Num n"
wenzelm@42912
   426
| "substb f (Less a1 a2) = Less (substa f a1) (substa f a2)"
wenzelm@42912
   427
| "substb f (And b1 b2) = And (substb f b1) (substb f b2)"
wenzelm@42912
   428
| "substb f (Neg b) = Neg (substb f b)"
wenzelm@42912
   429
wenzelm@42912
   430
text {* In textbooks about semantics one often finds substitution
wenzelm@42912
   431
  theorems, which express the relationship between substitution and
wenzelm@42912
   432
  evaluation.  For @{typ "'a aexp"} and @{typ "'a bexp"}, we can prove
wenzelm@42912
   433
  such a theorem by mutual induction, followed by simplification.
wenzelm@42912
   434
*}
wenzelm@42912
   435
wenzelm@42912
   436
lemma subst_one:
wenzelm@42912
   437
  "evala env (substa (Var (v := a')) a) = evala (env (v := evala env a')) a"
wenzelm@42912
   438
  "evalb env (substb (Var (v := a')) b) = evalb (env (v := evala env a')) b"
wenzelm@42912
   439
  by (induct a and b) simp_all
wenzelm@42912
   440
wenzelm@42912
   441
lemma subst_all:
wenzelm@42912
   442
  "evala env (substa s a) = evala (\<lambda>x. evala env (s x)) a"
wenzelm@42912
   443
  "evalb env (substb s b) = evalb (\<lambda>x. evala env (s x)) b"
wenzelm@42912
   444
  by (induct a and b) simp_all
wenzelm@42912
   445
wenzelm@42912
   446
wenzelm@42912
   447
subsubsection {* Example: a substitution function for terms *}
wenzelm@42912
   448
wenzelm@42912
   449
text {* Functions on datatypes with nested recursion are also defined
wenzelm@42912
   450
  by mutual primitive recursion. *}
wenzelm@42912
   451
blanchet@58305
   452
datatype_new ('a, 'b) "term" = Var 'a | App 'b "('a, 'b) term list"
wenzelm@42912
   453
wenzelm@42912
   454
text {* A substitution function on type @{typ "('a, 'b) term"} can be
wenzelm@42912
   455
  defined as follows, by working simultaneously on @{typ "('a, 'b)
wenzelm@42912
   456
  term list"}: *}
wenzelm@42912
   457
wenzelm@42912
   458
primrec subst_term :: "('a \<Rightarrow> ('a, 'b) term) \<Rightarrow> ('a, 'b) term \<Rightarrow> ('a, 'b) term" and
wenzelm@42912
   459
  subst_term_list :: "('a \<Rightarrow> ('a, 'b) term) \<Rightarrow> ('a, 'b) term list \<Rightarrow> ('a, 'b) term list"
wenzelm@42912
   460
where
wenzelm@42912
   461
  "subst_term f (Var a) = f a"
wenzelm@42912
   462
| "subst_term f (App b ts) = App b (subst_term_list f ts)"
wenzelm@42912
   463
| "subst_term_list f [] = []"
wenzelm@42912
   464
| "subst_term_list f (t # ts) = subst_term f t # subst_term_list f ts"
wenzelm@42912
   465
wenzelm@42912
   466
text {* The recursion scheme follows the structure of the unfolded
wenzelm@42912
   467
  definition of type @{typ "('a, 'b) term"}.  To prove properties of this
wenzelm@42912
   468
  substitution function, mutual induction is needed:
wenzelm@42912
   469
*}
wenzelm@42912
   470
wenzelm@42912
   471
lemma "subst_term (subst_term f1 \<circ> f2) t = subst_term f1 (subst_term f2 t)" and
wenzelm@42912
   472
  "subst_term_list (subst_term f1 \<circ> f2) ts = subst_term_list f1 (subst_term_list f2 ts)"
blanchet@58305
   473
  by (induct t and ts rule: subst_term.induct subst_term_list.induct) simp_all
wenzelm@42912
   474
wenzelm@42912
   475
wenzelm@42912
   476
subsubsection {* Example: a map function for infinitely branching trees *}
wenzelm@42912
   477
wenzelm@42912
   478
text {* Defining functions on infinitely branching datatypes by
wenzelm@42912
   479
  primitive recursion is just as easy.
wenzelm@42912
   480
*}
wenzelm@42912
   481
blanchet@58305
   482
datatype_new 'a tree = Atom 'a | Branch "nat \<Rightarrow> 'a tree"
wenzelm@42912
   483
wenzelm@42912
   484
primrec map_tree :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a tree \<Rightarrow> 'b tree"
wenzelm@42912
   485
where
wenzelm@42912
   486
  "map_tree f (Atom a) = Atom (f a)"
wenzelm@42912
   487
| "map_tree f (Branch ts) = Branch (\<lambda>x. map_tree f (ts x))"
wenzelm@42912
   488
wenzelm@42912
   489
text {* Note that all occurrences of functions such as @{text ts}
wenzelm@42912
   490
  above must be applied to an argument.  In particular, @{term
wenzelm@42912
   491
  "map_tree f \<circ> ts"} is not allowed here. *}
wenzelm@42912
   492
wenzelm@42912
   493
text {* Here is a simple composition lemma for @{term map_tree}: *}
wenzelm@42912
   494
wenzelm@42912
   495
lemma "map_tree g (map_tree f t) = map_tree (g \<circ> f) t"
wenzelm@42912
   496
  by (induct t) simp_all
wenzelm@42912
   497
wenzelm@42907
   498
wenzelm@42908
   499
subsection {* Proof methods related to recursive definitions *}
wenzelm@26849
   500
wenzelm@26849
   501
text {*
wenzelm@26849
   502
  \begin{matharray}{rcl}
wenzelm@42908
   503
    @{method_def (HOL) pat_completeness} & : & @{text method} \\
wenzelm@42908
   504
    @{method_def (HOL) relation} & : & @{text method} \\
wenzelm@42908
   505
    @{method_def (HOL) lexicographic_order} & : & @{text method} \\
wenzelm@42908
   506
    @{method_def (HOL) size_change} & : & @{text method} \\
bulwahn@45944
   507
    @{method_def (HOL) induction_schema} & : & @{text method} \\
wenzelm@26849
   508
  \end{matharray}
wenzelm@26849
   509
wenzelm@55112
   510
  @{rail \<open>
wenzelm@42908
   511
    @@{method (HOL) relation} @{syntax term}
wenzelm@42908
   512
    ;
wenzelm@42908
   513
    @@{method (HOL) lexicographic_order} (@{syntax clasimpmod} * )
wenzelm@42908
   514
    ;
wenzelm@42908
   515
    @@{method (HOL) size_change} ( orders (@{syntax clasimpmod} * ) )
wenzelm@42908
   516
    ;
bulwahn@45944
   517
    @@{method (HOL) induction_schema}
bulwahn@45944
   518
    ;
wenzelm@42908
   519
    orders: ( 'max' | 'min' | 'ms' ) *
wenzelm@55112
   520
  \<close>}
wenzelm@42908
   521
wenzelm@42908
   522
  \begin{description}
wenzelm@42908
   523
wenzelm@42908
   524
  \item @{method (HOL) pat_completeness} is a specialized method to
wenzelm@42908
   525
  solve goals regarding the completeness of pattern matching, as
wenzelm@42908
   526
  required by the @{command (HOL) "function"} package (cf.\
wenzelm@42908
   527
  \cite{isabelle-function}).
wenzelm@42908
   528
wenzelm@42908
   529
  \item @{method (HOL) relation}~@{text R} introduces a termination
wenzelm@42908
   530
  proof using the relation @{text R}.  The resulting proof state will
wenzelm@42908
   531
  contain goals expressing that @{text R} is wellfounded, and that the
wenzelm@42908
   532
  arguments of recursive calls decrease with respect to @{text R}.
wenzelm@42908
   533
  Usually, this method is used as the initial proof step of manual
wenzelm@42908
   534
  termination proofs.
wenzelm@42908
   535
wenzelm@42908
   536
  \item @{method (HOL) "lexicographic_order"} attempts a fully
wenzelm@42908
   537
  automated termination proof by searching for a lexicographic
wenzelm@42908
   538
  combination of size measures on the arguments of the function. The
wenzelm@42908
   539
  method accepts the same arguments as the @{method auto} method,
wenzelm@42930
   540
  which it uses internally to prove local descents.  The @{syntax
wenzelm@42930
   541
  clasimpmod} modifiers are accepted (as for @{method auto}).
wenzelm@42908
   542
wenzelm@42908
   543
  In case of failure, extensive information is printed, which can help
wenzelm@42908
   544
  to analyse the situation (cf.\ \cite{isabelle-function}).
wenzelm@42908
   545
wenzelm@42908
   546
  \item @{method (HOL) "size_change"} also works on termination goals,
wenzelm@42908
   547
  using a variation of the size-change principle, together with a
wenzelm@42908
   548
  graph decomposition technique (see \cite{krauss_phd} for details).
wenzelm@42908
   549
  Three kinds of orders are used internally: @{text max}, @{text min},
wenzelm@42908
   550
  and @{text ms} (multiset), which is only available when the theory
wenzelm@42908
   551
  @{text Multiset} is loaded. When no order kinds are given, they are
wenzelm@42908
   552
  tried in order. The search for a termination proof uses SAT solving
wenzelm@42908
   553
  internally.
wenzelm@42908
   554
wenzelm@42930
   555
  For local descent proofs, the @{syntax clasimpmod} modifiers are
wenzelm@42930
   556
  accepted (as for @{method auto}).
wenzelm@42908
   557
bulwahn@45944
   558
  \item @{method (HOL) induction_schema} derives user-specified
wenzelm@46283
   559
  induction rules from well-founded induction and completeness of
wenzelm@46283
   560
  patterns. This factors out some operations that are done internally
wenzelm@46283
   561
  by the function package and makes them available separately. See
wenzelm@46283
   562
  @{file "~~/src/HOL/ex/Induction_Schema.thy"} for examples.
bulwahn@45944
   563
wenzelm@42908
   564
  \end{description}
wenzelm@42908
   565
*}
wenzelm@42908
   566
wenzelm@42908
   567
wenzelm@42908
   568
subsection {* Functions with explicit partiality *}
wenzelm@42908
   569
wenzelm@42908
   570
text {*
wenzelm@42908
   571
  \begin{matharray}{rcl}
wenzelm@42908
   572
    @{command_def (HOL) "partial_function"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@42908
   573
    @{attribute_def (HOL) "partial_function_mono"} & : & @{text attribute} \\
wenzelm@42908
   574
  \end{matharray}
wenzelm@42908
   575
wenzelm@55112
   576
  @{rail \<open>
wenzelm@42908
   577
    @@{command (HOL) partial_function} @{syntax target}?
wenzelm@55112
   578
      '(' @{syntax nameref} ')' @{syntax "fixes"} \<newline>
wenzelm@42908
   579
      @'where' @{syntax thmdecl}? @{syntax prop}
wenzelm@55112
   580
  \<close>}
wenzelm@26849
   581
wenzelm@28760
   582
  \begin{description}
wenzelm@42123
   583
wenzelm@42908
   584
  \item @{command (HOL) "partial_function"}~@{text "(mode)"} defines
wenzelm@42908
   585
  recursive functions based on fixpoints in complete partial
wenzelm@42908
   586
  orders. No termination proof is required from the user or
wenzelm@42908
   587
  constructed internally. Instead, the possibility of non-termination
wenzelm@42908
   588
  is modelled explicitly in the result type, which contains an
wenzelm@42908
   589
  explicit bottom element.
wenzelm@42908
   590
wenzelm@42908
   591
  Pattern matching and mutual recursion are currently not supported.
wenzelm@42908
   592
  Thus, the specification consists of a single function described by a
wenzelm@42908
   593
  single recursive equation.
wenzelm@42908
   594
wenzelm@42908
   595
  There are no fixed syntactic restrictions on the body of the
wenzelm@42908
   596
  function, but the induced functional must be provably monotonic
Christian@52895
   597
  wrt.\ the underlying order.  The monotonicity proof is performed
wenzelm@42908
   598
  internally, and the definition is rejected when it fails. The proof
wenzelm@42908
   599
  can be influenced by declaring hints using the
wenzelm@42908
   600
  @{attribute (HOL) partial_function_mono} attribute.
wenzelm@42908
   601
wenzelm@42908
   602
  The mandatory @{text mode} argument specifies the mode of operation
wenzelm@42908
   603
  of the command, which directly corresponds to a complete partial
wenzelm@42908
   604
  order on the result type. By default, the following modes are
wenzelm@42908
   605
  defined:
wenzelm@26849
   606
wenzelm@42908
   607
  \begin{description}
wenzelm@46283
   608
wenzelm@42908
   609
  \item @{text option} defines functions that map into the @{type
wenzelm@42908
   610
  option} type. Here, the value @{term None} is used to model a
wenzelm@42908
   611
  non-terminating computation. Monotonicity requires that if @{term
wenzelm@46283
   612
  None} is returned by a recursive call, then the overall result must
wenzelm@46283
   613
  also be @{term None}. This is best achieved through the use of the
wenzelm@46283
   614
  monadic operator @{const "Option.bind"}.
wenzelm@42908
   615
wenzelm@42908
   616
  \item @{text tailrec} defines functions with an arbitrary result
wenzelm@42908
   617
  type and uses the slightly degenerated partial order where @{term
wenzelm@42908
   618
  "undefined"} is the bottom element.  Now, monotonicity requires that
wenzelm@42908
   619
  if @{term undefined} is returned by a recursive call, then the
wenzelm@42908
   620
  overall result must also be @{term undefined}. In practice, this is
wenzelm@42908
   621
  only satisfied when each recursive call is a tail call, whose result
wenzelm@42908
   622
  is directly returned. Thus, this mode of operation allows the
wenzelm@42908
   623
  definition of arbitrary tail-recursive functions.
wenzelm@46283
   624
wenzelm@42908
   625
  \end{description}
wenzelm@42908
   626
wenzelm@42908
   627
  Experienced users may define new modes by instantiating the locale
wenzelm@42908
   628
  @{const "partial_function_definitions"} appropriately.
wenzelm@42908
   629
wenzelm@42908
   630
  \item @{attribute (HOL) partial_function_mono} declares rules for
Christian@52895
   631
  use in the internal monotonicity proofs of partial function
wenzelm@42908
   632
  definitions.
wenzelm@26849
   633
wenzelm@28760
   634
  \end{description}
wenzelm@42908
   635
wenzelm@42908
   636
*}
wenzelm@42908
   637
wenzelm@42908
   638
wenzelm@42908
   639
subsection {* Old-style recursive function definitions (TFL) *}
wenzelm@42908
   640
wenzelm@42908
   641
text {*
wenzelm@42908
   642
  \begin{matharray}{rcl}
wenzelm@42908
   643
    @{command_def (HOL) "recdef"} & : & @{text "theory \<rightarrow> theory)"} \\
wenzelm@42908
   644
    @{command_def (HOL) "recdef_tc"}@{text "\<^sup>*"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@42908
   645
  \end{matharray}
wenzelm@42908
   646
wenzelm@46280
   647
  The old TFL commands @{command (HOL) "recdef"} and @{command (HOL)
wenzelm@46280
   648
  "recdef_tc"} for defining recursive are mostly obsolete; @{command
wenzelm@46280
   649
  (HOL) "function"} or @{command (HOL) "fun"} should be used instead.
wenzelm@46280
   650
wenzelm@55112
   651
  @{rail \<open>
wenzelm@55029
   652
    @@{command (HOL) recdef} ('(' @'permissive' ')')? \<newline>
wenzelm@42908
   653
      @{syntax name} @{syntax term} (@{syntax prop} +) hints?
wenzelm@42908
   654
    ;
wenzelm@42908
   655
    recdeftc @{syntax thmdecl}? tc
wenzelm@42908
   656
    ;
wenzelm@42908
   657
    hints: '(' @'hints' ( recdefmod * ) ')'
wenzelm@42908
   658
    ;
wenzelm@42908
   659
    recdefmod: (('recdef_simp' | 'recdef_cong' | 'recdef_wf')
wenzelm@42908
   660
      (() | 'add' | 'del') ':' @{syntax thmrefs}) | @{syntax clasimpmod}
wenzelm@42908
   661
    ;
wenzelm@42908
   662
    tc: @{syntax nameref} ('(' @{syntax nat} ')')?
wenzelm@55112
   663
  \<close>}
wenzelm@42908
   664
wenzelm@42908
   665
  \begin{description}
wenzelm@42908
   666
wenzelm@42908
   667
  \item @{command (HOL) "recdef"} defines general well-founded
wenzelm@42908
   668
  recursive functions (using the TFL package), see also
wenzelm@42908
   669
  \cite{isabelle-HOL}.  The ``@{text "(permissive)"}'' option tells
wenzelm@42908
   670
  TFL to recover from failed proof attempts, returning unfinished
wenzelm@42908
   671
  results.  The @{text recdef_simp}, @{text recdef_cong}, and @{text
wenzelm@42908
   672
  recdef_wf} hints refer to auxiliary rules to be used in the internal
wenzelm@42908
   673
  automated proof process of TFL.  Additional @{syntax clasimpmod}
wenzelm@42930
   674
  declarations may be given to tune the context of the Simplifier
wenzelm@42930
   675
  (cf.\ \secref{sec:simplifier}) and Classical reasoner (cf.\
wenzelm@42930
   676
  \secref{sec:classical}).
wenzelm@42908
   677
wenzelm@42908
   678
  \item @{command (HOL) "recdef_tc"}~@{text "c (i)"} recommences the
wenzelm@42908
   679
  proof for leftover termination condition number @{text i} (default
wenzelm@42908
   680
  1) as generated by a @{command (HOL) "recdef"} definition of
wenzelm@42908
   681
  constant @{text c}.
wenzelm@42908
   682
wenzelm@42908
   683
  Note that in most cases, @{command (HOL) "recdef"} is able to finish
wenzelm@42908
   684
  its internal proofs without manual intervention.
wenzelm@42908
   685
wenzelm@42908
   686
  \end{description}
wenzelm@42908
   687
wenzelm@42908
   688
  \medskip Hints for @{command (HOL) "recdef"} may be also declared
wenzelm@42908
   689
  globally, using the following attributes.
wenzelm@42908
   690
wenzelm@42908
   691
  \begin{matharray}{rcl}
wenzelm@42908
   692
    @{attribute_def (HOL) recdef_simp} & : & @{text attribute} \\
wenzelm@42908
   693
    @{attribute_def (HOL) recdef_cong} & : & @{text attribute} \\
wenzelm@42908
   694
    @{attribute_def (HOL) recdef_wf} & : & @{text attribute} \\
wenzelm@42908
   695
  \end{matharray}
wenzelm@42908
   696
wenzelm@55112
   697
  @{rail \<open>
wenzelm@42908
   698
    (@@{attribute (HOL) recdef_simp} | @@{attribute (HOL) recdef_cong} |
wenzelm@42908
   699
      @@{attribute (HOL) recdef_wf}) (() | 'add' | 'del')
wenzelm@55112
   700
  \<close>}
wenzelm@42908
   701
*}
wenzelm@42908
   702
wenzelm@42908
   703
blanchet@58305
   704
section {* Old-style datatypes \label{sec:hol-datatype} *}
wenzelm@42908
   705
wenzelm@42908
   706
text {*
wenzelm@42908
   707
  \begin{matharray}{rcl}
blanchet@58305
   708
    @{command_def (HOL) "old_datatype"} & : & @{text "theory \<rightarrow> theory"} \\
blanchet@58306
   709
    @{command_def (HOL) "old_rep_datatype"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@42908
   710
  \end{matharray}
wenzelm@42908
   711
wenzelm@55112
   712
  @{rail \<open>
blanchet@58305
   713
    @@{command (HOL) old_datatype} (spec + @'and')
wenzelm@42908
   714
    ;
blanchet@58306
   715
    @@{command (HOL) old_rep_datatype} ('(' (@{syntax name} +) ')')? (@{syntax term} +)
wenzelm@42908
   716
    ;
wenzelm@42908
   717
wenzelm@45839
   718
    spec: @{syntax typespec_sorts} @{syntax mixfix}? '=' (cons + '|')
wenzelm@42908
   719
    ;
wenzelm@42908
   720
    cons: @{syntax name} (@{syntax type} * ) @{syntax mixfix}?
wenzelm@55112
   721
  \<close>}
wenzelm@42908
   722
wenzelm@42908
   723
  \begin{description}
wenzelm@42908
   724
blanchet@58305
   725
  \item @{command (HOL) "old_datatype"} defines old-style inductive
blanchet@58305
   726
  datatypes in HOL.
wenzelm@42908
   727
blanchet@58306
   728
  \item @{command (HOL) "old_rep_datatype"} represents existing types as
blanchet@58305
   729
  old-style datatypes.
wenzelm@42908
   730
wenzelm@42908
   731
  \end{description}
wenzelm@42908
   732
blanchet@58305
   733
  These commands are mostly obsolete; @{command (HOL) "datatype"}
blanchet@58305
   734
  should be used instead.
wenzelm@42908
   735
wenzelm@42908
   736
  See \cite{isabelle-HOL} for more details on datatypes, but beware of
wenzelm@42908
   737
  the old-style theory syntax being used there!  Apart from proper
wenzelm@42908
   738
  proof methods for case-analysis and induction, there are also
wenzelm@42908
   739
  emulations of ML tactics @{method (HOL) case_tac} and @{method (HOL)
wenzelm@42908
   740
  induct_tac} available, see \secref{sec:hol-induct-tac}; these admit
wenzelm@42908
   741
  to refer directly to the internal structure of subgoals (including
wenzelm@42908
   742
  internally bound parameters).
wenzelm@26849
   743
*}
wenzelm@26849
   744
wenzelm@26849
   745
wenzelm@42910
   746
subsubsection {* Examples *}
wenzelm@42910
   747
wenzelm@42910
   748
text {* We define a type of finite sequences, with slightly different
wenzelm@42910
   749
  names than the existing @{typ "'a list"} that is already in @{theory
wenzelm@42910
   750
  Main}: *}
wenzelm@42910
   751
blanchet@58305
   752
datatype_new 'a seq = Empty | Seq 'a "'a seq"
wenzelm@42910
   753
wenzelm@42910
   754
text {* We can now prove some simple lemma by structural induction: *}
wenzelm@42910
   755
wenzelm@42910
   756
lemma "Seq x xs \<noteq> xs"
wenzelm@42910
   757
proof (induct xs arbitrary: x)
wenzelm@42910
   758
  case Empty
wenzelm@42910
   759
  txt {* This case can be proved using the simplifier: the freeness
wenzelm@42910
   760
    properties of the datatype are already declared as @{attribute
wenzelm@42910
   761
    simp} rules. *}
wenzelm@42910
   762
  show "Seq x Empty \<noteq> Empty"
wenzelm@42910
   763
    by simp
wenzelm@42910
   764
next
wenzelm@42910
   765
  case (Seq y ys)
wenzelm@42910
   766
  txt {* The step case is proved similarly. *}
wenzelm@42910
   767
  show "Seq x (Seq y ys) \<noteq> Seq y ys"
wenzelm@42910
   768
    using `Seq y ys \<noteq> ys` by simp
wenzelm@42910
   769
qed
wenzelm@42910
   770
wenzelm@42910
   771
text {* Here is a more succinct version of the same proof: *}
wenzelm@42910
   772
wenzelm@42910
   773
lemma "Seq x xs \<noteq> xs"
wenzelm@42910
   774
  by (induct xs arbitrary: x) simp_all
wenzelm@42910
   775
wenzelm@42910
   776
wenzelm@26849
   777
section {* Records \label{sec:hol-record} *}
wenzelm@26849
   778
wenzelm@26849
   779
text {*
wenzelm@26849
   780
  In principle, records merely generalize the concept of tuples, where
wenzelm@26849
   781
  components may be addressed by labels instead of just position.  The
wenzelm@26849
   782
  logical infrastructure of records in Isabelle/HOL is slightly more
wenzelm@26849
   783
  advanced, though, supporting truly extensible record schemes.  This
wenzelm@26849
   784
  admits operations that are polymorphic with respect to record
wenzelm@26849
   785
  extension, yielding ``object-oriented'' effects like (single)
wenzelm@26849
   786
  inheritance.  See also \cite{NaraschewskiW-TPHOLs98} for more
wenzelm@26849
   787
  details on object-oriented verification and record subtyping in HOL.
wenzelm@26849
   788
*}
wenzelm@26849
   789
wenzelm@26849
   790
wenzelm@26849
   791
subsection {* Basic concepts *}
wenzelm@26849
   792
wenzelm@26849
   793
text {*
wenzelm@26849
   794
  Isabelle/HOL supports both \emph{fixed} and \emph{schematic} records
wenzelm@26849
   795
  at the level of terms and types.  The notation is as follows:
wenzelm@26849
   796
wenzelm@26849
   797
  \begin{center}
wenzelm@26849
   798
  \begin{tabular}{l|l|l}
wenzelm@26849
   799
    & record terms & record types \\ \hline
wenzelm@26849
   800
    fixed & @{text "\<lparr>x = a, y = b\<rparr>"} & @{text "\<lparr>x :: A, y :: B\<rparr>"} \\
wenzelm@26849
   801
    schematic & @{text "\<lparr>x = a, y = b, \<dots> = m\<rparr>"} &
wenzelm@26849
   802
      @{text "\<lparr>x :: A, y :: B, \<dots> :: M\<rparr>"} \\
wenzelm@26849
   803
  \end{tabular}
wenzelm@26849
   804
  \end{center}
wenzelm@26849
   805
wenzelm@26849
   806
  \noindent The ASCII representation of @{text "\<lparr>x = a\<rparr>"} is @{text
wenzelm@26849
   807
  "(| x = a |)"}.
wenzelm@26849
   808
wenzelm@26849
   809
  A fixed record @{text "\<lparr>x = a, y = b\<rparr>"} has field @{text x} of value
wenzelm@26849
   810
  @{text a} and field @{text y} of value @{text b}.  The corresponding
wenzelm@26849
   811
  type is @{text "\<lparr>x :: A, y :: B\<rparr>"}, assuming that @{text "a :: A"}
wenzelm@26849
   812
  and @{text "b :: B"}.
wenzelm@26849
   813
wenzelm@26849
   814
  A record scheme like @{text "\<lparr>x = a, y = b, \<dots> = m\<rparr>"} contains fields
wenzelm@26849
   815
  @{text x} and @{text y} as before, but also possibly further fields
wenzelm@26849
   816
  as indicated by the ``@{text "\<dots>"}'' notation (which is actually part
wenzelm@26849
   817
  of the syntax).  The improper field ``@{text "\<dots>"}'' of a record
wenzelm@26849
   818
  scheme is called the \emph{more part}.  Logically it is just a free
wenzelm@26849
   819
  variable, which is occasionally referred to as ``row variable'' in
wenzelm@26849
   820
  the literature.  The more part of a record scheme may be
wenzelm@26849
   821
  instantiated by zero or more further components.  For example, the
wenzelm@26849
   822
  previous scheme may get instantiated to @{text "\<lparr>x = a, y = b, z =
wenzelm@26852
   823
  c, \<dots> = m'\<rparr>"}, where @{text m'} refers to a different more part.
wenzelm@26849
   824
  Fixed records are special instances of record schemes, where
wenzelm@26849
   825
  ``@{text "\<dots>"}'' is properly terminated by the @{text "() :: unit"}
wenzelm@26849
   826
  element.  In fact, @{text "\<lparr>x = a, y = b\<rparr>"} is just an abbreviation
wenzelm@26849
   827
  for @{text "\<lparr>x = a, y = b, \<dots> = ()\<rparr>"}.
wenzelm@42123
   828
wenzelm@26849
   829
  \medskip Two key observations make extensible records in a simply
wenzelm@26849
   830
  typed language like HOL work out:
wenzelm@26849
   831
wenzelm@26849
   832
  \begin{enumerate}
wenzelm@26849
   833
wenzelm@26849
   834
  \item the more part is internalized, as a free term or type
wenzelm@26849
   835
  variable,
wenzelm@26849
   836
wenzelm@26852
   837
  \item field names are externalized, they cannot be accessed within
wenzelm@26852
   838
  the logic as first-class values.
wenzelm@26849
   839
wenzelm@26849
   840
  \end{enumerate}
wenzelm@26849
   841
wenzelm@26849
   842
  \medskip In Isabelle/HOL record types have to be defined explicitly,
wenzelm@26849
   843
  fixing their field names and types, and their (optional) parent
wenzelm@26849
   844
  record.  Afterwards, records may be formed using above syntax, while
wenzelm@26849
   845
  obeying the canonical order of fields as given by their declaration.
wenzelm@26849
   846
  The record package provides several standard operations like
wenzelm@26849
   847
  selectors and updates.  The common setup for various generic proof
wenzelm@26849
   848
  tools enable succinct reasoning patterns.  See also the Isabelle/HOL
wenzelm@26849
   849
  tutorial \cite{isabelle-hol-book} for further instructions on using
wenzelm@26849
   850
  records in practice.
wenzelm@26849
   851
*}
wenzelm@26849
   852
wenzelm@26849
   853
wenzelm@26849
   854
subsection {* Record specifications *}
wenzelm@26849
   855
wenzelm@26849
   856
text {*
wenzelm@26849
   857
  \begin{matharray}{rcl}
wenzelm@28761
   858
    @{command_def (HOL) "record"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@26849
   859
  \end{matharray}
wenzelm@26849
   860
wenzelm@55112
   861
  @{rail \<open>
wenzelm@55029
   862
    @@{command (HOL) record} @{syntax typespec_sorts} '=' \<newline>
wenzelm@46494
   863
      (@{syntax type} '+')? (constdecl +)
wenzelm@46494
   864
    ;
wenzelm@46494
   865
    constdecl: @{syntax name} '::' @{syntax type} @{syntax mixfix}?
wenzelm@55112
   866
  \<close>}
wenzelm@26849
   867
wenzelm@28760
   868
  \begin{description}
wenzelm@26849
   869
wenzelm@28760
   870
  \item @{command (HOL) "record"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t = \<tau> + c\<^sub>1 :: \<sigma>\<^sub>1
wenzelm@28760
   871
  \<dots> c\<^sub>n :: \<sigma>\<^sub>n"} defines extensible record type @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"},
wenzelm@26849
   872
  derived from the optional parent record @{text "\<tau>"} by adding new
wenzelm@26849
   873
  field components @{text "c\<^sub>i :: \<sigma>\<^sub>i"} etc.
wenzelm@26849
   874
wenzelm@26849
   875
  The type variables of @{text "\<tau>"} and @{text "\<sigma>\<^sub>i"} need to be
wenzelm@26849
   876
  covered by the (distinct) parameters @{text "\<alpha>\<^sub>1, \<dots>,
wenzelm@26849
   877
  \<alpha>\<^sub>m"}.  Type constructor @{text t} has to be new, while @{text
wenzelm@26849
   878
  \<tau>} needs to specify an instance of an existing record type.  At
wenzelm@26849
   879
  least one new field @{text "c\<^sub>i"} has to be specified.
wenzelm@26849
   880
  Basically, field names need to belong to a unique record.  This is
wenzelm@26849
   881
  not a real restriction in practice, since fields are qualified by
wenzelm@26849
   882
  the record name internally.
wenzelm@26849
   883
wenzelm@26849
   884
  The parent record specification @{text \<tau>} is optional; if omitted
wenzelm@26849
   885
  @{text t} becomes a root record.  The hierarchy of all records
wenzelm@26849
   886
  declared within a theory context forms a forest structure, i.e.\ a
wenzelm@26849
   887
  set of trees starting with a root record each.  There is no way to
wenzelm@26849
   888
  merge multiple parent records!
wenzelm@26849
   889
wenzelm@26849
   890
  For convenience, @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"} is made a
wenzelm@26849
   891
  type abbreviation for the fixed record type @{text "\<lparr>c\<^sub>1 ::
wenzelm@26849
   892
  \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n\<rparr>"}, likewise is @{text
wenzelm@26849
   893
  "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m, \<zeta>) t_scheme"} made an abbreviation for
wenzelm@26849
   894
  @{text "\<lparr>c\<^sub>1 :: \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n, \<dots> ::
wenzelm@26849
   895
  \<zeta>\<rparr>"}.
wenzelm@26849
   896
wenzelm@28760
   897
  \end{description}
wenzelm@26849
   898
*}
wenzelm@26849
   899
wenzelm@26849
   900
wenzelm@26849
   901
subsection {* Record operations *}
wenzelm@26849
   902
wenzelm@26849
   903
text {*
wenzelm@26849
   904
  Any record definition of the form presented above produces certain
wenzelm@26849
   905
  standard operations.  Selectors and updates are provided for any
wenzelm@26849
   906
  field, including the improper one ``@{text more}''.  There are also
wenzelm@26849
   907
  cumulative record constructor functions.  To simplify the
wenzelm@26849
   908
  presentation below, we assume for now that @{text "(\<alpha>\<^sub>1, \<dots>,
wenzelm@26849
   909
  \<alpha>\<^sub>m) t"} is a root record with fields @{text "c\<^sub>1 ::
wenzelm@26849
   910
  \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n"}.
wenzelm@26849
   911
wenzelm@26849
   912
  \medskip \textbf{Selectors} and \textbf{updates} are available for
wenzelm@26849
   913
  any field (including ``@{text more}''):
wenzelm@26849
   914
wenzelm@26849
   915
  \begin{matharray}{lll}
wenzelm@26852
   916
    @{text "c\<^sub>i"} & @{text "::"} & @{text "\<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<sigma>\<^sub>i"} \\
wenzelm@26852
   917
    @{text "c\<^sub>i_update"} & @{text "::"} & @{text "\<sigma>\<^sub>i \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
wenzelm@26849
   918
  \end{matharray}
wenzelm@26849
   919
wenzelm@26849
   920
  There is special syntax for application of updates: @{text "r\<lparr>x :=
wenzelm@26849
   921
  a\<rparr>"} abbreviates term @{text "x_update a r"}.  Further notation for
wenzelm@26849
   922
  repeated updates is also available: @{text "r\<lparr>x := a\<rparr>\<lparr>y := b\<rparr>\<lparr>z :=
wenzelm@26849
   923
  c\<rparr>"} may be written @{text "r\<lparr>x := a, y := b, z := c\<rparr>"}.  Note that
wenzelm@26849
   924
  because of postfix notation the order of fields shown here is
wenzelm@26849
   925
  reverse than in the actual term.  Since repeated updates are just
wenzelm@26849
   926
  function applications, fields may be freely permuted in @{text "\<lparr>x
wenzelm@26849
   927
  := a, y := b, z := c\<rparr>"}, as far as logical equality is concerned.
wenzelm@26849
   928
  Thus commutativity of independent updates can be proven within the
wenzelm@26849
   929
  logic for any two fields, but not as a general theorem.
wenzelm@26849
   930
wenzelm@26849
   931
  \medskip The \textbf{make} operation provides a cumulative record
wenzelm@26849
   932
  constructor function:
wenzelm@26849
   933
wenzelm@26849
   934
  \begin{matharray}{lll}
wenzelm@26852
   935
    @{text "t.make"} & @{text "::"} & @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26849
   936
  \end{matharray}
wenzelm@26849
   937
wenzelm@26849
   938
  \medskip We now reconsider the case of non-root records, which are
wenzelm@26849
   939
  derived of some parent.  In general, the latter may depend on
wenzelm@26849
   940
  another parent as well, resulting in a list of \emph{ancestor
wenzelm@26849
   941
  records}.  Appending the lists of fields of all ancestors results in
wenzelm@26849
   942
  a certain field prefix.  The record package automatically takes care
wenzelm@26849
   943
  of this by lifting operations over this context of ancestor fields.
wenzelm@26849
   944
  Assuming that @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"} has ancestor
wenzelm@26849
   945
  fields @{text "b\<^sub>1 :: \<rho>\<^sub>1, \<dots>, b\<^sub>k :: \<rho>\<^sub>k"},
wenzelm@26849
   946
  the above record operations will get the following types:
wenzelm@26849
   947
wenzelm@26852
   948
  \medskip
wenzelm@26852
   949
  \begin{tabular}{lll}
wenzelm@26852
   950
    @{text "c\<^sub>i"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<sigma>\<^sub>i"} \\
wenzelm@42123
   951
    @{text "c\<^sub>i_update"} & @{text "::"} & @{text "\<sigma>\<^sub>i \<Rightarrow>
wenzelm@26852
   952
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow>
wenzelm@26852
   953
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
wenzelm@26852
   954
    @{text "t.make"} & @{text "::"} & @{text "\<rho>\<^sub>1 \<Rightarrow> \<dots> \<rho>\<^sub>k \<Rightarrow> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow>
wenzelm@26852
   955
      \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26852
   956
  \end{tabular}
wenzelm@26852
   957
  \medskip
wenzelm@26849
   958
wenzelm@26852
   959
  \noindent Some further operations address the extension aspect of a
wenzelm@26849
   960
  derived record scheme specifically: @{text "t.fields"} produces a
wenzelm@26849
   961
  record fragment consisting of exactly the new fields introduced here
wenzelm@26849
   962
  (the result may serve as a more part elsewhere); @{text "t.extend"}
wenzelm@26849
   963
  takes a fixed record and adds a given more part; @{text
wenzelm@26849
   964
  "t.truncate"} restricts a record scheme to a fixed record.
wenzelm@26849
   965
wenzelm@26852
   966
  \medskip
wenzelm@26852
   967
  \begin{tabular}{lll}
wenzelm@26852
   968
    @{text "t.fields"} & @{text "::"} & @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26852
   969
    @{text "t.extend"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr> \<Rightarrow>
wenzelm@26852
   970
      \<zeta> \<Rightarrow> \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
wenzelm@26852
   971
    @{text "t.truncate"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
wenzelm@26852
   972
  \end{tabular}
wenzelm@26852
   973
  \medskip
wenzelm@26849
   974
wenzelm@26849
   975
  \noindent Note that @{text "t.make"} and @{text "t.fields"} coincide
wenzelm@26849
   976
  for root records.
wenzelm@26849
   977
*}
wenzelm@26849
   978
wenzelm@26849
   979
wenzelm@26849
   980
subsection {* Derived rules and proof tools *}
wenzelm@26849
   981
wenzelm@26849
   982
text {*
wenzelm@26849
   983
  The record package proves several results internally, declaring
wenzelm@26849
   984
  these facts to appropriate proof tools.  This enables users to
wenzelm@26849
   985
  reason about record structures quite conveniently.  Assume that
wenzelm@26849
   986
  @{text t} is a record type as specified above.
wenzelm@26849
   987
wenzelm@26849
   988
  \begin{enumerate}
wenzelm@42123
   989
wenzelm@26849
   990
  \item Standard conversions for selectors or updates applied to
wenzelm@26849
   991
  record constructor terms are made part of the default Simplifier
wenzelm@26849
   992
  context; thus proofs by reduction of basic operations merely require
wenzelm@26849
   993
  the @{method simp} method without further arguments.  These rules
wenzelm@26849
   994
  are available as @{text "t.simps"}, too.
wenzelm@42123
   995
wenzelm@26849
   996
  \item Selectors applied to updated records are automatically reduced
wenzelm@26849
   997
  by an internal simplification procedure, which is also part of the
wenzelm@26849
   998
  standard Simplifier setup.
wenzelm@26849
   999
wenzelm@26849
  1000
  \item Inject equations of a form analogous to @{prop "(x, y) = (x',
wenzelm@26849
  1001
  y') \<equiv> x = x' \<and> y = y'"} are declared to the Simplifier and Classical
wenzelm@26849
  1002
  Reasoner as @{attribute iff} rules.  These rules are available as
wenzelm@26849
  1003
  @{text "t.iffs"}.
wenzelm@26849
  1004
wenzelm@26849
  1005
  \item The introduction rule for record equality analogous to @{text
wenzelm@26849
  1006
  "x r = x r' \<Longrightarrow> y r = y r' \<dots> \<Longrightarrow> r = r'"} is declared to the Simplifier,
wenzelm@26849
  1007
  and as the basic rule context as ``@{attribute intro}@{text "?"}''.
wenzelm@26849
  1008
  The rule is called @{text "t.equality"}.
wenzelm@26849
  1009
wenzelm@26849
  1010
  \item Representations of arbitrary record expressions as canonical
wenzelm@26849
  1011
  constructor terms are provided both in @{method cases} and @{method
wenzelm@26849
  1012
  induct} format (cf.\ the generic proof methods of the same name,
wenzelm@26849
  1013
  \secref{sec:cases-induct}).  Several variations are available, for
wenzelm@26849
  1014
  fixed records, record schemes, more parts etc.
wenzelm@42123
  1015
wenzelm@26849
  1016
  The generic proof methods are sufficiently smart to pick the most
wenzelm@26849
  1017
  sensible rule according to the type of the indicated record
wenzelm@26849
  1018
  expression: users just need to apply something like ``@{text "(cases
wenzelm@26849
  1019
  r)"}'' to a certain proof problem.
wenzelm@26849
  1020
wenzelm@26849
  1021
  \item The derived record operations @{text "t.make"}, @{text
wenzelm@26849
  1022
  "t.fields"}, @{text "t.extend"}, @{text "t.truncate"} are \emph{not}
wenzelm@26849
  1023
  treated automatically, but usually need to be expanded by hand,
wenzelm@26849
  1024
  using the collective fact @{text "t.defs"}.
wenzelm@26849
  1025
wenzelm@26849
  1026
  \end{enumerate}
wenzelm@26849
  1027
*}
wenzelm@26849
  1028
wenzelm@26849
  1029
wenzelm@42911
  1030
subsubsection {* Examples *}
wenzelm@42911
  1031
wenzelm@42911
  1032
text {* See @{file "~~/src/HOL/ex/Records.thy"}, for example. *}
wenzelm@42911
  1033
wenzelm@42908
  1034
section {* Typedef axiomatization \label{sec:hol-typedef} *}
wenzelm@42908
  1035
wenzelm@46280
  1036
text {*
wenzelm@46280
  1037
  \begin{matharray}{rcl}
wenzelm@46280
  1038
    @{command_def (HOL) "typedef"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
wenzelm@46280
  1039
  \end{matharray}
wenzelm@46280
  1040
wenzelm@46280
  1041
  A Gordon/HOL-style type definition is a certain axiom scheme that
wenzelm@46280
  1042
  identifies a new type with a subset of an existing type.  More
wenzelm@42908
  1043
  precisely, the new type is defined by exhibiting an existing type
wenzelm@42908
  1044
  @{text \<tau>}, a set @{text "A :: \<tau> set"}, and a theorem that proves
wenzelm@42908
  1045
  @{prop "\<exists>x. x \<in> A"}.  Thus @{text A} is a non-empty subset of @{text
wenzelm@42908
  1046
  \<tau>}, and the new type denotes this subset.  New functions are
wenzelm@42908
  1047
  postulated that establish an isomorphism between the new type and
wenzelm@42908
  1048
  the subset.  In general, the type @{text \<tau>} may involve type
wenzelm@42908
  1049
  variables @{text "\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n"} which means that the type definition
wenzelm@42908
  1050
  produces a type constructor @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} depending on
wenzelm@42908
  1051
  those type arguments.
wenzelm@42908
  1052
wenzelm@57480
  1053
  The axiomatization can be considered a ``definition'' in the sense of the
wenzelm@57480
  1054
  particular set-theoretic interpretation of HOL \cite{pitts93}, where the
wenzelm@57480
  1055
  universe of types is required to be downwards-closed wrt.\ arbitrary
wenzelm@57480
  1056
  non-empty subsets. Thus genuinely new types introduced by @{command
wenzelm@57480
  1057
  "typedef"} stay within the range of HOL models by construction.
wenzelm@57480
  1058
wenzelm@57480
  1059
  In contrast, the command @{command_ref type_synonym} from Isabelle/Pure
wenzelm@57480
  1060
  merely introduces syntactic abbreviations, without any logical
wenzelm@57480
  1061
  significance. Thus it is more faithful to the idea of a genuine type
wenzelm@57480
  1062
  definition, but less powerful in practice.
wenzelm@47859
  1063
wenzelm@55112
  1064
  @{rail \<open>
wenzelm@49836
  1065
    @@{command (HOL) typedef} abs_type '=' rep_set
wenzelm@26849
  1066
    ;
wenzelm@42908
  1067
    abs_type: @{syntax typespec_sorts} @{syntax mixfix}?
wenzelm@42908
  1068
    ;
wenzelm@42908
  1069
    rep_set: @{syntax term} (@'morphisms' @{syntax name} @{syntax name})?
wenzelm@55112
  1070
  \<close>}
wenzelm@26849
  1071
wenzelm@28760
  1072
  \begin{description}
wenzelm@26849
  1073
wenzelm@57480
  1074
  \item @{command (HOL) "typedef"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t = A"} produces an
wenzelm@57487
  1075
  axiomatization (\secref{sec:axiomatizations}) for a type definition in the
wenzelm@57480
  1076
  background theory of the current context, depending on a non-emptiness
wenzelm@57480
  1077
  result of the set @{text A} that needs to be proven here. The set @{text
wenzelm@57480
  1078
  A} may contain type variables @{text "\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n"} as specified on the
wenzelm@57480
  1079
  LHS, but no term variables.
wenzelm@42908
  1080
wenzelm@42908
  1081
  Even though a local theory specification, the newly introduced type
wenzelm@42908
  1082
  constructor cannot depend on parameters or assumptions of the
wenzelm@42908
  1083
  context: this is structurally impossible in HOL.  In contrast, the
wenzelm@42908
  1084
  non-emptiness proof may use local assumptions in unusual situations,
wenzelm@42908
  1085
  which could result in different interpretations in target contexts:
wenzelm@42908
  1086
  the meaning of the bijection between the representing set @{text A}
wenzelm@42908
  1087
  and the new type @{text t} may then change in different application
wenzelm@42908
  1088
  contexts.
wenzelm@42908
  1089
wenzelm@49836
  1090
  For @{command (HOL) "typedef"}~@{text "t = A"} the newly introduced
wenzelm@49836
  1091
  type @{text t} is accompanied by a pair of morphisms to relate it to
wenzelm@49836
  1092
  the representing set over the old type.  By default, the injection
wenzelm@49836
  1093
  from type to set is called @{text Rep_t} and its inverse @{text
wenzelm@49836
  1094
  Abs_t}: An explicit @{keyword (HOL) "morphisms"} specification
wenzelm@49836
  1095
  allows to provide alternative names.
wenzelm@26849
  1096
wenzelm@42908
  1097
  The core axiomatization uses the locale predicate @{const
wenzelm@42908
  1098
  type_definition} as defined in Isabelle/HOL.  Various basic
wenzelm@42908
  1099
  consequences of that are instantiated accordingly, re-using the
wenzelm@42908
  1100
  locale facts with names derived from the new type constructor.  Thus
wenzelm@42908
  1101
  the generic @{thm type_definition.Rep} is turned into the specific
wenzelm@42908
  1102
  @{text "Rep_t"}, for example.
wenzelm@42908
  1103
wenzelm@42908
  1104
  Theorems @{thm type_definition.Rep}, @{thm
wenzelm@42908
  1105
  type_definition.Rep_inverse}, and @{thm type_definition.Abs_inverse}
wenzelm@42908
  1106
  provide the most basic characterization as a corresponding
wenzelm@42908
  1107
  injection/surjection pair (in both directions).  The derived rules
wenzelm@42908
  1108
  @{thm type_definition.Rep_inject} and @{thm
wenzelm@42908
  1109
  type_definition.Abs_inject} provide a more convenient version of
wenzelm@42908
  1110
  injectivity, suitable for automated proof tools (e.g.\ in
wenzelm@42908
  1111
  declarations involving @{attribute simp} or @{attribute iff}).
wenzelm@42908
  1112
  Furthermore, the rules @{thm type_definition.Rep_cases}~/ @{thm
wenzelm@42908
  1113
  type_definition.Rep_induct}, and @{thm type_definition.Abs_cases}~/
wenzelm@42908
  1114
  @{thm type_definition.Abs_induct} provide alternative views on
wenzelm@42908
  1115
  surjectivity.  These rules are already declared as set or type rules
wenzelm@42908
  1116
  for the generic @{method cases} and @{method induct} methods,
wenzelm@42908
  1117
  respectively.
wenzelm@42908
  1118
wenzelm@28760
  1119
  \end{description}
wenzelm@42908
  1120
*}
wenzelm@42908
  1121
wenzelm@42908
  1122
subsubsection {* Examples *}
wenzelm@42908
  1123
wenzelm@42908
  1124
text {* Type definitions permit the introduction of abstract data
wenzelm@42908
  1125
  types in a safe way, namely by providing models based on already
wenzelm@42908
  1126
  existing types.  Given some abstract axiomatic description @{text P}
wenzelm@42908
  1127
  of a type, this involves two steps:
wenzelm@42908
  1128
wenzelm@42908
  1129
  \begin{enumerate}
wenzelm@42908
  1130
wenzelm@42908
  1131
  \item Find an appropriate type @{text \<tau>} and subset @{text A} which
wenzelm@42908
  1132
  has the desired properties @{text P}, and make a type definition
wenzelm@42908
  1133
  based on this representation.
wenzelm@42908
  1134
wenzelm@42908
  1135
  \item Prove that @{text P} holds for @{text \<tau>} by lifting @{text P}
wenzelm@42908
  1136
  from the representation.
wenzelm@26849
  1137
wenzelm@42908
  1138
  \end{enumerate}
wenzelm@42908
  1139
wenzelm@42908
  1140
  You can later forget about the representation and work solely in
wenzelm@42908
  1141
  terms of the abstract properties @{text P}.
wenzelm@42908
  1142
wenzelm@42908
  1143
  \medskip The following trivial example pulls a three-element type
wenzelm@42908
  1144
  into existence within the formal logical environment of HOL. *}
wenzelm@42908
  1145
wenzelm@49834
  1146
typedef three = "{(True, True), (True, False), (False, True)}"
wenzelm@42908
  1147
  by blast
wenzelm@42908
  1148
wenzelm@42908
  1149
definition "One = Abs_three (True, True)"
wenzelm@42908
  1150
definition "Two = Abs_three (True, False)"
wenzelm@42908
  1151
definition "Three = Abs_three (False, True)"
wenzelm@42908
  1152
wenzelm@42908
  1153
lemma three_distinct: "One \<noteq> Two"  "One \<noteq> Three"  "Two \<noteq> Three"
wenzelm@49812
  1154
  by (simp_all add: One_def Two_def Three_def Abs_three_inject)
wenzelm@42908
  1155
wenzelm@42908
  1156
lemma three_cases:
wenzelm@42908
  1157
  fixes x :: three obtains "x = One" | "x = Two" | "x = Three"
wenzelm@49812
  1158
  by (cases x) (auto simp: One_def Two_def Three_def Abs_three_inject)
wenzelm@42908
  1159
wenzelm@42908
  1160
text {* Note that such trivial constructions are better done with
blanchet@58305
  1161
  derived specification mechanisms such as @{command datatype_new}: *}
blanchet@58305
  1162
blanchet@58305
  1163
datatype_new three' = One' | Two' | Three'
wenzelm@42908
  1164
wenzelm@42908
  1165
text {* This avoids re-doing basic definitions and proofs from the
wenzelm@42908
  1166
  primitive @{command typedef} above. *}
wenzelm@26849
  1167
wenzelm@26849
  1168
wenzelm@50109
  1169
haftmann@41396
  1170
section {* Functorial structure of types *}
haftmann@41396
  1171
haftmann@41396
  1172
text {*
haftmann@41396
  1173
  \begin{matharray}{rcl}
blanchet@55467
  1174
    @{command_def (HOL) "functor"} & : & @{text "local_theory \<rightarrow> proof(prove)"}
haftmann@41396
  1175
  \end{matharray}
haftmann@41396
  1176
wenzelm@55112
  1177
  @{rail \<open>
blanchet@55467
  1178
    @@{command (HOL) functor} (@{syntax name} ':')? @{syntax term}
wenzelm@55112
  1179
  \<close>}
haftmann@41396
  1180
haftmann@41396
  1181
  \begin{description}
haftmann@41396
  1182
blanchet@55467
  1183
  \item @{command (HOL) "functor"}~@{text "prefix: m"} allows to
wenzelm@42617
  1184
  prove and register properties about the functorial structure of type
wenzelm@42617
  1185
  constructors.  These properties then can be used by other packages
wenzelm@42617
  1186
  to deal with those type constructors in certain type constructions.
wenzelm@42617
  1187
  Characteristic theorems are noted in the current local theory.  By
wenzelm@42617
  1188
  default, they are prefixed with the base name of the type
wenzelm@42617
  1189
  constructor, an explicit prefix can be given alternatively.
haftmann@41396
  1190
haftmann@41396
  1191
  The given term @{text "m"} is considered as \emph{mapper} for the
haftmann@41396
  1192
  corresponding type constructor and must conform to the following
haftmann@41396
  1193
  type pattern:
haftmann@41396
  1194
haftmann@41396
  1195
  \begin{matharray}{lll}
haftmann@41396
  1196
    @{text "m"} & @{text "::"} &
wenzelm@53015
  1197
      @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>k \<Rightarrow> (\<^vec>\<alpha>\<^sub>n) t \<Rightarrow> (\<^vec>\<beta>\<^sub>n) t"} \\
haftmann@41396
  1198
  \end{matharray}
haftmann@41396
  1199
haftmann@41396
  1200
  \noindent where @{text t} is the type constructor, @{text
wenzelm@53015
  1201
  "\<^vec>\<alpha>\<^sub>n"} and @{text "\<^vec>\<beta>\<^sub>n"} are distinct
wenzelm@53015
  1202
  type variables free in the local theory and @{text "\<sigma>\<^sub>1"},
wenzelm@53015
  1203
  \ldots, @{text "\<sigma>\<^sub>k"} is a subsequence of @{text "\<alpha>\<^sub>1 \<Rightarrow>
wenzelm@53015
  1204
  \<beta>\<^sub>1"}, @{text "\<beta>\<^sub>1 \<Rightarrow> \<alpha>\<^sub>1"}, \ldots,
wenzelm@53015
  1205
  @{text "\<alpha>\<^sub>n \<Rightarrow> \<beta>\<^sub>n"}, @{text "\<beta>\<^sub>n \<Rightarrow>
wenzelm@53015
  1206
  \<alpha>\<^sub>n"}.
haftmann@41396
  1207
haftmann@41396
  1208
  \end{description}
haftmann@41396
  1209
*}
haftmann@41396
  1210
wenzelm@47859
  1211
wenzelm@50109
  1212
section {* Quotient types *}
wenzelm@50109
  1213
wenzelm@50109
  1214
text {*
wenzelm@50109
  1215
  \begin{matharray}{rcl}
wenzelm@50109
  1216
    @{command_def (HOL) "quotient_type"} & : & @{text "local_theory \<rightarrow> proof(prove)"}\\
wenzelm@50109
  1217
    @{command_def (HOL) "quotient_definition"} & : & @{text "local_theory \<rightarrow> proof(prove)"}\\
wenzelm@50109
  1218
    @{command_def (HOL) "print_quotmapsQ3"} & : & @{text "context \<rightarrow>"}\\
wenzelm@50109
  1219
    @{command_def (HOL) "print_quotientsQ3"} & : & @{text "context \<rightarrow>"}\\
wenzelm@50109
  1220
    @{command_def (HOL) "print_quotconsts"} & : & @{text "context \<rightarrow>"}\\
wenzelm@50109
  1221
    @{method_def (HOL) "lifting"} & : & @{text method} \\
wenzelm@50109
  1222
    @{method_def (HOL) "lifting_setup"} & : & @{text method} \\
wenzelm@50109
  1223
    @{method_def (HOL) "descending"} & : & @{text method} \\
wenzelm@50109
  1224
    @{method_def (HOL) "descending_setup"} & : & @{text method} \\
wenzelm@50109
  1225
    @{method_def (HOL) "partiality_descending"} & : & @{text method} \\
wenzelm@50109
  1226
    @{method_def (HOL) "partiality_descending_setup"} & : & @{text method} \\
wenzelm@50109
  1227
    @{method_def (HOL) "regularize"} & : & @{text method} \\
wenzelm@50109
  1228
    @{method_def (HOL) "injection"} & : & @{text method} \\
wenzelm@50109
  1229
    @{method_def (HOL) "cleaning"} & : & @{text method} \\
wenzelm@50109
  1230
    @{attribute_def (HOL) "quot_thm"} & : & @{text attribute} \\
wenzelm@50109
  1231
    @{attribute_def (HOL) "quot_lifted"} & : & @{text attribute} \\
wenzelm@50109
  1232
    @{attribute_def (HOL) "quot_respect"} & : & @{text attribute} \\
wenzelm@50109
  1233
    @{attribute_def (HOL) "quot_preserve"} & : & @{text attribute} \\
wenzelm@50109
  1234
  \end{matharray}
wenzelm@50109
  1235
wenzelm@50109
  1236
  The quotient package defines a new quotient type given a raw type
kuncar@54334
  1237
  and a partial equivalence relation. The package also historically 
kuncar@54334
  1238
  includes automation for transporting definitions and theorems. 
kuncar@54334
  1239
  But most of this automation was superseded by the Lifting and Transfer
kuncar@54334
  1240
  packages. The user should consider using these two new packages for
kuncar@54334
  1241
  lifting definitions and transporting theorems.
wenzelm@50109
  1242
wenzelm@55112
  1243
  @{rail \<open>
wenzelm@55112
  1244
    @@{command (HOL) quotient_type} (spec)
wenzelm@55112
  1245
    ;
wenzelm@55029
  1246
    spec: @{syntax typespec} @{syntax mixfix}? '=' \<newline>
wenzelm@55029
  1247
     @{syntax type} '/' ('partial' ':')? @{syntax term} \<newline>
wenzelm@55112
  1248
     (@'morphisms' @{syntax name} @{syntax name})? (@'parametric' @{syntax thmref})?
wenzelm@55112
  1249
  \<close>}
wenzelm@55112
  1250
wenzelm@55112
  1251
  @{rail \<open>
wenzelm@55029
  1252
    @@{command (HOL) quotient_definition} constdecl? @{syntax thmdecl}? \<newline>
wenzelm@55112
  1253
    @{syntax term} 'is' @{syntax term}
wenzelm@55112
  1254
    ;
wenzelm@50109
  1255
    constdecl: @{syntax name} ('::' @{syntax type})? @{syntax mixfix}?
wenzelm@55112
  1256
  \<close>}
wenzelm@55112
  1257
wenzelm@55112
  1258
  @{rail \<open>
wenzelm@50109
  1259
    @@{method (HOL) lifting} @{syntax thmrefs}?
wenzelm@50109
  1260
    ;
wenzelm@50109
  1261
    @@{method (HOL) lifting_setup} @{syntax thmrefs}?
wenzelm@55112
  1262
  \<close>}
wenzelm@50109
  1263
wenzelm@50109
  1264
  \begin{description}
wenzelm@50109
  1265
kuncar@54334
  1266
  \item @{command (HOL) "quotient_type"} defines a new quotient type @{text \<tau>}. The
wenzelm@50109
  1267
  injection from a quotient type to a raw type is called @{text
kuncar@54334
  1268
  rep_\<tau>}, its inverse @{text abs_\<tau>} unless explicit @{keyword (HOL)
wenzelm@50109
  1269
  "morphisms"} specification provides alternative names. @{command
wenzelm@50109
  1270
  (HOL) "quotient_type"} requires the user to prove that the relation
wenzelm@50109
  1271
  is an equivalence relation (predicate @{text equivp}), unless the
Christian@52895
  1272
  user specifies explicitly @{text partial} in which case the
wenzelm@50109
  1273
  obligation is @{text part_equivp}.  A quotient defined with @{text
wenzelm@50109
  1274
  partial} is weaker in the sense that less things can be proved
wenzelm@50109
  1275
  automatically.
wenzelm@50109
  1276
kuncar@54334
  1277
  The command internally proves a Quotient theorem and sets up the Lifting
kuncar@54334
  1278
  package by the command @{command (HOL) setup_lifting}. Thus the Lifting 
kuncar@54334
  1279
  and Transfer packages can be used also with quotient types defined by
kuncar@54334
  1280
  @{command (HOL) "quotient_type"} without any extra set-up. The parametricity 
kuncar@54334
  1281
  theorem for the equivalence relation R can be provided as an extra argument 
kuncar@54334
  1282
  of the command and is passed to the corresponding internal call of @{command (HOL) setup_lifting}.
kuncar@54334
  1283
  This theorem allows the Lifting package to generate a stronger transfer rule for equality.
kuncar@54334
  1284
  
kuncar@54334
  1285
  \end{description}
kuncar@54334
  1286
kuncar@54334
  1287
  The most of the rest of the package was superseded by the Lifting and Transfer
kuncar@54334
  1288
  packages. The user should consider using these two new packages for
kuncar@54334
  1289
  lifting definitions and transporting theorems.
kuncar@54334
  1290
kuncar@54334
  1291
  \begin{description}  
kuncar@54334
  1292
wenzelm@50109
  1293
  \item @{command (HOL) "quotient_definition"} defines a constant on
wenzelm@50109
  1294
  the quotient type.
wenzelm@50109
  1295
wenzelm@50109
  1296
  \item @{command (HOL) "print_quotmapsQ3"} prints quotient map
wenzelm@50109
  1297
  functions.
wenzelm@50109
  1298
wenzelm@50109
  1299
  \item @{command (HOL) "print_quotientsQ3"} prints quotients.
wenzelm@50109
  1300
wenzelm@50109
  1301
  \item @{command (HOL) "print_quotconsts"} prints quotient constants.
wenzelm@50109
  1302
wenzelm@50109
  1303
  \item @{method (HOL) "lifting"} and @{method (HOL) "lifting_setup"}
wenzelm@50109
  1304
    methods match the current goal with the given raw theorem to be
wenzelm@50109
  1305
    lifted producing three new subgoals: regularization, injection and
wenzelm@50109
  1306
    cleaning subgoals. @{method (HOL) "lifting"} tries to apply the
wenzelm@50109
  1307
    heuristics for automatically solving these three subgoals and
wenzelm@50109
  1308
    leaves only the subgoals unsolved by the heuristics to the user as
wenzelm@50109
  1309
    opposed to @{method (HOL) "lifting_setup"} which leaves the three
wenzelm@50109
  1310
    subgoals unsolved.
wenzelm@50109
  1311
wenzelm@50109
  1312
  \item @{method (HOL) "descending"} and @{method (HOL)
wenzelm@50109
  1313
    "descending_setup"} try to guess a raw statement that would lift
wenzelm@50109
  1314
    to the current subgoal. Such statement is assumed as a new subgoal
wenzelm@50109
  1315
    and @{method (HOL) "descending"} continues in the same way as
wenzelm@50109
  1316
    @{method (HOL) "lifting"} does. @{method (HOL) "descending"} tries
wenzelm@50109
  1317
    to solve the arising regularization, injection and cleaning
Christian@52895
  1318
    subgoals with the analogous method @{method (HOL)
wenzelm@50109
  1319
    "descending_setup"} which leaves the four unsolved subgoals.
wenzelm@50109
  1320
wenzelm@50109
  1321
  \item @{method (HOL) "partiality_descending"} finds the regularized
wenzelm@50109
  1322
    theorem that would lift to the current subgoal, lifts it and
wenzelm@50109
  1323
    leaves as a subgoal. This method can be used with partial
wenzelm@50109
  1324
    equivalence quotients where the non regularized statements would
wenzelm@50109
  1325
    not be true. @{method (HOL) "partiality_descending_setup"} leaves
wenzelm@50109
  1326
    the injection and cleaning subgoals unchanged.
wenzelm@50109
  1327
wenzelm@50109
  1328
  \item @{method (HOL) "regularize"} applies the regularization
wenzelm@50109
  1329
    heuristics to the current subgoal.
wenzelm@50109
  1330
wenzelm@50109
  1331
  \item @{method (HOL) "injection"} applies the injection heuristics
wenzelm@50109
  1332
    to the current goal using the stored quotient respectfulness
wenzelm@50109
  1333
    theorems.
wenzelm@50109
  1334
wenzelm@50109
  1335
  \item @{method (HOL) "cleaning"} applies the injection cleaning
wenzelm@50109
  1336
    heuristics to the current subgoal using the stored quotient
wenzelm@50109
  1337
    preservation theorems.
wenzelm@50109
  1338
wenzelm@50109
  1339
  \item @{attribute (HOL) quot_lifted} attribute tries to
wenzelm@50109
  1340
    automatically transport the theorem to the quotient type.
wenzelm@50109
  1341
    The attribute uses all the defined quotients types and quotient
wenzelm@50109
  1342
    constants often producing undesired results or theorems that
wenzelm@50109
  1343
    cannot be lifted.
wenzelm@50109
  1344
wenzelm@50109
  1345
  \item @{attribute (HOL) quot_respect} and @{attribute (HOL)
wenzelm@50109
  1346
    quot_preserve} attributes declare a theorem as a respectfulness
wenzelm@50109
  1347
    and preservation theorem respectively.  These are stored in the
wenzelm@50109
  1348
    local theory store and used by the @{method (HOL) "injection"}
wenzelm@50109
  1349
    and @{method (HOL) "cleaning"} methods respectively.
wenzelm@50109
  1350
wenzelm@50109
  1351
  \item @{attribute (HOL) quot_thm} declares that a certain theorem
wenzelm@50109
  1352
    is a quotient extension theorem. Quotient extension theorems
wenzelm@50109
  1353
    allow for quotienting inside container types. Given a polymorphic
wenzelm@50109
  1354
    type that serves as a container, a map function defined for this
blanchet@55467
  1355
    container using @{command (HOL) "functor"} and a relation
wenzelm@50109
  1356
    map defined for for the container type, the quotient extension
wenzelm@50109
  1357
    theorem should be @{term "Quotient3 R Abs Rep \<Longrightarrow> Quotient3
wenzelm@50109
  1358
    (rel_map R) (map Abs) (map Rep)"}. Quotient extension theorems
wenzelm@50109
  1359
    are stored in a database and are used all the steps of lifting
wenzelm@50109
  1360
    theorems.
wenzelm@50109
  1361
wenzelm@50109
  1362
  \end{description}
wenzelm@50109
  1363
*}
wenzelm@50109
  1364
wenzelm@50109
  1365
wenzelm@50109
  1366
section {* Definition by specification \label{sec:hol-specification} *}
wenzelm@50109
  1367
wenzelm@50109
  1368
text {*
wenzelm@50109
  1369
  \begin{matharray}{rcl}
wenzelm@50109
  1370
    @{command_def (HOL) "specification"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
wenzelm@50109
  1371
  \end{matharray}
wenzelm@50109
  1372
wenzelm@55112
  1373
  @{rail \<open>
wenzelm@56270
  1374
    @@{command (HOL) specification} '(' (decl +) ')' \<newline>
wenzelm@56270
  1375
      (@{syntax thmdecl}? @{syntax prop} +)
wenzelm@55112
  1376
    ;
wenzelm@56270
  1377
    decl: (@{syntax name} ':')? @{syntax term} ('(' @'overloaded' ')')?
wenzelm@55112
  1378
  \<close>}
wenzelm@50109
  1379
wenzelm@50109
  1380
  \begin{description}
wenzelm@50109
  1381
wenzelm@50109
  1382
  \item @{command (HOL) "specification"}~@{text "decls \<phi>"} sets up a
wenzelm@50109
  1383
  goal stating the existence of terms with the properties specified to
wenzelm@50109
  1384
  hold for the constants given in @{text decls}.  After finishing the
wenzelm@50109
  1385
  proof, the theory will be augmented with definitions for the given
wenzelm@50109
  1386
  constants, as well as with theorems stating the properties for these
wenzelm@50109
  1387
  constants.
wenzelm@50109
  1388
wenzelm@56270
  1389
  @{text decl} declares a constant to be defined by the
wenzelm@50109
  1390
  specification given.  The definition for the constant @{text c} is
wenzelm@50109
  1391
  bound to the name @{text c_def} unless a theorem name is given in
wenzelm@50109
  1392
  the declaration.  Overloaded constants should be declared as such.
wenzelm@50109
  1393
wenzelm@50109
  1394
  \end{description}
wenzelm@50109
  1395
*}
wenzelm@50109
  1396
wenzelm@56270
  1397
Christian@52895
  1398
section {* Adhoc overloading of constants *}
Christian@52895
  1399
Christian@52895
  1400
text {*
Christian@52895
  1401
  \begin{tabular}{rcll}
Christian@52895
  1402
  @{command_def "adhoc_overloading"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
Christian@52895
  1403
  @{command_def "no_adhoc_overloading"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
Christian@52895
  1404
  @{attribute_def "show_variants"} & : & @{text "attribute"} & default @{text false} \\
Christian@52895
  1405
  \end{tabular}
Christian@52895
  1406
Christian@52895
  1407
  \medskip
Christian@52895
  1408
Christian@52895
  1409
  Adhoc overloading allows to overload a constant depending on
Christian@52895
  1410
  its type. Typically this involves the introduction of an
Christian@52895
  1411
  uninterpreted constant (used for input and output) and the addition
Christian@52895
  1412
  of some variants (used internally). For examples see
Christian@52895
  1413
  @{file "~~/src/HOL/ex/Adhoc_Overloading_Examples.thy"} and
Christian@52895
  1414
  @{file "~~/src/HOL/Library/Monad_Syntax.thy"}.
Christian@52895
  1415
wenzelm@55112
  1416
  @{rail \<open>
Christian@52895
  1417
    (@@{command adhoc_overloading} | @@{command no_adhoc_overloading})
wenzelm@55112
  1418
      (@{syntax nameref} (@{syntax term} + ) + @'and')
wenzelm@55112
  1419
  \<close>}
Christian@52895
  1420
Christian@52895
  1421
  \begin{description}
Christian@52895
  1422
wenzelm@53015
  1423
  \item @{command "adhoc_overloading"}~@{text "c v\<^sub>1 ... v\<^sub>n"}
Christian@52895
  1424
  associates variants with an existing constant.
Christian@52895
  1425
Christian@52895
  1426
  \item @{command "no_adhoc_overloading"} is similar to
Christian@52895
  1427
  @{command "adhoc_overloading"}, but removes the specified variants
Christian@52895
  1428
  from the present context.
Christian@52895
  1429
  
Christian@52895
  1430
  \item @{attribute "show_variants"} controls printing of variants
Christian@52895
  1431
  of overloaded constants. If enabled, the internally used variants
Christian@52895
  1432
  are printed instead of their respective overloaded constants. This
Christian@52895
  1433
  is occasionally useful to check whether the system agrees with a
Christian@52895
  1434
  user's expectations about derived variants.
Christian@52895
  1435
Christian@52895
  1436
  \end{description}
Christian@52895
  1437
*}
wenzelm@50109
  1438
wenzelm@57507
  1439
wenzelm@50109
  1440
chapter {* Proof tools *}
wenzelm@50109
  1441
wenzelm@50109
  1442
section {* Adhoc tuples *}
wenzelm@50109
  1443
wenzelm@50109
  1444
text {*
wenzelm@50109
  1445
  \begin{matharray}{rcl}
wenzelm@50109
  1446
    @{attribute_def (HOL) split_format}@{text "\<^sup>*"} & : & @{text attribute} \\
wenzelm@50109
  1447
  \end{matharray}
wenzelm@50109
  1448
wenzelm@55112
  1449
  @{rail \<open>
wenzelm@50109
  1450
    @@{attribute (HOL) split_format} ('(' 'complete' ')')?
wenzelm@55112
  1451
  \<close>}
wenzelm@50109
  1452
wenzelm@50109
  1453
  \begin{description}
wenzelm@50109
  1454
wenzelm@50109
  1455
  \item @{attribute (HOL) split_format}\ @{text "(complete)"} causes
wenzelm@50109
  1456
  arguments in function applications to be represented canonically
wenzelm@50109
  1457
  according to their tuple type structure.
wenzelm@50109
  1458
wenzelm@50109
  1459
  Note that this operation tends to invent funny names for new local
wenzelm@50109
  1460
  parameters introduced.
wenzelm@50109
  1461
wenzelm@50109
  1462
  \end{description}
wenzelm@50109
  1463
*}
wenzelm@50109
  1464
wenzelm@50109
  1465
huffman@47821
  1466
section {* Transfer package *}
huffman@47821
  1467
huffman@47821
  1468
text {*
huffman@47821
  1469
  \begin{matharray}{rcl}
huffman@47821
  1470
    @{method_def (HOL) "transfer"} & : & @{text method} \\
huffman@47821
  1471
    @{method_def (HOL) "transfer'"} & : & @{text method} \\
huffman@47821
  1472
    @{method_def (HOL) "transfer_prover"} & : & @{text method} \\
kuncar@54334
  1473
    @{attribute_def (HOL) "Transfer.transferred"} & : & @{text attribute} \\
kuncar@54334
  1474
    @{attribute_def (HOL) "untransferred"} & : & @{text attribute} \\
huffman@47821
  1475
    @{attribute_def (HOL) "transfer_rule"} & : & @{text attribute} \\
kuncar@54334
  1476
    @{attribute_def (HOL) "transfer_domain_rule"} & : & @{text attribute} \\
huffman@47821
  1477
    @{attribute_def (HOL) "relator_eq"} & : & @{text attribute} \\
kuncar@54334
  1478
    @{attribute_def (HOL) "relator_domain"} & : & @{text attribute} \\
huffman@47821
  1479
  \end{matharray}
huffman@47821
  1480
huffman@47821
  1481
  \begin{description}
huffman@47821
  1482
huffman@47821
  1483
  \item @{method (HOL) "transfer"} method replaces the current subgoal
huffman@47821
  1484
    with a logically equivalent one that uses different types and
huffman@47821
  1485
    constants. The replacement of types and constants is guided by the
huffman@47821
  1486
    database of transfer rules. Goals are generalized over all free
huffman@47821
  1487
    variables by default; this is necessary for variables whose types
huffman@47821
  1488
    change, but can be overridden for specific variables with e.g.
huffman@47821
  1489
    @{text "transfer fixing: x y z"}.
huffman@47821
  1490
huffman@47821
  1491
  \item @{method (HOL) "transfer'"} is a variant of @{method (HOL)
huffman@47821
  1492
    transfer} that allows replacing a subgoal with one that is
huffman@47821
  1493
    logically stronger (rather than equivalent). For example, a
huffman@47821
  1494
    subgoal involving equality on a quotient type could be replaced
huffman@47821
  1495
    with a subgoal involving equality (instead of the corresponding
huffman@47821
  1496
    equivalence relation) on the underlying raw type.
huffman@47821
  1497
huffman@47821
  1498
  \item @{method (HOL) "transfer_prover"} method assists with proving
huffman@47821
  1499
    a transfer rule for a new constant, provided the constant is
huffman@47821
  1500
    defined in terms of other constants that already have transfer
huffman@47821
  1501
    rules. It should be applied after unfolding the constant
huffman@47821
  1502
    definitions.
huffman@47821
  1503
kuncar@54334
  1504
  \item @{attribute (HOL) "untransferred"} proves the same equivalent theorem
kuncar@54334
  1505
     as @{method (HOL) "transfer"} internally does.
kuncar@54334
  1506
kuncar@54334
  1507
  \item @{attribute (HOL) Transfer.transferred} works in the opposite
kuncar@54334
  1508
    direction than @{method (HOL) "transfer'"}. E.g., given the transfer
kuncar@54334
  1509
    relation @{text "ZN x n \<equiv> (x = int n)"}, corresponding transfer rules and the theorem
kuncar@54334
  1510
    @{text "\<forall>x::int \<in> {0..}. x < x + 1"}, the attribute would prove 
kuncar@54334
  1511
    @{text "\<forall>n::nat. n < n + 1"}. The attribute is still in experimental
kuncar@54334
  1512
    phase of development.
kuncar@54334
  1513
huffman@47821
  1514
  \item @{attribute (HOL) "transfer_rule"} attribute maintains a
huffman@47821
  1515
    collection of transfer rules, which relate constants at two
huffman@47821
  1516
    different types. Typical transfer rules may relate different type
huffman@47821
  1517
    instances of the same polymorphic constant, or they may relate an
huffman@47821
  1518
    operation on a raw type to a corresponding operation on an
huffman@47821
  1519
    abstract type (quotient or subtype). For example:
huffman@47821
  1520
huffman@47821
  1521
    @{text "((A ===> B) ===> list_all2 A ===> list_all2 B) map map"}\\
huffman@47821
  1522
    @{text "(cr_int ===> cr_int ===> cr_int) (\<lambda>(x,y) (u,v). (x+u, y+v)) plus"}
huffman@47821
  1523
huffman@47821
  1524
    Lemmas involving predicates on relations can also be registered
huffman@47821
  1525
    using the same attribute. For example:
huffman@47821
  1526
huffman@47821
  1527
    @{text "bi_unique A \<Longrightarrow> (list_all2 A ===> op =) distinct distinct"}\\
blanchet@55944
  1528
    @{text "\<lbrakk>bi_unique A; bi_unique B\<rbrakk> \<Longrightarrow> bi_unique (rel_prod A B)"}
huffman@47821
  1529
kuncar@57829
  1530
    Preservation of predicates on relations (@{text "bi_unique, bi_total,
kuncar@57829
  1531
    right_unique, right_total, left_unique, left_total"}) with the respect to a relator
kuncar@57829
  1532
    is proved automatically if the involved type is BNF\cite{isabelle-datatypes} without dead variables.
kuncar@57829
  1533
kuncar@54334
  1534
  \item @{attribute (HOL) "transfer_domain_rule"} attribute maintains a collection
kuncar@54334
  1535
    of rules, which specify a domain of a transfer relation by a predicate.
kuncar@54334
  1536
    E.g., given the transfer relation @{text "ZN x n \<equiv> (x = int n)"}, 
kuncar@54334
  1537
    one can register the following transfer domain rule: 
kuncar@54334
  1538
    @{text "Domainp ZN = (\<lambda>x. x \<ge> 0)"}. The rules allow the package to produce
kuncar@54334
  1539
    more readable transferred goals, e.g., when quantifiers are transferred.
kuncar@54334
  1540
huffman@47821
  1541
  \item @{attribute (HOL) relator_eq} attribute collects identity laws
kuncar@57829
  1542
    for relators of various type constructors, e.g. @{term "rel_set
huffman@47821
  1543
    (op =) = (op =)"}. The @{method (HOL) transfer} method uses these
huffman@47821
  1544
    lemmas to infer transfer rules for non-polymorphic constants on
kuncar@57829
  1545
    the fly. For examples see @{file
kuncar@57829
  1546
    "~~/src/HOL/Lifting_Set.thy"} or @{file "~~/src/HOL/Lifting.thy"}. 
kuncar@57829
  1547
    This property is proved automatically if the involved type is BNF without dead variables.
huffman@47821
  1548
kuncar@54334
  1549
  \item @{attribute_def (HOL) "relator_domain"} attribute collects rules 
kuncar@57829
  1550
    describing domains of relators by predicators. E.g., 
kuncar@57829
  1551
    @{term "Domainp (rel_set T) = (\<lambda>A. Ball A (Domainp T))"}. This allows the package 
kuncar@57829
  1552
    to lift transfer domain rules through type constructors. For examples see @{file
kuncar@57829
  1553
    "~~/src/HOL/Lifting_Set.thy"} or @{file "~~/src/HOL/Lifting.thy"}.
kuncar@57829
  1554
    This property is proved automatically if the involved type is BNF without dead variables.
kuncar@54334
  1555
huffman@47821
  1556
  \end{description}
huffman@47821
  1557
kuncar@54334
  1558
  Theoretical background can be found in \cite{Huffman-Kuncar:2013:lifting_transfer}.
huffman@47821
  1559
*}
huffman@47821
  1560
wenzelm@47859
  1561
kuncar@47802
  1562
section {* Lifting package *}
kuncar@47802
  1563
kuncar@47802
  1564
text {*
kuncar@54334
  1565
  The Lifting package allows users to lift terms of the raw type to the abstract type, which is 
kuncar@54334
  1566
  a necessary step in building a library for an abstract type. Lifting defines a new constant 
kuncar@54334
  1567
  by combining coercion functions (Abs and Rep) with the raw term. It also proves an appropriate 
kuncar@54334
  1568
  transfer rule for the Transfer package and, if possible, an equation for the code generator.
kuncar@54334
  1569
kuncar@54334
  1570
  The Lifting package provides two main commands: @{command (HOL) "setup_lifting"} for initializing 
kuncar@54334
  1571
  the package to work with a new type, and @{command (HOL) "lift_definition"} for lifting constants. 
kuncar@54334
  1572
  The Lifting package works with all four kinds of type abstraction: type copies, subtypes, 
kuncar@54334
  1573
  total quotients and partial quotients.
kuncar@54334
  1574
kuncar@54334
  1575
  Theoretical background can be found in \cite{Huffman-Kuncar:2013:lifting_transfer}.
kuncar@54334
  1576
kuncar@47802
  1577
  \begin{matharray}{rcl}
kuncar@47802
  1578
    @{command_def (HOL) "setup_lifting"} & : & @{text "local_theory \<rightarrow> local_theory"}\\
kuncar@47802
  1579
    @{command_def (HOL) "lift_definition"} & : & @{text "local_theory \<rightarrow> proof(prove)"}\\
kuncar@54334
  1580
    @{command_def (HOL) "lifting_forget"} & : & @{text "local_theory \<rightarrow> local_theory"}\\
kuncar@54334
  1581
    @{command_def (HOL) "lifting_update"} & : & @{text "local_theory \<rightarrow> local_theory"}\\
kuncar@53219
  1582
    @{command_def (HOL) "print_quot_maps"} & : & @{text "context \<rightarrow>"}\\
kuncar@47802
  1583
    @{command_def (HOL) "print_quotients"} & : & @{text "context \<rightarrow>"}\\
kuncar@47802
  1584
    @{attribute_def (HOL) "quot_map"} & : & @{text attribute} \\
kuncar@56519
  1585
    @{attribute_def (HOL) "relator_eq_onp"} & : & @{text attribute} \\
kuncar@54334
  1586
    @{attribute_def (HOL) "relator_mono"} & : & @{text attribute} \\
kuncar@54334
  1587
    @{attribute_def (HOL) "relator_distr"} & : & @{text attribute} \\
kuncar@54334
  1588
    @{attribute_def (HOL) "quot_del"} & : & @{text attribute} \\
kuncar@54334
  1589
    @{attribute_def (HOL) "lifting_restore"} & : & @{text attribute} \\   
kuncar@47802
  1590
  \end{matharray}
kuncar@47802
  1591
wenzelm@55112
  1592
  @{rail \<open>
wenzelm@55029
  1593
    @@{command (HOL) setup_lifting} ('(' 'no_code' ')')? \<newline>
kuncar@54334
  1594
      @{syntax thmref} @{syntax thmref}? (@'parametric' @{syntax thmref})?;
wenzelm@55112
  1595
  \<close>}
wenzelm@55112
  1596
wenzelm@55112
  1597
  @{rail \<open>
wenzelm@55029
  1598
    @@{command (HOL) lift_definition} @{syntax name} '::' @{syntax type}  @{syntax mixfix}? \<newline>
kuncar@57829
  1599
      'is' @{syntax term} (@'parametric' (@{syntax thmref}+))?;
wenzelm@55112
  1600
  \<close>}
wenzelm@55112
  1601
wenzelm@55112
  1602
  @{rail \<open>
kuncar@54334
  1603
    @@{command (HOL) lifting_forget} @{syntax nameref};
wenzelm@55112
  1604
  \<close>}
wenzelm@55112
  1605
wenzelm@55112
  1606
  @{rail \<open>
kuncar@54334
  1607
    @@{command (HOL) lifting_update} @{syntax nameref};
wenzelm@55112
  1608
  \<close>}
wenzelm@55112
  1609
wenzelm@55112
  1610
  @{rail \<open>
kuncar@54334
  1611
    @@{attribute (HOL) lifting_restore} @{syntax thmref} (@{syntax thmref} @{syntax thmref})?;
wenzelm@55112
  1612
  \<close>}
kuncar@47802
  1613
kuncar@47802
  1614
  \begin{description}
kuncar@47802
  1615
wenzelm@47859
  1616
  \item @{command (HOL) "setup_lifting"} Sets up the Lifting package
kuncar@54334
  1617
    to work with a user-defined type. 
kuncar@54334
  1618
    The command supports two modes. The first one is a low-level mode when 
kuncar@54334
  1619
    the user must provide as a first
kuncar@54334
  1620
    argument of @{command (HOL) "setup_lifting"} a
kuncar@57829
  1621
    quotient theorem @{term "Quotient R Abs Rep T"}. The
kuncar@54334
  1622
    package configures a transfer rule for equality, a domain transfer
kuncar@54334
  1623
    rules and sets up the @{command_def (HOL) "lift_definition"}
kuncar@57829
  1624
    command to work with the abstract type. An optional theorem @{term "reflp R"}, which certifies that 
kuncar@54334
  1625
    the equivalence relation R is total,
kuncar@54334
  1626
    can be provided as a second argument. This allows the package to generate stronger transfer
kuncar@54334
  1627
    rules. And finally, the parametricity theorem for R can be provided as a third argument.
kuncar@54334
  1628
    This allows the package to generate a stronger transfer rule for equality.
kuncar@54334
  1629
kuncar@54334
  1630
    Users generally will not prove the @{text Quotient} theorem manually for 
kuncar@54334
  1631
    new types, as special commands exist to automate the process.
kuncar@54334
  1632
    
kuncar@54334
  1633
    When a new subtype is defined by @{command (HOL) typedef}, @{command (HOL) "lift_definition"} 
kuncar@54334
  1634
    can be used in its
kuncar@54334
  1635
    second mode, where only the type_definition theorem @{text "type_definition Rep Abs A"}
kuncar@54334
  1636
    is used as an argument of the command. The command internally proves the corresponding 
kuncar@54334
  1637
    Quotient theorem and registers it with @{command (HOL) setup_lifting} using its first mode.
kuncar@54334
  1638
kuncar@54334
  1639
    For quotients, the command @{command (HOL) quotient_type} can be used. The command defines 
kuncar@54334
  1640
    a new quotient type and similarly to the previous case, the corresponding Quotient theorem is proved 
kuncar@54334
  1641
    and registered by @{command (HOL) setup_lifting}.
kuncar@54334
  1642
    
kuncar@54334
  1643
    The command @{command (HOL) "setup_lifting"} also sets up the code generator
kuncar@54334
  1644
    for the new type. Later on, when a new constant is defined by @{command (HOL) "lift_definition"},
kuncar@54334
  1645
    the Lifting package proves and registers a code equation (if there is one) for the new constant.
kuncar@54334
  1646
    If the option @{text "no_code"} is specified, the Lifting package does not set up the code
kuncar@54334
  1647
    generator and as a consequence no code equations involving an abstract type are registered
kuncar@54334
  1648
    by @{command (HOL) "lift_definition"}.
kuncar@54334
  1649
kuncar@54334
  1650
  \item @{command (HOL) "lift_definition"} @{text "f :: \<tau>"} @{keyword (HOL) "is"} @{text t}
wenzelm@47859
  1651
    Defines a new function @{text f} with an abstract type @{text \<tau>}
wenzelm@47859
  1652
    in terms of a corresponding operation @{text t} on a
kuncar@54334
  1653
    representation type. More formally, if @{text "t :: \<sigma>"}, then
kuncar@54334
  1654
    the command builds a term @{text "F"} as a corresponding combination of abstraction 
kuncar@54334
  1655
    and representation functions such that @{text "F :: \<sigma> \<Rightarrow> \<tau>" } and 
kuncar@54334
  1656
    defines @{text f} is as @{text "f \<equiv> F t"}.
kuncar@54334
  1657
    The term @{text t} does not have to be necessarily a constant but it can be any term.
kuncar@54334
  1658
kuncar@54334
  1659
    The command opens a proof environment and the user must discharge 
kuncar@54334
  1660
    a respectfulness proof obligation. For a type copy, i.e., a typedef with @{text
kuncar@54334
  1661
    UNIV}, the obligation is discharged automatically. The proof goal is
kuncar@47802
  1662
    presented in a user-friendly, readable form. A respectfulness
wenzelm@47859
  1663
    theorem in the standard format @{text f.rsp} and a transfer rule
kuncar@54334
  1664
    @{text f.transfer} for the Transfer package are generated by the
wenzelm@47859
  1665
    package.
kuncar@47802
  1666
kuncar@57829
  1667
    The user can specify a parametricity theorems for @{text t} after the keyword 
kuncar@54334
  1668
    @{keyword "parametric"}, which allows the command
kuncar@57829
  1669
    to generate parametric transfer rules for @{text f}.
kuncar@54334
  1670
kuncar@50879
  1671
    For each constant defined through trivial quotients (type copies or
kuncar@50879
  1672
    subtypes) @{text f.rep_eq} is generated. The equation is a code certificate
kuncar@50879
  1673
    that defines @{text f} using the representation function.
kuncar@50879
  1674
kuncar@50879
  1675
    For each constant @{text f.abs_eq} is generated. The equation is unconditional
kuncar@50879
  1676
    for total quotients. The equation defines @{text f} using
kuncar@50879
  1677
    the abstraction function.
kuncar@50879
  1678
kuncar@54334
  1679
    Integration with [@{attribute code} abstract]: For subtypes (e.g.,
wenzelm@47859
  1680
    corresponding to a datatype invariant, such as dlist), @{command
kuncar@50879
  1681
    (HOL) "lift_definition"} uses a code certificate theorem
kuncar@50879
  1682
    @{text f.rep_eq} as a code equation.
kuncar@50879
  1683
kuncar@54334
  1684
    Integration with [@{attribute code} equation]: For total quotients, @{command
kuncar@50879
  1685
    (HOL) "lift_definition"} uses @{text f.abs_eq} as a code equation.
kuncar@47802
  1686
kuncar@54334
  1687
  \item @{command (HOL) lifting_forget} and  @{command (HOL) lifting_update}
kuncar@54334
  1688
    These two commands serve for storing and deleting the set-up of
kuncar@54334
  1689
    the Lifting package and corresponding transfer rules defined by this package.
kuncar@54334
  1690
    This is useful for hiding of type construction details of an abstract type 
kuncar@54334
  1691
    when the construction is finished but it still allows additions to this construction
kuncar@54334
  1692
    when this is later necessary.
kuncar@54334
  1693
kuncar@54334
  1694
    Whenever the Lifting package is set up with a new abstract type @{text "\<tau>"} by  
kuncar@54334
  1695
    @{command_def (HOL) "lift_definition"}, the package defines a new bundle
kuncar@54334
  1696
    that is called @{text "\<tau>.lifting"}. This bundle already includes set-up for the Lifting package. 
kuncar@54334
  1697
    The new transfer rules
kuncar@54334
  1698
    introduced by @{command (HOL) "lift_definition"} can be stored in the bundle by
kuncar@54334
  1699
    the command @{command (HOL) "lifting_update"} @{text "\<tau>.lifting"}.
kuncar@54334
  1700
kuncar@54334
  1701
    The command @{command (HOL) "lifting_forget"} @{text "\<tau>.lifting"} deletes set-up of the Lifting 
kuncar@54334
  1702
    package
kuncar@54334
  1703
    for @{text \<tau>} and deletes all the transfer rules that were introduced
kuncar@54334
  1704
    by @{command (HOL) "lift_definition"} using @{text \<tau>} as an abstract type.
kuncar@54334
  1705
kuncar@54334
  1706
    The stored set-up in a bundle can be reintroduced by the Isar commands for including a bundle
kuncar@54334
  1707
    (@{command "include"}, @{keyword "includes"} and @{command "including"}).
kuncar@54334
  1708
kuncar@53219
  1709
  \item @{command (HOL) "print_quot_maps"} prints stored quotient map
wenzelm@47859
  1710
    theorems.
wenzelm@47859
  1711
wenzelm@47859
  1712
  \item @{command (HOL) "print_quotients"} prints stored quotient
wenzelm@47859
  1713
    theorems.
wenzelm@47859
  1714
wenzelm@47859
  1715
  \item @{attribute (HOL) quot_map} registers a quotient map
kuncar@57829
  1716
    theorem, a theorem showing how to "lift" quotients over type constructors. 
kuncar@57829
  1717
    E.g., @{term "Quotient R Abs Rep T \<Longrightarrow> 
kuncar@57829
  1718
    Quotient (rel_set R) (image Abs) (image Rep) (rel_set T)"}. 
kuncar@54334
  1719
    For examples see @{file
kuncar@57829
  1720
    "~~/src/HOL/Lifting_Set.thy"} or @{file "~~/src/HOL/Lifting.thy"}.
kuncar@57829
  1721
    This property is proved automatically if the involved type is BNF without dead variables.
wenzelm@47859
  1722
kuncar@56519
  1723
  \item @{attribute (HOL) relator_eq_onp} registers a theorem that
kuncar@57829
  1724
    shows that a relator applied to an equality restricted by a predicate @{term P} (i.e., @{term
kuncar@57829
  1725
    "eq_onp P"}) is equal 
kuncar@57829
  1726
    to a predicator applied to the @{term P}. The combinator @{const eq_onp} is used for 
kuncar@57829
  1727
    internal encoding of proper subtypes. Such theorems allows the package to hide @{text
kuncar@56519
  1728
    eq_onp} from a user in a user-readable form of a
wenzelm@47859
  1729
    respectfulness theorem. For examples see @{file
kuncar@57829
  1730
    "~~/src/HOL/Lifting_Set.thy"} or @{file "~~/src/HOL/Lifting.thy"}.
kuncar@57829
  1731
    This property is proved automatically if the involved type is BNF without dead variables.
kuncar@47802
  1732
kuncar@54334
  1733
  \item @{attribute (HOL) "relator_mono"} registers a property describing a monotonicity of a relator.
kuncar@57829
  1734
    E.g., @{term "A \<le> B \<Longrightarrow> rel_set A \<le> rel_set B"}. 
kuncar@54334
  1735
    This property is needed for proving a stronger transfer rule in @{command_def (HOL) "lift_definition"}
kuncar@57829
  1736
    when a parametricity theorem for the raw term is specified and also for the reflexivity prover.
kuncar@57829
  1737
    For examples see @{file
kuncar@57829
  1738
    "~~/src/HOL/Lifting_Set.thy"} or @{file "~~/src/HOL/Lifting.thy"}.
kuncar@57829
  1739
    This property is proved automatically if the involved type is BNF without dead variables.
kuncar@54334
  1740
kuncar@54334
  1741
  \item @{attribute (HOL) "relator_distr"} registers a property describing a distributivity
kuncar@54334
  1742
    of the relation composition and a relator. E.g., 
kuncar@57829
  1743
    @{text "rel_set R \<circ>\<circ> rel_set S = rel_set (R \<circ>\<circ> S)"}. 
kuncar@54334
  1744
    This property is needed for proving a stronger transfer rule in @{command_def (HOL) "lift_definition"}
kuncar@54334
  1745
    when a parametricity theorem for the raw term is specified.
kuncar@54334
  1746
    When this equality does not hold unconditionally (e.g., for the function type), the user can specified
kuncar@54334
  1747
    each direction separately and also register multiple theorems with different set of assumptions.
kuncar@54334
  1748
    This attribute can be used only after the monotonicity property was already registered by
kuncar@57829
  1749
    @{attribute (HOL) "relator_mono"}. For examples see @{file
kuncar@57829
  1750
    "~~/src/HOL/Lifting_Set.thy"} or @{file "~~/src/HOL/Lifting.thy"}.
kuncar@57829
  1751
    This property is proved automatically if the involved type is BNF without dead variables.
kuncar@50877
  1752
kuncar@50877
  1753
  \item @{attribute (HOL) quot_del} deletes a corresponding Quotient theorem
kuncar@50877
  1754
    from the Lifting infrastructure and thus de-register the corresponding quotient. 
kuncar@50877
  1755
    This effectively causes that @{command (HOL) lift_definition}  will not
kuncar@54334
  1756
    do any lifting for the corresponding type. This attribute is rather used for low-level
kuncar@54334
  1757
    manipulation with set-up of the Lifting package because @{command (HOL) lifting_forget} is
kuncar@54334
  1758
    preferred for normal usage.
kuncar@54334
  1759
kuncar@54334
  1760
  \item @{attribute (HOL) lifting_restore} @{text "Quotient_thm pcr_def pcr_cr_eq_thm"} 
kuncar@54334
  1761
    registers the Quotient theorem @{text Quotient_thm} in the Lifting infrastructure 
kuncar@54334
  1762
    and thus sets up lifting for an abstract type @{text \<tau>} (that is defined by @{text Quotient_thm}).
kuncar@54334
  1763
    Optional theorems @{text pcr_def} and @{text pcr_cr_eq_thm} can be specified to register 
kuncar@54334
  1764
    the parametrized
kuncar@54334
  1765
    correspondence relation for @{text \<tau>}. E.g., for @{text "'a dlist"}, @{text pcr_def} is
kuncar@54334
  1766
    @{text "pcr_dlist A \<equiv> list_all2 A \<circ>\<circ> cr_dlist"} and @{text pcr_cr_eq_thm} is 
kuncar@54334
  1767
    @{text "pcr_dlist op= = op="}.
kuncar@54334
  1768
    This attribute is rather used for low-level
kuncar@54334
  1769
    manipulation with set-up of the Lifting package because using of the bundle @{text \<tau>.lifting} 
kuncar@54334
  1770
    together with the commands @{command (HOL) lifting_forget} and @{command (HOL) lifting_update} is
kuncar@54334
  1771
    preferred for normal usage.
kuncar@54334
  1772
kuncar@57829
  1773
  \item Integration with the BNF package\cite{isabelle-datatypes}: 
kuncar@57829
  1774
    As already mentioned, the theorems that are registered
kuncar@57829
  1775
    by the following attributes are proved and registered automatically if the involved type
kuncar@57829
  1776
    is BNF without dead variables: @{attribute (HOL) quot_map}, @{attribute (HOL) relator_eq_onp}, 
kuncar@57829
  1777
    @{attribute (HOL) "relator_mono"}, @{attribute (HOL) "relator_distr"}. Also the definition of a 
kuncar@57829
  1778
    relator and predicator is provided automatically. Moreover, if the BNF represents a datatype, 
kuncar@57829
  1779
    simplification rules for a predicator are again proved automatically.
kuncar@57829
  1780
  
kuncar@47802
  1781
  \end{description}
kuncar@47802
  1782
*}
kuncar@47802
  1783
wenzelm@47859
  1784
noschinl@43994
  1785
section {* Coercive subtyping *}
noschinl@43994
  1786
noschinl@43994
  1787
text {*
noschinl@43994
  1788
  \begin{matharray}{rcl}
noschinl@43994
  1789
    @{attribute_def (HOL) coercion} & : & @{text attribute} \\
noschinl@43994
  1790
    @{attribute_def (HOL) coercion_enabled} & : & @{text attribute} \\
noschinl@43994
  1791
    @{attribute_def (HOL) coercion_map} & : & @{text attribute} \\
noschinl@43994
  1792
  \end{matharray}
noschinl@43994
  1793
wenzelm@46280
  1794
  Coercive subtyping allows the user to omit explicit type
wenzelm@46280
  1795
  conversions, also called \emph{coercions}.  Type inference will add
wenzelm@46280
  1796
  them as necessary when parsing a term. See
wenzelm@46280
  1797
  \cite{traytel-berghofer-nipkow-2011} for details.
wenzelm@46280
  1798
wenzelm@55112
  1799
  @{rail \<open>
noschinl@43994
  1800
    @@{attribute (HOL) coercion} (@{syntax term})?
noschinl@43994
  1801
    ;
noschinl@43994
  1802
    @@{attribute (HOL) coercion_map} (@{syntax term})?
wenzelm@55112
  1803
  \<close>}
noschinl@43994
  1804
noschinl@43994
  1805
  \begin{description}
noschinl@43994
  1806
noschinl@43994
  1807
  \item @{attribute (HOL) "coercion"}~@{text "f"} registers a new
wenzelm@53015
  1808
  coercion function @{text "f :: \<sigma>\<^sub>1 \<Rightarrow> \<sigma>\<^sub>2"} where @{text "\<sigma>\<^sub>1"} and
wenzelm@53015
  1809
  @{text "\<sigma>\<^sub>2"} are type constructors without arguments.  Coercions are
wenzelm@46280
  1810
  composed by the inference algorithm if needed.  Note that the type
wenzelm@46280
  1811
  inference algorithm is complete only if the registered coercions
wenzelm@46280
  1812
  form a lattice.
noschinl@43994
  1813
wenzelm@46280
  1814
  \item @{attribute (HOL) "coercion_map"}~@{text "map"} registers a
wenzelm@46280
  1815
  new map function to lift coercions through type constructors. The
wenzelm@46280
  1816
  function @{text "map"} must conform to the following type pattern
noschinl@43994
  1817
noschinl@43994
  1818
  \begin{matharray}{lll}
noschinl@43994
  1819
    @{text "map"} & @{text "::"} &
wenzelm@53015
  1820
      @{text "f\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> f\<^sub>n \<Rightarrow> (\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t \<Rightarrow> (\<beta>\<^sub>1, \<dots>, \<beta>\<^sub>n) t"} \\
noschinl@43994
  1821
  \end{matharray}
noschinl@43994
  1822
wenzelm@53015
  1823
  where @{text "t"} is a type constructor and @{text "f\<^sub>i"} is of type
wenzelm@53015
  1824
  @{text "\<alpha>\<^sub>i \<Rightarrow> \<beta>\<^sub>i"} or @{text "\<beta>\<^sub>i \<Rightarrow> \<alpha>\<^sub>i"}.  Registering a map function
wenzelm@46280
  1825
  overwrites any existing map function for this particular type
wenzelm@46280
  1826
  constructor.
noschinl@43994
  1827
noschinl@43994
  1828
  \item @{attribute (HOL) "coercion_enabled"} enables the coercion
noschinl@43994
  1829
  inference algorithm.
noschinl@43994
  1830
noschinl@43994
  1831
  \end{description}
noschinl@43994
  1832
*}
noschinl@43994
  1833
noschinl@43994
  1834
wenzelm@26849
  1835
section {* Arithmetic proof support *}
wenzelm@26849
  1836
wenzelm@26849
  1837
text {*
wenzelm@26849
  1838
  \begin{matharray}{rcl}
wenzelm@28761
  1839
    @{method_def (HOL) arith} & : & @{text method} \\
nipkow@30863
  1840
    @{attribute_def (HOL) arith} & : & @{text attribute} \\
wenzelm@28761
  1841
    @{attribute_def (HOL) arith_split} & : & @{text attribute} \\
wenzelm@26849
  1842
  \end{matharray}
wenzelm@26849
  1843
wenzelm@46280
  1844
  \begin{description}
wenzelm@26849
  1845
wenzelm@46280
  1846
  \item @{method (HOL) arith} decides linear arithmetic problems (on
wenzelm@46280
  1847
  types @{text nat}, @{text int}, @{text real}).  Any current facts
wenzelm@46280
  1848
  are inserted into the goal before running the procedure.
wenzelm@26849
  1849
wenzelm@46280
  1850
  \item @{attribute (HOL) arith} declares facts that are supplied to
wenzelm@46280
  1851
  the arithmetic provers implicitly.
wenzelm@46280
  1852
wenzelm@46280
  1853
  \item @{attribute (HOL) arith_split} attribute declares case split
wenzelm@30865
  1854
  rules to be expanded before @{method (HOL) arith} is invoked.
nipkow@30863
  1855
wenzelm@46280
  1856
  \end{description}
wenzelm@46280
  1857
wenzelm@46280
  1858
  Note that a simpler (but faster) arithmetic prover is already
wenzelm@46280
  1859
  invoked by the Simplifier.
wenzelm@26849
  1860
*}
wenzelm@26849
  1861
wenzelm@26849
  1862
wenzelm@30169
  1863
section {* Intuitionistic proof search *}
wenzelm@30169
  1864
wenzelm@30169
  1865
text {*
wenzelm@30169
  1866
  \begin{matharray}{rcl}
wenzelm@30171
  1867
    @{method_def (HOL) iprover} & : & @{text method} \\
wenzelm@30169
  1868
  \end{matharray}
wenzelm@30169
  1869
wenzelm@55112
  1870
  @{rail \<open>
wenzelm@55112
  1871
    @@{method (HOL) iprover} (@{syntax rulemod} *)
wenzelm@55112
  1872
  \<close>}
wenzelm@30169
  1873
wenzelm@46280
  1874
  \begin{description}
wenzelm@46280
  1875
wenzelm@46280
  1876
  \item @{method (HOL) iprover} performs intuitionistic proof search,
wenzelm@46280
  1877
  depending on specifically declared rules from the context, or given
wenzelm@46280
  1878
  as explicit arguments.  Chained facts are inserted into the goal
wenzelm@46280
  1879
  before commencing proof search.
wenzelm@35613
  1880
wenzelm@30169
  1881
  Rules need to be classified as @{attribute (Pure) intro},
wenzelm@30169
  1882
  @{attribute (Pure) elim}, or @{attribute (Pure) dest}; here the
wenzelm@30169
  1883
  ``@{text "!"}'' indicator refers to ``safe'' rules, which may be
wenzelm@30169
  1884
  applied aggressively (without considering back-tracking later).
wenzelm@30169
  1885
  Rules declared with ``@{text "?"}'' are ignored in proof search (the
wenzelm@42626
  1886
  single-step @{method (Pure) rule} method still observes these).  An
wenzelm@30169
  1887
  explicit weight annotation may be given as well; otherwise the
wenzelm@30169
  1888
  number of rule premises will be taken into account here.
wenzelm@46280
  1889
wenzelm@46280
  1890
  \end{description}
wenzelm@30169
  1891
*}
wenzelm@30169
  1892
wenzelm@46280
  1893
blanchet@43578
  1894
section {* Model Elimination and Resolution *}
blanchet@43578
  1895
blanchet@43578
  1896
text {*
blanchet@43578
  1897
  \begin{matharray}{rcl}
blanchet@43578
  1898
    @{method_def (HOL) "meson"} & : & @{text method} \\
blanchet@43578
  1899
    @{method_def (HOL) "metis"} & : & @{text method} \\
blanchet@43578
  1900
  \end{matharray}
blanchet@43578
  1901
wenzelm@55112
  1902
  @{rail \<open>
blanchet@43578
  1903
    @@{method (HOL) meson} @{syntax thmrefs}?
blanchet@43578
  1904
    ;
wenzelm@46280
  1905
    @@{method (HOL) metis}
wenzelm@46280
  1906
      ('(' ('partial_types' | 'full_types' | 'no_types' | @{syntax name}) ')')?
wenzelm@46280
  1907
      @{syntax thmrefs}?
wenzelm@55112
  1908
  \<close>}
blanchet@43578
  1909
wenzelm@46280
  1910
  \begin{description}
wenzelm@46280
  1911
wenzelm@46280
  1912
  \item @{method (HOL) meson} implements Loveland's model elimination
wenzelm@46280
  1913
  procedure \cite{loveland-78}.  See @{file
wenzelm@46280
  1914
  "~~/src/HOL/ex/Meson_Test.thy"} for examples.
blanchet@43578
  1915
wenzelm@46280
  1916
  \item @{method (HOL) metis} combines ordered resolution and ordered
wenzelm@46280
  1917
  paramodulation to find first-order (or mildly higher-order) proofs.
wenzelm@46280
  1918
  The first optional argument specifies a type encoding; see the
wenzelm@46280
  1919
  Sledgehammer manual \cite{isabelle-sledgehammer} for details.  The
wenzelm@46280
  1920
  directory @{file "~~/src/HOL/Metis_Examples"} contains several small
wenzelm@46280
  1921
  theories developed to a large extent using @{method (HOL) metis}.
wenzelm@46280
  1922
wenzelm@46280
  1923
  \end{description}
blanchet@43578
  1924
*}
wenzelm@30169
  1925
wenzelm@46280
  1926
wenzelm@50130
  1927
section {* Algebraic reasoning via Gr\"obner bases *}
wenzelm@50130
  1928
wenzelm@50130
  1929
text {*
wenzelm@50130
  1930
  \begin{matharray}{rcl}
wenzelm@50130
  1931
    @{method_def (HOL) "algebra"} & : & @{text method} \\
wenzelm@50130
  1932
    @{attribute_def (HOL) algebra} & : & @{text attribute} \\
wenzelm@50130
  1933
  \end{matharray}
wenzelm@50130
  1934
wenzelm@55112
  1935
  @{rail \<open>
wenzelm@50130
  1936
    @@{method (HOL) algebra}
wenzelm@50130
  1937
      ('add' ':' @{syntax thmrefs})?
wenzelm@50130
  1938
      ('del' ':' @{syntax thmrefs})?
wenzelm@50130
  1939
    ;
wenzelm@50130
  1940
    @@{attribute (HOL) algebra} (() | 'add' | 'del')
wenzelm@55112
  1941
  \<close>}
wenzelm@50130
  1942
wenzelm@50130
  1943
  \begin{description}
wenzelm@50130
  1944
wenzelm@50130
  1945
  \item @{method (HOL) algebra} performs algebraic reasoning via
wenzelm@50130
  1946
  Gr\"obner bases, see also \cite{Chaieb-Wenzel:2007} and
wenzelm@50130
  1947
  \cite[\S3.2]{Chaieb-thesis}. The method handles deals with two main
wenzelm@50130
  1948
  classes of problems:
wenzelm@50130
  1949
wenzelm@50130
  1950
  \begin{enumerate}
wenzelm@50130
  1951
wenzelm@50130
  1952
  \item Universal problems over multivariate polynomials in a
wenzelm@50130
  1953
  (semi)-ring/field/idom; the capabilities of the method are augmented
wenzelm@50130
  1954
  according to properties of these structures. For this problem class
wenzelm@50130
  1955
  the method is only complete for algebraically closed fields, since
wenzelm@50130
  1956
  the underlying method is based on Hilbert's Nullstellensatz, where
wenzelm@50130
  1957
  the equivalence only holds for algebraically closed fields.
wenzelm@50130
  1958
wenzelm@50130
  1959
  The problems can contain equations @{text "p = 0"} or inequations
wenzelm@50130
  1960
  @{text "q \<noteq> 0"} anywhere within a universal problem statement.
wenzelm@50130
  1961
wenzelm@50130
  1962
  \item All-exists problems of the following restricted (but useful)
wenzelm@50130
  1963
  form:
wenzelm@50130
  1964
wenzelm@50130
  1965
  @{text [display] "\<forall>x\<^sub>1 \<dots> x\<^sub>n.
wenzelm@50130
  1966
    e\<^sub>1(x\<^sub>1, \<dots>, x\<^sub>n) = 0 \<and> \<dots> \<and> e\<^sub>m(x\<^sub>1, \<dots>, x\<^sub>n) = 0 \<longrightarrow>
wenzelm@50130
  1967
    (\<exists>y\<^sub>1 \<dots> y\<^sub>k.
wenzelm@50130
  1968
      p\<^sub>1\<^sub>1(x\<^sub>1, \<dots> ,x\<^sub>n) * y\<^sub>1 + \<dots> + p\<^sub>1\<^sub>k(x\<^sub>1, \<dots>, x\<^sub>n) * y\<^sub>k = 0 \<and>
wenzelm@50130
  1969
      \<dots> \<and>
wenzelm@50130
  1970
      p\<^sub>t\<^sub>1(x\<^sub>1, \<dots>, x\<^sub>n) * y\<^sub>1 + \<dots> + p\<^sub>t\<^sub>k(x\<^sub>1, \<dots>, x\<^sub>n) * y\<^sub>k = 0)"}
wenzelm@50130
  1971
wenzelm@50130
  1972
  Here @{text "e\<^sub>1, \<dots>, e\<^sub>n"} and the @{text "p\<^sub>i\<^sub>j"} are multivariate
wenzelm@50130
  1973
  polynomials only in the variables mentioned as arguments.
wenzelm@50130
  1974
wenzelm@50130
  1975
  \end{enumerate}
wenzelm@50130
  1976
wenzelm@50130
  1977
  The proof method is preceded by a simplification step, which may be
wenzelm@50130
  1978
  modified by using the form @{text "(algebra add: ths\<^sub>1 del: ths\<^sub>2)"}.
wenzelm@50130
  1979
  This acts like declarations for the Simplifier
wenzelm@50130
  1980
  (\secref{sec:simplifier}) on a private simpset for this tool.
wenzelm@50130
  1981
wenzelm@50130
  1982
  \item @{attribute algebra} (as attribute) manages the default
wenzelm@50130
  1983
  collection of pre-simplification rules of the above proof method.
wenzelm@50130
  1984
wenzelm@50130
  1985
  \end{description}
wenzelm@50130
  1986
*}
wenzelm@50130
  1987
wenzelm@50130
  1988
wenzelm@50130
  1989
subsubsection {* Example *}
wenzelm@50130
  1990
wenzelm@50130
  1991
text {* The subsequent example is from geometry: collinearity is
wenzelm@50130
  1992
  invariant by rotation.  *}
wenzelm@50130
  1993
wenzelm@50130
  1994
type_synonym point = "int \<times> int"
wenzelm@50130
  1995
wenzelm@50130
  1996
fun collinear :: "point \<Rightarrow> point \<Rightarrow> point \<Rightarrow> bool" where
wenzelm@50130
  1997
  "collinear (Ax, Ay) (Bx, By) (Cx, Cy) \<longleftrightarrow>
wenzelm@50130
  1998
    (Ax - Bx) * (By - Cy) = (Ay - By) * (Bx - Cx)"
wenzelm@50130
  1999
wenzelm@50130
  2000
lemma collinear_inv_rotation:
wenzelm@53015
  2001
  assumes "collinear (Ax, Ay) (Bx, By) (Cx, Cy)" and "c\<^sup>2 + s\<^sup>2 = 1"
wenzelm@50130
  2002
  shows "collinear (Ax * c - Ay * s, Ay * c + Ax * s)
wenzelm@50130
  2003
    (Bx * c - By * s, By * c + Bx * s) (Cx * c - Cy * s, Cy * c + Cx * s)"
wenzelm@50130
  2004
  using assms by (algebra add: collinear.simps)
wenzelm@50130
  2005
wenzelm@50130
  2006
text {*
wenzelm@53982
  2007
 See also @{file "~~/src/HOL/ex/Groebner_Examples.thy"}.
wenzelm@50130
  2008
*}
wenzelm@50130
  2009
wenzelm@50130
  2010
wenzelm@30171
  2011
section {* Coherent Logic *}
wenzelm@30171
  2012
wenzelm@30171
  2013
text {*
wenzelm@30171
  2014
  \begin{matharray}{rcl}
wenzelm@30171
  2015
    @{method_def (HOL) "coherent"} & : & @{text method} \\
wenzelm@30171
  2016
  \end{matharray}
wenzelm@30171
  2017
wenzelm@55112
  2018
  @{rail \<open>
wenzelm@42596
  2019
    @@{method (HOL) coherent} @{syntax thmrefs}?
wenzelm@55112
  2020
  \<close>}
wenzelm@30171
  2021
wenzelm@46280
  2022
  \begin{description}
wenzelm@46280
  2023
wenzelm@46280
  2024
  \item @{method (HOL) coherent} solves problems of \emph{Coherent
wenzelm@46280
  2025
  Logic} \cite{Bezem-Coquand:2005}, which covers applications in
wenzelm@46280
  2026
  confluence theory, lattice theory and projective geometry.  See
wenzelm@46280
  2027
  @{file "~~/src/HOL/ex/Coherent.thy"} for some examples.
wenzelm@46280
  2028
wenzelm@46280
  2029
  \end{description}
wenzelm@30171
  2030
*}
wenzelm@30171
  2031
wenzelm@30171
  2032
blanchet@42215
  2033
section {* Proving propositions *}
blanchet@42215
  2034
blanchet@42215
  2035
text {*
blanchet@42215
  2036
  In addition to the standard proof methods, a number of diagnosis
blanchet@42215
  2037
  tools search for proofs and provide an Isar proof snippet on success.
blanchet@42215
  2038
  These tools are available via the following commands.
blanchet@42215
  2039
blanchet@42215
  2040
  \begin{matharray}{rcl}
blanchet@42215
  2041
    @{command_def (HOL) "solve_direct"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@42215
  2042
    @{command_def (HOL) "try"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@46641
  2043
    @{command_def (HOL) "try0"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@42215
  2044
    @{command_def (HOL) "sledgehammer"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@42215
  2045
    @{command_def (HOL) "sledgehammer_params"} & : & @{text "theory \<rightarrow> theory"}
blanchet@42215
  2046
  \end{matharray}
blanchet@42215
  2047
wenzelm@55112
  2048
  @{rail \<open>
blanchet@43040
  2049
    @@{command (HOL) try}
blanchet@43040
  2050
    ;
blanchet@43040
  2051
blanchet@46641
  2052
    @@{command (HOL) try0} ( ( ( 'simp' | 'intro' | 'elim' | 'dest' ) ':' @{syntax thmrefs} ) + ) ?
wenzelm@42596
  2053
      @{syntax nat}?
blanchet@42215
  2054
    ;
blanchet@43040
  2055
wenzelm@42596
  2056
    @@{command (HOL) sledgehammer} ( '[' args ']' )? facts? @{syntax nat}?
blanchet@42215
  2057
    ;
blanchet@42215
  2058
wenzelm@42596
  2059
    @@{command (HOL) sledgehammer_params} ( ( '[' args ']' ) ? )
blanchet@42215
  2060
    ;
wenzelm@42596
  2061
    args: ( @{syntax name} '=' value + ',' )
blanchet@42215
  2062
    ;
wenzelm@42596
  2063
    facts: '(' ( ( ( ( 'add' | 'del' ) ':' ) ? @{syntax thmrefs} ) + ) ? ')'
wenzelm@55112
  2064
  \<close>} % FIXME check args "value"
blanchet@42215
  2065
blanchet@42215
  2066
  \begin{description}
blanchet@42215
  2067
wenzelm@46283
  2068
  \item @{command (HOL) "solve_direct"} checks whether the current
wenzelm@46283
  2069
  subgoals can be solved directly by an existing theorem. Duplicate
wenzelm@46283
  2070
  lemmas can be detected in this way.
blanchet@42215
  2071
blanchet@46641
  2072
  \item @{command (HOL) "try0"} attempts to prove a subgoal
wenzelm@46283
  2073
  using a combination of standard proof methods (@{method auto},
wenzelm@46283
  2074
  @{method simp}, @{method blast}, etc.).  Additional facts supplied
wenzelm@46283
  2075
  via @{text "simp:"}, @{text "intro:"}, @{text "elim:"}, and @{text
wenzelm@46283
  2076
  "dest:"} are passed to the appropriate proof methods.
blanchet@42215
  2077
bulwahn@43914
  2078
  \item @{command (HOL) "try"} attempts to prove or disprove a subgoal
wenzelm@46283
  2079
  using a combination of provers and disprovers (@{command (HOL)
wenzelm@46283
  2080
  "solve_direct"}, @{command (HOL) "quickcheck"}, @{command (HOL)
blanchet@46641
  2081
  "try0"}, @{command (HOL) "sledgehammer"}, @{command (HOL)
wenzelm@46283
  2082
  "nitpick"}).
bulwahn@43914
  2083
wenzelm@46283
  2084
  \item @{command (HOL) "sledgehammer"} attempts to prove a subgoal
wenzelm@46283
  2085
  using external automatic provers (resolution provers and SMT
wenzelm@46283
  2086
  solvers). See the Sledgehammer manual \cite{isabelle-sledgehammer}
wenzelm@46283
  2087
  for details.
blanchet@42215
  2088
wenzelm@46283
  2089
  \item @{command (HOL) "sledgehammer_params"} changes @{command (HOL)
wenzelm@46283
  2090
  "sledgehammer"} configuration options persistently.
blanchet@42215
  2091
blanchet@42215
  2092
  \end{description}
blanchet@42215
  2093
*}
blanchet@42215
  2094
blanchet@42215
  2095
haftmann@31912
  2096
section {* Checking and refuting propositions *}
haftmann@31912
  2097
haftmann@31912
  2098
text {*
haftmann@31912
  2099
  Identifying incorrect propositions usually involves evaluation of
blanchet@42215
  2100
  particular assignments and systematic counterexample search.  This
haftmann@31912
  2101
  is supported by the following commands.
haftmann@31912
  2102
haftmann@31912
  2103
  \begin{matharray}{rcl}
haftmann@31912
  2104
    @{command_def (HOL) "value"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
bulwahn@45409
  2105
    @{command_def (HOL) "values"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@31912
  2106
    @{command_def (HOL) "quickcheck"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@42215
  2107
    @{command_def (HOL) "nitpick"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
blanchet@42215
  2108
    @{command_def (HOL) "quickcheck_params"} & : & @{text "theory \<rightarrow> theory"} \\
bulwahn@45943
  2109
    @{command_def (HOL) "nitpick_params"} & : & @{text "theory \<rightarrow> theory"} \\
bulwahn@46592
  2110
    @{command_def (HOL) "quickcheck_generator"} & : & @{text "theory \<rightarrow> theory"} \\
bulwahn@46592
  2111
    @{command_def (HOL) "find_unused_assms"} & : & @{text "context \<rightarrow>"}
haftmann@31912
  2112
  \end{matharray}
haftmann@31912
  2113
wenzelm@55112
  2114
  @{rail \<open>
haftmann@58100
  2115
    @@{command (HOL) value} ( '[' @{syntax name} ']' )? modes? @{syntax term}
haftmann@31912
  2116
    ;
haftmann@31912
  2117
bulwahn@45409
  2118
    @@{command (HOL) values} modes? @{syntax nat}? @{syntax term}
bulwahn@45409
  2119
    ;
bulwahn@45409
  2120
blanchet@49993
  2121
    (@@{command (HOL) quickcheck} | @@{command (HOL) nitpick})
wenzelm@42596
  2122
      ( '[' args ']' )? @{syntax nat}?
haftmann@31912
  2123
    ;
haftmann@31912
  2124
blanchet@49993
  2125
    (@@{command (HOL) quickcheck_params} |
wenzelm@42596
  2126
      @@{command (HOL) nitpick_params}) ( '[' args ']' )?
haftmann@31912
  2127
    ;
bulwahn@46592
  2128
wenzelm@55029
  2129
    @@{command (HOL) quickcheck_generator} @{syntax nameref} \<newline>
bulwahn@45943
  2130
      'operations:' ( @{syntax term} +)
bulwahn@45943
  2131
    ;
haftmann@31912
  2132
wenzelm@46628
  2133
    @@{command (HOL) find_unused_assms} @{syntax name}?
bulwahn@46592
  2134
    ;
wenzelm@42596
  2135
    modes: '(' (@{syntax name} +) ')'
haftmann@31912
  2136
    ;
wenzelm@42596
  2137
    args: ( @{syntax name} '=' value + ',' )
wenzelm@55112
  2138
  \<close>} % FIXME check "value"
haftmann@31912
  2139
haftmann@31912
  2140
  \begin{description}
haftmann@31912
  2141
haftmann@31912
  2142
  \item @{command (HOL) "value"}~@{text t} evaluates and prints a
wenzelm@46283
  2143
  term; optionally @{text modes} can be specified, which are appended
wenzelm@46283
  2144
  to the current print mode; see \secref{sec:print-modes}.
haftmann@56927
  2145
  Evaluation is tried first using ML, falling
haftmann@56927
  2146
  back to normalization by evaluation if this fails.
haftmann@58100
  2147
  Alternatively a specific evaluator can be selected using square
haftmann@58100
  2148
  brackets; typical evaluators use the current set of code equations
haftmann@58100
  2149
  to normalize and include @{text simp} for fully symbolic evaluation
haftmann@58100
  2150
  using the simplifier, @{text nbe} for \emph{normalization by
haftmann@58100
  2151
  evaluation} and \emph{code} for code generation in SML.
haftmann@31912
  2152
wenzelm@46283
  2153
  \item @{command (HOL) "values"}~@{text t} enumerates a set
wenzelm@46283
  2154
  comprehension by evaluation and prints its values up to the given
wenzelm@46283
  2155
  number of solutions; optionally @{text modes} can be specified,
wenzelm@46283
  2156
  which are appended to the current print mode; see
wenzelm@46283
  2157
  \secref{sec:print-modes}.
bulwahn@45409
  2158
haftmann@31912
  2159
  \item @{command (HOL) "quickcheck"} tests the current goal for
wenzelm@46283
  2160
  counterexamples using a series of assignments for its free
wenzelm@46283
  2161
  variables; by default the first subgoal is tested, an other can be
wenzelm@46283
  2162
  selected explicitly using an optional goal index.  Assignments can
Christian@52895
  2163
  be chosen exhausting the search space up to a given size, or using a
wenzelm@46283
  2164
  fixed number of random assignments in the search space, or exploring
wenzelm@46283
  2165
  the search space symbolically using narrowing.  By default,
wenzelm@46283
  2166
  quickcheck uses exhaustive testing.  A number of configuration
wenzelm@46283
  2167
  options are supported for @{command (HOL) "quickcheck"}, notably:
haftmann@31912
  2168
haftmann@31912
  2169
    \begin{description}
haftmann@31912
  2170
bulwahn@43914
  2171
    \item[@{text tester}] specifies which testing approach to apply.
wenzelm@46283
  2172
    There are three testers, @{text exhaustive}, @{text random}, and
wenzelm@46283
  2173
    @{text narrowing}.  An unknown configuration option is treated as
wenzelm@46283
  2174
    an argument to tester, making @{text "tester ="} optional.  When
wenzelm@46283
  2175
    multiple testers are given, these are applied in parallel.  If no
wenzelm@46283
  2176
    tester is specified, quickcheck uses the testers that are set
wenzelm@46283
  2177
    active, i.e., configurations @{attribute
wenzelm@46283
  2178
    quickcheck_exhaustive_active}, @{attribute
wenzelm@46283
  2179
    quickcheck_random_active}, @{attribute
wenzelm@46283
  2180
    quickcheck_narrowing_active} are set to true.
wenzelm@46283
  2181
wenzelm@40254
  2182
    \item[@{text size}] specifies the maximum size of the search space
wenzelm@40254
  2183
    for assignment values.
haftmann@31912
  2184
bulwahn@45758
  2185
    \item[@{text genuine_only}] sets quickcheck only to return genuine
wenzelm@46283
  2186
    counterexample, but not potentially spurious counterexamples due
wenzelm@46283
  2187
    to underspecified functions.
bulwahn@46498
  2188
bulwahn@46498
  2189
    \item[@{text abort_potential}] sets quickcheck to abort once it
bulwahn@46498
  2190
    found a potentially spurious counterexample and to not continue
bulwahn@46498
  2191
    to search for a further genuine counterexample.
bulwahn@46498
  2192
    For this option to be effective, the @{text genuine_only} option
bulwahn@46498
  2193
    must be set to false.
wenzelm@47859
  2194
bulwahn@42092
  2195
    \item[@{text eval}] takes a term or a list of terms and evaluates
wenzelm@46283
  2196
    these terms under the variable assignment found by quickcheck.
bulwahn@48159
  2197
    This option is currently only supported by the default
bulwahn@48159
  2198
    (exhaustive) tester.
wenzelm@42123
  2199
wenzelm@40254
  2200
    \item[@{text iterations}] sets how many sets of assignments are
wenzelm@40254
  2201
    generated for each particular size.
haftmann@31912
  2202
wenzelm@40254
  2203
    \item[@{text no_assms}] specifies whether assumptions in
wenzelm@40254
  2204
    structured proofs should be ignored.
blanchet@35331
  2205
bulwahn@47349
  2206
    \item[@{text locale}] specifies how to process conjectures in
bulwahn@47349
  2207
    a locale context, i.e., they can be interpreted or expanded.
bulwahn@47349
  2208
    The option is a whitespace-separated list of the two words
bulwahn@47349
  2209
    @{text interpret} and @{text expand}. The list determines the
wenzelm@47859
  2210
    order they are employed. The default setting is to first use
bulwahn@47349
  2211
    interpretations and then test the expanded conjecture.
bulwahn@47349
  2212
    The option is only provided as attribute declaration, but not
wenzelm@47859
  2213
    as parameter to the command.
bulwahn@47349
  2214
wenzelm@40254
  2215
    \item[@{text timeout}] sets the time limit in seconds.
bulwahn@40245
  2216
wenzelm@40254
  2217
    \item[@{text default_type}] sets the type(s) generally used to
wenzelm@40254
  2218
    instantiate type variables.
bulwahn@40245
  2219
wenzelm@40254
  2220
    \item[@{text report}] if set quickcheck reports how many tests
wenzelm@40254
  2221
    fulfilled the preconditions.
bulwahn@40245
  2222
bulwahn@46592
  2223
    \item[@{text use_subtype}] if set quickcheck automatically lifts
bulwahn@46592
  2224
    conjectures to registered subtypes if possible, and tests the
bulwahn@46592
  2225
    lifted conjecture.
bulwahn@46592
  2226
bulwahn@45766
  2227
    \item[@{text quiet}] if set quickcheck does not output anything
bulwahn@45766
  2228
    while testing.
wenzelm@47859
  2229
wenzelm@46283
  2230
    \item[@{text verbose}] if set quickcheck informs about the current
wenzelm@46283
  2231
    size and cardinality while testing.
bulwahn@40245
  2232
wenzelm@40254
  2233
    \item[@{text expect}] can be used to check if the user's
wenzelm@40254
  2234
    expectation was met (@{text no_expectation}, @{text
wenzelm@40254
  2235
    no_counterexample}, or @{text counterexample}).
bulwahn@40245
  2236
haftmann@31912
  2237
    \end{description}
haftmann@31912
  2238
wenzelm@46283
  2239
  These option can be given within square brackets.
haftmann@31912
  2240
Andreas@56363
  2241
  Using the following type classes, the testers generate values and convert
Andreas@56363
  2242
  them back into Isabelle terms for displaying counterexamples.
Andreas@56363
  2243
    \begin{description}
Andreas@56363
  2244
    \item[@{text exhaustive}] The parameters of the type classes @{class exhaustive}
Andreas@56363
  2245
      and @{class full_exhaustive} implement the testing. They take a 
Andreas@56363
  2246
      testing function as a parameter, which takes a value of type @{typ "'a"}
Andreas@56363
  2247
      and optionally produces a counterexample, and a size parameter for the test values.
Andreas@56363
  2248
      In @{class full_exhaustive}, the testing function parameter additionally 
Andreas@56363
  2249
      expects a lazy term reconstruction in the type @{typ Code_Evaluation.term}
Andreas@56363
  2250
      of the tested value.
Andreas@56363
  2251
Andreas@56363
  2252
      The canonical implementation for @{text exhaustive} testers calls the given
Andreas@56363
  2253
      testing function on all values up to the given size and stops as soon
Andreas@56363
  2254
      as a counterexample is found.
Andreas@56363
  2255
Andreas@56363
  2256
    \item[@{text random}] The operation @{const Quickcheck_Random.random}
Andreas@56363
  2257
      of the type class @{class random} generates a pseudo-random
Andreas@56363
  2258
      value of the given size and a lazy term reconstruction of the value
Andreas@56363
  2259
      in the type @{typ Code_Evaluation.term}. A pseudo-randomness generator
Andreas@56363
  2260
      is defined in theory @{theory Random}.
Andreas@56363
  2261
      
Andreas@56363
  2262
    \item[@{text narrowing}] implements Haskell's Lazy Smallcheck~\cite{runciman-naylor-lindblad}
Andreas@56363
  2263
      using the type classes @{class narrowing} and @{class partial_term_of}.
Andreas@56363
  2264
      Variables in the current goal are initially represented as symbolic variables.
Andreas@56363
  2265
      If the execution of the goal tries to evaluate one of them, the test engine
Andreas@56363
  2266
      replaces it with refinements provided by @{const narrowing}.
Andreas@56363
  2267
      Narrowing views every value as a sum-of-products which is expressed using the operations
Andreas@56363
  2268
      @{const Quickcheck_Narrowing.cons} (embedding a value),
Andreas@56363
  2269
      @{const Quickcheck_Narrowing.apply} (product) and @{const Quickcheck_Narrowing.sum} (sum).
Andreas@56363
  2270
      The refinement should enable further evaluation of the goal.
Andreas@56363
  2271
Andreas@56363
  2272
      For example, @{const narrowing} for the list type @{typ "'a :: narrowing list"}
Andreas@56363
  2273
      can be recursively defined as
Andreas@56363
  2274
      @{term "Quickcheck_Narrowing.sum (Quickcheck_Narrowing.cons [])
Andreas@56363
  2275
                (Quickcheck_Narrowing.apply
Andreas@56363
  2276
                  (Quickcheck_Narrowing.apply
Andreas@56363
  2277
                    (Quickcheck_Narrowing.cons (op #))
Andreas@56363
  2278
                    narrowing)
Andreas@56363
  2279
                  narrowing)"}.
Andreas@56363
  2280
      If a symbolic variable of type @{typ "_ list"} is evaluated, it is replaced by (i)~the empty
Andreas@56363
  2281
      list @{term "[]"} and (ii)~by a non-empty list whose head and tail can then be recursively
Andreas@56363
  2282
      refined if needed.
Andreas@56363
  2283
Andreas@56363
  2284
      To reconstruct counterexamples, the operation @{const partial_term_of} transforms
Andreas@56363
  2285
      @{text narrowing}'s deep representation of terms to the type @{typ Code_Evaluation.term}.
Andreas@56363
  2286
      The deep representation models symbolic variables as
Andreas@56363
  2287
      @{const Quickcheck_Narrowing.Narrowing_variable}, which are normally converted to
Andreas@56363
  2288
      @{const Code_Evaluation.Free}, and refined values as
Andreas@56363
  2289
      @{term "Quickcheck_Narrowing.Narrowing_constructor i args"}, where @{term "i :: integer"}
Andreas@56363
  2290
      denotes the index in the sum of refinements. In the above example for lists,
Andreas@56363
  2291
      @{term "0"} corresponds to @{term "[]"} and @{term "1"}
Andreas@56363
  2292
      to @{term "op #"}.
Andreas@56363
  2293
Andreas@56363
  2294
      The command @{command (HOL) "code_datatype"} sets up @{const partial_term_of}
Andreas@56363
  2295
      such that the @{term "i"}-th refinement is interpreted as the @{term "i"}-th constructor,
Andreas@56363
  2296
      but it does not ensures consistency with @{const narrowing}.
Andreas@56363
  2297
    \end{description}
Andreas@56363
  2298
wenzelm@46283
  2299
  \item @{command (HOL) "quickcheck_params"} changes @{command (HOL)
wenzelm@46283
  2300
  "quickcheck"} configuration options persistently.
blanchet@42215
  2301
bulwahn@45943
  2302
  \item @{command (HOL) "quickcheck_generator"} creates random and
wenzelm@46283
  2303
  exhaustive value generators for a given type and operations.  It
wenzelm@46283
  2304
  generates values by using the operations as if they were
wenzelm@46283
  2305
  constructors of that type.
bulwahn@45943
  2306
wenzelm@46283
  2307
  \item @{command (HOL) "nitpick"} tests the current goal for
wenzelm@46283
  2308
  counterexamples using a reduction to first-order relational
wenzelm@46283
  2309
  logic. See the Nitpick manual \cite{isabelle-nitpick} for details.
blanchet@42215
  2310
wenzelm@46283
  2311
  \item @{command (HOL) "nitpick_params"} changes @{command (HOL)
wenzelm@46283
  2312
  "nitpick"} configuration options persistently.
haftmann@31912
  2313
bulwahn@46592
  2314
  \item @{command (HOL) "find_unused_assms"} finds potentially superfluous
bulwahn@46592
  2315
  assumptions in theorems using quickcheck.
bulwahn@46592
  2316
  It takes the theory name to be checked for superfluous assumptions as
bulwahn@46592
  2317
  optional argument. If not provided, it checks the current theory.
bulwahn@46592
  2318
  Options to the internal quickcheck invocations can be changed with
bulwahn@46592
  2319
  common configuration declarations.
bulwahn@46592
  2320
haftmann@31912
  2321
  \end{description}
haftmann@31912
  2322
*}
haftmann@31912
  2323
haftmann@31912
  2324
wenzelm@28752
  2325
section {* Unstructured case analysis and induction \label{sec:hol-induct-tac} *}
wenzelm@26849
  2326
wenzelm@26849
  2327
text {*
wenzelm@27123
  2328
  The following tools of Isabelle/HOL support cases analysis and
wenzelm@27123
  2329
  induction in unstructured tactic scripts; see also
wenzelm@27123
  2330
  \secref{sec:cases-induct} for proper Isar versions of similar ideas.
wenzelm@26849
  2331
wenzelm@26849
  2332
  \begin{matharray}{rcl}
wenzelm@28761
  2333
    @{method_def (HOL) case_tac}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  2334
    @{method_def (HOL) induct_tac}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  2335
    @{method_def (HOL) ind_cases}@{text "\<^sup>*"} & : & @{text method} \\
wenzelm@28761
  2336
    @{command_def (HOL) "inductive_cases"}@{text "\<^sup>*"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@26849
  2337
  \end{matharray}
wenzelm@26849
  2338
wenzelm@55112
  2339
  @{rail \<open>
wenzelm@42705
  2340
    @@{method (HOL) case_tac} @{syntax goal_spec}? @{syntax term} rule?
wenzelm@26849
  2341
    ;
wenzelm@42705
  2342
    @@{method (HOL) induct_tac} @{syntax goal_spec}? (@{syntax insts} * @'and') rule?
wenzelm@26849
  2343
    ;
wenzelm@42596
  2344
    @@{method (HOL) ind_cases} (@{syntax prop}+) (@'for' (@{syntax name}+))?
wenzelm@26849
  2345
    ;
wenzelm@42596
  2346
    @@{command (HOL) inductive_cases} (@{syntax thmdecl}? (@{syntax prop}+) + @'and')
wenzelm@26849
  2347
    ;
wenzelm@42596
  2348
    rule: 'rule' ':' @{syntax thmref}
wenzelm@55112
  2349
  \<close>}
wenzelm@26849
  2350
wenzelm@28760
  2351
  \begin{description}
wenzelm@26849
  2352
wenzelm@28760
  2353
  \item @{method (HOL) case_tac} and @{method (HOL) induct_tac} admit
wenzelm@28760
  2354
  to reason about inductive types.  Rules are selected according to
wenzelm@28760
  2355
  the declarations by the @{attribute cases} and @{attribute induct}
wenzelm@28760
  2356
  attributes, cf.\ \secref{sec:cases-induct}.  The @{command (HOL)
blanchet@58305
  2357
  datatype_new} package already takes care of this.
wenzelm@27123
  2358
wenzelm@27123
  2359
  These unstructured tactics feature both goal addressing and dynamic
wenzelm@26849
  2360
  instantiation.  Note that named rule cases are \emph{not} provided
wenzelm@27123
  2361
  as would be by the proper @{method cases} and @{method induct} proof
wenzelm@27123
  2362
  methods (see \secref{sec:cases-induct}).  Unlike the @{method
wenzelm@27123
  2363
  induct} method, @{method induct_tac} does not handle structured rule
wenzelm@27123
  2364
  statements, only the compact object-logic conclusion of the subgoal
wenzelm@27123
  2365
  being addressed.
wenzelm@42123
  2366
wenzelm@28760
  2367
  \item @{method (HOL) ind_cases} and @{command (HOL)
wenzelm@28760
  2368
  "inductive_cases"} provide an interface to the internal @{ML_text
wenzelm@26860
  2369
  mk_cases} operation.  Rules are simplified in an unrestricted
wenzelm@26860
  2370
  forward manner.
wenzelm@26849
  2371
wenzelm@26849
  2372
  While @{method (HOL) ind_cases} is a proof method to apply the
wenzelm@26849
  2373
  result immediately as elimination rules, @{command (HOL)
wenzelm@26849
  2374
  "inductive_cases"} provides case split theorems at the theory level
wenzelm@26849
  2375
  for later use.  The @{keyword "for"} argument of the @{method (HOL)
wenzelm@26849
  2376
  ind_cases} method allows to specify a list of variables that should
wenzelm@26849
  2377
  be generalized before applying the resulting rule.
wenzelm@26849
  2378
wenzelm@28760
  2379
  \end{description}
wenzelm@26849
  2380
*}
wenzelm@26849
  2381
wenzelm@26849
  2382
wenzelm@50109
  2383
chapter {* Executable code *}
wenzelm@26849
  2384
wenzelm@42627
  2385
text {* For validation purposes, it is often useful to \emph{execute}
wenzelm@42627
  2386
  specifications.  In principle, execution could be simulated by
wenzelm@42627
  2387
  Isabelle's inference kernel, i.e. by a combination of resolution and
wenzelm@42627
  2388
  simplification.  Unfortunately, this approach is rather inefficient.
wenzelm@42627
  2389
  A more efficient way of executing specifications is to translate
wenzelm@42627
  2390
  them into a functional programming language such as ML.
wenzelm@26849
  2391
wenzelm@45192
  2392
  Isabelle provides a generic framework to support code generation
wenzelm@42627
  2393
  from executable specifications.  Isabelle/HOL instantiates these
wenzelm@45192
  2394
  mechanisms in a way that is amenable to end-user applications.  Code
wenzelm@45192
  2395
  can be generated for functional programs (including overloading
wenzelm@45192
  2396
  using type classes) targeting SML \cite{SML}, OCaml \cite{OCaml},
wenzelm@45192
  2397
  Haskell \cite{haskell-revised-report} and Scala
wenzelm@42627
  2398
  \cite{scala-overview-tech-report}.  Conceptually, code generation is
wenzelm@42627
  2399
  split up in three steps: \emph{selection} of code theorems,
wenzelm@42627
  2400
  \emph{translation} into an abstract executable view and
wenzelm@42627
  2401
  \emph{serialization} to a specific \emph{target language}.
wenzelm@42627
  2402
  Inductive specifications can be executed using the predicate
wenzelm@42627
  2403
  compiler which operates within HOL.  See \cite{isabelle-codegen} for
wenzelm@42627
  2404
  an introduction.
haftmann@37422
  2405
haftmann@37422
  2406
  \begin{matharray}{rcl}
haftmann@37422
  2407
    @{command_def (HOL) "export_code"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
haftmann@37422
  2408
    @{attribute_def (HOL) code} & : & @{text attribute} \\