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