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