src/Pure/Isar/element.ML
author wenzelm
Sun Jul 08 19:51:58 2007 +0200 (2007-07-08)
changeset 23655 d2d1138e0ddc
parent 23414 927203ad4b3a
child 24137 8d7896398147
permissions -rw-r--r--
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
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@21440
    21
    Notes of string * ((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@21581
    24
  val facts_map: (('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt) ->
wenzelm@21581
    25
   ((string * Attrib.src list) * ('fact * Attrib.src list) list) list ->
wenzelm@21581
    26
   ((string * Attrib.src list) * ('c * Attrib.src list) list) list
wenzelm@18140
    27
  val map_ctxt: {name: string -> string,
wenzelm@18669
    28
    var: string * mixfix -> string * mixfix,
wenzelm@18140
    29
    typ: 'typ -> 'a, term: 'term -> 'b, fact: 'fact -> 'c,
wenzelm@18140
    30
    attrib: Attrib.src -> Attrib.src} -> ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
wenzelm@21528
    31
  val map_ctxt_attrib: (Attrib.src -> Attrib.src) ->
wenzelm@21528
    32
    ('typ, 'term, 'fact) ctxt -> ('typ, 'term, 'fact) ctxt
wenzelm@21481
    33
  val morph_ctxt: morphism -> context_i -> context_i
wenzelm@19808
    34
  val params_of: context_i -> (string * typ) list
wenzelm@19808
    35
  val prems_of: context_i -> term list
wenzelm@19808
    36
  val facts_of: theory -> context_i ->
wenzelm@19808
    37
    ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
wenzelm@19777
    38
  val pretty_stmt: Proof.context -> statement_i -> Pretty.T list
wenzelm@19777
    39
  val pretty_ctxt: Proof.context -> context_i -> Pretty.T list
wenzelm@19777
    40
  val pretty_statement: Proof.context -> string -> thm -> Pretty.T
wenzelm@19777
    41
  type witness
wenzelm@19777
    42
  val map_witness: (term * thm -> term * thm) -> witness -> witness
wenzelm@21481
    43
  val morph_witness: morphism -> witness -> witness
wenzelm@19777
    44
  val witness_prop: witness -> term
wenzelm@19777
    45
  val witness_hyps: witness -> term list
wenzelm@19777
    46
  val assume_witness: theory -> term -> witness
wenzelm@20058
    47
  val prove_witness: Proof.context -> term -> tactic -> witness
wenzelm@19777
    48
  val conclude_witness: witness -> thm
wenzelm@19777
    49
  val mark_witness: term -> term
wenzelm@19777
    50
  val make_witness: term -> thm -> witness
ballarin@19931
    51
  val dest_witness: witness -> term * thm
ballarin@20068
    52
  val transfer_witness: theory -> witness -> witness
wenzelm@19808
    53
  val refine_witness: Proof.state -> Proof.state Seq.seq
ballarin@22658
    54
  val pretty_witness: Proof.context -> witness -> Pretty.T
wenzelm@18140
    55
  val rename: (string * (string * mixfix option)) list -> string -> string
wenzelm@18669
    56
  val rename_var: (string * (string * mixfix option)) list -> string * mixfix -> string * mixfix
wenzelm@18140
    57
  val rename_term: (string * (string * mixfix option)) list -> term -> term
wenzelm@18140
    58
  val rename_thm: (string * (string * mixfix option)) list -> thm -> thm
wenzelm@21481
    59
  val rename_morphism: (string * (string * mixfix option)) list -> morphism
wenzelm@18140
    60
  val instT_type: typ Symtab.table -> typ -> typ
wenzelm@18140
    61
  val instT_term: typ Symtab.table -> term -> term
wenzelm@18140
    62
  val instT_thm: theory -> typ Symtab.table -> thm -> thm
wenzelm@21481
    63
  val instT_morphism: theory -> typ Symtab.table -> morphism
wenzelm@18140
    64
  val inst_term: typ Symtab.table * term Symtab.table -> term -> term
wenzelm@18140
    65
  val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
wenzelm@21481
    66
  val inst_morphism: theory -> typ Symtab.table * term Symtab.table -> morphism
wenzelm@19777
    67
  val satisfy_thm: witness list -> thm -> thm
wenzelm@21481
    68
  val satisfy_morphism: witness list -> morphism
wenzelm@20264
    69
  val satisfy_facts: witness list ->
wenzelm@20264
    70
    ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@20264
    71
    ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
wenzelm@21581
    72
  val generalize_facts: Proof.context -> Proof.context ->
wenzelm@20264
    73
    ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@20264
    74
    ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
wenzelm@18140
    75
end;
wenzelm@18140
    76
wenzelm@18140
    77
structure Element: ELEMENT =
wenzelm@18140
    78
struct
wenzelm@18140
    79
wenzelm@19777
    80
(** language elements **)
wenzelm@19777
    81
wenzelm@19777
    82
(* statement *)
wenzelm@19259
    83
wenzelm@19259
    84
datatype ('typ, 'term) stmt =
wenzelm@19585
    85
  Shows of ((string * Attrib.src list) * ('term * 'term list) list) list |
wenzelm@19259
    86
  Obtains of (string * ((string * 'typ option) list * 'term list)) list;
wenzelm@19259
    87
wenzelm@19259
    88
type statement = (string, string) stmt;
wenzelm@19259
    89
type statement_i = (typ, term) stmt;
wenzelm@19259
    90
wenzelm@19259
    91
wenzelm@19777
    92
(* context *)
wenzelm@18140
    93
wenzelm@18140
    94
datatype ('typ, 'term, 'fact) ctxt =
wenzelm@18669
    95
  Fixes of (string * 'typ option * mixfix) list |
wenzelm@18140
    96
  Constrains of (string * 'typ) list |
wenzelm@19585
    97
  Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list |
wenzelm@18140
    98
  Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
wenzelm@21440
    99
  Notes of string * ((string * Attrib.src list) * ('fact * Attrib.src list) list) list;
wenzelm@18140
   100
wenzelm@18140
   101
type context = (string, string, thmref) ctxt;
wenzelm@18140
   102
type context_i = (typ, term, thm list) ctxt;
wenzelm@18140
   103
wenzelm@21581
   104
fun facts_map f facts = Notes ("", facts) |> f |> (fn Notes (_, facts') => facts');
wenzelm@21581
   105
wenzelm@18140
   106
fun map_ctxt {name, var, typ, term, fact, attrib} =
wenzelm@18140
   107
  fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) =>
wenzelm@18140
   108
       let val (x', mx') = var (x, mx) in (x', Option.map typ T, mx') end))
wenzelm@18669
   109
   | Constrains xs => Constrains (xs |> map (fn (x, T) => (#1 (var (x, NoSyn)), typ T)))
wenzelm@18140
   110
   | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
wenzelm@19585
   111
      ((name a, map attrib atts), propps |> map (fn (t, ps) => (term t, map term ps)))))
wenzelm@18140
   112
   | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
wenzelm@18140
   113
      ((name a, map attrib atts), (term t, map term ps))))
wenzelm@21440
   114
   | Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) =>
wenzelm@18140
   115
      ((name a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
wenzelm@18140
   116
wenzelm@21528
   117
fun map_ctxt_attrib attrib =
wenzelm@21528
   118
  map_ctxt {name = I, var = I, typ = I, term = I, fact = I, attrib = attrib};
wenzelm@21528
   119
wenzelm@21481
   120
fun morph_ctxt phi = map_ctxt
wenzelm@21481
   121
 {name = Morphism.name phi,
wenzelm@21481
   122
  var = Morphism.var phi,
wenzelm@21481
   123
  typ = Morphism.typ phi,
wenzelm@21481
   124
  term = Morphism.term phi,
wenzelm@21521
   125
  fact = Morphism.fact phi,
wenzelm@21481
   126
  attrib = Args.morph_values phi};
wenzelm@18140
   127
wenzelm@19808
   128
wenzelm@19808
   129
(* logical content *)
wenzelm@19808
   130
wenzelm@19777
   131
fun params_of (Fixes fixes) = fixes |> map
wenzelm@19777
   132
    (fn (x, SOME T, _) => (x, T)
wenzelm@19777
   133
      | (x, _, _) => raise TERM ("Untyped context element parameter " ^ quote x, []))
wenzelm@19777
   134
  | params_of _ = [];
wenzelm@18140
   135
wenzelm@19777
   136
fun prems_of (Assumes asms) = maps (map fst o snd) asms
wenzelm@19777
   137
  | prems_of (Defines defs) = map (fst o snd) defs
wenzelm@19777
   138
  | prems_of _ = [];
wenzelm@18140
   139
wenzelm@20233
   140
fun assume thy t = Assumption.assume (Thm.cterm_of thy t);
wenzelm@19808
   141
wenzelm@19808
   142
fun facts_of thy (Assumes asms) = map (apsnd (map (fn (t, _) => ([assume thy t], [])))) asms
wenzelm@19808
   143
  | facts_of thy (Defines defs) = map (apsnd (fn (t, _) => [([assume thy t], [])])) defs
wenzelm@21440
   144
  | facts_of _ (Notes (_, facts)) = facts
wenzelm@19808
   145
  | facts_of _ _ = [];
wenzelm@19808
   146
wenzelm@18894
   147
wenzelm@18894
   148
wenzelm@19259
   149
(** pretty printing **)
wenzelm@19259
   150
wenzelm@19267
   151
fun pretty_items _ _ [] = []
wenzelm@19267
   152
  | pretty_items keyword sep (x :: ys) =
wenzelm@19267
   153
      Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] ::
wenzelm@19267
   154
        map (fn y => Pretty.block [Pretty.str "  ", Pretty.keyword sep, Pretty.brk 1, y]) ys;
wenzelm@19259
   155
wenzelm@19259
   156
fun pretty_name_atts ctxt (name, atts) sep =
wenzelm@19259
   157
  if name = "" andalso null atts then []
wenzelm@19731
   158
  else [Pretty.block
wenzelm@21032
   159
    (Pretty.breaks (Pretty.str name :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))];
wenzelm@19259
   160
wenzelm@19259
   161
wenzelm@19259
   162
(* pretty_stmt *)
wenzelm@19259
   163
wenzelm@19259
   164
fun pretty_stmt ctxt =
wenzelm@19259
   165
  let
wenzelm@19259
   166
    val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
wenzelm@19259
   167
    val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
wenzelm@19267
   168
    val prt_terms = separate (Pretty.keyword "and") o map prt_term;
wenzelm@19259
   169
    val prt_name_atts = pretty_name_atts ctxt;
wenzelm@19259
   170
wenzelm@19259
   171
    fun prt_show (a, ts) =
wenzelm@19267
   172
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts)));
wenzelm@19259
   173
wenzelm@19259
   174
    fun prt_var (x, SOME T) = Pretty.block [Pretty.str (x ^ " ::"), Pretty.brk 1, prt_typ T]
wenzelm@19259
   175
      | prt_var (x, NONE) = Pretty.str x;
wenzelm@19585
   176
    val prt_vars =  separate (Pretty.keyword "and") o map prt_var;
wenzelm@19259
   177
wenzelm@19267
   178
    fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts))
wenzelm@19259
   179
      | prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks
wenzelm@19585
   180
          (prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts));
wenzelm@19259
   181
  in
wenzelm@19267
   182
    fn Shows shows => pretty_items "shows" "and" (map prt_show shows)
wenzelm@19267
   183
     | Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains)
wenzelm@19259
   184
  end;
wenzelm@19259
   185
wenzelm@18894
   186
wenzelm@19259
   187
(* pretty_ctxt *)
wenzelm@19259
   188
wenzelm@19259
   189
fun pretty_ctxt ctxt =
wenzelm@19259
   190
  let
wenzelm@19259
   191
    val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
wenzelm@19259
   192
    val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
wenzelm@19259
   193
    val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
wenzelm@19259
   194
    val prt_name_atts = pretty_name_atts ctxt;
wenzelm@19259
   195
wenzelm@19267
   196
    fun prt_mixfix NoSyn = []
wenzelm@19267
   197
      | prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx];
wenzelm@19267
   198
wenzelm@19259
   199
    fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
wenzelm@19259
   200
          prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
wenzelm@19259
   201
      | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_mixfix mx);
wenzelm@19259
   202
    fun prt_constrain (x, T) = prt_fix (x, SOME T, NoSyn);
wenzelm@18894
   203
wenzelm@19259
   204
    fun prt_asm (a, ts) =
wenzelm@19259
   205
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
wenzelm@19259
   206
    fun prt_def (a, (t, _)) =
wenzelm@19259
   207
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
wenzelm@19259
   208
wenzelm@19259
   209
    fun prt_fact (ths, []) = map prt_thm ths
wenzelm@19259
   210
      | prt_fact (ths, atts) = Pretty.enclose "(" ")"
wenzelm@21032
   211
          (Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts;
wenzelm@19259
   212
    fun prt_note (a, ths) =
wenzelm@19482
   213
      Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths)));
