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