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