src/Pure/Isar/element.ML
author wenzelm
Mon Jun 05 21:54:23 2006 +0200 (2006-06-05)
changeset 19777 77929c3d2b74
parent 19731 581cdbdbba9a
child 19808 396dd23c54ef
permissions -rw-r--r--
added params_of, prems_of;
added type witness (from locale.ML);
misc cleanup;
wenzelm@18140
     1
(*  Title:      Pure/Isar/element.ML
wenzelm@18140
     2
    ID:         $Id$
wenzelm@18140
     3
    Author:     Makarius
wenzelm@18140
     4
wenzelm@19777
     5
Explicit data structures for some Isar language elements, with derived
wenzelm@19777
     6
logical operations.
wenzelm@18140
     7
*)
wenzelm@18140
     8
wenzelm@18140
     9
signature ELEMENT =
wenzelm@18140
    10
sig
wenzelm@19259
    11
  datatype ('typ, 'term) stmt =
wenzelm@19585
    12
    Shows of ((string * Attrib.src list) * ('term * 'term list) list) list |
wenzelm@19259
    13
    Obtains of (string * ((string * 'typ option) list * 'term list)) list
wenzelm@19259
    14
  type statement  (*= (string, string) stmt*)
wenzelm@19259
    15
  type statement_i  (*= (typ, term) stmt*)
wenzelm@18140
    16
  datatype ('typ, 'term, 'fact) ctxt =
wenzelm@18669
    17
    Fixes of (string * 'typ option * mixfix) list |
wenzelm@18140
    18
    Constrains of (string * 'typ) list |
wenzelm@19585
    19
    Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list |
wenzelm@18140
    20
    Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
wenzelm@18140
    21
    Notes of ((string * Attrib.src list) * ('fact * Attrib.src list) list) list
wenzelm@18140
    22
  type context (*= (string, string, thmref) ctxt*)
wenzelm@18140
    23
  type context_i (*= (typ, term, thm list) ctxt*)
wenzelm@18140
    24
  val map_ctxt: {name: string -> string,
wenzelm@18669
    25
    var: string * mixfix -> string * mixfix,
wenzelm@18140
    26
    typ: 'typ -> 'a, term: 'term -> 'b, fact: 'fact -> 'c,
wenzelm@18140
    27
    attrib: Attrib.src -> Attrib.src} -> ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
wenzelm@18140
    28
  val map_ctxt_values: (typ -> typ) -> (term -> term) -> (thm -> thm) -> context_i -> context_i
wenzelm@19777
    29
  val params_of: ('typ, 'term, 'fact) ctxt -> (string * 'typ) list
wenzelm@19777
    30
  val prems_of: ('typ, 'term, 'fact) ctxt -> 'term list
wenzelm@19777
    31
  val pretty_stmt: Proof.context -> statement_i -> Pretty.T list
wenzelm@19777
    32
  val pretty_ctxt: Proof.context -> context_i -> Pretty.T list
wenzelm@19777
    33
  val pretty_statement: Proof.context -> string -> thm -> Pretty.T
wenzelm@19777
    34
wenzelm@19777
    35
  type witness
wenzelm@19777
    36
  val map_witness: (term * thm -> term * thm) -> witness -> witness
wenzelm@19777
    37
  val witness_prop: witness -> term
wenzelm@19777
    38
  val witness_hyps: witness -> term list
wenzelm@19777
    39
  val assume_witness: theory -> term -> witness
wenzelm@19777
    40
  val prove_witness: theory -> term -> tactic -> witness
wenzelm@19777
    41
  val conclude_witness: witness -> thm
wenzelm@19777
    42
  val mark_witness: term -> term
wenzelm@19777
    43
  val make_witness: term -> thm -> witness
wenzelm@19777
    44
  val refine_witness: Proof.state -> Proof.state
wenzelm@19777
    45
wenzelm@18140
    46
  val rename: (string * (string * mixfix option)) list -> string -> string
wenzelm@18669
    47
  val rename_var: (string * (string * mixfix option)) list -> string * mixfix -> string * mixfix
wenzelm@18140
    48
  val rename_term: (string * (string * mixfix option)) list -> term -> term
wenzelm@18140
    49
  val rename_thm: (string * (string * mixfix option)) list -> thm -> thm
wenzelm@19777
    50
  val rename_witness: (string * (string * mixfix option)) list -> witness -> witness
wenzelm@18140
    51
  val rename_ctxt: (string * (string * mixfix option)) list -> context_i -> context_i
wenzelm@18140
    52
  val instT_type: typ Symtab.table -> typ -> typ
wenzelm@18140
    53
  val instT_term: typ Symtab.table -> term -> term
wenzelm@18140
    54
  val instT_thm: theory -> typ Symtab.table -> thm -> thm
wenzelm@19777
    55
  val instT_witness: theory -> typ Symtab.table -> witness -> witness
wenzelm@18140
    56
  val instT_ctxt: theory -> typ Symtab.table -> context_i -> context_i
wenzelm@18140
    57
  val inst_term: typ Symtab.table * term Symtab.table -> term -> term
wenzelm@18140
    58
  val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
wenzelm@19777
    59
  val inst_witness: theory -> typ Symtab.table * term Symtab.table -> witness -> witness
wenzelm@18140
    60
  val inst_ctxt: theory -> typ Symtab.table * term Symtab.table -> context_i -> context_i
wenzelm@19777
    61
  val satisfy_thm: witness list -> thm -> thm
wenzelm@19777
    62
  val satisfy_witness: witness list -> witness -> witness
wenzelm@18140
    63
end;
wenzelm@18140
    64
wenzelm@18140
    65
structure Element: ELEMENT =
wenzelm@18140
    66
struct
wenzelm@18140
    67
wenzelm@19259
    68
wenzelm@19777
    69
(** language elements **)
wenzelm@19777
    70
wenzelm@19777
    71
(* statement *)
wenzelm@19259
    72
wenzelm@19259
    73
datatype ('typ, 'term) stmt =
wenzelm@19585
    74
  Shows of ((string * Attrib.src list) * ('term * 'term list) list) list |
wenzelm@19259
    75
  Obtains of (string * ((string * 'typ option) list * 'term list)) list;
wenzelm@19259
    76
wenzelm@19259
    77
type statement = (string, string) stmt;
wenzelm@19259
    78
type statement_i = (typ, term) stmt;
wenzelm@19259
    79
wenzelm@19259
    80
wenzelm@19777
    81
(* context *)
wenzelm@18140
    82
wenzelm@18140
    83
datatype ('typ, 'term, 'fact) ctxt =
wenzelm@18669
    84
  Fixes of (string * 'typ option * mixfix) list |
wenzelm@18140
    85
  Constrains of (string * 'typ) list |
wenzelm@19585
    86
  Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list |
wenzelm@18140
    87
  Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
wenzelm@18140
    88
  Notes of ((string * Attrib.src list) * ('fact * Attrib.src list) list) list;
wenzelm@18140
    89
wenzelm@18140
    90
type context = (string, string, thmref) ctxt;
wenzelm@18140
    91
type context_i = (typ, term, thm list) ctxt;
wenzelm@18140
    92
wenzelm@18140
    93
fun map_ctxt {name, var, typ, term, fact, attrib} =
wenzelm@18140
    94
  fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) =>
wenzelm@18140
    95
       let val (x', mx') = var (x, mx) in (x', Option.map typ T, mx') end))
wenzelm@18669
    96
   | Constrains xs => Constrains (xs |> map (fn (x, T) => (#1 (var (x, NoSyn)), typ T)))
wenzelm@18140
    97
   | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
wenzelm@19585
    98
      ((name a, map attrib atts), propps |> map (fn (t, ps) => (term t, map term ps)))))
wenzelm@18140
    99
   | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
wenzelm@18140
   100
      ((name a, map attrib atts), (term t, map term ps))))
wenzelm@18140
   101
   | Notes facts => Notes (facts |> map (fn ((a, atts), bs) =>
wenzelm@18140
   102
      ((name a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
wenzelm@18140
   103
wenzelm@18140
   104
fun map_ctxt_values typ term thm = map_ctxt
wenzelm@18140
   105
  {name = I, var = I, typ = typ, term = term, fact = map thm,
wenzelm@18140
   106
    attrib = Args.map_values I typ term thm};
wenzelm@18140
   107
wenzelm@19777
   108
fun params_of (Fixes fixes) = fixes |> map
wenzelm@19777
   109
    (fn (x, SOME T, _) => (x, T)
wenzelm@19777
   110
      | (x, _, _) => raise TERM ("Untyped context element parameter " ^ quote x, []))
wenzelm@19777
   111
  | params_of _ = [];
wenzelm@18140
   112
wenzelm@19777
   113
fun prems_of (Assumes asms) = maps (map fst o snd) asms
wenzelm@19777
   114
  | prems_of (Defines defs) = map (fst o snd) defs
wenzelm@19777
   115
  | prems_of _ = [];
wenzelm@18140
   116
wenzelm@18894
   117
wenzelm@18894
   118
wenzelm@19259
   119
(** pretty printing **)
wenzelm@19259
   120
wenzelm@19267
   121
fun pretty_items _ _ [] = []
wenzelm@19267
   122
  | pretty_items keyword sep (x :: ys) =
wenzelm@19267
   123
      Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] ::
wenzelm@19267
   124
        map (fn y => Pretty.block [Pretty.str "  ", Pretty.keyword sep, Pretty.brk 1, y]) ys;
wenzelm@19259
   125
wenzelm@19259
   126
fun pretty_name_atts ctxt (name, atts) sep =
wenzelm@19259
   127
  if name = "" andalso null atts then []
wenzelm@19731
   128
  else [Pretty.block
wenzelm@19731
   129
    (Pretty.breaks (Pretty.str name :: Args.pretty_attribs ctxt atts @ [Pretty.str sep]))];
wenzelm@19259
   130
wenzelm@19259
   131
wenzelm@19259
   132
(* pretty_stmt *)
wenzelm@19259
   133
wenzelm@19259
   134
fun pretty_stmt ctxt =
wenzelm@19259
   135
  let
wenzelm@19259
   136
    val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
wenzelm@19259
   137
    val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
wenzelm@19267
   138
    val prt_terms = separate (Pretty.keyword "and") o map prt_term;
wenzelm@19259
   139
    val prt_name_atts = pretty_name_atts ctxt;
wenzelm@19259
   140
wenzelm@19259
   141
    fun prt_show (a, ts) =
wenzelm@19267
   142
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts)));
wenzelm@19259
   143
wenzelm@19259
   144
    fun prt_var (x, SOME T) = Pretty.block [Pretty.str (x ^ " ::"), Pretty.brk 1, prt_typ T]
wenzelm@19259
   145
      | prt_var (x, NONE) = Pretty.str x;
wenzelm@19585
   146
    val prt_vars =  separate (Pretty.keyword "and") o map prt_var;
wenzelm@19259
   147
wenzelm@19267
   148
    fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts))
wenzelm@19259
   149
      | prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks
wenzelm@19585
   150
          (prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts));
wenzelm@19259
   151
  in
wenzelm@19267
   152
    fn Shows shows => pretty_items "shows" "and" (map prt_show shows)
wenzelm@19267
   153
     | Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains)
wenzelm@19259
   154
  end;
wenzelm@19259
   155
wenzelm@18894
   156
wenzelm@19259
   157
(* pretty_ctxt *)
wenzelm@19259
   158
wenzelm@19259
   159
fun pretty_ctxt ctxt =
wenzelm@19259
   160
  let
wenzelm@19259
   161
    val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
wenzelm@19259
   162
    val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
wenzelm@19259
   163
    val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
wenzelm@19259
   164
    val prt_name_atts = pretty_name_atts ctxt;
wenzelm@19259
   165
wenzelm@19267
   166
    fun prt_mixfix NoSyn = []
wenzelm@19267
   167
      | prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx];
wenzelm@19267
   168
wenzelm@19259
   169
    fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
wenzelm@19259
   170
          prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
wenzelm@19259
   171
      | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_mixfix mx);
wenzelm@19259
   172
    fun prt_constrain (x, T) = prt_fix (x, SOME T, NoSyn);
wenzelm@18894
   173
wenzelm@19259
   174
    fun prt_asm (a, ts) =
wenzelm@19259
   175
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
wenzelm@19259
   176
    fun prt_def (a, (t, _)) =
wenzelm@19259
   177
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
wenzelm@19259
   178
wenzelm@19259
   179
    fun prt_fact (ths, []) = map prt_thm ths
wenzelm@19259
   180
      | prt_fact (ths, atts) = Pretty.enclose "(" ")"
wenzelm@19259
   181
          (Pretty.breaks (map prt_thm ths)) :: Args.pretty_attribs ctxt atts;
wenzelm@19259
   182
    fun prt_note (a, ths) =
wenzelm@19482
   183
      Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths)));