wenzelm@19259
   214
  in
wenzelm@19267
   215
    fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes)
wenzelm@19267
   216
     | Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs)
wenzelm@19267
   217
     | Assumes asms => pretty_items "assumes" "and" (map prt_asm asms)
wenzelm@19267
   218
     | Defines defs => pretty_items "defines" "and" (map prt_def defs)
wenzelm@21440
   219
     | Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts)
wenzelm@21440
   220
     | Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts)
wenzelm@19259
   221
  end;
wenzelm@18894
   222
wenzelm@19267
   223
wenzelm@19267
   224
(* pretty_statement *)
wenzelm@19267
   225
wenzelm@19267
   226
local
wenzelm@19267
   227
wenzelm@19267
   228
fun thm_name kind th prts =
wenzelm@19267
   229
  let val head =
wenzelm@21965
   230
    if PureThy.has_name_hint th then
wenzelm@21965
   231
      Pretty.block [Pretty.command kind,
wenzelm@21965
   232
        Pretty.brk 1, Pretty.str (Sign.base_name (PureThy.get_name_hint th) ^ ":")]
wenzelm@21965
   233
    else Pretty.command kind
wenzelm@19267
   234
  in Pretty.block (Pretty.fbreaks (head :: prts)) end;
wenzelm@19267
   235
