src/Pure/meta_simplifier.ML
author berghofe
Tue, 01 Oct 2002 11:17:25 +0200
changeset 13614 0b91269c0b13
parent 13607 6908230623a3
child 13661 ec97dfc2bfe0
permissions -rw-r--r--
Deleted superfluous dest_implies.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
     1
(*  Title:      Pure/meta_simplifier.ML
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
     2
    ID:         $Id$
11672
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
     3
    Author:     Tobias Nipkow and Stefan Berghofer
12783
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
     5
11672
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
     6
Meta-level Simplification.
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
     7
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
     8
11672
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
     9
signature BASIC_META_SIMPLIFIER =
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    10
sig
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    11
  val trace_simp: bool ref
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    12
  val debug_simp: bool ref
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    13
end;
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    14
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    15
signature META_SIMPLIFIER =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    16
sig
11672
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    17
  include BASIC_META_SIMPLIFIER
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    18
  exception SIMPLIFIER of string * thm
13486
54464ea94d6f exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents: 13458
diff changeset
    19
  exception SIMPROC_FAIL of string * exn
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    20
  type meta_simpset
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    21
  val dest_mss          : meta_simpset ->
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    22
    {simps: thm list, congs: thm list, procs: (string * cterm list) list}
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    23
  val empty_mss         : meta_simpset
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    24
  val clear_mss         : meta_simpset -> meta_simpset
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    25
  val merge_mss         : meta_simpset * meta_simpset -> meta_simpset
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    26
  val add_simps         : meta_simpset * thm list -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    27
  val del_simps         : meta_simpset * thm list -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    28
  val mss_of            : thm list -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    29
  val add_congs         : meta_simpset * thm list -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    30
  val del_congs         : meta_simpset * thm list -> meta_simpset
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    31
  val add_simprocs      : meta_simpset *
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    32
    (string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp) list
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    33
      -> meta_simpset
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    34
  val del_simprocs      : meta_simpset *
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    35
    (string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp) list
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    36
      -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    37
  val add_prems         : meta_simpset * thm list -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    38
  val prems_of_mss      : meta_simpset -> thm list
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    39
  val set_mk_rews       : meta_simpset * (thm -> thm list) -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    40
  val set_mk_sym        : meta_simpset * (thm -> thm option) -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    41
  val set_mk_eq_True    : meta_simpset * (thm -> thm option) -> meta_simpset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    42
  val set_termless      : meta_simpset * (term * term -> bool) -> meta_simpset
12779
c5739c1431ab export beta_eta_conversion;
wenzelm
parents: 12603
diff changeset
    43
  val beta_eta_conversion: cterm -> thm
11672
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    44
  val rewrite_cterm: bool * bool * bool ->
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    45
    (meta_simpset -> thm -> thm option) -> meta_simpset -> cterm -> thm
11736
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
    46
  val goals_conv        : (int -> bool) -> (cterm -> thm) -> cterm -> thm
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
    47
  val forall_conv       : (cterm -> thm) -> cterm -> thm
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
    48
  val fconv_rule        : (cterm -> thm) -> thm -> thm
11767
7380c9d45626 tuned rewrite/simplify interface;
wenzelm
parents: 11760
diff changeset
    49
  val rewrite_aux       : (meta_simpset -> thm -> thm option) -> bool -> thm list -> cterm -> thm
7380c9d45626 tuned rewrite/simplify interface;
wenzelm
parents: 11760
diff changeset
    50
  val simplify_aux      : (meta_simpset -> thm -> thm option) -> bool -> thm list -> thm -> thm
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    51
  val rewrite_thm       : bool * bool * bool
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    52
                          -> (meta_simpset -> thm -> thm option)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    53
                          -> meta_simpset -> thm -> thm
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    54
  val rewrite_goals_rule_aux: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    55
  val rewrite_goal_rule : bool* bool * bool
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    56
                          -> (meta_simpset -> thm -> thm option)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    57
                          -> meta_simpset -> int -> thm -> thm
13196
08c42252346f Changed interface of rewrite_term.
berghofe
parents: 12979
diff changeset
    58
  val rewrite_term: Sign.sg -> thm list -> (term -> term option) list -> term -> term
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    59
end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    60
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    61
structure MetaSimplifier : META_SIMPLIFIER =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    62
struct
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    63
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    64
(** diagnostics **)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    65
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    66
exception SIMPLIFIER of string * thm;
13486
54464ea94d6f exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents: 13458
diff changeset
    67
exception SIMPROC_FAIL of string * exn;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    68
11505
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
    69
val simp_depth = ref 0;
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
    70
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    71
local
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    72
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    73
fun println a =
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    74
  tracing ((case ! simp_depth of 0 => "" | n => "[" ^ string_of_int n ^ "]") ^ a);
11505
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
    75
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
    76
fun prnt warn a = if warn then warning a else println a;
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    77
fun prtm warn a sign t = prnt warn (a ^ "\n" ^ Sign.string_of_term sign t);
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    78
fun prctm warn a t = prnt warn (a ^ "\n" ^ Display.string_of_cterm t);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    79
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    80
in
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    81
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
    82
fun prthm warn a = prctm warn a o Thm.cprop_of;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    83
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    84
val trace_simp = ref false;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    85
val debug_simp = ref false;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    86
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    87
fun trace warn a = if !trace_simp then prnt warn a else ();
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    88
fun debug warn a = if !debug_simp then prnt warn a else ();
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    89
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    90
fun trace_term warn a sign t = if !trace_simp then prtm warn a sign t else ();
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    91
fun trace_cterm warn a t = if !trace_simp then prctm warn a t else ();
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    92
fun debug_term warn a sign t = if !debug_simp then prtm warn a sign t else ();
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    93
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
    94
fun trace_thm a thm =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    95
  let val {sign, prop, ...} = rep_thm thm
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
    96
  in trace_term false a sign prop end;
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
    97
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
    98
fun trace_named_thm a (thm, name) =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
    99
  trace_thm (a ^ (if name = "" then "" else " " ^ quote name) ^ ":") thm;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   100
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   101
end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   102
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   103
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   104
(** meta simp sets **)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   105
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   106
(* basic components *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   107
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   108
type rrule = {thm: thm, name: string, lhs: term, elhs: cterm, fo: bool, perm: bool};
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   109
(* thm: the rewrite rule
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   110
   name: name of theorem from which rewrite rule was extracted
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   111
   lhs: the left-hand side
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   112
   elhs: the etac-contracted lhs.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   113
   fo:  use first-order matching
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   114
   perm: the rewrite rule is permutative
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   115
Remarks:
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   116
  - elhs is used for matching,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   117
    lhs only for preservation of bound variable names.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   118
  - fo is set iff
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   119
    either elhs is first-order (no Var is applied),
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   120
           in which case fo-matching is complete,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   121
    or elhs is not a pattern,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   122
       in which case there is nothing better to do.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   123
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   124
type cong = {thm: thm, lhs: cterm};
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   125
type simproc =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   126
 {name: string, proc: Sign.sg -> thm list -> term -> thm option, lhs: cterm, id: stamp};
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   127
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   128
fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   129
  #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   130
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   131
fun eq_cong ({thm = thm1, ...}: cong, {thm = thm2, ...}: cong) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   132
  #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   133
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   134
fun eq_prem (thm1, thm2) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   135
  #prop (rep_thm thm1) aconv #prop (rep_thm thm2);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   136
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   137
fun eq_simproc ({id = s1, ...}:simproc, {id = s2, ...}:simproc) = (s1 = s2);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   138
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   139
fun mk_simproc (name, proc, lhs, id) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   140
  {name = name, proc = proc, lhs = lhs, id = id};
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   141
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   142
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   143
(* datatype mss *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   144
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   145
(*
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   146
  A "mss" contains data needed during conversion:
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   147
    rules: discrimination net of rewrite rules;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   148
    congs: association list of congruence rules and
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   149
           a list of `weak' congruence constants.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   150
           A congruence is `weak' if it avoids normalization of some argument.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   151
    procs: discrimination net of simplification procedures
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   152
      (functions that prove rewrite rules on the fly);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   153
    bounds: names of bound variables already used
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   154
      (for generating new names when rewriting under lambda abstractions);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   155
    prems: current premises;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   156
    mk_rews: mk: turns simplification thms into rewrite rules;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   157
             mk_sym: turns == around; (needs Drule!)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   158
             mk_eq_True: turns P into P == True - logic specific;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   159
    termless: relation for ordered rewriting;
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   160
    depth: depth of conditional rewriting;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   161
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   162
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   163
datatype meta_simpset =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   164
  Mss of {
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   165
    rules: rrule Net.net,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   166
    congs: (string * cong) list * string list,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   167
    procs: simproc Net.net,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   168
    bounds: string list,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   169
    prems: thm list,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   170
    mk_rews: {mk: thm -> thm list,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   171
              mk_sym: thm -> thm option,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   172
              mk_eq_True: thm -> thm option},
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   173
    termless: term * term -> bool,
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   174
    depth: int};
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   175
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   176
fun mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless, depth) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   177
  Mss {rules = rules, congs = congs, procs = procs, bounds = bounds,
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   178
       prems=prems, mk_rews=mk_rews, termless=termless, depth=depth};
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   179
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   180
fun upd_rules(Mss{rules,congs,procs,bounds,prems,mk_rews,termless,depth}, rules') =
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   181
  mk_mss(rules',congs,procs,bounds,prems,mk_rews,termless,depth);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   182
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   183
val empty_mss =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   184
  let val mk_rews = {mk = K [], mk_sym = K None, mk_eq_True = K None}
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   185
  in mk_mss (Net.empty, ([], []), Net.empty, [], [], mk_rews, Term.termless, 0) end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   186
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   187
fun clear_mss (Mss {mk_rews, termless, ...}) =
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   188
  mk_mss (Net.empty, ([], []), Net.empty, [], [], mk_rews, termless,0);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   189
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   190
fun incr_depth(Mss{rules,congs,procs,bounds,prems,mk_rews,termless,depth}) =
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   191
  mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless, depth+1)
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   192
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   193
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   194
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   195
(** simpset operations **)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   196
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   197
(* term variables *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   198
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   199
val add_term_varnames = foldl_aterms (fn (xs, Var (x, _)) => ins_ix (x, xs) | (xs, _) => xs);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   200
fun term_varnames t = add_term_varnames ([], t);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   201
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   202
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   203
(* dest_mss *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   204
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   205
fun dest_mss (Mss {rules, congs, procs, ...}) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   206
  {simps = map (fn (_, {thm, ...}) => thm) (Net.dest rules),
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   207
   congs = map (fn (_, {thm, ...}) => thm) (fst congs),
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   208
   procs =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   209
     map (fn (_, {name, lhs, id, ...}) => ((name, lhs), id)) (Net.dest procs)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   210
     |> partition_eq eq_snd
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   211
     |> map (fn ps => (#1 (#1 (hd ps)), map (#2 o #1) ps))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   212
     |> Library.sort_wrt #1};
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   213
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   214
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   215
(* merge_mss *)       (*NOTE: ignores mk_rews, termless and depth of 2nd mss*)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   216
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   217
fun merge_mss
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   218
 (Mss {rules = rules1, congs = (congs1,weak1), procs = procs1,
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   219
       bounds = bounds1, prems = prems1, mk_rews, termless, depth},
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   220
  Mss {rules = rules2, congs = (congs2,weak2), procs = procs2,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   221
       bounds = bounds2, prems = prems2, ...}) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   222
      mk_mss
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   223
       (Net.merge (rules1, rules2, eq_rrule),
12285
6490fc7b3eed gen_merge_lists;
wenzelm
parents: 12262
diff changeset
   224
        (gen_merge_lists (eq_cong o pairself snd) congs1 congs2,
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   225
        merge_lists weak1 weak2),
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   226
        Net.merge (procs1, procs2, eq_simproc),
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   227
        merge_lists bounds1 bounds2,
12285
6490fc7b3eed gen_merge_lists;
wenzelm
parents: 12262
diff changeset
   228
        gen_merge_lists eq_prem prems1 prems2,
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   229
        mk_rews, termless, depth);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   230
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   231
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   232
(* add_simps *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   233
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   234
fun mk_rrule2{thm, name, lhs, elhs, perm} =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   235
  let val fo = Pattern.first_order (term_of elhs) orelse not(Pattern.pattern (term_of elhs))
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   236
  in {thm=thm, name=name, lhs=lhs, elhs=elhs, fo=fo, perm=perm} end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   237
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   238
fun insert_rrule quiet (mss as Mss {rules,...},
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   239
                 rrule as {thm,name,lhs,elhs,perm}) =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   240
  (trace_named_thm "Adding rewrite rule" (thm, name);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   241
   let val rrule2 as {elhs,...} = mk_rrule2 rrule
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   242
       val rules' = Net.insert_term ((term_of elhs, rrule2), rules, eq_rrule)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   243
   in upd_rules(mss,rules') end
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   244
   handle Net.INSERT => if quiet then mss else
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   245
     (prthm true "Ignoring duplicate rewrite rule:" thm; mss));
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   246
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   247
fun vperm (Var _, Var _) = true
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   248
  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   249
  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   250
  | vperm (t, u) = (t = u);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   251
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   252
fun var_perm (t, u) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   253
  vperm (t, u) andalso eq_set (term_varnames t, term_varnames u);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   254
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   255
(* FIXME: it seems that the conditions on extra variables are too liberal if
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   256
prems are nonempty: does solving the prems really guarantee instantiation of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   257
all its Vars? Better: a dynamic check each time a rule is applied.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   258
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   259
fun rewrite_rule_extra_vars prems elhs erhs =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   260
  not (term_varnames erhs subset foldl add_term_varnames (term_varnames elhs, prems))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   261
  orelse
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   262
  not ((term_tvars erhs) subset
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   263
       (term_tvars elhs  union  List.concat(map term_tvars prems)));
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   264
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   265
(*Simple test for looping rewrite rules and stupid orientations*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   266
fun reorient sign prems lhs rhs =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   267
   rewrite_rule_extra_vars prems lhs rhs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   268
  orelse
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   269
   is_Var (head_of lhs)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   270
  orelse
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   271
   (exists (apl (lhs, Logic.occs)) (rhs :: prems))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   272
  orelse
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   273
   (null prems andalso
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   274
    Pattern.matches (#tsig (Sign.rep_sg sign)) (lhs, rhs))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   275
    (*the condition "null prems" is necessary because conditional rewrites
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   276
      with extra variables in the conditions may terminate although
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   277
      the rhs is an instance of the lhs. Example: ?m < ?n ==> f(?n) == f(?m)*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   278
  orelse
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   279
   (is_Const lhs andalso not(is_Const rhs))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   280
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   281
fun decomp_simp thm =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   282
  let val {sign, prop, ...} = rep_thm thm;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   283
      val prems = Logic.strip_imp_prems prop;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   284
      val concl = Drule.strip_imp_concl (cprop_of thm);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   285
      val (lhs, rhs) = Drule.dest_equals concl handle TERM _ =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   286
        raise SIMPLIFIER ("Rewrite rule not a meta-equality", thm)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   287
      val elhs = snd (Drule.dest_equals (cprop_of (Thm.eta_conversion lhs)));
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   288
      val elhs = if elhs=lhs then lhs else elhs (* try to share *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   289
      val erhs = Pattern.eta_contract (term_of rhs);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   290
      val perm = var_perm (term_of elhs, erhs) andalso not (term_of elhs aconv erhs)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   291
                 andalso not (is_Var (term_of elhs))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   292
  in (sign, prems, term_of lhs, elhs, term_of rhs, perm) end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   293
12783
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   294
fun decomp_simp' thm =
12979
4c76bce4ce39 decomp_simp': use lhs instead of elhs (preserves more bound variable names);
wenzelm
parents: 12783
diff changeset
   295
  let val (_, _, lhs, _, rhs, _) = decomp_simp thm in
12783
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   296
    if Thm.nprems_of thm > 0 then raise SIMPLIFIER ("Bad conditional rewrite rule", thm)
12979
4c76bce4ce39 decomp_simp': use lhs instead of elhs (preserves more bound variable names);
wenzelm
parents: 12783
diff changeset
   297
    else (lhs, rhs)
12783
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   298
  end;
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   299
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   300
fun mk_eq_True (Mss{mk_rews={mk_eq_True,...},...}) (thm, name) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   301
  case mk_eq_True thm of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   302
    None => []
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   303
  | Some eq_True =>
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   304
      let val (_,_,lhs,elhs,_,_) = decomp_simp eq_True
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   305
      in [{thm=eq_True, name=name, lhs=lhs, elhs=elhs, perm=false}] end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   306
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   307
(* create the rewrite rule and possibly also the ==True variant,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   308
   in case there are extra vars on the rhs *)
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   309
fun rrule_eq_True(thm,name,lhs,elhs,rhs,mss,thm2) =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   310
  let val rrule = {thm=thm, name=name, lhs=lhs, elhs=elhs, perm=false}
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   311
  in if (term_varnames rhs)  subset (term_varnames lhs) andalso
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   312
        (term_tvars rhs) subset (term_tvars lhs)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   313
     then [rrule]
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   314
     else mk_eq_True mss (thm2, name) @ [rrule]
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   315
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   316
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   317
fun mk_rrule mss (thm, name) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   318
  let val (_,prems,lhs,elhs,rhs,perm) = decomp_simp thm
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   319
  in if perm then [{thm=thm, name=name, lhs=lhs, elhs=elhs, perm=true}] else
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   320
     (* weak test for loops: *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   321
     if rewrite_rule_extra_vars prems lhs rhs orelse
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   322
        is_Var (term_of elhs)
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   323
     then mk_eq_True mss (thm, name)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   324
     else rrule_eq_True(thm,name,lhs,elhs,rhs,mss,thm)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   325
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   326
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   327
fun orient_rrule mss (thm, name) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   328
  let val (sign,prems,lhs,elhs,rhs,perm) = decomp_simp thm
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   329
  in if perm then [{thm=thm, name=name, lhs=lhs, elhs=elhs, perm=true}]
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   330
     else if reorient sign prems lhs rhs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   331
          then if reorient sign prems rhs lhs
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   332
               then mk_eq_True mss (thm, name)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   333
               else let val Mss{mk_rews={mk_sym,...},...} = mss
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   334
                    in case mk_sym thm of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   335
                         None => []
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   336
                       | Some thm' =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   337
                           let val (_,_,lhs',elhs',rhs',_) = decomp_simp thm'
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   338
                           in rrule_eq_True(thm',name,lhs',elhs',rhs',mss,thm) end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   339
                    end
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   340
          else rrule_eq_True(thm,name,lhs,elhs,rhs,mss,thm)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   341
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   342
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   343
fun extract_rews(Mss{mk_rews = {mk,...},...},thms) =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   344
  flat (map (fn thm => map (rpair (Thm.name_of_thm thm)) (mk thm)) thms);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   345
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   346
fun orient_comb_simps comb mk_rrule (mss,thms) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   347
  let val rews = extract_rews(mss,thms)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   348
      val rrules = flat (map mk_rrule rews)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   349
  in foldl comb (mss,rrules) end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   350
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   351
(* Add rewrite rules explicitly; do not reorient! *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   352
fun add_simps(mss,thms) =
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   353
  orient_comb_simps (insert_rrule false) (mk_rrule mss) (mss,thms);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   354
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   355
fun mss_of thms = foldl (insert_rrule false) (empty_mss, flat
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   356
  (map (fn thm => mk_rrule empty_mss (thm, Thm.name_of_thm thm)) thms));
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   357
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   358
fun extract_safe_rrules(mss,thm) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   359
  flat (map (orient_rrule mss) (extract_rews(mss,[thm])));
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   360
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   361
(* del_simps *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   362
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   363
fun del_rrule(mss as Mss {rules,...},
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   364
              rrule as {thm, elhs, ...}) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   365
  (upd_rules(mss, Net.delete_term ((term_of elhs, rrule), rules, eq_rrule))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   366
   handle Net.DELETE =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   367
     (prthm true "Rewrite rule not in simpset:" thm; mss));
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   368
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   369
fun del_simps(mss,thms) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   370
  orient_comb_simps del_rrule (map mk_rrule2 o mk_rrule mss) (mss,thms);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   371
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   372
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   373
(* add_congs *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   374
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   375
fun is_full_cong_prems [] varpairs = null varpairs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   376
  | is_full_cong_prems (p::prems) varpairs =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   377
    (case Logic.strip_assums_concl p of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   378
       Const("==",_) $ lhs $ rhs =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   379
         let val (x,xs) = strip_comb lhs and (y,ys) = strip_comb rhs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   380
         in is_Var x  andalso  forall is_Bound xs  andalso
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   381
            null(findrep(xs))  andalso xs=ys andalso
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   382
            (x,y) mem varpairs andalso
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   383
            is_full_cong_prems prems (varpairs\(x,y))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   384
         end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   385
     | _ => false);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   386
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   387
fun is_full_cong thm =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   388
let val prems = prems_of thm
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   389
    and concl = concl_of thm
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   390
    val (lhs,rhs) = Logic.dest_equals concl
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   391
    val (f,xs) = strip_comb lhs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   392
    and (g,ys) = strip_comb rhs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   393
in
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   394
  f=g andalso null(findrep(xs@ys)) andalso length xs = length ys andalso
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   395
  is_full_cong_prems prems (xs ~~ ys)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   396
end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   397
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   398
fun add_cong (Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, thm) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   399
  let
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   400
    val (lhs, _) = Drule.dest_equals (Drule.strip_imp_concl (cprop_of thm)) handle TERM _ =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   401
      raise SIMPLIFIER ("Congruence not a meta-equality", thm);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   402
(*   val lhs = Pattern.eta_contract lhs; *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   403
    val (a, _) = dest_Const (head_of (term_of lhs)) handle TERM _ =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   404
      raise SIMPLIFIER ("Congruence must start with a constant", thm);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   405
    val (alist,weak) = congs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   406
    val alist2 = overwrite_warn (alist, (a,{lhs=lhs, thm=thm}))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   407
           ("Overwriting congruence rule for " ^ quote a);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   408
    val weak2 = if is_full_cong thm then weak else a::weak
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   409
  in
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   410
    mk_mss (rules,(alist2,weak2),procs,bounds,prems,mk_rews,termless,depth)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   411
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   412
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   413
val (op add_congs) = foldl add_cong;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   414
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   415
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   416
(* del_congs *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   417
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   418
fun del_cong (Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, thm) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   419
  let
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   420
    val (lhs, _) = Logic.dest_equals (concl_of thm) handle TERM _ =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   421
      raise SIMPLIFIER ("Congruence not a meta-equality", thm);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   422
(*   val lhs = Pattern.eta_contract lhs; *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   423
    val (a, _) = dest_Const (head_of lhs) handle TERM _ =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   424
      raise SIMPLIFIER ("Congruence must start with a constant", thm);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   425
    val (alist,_) = congs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   426
    val alist2 = filter (fn (x,_)=> x<>a) alist
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   427
    val weak2 = mapfilter (fn(a,{thm,...}) => if is_full_cong thm then None
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   428
                                              else Some a)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   429
                   alist2
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   430
  in
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   431
    mk_mss (rules,(alist2,weak2),procs,bounds,prems,mk_rews,termless,depth)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   432
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   433
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   434
val (op del_congs) = foldl del_cong;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   435
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   436
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   437
(* add_simprocs *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   438
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   439
fun add_proc (mss as Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth},
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   440
    (name, lhs, proc, id)) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   441
  let val {sign, t, ...} = rep_cterm lhs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   442
  in (trace_term false ("Adding simplification procedure " ^ quote name ^ " for")
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   443
      sign t;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   444
    mk_mss (rules, congs,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   445
      Net.insert_term ((t, mk_simproc (name, proc, lhs, id)), procs, eq_simproc)
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   446
        handle Net.INSERT =>
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   447
            (warning ("Ignoring duplicate simplification procedure \""
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   448
                      ^ name ^ "\"");
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   449
             procs),
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   450
        bounds, prems, mk_rews, termless,depth))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   451
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   452
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   453
fun add_simproc (mss, (name, lhss, proc, id)) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   454
  foldl add_proc (mss, map (fn lhs => (name, lhs, proc, id)) lhss);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   455
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   456
val add_simprocs = foldl add_simproc;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   457
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   458
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   459
(* del_simprocs *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   460
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   461
fun del_proc (mss as Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth},
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   462
    (name, lhs, proc, id)) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   463
  mk_mss (rules, congs,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   464
    Net.delete_term ((term_of lhs, mk_simproc (name, proc, lhs, id)), procs, eq_simproc)
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   465
      handle Net.DELETE =>
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   466
          (warning ("Simplification procedure \"" ^ name ^
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   467
                       "\" not in simpset"); procs),
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   468
      bounds, prems, mk_rews, termless, depth);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   469
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   470
fun del_simproc (mss, (name, lhss, proc, id)) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   471
  foldl del_proc (mss, map (fn lhs => (name, lhs, proc, id)) lhss);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   472
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   473
val del_simprocs = foldl del_simproc;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   474
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   475
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   476
(* prems *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   477
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   478
fun add_prems (Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, thms) =
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   479
  mk_mss (rules, congs, procs, bounds, thms @ prems, mk_rews, termless, depth);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   480
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   481
fun prems_of_mss (Mss {prems, ...}) = prems;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   482
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   483
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   484
(* mk_rews *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   485
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   486
fun set_mk_rews
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   487
  (Mss {rules, congs, procs, bounds, prems, mk_rews, termless, depth}, mk) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   488
    mk_mss (rules, congs, procs, bounds, prems,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   489
            {mk=mk, mk_sym= #mk_sym mk_rews, mk_eq_True= #mk_eq_True mk_rews},
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   490
            termless, depth);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   491
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   492
fun set_mk_sym
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   493
  (Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, mk_sym) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   494
    mk_mss (rules, congs, procs, bounds, prems,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   495
            {mk= #mk mk_rews, mk_sym= mk_sym, mk_eq_True= #mk_eq_True mk_rews},
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   496
            termless,depth);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   497
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   498
fun set_mk_eq_True
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   499
  (Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, mk_eq_True) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   500
    mk_mss (rules, congs, procs, bounds, prems,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   501
            {mk= #mk mk_rews, mk_sym= #mk_sym mk_rews, mk_eq_True= mk_eq_True},
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   502
            termless,depth);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   503
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   504
(* termless *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   505
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   506
fun set_termless
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   507
  (Mss {rules, congs, procs, bounds, prems, mk_rews, depth, ...}, termless) =
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   508
    mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless, depth);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   509
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   510
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   511
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   512
(** rewriting **)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   513
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   514
(*
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   515
  Uses conversions, see:
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   516
    L C Paulson, A higher-order implementation of rewriting,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   517
    Science of Computer Programming 3 (1983), pages 119-149.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   518
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   519
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   520
val dest_eq = Drule.dest_equals o cprop_of;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   521
val lhs_of = fst o dest_eq;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   522
val rhs_of = snd o dest_eq;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   523
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   524
fun beta_eta_conversion t =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   525
  let val thm = beta_conversion true t;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   526
  in transitive thm (eta_conversion (rhs_of thm)) end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   527
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   528
fun check_conv msg thm thm' =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   529
  let
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   530
    val thm'' = transitive thm (transitive
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   531
      (symmetric (beta_eta_conversion (lhs_of thm'))) thm')
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
   532
  in (if msg then trace_thm "SUCCEEDED" thm' else (); Some thm'') end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   533
  handle THM _ =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   534
    let val {sign, prop = _ $ _ $ prop0, ...} = rep_thm thm;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   535
    in
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
   536
      (trace_thm "Proved wrong thm (Check subgoaler?)" thm';
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   537
       trace_term false "Should have proved:" sign prop0;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   538
       None)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   539
    end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   540
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   541
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   542
(* mk_procrule *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   543
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   544
fun mk_procrule thm =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   545
  let val (_,prems,lhs,elhs,rhs,_) = decomp_simp thm
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   546
  in if rewrite_rule_extra_vars prems lhs rhs
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   547
     then (prthm true "Extra vars on rhs:" thm; [])
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   548
     else [mk_rrule2{thm=thm, name="", lhs=lhs, elhs=elhs, perm=false}]
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   549
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   550
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   551
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   552
(* conversion to apply the meta simpset to a term *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   553
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   554
(* Since the rewriting strategy is bottom-up, we avoid re-normalizing already
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   555
   normalized terms by carrying around the rhs of the rewrite rule just
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   556
   applied. This is called the `skeleton'. It is decomposed in parallel
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   557
   with the term. Once a Var is encountered, the corresponding term is
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   558
   already in normal form.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   559
   skel0 is a dummy skeleton that is to enforce complete normalization.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   560
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   561
val skel0 = Bound 0;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   562
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   563
(* Use rhs as skeleton only if the lhs does not contain unnormalized bits.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   564
   The latter may happen iff there are weak congruence rules for constants
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   565
   in the lhs.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   566
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   567
fun uncond_skel((_,weak),(lhs,rhs)) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   568
  if null weak then rhs (* optimization *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   569
  else if exists_Const (fn (c,_) => c mem weak) lhs then skel0
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   570
       else rhs;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   571
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   572
(* Behaves like unconditional rule if rhs does not contain vars not in the lhs.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   573
   Otherwise those vars may become instantiated with unnormalized terms
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   574
   while the premises are solved.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   575
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   576
fun cond_skel(args as (congs,(lhs,rhs))) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   577
  if term_varnames rhs subset term_varnames lhs then uncond_skel(args)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   578
  else skel0;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   579
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   580
(*
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   581
  we try in order:
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   582
    (1) beta reduction
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   583
    (2) unconditional rewrite rules
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   584
    (3) conditional rewrite rules
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   585
    (4) simplification procedures
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   586
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   587
  IMPORTANT: rewrite rules must not introduce new Vars or TVars!
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   588
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   589
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   590
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   591
fun rewritec (prover, signt, maxt)
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   592
             (mss as Mss{rules, procs, termless, prems, congs, depth,...}) t =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   593
  let
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   594
    val eta_thm = Thm.eta_conversion t;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   595
    val eta_t' = rhs_of eta_thm;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   596
    val eta_t = term_of eta_t';
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   597
    val tsigt = Sign.tsig_of signt;
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   598
    fun rew {thm, name, lhs, elhs, fo, perm} =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   599
      let
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   600
        val {sign, prop, maxidx, ...} = rep_thm thm;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   601
        val _ = if Sign.subsig (sign, signt) then ()
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   602
                else (prthm true "Ignoring rewrite rule from different theory:" thm;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   603
                      raise Pattern.MATCH);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   604
        val (rthm, elhs') = if maxt = ~1 then (thm, elhs)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   605
          else (Thm.incr_indexes (maxt+1) thm, Thm.cterm_incr_indexes (maxt+1) elhs);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   606
        val insts = if fo then Thm.cterm_first_order_match (elhs', eta_t')
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   607
                          else Thm.cterm_match (elhs', eta_t');
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   608
        val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   609
        val prop' = #prop (rep_thm thm');
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   610
        val unconditional = (Logic.count_prems (prop',0) = 0);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   611
        val (lhs', rhs') = Logic.dest_equals (Logic.strip_imp_concl prop')
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   612
      in
11295
66925f23ac7f improved tracing of permutative rules.
nipkow
parents: 11291
diff changeset
   613
        if perm andalso not (termless (rhs', lhs'))
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   614
        then (trace_named_thm "Cannot apply permutative rewrite rule" (thm, name);
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
   615
              trace_thm "Term does not become smaller:" thm'; None)
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   616
        else (trace_named_thm "Applying instance of rewrite rule" (thm, name);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   617
           if unconditional
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   618
           then
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
   619
             (trace_thm "Rewriting:" thm';
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   620
              let val lr = Logic.dest_equals prop;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   621
                  val Some thm'' = check_conv false eta_thm thm'
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   622
              in Some (thm'', uncond_skel (congs, lr)) end)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   623
           else
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
   624
             (trace_thm "Trying to rewrite:" thm';
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   625
              case prover (incr_depth mss) thm' of
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
   626
                None       => (trace_thm "FAILED" thm'; None)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   627
              | Some thm2 =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   628
                  (case check_conv true eta_thm thm2 of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   629
                     None => None |
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   630
                     Some thm2' =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   631
                       let val concl = Logic.strip_imp_concl prop
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   632
                           val lr = Logic.dest_equals concl
11505
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
   633
                       in Some (thm2', cond_skel (congs, lr)) end)))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   634
      end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   635
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   636
    fun rews [] = None
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   637
      | rews (rrule :: rrules) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   638
          let val opt = rew rrule handle Pattern.MATCH => None
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   639
          in case opt of None => rews rrules | some => some end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   640
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   641
    fun sort_rrules rrs = let
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   642
      fun is_simple({thm, ...}:rrule) = case #prop (rep_thm thm) of
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   643
                                      Const("==",_) $ _ $ _ => true
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   644
                                      | _                   => false
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   645
      fun sort []        (re1,re2) = re1 @ re2
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   646
        | sort (rr::rrs) (re1,re2) = if is_simple rr
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   647
                                     then sort rrs (rr::re1,re2)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   648
                                     else sort rrs (re1,rr::re2)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   649
    in sort rrs ([],[]) end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   650
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   651
    fun proc_rews ([]:simproc list) = None
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   652
      | proc_rews ({name, proc, lhs, ...} :: ps) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   653
          if Pattern.matches tsigt (term_of lhs, term_of t) then
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   654
            (debug_term false ("Trying procedure " ^ quote name ^ " on:") signt eta_t;
13486
54464ea94d6f exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents: 13458
diff changeset
   655
             case transform_failure (curry SIMPROC_FAIL name)
54464ea94d6f exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents: 13458
diff changeset
   656
                 (fn () => proc signt prems eta_t) () of
54464ea94d6f exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents: 13458
diff changeset
   657
               None => (debug false "FAILED"; proc_rews ps)
54464ea94d6f exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents: 13458
diff changeset
   658
             | Some raw_thm =>
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
   659
                 (trace_thm ("Procedure " ^ quote name ^ " produced rewrite rule:") raw_thm;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   660
                  (case rews (mk_procrule raw_thm) of
13486
54464ea94d6f exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents: 13458
diff changeset
   661
                    None => (trace_cterm true ("IGNORED result of simproc " ^ quote name ^
54464ea94d6f exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents: 13458
diff changeset
   662
                      " -- does not match") t; proc_rews ps)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   663
                  | some => some)))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   664
          else proc_rews ps;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   665
  in case eta_t of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   666
       Abs _ $ _ => Some (transitive eta_thm
12155
13c5469b4bb3 congc now returns None if congruence rule has no effect.
berghofe
parents: 11886
diff changeset
   667
         (beta_conversion false eta_t'), skel0)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   668
     | _ => (case rews (sort_rrules (Net.match_term rules eta_t)) of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   669
               None => proc_rews (Net.match_term procs eta_t)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   670
             | some => some)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   671
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   672
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   673
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   674
(* conversion to apply a congruence rule to a term *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   675
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   676
fun congc (prover,signt,maxt) {thm=cong,lhs=lhs} t =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   677
  let val {sign, ...} = rep_thm cong
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   678
      val _ = if Sign.subsig (sign, signt) then ()
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   679
                 else error("Congruence rule from different theory")
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   680
      val rthm = if maxt = ~1 then cong else Thm.incr_indexes (maxt+1) cong;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   681
      val rlhs = fst (Drule.dest_equals (Drule.strip_imp_concl (cprop_of rthm)));
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   682
      val insts = Thm.cterm_match (rlhs, t)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   683
      (* Pattern.match can raise Pattern.MATCH;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   684
         is handled when congc is called *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   685
      val thm' = Thm.instantiate insts (Thm.rename_boundvars (term_of rlhs) (term_of t) rthm);
13569
69a6b3aa0f38 drule: added nRS
nipkow
parents: 13486
diff changeset
   686
      val unit = trace_thm "Applying congruence rule:" thm';
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   687
      fun err (msg, thm) = (prthm false msg thm; error "Failed congruence proof!")
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   688
  in case prover thm' of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   689
       None => err ("Could not prove", thm')
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   690
     | Some thm2 => (case check_conv true (beta_eta_conversion t) thm2 of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   691
          None => err ("Should not have proved", thm2)
12155
13c5469b4bb3 congc now returns None if congruence rule has no effect.
berghofe
parents: 11886
diff changeset
   692
        | Some thm2' =>
13c5469b4bb3 congc now returns None if congruence rule has no effect.
berghofe
parents: 11886
diff changeset
   693
            if op aconv (pairself term_of (dest_equals (cprop_of thm2')))
13c5469b4bb3 congc now returns None if congruence rule has no effect.
berghofe
parents: 11886
diff changeset
   694
            then None else Some thm2')
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   695
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   696
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   697
val (cA, (cB, cC)) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   698
  apsnd dest_equals (dest_implies (hd (cprems_of Drule.imp_cong)));
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   699
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   700
fun transitive1 None None = None
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   701
  | transitive1 (Some thm1) None = Some thm1
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   702
  | transitive1 None (Some thm2) = Some thm2
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   703
  | transitive1 (Some thm1) (Some thm2) = Some (transitive thm1 thm2)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   704
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   705
fun transitive2 thm = transitive1 (Some thm);
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   706
fun transitive3 thm = transitive1 thm o Some;
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   707
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   708
fun imp_cong' e = combination (combination refl_implies e);
12155
13c5469b4bb3 congc now returns None if congruence rule has no effect.
berghofe
parents: 11886
diff changeset
   709
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   710
fun bottomc ((simprem,useprem,mutsimp), prover, sign, maxidx) =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   711
  let
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   712
    fun botc skel mss t =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   713
          if is_Var skel then None
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   714
          else
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   715
          (case subc skel mss t of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   716
             some as Some thm1 =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   717
               (case rewritec (prover, sign, maxidx) mss (rhs_of thm1) of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   718
                  Some (thm2, skel2) =>
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   719
                    transitive2 (transitive thm1 thm2)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   720
                      (botc skel2 mss (rhs_of thm2))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   721
                | None => some)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   722
           | None =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   723
               (case rewritec (prover, sign, maxidx) mss t of
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   724
                  Some (thm2, skel2) => transitive2 thm2
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   725
                    (botc skel2 mss (rhs_of thm2))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   726
                | None => None))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   727
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   728
    and try_botc mss t =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   729
          (case botc skel0 mss t of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   730
             Some trec1 => trec1 | None => (reflexive t))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   731
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   732
    and subc skel
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   733
          (mss as Mss{rules,congs,procs,bounds,prems,mk_rews,termless,depth}) t0 =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   734
       (case term_of t0 of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   735
           Abs (a, T, t) =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   736
             let val b = variant bounds a
10767
8fa4aafa7314 Thm: dest_comb, dest_abs, capply, cabs no longer global;
wenzelm
parents: 10413
diff changeset
   737
                 val (v, t') = Thm.dest_abs (Some ("." ^ b)) t0
11504
935f9e8de0d0 Traced depth of conditional rewriting
nipkow
parents: 11371
diff changeset
   738
                 val mss' = mk_mss (rules, congs, procs, b :: bounds, prems, mk_rews, termless,depth)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   739
                 val skel' = case skel of Abs (_, _, sk) => sk | _ => skel0
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   740
             in case botc skel' mss' t' of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   741
                  Some thm => Some (abstract_rule a v thm)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   742
                | None => None
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   743
             end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   744
         | t $ _ => (case t of
13614
0b91269c0b13 Deleted superfluous dest_implies.
berghofe
parents: 13607
diff changeset
   745
             Const ("==>", _) $ _  => impc t0 mss
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   746
           | Abs _ =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   747
               let val thm = beta_conversion false t0
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   748
               in case subc skel0 mss (rhs_of thm) of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   749
                    None => Some thm
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   750
                  | Some thm' => Some (transitive thm thm')
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   751
               end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   752
           | _  =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   753
               let fun appc () =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   754
                     let
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   755
                       val (tskel, uskel) = case skel of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   756
                           tskel $ uskel => (tskel, uskel)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   757
                         | _ => (skel0, skel0);
10767
8fa4aafa7314 Thm: dest_comb, dest_abs, capply, cabs no longer global;
wenzelm
parents: 10413
diff changeset
   758
                       val (ct, cu) = Thm.dest_comb t0
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   759
                     in
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   760
                     (case botc tskel mss ct of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   761
                        Some thm1 =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   762
                          (case botc uskel mss cu of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   763
                             Some thm2 => Some (combination thm1 thm2)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   764
                           | None => Some (combination thm1 (reflexive cu)))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   765
                      | None =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   766
                          (case botc uskel mss cu of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   767
                             Some thm1 => Some (combination (reflexive ct) thm1)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   768
                           | None => None))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   769
                     end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   770
                   val (h, ts) = strip_comb t
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   771
               in case h of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   772
                    Const(a, _) =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   773
                      (case assoc_string (fst congs, a) of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   774
                         None => appc ()
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   775
                       | Some cong =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   776
(* post processing: some partial applications h t1 ... tj, j <= length ts,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   777
   may be a redex. Example: map (%x.x) = (%xs.xs) wrt map_cong *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   778
                          (let
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   779
                             val thm = congc (prover mss, sign, maxidx) cong t0;
12155
13c5469b4bb3 congc now returns None if congruence rule has no effect.
berghofe
parents: 11886
diff changeset
   780
                             val t = if_none (apsome rhs_of thm) t0;
10767
8fa4aafa7314 Thm: dest_comb, dest_abs, capply, cabs no longer global;
wenzelm
parents: 10413
diff changeset
   781
                             val (cl, cr) = Thm.dest_comb t
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   782
                             val dVar = Var(("", 0), dummyT)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   783
                             val skel =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   784
                               list_comb (h, replicate (length ts) dVar)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   785
                           in case botc skel mss cl of
12155
13c5469b4bb3 congc now returns None if congruence rule has no effect.
berghofe
parents: 11886
diff changeset
   786
                                None => thm
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   787
                              | Some thm' => transitive3 thm
12155
13c5469b4bb3 congc now returns None if congruence rule has no effect.
berghofe
parents: 11886
diff changeset
   788
                                  (combination thm' (reflexive cr))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   789
                           end handle TERM _ => error "congc result"
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   790
                                    | Pattern.MATCH => appc ()))
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   791
                  | _ => appc ()
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   792
               end)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   793
         | _ => None)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   794
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   795
    and impc ct mss =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   796
      if mutsimp then mut_impc0 [] ct [] [] mss else nonmut_impc ct mss
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   797
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   798
    and rules_of_prem mss prem =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   799
      if maxidx_of_term (term_of prem) <> ~1
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   800
      then (trace_cterm true
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   801
        "Cannot add premise as rewrite rule because it contains (type) unknowns:" prem; ([], None))
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   802
      else
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   803
        let val asm = assume prem
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   804
        in (extract_safe_rrules (mss, asm), Some asm) end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   805
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   806
    and add_rrules (rrss, asms) mss =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   807
      add_prems (foldl (insert_rrule true) (mss, flat rrss), mapfilter I asms)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   808
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   809
    and disch r (prem, eq) =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   810
      let
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   811
        val (lhs, rhs) = dest_eq eq;
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   812
        val eq' = implies_elim (Thm.instantiate
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   813
          ([], [(cA, prem), (cB, lhs), (cC, rhs)]) Drule.imp_cong)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   814
          (implies_intr prem eq)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   815
      in if not r then eq' else
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   816
        let
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   817
          val (prem', concl) = dest_implies lhs;
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   818
          val (prem'', _) = dest_implies rhs
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   819
        in transitive (transitive
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   820
          (Thm.instantiate ([], [(cA, prem'), (cB, prem), (cC, concl)])
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   821
             Drule.swap_prems_eq) eq')
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   822
          (Thm.instantiate ([], [(cA, prem), (cB, prem''), (cC, concl)])
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   823
             Drule.swap_prems_eq)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   824
        end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   825
      end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   826
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   827
    and rebuild [] _ _ _ _ eq = eq
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   828
      | rebuild (prem :: prems) concl (rrs :: rrss) (asm :: asms) mss eq =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   829
          let
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   830
            val mss' = add_rrules (rev rrss, rev asms) mss;
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   831
            val concl' =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   832
              Drule.mk_implies (prem, if_none (apsome rhs_of eq) concl);
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   833
            val dprem = apsome (curry (disch false) prem)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   834
          in case rewritec (prover, sign, maxidx) mss' concl' of
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   835
              None => rebuild prems concl' rrss asms mss (dprem eq)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   836
            | Some (eq', _) => transitive2 (foldl (disch false o swap)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   837
                  (the (transitive3 (dprem eq) eq'), prems))
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   838
                (mut_impc0 (rev prems) (rhs_of eq') (rev rrss) (rev asms) mss)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   839
          end
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   840
          
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   841
    and mut_impc0 prems concl rrss asms mss =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   842
      let
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   843
        val prems' = strip_imp_prems concl;
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   844
        val (rrss', asms') = split_list (map (rules_of_prem mss) prems')
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   845
      in mut_impc (prems @ prems') (strip_imp_concl concl) (rrss @ rrss')
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   846
        (asms @ asms') [] [] [] [] mss ~1 ~1
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   847
      end
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   848
 
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   849
    and mut_impc [] concl [] [] prems' rrss' asms' eqns mss changed k =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   850
        transitive1 (foldl (fn (eq2, (eq1, prem)) => transitive1 eq1
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   851
            (apsome (curry (disch false) prem) eq2)) (None, eqns ~~ prems'))
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   852
          (if changed > 0 then
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   853
             mut_impc (rev prems') concl (rev rrss') (rev asms')
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   854
               [] [] [] [] mss ~1 changed
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   855
           else rebuild prems' concl rrss' asms' mss
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   856
             (botc skel0 (add_rrules (rev rrss', rev asms') mss) concl))
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   857
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   858
      | mut_impc (prem :: prems) concl (rrs :: rrss) (asm :: asms)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   859
          prems' rrss' asms' eqns mss changed k =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   860
        case (if k = 0 then None else botc skel0 (add_rrules
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   861
          (rev rrss' @ rrss, rev asms' @ asms) mss) prem) of
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   862
            None => mut_impc prems concl rrss asms (prem :: prems')
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   863
              (rrs :: rrss') (asm :: asms') (None :: eqns) mss changed
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   864
              (if k = 0 then 0 else k - 1)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   865
          | Some eqn =>
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   866
            let
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   867
              val prem' = rhs_of eqn;
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   868
              val tprems = map term_of prems;
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   869
              val i = 1 + foldl Int.max (~1, map (fn p =>
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   870
                find_index_eq p tprems) (#hyps (rep_thm eqn)));
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   871
              val (rrs', asm') = rules_of_prem mss prem'
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   872
            in mut_impc prems concl rrss asms (prem' :: prems')
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   873
              (rrs' :: rrss') (asm' :: asms') (Some (foldr (disch true)
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   874
                (take (i, prems), imp_cong' eqn (reflexive (Drule.list_implies
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   875
                  (drop (i, prems), concl))))) :: eqns) mss (length prems') ~1
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   876
            end
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   877
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   878
     (* legacy code - only for backwards compatibility *)
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   879
     and nonmut_impc ct mss =
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   880
       let val (prem, conc) = dest_implies ct;
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   881
           val thm1 = if simprem then botc skel0 mss prem else None;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   882
           val prem1 = if_none (apsome rhs_of thm1) prem;
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   883
           val mss1 = if not useprem then mss else add_rrules
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   884
             (apsnd single (apfst single (rules_of_prem mss prem1))) mss
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   885
       in (case botc skel0 mss1 conc of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   886
           None => (case thm1 of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   887
               None => None
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   888
             | Some thm1' => Some (imp_cong' thm1' (reflexive conc)))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   889
         | Some thm2 =>
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   890
           let val thm2' = disch false (prem1, thm2)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   891
           in (case thm1 of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   892
               None => Some thm2'
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   893
             | Some thm1' =>
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   894
                 Some (transitive (imp_cong' thm1' (reflexive conc)) thm2'))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   895
           end)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   896
       end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   897
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   898
 in try_botc end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   899
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   900
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   901
(*** Meta-rewriting: rewrites t to u and returns the theorem t==u ***)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   902
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   903
(*
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   904
  Parameters:
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   905
    mode = (simplify A,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   906
            use A in simplifying B,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   907
            use prems of B (if B is again a meta-impl.) to simplify A)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   908
           when simplifying A ==> B
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   909
    mss: contains equality theorems of the form [|p1,...|] ==> t==u
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   910
    prover: how to solve premises in conditional rewrites and congruences
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   911
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   912
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   913
fun rewrite_cterm mode prover mss ct =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   914
  let val {sign, t, maxidx, ...} = rep_cterm ct
11505
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
   915
      val Mss{depth, ...} = mss
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
   916
  in simp_depth := depth;
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
   917
     bottomc (mode, prover, sign, maxidx) mss ct
a410fa8acfca Implemented indentation schema for conditional rewrite trace.
nipkow
parents: 11504
diff changeset
   918
  end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   919
  handle THM (s, _, thms) =>
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   920
    error ("Exception THM was raised in simplifier:\n" ^ s ^ "\n" ^
11886
36d0585f87de Display.pretty_thms;
wenzelm
parents: 11767
diff changeset
   921
      Pretty.string_of (Display.pretty_thms thms));
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   922
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   923
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   924
(*Do not rewrite flex-flex pairs*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   925
fun goals_conv pred cv =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   926
  let fun gconv i ct =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   927
        let val (A,B) = Drule.dest_implies ct
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   928
            val (thA,j) = case term_of A of
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   929
                  Const("=?=",_)$_$_ => (reflexive A, i)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   930
                | _ => (if pred i then cv A else reflexive A, i+1)
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
   931
        in  imp_cong' thA (gconv j B) end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   932
        handle TERM _ => reflexive ct
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   933
  in gconv 1 end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   934
11737
0ec18d3131b5 Tuned comment.
berghofe
parents: 11736
diff changeset
   935
(* Rewrite A in !!x1,...,xn. A *)
11736
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   936
fun forall_conv cv ct =
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   937
  let val p as (ct1, ct2) = Thm.dest_comb ct
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   938
  in (case pairself term_of p of
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   939
      (Const ("all", _), Abs (s, _, _)) =>
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   940
         let val (v, ct') = Thm.dest_abs (Some "@") ct2;
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   941
         in Thm.combination (Thm.reflexive ct1)
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   942
           (Thm.abstract_rule s v (forall_conv cv ct'))
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   943
         end
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   944
    | _ => cv ct)
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   945
  end handle TERM _ => cv ct;
da6fc37ed6fa - Exported goals_conv and fconv_rule
berghofe
parents: 11689
diff changeset
   946
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   947
(*Use a conversion to transform a theorem*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   948
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   949
11760
wenzelm
parents: 11737
diff changeset
   950
(*Rewrite a cterm*)
11767
7380c9d45626 tuned rewrite/simplify interface;
wenzelm
parents: 11760
diff changeset
   951
fun rewrite_aux _ _ [] = (fn ct => Thm.reflexive ct)
7380c9d45626 tuned rewrite/simplify interface;
wenzelm
parents: 11760
diff changeset
   952
  | rewrite_aux prover full thms = rewrite_cterm (full, false, false) prover (mss_of thms);
11672
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
   953
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   954
(*Rewrite a theorem*)
11767
7380c9d45626 tuned rewrite/simplify interface;
wenzelm
parents: 11760
diff changeset
   955
fun simplify_aux _ _ [] = (fn th => th)
7380c9d45626 tuned rewrite/simplify interface;
wenzelm
parents: 11760
diff changeset
   956
  | simplify_aux prover full thms =
7380c9d45626 tuned rewrite/simplify interface;
wenzelm
parents: 11760
diff changeset
   957
      fconv_rule (rewrite_cterm (full, false, false) prover (mss_of thms));
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   958
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   959
fun rewrite_thm mode prover mss = fconv_rule (rewrite_cterm mode prover mss);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   960
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   961
(*Rewrite the subgoals of a proof state (represented by a theorem) *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   962
fun rewrite_goals_rule_aux _ []   th = th
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   963
  | rewrite_goals_rule_aux prover thms th =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   964
      fconv_rule (goals_conv (K true) (rewrite_cterm (true, true, false) prover
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   965
        (mss_of thms))) th;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   966
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   967
(*Rewrite the subgoal of a proof state (represented by a theorem) *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   968
fun rewrite_goal_rule mode prover mss i thm =
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   969
  if 0 < i  andalso  i <= nprems_of thm
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   970
  then fconv_rule (goals_conv (fn j => j=i) (rewrite_cterm mode prover mss)) thm
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   971
  else raise THM("rewrite_goal_rule",i,[thm]);
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   972
12783
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   973
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   974
(*simple term rewriting -- without proofs*)
13196
08c42252346f Changed interface of rewrite_term.
berghofe
parents: 12979
diff changeset
   975
fun rewrite_term sg rules procs =
08c42252346f Changed interface of rewrite_term.
berghofe
parents: 12979
diff changeset
   976
  Pattern.rewrite_term (Sign.tsig_of sg) (map decomp_simp' rules) procs;
12783
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   977
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   978
end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   979
11672
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
   980
structure BasicMetaSimplifier: BASIC_META_SIMPLIFIER = MetaSimplifier;
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
   981
open BasicMetaSimplifier;