wenzelm@19259
   184
  in
wenzelm@19267
   185
    fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes)
wenzelm@19267
   186
     | Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs)
wenzelm@19267
   187
     | Assumes asms => pretty_items "assumes" "and" (map prt_asm asms)
wenzelm@19267
   188
     | Defines defs => pretty_items "defines" "and" (map prt_def defs)
wenzelm@19267
   189
     | Notes facts => pretty_items "notes" "and" (map prt_note facts)
wenzelm@19259
   190
  end;
wenzelm@18894
   191
wenzelm@19267
   192
wenzelm@19267
   193
(* pretty_statement *)
wenzelm@19267
   194
wenzelm@19267
   195
local
wenzelm@19267
   196
wenzelm@19267
   197
fun thm_name kind th prts =
wenzelm@19267
   198
  let val head =
wenzelm@19267
   199
    (case #1 (Thm.get_name_tags th) of
wenzelm@19267
   200
      "" => Pretty.command kind
wenzelm@19267
   201
    | a => Pretty.block [Pretty.command kind, Pretty.brk 1, Pretty.str (Sign.base_name a ^ ":")])
wenzelm@19267
   202
  in Pretty.block (Pretty.fbreaks (head :: prts)) end;
wenzelm@19267
   203
wenzelm@19267
   204