wenzelm@19267
   236
fun obtain prop ctxt =
wenzelm@19267
   237
  let
wenzelm@20150
   238
    val ((xs, prop'), ctxt') = Variable.focus prop ctxt;
wenzelm@20150
   239
    val As = Logic.strip_imp_prems (Thm.term_of prop');
wenzelm@20150
   240
    fun var (x, T) = (ProofContext.revert_skolem ctxt' x, SOME T);
wenzelm@20218
   241
  in (("", (map (var o Term.dest_Free o Thm.term_of) xs, As)), ctxt') end;
wenzelm@19267
   242
wenzelm@19267
   243
in
wenzelm@19267
   244
wenzelm@19267
   245
fun pretty_statement ctxt kind raw_th =
wenzelm@19267
   246
  let
wenzelm@19267
   247
    val thy = ProofContext.theory_of ctxt;
wenzelm@20150
   248
    val cert = Thm.cterm_of thy;
wenzelm@20150
   249
wenzelm@21605
   250
    val th = MetaSimplifier.norm_hhf raw_th;
wenzelm@20150
   251
    val is_elim = ObjectLogic.is_elim th;
wenzelm@19267
   252
wenzelm@22568
   253
    val ((_, [th']), ctxt') = Variable.import_thms true [th] ctxt;
wenzelm@20150
   254
    val prop = Thm.prop_of th';
wenzelm@20150
   255
    val (prems, concl) = Logic.strip_horn prop;
wenzelm@20150
   256
    val concl_term = ObjectLogic.drop_judgment thy concl;
wenzelm@19267
   257
wenzelm@20150
   258
    val fixes = fold_aterms (fn v as Free (x, T) =>
wenzelm@20150
   259
        if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term)
wenzelm@20150
   260
        then insert (op =) (x, T) else I | _ => I) prop []
wenzelm@20150
   261
      |> rev |> map (apfst (ProofContext.revert_skolem ctxt'));
wenzelm@20150
   262
    val (assumes, cases) = take_suffix (fn prem =>
wenzelm@20150
   263
      is_elim andalso concl aconv Logic.strip_assums_concl prem) prems;
wenzelm@19267
   264
  in
wenzelm@20150
   265
    pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (x, SOME T, NoSyn)) fixes)) @
wenzelm@20150
   266
    pretty_ctxt ctxt' (Assumes (map (fn t => (("", []), [(t, [])])) assumes)) @
wenzelm@20150
   267
    pretty_stmt ctxt'
wenzelm@19585
   268
     (if null cases then Shows [(("", []), [(concl, [])])]
wenzelm@20150
   269
      else Obtains (#1 (fold_map (obtain o cert) cases ctxt')))
wenzelm@19267
   270
  end |> thm_name kind raw_th;
wenzelm@19267
   271
wenzelm@18140
   272
end;
wenzelm@19267
   273
wenzelm@19777
   274
wenzelm@19777
   275
wenzelm@19777
   276
(** logical operations **)
wenzelm@19777
   277
wenzelm@19777
   278
(* witnesses -- hypotheses as protected facts *)
wenzelm@19777
   279
wenzelm@19777
   280
datatype witness = Witness of term * thm;
wenzelm@19777
   281
wenzelm@19777
   282
fun map_witness f (Witness witn) = Witness (f witn);
wenzelm@19777
   283
wenzelm@21481
   284
fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th));
wenzelm@21481
   285
wenzelm@19777
   286
fun witness_prop (Witness (t, _)) = t;
wenzelm@19777
   287
fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
wenzelm@19777
   288
wenzelm@19777
   289
fun assume_witness thy t =
wenzelm@19777
   290
  Witness (t, Goal.protect (Thm.assume (Thm.cterm_of thy t)));
wenzelm@19777
   291
wenzelm@20058
   292
fun prove_witness ctxt t tac =
wenzelm@20058
   293
  Witness (t, Goal.prove ctxt [] [] (Logic.protect t) (fn _ =>
wenzelm@19777
   294
    Tactic.rtac Drule.protectI 1 THEN tac));
wenzelm@19777
   295
wenzelm@21605
   296
fun conclude_witness (Witness (_, th)) = MetaSimplifier.norm_hhf_protect (Goal.conclude th);
wenzelm@19777
   297
wenzelm@19777
   298
val mark_witness = Logic.protect;
wenzelm@19777
   299
wenzelm@19777
   300
fun make_witness t th = Witness (t, th);
ballarin@19931
   301
fun dest_witness (Witness w) = w;
ballarin@19931
   302
ballarin@20068
   303
fun transfer_witness thy (Witness (t, th)) = Witness (t, Thm.transfer thy th);
ballarin@20068
   304
wenzelm@19777
   305
val refine_witness =
wenzelm@19777
   306
  Proof.refine (Method.Basic (K (Method.RAW_METHOD
wenzelm@19777
   307
    (K (ALLGOALS
wenzelm@23414
   308
      (CONJUNCTS (ALLGOALS
wenzelm@23414
   309
        (CONJUNCTS (TRYALL (Tactic.rtac Drule.protectI)))))))), Position.none));
wenzelm@19777
   310
ballarin@22658
   311
fun pretty_witness ctxt witn =
wenzelm@22672
   312
  let val prt_term = Pretty.quote o ProofContext.pretty_term ctxt in
ballarin@22658
   313
    Pretty.block (prt_term (witness_prop witn) ::
ballarin@22658
   314
      (if ! show_hyps then [Pretty.brk 2, Pretty.list "[" "]"
ballarin@22658
   315
         (map prt_term (witness_hyps witn))] else []))
ballarin@22658
   316
  end;
ballarin@22658
   317
wenzelm@19777
   318
wenzelm@19777
   319
(* derived rules *)
wenzelm@19777
   320
wenzelm@20007
   321
fun instantiate_tfrees thy subst th =
wenzelm@19777
   322
  let
wenzelm@19777
   323
    val certT = Thm.ctyp_of thy;
wenzelm@20007
   324
    val idx = Thm.maxidx_of th + 1;
wenzelm@20007
   325
    fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
wenzelm@20007
   326
wenzelm@20007
   327
    fun add_inst (a, S) insts =
wenzelm@20007
   328
      if AList.defined (op =) insts a then insts
wenzelm@20007
   329
      else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
wenzelm@20007
   330
    val insts =
wenzelm@20007
   331
      Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
wenzelm@20007
   332
        (Thm.full_prop_of th) [];
wenzelm@19777
   333
  in
wenzelm@20007
   334
    th
wenzelm@20007
   335
    |> Thm.generalize (map fst insts, []) idx
wenzelm@20007
   336
    |> Thm.instantiate (map cert_inst insts, [])
wenzelm@19777
   337
  end;
wenzelm@19777
   338
wenzelm@19777
   339
fun instantiate_frees thy subst =
wenzelm@19777
   340
  let val cert = Thm.cterm_of thy in
wenzelm@19777
   341
    Drule.forall_intr_list (map (cert o Free o fst) subst) #>
wenzelm@19777
   342
    Drule.forall_elim_list (map (cert o snd) subst)
wenzelm@19777
   343
  end;
wenzelm@19777
   344
wenzelm@19777
   345
fun hyps_rule rule th =
wenzelm@21521
   346
  let val {hyps, ...} = Thm.crep_thm th in
wenzelm@19777
   347
    Drule.implies_elim_list
wenzelm@19777
   348
      (rule (Drule.implies_intr_list hyps th))
wenzelm@21521
   349
      (map (Thm.assume o Drule.cterm_rule rule) hyps)
wenzelm@19777
   350
  end;
wenzelm@19777
   351
wenzelm@19777
   352
wenzelm@19777
   353
(* rename *)
wenzelm@19777
   354
wenzelm@19777
   355
fun rename ren x =
wenzelm@19777
   356
  (case AList.lookup (op =) ren (x: string) of
wenzelm@19777
   357
    NONE => x
wenzelm@19777
   358
  | SOME (x', _) => x');
wenzelm@19777
   359
wenzelm@19777
   360
fun rename_var ren (x, mx) =
wenzelm@19777
   361
  (case (AList.lookup (op =) ren (x: string), mx) of
wenzelm@19777
   362
    (NONE, _) => (x, mx)
wenzelm@19777
   363
  | (SOME (x', NONE), Structure) => (x', mx)
wenzelm@19777
   364
  | (SOME (x', SOME _), Structure) =>
wenzelm@19777
   365
      error ("Attempt to change syntax of structure parameter " ^ quote x)
wenzelm@19777
   366
  | (SOME (x', NONE), _) => (x', NoSyn)
wenzelm@19777
   367
  | (SOME (x', SOME mx'), _) => (x', mx'));
wenzelm@19777
   368
wenzelm@19777
   369
fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
wenzelm@19777
   370
  | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
wenzelm@19777
   371
  | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
wenzelm@19777
   372
  | rename_term _ a = a;
wenzelm@19777
   373
wenzelm@19777
   374
fun rename_thm ren th =
wenzelm@19777
   375
  let
wenzelm@20304
   376
    val thy = Thm.theory_of_thm th;
wenzelm@22691
   377
    val subst = (Thm.fold_terms o Term.fold_aterms)
wenzelm@20304
   378
      (fn Free (x, T) =>
wenzelm@19777
   379
        let val x' = rename ren x
wenzelm@20304
   380
        in if x = x' then I else insert (eq_fst (op =)) ((x, T), Free (x', T)) end
wenzelm@20304
   381
      | _ => I) th [];
wenzelm@19777
   382
  in
wenzelm@19777
   383
    if null subst then th
wenzelm@20304
   384
    else th |> hyps_rule (instantiate_frees thy subst)
wenzelm@19777
   385
  end;
wenzelm@19777
   386
wenzelm@21481
   387
fun rename_morphism ren = Morphism.morphism
wenzelm@21521
   388
  {name = I, var = rename_var ren, typ = I, term = rename_term ren, fact = map (rename_thm ren)};
wenzelm@19777
   389
wenzelm@19777
   390
wenzelm@19777
   391
(* instantiate types *)
wenzelm@19777
   392
wenzelm@19777
   393
fun instT_type env =
wenzelm@19777
   394
  if Symtab.is_empty env then I
wenzelm@19777
   395
  else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
wenzelm@19777
   396
wenzelm@19777
   397
fun instT_term env =
wenzelm@19777
   398
  if Symtab.is_empty env then I
wenzelm@20548
   399
  else Term.map_types (instT_type env);
wenzelm@19777
   400
wenzelm@22691
   401
fun instT_subst env th = (Thm.fold_terms o Term.fold_types o Term.fold_atyps)
wenzelm@20304
   402
  (fn T as TFree (a, _) =>
wenzelm@20304
   403
    let val T' = the_default T (Symtab.lookup env a)
wenzelm@20304
   404
    in if T = T' then I else insert (op =) (a, T') end
wenzelm@20304
   405
  | _ => I) th [];
wenzelm@19777
   406
wenzelm@19777
   407
fun instT_thm thy env th =
wenzelm@19777
   408
  if Symtab.is_empty env then th
wenzelm@19777
   409
  else
wenzelm@19777
   410
    let val subst = instT_subst env th
wenzelm@19777
   411
    in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
wenzelm@19777
   412
wenzelm@22672
   413
fun instT_morphism thy env =
wenzelm@22672
   414
  let val thy_ref = Theory.self_ref thy in
wenzelm@22672
   415
    Morphism.morphism
wenzelm@22672
   416
     {name = I, var = I,
wenzelm@22672
   417
      typ = instT_type env,
wenzelm@22672
   418
      term = instT_term env,
wenzelm@22672
   419
      fact = map (fn th => instT_thm (Theory.deref thy_ref) env th)}
wenzelm@22672
   420
  end;
wenzelm@19777
   421
wenzelm@19777
   422
wenzelm@19777
   423
(* instantiate types and terms *)
wenzelm@19777
   424
wenzelm@19777
   425
fun inst_term (envT, env) =
wenzelm@19777
   426
  if Symtab.is_empty env then instT_term envT
wenzelm@19777
   427
  else
wenzelm@19777
   428
    let
wenzelm@19777
   429
      val instT = instT_type envT;
wenzelm@19777
   430
      fun inst (Const (x, T)) = Const (x, instT T)
wenzelm@19777
   431
        | inst (Free (x, T)) =
wenzelm@19777
   432
            (case Symtab.lookup env x of
wenzelm@19777
   433
              NONE => Free (x, instT T)
wenzelm@19777
   434
            | SOME t => t)
wenzelm@19777
   435
        | inst (Var (xi, T)) = Var (xi, instT T)
wenzelm@19777
   436
        | inst (b as Bound _) = b
wenzelm@19777
   437
        | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
wenzelm@19777
   438
        | inst (t $ u) = inst t $ inst u;
wenzelm@19777
   439
    in Envir.beta_norm o inst end;
wenzelm@19777
   440
wenzelm@19777
   441
fun inst_thm thy (envT, env) th =
wenzelm@19777
   442
  if Symtab.is_empty env then instT_thm thy envT th
wenzelm@19777
   443
  else
wenzelm@19777
   444
    let
wenzelm@19777
   445
      val substT = instT_subst envT th;
wenzelm@22691
   446
      val subst = (Thm.fold_terms o Term.fold_aterms)
wenzelm@20304
   447
       (fn Free (x, T) =>
wenzelm@19777
   448
          let
wenzelm@19777
   449
            val T' = instT_type envT T;
wenzelm@19777
   450
            val t = Free (x, T');
wenzelm@19777
   451
            val t' = the_default t (Symtab.lookup env x);
wenzelm@20304
   452
          in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
wenzelm@20304
   453
       | _ => I) th [];
wenzelm@19777
   454
    in
wenzelm@19777
   455
      if null substT andalso null subst then th
wenzelm@19777
   456
      else th |> hyps_rule
wenzelm@19777
   457
       (instantiate_tfrees thy substT #>
wenzelm@19777
   458
        instantiate_frees thy subst #>
wenzelm@22900
   459
        Conv.fconv_rule (Thm.beta_conversion true))
wenzelm@19777
   460
    end;
wenzelm@19777
   461
wenzelm@22672
   462
fun inst_morphism thy envs =
wenzelm@22672
   463
  let val thy_ref = Theory.self_ref thy in
wenzelm@22672
   464
    Morphism.morphism
wenzelm@22672
   465
     {name = I, var = I,
wenzelm@22672
   466
      typ = instT_type (#1 envs),
wenzelm@22672
   467
      term = inst_term envs,
wenzelm@22672
   468
      fact = map (fn th => inst_thm (Theory.deref thy_ref) envs th)}
wenzelm@22672
   469
  end;
wenzelm@19777
   470
wenzelm@19777
   471
wenzelm@19777
   472
(* satisfy hypotheses *)
wenzelm@19777
   473
wenzelm@19777
   474
fun satisfy_thm witns thm = thm |> fold (fn hyp =>
wenzelm@19777
   475
    (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
wenzelm@19777
   476
      NONE => I
wenzelm@19777
   477
    | SOME (Witness (_, th)) => Drule.implies_intr_protected [hyp] #> Goal.comp_hhf th))
wenzelm@19777
   478
  (#hyps (Thm.crep_thm thm));
wenzelm@19777
   479
wenzelm@21497
   480
fun satisfy_morphism witns = Morphism.thm_morphism (satisfy_thm witns);
wenzelm@19843
   481
wenzelm@21581
   482
fun satisfy_facts witns = facts_map (morph_ctxt (satisfy_morphism witns));
wenzelm@20264
   483
wenzelm@20264
   484
wenzelm@20264
   485
(* generalize type/term parameters *)
wenzelm@20264
   486
wenzelm@20264
   487
val maxidx_atts = fold Args.maxidx_values;
wenzelm@20264
   488
wenzelm@21581
   489
fun generalize_facts inner outer facts =
wenzelm@20264
   490
  let
wenzelm@20264
   491
    val thy = ProofContext.theory_of inner;
wenzelm@20264
   492
    val maxidx =
wenzelm@20264
   493
      fold (fn ((_, atts), bs) => maxidx_atts atts #> fold (maxidx_atts o #2) bs) facts ~1;
wenzelm@21581
   494
    val exp_fact = map (Thm.adjust_maxidx_thm maxidx) #> Variable.export inner outer;
wenzelm@21521
   495
    val exp_term = Drule.term_rule thy (singleton exp_fact);
wenzelm@21521
   496
    val exp_typ = Logic.type_map exp_term;
wenzelm@21521
   497
    val morphism =
wenzelm@21521
   498
      Morphism.morphism {name = I, var = I, typ = exp_typ, term = exp_term, fact = exp_fact};
wenzelm@21581
   499
  in facts_map (morph_ctxt morphism) facts end;
wenzelm@20886
   500
wenzelm@19267
   501
end;