src/Doc/Implementation/Eq.thy
author wenzelm
Sun, 15 Jan 2023 18:30:18 +0100
changeset 76987 4c275405faae
parent 73764 35d8132633c6
permissions -rw-r--r--
isabelle update -u cite;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61656
cfabbc083977 more uniform jEdit properties;
wenzelm
parents: 61493
diff changeset
     1
(*:maxLineLen=78:*)
cfabbc083977 more uniform jEdit properties;
wenzelm
parents: 61493
diff changeset
     2
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
     3
theory Eq
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
     4
imports Base
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
     5
begin
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
     6
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
     7
chapter \<open>Equational reasoning\<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
     8
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
     9
text \<open>
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    10
  Equality is one of the most fundamental concepts of mathematics. The
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    11
  Isabelle/Pure logic (\chref{ch:logic}) provides a builtin relation \<open>\<equiv> :: \<alpha> \<Rightarrow>
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    12
  \<alpha> \<Rightarrow> prop\<close> that expresses equality of arbitrary terms (or propositions) at
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    13
  the framework level, as expressed by certain basic inference rules
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    14
  (\secref{sec:eq-rules}).
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    15
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    16
  Equational reasoning means to replace equals by equals, using reflexivity
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    17
  and transitivity to form chains of replacement steps, and congruence rules
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    18
  to access sub-structures. Conversions (\secref{sec:conv}) provide a
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    19
  convenient framework to compose basic equational steps to build specific
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    20
  equational reasoning tools.
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    21
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    22
  Higher-order matching is able to provide suitable instantiations for giving
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    23
  equality rules, which leads to the versatile concept of \<open>\<lambda>\<close>-term rewriting
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    24
  (\secref{sec:rewriting}). Internally this is based on the general-purpose
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    25
  Simplifier engine of Isabelle, which is more specific and more efficient
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    26
  than plain conversions.
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    27
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    28
  Object-logics usually introduce specific notions of equality or equivalence,
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    29
  and relate it with the Pure equality. This enables to re-use the Pure tools
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    30
  for equational reasoning for particular object-logic connectives as well.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    31
\<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    32
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    33
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    34
section \<open>Basic equality rules \label{sec:eq-rules}\<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    35
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    36
text \<open>
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    37
  Isabelle/Pure uses \<open>\<equiv>\<close> for equality of arbitrary terms, which includes
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    38
  equivalence of propositions of the logical framework. The conceptual
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    39
  axiomatization of the constant \<open>\<equiv> :: \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> prop\<close> is given in
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    40
  \figref{fig:pure-equality}. The inference kernel presents slightly different
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    41
  equality rules, which may be understood as derived rules from this minimal
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    42
  axiomatization. The Pure theory also provides some theorems that express the
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    43
  same reasoning schemes as theorems that can be composed like object-level
50085
24ef81a22ee9 updated basic equality rules;
wenzelm
parents: 48985
diff changeset
    44
  rules as explained in \secref{sec:obj-rules}.
24ef81a22ee9 updated basic equality rules;
wenzelm
parents: 48985
diff changeset
    45
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 63680
diff changeset
    46
  For example, \<^ML>\<open>Thm.symmetric\<close> as Pure inference is an ML function that
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    47
  maps a theorem \<open>th\<close> stating \<open>t \<equiv> u\<close> to one stating \<open>u \<equiv> t\<close>. In contrast,
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    48
  @{thm [source] Pure.symmetric} as Pure theorem expresses the same reasoning
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    49
  in declarative form. If used like \<open>th [THEN Pure.symmetric]\<close> in Isar source
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    50
  notation, it achieves a similar effect as the ML inference function,
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 63680
diff changeset
    51
  although the rule attribute @{attribute THEN} or ML operator \<^ML>\<open>op RS\<close>
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    52
  involve the full machinery of higher-order unification (modulo
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    53
  \<open>\<beta>\<eta>\<close>-conversion) and lifting of \<open>\<And>/\<Longrightarrow>\<close> contexts.
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    54
\<close>
50085
24ef81a22ee9 updated basic equality rules;
wenzelm
parents: 48985
diff changeset
    55
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    56
text %mlref \<open>
50085
24ef81a22ee9 updated basic equality rules;
wenzelm
parents: 48985
diff changeset
    57
  \begin{mldecls}