fun obtain prop ctxt =
wenzelm@19267
   205
  let
wenzelm@19267
   206
    val xs = ProofContext.rename_frees ctxt [] (Logic.strip_params prop);
wenzelm@19267
   207
    val args = rev (map Free xs);
wenzelm@19267
   208
    val As = Logic.strip_assums_hyp prop |> map (fn t => Term.subst_bounds (args, t));
wenzelm@19267
   209
    val ctxt' = ctxt |> fold ProofContext.declare_term args;
wenzelm@19267
   210
  in (("", (map (apsnd SOME) xs, As)), ctxt') end;
wenzelm@19267
   211
wenzelm@19267
   212
in
wenzelm@19267
   213
wenzelm@19267
   214
fun pretty_statement ctxt kind raw_th =
wenzelm@19267
   215
  let
wenzelm@19267
   216
    val thy = ProofContext.theory_of ctxt;
wenzelm@19267
   217
    val th = Goal.norm_hhf raw_th;
wenzelm@19267
   218
wenzelm@19267
   219
    fun standard_thesis t =
wenzelm@19267
   220
      let
wenzelm@19267
   221
        val C = ObjectLogic.drop_judgment thy (Thm.concl_of th);
wenzelm@19466
   222
        val C' = Var ((AutoBind.thesisN, Thm.maxidx_of th + 1), Term.fastype_of C);
wenzelm@19267
   223
      in Term.subst_atomic [(C, C')] t end;
wenzelm@19267
   224
wenzelm@19267
   225
    val raw_prop =
wenzelm@19267
   226
      Thm.prop_of th
wenzelm@19267
   227
      |> singleton (ProofContext.monomorphic ctxt)
wenzelm@19267
   228
      |> K (ObjectLogic.is_elim th) ? standard_thesis
wenzelm@19267
   229
      |> Term.zero_var_indexes;
wenzelm@19267
   230
wenzelm@19267
   231
    val vars = Term.add_vars raw_prop [];
wenzelm@19267
   232
    val frees = ProofContext.rename_frees ctxt [raw_prop] (map (apfst fst) vars);
wenzelm@19305
   233
    val fixes = rev (filter_out (fn (x, _) => x = AutoBind.thesisN) frees);
wenzelm@19267
   234
wenzelm@19267
   235
    val prop = Term.instantiate ([], vars ~~ map Free frees) raw_prop;
wenzelm@19267
   236
    val (prems, concl) = Logic.strip_horn prop;
wenzelm@19267
   237
    val thesis = ObjectLogic.fixed_judgment thy AutoBind.thesisN;
wenzelm@19267
   238
    val (asms, cases) = take_suffix (fn prem => thesis aconv Logic.strip_assums_concl prem) prems;
wenzelm@19267
   239
  in
wenzelm@19267
   240
    pretty_ctxt ctxt (Fixes (map (fn (x, T) => (x, SOME T, NoSyn)) fixes)) @
wenzelm@19585
   241
    pretty_ctxt ctxt (Assumes (map (fn t => (("", []), [(t, [])])) asms)) @
wenzelm@19267
   242
    pretty_stmt ctxt
wenzelm@19585
   243
     (if null cases then Shows [(("", []), [(concl, [])])]
wenzelm@19267
   244
      else Obtains (#1 (fold_map obtain cases (ctxt |> ProofContext.declare_term prop))))
wenzelm@19267
   245
  end |> thm_name kind raw_th;
wenzelm@19267
   246
wenzelm@18140
   247
end;
wenzelm@19267
   248
wenzelm@19777
   249
wenzelm@19777
   250
wenzelm@19777
   251
(** logical operations **)
wenzelm@19777
   252
wenzelm@19777
   253
(* witnesses -- hypotheses as protected facts *)
wenzelm@19777
   254
wenzelm@19777
   255
datatype witness = Witness of term * thm;
wenzelm@19777
   256
wenzelm@19777
   257
fun map_witness f (Witness witn) = Witness (f witn);
wenzelm@19777
   258
wenzelm@19777
   259
fun witness_prop (Witness (t, _)) = t;
wenzelm@19777
   260
fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
wenzelm@19777
   261
wenzelm@19777
   262
fun assume_witness thy t =
wenzelm@19777
   263
  Witness (t, Goal.protect (Thm.assume (Thm.cterm_of thy t)));
wenzelm@19777
   264
wenzelm@19777
   265
fun prove_witness thy t tac =
wenzelm@19777
   266
  Witness (t, Goal.prove thy [] [] (Logic.protect t) (fn _ =>
wenzelm@19777
   267
    Tactic.rtac Drule.protectI 1 THEN tac));
wenzelm@19777
   268
wenzelm@19777
   269
fun conclude_witness (Witness (_, th)) = Goal.norm_hhf (Goal.conclude th);
wenzelm@19777
   270
wenzelm@19777
   271
val mark_witness = Logic.protect;
wenzelm@19777
   272
wenzelm@19777
   273
fun make_witness t th = Witness (t, th);
wenzelm@19777
   274
wenzelm@19777
   275
val refine_witness =
wenzelm@19777
   276
  Proof.refine (Method.Basic (K (Method.RAW_METHOD
wenzelm@19777
   277
    (K (ALLGOALS
wenzelm@19777
   278
      (PRECISE_CONJUNCTS ~1 (ALLGOALS
wenzelm@19777
   279
        (PRECISE_CONJUNCTS ~1 (TRYALL (Tactic.rtac Drule.protectI))))))))))
wenzelm@19777
   280
  #> Seq.hd;
wenzelm@19777
   281
wenzelm@19777
   282
wenzelm@19777
   283
(* derived rules *)
wenzelm@19777
   284
wenzelm@19777
   285
fun instantiate_tfrees thy subst =
wenzelm@19777
   286
  let
wenzelm@19777
   287
    val certT = Thm.ctyp_of thy;
wenzelm@19777
   288
    fun inst vs (a, T) = AList.lookup (op =) vs a
wenzelm@19777
   289
      |> Option.map (fn v => (certT (TVar v), certT T));
wenzelm@19777
   290
  in
wenzelm@19777
   291
    Drule.tvars_intr_list (map fst subst) #->
wenzelm@19777
   292
    (fn vs => Thm.instantiate (map_filter (inst vs) subst, []))
wenzelm@19777
   293
  end;
wenzelm@19777
   294
wenzelm@19777
   295
fun instantiate_frees thy subst =
wenzelm@19777
   296
  let val cert = Thm.cterm_of thy in
wenzelm@19777
   297
    Drule.forall_intr_list (map (cert o Free o fst) subst) #>
wenzelm@19777
   298
    Drule.forall_elim_list (map (cert o snd) subst)
wenzelm@19777
   299
  end;
wenzelm@19777
   300
wenzelm@19777
   301
fun hyps_rule rule th =
wenzelm@19777
   302
  let
wenzelm@19777
   303
    val cterm_rule = Thm.reflexive #> rule #> Thm.cprop_of #> Drule.dest_equals #> #1;
wenzelm@19777
   304
    val {hyps, ...} = Thm.crep_thm th;
wenzelm@19777
   305
  in
wenzelm@19777
   306
    Drule.implies_elim_list
wenzelm@19777
   307
      (rule (Drule.implies_intr_list hyps th))
wenzelm@19777
   308
      (map (Thm.assume o cterm_rule) hyps)
wenzelm@19777
   309
  end;
wenzelm@19777
   310
wenzelm@19777
   311
wenzelm@19777
   312
(* rename *)
wenzelm@19777
   313
wenzelm@19777
   314
fun rename ren x =
wenzelm@19777
   315
  (case AList.lookup (op =) ren (x: string) of
wenzelm@19777
   316
    NONE => x
wenzelm@19777
   317
  | SOME (x', _) => x');
wenzelm@19777
   318
wenzelm@19777
   319
fun rename_var ren (x, mx) =
wenzelm@19777
   320
  (case (AList.lookup (op =) ren (x: string), mx) of
wenzelm@19777
   321
    (NONE, _) => (x, mx)
wenzelm@19777
   322
  | (SOME (x', NONE), Structure) => (x', mx)
wenzelm@19777
   323
  | (SOME (x', SOME _), Structure) =>
wenzelm@19777
   324
      error ("Attempt to change syntax of structure parameter " ^ quote x)
wenzelm@19777
   325
  | (SOME (x', NONE), _) => (x', NoSyn)
wenzelm@19777
   326
  | (SOME (x', SOME mx'), _) => (x', mx'));
wenzelm@19777
   327
wenzelm@19777
   328
fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
wenzelm@19777
   329
  | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
wenzelm@19777
   330
  | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
wenzelm@19777
   331
  | rename_term _ a = a;
wenzelm@19777
   332
wenzelm@19777
   333
fun rename_thm ren th =
wenzelm@19777
   334
  let
wenzelm@19777
   335
    val subst = Drule.frees_of th
wenzelm@19777
   336
      |> map_filter (fn (x, T) =>
wenzelm@19777
   337
        let val x' = rename ren x
wenzelm@19777
   338
        in if x = x' then NONE else SOME ((x, T), (Free (x', T))) end);
wenzelm@19777
   339
  in
wenzelm@19777
   340
    if null subst then th
wenzelm@19777
   341
    else th |> hyps_rule (instantiate_frees (Thm.theory_of_thm th) subst)
wenzelm@19777
   342
  end;
wenzelm@19777
   343
wenzelm@19777
   344
fun rename_witness ren =
wenzelm@19777
   345
  map_witness (fn (t, th) => (rename_term ren t, rename_thm ren th));
wenzelm@19777
   346
wenzelm@19777
   347
fun rename_ctxt ren =
wenzelm@19777
   348
  map_ctxt_values I (rename_term ren) (rename_thm ren)
wenzelm@19777
   349
  #> map_ctxt {name = I, typ = I, term = I, fact = I, attrib = I, var = rename_var ren};
wenzelm@19777
   350
wenzelm@19777
   351
wenzelm@19777
   352
(* instantiate types *)
wenzelm@19777
   353
wenzelm@19777
   354
fun instT_type env =
wenzelm@19777
   355
  if Symtab.is_empty env then I
wenzelm@19777
   356
  else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
wenzelm@19777
   357
wenzelm@19777
   358
fun instT_term env =
wenzelm@19777
   359
  if Symtab.is_empty env then I
wenzelm@19777
   360
  else Term.map_term_types (instT_type env);
wenzelm@19777
   361
wenzelm@19777
   362
fun instT_subst env th =
wenzelm@19777
   363
  Drule.tfrees_of th
wenzelm@19777
   364
  |> map_filter (fn (a, S) =>
wenzelm@19777
   365
    let
wenzelm@19777
   366
      val T = TFree (a, S);
wenzelm@19777
   367
      val T' = the_default T (Symtab.lookup env a);
wenzelm@19777
   368
    in if T = T' then NONE else SOME (a, T') end);
wenzelm@19777
   369
wenzelm@19777
   370
fun instT_thm thy env th =
wenzelm@19777
   371
  if Symtab.is_empty env then th
wenzelm@19777
   372
  else
wenzelm@19777
   373
    let val subst = instT_subst env th
wenzelm@19777
   374
    in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
wenzelm@19777
   375
wenzelm@19777
   376
fun instT_witness thy env =
wenzelm@19777
   377
  map_witness (fn (t, th) => (instT_term env t, instT_thm thy env th));
wenzelm@19777
   378
wenzelm@19777
   379
fun instT_ctxt thy env =
wenzelm@19777
   380
  map_ctxt_values (instT_type env) (instT_term env) (instT_thm thy env);
wenzelm@19777
   381
wenzelm@19777
   382
wenzelm@19777
   383
(* instantiate types and terms *)
wenzelm@19777
   384
wenzelm@19777
   385
fun inst_term (envT, env) =
wenzelm@19777
   386
  if Symtab.is_empty env then instT_term envT
wenzelm@19777
   387
  else
wenzelm@19777
   388
    let
wenzelm@19777
   389
      val instT = instT_type envT;
wenzelm@19777
   390
      fun inst (Const (x, T)) = Const (x, instT T)
wenzelm@19777
   391
        | inst (Free (x, T)) =
wenzelm@19777
   392
            (case Symtab.lookup env x of
wenzelm@19777
   393
              NONE => Free (x, instT T)
wenzelm@19777
   394
            | SOME t => t)
wenzelm@19777
   395
        | inst (Var (xi, T)) = Var (xi, instT T)
wenzelm@19777
   396
        | inst (b as Bound _) = b
wenzelm@19777
   397
        | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
wenzelm@19777
   398
        | inst (t $ u) = inst t $ inst u;
wenzelm@19777
   399
    in Envir.beta_norm o inst end;
wenzelm@19777
   400
wenzelm@19777
   401
fun inst_thm thy (envT, env) th =
wenzelm@19777
   402
  if Symtab.is_empty env then instT_thm thy envT th
wenzelm@19777
   403
  else
wenzelm@19777
   404
    let
wenzelm@19777
   405
      val substT = instT_subst envT th;
wenzelm@19777
   406
      val subst = Drule.frees_of th
wenzelm@19777
   407
        |> map_filter (fn (x, T) =>
wenzelm@19777
   408
          let
wenzelm@19777
   409
            val T' = instT_type envT T;
wenzelm@19777
   410
            val t = Free (x, T');
wenzelm@19777
   411
            val t' = the_default t (Symtab.lookup env x);
wenzelm@19777
   412
          in if t aconv t' then NONE else SOME ((x, T'), t') end);
wenzelm@19777
   413
    in
wenzelm@19777
   414
      if null substT andalso null subst then th
wenzelm@19777
   415
      else th |> hyps_rule
wenzelm@19777
   416
       (instantiate_tfrees thy substT #>
wenzelm@19777
   417
        instantiate_frees thy subst #>
wenzelm@19777
   418
        Drule.fconv_rule (Thm.beta_conversion true))
wenzelm@19777
   419
    end;
wenzelm@19777
   420
wenzelm@19777
   421
fun inst_witness thy envs =
wenzelm@19777
   422
  map_witness (fn (t, th) => (inst_term envs t, inst_thm thy envs th));
wenzelm@19777
   423
wenzelm@19777
   424
fun inst_ctxt thy envs =
wenzelm@19777
   425
  map_ctxt_values (instT_type (#1 envs)) (inst_term envs) (inst_thm thy envs);
wenzelm@19777
   426
wenzelm@19777
   427
wenzelm@19777
   428
(* satisfy hypotheses *)
wenzelm@19777
   429
wenzelm@19777
   430
fun satisfy_thm witns thm = thm |> fold (fn hyp =>
wenzelm@19777
   431
    (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
wenzelm@19777
   432
      NONE => I
wenzelm@19777
   433
    | SOME (Witness (_, th)) => Drule.implies_intr_protected [hyp] #> Goal.comp_hhf th))
wenzelm@19777
   434
  (#hyps (Thm.crep_thm thm));
wenzelm@19777
   435
wenzelm@19777
   436
fun satisfy_witness witns = map_witness (apsnd (satisfy_thm witns));
wenzelm@19777
   437
wenzelm@19267
   438
end;