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