src/Pure/raw_simplifier.ML
author haftmann
Wed, 22 Jan 2020 19:17:59 +0000
changeset 71403 43c2355648d2
parent 71318 1be996d8bb98
child 74227 fdcc7e8f95ea
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 41227
diff changeset
     1
(*  Title:      Pure/raw_simplifier.ML
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents: 28839
diff changeset
     2
    Author:     Tobias Nipkow and Stefan Berghofer, TU Muenchen
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
     3
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 41227
diff changeset
     4
Higher-order Simplification.
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
     5
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
     6
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
     7
infix 4
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45405
diff changeset
     8
  addsimps delsimps addsimprocs delsimprocs
52037
837211662fb8 tuned signature -- depend on context by default;
wenzelm
parents: 51717
diff changeset
     9
  setloop addloop delloop
45625
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45621
diff changeset
    10
  setSSolver addSSolver setSolver addSolver;
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
    11
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 41227
diff changeset
    12
signature BASIC_RAW_SIMPLIFIER =
11672
8e75b78f33f3 full_rewrite_cterm_aux (see also tactic.ML);
wenzelm
parents: 11505
diff changeset
    13
sig
41227
11e7ee2ca77f tuned signature;
wenzelm
parents: 41226
diff changeset
    14
  val simp_depth_limit: int Config.T
11e7ee2ca77f tuned signature;
wenzelm
parents: 41226
diff changeset
    15
  val simp_trace_depth_limit: int Config.T
40878
7695e4de4d86 renamed trace_simp to simp_trace, and debug_simp to simp_debug;
wenzelm
parents: 39163
diff changeset
    16
  val simp_debug: bool Config.T
7695e4de4d86 renamed trace_simp to simp_trace, and debug_simp to simp_debug;
wenzelm
parents: 39163
diff changeset
    17
  val simp_trace: bool Config.T
51590
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
    18
  type cong_name = bool * string
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
    19
  type rrule
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
    20
  val mk_rrules: Proof.context -> thm list -> rrule list
16807
730cace0ae48 export eq_rrule;
wenzelm
parents: 16665
diff changeset
    21
  val eq_rrule: rrule * rrule -> bool
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
    22
  type proc
17614
37ee526db497 added mk_solver';
wenzelm
parents: 17496
diff changeset
    23
  type solver
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    24
  val mk_solver: string -> (Proof.context -> int -> tactic) -> solver
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    25
  type simpset
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
    26
  val empty_ss: simpset
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
    27
  val merge_ss: simpset * simpset -> simpset
30356
36d0e00af606 added dest_ss;
wenzelm
parents: 30336
diff changeset
    28
  val dest_ss: simpset ->
36d0e00af606 added dest_ss;
wenzelm
parents: 30336
diff changeset
    29
   {simps: (string * thm) list,
61098
e1b4b24f2ebd eliminated pointless cterms;
wenzelm
parents: 61095
diff changeset
    30
    procs: (string * term list) list,
51590
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
    31
    congs: (cong_name * thm) list,
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
    32
    weak_congs: cong_name list,
30356
36d0e00af606 added dest_ss;
wenzelm
parents: 30336
diff changeset
    33
    loopers: string list,
36d0e00af606 added dest_ss;
wenzelm
parents: 30336
diff changeset
    34
    unsafe_solvers: string list,
36d0e00af606 added dest_ss;
wenzelm
parents: 30336
diff changeset
    35
    safe_solvers: string list}
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
    36
  type simproc
22234
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
    37
  val eq_simproc: simproc * simproc -> bool
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 61098
diff changeset
    38
  val cert_simproc: theory -> string ->
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
    39
    {lhss: term list, proc: morphism -> Proof.context -> cterm -> thm option} -> simproc
45290
f599ac41e7f5 tuned signature -- refined terminology;
wenzelm
parents: 44058
diff changeset
    40
  val transform_simproc: morphism -> simproc -> simproc
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    41
  val simpset_of: Proof.context -> simpset
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    42
  val put_simpset: simpset -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    43
  val simpset_map: Proof.context -> (Proof.context -> Proof.context) -> simpset -> simpset
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    44
  val map_theory_simpset: (Proof.context -> Proof.context) -> theory -> theory
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    45
  val empty_simpset: Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    46
  val clear_simpset: Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    47
  val addsimps: Proof.context * thm list -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    48
  val delsimps: Proof.context * thm list -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    49
  val addsimprocs: Proof.context * simproc list -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    50
  val delsimprocs: Proof.context * simproc list -> Proof.context
52037
837211662fb8 tuned signature -- depend on context by default;
wenzelm
parents: 51717
diff changeset
    51
  val setloop: Proof.context * (Proof.context -> int -> tactic) -> Proof.context
837211662fb8 tuned signature -- depend on context by default;
wenzelm
parents: 51717
diff changeset
    52
  val addloop: Proof.context * (string * (Proof.context -> int -> tactic)) -> Proof.context
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    53
  val delloop: Proof.context * string -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    54
  val setSSolver: Proof.context * solver -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    55
  val addSSolver: Proof.context * solver -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    56
  val setSolver: Proof.context * solver -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    57
  val addSolver: Proof.context * solver -> Proof.context
21708
45e7491bea47 reorganized structure Tactic vs. MetaSimplifier;
wenzelm
parents: 21646
diff changeset
    58
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54731
diff changeset
    59
  val rewrite_rule: Proof.context -> thm list -> thm -> thm
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54731
diff changeset
    60
  val rewrite_goals_rule: Proof.context -> thm list -> thm -> thm
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54731
diff changeset
    61
  val rewrite_goals_tac: Proof.context -> thm list -> tactic
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54731
diff changeset
    62
  val rewrite_goal_tac: Proof.context -> thm list -> int -> tactic
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54731
diff changeset
    63
  val prune_params_tac: Proof.context -> tactic
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54731
diff changeset
    64
  val fold_rule: Proof.context -> thm list -> thm -> thm
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54731
diff changeset
    65
  val fold_goals_tac: Proof.context -> thm list -> tactic
54883
dd04a8b654fc proper context for norm_hhf and derived operations;
wenzelm
parents: 54742
diff changeset
    66
  val norm_hhf: Proof.context -> thm -> thm
dd04a8b654fc proper context for norm_hhf and derived operations;
wenzelm
parents: 54742
diff changeset
    67
  val norm_hhf_protect: Proof.context -> thm -> thm
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
    68
end;
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
    69
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 41227
diff changeset
    70
signature RAW_SIMPLIFIER =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
    71
sig
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 41227
diff changeset
    72
  include BASIC_RAW_SIMPLIFIER
54997
811c35e81ac5 tuned signature;
wenzelm
parents: 54984
diff changeset
    73
  exception SIMPLIFIER of string * thm list
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
    74
  type trace_ops
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
    75
  val set_trace_ops: trace_ops -> theory -> theory
71235
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
    76
  val subgoal_tac: Proof.context -> int -> tactic
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
    77
  val loop_tac: Proof.context -> int -> tactic
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
    78
  val solvers: Proof.context -> solver list * solver list
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    79
  val map_ss: (Proof.context -> Proof.context) -> Context.generic -> Context.generic
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    80
  val prems_of: Proof.context -> thm list
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    81
  val add_simp: thm -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    82
  val del_simp: thm -> Proof.context -> Proof.context
68403
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
    83
  val flip_simp: thm -> Proof.context -> Proof.context
63221
7d43fbbaba28 avoid warnings on duplicate rules in the given list;
wenzelm
parents: 62913
diff changeset
    84
  val init_simpset: thm list -> Proof.context -> Proof.context
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    85
  val add_eqcong: thm -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    86
  val del_eqcong: thm -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    87
  val add_cong: thm -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    88
  val del_cong: thm -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    89
  val mksimps: Proof.context -> thm -> thm list
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    90
  val set_mksimps: (Proof.context -> thm -> thm list) -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    91
  val set_mkcong: (Proof.context -> thm -> thm) -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    92
  val set_mksym: (Proof.context -> thm -> thm option) -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    93
  val set_mkeqTrue: (Proof.context -> thm -> thm option) -> Proof.context -> Proof.context
70586
57df8a85317a clarified signature;
wenzelm
parents: 70528
diff changeset
    94
  val set_term_ord: term ord -> Proof.context -> Proof.context
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    95
  val set_subgoaler: (Proof.context -> int -> tactic) -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    96
  val solver: Proof.context -> solver -> int -> tactic
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    97
  val default_mk_sym: Proof.context -> thm -> thm option
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    98
  val add_prems: thm list -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
    99
  val set_reorient: (Proof.context -> term list -> term -> term -> bool) ->
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   100
    Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   101
  val set_solvers: solver list -> Proof.context -> Proof.context
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   102
  val rewrite_cterm: bool * bool * bool ->
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   103
    (Proof.context -> thm -> thm option) -> Proof.context -> conv
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16380
diff changeset
   104
  val rewrite_term: theory -> thm list -> (term -> term option) list -> term -> term
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   105
  val rewrite_thm: bool * bool * bool ->
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   106
    (Proof.context -> thm -> thm option) -> Proof.context -> thm -> thm
46465
5ba52c337cd0 tuned signature;
wenzelm
parents: 46462
diff changeset
   107
  val generic_rewrite_goal_tac: bool * bool * bool ->
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   108
    (Proof.context -> tactic) -> Proof.context -> int -> tactic
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54731
diff changeset
   109
  val rewrite: Proof.context -> bool -> thm list -> conv
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   110
end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   111
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 41227
diff changeset
   112
structure Raw_Simplifier: RAW_SIMPLIFIER =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   113
struct
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   114
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   115
(** datatype simpset **)
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   116
51590
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   117
(* congruence rules *)
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   118
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   119
type cong_name = bool * string;
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   120
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   121
fun cong_name (Const (a, _)) = SOME (true, a)
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   122
  | cong_name (Free (a, _)) = SOME (false, a)
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   123
  | cong_name _ = NONE;
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   124
71239
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   125
structure Congtab = Table(type key = cong_name val ord = prod_ord bool_ord fast_string_ord);
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   126
51590
c52891242de2 more formal cong_name;
wenzelm
parents: 49660
diff changeset
   127
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   128
(* rewrite rules *)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   129
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   130
type rrule =
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   131
 {thm: thm,         (*the rewrite rule*)
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   132
  name: string,     (*name of theorem from which rewrite rule was extracted*)
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   133
  lhs: term,        (*the left-hand side*)
58836
4037bb00d08e tuned spelling;
wenzelm
parents: 57859
diff changeset
   134
  elhs: cterm,      (*the eta-contracted lhs*)
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   135
  extra: bool,      (*extra variables outside of elhs*)
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   136
  fo: bool,         (*use first-order matching*)
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   137
  perm: bool};      (*the rewrite rule is permutative*)
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   138
61057
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   139
fun trim_context_rrule ({thm, name, lhs, elhs, extra, fo, perm}: rrule) =
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   140
  {thm = Thm.trim_context thm, name = name, lhs = lhs, elhs = Thm.trim_context_cterm elhs,
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   141
    extra = extra, fo = fo, perm = perm};
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   142
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   143
(*
12603
7d2bca103101 tuned tracing markup;
wenzelm
parents: 12285
diff changeset
   144
Remarks:
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   145
  - elhs is used for matching,
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   146
    lhs only for preservation of bound variable names;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   147
  - fo is set iff
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   148
    either elhs is first-order (no Var is applied),
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   149
      in which case fo-matching is complete,
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   150
    or elhs is not a pattern,
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   151
      in which case there is nothing better to do;
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   152
*)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   153
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   154
fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) =
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 22254
diff changeset
   155
  Thm.eq_thm_prop (thm1, thm2);
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   156
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   157
(* FIXME: it seems that the conditions on extra variables are too liberal if
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   158
prems are nonempty: does solving the prems really guarantee instantiation of
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   159
all its Vars? Better: a dynamic check each time a rule is applied.
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   160
*)
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   161
fun rewrite_rule_extra_vars prems elhs erhs =
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   162
  let
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   163
    val elhss = elhs :: prems;
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   164
    val tvars = fold Term.add_tvars elhss [];
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   165
    val vars = fold Term.add_vars elhss [];
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   166
  in
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   167
    erhs |> Term.exists_type (Term.exists_subtype
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   168
      (fn TVar v => not (member (op =) tvars v) | _ => false)) orelse
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   169
    erhs |> Term.exists_subterm
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   170
      (fn Var v => not (member (op =) vars v) | _ => false)
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   171
  end;
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   172
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   173
fun rrule_extra_vars elhs thm =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   174
  rewrite_rule_extra_vars [] (Thm.term_of elhs) (Thm.full_prop_of thm);
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   175
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   176
fun mk_rrule2 {thm, name, lhs, elhs, perm} =
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   177
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   178
    val t = Thm.term_of elhs;
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   179
    val fo = Pattern.first_order t orelse not (Pattern.pattern t);
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   180
    val extra = rrule_extra_vars elhs thm;
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   181
  in {thm = thm, name = name, lhs = lhs, elhs = elhs, extra = extra, fo = fo, perm = perm} end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   182
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   183
(*simple test for looping rewrite rules and stupid orientations*)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   184
fun default_reorient ctxt prems lhs rhs =
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   185
  rewrite_rule_extra_vars prems lhs rhs
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   186
    orelse
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   187
  is_Var (head_of lhs)
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   188
    orelse
