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