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