16305
5e7b6731b004 Added the t = x "fix" - in (* ... *) :-(
nipkow
parents: 16042
diff changeset
   189
(* turns t = x around, which causes a headache if x is a local variable -
5e7b6731b004 Added the t = x "fix" - in (* ... *) :-(
nipkow
parents: 16042
diff changeset
   190
   usually it is very useful :-(
5e7b6731b004 Added the t = x "fix" - in (* ... *) :-(
nipkow
parents: 16042
diff changeset
   191
  is_Free rhs andalso not(is_Free lhs) andalso not(Logic.occs(rhs,lhs))
5e7b6731b004 Added the t = x "fix" - in (* ... *) :-(
nipkow
parents: 16042
diff changeset
   192
  andalso not(exists_subterm is_Var lhs)
5e7b6731b004 Added the t = x "fix" - in (* ... *) :-(
nipkow
parents: 16042
diff changeset
   193
    orelse
5e7b6731b004 Added the t = x "fix" - in (* ... *) :-(
nipkow
parents: 16042
diff changeset
   194
*)
16842
wenzelm
parents: 16807
diff changeset
   195
  exists (fn t => Logic.occs (lhs, t)) (rhs :: prems)
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   196
    orelse
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   197
  null prems andalso Pattern.matches (Proof_Context.theory_of ctxt) (lhs, rhs)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   198
    (*the condition "null prems" is necessary because conditional rewrites
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   199
      with extra variables in the conditions may terminate although
67721
5348bea4accd eliminated ASCII syntax from Pure bootstrap;
wenzelm
parents: 67649
diff changeset
   200
      the rhs is an instance of the lhs; example: ?m < ?n \<Longrightarrow> f ?n \<equiv> f ?m *)
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   201
    orelse
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   202
  is_Const lhs andalso not (is_Const rhs);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   203
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   204
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   205
(* simplification procedures *)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   206
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   207
datatype proc =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   208
  Proc of
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   209
   {name: string,
61098
e1b4b24f2ebd eliminated pointless cterms;
wenzelm
parents: 61095
diff changeset
   210
    lhs: term,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   211
    proc: Proof.context -> cterm -> thm option,
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   212
    stamp: stamp};
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   213
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   214
fun eq_proc (Proc {stamp = stamp1, ...}, Proc {stamp = stamp2, ...}) = stamp1 = stamp2;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   215
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   216
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   217
(* solvers *)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   218
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   219
datatype solver =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   220
  Solver of
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   221
   {name: string,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   222
    solver: Proof.context -> int -> tactic,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   223
    id: stamp};
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   224
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   225
fun mk_solver name solver = Solver {name = name, solver = solver, id = stamp ()};
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   226
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   227
fun solver_name (Solver {name, ...}) = name;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   228
fun solver ctxt (Solver {solver = tac, ...}) = tac ctxt;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   229
fun eq_solver (Solver {id = id1, ...}, Solver {id = id2, ...}) = (id1 = id2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   230
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   231
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   232
(* simplification sets *)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   233
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   234
(*A simpset contains data required during conversion:
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   235
    rules: discrimination net of rewrite rules;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   236
    prems: current premises;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   237
    depth: simp_depth and exceeded flag;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   238
    congs: association list of congruence rules and
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   239
           a list of `weak' congruence constants.
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   240
           A congruence is `weak' if it avoids normalization of some argument.
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   241
    procs: discrimination net of simplification procedures
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   242
      (functions that prove rewrite rules on the fly);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   243
    mk_rews:
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   244
      mk: turn simplification thms into rewrite rules;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   245
      mk_cong: prepare congruence rules;
67721
5348bea4accd eliminated ASCII syntax from Pure bootstrap;
wenzelm
parents: 67649
diff changeset
   246
      mk_sym: turn \<equiv> around;
5348bea4accd eliminated ASCII syntax from Pure bootstrap;
wenzelm
parents: 67649
diff changeset
   247
      mk_eq_True: turn P into P \<equiv> True;
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   248
    term_ord: for ordered rewriting;*)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   249
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   250
datatype simpset =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   251
  Simpset of
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   252
   {rules: rrule Net.net,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   253
    prems: thm list,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   254
    depth: int * bool Unsynchronized.ref} *
71239
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   255
   {congs: thm Congtab.table * cong_name list,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   256
    procs: proc Net.net,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   257
    mk_rews:
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   258
     {mk: Proof.context -> thm -> thm list,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   259
      mk_cong: Proof.context -> thm -> thm,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   260
      mk_sym: Proof.context -> thm -> thm option,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   261
      mk_eq_True: Proof.context -> thm -> thm option,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   262
      reorient: Proof.context -> term list -> term -> term -> bool},
70586
57df8a85317a clarified signature;
wenzelm
parents: 70528
diff changeset
   263
    term_ord: term ord,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   264
    subgoal_tac: Proof.context -> int -> tactic,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   265
    loop_tacs: (string * (Proof.context -> int -> tactic)) list,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   266
    solvers: solver list * solver list};
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   267
54728
445e7947c6b5 tuned signature;
wenzelm
parents: 54727
diff changeset
   268
fun internal_ss (Simpset (_, ss2)) = ss2;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   269
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   270
fun make_ss1 (rules, prems, depth) = {rules = rules, prems = prems, depth = depth};
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   271
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   272
fun map_ss1 f {rules, prems, depth} = make_ss1 (f (rules, prems, depth));
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   273
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   274
fun make_ss2 (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) =
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   275
  {congs = congs, procs = procs, mk_rews = mk_rews, term_ord = term_ord,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   276
    subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers};
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   277
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   278
fun map_ss2 f {congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers} =
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   279
  make_ss2 (f (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers));
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   280
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   281
fun make_simpset (args1, args2) = Simpset (make_ss1 args1, make_ss2 args2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   282
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   283
fun dest_ss (Simpset ({rules, ...}, {congs, procs, loop_tacs, solvers, ...})) =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   284
 {simps = Net.entries rules
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   285
    |> map (fn {name, thm, ...} => (name, thm)),
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   286
  procs = Net.entries procs
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   287
    |> map (fn Proc {name, lhs, stamp, ...} => ((name, lhs), stamp))
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   288
    |> partition_eq (eq_snd op =)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   289
    |> map (fn ps => (fst (fst (hd ps)), map (snd o fst) ps)),
71239
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   290
  congs = congs |> fst |> Congtab.dest,
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   291
  weak_congs = congs |> snd,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   292
  loopers = map fst loop_tacs,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   293
  unsafe_solvers = map solver_name (#1 solvers),
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   294
  safe_solvers = map solver_name (#2 solvers)};
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   295
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   296
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   297
(* empty *)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   298
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   299
fun init_ss depth mk_rews term_ord subgoal_tac solvers =
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   300
  make_simpset ((Net.empty, [], depth),
71239
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   301
    ((Congtab.empty, []), Net.empty, mk_rews, term_ord, subgoal_tac, [], solvers));
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   302
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   303
fun default_mk_sym _ th = SOME (th RS Drule.symmetric_thm);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   304
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   305
val empty_ss =
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   306
  init_ss (0, Unsynchronized.ref false)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   307
    {mk = fn _ => fn th => if can Logic.dest_equals (Thm.concl_of th) then [th] else [],
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   308
      mk_cong = K I,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   309
      mk_sym = default_mk_sym,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   310
      mk_eq_True = K (K NONE),
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   311
      reorient = default_reorient}
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   312
    Term_Ord.term_ord (K (K no_tac)) ([], []);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   313
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   314
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   315
(* merge *)  (*NOTE: ignores some fields of 2nd simpset*)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   316
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   317
fun merge_ss (ss1, ss2) =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   318
  if pointer_eq (ss1, ss2) then ss1
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   319
  else
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   320
    let
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   321
      val Simpset ({rules = rules1, prems = prems1, depth = depth1},
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   322
       {congs = (congs1, weak1), procs = procs1, mk_rews, term_ord, subgoal_tac,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   323
        loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1)}) = ss1;
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   324
      val Simpset ({rules = rules2, prems = prems2, depth = depth2},
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   325
       {congs = (congs2, weak2), procs = procs2, mk_rews = _, term_ord = _, subgoal_tac = _,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   326
        loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2)}) = ss2;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   327
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   328
      val rules' = Net.merge eq_rrule (rules1, rules2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   329
      val prems' = Thm.merge_thms (prems1, prems2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   330
      val depth' = if #1 depth1 < #1 depth2 then depth2 else depth1;
71239
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   331
      val congs' = Congtab.merge (K true) (congs1, congs2);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   332
      val weak' = merge (op =) (weak1, weak2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   333
      val procs' = Net.merge eq_proc (procs1, procs2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   334
      val loop_tacs' = AList.merge (op =) (K true) (loop_tacs1, loop_tacs2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   335
      val unsafe_solvers' = merge eq_solver (unsafe_solvers1, unsafe_solvers2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   336
      val solvers' = merge eq_solver (solvers1, solvers2);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   337
    in
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   338
      make_simpset ((rules', prems', depth'), ((congs', weak'), procs',
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   339
        mk_rews, term_ord, subgoal_tac, loop_tacs', (unsafe_solvers', solvers')))
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   340
    end;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   341
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   342
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   343
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   344
(** context data **)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   345
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   346
structure Simpset = Generic_Data
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   347
(
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   348
  type T = simpset;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   349
  val empty = empty_ss;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   350
  val extend = I;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   351
  val merge = merge_ss;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   352
);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   353
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   354
val simpset_of = Simpset.get o Context.Proof;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   355
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   356
fun map_simpset f = Context.proof_map (Simpset.map f);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   357
fun map_simpset1 f = map_simpset (fn Simpset (ss1, ss2) => Simpset (map_ss1 f ss1, ss2));
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   358
fun map_simpset2 f = map_simpset (fn Simpset (ss1, ss2) => Simpset (ss1, map_ss2 f ss2));
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   359
71403
haftmann
parents: 71318
diff changeset
   360
fun put_simpset ss = map_simpset (K ss);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   361
71403
haftmann
parents: 71318
diff changeset
   362
fun simpset_map ctxt f ss = ctxt |> put_simpset ss |> f |> simpset_of;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   363
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   364
val empty_simpset = put_simpset empty_ss;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   365
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   366
fun map_theory_simpset f thy =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   367
  let
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   368
    val ctxt' = f (Proof_Context.init_global thy);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   369
    val thy' = Proof_Context.theory_of ctxt';
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   370
  in Context.theory_map (Simpset.map (K (simpset_of ctxt'))) thy' end;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   371
57859
29e728588163 more careful treatment of context visibility for rule declarations (see also 39d9c7f175e0, e639d91d9073) -- avoid duplicate warnings;
wenzelm
parents: 56438
diff changeset
   372
fun map_ss f = Context.mapping (map_theory_simpset (f o Context_Position.not_really)) f;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   373
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   374
val clear_simpset =
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   375
  map_simpset (fn Simpset ({depth, ...}, {mk_rews, term_ord, subgoal_tac, solvers, ...}) =>
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   376
    init_ss depth mk_rews term_ord subgoal_tac solvers);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   377
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   378
71235
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   379
(* accessors for tactis *)
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   380
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   381
fun subgoal_tac ctxt = (#subgoal_tac o internal_ss o simpset_of) ctxt ctxt;
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   382
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   383
fun loop_tac ctxt =
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   384
  FIRST' (map (fn (_, tac) => tac ctxt) (rev ((#loop_tacs o internal_ss o simpset_of) ctxt)));
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   385
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   386
val solvers = #solvers o internal_ss o simpset_of
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   387
d12c58e12c51 more direct accessors for simpset
haftmann
parents: 71234
diff changeset
   388
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   389
(* simp depth *)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   390
66934
b86513bcf7ac reduced simp_depth_limit
nipkow
parents: 64556
diff changeset
   391
(*
b86513bcf7ac reduced simp_depth_limit
nipkow
parents: 64556
diff changeset
   392
The simp_depth_limit is meant to abort infinite recursion of the simplifier
b86513bcf7ac reduced simp_depth_limit
nipkow
parents: 64556
diff changeset
   393
early but should not terminate "normal" executions.
b86513bcf7ac reduced simp_depth_limit
nipkow
parents: 64556
diff changeset
   394
As of 2017, 25 would suffice; 40 builds in a safety margin.
b86513bcf7ac reduced simp_depth_limit
nipkow
parents: 64556
diff changeset
   395
*)
b86513bcf7ac reduced simp_depth_limit
nipkow
parents: 64556
diff changeset
   396
69575
f77cc54f6d47 clarified signature: more types;
wenzelm
parents: 69137
diff changeset
   397
val simp_depth_limit = Config.declare_int ("simp_depth_limit", \<^here>) (K 40);
f77cc54f6d47 clarified signature: more types;
wenzelm
parents: 69137
diff changeset
   398
val simp_trace_depth_limit = Config.declare_int ("simp_trace_depth_limit", \<^here>) (K 1);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   399
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   400
fun inc_simp_depth ctxt =
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   401
  ctxt |> map_simpset1 (fn (rules, prems, (depth, exceeded)) =>
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   402
    (rules, prems,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   403
      (depth + 1,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   404
        if depth = Config.get ctxt simp_trace_depth_limit
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   405
        then Unsynchronized.ref false else exceeded)));
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   406
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   407
fun simp_depth ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   408
  let val Simpset ({depth = (depth, _), ...}, _) = simpset_of ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   409
  in depth end;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   410
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   411
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   412
(* diagnostics *)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   413
54997
811c35e81ac5 tuned signature;
wenzelm
parents: 54984
diff changeset
   414
exception SIMPLIFIER of string * thm list;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   415
69575
f77cc54f6d47 clarified signature: more types;
wenzelm
parents: 69137
diff changeset
   416
val simp_debug = Config.declare_bool ("simp_debug", \<^here>) (K false);
f77cc54f6d47 clarified signature: more types;
wenzelm
parents: 69137
diff changeset
   417
val simp_trace = Config.declare_bool ("simp_trace", \<^here>) (K false);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   418
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   419
fun cond_warning ctxt msg =
57859
29e728588163 more careful treatment of context visibility for rule declarations (see also 39d9c7f175e0, e639d91d9073) -- avoid duplicate warnings;
wenzelm
parents: 56438
diff changeset
   420
  if Context_Position.is_really_visible ctxt then warning (msg ()) else ();
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   421
55031
wenzelm
parents: 55028
diff changeset
   422
fun cond_tracing' ctxt flag msg =
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   423
  if Config.get ctxt flag then
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   424
    let
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   425
      val Simpset ({depth = (depth, exceeded), ...}, _) = simpset_of ctxt;
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   426
      val depth_limit = Config.get ctxt simp_trace_depth_limit;
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   427
    in
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   428
      if depth > depth_limit then
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   429
        if ! exceeded then () else (tracing "simp_trace_depth_limit exceeded!"; exceeded := true)
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   430
      else (tracing (enclose "[" "]" (string_of_int depth) ^ msg ()); exceeded := false)
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   431
    end
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   432
  else ();
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   433
55031
wenzelm
parents: 55028
diff changeset
   434
fun cond_tracing ctxt = cond_tracing' ctxt simp_trace;
wenzelm
parents: 55028
diff changeset
   435
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   436
fun print_term ctxt s t =
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   437
  s ^ "\n" ^ Syntax.string_of_term ctxt t;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   438
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   439
fun print_thm ctxt s (name, th) =
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   440
  print_term ctxt (if name = "" then s else s ^ " " ^ quote name ^ ":") (Thm.full_prop_of th);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   441
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   442
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   443
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   444
(** simpset operations **)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   445
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   446
(* prems *)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   447
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   448
fun prems_of ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   449
  let val Simpset ({prems, ...}, _) = simpset_of ctxt in prems end;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   450
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   451
fun add_prems ths =
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   452
  map_simpset1 (fn (rules, prems, depth) => (rules, ths @ prems, depth));
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   453
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   454
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   455
(* maintain simp rules *)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   456
68403
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   457
fun del_rrule loud (rrule as {thm, elhs, ...}) ctxt =
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   458
  ctxt |> map_simpset1 (fn (rules, prems, depth) =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   459
    (Net.delete_term eq_rrule (Thm.term_of elhs, rrule) rules, prems, depth))
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   460
  handle Net.DELETE =>
68403
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   461
    (if not loud then ()
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   462
     else cond_warning ctxt
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   463
            (fn () => print_thm ctxt "Rewrite rule not in simpset:" ("", thm));
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   464
     ctxt);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   465
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   466
fun insert_rrule (rrule as {thm, name, ...}) ctxt =
55031
wenzelm
parents: 55028
diff changeset
   467
 (cond_tracing ctxt (fn () => print_thm ctxt "Adding rewrite rule" (name, thm));
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   468
  ctxt |> map_simpset1 (fn (rules, prems, depth) =>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   469
    let
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   470
      val rrule2 as {elhs, ...} = mk_rrule2 rrule;
61057
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   471
      val rules' = Net.insert_term eq_rrule (Thm.term_of elhs, trim_context_rrule rrule2) rules;
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
   472
    in (rules', prems, depth) end)
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   473
  handle Net.INSERT =>
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   474
    (cond_warning ctxt (fn () => print_thm ctxt "Ignoring duplicate rewrite rule:" ("", thm));
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   475
      ctxt));
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   476
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   477
local
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   478
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   479
fun vperm (Var _, Var _) = true
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   480
  | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   481
  | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   482
  | vperm (t, u) = (t = u);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   483
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   484
fun var_perm (t, u) =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   485
  vperm (t, u) andalso eq_set (op =) (Term.add_vars t [], Term.add_vars u []);
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   486
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   487
in
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   488
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   489
fun decomp_simp thm =
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   490
  let
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26424
diff changeset
   491
    val prop = Thm.prop_of thm;
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   492
    val prems = Logic.strip_imp_prems prop;
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   493
    val concl = Drule.strip_imp_concl (Thm.cprop_of thm);
22902
ac833b4bb7ee moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents: 22892
diff changeset
   494
    val (lhs, rhs) = Thm.dest_equals concl handle TERM _ =>
54997
811c35e81ac5 tuned signature;
wenzelm
parents: 54984
diff changeset
   495
      raise SIMPLIFIER ("Rewrite rule not a meta-equality", [thm]);
20579
4dc799edef89 Thm.dest_arg;
wenzelm
parents: 20546
diff changeset
   496
    val elhs = Thm.dest_arg (Thm.cprop_of (Thm.eta_conversion lhs));
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   497
    val erhs = Envir.eta_contract (Thm.term_of rhs);
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   498
    val perm =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   499
      var_perm (Thm.term_of elhs, erhs) andalso
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   500
      not (Thm.term_of elhs aconv erhs) andalso
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   501
      not (is_Var (Thm.term_of elhs));
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   502
  in (prems, Thm.term_of lhs, elhs, Thm.term_of rhs, perm) end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   503
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   504
end;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   505
12783
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   506
fun decomp_simp' thm =
52091
9ec2d47de6a7 more rigorous check of simplifier context;
wenzelm
parents: 52082
diff changeset
   507
  let val (_, lhs, _, rhs, _) = decomp_simp thm in
54997
811c35e81ac5 tuned signature;
wenzelm
parents: 54984
diff changeset
   508
    if Thm.nprems_of thm > 0 then raise SIMPLIFIER ("Bad conditional rewrite rule", [thm])
12979
4c76bce4ce39 decomp_simp': use lhs instead of elhs (preserves more bound variable names);
wenzelm
parents: 12783
diff changeset
   509
    else (lhs, rhs)
12783
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   510
  end;
36ca5ea8092c interface to Pattern.rewrite_term;
wenzelm
parents: 12779
diff changeset
   511
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   512
fun mk_eq_True ctxt (thm, name) =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   513
  let val Simpset (_, {mk_rews = {mk_eq_True, ...}, ...}) = simpset_of ctxt in
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   514
    (case mk_eq_True ctxt thm of
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   515
      NONE => []
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   516
    | SOME eq_True =>
52091
9ec2d47de6a7 more rigorous check of simplifier context;
wenzelm
parents: 52082
diff changeset
   517
        let val (_, lhs, elhs, _, _) = decomp_simp eq_True;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   518
        in [{thm = eq_True, name = name, lhs = lhs, elhs = elhs, perm = false}] end)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   519
  end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   520
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   521
(*create the rewrite rule and possibly also the eq_True variant,
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   522
  in case there are extra vars on the rhs*)
52082
wenzelm
parents: 52037
diff changeset
   523
fun rrule_eq_True ctxt thm name lhs elhs rhs thm2 =
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   524
  let val rrule = {thm = thm, name = name, lhs = lhs, elhs = elhs, perm = false} in
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   525
    if rewrite_rule_extra_vars [] lhs rhs then
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   526
      mk_eq_True ctxt (thm2, name) @ [rrule]
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   527
    else [rrule]
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   528
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   529
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   530
fun mk_rrule ctxt (thm, name) =
52091
9ec2d47de6a7 more rigorous check of simplifier context;
wenzelm
parents: 52082
diff changeset
   531
  let val (prems, lhs, elhs, rhs, perm) = decomp_simp thm in
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   532
    if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   533
    else
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   534
      (*weak test for loops*)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   535
      if rewrite_rule_extra_vars prems lhs rhs orelse is_Var (Thm.term_of elhs)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   536
      then mk_eq_True ctxt (thm, name)
52082
wenzelm
parents: 52037
diff changeset
   537
      else rrule_eq_True ctxt thm name lhs elhs rhs thm
70472
cf66d2db97fe more robust and convenient treatment of implicit context;
wenzelm
parents: 69575
diff changeset
   538
  end |> map (fn {thm, name, lhs, elhs, perm} =>
cf66d2db97fe more robust and convenient treatment of implicit context;
wenzelm
parents: 69575
diff changeset
   539
    {thm = Thm.trim_context thm, name = name, lhs = lhs,
cf66d2db97fe more robust and convenient treatment of implicit context;
wenzelm
parents: 69575
diff changeset
   540
      elhs = Thm.trim_context_cterm elhs, perm = perm});
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   541
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   542
fun orient_rrule ctxt (thm, name) =
18208
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   543
  let
52091
9ec2d47de6a7 more rigorous check of simplifier context;
wenzelm
parents: 52082
diff changeset
   544
    val (prems, lhs, elhs, rhs, perm) = decomp_simp thm;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   545
    val Simpset (_, {mk_rews = {reorient, mk_sym, ...}, ...}) = simpset_of ctxt;
18208
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   546
  in
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   547
    if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   548
    else if reorient ctxt prems lhs rhs then
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   549
      if reorient ctxt prems rhs lhs
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   550
      then mk_eq_True ctxt (thm, name)
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   551
      else
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   552
        (case mk_sym ctxt thm of
18208
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   553
          NONE => []
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   554
        | SOME thm' =>
52091
9ec2d47de6a7 more rigorous check of simplifier context;
wenzelm
parents: 52082
diff changeset
   555
            let val (_, lhs', elhs', rhs', _) = decomp_simp thm'
52082
wenzelm
parents: 52037
diff changeset
   556
            in rrule_eq_True ctxt thm' name lhs' elhs' rhs' thm end)
wenzelm
parents: 52037
diff changeset
   557
    else rrule_eq_True ctxt thm name lhs elhs rhs thm
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   558
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   559
68046
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   560
fun extract_rews ctxt sym thms =
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   561
  let
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   562
    val Simpset (_, {mk_rews = {mk, ...}, ...}) = simpset_of ctxt;
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   563
    val mk =
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   564
      if sym then fn ctxt => fn th => (mk ctxt th) RL [Drule.symmetric_thm]
69137
90fce429e1bc Jenkins: run ocaml_setup
Lars Hupel <lars.hupel@mytum.de>
parents: 68405
diff changeset
   565
      else mk
68046
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   566
  in maps (fn thm => map (rpair (Thm.get_name_hint thm)) (mk ctxt thm)) thms
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   567
  end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   568
54982
wenzelm
parents: 54895
diff changeset
   569
fun extract_safe_rrules ctxt thm =
68046
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   570
  maps (orient_rrule ctxt) (extract_rews ctxt false [thm]);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   571
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
   572
fun mk_rrules ctxt thms =
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
   573
  let
68046
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   574
    val rews = extract_rews ctxt false thms
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
   575
    val raw_rrules = flat (map (mk_rrule ctxt) rews)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
   576
  in map mk_rrule2 raw_rrules end
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
   577
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   578
20028
b9752164ad92 add/del_simps: warning for inactive simpset (no context);
wenzelm
parents: 19798
diff changeset
   579
(* add/del rules explicitly *)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   580
61090
16f7f9aa4263 more thorough transfer;
wenzelm
parents: 61057
diff changeset
   581
local
16f7f9aa4263 more thorough transfer;
wenzelm
parents: 61057
diff changeset
   582
68046
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   583
fun comb_simps ctxt comb mk_rrule sym thms =
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   584
  let val rews = extract_rews ctxt sym (map (Thm.transfer' ctxt) thms);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   585
  in fold (fold comb o mk_rrule) rews ctxt end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   586
68405
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   587
(*
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   588
This code checks if the symetric version of a rule is already in the simpset.
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   589
However, the variable names in the two versions of the rule may differ.
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   590
Thus the current test modulo eq_rrule is too weak to be useful
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   591
and needs to be refined.
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   592
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   593
fun present ctxt rules (rrule as {thm, elhs, ...}) =
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   594
  (Net.insert_term eq_rrule (Thm.term_of elhs, trim_context_rrule rrule) rules;
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   595
   false)
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   596
  handle Net.INSERT =>
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   597
    (cond_warning ctxt
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   598
       (fn () => print_thm ctxt "Symmetric rewrite rule already in simpset:" ("", thm));
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   599
     true);
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   600
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   601
fun sym_present ctxt thms =
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   602
  let
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   603
    val rews = extract_rews ctxt true (map (Thm.transfer' ctxt) thms);
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   604
    val rrules = map mk_rrule2 (flat(map (mk_rrule ctxt) rews))
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   605
    val Simpset({rules, ...},_) = simpset_of ctxt
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   606
  in exists (present ctxt rules) rrules end
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   607
*)
61090
16f7f9aa4263 more thorough transfer;
wenzelm
parents: 61057
diff changeset
   608
in
16f7f9aa4263 more thorough transfer;
wenzelm
parents: 61057
diff changeset
   609
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   610
fun ctxt addsimps thms =
68046
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   611
  comb_simps ctxt insert_rrule (mk_rrule ctxt) false thms;
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   612
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   613
fun addsymsimps ctxt thms =
6aba668aea78 new simp modifier: reorient
nipkow
parents: 67721
diff changeset
   614
  comb_simps ctxt insert_rrule (mk_rrule ctxt) true thms;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   615
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   616
fun ctxt delsimps thms =
68403
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   617
  comb_simps ctxt (del_rrule true) (map mk_rrule2 o mk_rrule ctxt) false thms;
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   618
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   619
fun delsimps_quiet ctxt thms =
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   620
  comb_simps ctxt (del_rrule false) (map mk_rrule2 o mk_rrule ctxt) false thms;
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   621
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   622
fun add_simp thm ctxt = ctxt addsimps [thm];
68405
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   623
(*
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   624
with check for presence of symmetric version:
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   625
  if sym_present ctxt [thm]
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   626
  then (cond_warning ctxt (fn () => print_thm ctxt "Ignoring rewrite rule:" ("", thm)); ctxt)
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   627
  else ctxt addsimps [thm];
6a0852b8e5a8 comments
nipkow
parents: 68403
diff changeset
   628
*)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   629
fun del_simp thm ctxt = ctxt delsimps [thm];
68403
223172b97d0b reorient -> split; documented split
nipkow
parents: 68046
diff changeset
   630
fun flip_simp thm ctxt = addsymsimps (delsimps_quiet ctxt [thm]) [thm];
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   631
61090
16f7f9aa4263 more thorough transfer;
wenzelm
parents: 61057
diff changeset
   632
end;
16f7f9aa4263 more thorough transfer;
wenzelm
parents: 61057
diff changeset
   633
63221
7d43fbbaba28 avoid warnings on duplicate rules in the given list;
wenzelm
parents: 62913
diff changeset
   634
fun init_simpset thms ctxt = ctxt
7d43fbbaba28 avoid warnings on duplicate rules in the given list;
wenzelm
parents: 62913
diff changeset
   635
  |> Context_Position.set_visible false
7d43fbbaba28 avoid warnings on duplicate rules in the given list;
wenzelm
parents: 62913
diff changeset
   636
  |> empty_simpset
7d43fbbaba28 avoid warnings on duplicate rules in the given list;
wenzelm
parents: 62913
diff changeset
   637
  |> fold add_simp thms
7d43fbbaba28 avoid warnings on duplicate rules in the given list;
wenzelm
parents: 62913
diff changeset
   638
  |> Context_Position.restore_visible ctxt;
7d43fbbaba28 avoid warnings on duplicate rules in the given list;
wenzelm
parents: 62913
diff changeset
   639
57859
29e728588163 more careful treatment of context visibility for rule declarations (see also 39d9c7f175e0, e639d91d9073) -- avoid duplicate warnings;
wenzelm
parents: 56438
diff changeset
   640
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   641
(* congs *)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   642
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   643
local
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   644
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   645
fun is_full_cong_prems [] [] = true
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   646
  | is_full_cong_prems [] _ = false
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   647
  | is_full_cong_prems (p :: prems) varpairs =
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   648
      (case Logic.strip_assums_concl p of
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55635
diff changeset
   649
        Const ("Pure.eq", _) $ lhs $ rhs =>
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   650
          let val (x, xs) = strip_comb lhs and (y, ys) = strip_comb rhs in
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   651
            is_Var x andalso forall is_Bound xs andalso
20972
db0425645cc7 abandoned findrep
haftmann
parents: 20905
diff changeset
   652
            not (has_duplicates (op =) xs) andalso xs = ys andalso
20671
2aa8269a868e member (op =);
wenzelm
parents: 20579
diff changeset
   653
            member (op =) varpairs (x, y) andalso
19303
7da7e96bd74d gen_eq_set, remove (op =);
wenzelm
parents: 19142
diff changeset
   654
            is_full_cong_prems prems (remove (op =) (x, y) varpairs)
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   655
          end
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   656
      | _ => false);
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   657
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   658
fun is_full_cong thm =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   659
  let
43597
b4a093e755db tuned signature;
wenzelm
parents: 43596
diff changeset
   660
    val prems = Thm.prems_of thm and concl = Thm.concl_of thm;
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   661
    val (lhs, rhs) = Logic.dest_equals concl;
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   662
    val (f, xs) = strip_comb lhs and (g, ys) = strip_comb rhs;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   663
  in
20972
db0425645cc7 abandoned findrep
haftmann
parents: 20905
diff changeset
   664
    f = g andalso not (has_duplicates (op =) (xs @ ys)) andalso length xs = length ys andalso
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   665
    is_full_cong_prems prems (xs ~~ ys)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   666
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   667
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   668
fun mk_cong ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   669
  let val Simpset (_, {mk_rews = {mk_cong = f, ...}, ...}) = simpset_of ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   670
  in f ctxt end;
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45405
diff changeset
   671
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45405
diff changeset
   672
in
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45405
diff changeset
   673
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   674
fun add_eqcong thm ctxt = ctxt |> map_simpset2
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   675
  (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) =>
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   676
    let
45621
wenzelm
parents: 45620
diff changeset
   677
      val (lhs, _) = Logic.dest_equals (Thm.concl_of thm)
54997
811c35e81ac5 tuned signature;
wenzelm
parents: 54984
diff changeset
   678
        handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", [thm]);
18929
d81435108688 Envir.(beta_)eta_contract;
wenzelm
parents: 18573
diff changeset
   679
    (*val lhs = Envir.eta_contract lhs;*)
45621
wenzelm
parents: 45620
diff changeset
   680
      val a = the (cong_name (head_of lhs)) handle Option.Option =>
54997
811c35e81ac5 tuned signature;
wenzelm
parents: 54984
diff changeset
   681
        raise SIMPLIFIER ("Congruence must start with a constant or free variable", [thm]);
22221
8a8aa6114a89 changed cong alist - now using AList operations instead of overwrite_warn
haftmann
parents: 22008
diff changeset
   682
      val (xs, weak) = congs;
71239
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   683
      val xs' = Congtab.update (a, Thm.trim_context thm) xs;
22221
8a8aa6114a89 changed cong alist - now using AList operations instead of overwrite_warn
haftmann
parents: 22008
diff changeset
   684
      val weak' = if is_full_cong thm then weak else a :: weak;
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   685
    in ((xs', weak'), procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) end);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   686
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   687
fun del_eqcong thm ctxt = ctxt |> map_simpset2
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   688
  (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) =>
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   689
    let
45621
wenzelm
parents: 45620
diff changeset
   690
      val (lhs, _) = Logic.dest_equals (Thm.concl_of thm)
54997
811c35e81ac5 tuned signature;
wenzelm
parents: 54984
diff changeset
   691
        handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", [thm]);
18929
d81435108688 Envir.(beta_)eta_contract;
wenzelm
parents: 18573
diff changeset
   692
    (*val lhs = Envir.eta_contract lhs;*)
20057
058e913bac71 tuned exception handling;
wenzelm
parents: 20028
diff changeset
   693
      val a = the (cong_name (head_of lhs)) handle Option.Option =>
54997
811c35e81ac5 tuned signature;
wenzelm
parents: 54984
diff changeset
   694
        raise SIMPLIFIER ("Congruence must start with a constant", [thm]);
22221
8a8aa6114a89 changed cong alist - now using AList operations instead of overwrite_warn
haftmann
parents: 22008
diff changeset
   695
      val (xs, _) = congs;
71239
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   696
      val xs' = Congtab.delete_safe a xs;
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
   697
      val weak' = Congtab.fold (fn (a, th) => if is_full_cong th then I else insert (op =) a) xs' [];
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   698
    in ((xs', weak'), procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) end);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   699
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   700
fun add_cong thm ctxt = add_eqcong (mk_cong ctxt thm) ctxt;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   701
fun del_cong thm ctxt = del_eqcong (mk_cong ctxt thm) ctxt;
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   702
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   703
end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   704
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   705
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   706
(* simprocs *)
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   707
22234
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
   708
datatype simproc =
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
   709
  Simproc of
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
   710
    {name: string,
61098
e1b4b24f2ebd eliminated pointless cterms;
wenzelm
parents: 61095
diff changeset
   711
     lhss: term list,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   712
     proc: morphism -> Proof.context -> cterm -> thm option,
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   713
     stamp: stamp};
22234
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
   714
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   715
fun eq_simproc (Simproc {stamp = stamp1, ...}, Simproc {stamp = stamp2, ...}) = stamp1 = stamp2;
22008
bfc462bfc574 added mk_simproc': tuned interface;
wenzelm
parents: 21962
diff changeset
   716
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   717
fun cert_simproc thy name {lhss, proc} =
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   718
  Simproc {name = name, lhss = map (Sign.cert_term thy) lhss, proc = proc, stamp = stamp ()};
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 61098
diff changeset
   719
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   720
fun transform_simproc phi (Simproc {name, lhss, proc, stamp}) =
22234
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
   721
  Simproc
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
   722
   {name = name,
61098
e1b4b24f2ebd eliminated pointless cterms;
wenzelm
parents: 61095
diff changeset
   723
    lhss = map (Morphism.term phi) lhss,
22669
62857ad97cca Morphism.transform/form;
wenzelm
parents: 22360
diff changeset
   724
    proc = Morphism.transform phi proc,
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   725
    stamp = stamp};
22234
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
   726
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   727
local
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   728
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   729
fun add_proc (proc as Proc {name, lhs, ...}) ctxt =
55031
wenzelm
parents: 55028
diff changeset
   730
 (cond_tracing ctxt (fn () =>
61098
e1b4b24f2ebd eliminated pointless cterms;
wenzelm
parents: 61095
diff changeset
   731
    print_term ctxt ("Adding simplification procedure " ^ quote name ^ " for") lhs);
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   732
  ctxt |> map_simpset2
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   733
    (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) =>
61098
e1b4b24f2ebd eliminated pointless cterms;
wenzelm
parents: 61095
diff changeset
   734
      (congs, Net.insert_term eq_proc (lhs, proc) procs,
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   735
        mk_rews, term_ord, subgoal_tac, loop_tacs, solvers))
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   736
  handle Net.INSERT =>
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   737
    (cond_warning ctxt (fn () => "Ignoring duplicate simplification procedure " ^ quote name);
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   738
      ctxt));
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   739
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   740
fun del_proc (proc as Proc {name, lhs, ...}) ctxt =
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   741
  ctxt |> map_simpset2
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   742
    (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) =>
61098
e1b4b24f2ebd eliminated pointless cterms;
wenzelm
parents: 61095
diff changeset
   743
      (congs, Net.delete_term eq_proc (lhs, proc) procs,
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   744
        mk_rews, term_ord, subgoal_tac, loop_tacs, solvers))
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   745
  handle Net.DELETE =>
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   746
    (cond_warning ctxt (fn () => "Simplification procedure " ^ quote name ^ " not in simpset");
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   747
      ctxt);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   748
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   749
fun prep_procs (Simproc {name, lhss, proc, stamp}) =
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 62876
diff changeset
   750
  lhss |> map (fn lhs => Proc {name = name, lhs = lhs, proc = Morphism.form proc, stamp = stamp});
22234
52ba19aaa9c2 type simproc: explicitl dependency of morphism;
wenzelm
parents: 22221
diff changeset
   751
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   752
in
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   753
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   754
fun ctxt addsimprocs ps = fold (fold add_proc o prep_procs) ps ctxt;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   755
fun ctxt delsimprocs ps = fold (fold del_proc o prep_procs) ps ctxt;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   756
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   757
end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   758
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   759
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   760
(* mk_rews *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   761
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   762
local
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   763
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   764
fun map_mk_rews f =
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   765
  map_simpset2 (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) =>
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   766
    let
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   767
      val {mk, mk_cong, mk_sym, mk_eq_True, reorient} = mk_rews;
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   768
      val (mk', mk_cong', mk_sym', mk_eq_True', reorient') =
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   769
        f (mk, mk_cong, mk_sym, mk_eq_True, reorient);
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   770
      val mk_rews' = {mk = mk', mk_cong = mk_cong', mk_sym = mk_sym', mk_eq_True = mk_eq_True',
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   771
        reorient = reorient'};
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   772
    in (congs, procs, mk_rews', term_ord, subgoal_tac, loop_tacs, solvers) end);
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   773
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   774
in
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   775
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   776
fun mksimps ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   777
  let val Simpset (_, {mk_rews = {mk, ...}, ...}) = simpset_of ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   778
  in mk ctxt end;
30318
3d03190d2864 replaced archaic use of rep_ss by Simplifier.mksimps;
wenzelm
parents: 29269
diff changeset
   779
45625
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45621
diff changeset
   780
fun set_mksimps mk = map_mk_rews (fn (_, mk_cong, mk_sym, mk_eq_True, reorient) =>
18208
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   781
  (mk, mk_cong, mk_sym, mk_eq_True, reorient));
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   782
45625
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45621
diff changeset
   783
fun set_mkcong mk_cong = map_mk_rews (fn (mk, _, mk_sym, mk_eq_True, reorient) =>
18208
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   784
  (mk, mk_cong, mk_sym, mk_eq_True, reorient));
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   785
45625
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45621
diff changeset
   786
fun set_mksym mk_sym = map_mk_rews (fn (mk, mk_cong, _, mk_eq_True, reorient) =>
18208
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   787
  (mk, mk_cong, mk_sym, mk_eq_True, reorient));
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   788
45625
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45621
diff changeset
   789
fun set_mkeqTrue mk_eq_True = map_mk_rews (fn (mk, mk_cong, mk_sym, _, reorient) =>
18208
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   790
  (mk, mk_cong, mk_sym, mk_eq_True, reorient));
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   791
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   792
fun set_reorient reorient = map_mk_rews (fn (mk, mk_cong, mk_sym, mk_eq_True, _) =>
dbdcf366db53 simpset: added reorient field, set_reorient;
wenzelm
parents: 17966
diff changeset
   793
  (mk, mk_cong, mk_sym, mk_eq_True, reorient));
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   794
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   795
end;
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   796
14242
ec70653a02bf Added access to the mk_rews field (and friends).
skalberg
parents: 14040
diff changeset
   797
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   798
(* term_ord *)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   799
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   800
fun set_term_ord term_ord =
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   801
  map_simpset2 (fn (congs, procs, mk_rews, _, subgoal_tac, loop_tacs, solvers) =>
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   802
   (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   803
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   804
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   805
(* tactics *)
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   806
45625
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45621
diff changeset
   807
fun set_subgoaler subgoal_tac =
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   808
  map_simpset2 (fn (congs, procs, mk_rews, term_ord, _, loop_tacs, solvers) =>
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   809
   (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   810
52037
837211662fb8 tuned signature -- depend on context by default;
wenzelm
parents: 51717
diff changeset
   811
fun ctxt setloop tac = ctxt |>
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   812
  map_simpset2 (fn (congs, procs, mk_rews, term_ord, subgoal_tac, _, solvers) =>
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   813
   (congs, procs, mk_rews, term_ord, subgoal_tac, [("", tac)], solvers));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   814
52037
837211662fb8 tuned signature -- depend on context by default;
wenzelm
parents: 51717
diff changeset
   815
fun ctxt addloop (name, tac) = ctxt |>
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   816
  map_simpset2 (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) =>
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   817
    (congs, procs, mk_rews, term_ord, subgoal_tac,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   818
     AList.update (op =) (name, tac) loop_tacs, solvers));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   819
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   820
fun ctxt delloop name = ctxt |>
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   821
  map_simpset2 (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) =>
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   822
    (congs, procs, mk_rews, term_ord, subgoal_tac,
38834
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
   823
     (if AList.defined (op =) loop_tacs name then ()
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   824
      else cond_warning ctxt (fn () => "No such looper in simpset: " ^ quote name);
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   825
      AList.delete (op =) name loop_tacs), solvers));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   826
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   827
fun ctxt setSSolver solver = ctxt |> map_simpset2
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   828
  (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, (unsafe_solvers, _)) =>
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   829
    (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, (unsafe_solvers, [solver])));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   830
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   831
fun ctxt addSSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, term_ord,
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   832
  subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, term_ord,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   833
    subgoal_tac, loop_tacs, (unsafe_solvers, insert eq_solver solver solvers)));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   834
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   835
fun ctxt setSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, term_ord,
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   836
  subgoal_tac, loop_tacs, (_, solvers)) => (congs, procs, mk_rews, term_ord,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   837
    subgoal_tac, loop_tacs, ([solver], solvers)));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   838
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   839
fun ctxt addSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, term_ord,
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   840
  subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, term_ord,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   841
    subgoal_tac, loop_tacs, (insert eq_solver solver unsafe_solvers, solvers)));
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   842
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   843
fun set_solvers solvers = map_simpset2 (fn (congs, procs, mk_rews, term_ord,
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   844
  subgoal_tac, loop_tacs, _) => (congs, procs, mk_rews, term_ord,
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   845
  subgoal_tac, loop_tacs, (solvers, solvers)));
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   846
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   847
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   848
(* trace operations *)
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   849
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   850
type trace_ops =
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   851
 {trace_invoke: {depth: int, term: term} -> Proof.context -> Proof.context,
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
   852
  trace_apply: {unconditional: bool, term: term, thm: thm, rrule: rrule} ->
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   853
    Proof.context -> (Proof.context -> (thm * term) option) -> (thm * term) option};
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   854
54731
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   855
structure Trace_Ops = Theory_Data
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   856
(
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   857
  type T = trace_ops;
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   858
  val empty: T =
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   859
   {trace_invoke = fn _ => fn ctxt => ctxt,
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   860
    trace_apply = fn _ => fn ctxt => fn cont => cont ctxt};
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   861
  val extend = I;
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   862
  fun merge (trace_ops, _) = trace_ops;
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   863
);
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   864
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   865
val set_trace_ops = Trace_Ops.put;
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   866
384ac33802b0 clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
wenzelm
parents: 54729
diff changeset
   867
val trace_ops = Trace_Ops.get o Proof_Context.theory_of;
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   868
fun trace_invoke args ctxt = #trace_invoke (trace_ops ctxt) args ctxt;
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   869
fun trace_apply args ctxt = #trace_apply (trace_ops ctxt) args ctxt;
15006
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   870
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   871
107e4dfd3b96 Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents: 15001
diff changeset
   872
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   873
(** rewriting **)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   874
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   875
(*
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   876
  Uses conversions, see:
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   877
    L C Paulson, A higher-order implementation of rewriting,
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   878
    Science of Computer Programming 3 (1983), pages 119-149.
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   879
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   880
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   881
fun check_conv ctxt msg thm thm' =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   882
  let
36944
dbf831a50e4a less pervasive names from structure Thm;
wenzelm
parents: 36610
diff changeset
   883
    val thm'' = Thm.transitive thm thm' handle THM _ =>
59690
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   884
      let
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   885
        val nthm' =
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   886
          Thm.transitive (Thm.symmetric (Drule.beta_eta_conversion (Thm.lhs_of thm'))) thm'
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   887
      in Thm.transitive thm nthm' handle THM _ =>
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   888
           let
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   889
             val nthm =
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   890
               Thm.transitive thm (Drule.beta_eta_conversion (Thm.rhs_of thm))
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   891
           in Thm.transitive nthm nthm' end
46b635624feb rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
nipkow
parents: 59647
diff changeset
   892
      end
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   893
    val _ =
55031
wenzelm
parents: 55028
diff changeset
   894
      if msg then cond_tracing ctxt (fn () => print_thm ctxt "SUCCEEDED" ("", thm'))
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   895
      else ();
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   896
  in SOME thm'' end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   897
  handle THM _ =>
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26424
diff changeset
   898
    let
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26424
diff changeset
   899
      val _ $ _ $ prop0 = Thm.prop_of thm;
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   900
      val _ =
55032
b49366215417 back to conditional tracing instead of noisy warning (see also 00e849f5b397): these incidents happen occasionally;
wenzelm
parents: 55031
diff changeset
   901
        cond_tracing ctxt (fn () =>
b49366215417 back to conditional tracing instead of noisy warning (see also 00e849f5b397): these incidents happen occasionally;
wenzelm
parents: 55031
diff changeset
   902
          print_thm ctxt "Proved wrong theorem (bad subgoaler?)" ("", thm') ^ "\n" ^
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   903
          print_term ctxt "Should have proved:" prop0);
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   904
    in NONE end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   905
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   906
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   907
(* mk_procrule *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   908
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
   909
fun mk_procrule ctxt thm =
70528
9b3610fe74d6 treat simproc results as atomic -- more compact proof terms;
wenzelm
parents: 70472
diff changeset
   910
  let
9b3610fe74d6 treat simproc results as atomic -- more compact proof terms;
wenzelm
parents: 70472
diff changeset
   911
    val (prems, lhs, elhs, rhs, _) = decomp_simp thm
9b3610fe74d6 treat simproc results as atomic -- more compact proof terms;
wenzelm
parents: 70472
diff changeset
   912
    val thm' = Thm.close_derivation \<^here> thm;
9b3610fe74d6 treat simproc results as atomic -- more compact proof terms;
wenzelm
parents: 70472
diff changeset
   913
  in
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   914
    if rewrite_rule_extra_vars prems lhs rhs
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   915
    then (cond_warning ctxt (fn () => print_thm ctxt "Extra vars on rhs:" ("", thm)); [])
70528
9b3610fe74d6 treat simproc results as atomic -- more compact proof terms;
wenzelm
parents: 70472
diff changeset
   916
    else [mk_rrule2 {thm = thm', name = "", lhs = lhs, elhs = elhs, perm = false}]
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   917
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   918
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   919
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   920
(* rewritec: conversion to apply the meta simpset to a term *)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   921
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   922
(*Since the rewriting strategy is bottom-up, we avoid re-normalizing already
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   923
  normalized terms by carrying around the rhs of the rewrite rule just
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   924
  applied. This is called the `skeleton'. It is decomposed in parallel
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   925
  with the term. Once a Var is encountered, the corresponding term is
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   926
  already in normal form.
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   927
  skel0 is a dummy skeleton that is to enforce complete normalization.*)
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   928
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   929
val skel0 = Bound 0;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   930
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   931
(*Use rhs as skeleton only if the lhs does not contain unnormalized bits.
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   932
  The latter may happen iff there are weak congruence rules for constants
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   933
  in the lhs.*)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   934
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   935
fun uncond_skel ((_, weak), (lhs, rhs)) =
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   936
  if null weak then rhs  (*optimization*)
51591
e4aeb102ad70 amended uncond_skel to observe notion of cong_name properly -- may affect simplification with Free congs;
wenzelm
parents: 51590
diff changeset
   937
  else if exists_subterm
e4aeb102ad70 amended uncond_skel to observe notion of cong_name properly -- may affect simplification with Free congs;
wenzelm
parents: 51590
diff changeset
   938
    (fn Const (a, _) => member (op =) weak (true, a)
e4aeb102ad70 amended uncond_skel to observe notion of cong_name properly -- may affect simplification with Free congs;
wenzelm
parents: 51590
diff changeset
   939
      | Free (a, _) => member (op =) weak (false, a)
e4aeb102ad70 amended uncond_skel to observe notion of cong_name properly -- may affect simplification with Free congs;
wenzelm
parents: 51590
diff changeset
   940
      | _ => false) lhs then skel0
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   941
  else rhs;
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   942
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   943
(*Behaves like unconditional rule if rhs does not contain vars not in the lhs.
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   944
  Otherwise those vars may become instantiated with unnormalized terms
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   945
  while the premises are solved.*)
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   946
32797
2e8fae2d998c eliminated dead code;
wenzelm
parents: 32738
diff changeset
   947
fun cond_skel (args as (_, (lhs, rhs))) =
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 33037
diff changeset
   948
  if subset (op =) (Term.add_vars rhs [], Term.add_vars lhs []) then uncond_skel args
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   949
  else skel0;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   950
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   951
(*
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
   952
  Rewriting -- we try in order:
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   953
    (1) beta reduction
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   954
    (2) unconditional rewrite rules
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   955
    (3) conditional rewrite rules
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   956
    (4) simplification procedures
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   957
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   958
  IMPORTANT: rewrite rules must not introduce new Vars or TVars!
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   959
*)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   960
52091
9ec2d47de6a7 more rigorous check of simplifier context;
wenzelm
parents: 52082
diff changeset
   961
fun rewritec (prover, maxt) ctxt t =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   962
  let
61057
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   963
    val thy = Proof_Context.theory_of ctxt;
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   964
    val Simpset ({rules, ...}, {congs, procs, term_ord, ...}) = simpset_of ctxt;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   965
    val eta_thm = Thm.eta_conversion t;
22902
ac833b4bb7ee moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents: 22892
diff changeset
   966
    val eta_t' = Thm.rhs_of eta_thm;
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   967
    val eta_t = Thm.term_of eta_t';
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
   968
    fun rew rrule =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   969
      let
61057
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   970
        val {thm = thm0, name, lhs, elhs = elhs0, extra, fo, perm} = rrule;
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   971
        val thm = Thm.transfer thy thm0;
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   972
        val elhs = Thm.transfer_cterm thy elhs0;
32797
2e8fae2d998c eliminated dead code;
wenzelm
parents: 32738
diff changeset
   973
        val prop = Thm.prop_of thm;
20546
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   974
        val (rthm, elhs') =
8923deb735ad rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents: 20330
diff changeset
   975
          if maxt = ~1 orelse not extra then (thm, elhs)
22902
ac833b4bb7ee moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents: 22892
diff changeset
   976
          else (Thm.incr_indexes (maxt + 1) thm, Thm.incr_indexes_cterm (maxt + 1) elhs);
61057
5f6a1e31f3ad trim context for persistent storage;
wenzelm
parents: 60822
diff changeset
   977
22902
ac833b4bb7ee moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents: 22892
diff changeset
   978
        val insts =
ac833b4bb7ee moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents: 22892
diff changeset
   979
          if fo then Thm.first_order_match (elhs', eta_t')
ac833b4bb7ee moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents: 22892
diff changeset
   980
          else Thm.match (elhs', eta_t');
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   981
        val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm);
14643
wenzelm
parents: 14330
diff changeset
   982
        val prop' = Thm.prop_of thm';
21576
8c11b1ce2f05 simplified Logic.count_prems;
wenzelm
parents: 21565
diff changeset
   983
        val unconditional = (Logic.count_prems prop' = 0);
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
   984
        val (lhs', rhs') = Logic.dest_equals (Logic.strip_imp_concl prop');
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 55032
diff changeset
   985
        val trace_args = {unconditional = unconditional, term = eta_t, thm = thm', rrule = rrule};
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
   986
      in
67561
f0b11413f1c9 clarified signature: prefer proper order operation;
wenzelm
parents: 66934
diff changeset
   987
        if perm andalso is_greater_equal (term_ord (rhs', lhs'))
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
   988
        then
55031
wenzelm
parents: 55028
diff changeset
   989
         (cond_tracing ctxt (fn () =>
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   990
            print_thm ctxt "Cannot apply permutative rewrite rule" (name, thm) ^ "\n" ^
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   991
            print_thm ctxt "Term does not become smaller:" ("", thm'));
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
   992
          NONE)
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
   993
        else
55031
wenzelm
parents: 55028
diff changeset
   994
         (cond_tracing ctxt (fn () =>
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
   995
            print_thm ctxt "Applying instance of rewrite rule" (name, thm));
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
   996
          if unconditional
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
   997
          then
55031
wenzelm
parents: 55028
diff changeset
   998
           (cond_tracing ctxt (fn () => print_thm ctxt "Rewriting:" ("", thm'));
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
   999
            trace_apply trace_args ctxt (fn ctxt' =>
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1000
              let
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1001
                val lr = Logic.dest_equals prop;
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1002
                val SOME thm'' = check_conv ctxt' false eta_thm thm';
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1003
              in SOME (thm'', uncond_skel (congs, lr)) end))
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1004
          else
55031
wenzelm
parents: 55028
diff changeset
  1005
           (cond_tracing ctxt (fn () => print_thm ctxt "Trying to rewrite:" ("", thm'));
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1006
            if simp_depth ctxt > Config.get ctxt simp_depth_limit
55031
wenzelm
parents: 55028
diff changeset
  1007
            then (cond_tracing ctxt (fn () => "simp_depth_limit exceeded - giving up"); NONE)
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1008
            else
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1009
              trace_apply trace_args ctxt (fn ctxt' =>
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1010
                (case prover ctxt' thm' of
55031
wenzelm
parents: 55028
diff changeset
  1011
                  NONE => (cond_tracing ctxt' (fn () => print_thm ctxt' "FAILED" ("", thm')); NONE)
54729
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1012
                | SOME thm2 =>
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1013
                    (case check_conv ctxt' true eta_thm thm2 of
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1014
                      NONE => NONE
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1015
                    | SOME thm2' =>
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1016
                        let
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1017
                          val concl = Logic.strip_imp_concl prop;
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1018
                          val lr = Logic.dest_equals concl;
c5cd7a58cf2d generic trace operations for main steps of Simplifier;
wenzelm
parents: 54728
diff changeset
  1019
                        in SOME (thm2', cond_skel (congs, lr)) end)))))
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1020
      end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1021
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1022
    fun rews [] = NONE
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1023
      | rews (rrule :: rrules) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1024
          let val opt = rew rrule handle Pattern.MATCH => NONE
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1025
          in (case opt of NONE => rews rrules | some => some) end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1026
38834
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1027
    fun sort_rrules rrs =
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1028
      let
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1029
        fun is_simple ({thm, ...}: rrule) =
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1030
          (case Thm.prop_of thm of
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55635
diff changeset
  1031
            Const ("Pure.eq", _) $ _ $ _ => true
38834
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1032
          | _ => false);
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1033
        fun sort [] (re1, re2) = re1 @ re2
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1034
          | sort (rr :: rrs) (re1, re2) =
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1035
              if is_simple rr
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1036
              then sort rrs (rr :: re1, re2)
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1037
              else sort rrs (re1, rr :: re2);
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1038
      in sort rrs ([], []) end;
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1039
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1040
    fun proc_rews [] = NONE
15023
0e4689f411d5 major cleanup; got rid of obsolete meta_simpset;
wenzelm
parents: 15011
diff changeset
  1041
      | proc_rews (Proc {name, proc, lhs, ...} :: ps) =
61098
e1b4b24f2ebd eliminated pointless cterms;
wenzelm
parents: 61095
diff changeset
  1042
          if Pattern.matches (Proof_Context.theory_of ctxt) (lhs, Thm.term_of t) then
55031
wenzelm
parents: 55028
diff changeset
  1043
            (cond_tracing' ctxt simp_debug (fn () =>
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1044
              print_term ctxt ("Trying procedure " ^ quote name ^ " on:") eta_t);
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1045
             (case proc ctxt eta_t' of
55031
wenzelm
parents: 55028
diff changeset
  1046
               NONE => (cond_tracing' ctxt simp_debug (fn () => "FAILED"); proc_rews ps)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1047
             | SOME raw_thm =>
55031
wenzelm
parents: 55028
diff changeset
  1048
                 (cond_tracing ctxt (fn () =>
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1049
                    print_thm ctxt ("Procedure " ^ quote name ^ " produced rewrite rule:")
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1050
                      ("", raw_thm));
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1051
                  (case rews (mk_procrule ctxt raw_thm) of
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1052
                    NONE =>
55031
wenzelm
parents: 55028
diff changeset
  1053
                     (cond_tracing ctxt (fn () =>
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1054
                        print_term ctxt ("IGNORED result of simproc " ^ quote name ^
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1055
                            " -- does not match") (Thm.term_of t));
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1056
                      proc_rews ps)
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1057
                  | some => some))))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1058
          else proc_rews ps;
38834
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1059
  in
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1060
    (case eta_t of
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1061
      Abs _ $ _ => SOME (Thm.transitive eta_thm (Thm.beta_conversion false eta_t'), skel0)
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1062
    | _ =>
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1063
      (case rews (sort_rrules (Net.match_term rules eta_t)) of
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1064
        NONE => proc_rews (Net.match_term procs eta_t)
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1065
      | some => some))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1066
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1067
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1068
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1069
(* conversion to apply a congruence rule to a term *)
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1070
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1071
fun congc prover ctxt maxt cong t =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1072
  let
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1073
    val rthm = Thm.incr_indexes (maxt + 1) cong;
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
  1074
    val rlhs = fst (Thm.dest_equals (Drule.strip_imp_concl (Thm.cprop_of rthm)));
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1075
    val insts = Thm.match (rlhs, t)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1076
    (* Thm.match can raise Pattern.MATCH;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1077
       is handled when congc is called *)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
  1078
    val thm' =
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
  1079
      Thm.instantiate insts (Thm.rename_boundvars (Thm.term_of rlhs) (Thm.term_of t) rthm);
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1080
    val _ =
55031
wenzelm
parents: 55028
diff changeset
  1081
      cond_tracing ctxt (fn () => print_thm ctxt "Applying congruence rule:" ("", thm'));
wenzelm
parents: 55028
diff changeset
  1082
    fun err (msg, thm) = (cond_tracing ctxt (fn () => print_thm ctxt msg ("", thm)); NONE);
38834
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1083
  in
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1084
    (case prover thm' of
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1085
      NONE => err ("Congruence proof failed.  Could not prove", thm')
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1086
    | SOME thm2 =>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1087
        (case check_conv ctxt true (Drule.beta_eta_conversion t) thm2 of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1088
          NONE => err ("Congruence proof failed.  Should not have proved", thm2)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1089
        | SOME thm2' =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
  1090
            if op aconv (apply2 Thm.term_of (Thm.dest_equals (Thm.cprop_of thm2')))
38834
658fcba35ed7 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38715
diff changeset
  1091
            then NONE else SOME thm2'))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1092
  end;
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1093
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60324
diff changeset
  1094
val vA = (("A", 0), propT);
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60324
diff changeset
  1095
val vB = (("B", 0), propT);
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60324
diff changeset
  1096
val vC = (("C", 0), propT);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1097
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1098
fun transitive1 NONE NONE = NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1099
  | transitive1 (SOME thm1) NONE = SOME thm1
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1100
  | transitive1 NONE (SOME thm2) = SOME thm2
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1101
  | transitive1 (SOME thm1) (SOME thm2) = SOME (Thm.transitive thm1 thm2);
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1102
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1103
fun transitive2 thm = transitive1 (SOME thm);
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15460
diff changeset
  1104
fun transitive3 thm = transitive1 thm o SOME;
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
  1105
52091
9ec2d47de6a7 more rigorous check of simplifier context;
wenzelm
parents: 52082
diff changeset
  1106
fun bottomc ((simprem, useprem, mutsimp), prover, maxidx) =
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1107
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1108
    fun botc skel ctxt t =
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1109
      if is_Var skel then NONE
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1110
      else
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1111
        (case subc skel ctxt t of
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1112
           some as SOME thm1 =>
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1113
             (case rewritec (prover, maxidx) ctxt (Thm.rhs_of thm1) of
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1114
                SOME (thm2, skel2) =>
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1115
                  transitive2 (Thm.transitive thm1 thm2)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1116
                    (botc skel2 ctxt (Thm.rhs_of thm2))
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1117
              | NONE => some)
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1118
         | NONE =>
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1119
             (case rewritec (prover, maxidx) ctxt t of
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1120
                SOME (thm2, skel2) => transitive2 thm2
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1121
                  (botc skel2 ctxt (Thm.rhs_of thm2))
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1122
              | NONE => NONE))
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1123
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1124
    and try_botc ctxt t =
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1125
      (case botc skel0 ctxt t of
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1126
        SOME trec1 => trec1
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1127
      | NONE => Thm.reflexive t)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1128
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1129
    and subc skel ctxt t0 =
55014
a93f496f6c30 general notion of auxiliary bounds within context;
wenzelm
parents: 55000
diff changeset
  1130
        let val Simpset (_, {congs, ...}) = simpset_of ctxt in
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
  1131
          (case Thm.term_of t0 of
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1132
              Abs (a, T, _) =>
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1133
                let
55635
00e900057b38 tuned signature;
wenzelm
parents: 55377
diff changeset
  1134
                    val (v, ctxt') = Variable.next_bound (a, T) ctxt;
00e900057b38 tuned signature;
wenzelm
parents: 55377
diff changeset
  1135
                    val b = #1 (Term.dest_Free v);
00e900057b38 tuned signature;
wenzelm
parents: 55377
diff changeset
  1136
                    val (v', t') = Thm.dest_abs (SOME b) t0;
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
  1137
                    val b' = #1 (Term.dest_Free (Thm.term_of v'));
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1138
                    val _ =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1139
                      if b <> b' then
55635
00e900057b38 tuned signature;
wenzelm
parents: 55377
diff changeset
  1140
                        warning ("Bad Simplifier context: renamed bound variable " ^
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1141
                          quote b ^ " to " ^ quote b' ^ Position.here (Position.thread_data ()))
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1142
                      else ();
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1143
                    val skel' = (case skel of Abs (_, _, sk) => sk | _ => skel0);
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1144
                in
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1145
                  (case botc skel' ctxt' t' of
55635
00e900057b38 tuned signature;
wenzelm
parents: 55377
diff changeset
  1146
                    SOME thm => SOME (Thm.abstract_rule a v' thm)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1147
                  | NONE => NONE)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1148
                end
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1149
            | t $ _ =>
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1150
              (case t of
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55635
diff changeset
  1151
                Const ("Pure.imp", _) $ _  => impc t0 ctxt
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1152
              | Abs _ =>
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1153
                  let val thm = Thm.beta_conversion false t0
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1154
                  in
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1155
                    (case subc skel0 ctxt (Thm.rhs_of thm) of
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1156
                      NONE => SOME thm
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1157
                    | SOME thm' => SOME (Thm.transitive thm thm'))
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1158
                  end
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1159
              | _  =>
54727
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1160
                  let
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1161
                    fun appc () =
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1162
                      let
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1163
                        val (tskel, uskel) =
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1164
                          (case skel of
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1165
                            tskel $ uskel => (tskel, uskel)
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1166
                          | _ => (skel0, skel0));
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1167
                        val (ct, cu) = Thm.dest_comb t0;
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1168
                      in
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1169
                        (case botc tskel ctxt ct of
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1170
                          SOME thm1 =>
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1171
                            (case botc uskel ctxt cu of
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1172
                              SOME thm2 => SOME (Thm.combination thm1 thm2)
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1173
                            | NONE => SOME (Thm.combination thm1 (Thm.reflexive cu)))
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1174
                        | NONE =>
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1175
                            (case botc uskel ctxt cu of
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1176
                              SOME thm1 => SOME (Thm.combination (Thm.reflexive ct) thm1)
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1177
                            | NONE => NONE))
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1178
                      end;
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1179
                    val (h, ts) = strip_comb t;
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1180
                  in
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1181
                    (case cong_name h of
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1182
                      SOME a =>
71239
acc6cb1a1a67 proper table data structure
haftmann
parents: 71235
diff changeset
  1183
                        (case Congtab.lookup (fst congs) a of
61095
50e793295ce1 trim context for persistent storage;
wenzelm
parents: 61090
diff changeset
  1184
                          NONE => appc ()
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1185
                        | SOME cong =>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1186
     (*post processing: some partial applications h t1 ... tj, j <= length ts,
67721
5348bea4accd eliminated ASCII syntax from Pure bootstrap;
wenzelm
parents: 67649
diff changeset
  1187
       may be a redex. Example: map (\<lambda>x. x) = (\<lambda>xs. xs) wrt map_cong*)
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1188
                           (let
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1189
                              val thm = congc (prover ctxt) ctxt maxidx cong t0;
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1190
                              val t = the_default t0 (Option.map Thm.rhs_of thm);
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1191
                              val (cl, cr) = Thm.dest_comb t
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1192
                              val dVar = Var(("", 0), dummyT)
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1193
                              val skel =
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1194
                                list_comb (h, replicate (length ts) dVar)
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1195
                            in
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1196
                              (case botc skel ctxt cl of
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1197
                                NONE => thm
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1198
                              | SOME thm' =>
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1199
                                  transitive3 thm (Thm.combination thm' (Thm.reflexive cr)))
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1200
                            end handle Pattern.MATCH => appc ()))
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1201
                     | _ => appc ())
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1202
                  end)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1203
            | _ => NONE)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1204
        end
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1205
    and impc ct ctxt =
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1206
      if mutsimp then mut_impc0 [] ct [] [] ctxt
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1207
      else nonmut_impc ct ctxt
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1208
54984
da70ab8531f4 more elementary management of declared hyps, below structure Assumption;
wenzelm
parents: 54982
diff changeset
  1209
    and rules_of_prem prem ctxt =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
  1210
      if maxidx_of_term (Thm.term_of prem) <> ~1
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1211
      then
55031
wenzelm
parents: 55028
diff changeset
  1212
       (cond_tracing ctxt (fn () =>
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1213
          print_term ctxt "Cannot add premise as rewrite rule because it contains (type) unknowns:"
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
  1214
            (Thm.term_of prem));
55028
00e849f5b397 clarified Simplifier diagnostics -- simplified ML;
wenzelm
parents: 55014
diff changeset
  1215
        (([], NONE), ctxt))
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
  1216
      else
54984
da70ab8531f4 more elementary management of declared hyps, below structure Assumption;
wenzelm
parents: 54982
diff changeset
  1217
        let val (asm, ctxt') = Thm.assume_hyps prem ctxt
da70ab8531f4 more elementary management of declared hyps, below structure Assumption;
wenzelm
parents: 54982
diff changeset
  1218
        in ((extract_safe_rrules ctxt' asm, SOME asm), ctxt') end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1219
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1220
    and add_rrules (rrss, asms) ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1221
      (fold o fold) insert_rrule rrss ctxt |> add_prems (map_filter I asms)
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1222
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22902
diff changeset
  1223
    and disch r prem eq =
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
  1224
      let
22902
ac833b4bb7ee moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents: 22892
diff changeset
  1225
        val (lhs, rhs) = Thm.dest_equals (Thm.cprop_of eq);
54727
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1226
        val eq' =
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1227
          Thm.implies_elim
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60324
diff changeset
  1228
            (Thm.instantiate ([], [(vA, prem), (vB, lhs), (vC, rhs)]) Drule.imp_cong)
54727
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1229
            (Thm.implies_intr prem eq);
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1230
      in
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1231
        if not r then eq'
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1232
        else
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1233
          let
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1234
            val (prem', concl) = Thm.dest_implies lhs;
54727
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1235
            val (prem'', _) = Thm.dest_implies rhs;
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1236
          in
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1237
            Thm.transitive
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1238
              (Thm.transitive
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60324
diff changeset
  1239
                (Thm.instantiate ([], [(vA, prem'), (vB, prem), (vC, concl)]) Drule.swap_prems_eq)
54727
a806e7251cf0 tuned whitespace;
wenzelm
parents: 54725
diff changeset
  1240
                eq')
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60324
diff changeset
  1241
              (Thm.instantiate ([], [(vA, prem), (vB, prem''), (vC, concl)]) Drule.swap_prems_eq)
54725
fc384e0a7f51 tuned whitespace;
wenzelm
parents: 54724
diff changeset
  1242
          end
10413
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1243
      end
0e015d9bea4e Added new file meta_simplifier.ML
berghofe
parents:
diff changeset
  1244
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
  1245
    and rebuild [] _ _ _ _ eq = eq
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1246
      | rebuild (prem :: prems) concl (_ :: rrss) (_ :: asms) ctxt eq =
13607
6908230623a3 Completely reimplemented mutual simplification of premises.
berghofe
parents: 13569
diff changeset
  1247
          let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51611
diff changeset
  1248
            val ctxt' = add_rrules (rev rrss, rev asms) ctxt;
13607
6908230623a3 Completely