73764
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    58
  @{define_ML Thm.reflexive: "cterm -> thm"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    59
  @{define_ML Thm.symmetric: "thm -> thm"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    60
  @{define_ML Thm.transitive: "thm -> thm -> thm"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    61
  @{define_ML Thm.abstract_rule: "string -> cterm -> thm -> thm"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    62
  @{define_ML Thm.combination: "thm -> thm -> thm"} \\[0.5ex]
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    63
  @{define_ML Thm.equal_intr: "thm -> thm -> thm"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    64
  @{define_ML Thm.equal_elim: "thm -> thm -> thm"} \\
50085
24ef81a22ee9 updated basic equality rules;
wenzelm
parents: 48985
diff changeset
    65
  \end{mldecls}
24ef81a22ee9 updated basic equality rules;
wenzelm
parents: 48985
diff changeset
    66
63680
6e1e8b5abbfa more symbols;
wenzelm
parents: 61854
diff changeset
    67
  See also \<^file>\<open>~~/src/Pure/thm.ML\<close> for further description of these inference
6e1e8b5abbfa more symbols;
wenzelm
parents: 61854
diff changeset
    68
  rules, and a few more for primitive \<open>\<beta>\<close> and \<open>\<eta>\<close> conversions. Note that \<open>\<alpha>\<close>
6e1e8b5abbfa more symbols;
wenzelm
parents: 61854
diff changeset
    69
  conversion is implicit due to the representation of terms with de-Bruijn
6e1e8b5abbfa more symbols;
wenzelm
parents: 61854
diff changeset
    70
  indices (\secref{sec:terms}).
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
    71
\<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    72
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    73
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    74
section \<open>Conversions \label{sec:conv}\<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    75
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    76
text \<open>
76987
4c275405faae isabelle update -u cite;
wenzelm
parents: 73764
diff changeset
    77
  The classic article \<^cite>\<open>"paulson:1983"\<close> introduces the concept of
73592
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    78
  conversion for Cambridge LCF. This was historically important to implement
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    79
  all kinds of ``simplifiers'', but the Isabelle Simplifier is done quite
76987
4c275405faae isabelle update -u cite;
wenzelm
parents: 73764
diff changeset
    80
  differently, see \<^cite>\<open>\<open>\S9.3\<close> in "isabelle-isar-ref"\<close>.
73592
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    81
\<close>
50122
7ae7efef5ad8 more refs;
wenzelm
parents: 50085
diff changeset
    82
73592
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    83
text %mlref \<open>
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    84
  \begin{mldecls}
73764
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    85
  @{define_ML_structure Conv} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    86
  @{define_ML_type conv} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
    87
  @{define_ML Simplifier.asm_full_rewrite : "Proof.context -> conv"} \\
73592
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    88
  \end{mldecls}
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    89
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    90
  \<^descr> \<^ML_structure>\<open>Conv\<close> is a library of combinators to build conversions,
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    91
  over type \<^ML_type>\<open>conv\<close> (see also \<^file>\<open>~~/src/Pure/conv.ML\<close>). This is one
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    92
  of the few Isabelle/ML modules that are usually used with \<^verbatim>\<open>open\<close>: finding
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    93
  examples works by searching for ``\<^verbatim>\<open>open Conv\<close>'' instead of ``\<^verbatim>\<open>Conv.\<close>''.
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    94
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    95
  \<^descr> \<^ML>\<open>Simplifier.asm_full_rewrite\<close> invokes the Simplifier as a
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    96
  conversion. There are a few related operations, corresponding to the various
c642c3cbbf0e more documentation on "Conversions";
wenzelm
parents: 69597
diff changeset
    97
  modes of simplification.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    98
\<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
    99
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   100
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   101
section \<open>Rewriting \label{sec:rewriting}\<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   102
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   103
text \<open>
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   104
  Rewriting normalizes a given term (theorem or goal) by replacing instances
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   105
  of given equalities \<open>t \<equiv> u\<close> in subterms. Rewriting continues until no
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   106
  rewrites are applicable to any subterm. This may be used to unfold simple
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   107
  definitions of the form \<open>f x\<^sub>1 \<dots> x\<^sub>n \<equiv> u\<close>, but is slightly more general than
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   108
  that. \<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   109
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   110
text %mlref \<open>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   111
  \begin{mldecls}
73764
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
   112
  @{define_ML rewrite_rule: "Proof.context -> thm list -> thm -> thm"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
   113
  @{define_ML rewrite_goals_rule: "Proof.context -> thm list -> thm -> thm"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
   114
  @{define_ML rewrite_goal_tac: "Proof.context -> thm list -> int -> tactic"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
   115
  @{define_ML rewrite_goals_tac: "Proof.context -> thm list -> tactic"} \\
35d8132633c6 clarified names;
wenzelm
parents: 73592
diff changeset
   116
  @{define_ML fold_goals_tac: "Proof.context -> thm list -> tactic"} \\
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   117
  \end{mldecls}
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   118
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 63680
diff changeset
   119
  \<^descr> \<^ML>\<open>rewrite_rule\<close>~\<open>ctxt rules thm\<close> rewrites the whole theorem by the
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   120
  given rules.
46486
4a607979290d updated rewrite_goals_rule, rewrite_rule;
wenzelm
parents: 46295
diff changeset
   121
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 63680
diff changeset
   122
  \<^descr> \<^ML>\<open>rewrite_goals_rule\<close>~\<open>ctxt rules thm\<close> rewrites the outer premises of
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   123
  the given theorem. Interpreting the same as a goal state
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   124
  (\secref{sec:tactical-goals}) it means to rewrite all subgoals (in the same
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 63680
diff changeset
   125
  manner as \<^ML>\<open>rewrite_goals_tac\<close>).
46486
4a607979290d updated rewrite_goals_rule, rewrite_rule;
wenzelm
parents: 46295
diff changeset
   126
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 63680
diff changeset
   127
  \<^descr> \<^ML>\<open>rewrite_goal_tac\<close>~\<open>ctxt rules i\<close> rewrites subgoal \<open>i\<close> by the given
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   128
  rewrite rules.
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   129
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 63680
diff changeset
   130
  \<^descr> \<^ML>\<open>rewrite_goals_tac\<close>~\<open>ctxt rules\<close> rewrites all subgoals by the given
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   131
  rewrite rules.
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   132
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 63680
diff changeset
   133
  \<^descr> \<^ML>\<open>fold_goals_tac\<close>~\<open>ctxt rules\<close> essentially uses \<^ML>\<open>rewrite_goals_tac\<close>
61854
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   134
  with the symmetric form of each member of \<open>rules\<close>, re-ordered to fold longer
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   135
  expression first. This supports to idea to fold primitive definitions that
38b049cd3aad tuned whitespace;
wenzelm
parents: 61656
diff changeset
   136
  appear in expended form in the proof state.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   137
\<close>
46295
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   138
2548a85b0e02 basic setup for equational reasoning;
wenzelm
parents:
diff changeset
   139
end