src/Pure/Isar/element.ML
author haftmann
Mon Nov 29 11:38:59 2010 +0100 (2010-11-29 ago)
changeset 40803 3f66ea311d44
parent 39557 fe5722fce758
child 41228 e1fce873b814
permissions -rw-r--r--
tuned
wenzelm@18140
     1
(*  Title:      Pure/Isar/element.ML
wenzelm@18140
     2
    Author:     Makarius
wenzelm@18140
     3
wenzelm@19777
     4
Explicit data structures for some Isar language elements, with derived
wenzelm@19777
     5
logical operations.
wenzelm@18140
     6
*)
wenzelm@18140
     7
wenzelm@18140
     8
signature ELEMENT =
wenzelm@18140
     9
sig
wenzelm@19259
    10
  datatype ('typ, 'term) stmt =
wenzelm@28084
    11
    Shows of (Attrib.binding * ('term * 'term list) list) list |
haftmann@29578
    12
    Obtains of (binding * ((binding * 'typ option) list * 'term list)) list
wenzelm@26336
    13
  type statement = (string, string) stmt
wenzelm@26336
    14
  type statement_i = (typ, term) stmt
wenzelm@18140
    15
  datatype ('typ, 'term, 'fact) ctxt =
haftmann@29578
    16
    Fixes of (binding * 'typ option * mixfix) list |
wenzelm@18140
    17
    Constrains of (string * 'typ) list |
wenzelm@28084
    18
    Assumes of (Attrib.binding * ('term * 'term list) list) list |
wenzelm@28084
    19
    Defines of (Attrib.binding * ('term * 'term list)) list |
wenzelm@28084
    20
    Notes of string * (Attrib.binding * ('fact * Attrib.src list) list) list
wenzelm@26336
    21
  type context = (string, string, Facts.ref) ctxt
wenzelm@26336
    22
  type context_i = (typ, term, thm list) ctxt
wenzelm@21581
    23
  val facts_map: (('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt) ->
wenzelm@28084
    24
   (Attrib.binding * ('fact * Attrib.src list) list) list ->
wenzelm@28084
    25
   (Attrib.binding * ('c * Attrib.src list) list) list
wenzelm@29603
    26
  val map_ctxt: {binding: binding -> binding, typ: 'typ -> 'a, term: 'term -> 'b,
wenzelm@29603
    27
    pattern: 'term -> 'b, fact: 'fact -> 'c, attrib: Attrib.src -> Attrib.src} ->
wenzelm@29603
    28
    ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
wenzelm@21528
    29
  val map_ctxt_attrib: (Attrib.src -> Attrib.src) ->
wenzelm@21528
    30
    ('typ, 'term, 'fact) ctxt -> ('typ, 'term, 'fact) ctxt
wenzelm@21481
    31
  val morph_ctxt: morphism -> context_i -> context_i
wenzelm@19777
    32
  val pretty_stmt: Proof.context -> statement_i -> Pretty.T list
wenzelm@19777
    33
  val pretty_ctxt: Proof.context -> context_i -> Pretty.T list
wenzelm@19777
    34
  val pretty_statement: Proof.context -> string -> thm -> Pretty.T
wenzelm@19777
    35
  type witness
haftmann@29578
    36
  val prove_witness: Proof.context -> term -> tactic -> witness
haftmann@29578
    37
  val witness_proof: (witness list list -> Proof.context -> Proof.context) ->
haftmann@29578
    38
    term list list -> Proof.context -> Proof.state
haftmann@29578
    39
  val witness_proof_eqs: (witness list list -> thm list -> Proof.context -> Proof.context) ->
haftmann@29578
    40
    term list list -> term list -> Proof.context -> Proof.state
haftmann@29578
    41
  val witness_local_proof: (witness list list -> Proof.state -> Proof.state) ->
haftmann@29578
    42
    string -> term list list -> Proof.context -> bool -> Proof.state -> Proof.state
ballarin@38108
    43
  val witness_local_proof_eqs: (witness list list -> thm list -> Proof.state -> Proof.state) ->
ballarin@38108
    44
    string -> term list list -> term list -> Proof.context -> bool -> Proof.state ->
ballarin@38108
    45
    Proof.state
wenzelm@21481
    46
  val morph_witness: morphism -> witness -> witness
wenzelm@19777
    47
  val conclude_witness: witness -> thm
ballarin@22658
    48
  val pretty_witness: Proof.context -> witness -> Pretty.T
wenzelm@18140
    49
  val instT_type: typ Symtab.table -> typ -> typ
wenzelm@18140
    50
  val instT_term: typ Symtab.table -> term -> term
wenzelm@18140
    51
  val instT_thm: theory -> typ Symtab.table -> thm -> thm
wenzelm@21481
    52
  val instT_morphism: theory -> typ Symtab.table -> morphism
wenzelm@18140
    53
  val inst_term: typ Symtab.table * term Symtab.table -> term -> term
wenzelm@18140
    54
  val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
wenzelm@21481
    55
  val inst_morphism: theory -> typ Symtab.table * term Symtab.table -> morphism
wenzelm@19777
    56
  val satisfy_thm: witness list -> thm -> thm
wenzelm@21481
    57
  val satisfy_morphism: witness list -> morphism
wenzelm@20264
    58
  val satisfy_facts: witness list ->
wenzelm@28084
    59
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28084
    60
    (Attrib.binding * (thm list * Attrib.src list) list) list
haftmann@36674
    61
  val eq_morphism: theory -> thm list -> morphism option
ballarin@29218
    62
  val transfer_morphism: theory -> morphism
ballarin@38108
    63
  val generic_note_thmss: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
ballarin@38108
    64
    Context.generic -> (string * thm list) list * Context.generic
wenzelm@30775
    65
  val init: context_i -> Context.generic -> Context.generic
wenzelm@30777
    66
  val activate_i: context_i -> Proof.context -> context_i * Proof.context
wenzelm@30777
    67
  val activate: (typ, term, Facts.ref) ctxt -> Proof.context -> context_i * Proof.context
wenzelm@18140
    68
end;
wenzelm@18140
    69
wenzelm@18140
    70
structure Element: ELEMENT =
wenzelm@18140
    71
struct
wenzelm@18140
    72
wenzelm@19777
    73
(** language elements **)
wenzelm@19777
    74
wenzelm@19777
    75
(* statement *)
wenzelm@19259
    76
wenzelm@19259
    77
datatype ('typ, 'term) stmt =
wenzelm@28084
    78
  Shows of (Attrib.binding * ('term * 'term list) list) list |
haftmann@29578
    79
  Obtains of (binding * ((binding * 'typ option) list * 'term list)) list;
wenzelm@19259
    80
wenzelm@19259
    81
type statement = (string, string) stmt;
wenzelm@19259
    82
type statement_i = (typ, term) stmt;
wenzelm@19259
    83
wenzelm@19259
    84
wenzelm@19777
    85
(* context *)
wenzelm@18140
    86
wenzelm@18140
    87
datatype ('typ, 'term, 'fact) ctxt =
haftmann@29578
    88
  Fixes of (binding * 'typ option * mixfix) list |
wenzelm@18140
    89
  Constrains of (string * 'typ) list |
wenzelm@28084
    90
  Assumes of (Attrib.binding * ('term * 'term list) list) list |
wenzelm@28084
    91
  Defines of (Attrib.binding * ('term * 'term list)) list |
wenzelm@28084
    92
  Notes of string * (Attrib.binding * ('fact * Attrib.src list) list) list;
wenzelm@18140
    93
wenzelm@26336
    94
type context = (string, string, Facts.ref) ctxt;
wenzelm@18140
    95
type context_i = (typ, term, thm list) ctxt;
wenzelm@18140
    96
wenzelm@21581
    97
fun facts_map f facts = Notes ("", facts) |> f |> (fn Notes (_, facts') => facts');
wenzelm@21581
    98
wenzelm@29603
    99
fun map_ctxt {binding, typ, term, pattern, fact, attrib} =
wenzelm@29603
   100
  fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) => (binding x, Option.map typ T, mx)))
wenzelm@28079
   101
   | Constrains xs => Constrains (xs |> map (fn (x, T) =>
wenzelm@30585
   102
      (Name.of_binding (binding (Binding.name x)), typ T)))
wenzelm@18140
   103
   | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
wenzelm@29603
   104
      ((binding a, map attrib atts), propps |> map (fn (t, ps) => (term t, map pattern ps)))))
wenzelm@18140
   105
   | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
wenzelm@29603
   106
      ((binding a, map attrib atts), (term t, map pattern ps))))
wenzelm@21440
   107
   | Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) =>
haftmann@28965
   108
      ((binding a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
wenzelm@18140
   109
wenzelm@21528
   110
fun map_ctxt_attrib attrib =
wenzelm@29603
   111
  map_ctxt {binding = I, typ = I, term = I, pattern = I, fact = I, attrib = attrib};
wenzelm@21528
   112
wenzelm@21481
   113
fun morph_ctxt phi = map_ctxt
haftmann@28965
   114
 {binding = Morphism.binding phi,
wenzelm@21481
   115
  typ = Morphism.typ phi,
wenzelm@21481
   116
  term = Morphism.term phi,
wenzelm@29603
   117
  pattern = Morphism.term phi,
wenzelm@21521
   118
  fact = Morphism.fact phi,
wenzelm@21481
   119
  attrib = Args.morph_values phi};
wenzelm@18140
   120
wenzelm@19808
   121
wenzelm@18894
   122
wenzelm@19259
   123
(** pretty printing **)
wenzelm@19259
   124
wenzelm@19267
   125
fun pretty_items _ _ [] = []
wenzelm@19267
   126
  | pretty_items keyword sep (x :: ys) =
wenzelm@19267
   127
      Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] ::
wenzelm@19267
   128
        map (fn y => Pretty.block [Pretty.str "  ", Pretty.keyword sep, Pretty.brk 1, y]) ys;
wenzelm@19259
   129
haftmann@28862
   130
fun pretty_name_atts ctxt (b, atts) sep =
wenzelm@30219
   131
  if Binding.is_empty b andalso null atts then []
wenzelm@30219
   132
  else [Pretty.block (Pretty.breaks
wenzelm@30219
   133
    (Pretty.str (Binding.str_of b) :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))];
wenzelm@19259
   134
wenzelm@19259
   135
wenzelm@19259
   136
(* pretty_stmt *)
wenzelm@19259
   137
wenzelm@19259
   138
fun pretty_stmt ctxt =
wenzelm@19259
   139
  let
wenzelm@24920
   140
    val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt;
wenzelm@24920
   141
    val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
wenzelm@19267
   142
    val prt_terms = separate (Pretty.keyword "and") o map prt_term;
wenzelm@19259
   143
    val prt_name_atts = pretty_name_atts ctxt;
wenzelm@19259
   144
wenzelm@19259
   145
    fun prt_show (a, ts) =
wenzelm@19267
   146
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts)));
wenzelm@19259
   147
wenzelm@28079
   148
    fun prt_var (x, SOME T) = Pretty.block
wenzelm@30223
   149
          [Pretty.str (Binding.name_of x ^ " ::"), Pretty.brk 1, prt_typ T]
wenzelm@30223
   150
      | prt_var (x, NONE) = Pretty.str (Binding.name_of x);
wenzelm@26721
   151
    val prt_vars = separate (Pretty.keyword "and") o map prt_var;
wenzelm@19259
   152
wenzelm@19267
   153
    fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts))
wenzelm@19259
   154
      | prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks
wenzelm@19585
   155
          (prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts));
wenzelm@19259
   156
  in
wenzelm@19267
   157
    fn Shows shows => pretty_items "shows" "and" (map prt_show shows)
wenzelm@19267
   158
     | Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains)
wenzelm@19259
   159
  end;
wenzelm@19259
   160
wenzelm@18894
   161
wenzelm@19259
   162
(* pretty_ctxt *)
wenzelm@19259
   163
wenzelm@19259
   164
fun pretty_ctxt ctxt =
wenzelm@19259
   165
  let
wenzelm@24920
   166
    val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt;
wenzelm@24920
   167
    val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
wenzelm@32091
   168
    val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
wenzelm@19259
   169
    val prt_name_atts = pretty_name_atts ctxt;
wenzelm@19259
   170
wenzelm@19267
   171
    fun prt_mixfix NoSyn = []
wenzelm@19267
   172
      | prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx];
wenzelm@19267
   173
wenzelm@30223
   174
    fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (Binding.name_of x ^ " ::") ::
wenzelm@28079
   175
          Pretty.brk 1 :: prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
wenzelm@30223
   176
      | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str (Binding.name_of x) ::
wenzelm@28079
   177
          Pretty.brk 1 :: prt_mixfix mx);
haftmann@28965
   178
    fun prt_constrain (x, T) = prt_fix (Binding.name x, SOME T, NoSyn);
wenzelm@18894
   179
wenzelm@19259
   180
    fun prt_asm (a, ts) =
wenzelm@19259
   181
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
wenzelm@19259
   182
    fun prt_def (a, (t, _)) =
wenzelm@19259
   183
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
wenzelm@19259
   184
wenzelm@19259
   185
    fun prt_fact (ths, []) = map prt_thm ths
wenzelm@19259
   186
      | prt_fact (ths, atts) = Pretty.enclose "(" ")"
wenzelm@21032
   187
          (Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts;
wenzelm@19259
   188
    fun prt_note (a, ths) =
wenzelm@19482
   189
      Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths)));
wenzelm@19259
   190
  in
wenzelm@19267
   191
    fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes)
wenzelm@19267
   192
     | Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs)
wenzelm@19267
   193
     | Assumes asms => pretty_items "assumes" "and" (map prt_asm asms)
wenzelm@19267
   194
     | Defines defs => pretty_items "defines" "and" (map prt_def defs)
wenzelm@21440
   195
     | Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts)
wenzelm@21440
   196
     | Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts)
wenzelm@19259
   197
  end;
wenzelm@18894
   198
wenzelm@19267
   199
wenzelm@19267
   200
(* pretty_statement *)
wenzelm@19267
   201
wenzelm@19267
   202
local
wenzelm@19267
   203
wenzelm@19267
   204
fun thm_name kind th prts =
wenzelm@19267
   205
  let val head =
wenzelm@27865
   206
    if Thm.has_name_hint th then
wenzelm@21965
   207
      Pretty.block [Pretty.command kind,
wenzelm@30364
   208
        Pretty.brk 1, Pretty.str (Long_Name.base_name (Thm.get_name_hint th) ^ ":")]
wenzelm@21965
   209
    else Pretty.command kind
wenzelm@19267
   210
  in Pretty.block (Pretty.fbreaks (head :: prts)) end;
wenzelm@19267
   211
haftmann@28965
   212
fun fix (x, T) = (Binding.name x, SOME T);
wenzelm@28079
   213
wenzelm@19267
   214
fun obtain prop ctxt =
wenzelm@19267
   215
  let
wenzelm@20150
   216
    val ((xs, prop'), ctxt') = Variable.focus prop ctxt;
wenzelm@20150
   217
    val As = Logic.strip_imp_prems (Thm.term_of prop');
wenzelm@32199
   218
  in ((Binding.empty, (map (fix o Term.dest_Free o Thm.term_of o #2) xs, As)), ctxt') end;
wenzelm@19267
   219
wenzelm@19267
   220
in
wenzelm@19267
   221
wenzelm@19267
   222
fun pretty_statement ctxt kind raw_th =
wenzelm@19267
   223
  let
wenzelm@19267
   224
    val thy = ProofContext.theory_of ctxt;
wenzelm@20150
   225
    val cert = Thm.cterm_of thy;
wenzelm@20150
   226
wenzelm@21605
   227
    val th = MetaSimplifier.norm_hhf raw_th;
wenzelm@35625
   228
    val is_elim = Object_Logic.is_elim th;
wenzelm@19267
   229
wenzelm@31794
   230
    val ((_, [th']), ctxt') = Variable.import true [th] (Variable.set_body false ctxt);
wenzelm@20150
   231
    val prop = Thm.prop_of th';
wenzelm@20150
   232
    val (prems, concl) = Logic.strip_horn prop;
wenzelm@35625
   233
    val concl_term = Object_Logic.drop_judgment thy concl;
wenzelm@19267
   234
wenzelm@20150
   235
    val fixes = fold_aterms (fn v as Free (x, T) =>
wenzelm@20150
   236
        if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term)
wenzelm@26716
   237
        then insert (op =) (x, T) else I | _ => I) prop [] |> rev;
wenzelm@20150
   238
    val (assumes, cases) = take_suffix (fn prem =>
wenzelm@20150
   239
      is_elim andalso concl aconv Logic.strip_assums_concl prem) prems;
wenzelm@19267
   240
  in
haftmann@28965
   241
    pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) fixes)) @
haftmann@28965
   242
    pretty_ctxt ctxt' (Assumes (map (fn t => (Attrib.empty_binding, [(t, [])])) assumes)) @
haftmann@28965
   243
     (if null cases then pretty_stmt ctxt' (Shows [(Attrib.empty_binding, [(concl, [])])])
wenzelm@26716
   244
      else
wenzelm@26716
   245
        let val (clauses, ctxt'') = fold_map (obtain o cert) cases ctxt'
wenzelm@26716
   246
        in pretty_stmt ctxt'' (Obtains clauses) end)
wenzelm@19267
   247
  end |> thm_name kind raw_th;
wenzelm@19267
   248
wenzelm@18140
   249
end;
wenzelm@19267
   250
wenzelm@19777
   251
wenzelm@19777
   252
wenzelm@19777
   253
(** logical operations **)
wenzelm@19777
   254
wenzelm@19777
   255
(* witnesses -- hypotheses as protected facts *)
wenzelm@19777
   256
wenzelm@19777
   257
datatype witness = Witness of term * thm;
wenzelm@19777
   258
haftmann@29578
   259
val mark_witness = Logic.protect;
haftmann@29578
   260
fun witness_prop (Witness (t, _)) = t;
haftmann@29578
   261
fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
wenzelm@19777
   262
fun map_witness f (Witness witn) = Witness (f witn);
wenzelm@19777
   263
wenzelm@21481
   264
fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th));
wenzelm@21481
   265
wenzelm@20058
   266
fun prove_witness ctxt t tac =
haftmann@29578
   267
  Witness (t, Thm.close_derivation (Goal.prove ctxt [] [] (mark_witness t) (fn _ =>
wenzelm@25202
   268
    Tactic.rtac Drule.protectI 1 THEN tac)));
wenzelm@19777
   269
wenzelm@29603
   270
haftmann@29578
   271
local
haftmann@29578
   272
haftmann@29578
   273
val refine_witness =
wenzelm@30510
   274
  Proof.refine (Method.Basic (K (RAW_METHOD
haftmann@29578
   275
    (K (ALLGOALS
haftmann@29578
   276
      (CONJUNCTS (ALLGOALS
wenzelm@32194
   277
        (CONJUNCTS (TRYALL (Tactic.rtac Drule.protectI))))))))));
wenzelm@25624
   278
haftmann@29578
   279
fun gen_witness_proof proof after_qed wit_propss eq_props =
haftmann@29578
   280
  let
haftmann@29578
   281
    val propss = (map o map) (fn prop => (mark_witness prop, [])) wit_propss
haftmann@29578
   282
      @ [map (rpair []) eq_props];
haftmann@29578
   283
    fun after_qed' thmss =
wenzelm@29603
   284
      let val (wits, eqs) = split_last ((map o map) Thm.close_derivation thmss);
haftmann@29578
   285
      in after_qed ((map2 o map2) (curry Witness) wit_propss wits) eqs end;
haftmann@29578
   286
  in proof after_qed' propss #> refine_witness #> Seq.hd end;
haftmann@29578
   287
ballarin@38108
   288
fun proof_local cmd goal_ctxt int after_qed' propss =
ballarin@38108
   289
    Proof.map_context (K goal_ctxt)
ballarin@38108
   290
    #> Proof.local_goal (Proof_Display.print_results int) (K I) ProofContext.bind_propp_i
ballarin@38108
   291
      cmd NONE after_qed' (map (pair Thm.empty_binding) propss);
haftmann@29578
   292
in
haftmann@29578
   293
haftmann@29578
   294
fun witness_proof after_qed wit_propss =
wenzelm@36323
   295
  gen_witness_proof (Proof.theorem NONE) (fn wits => fn _ => after_qed wits)
haftmann@29578
   296
    wit_propss [];
haftmann@29578
   297
wenzelm@36323
   298
val witness_proof_eqs = gen_witness_proof (Proof.theorem NONE);
haftmann@29578
   299
haftmann@29578
   300
fun witness_local_proof after_qed cmd wit_propss goal_ctxt int =
ballarin@38108
   301
  gen_witness_proof (proof_local cmd goal_ctxt int)
haftmann@29578
   302
    (fn wits => fn _ => after_qed wits) wit_propss [];
haftmann@29578
   303
ballarin@38108
   304
fun witness_local_proof_eqs after_qed cmd wit_propss eq_props goal_ctxt int =
ballarin@38108
   305
  gen_witness_proof (proof_local cmd goal_ctxt int) after_qed wit_propss eq_props;
wenzelm@29603
   306
end;
wenzelm@29603
   307
wenzelm@19777
   308
wenzelm@25302
   309
fun compose_witness (Witness (_, th)) r =
wenzelm@25302
   310
  let
wenzelm@25302
   311
    val th' = Goal.conclude th;
wenzelm@25302
   312
    val A = Thm.cprem_of r 1;
ballarin@25739
   313
  in
ballarin@25739
   314
    Thm.implies_elim
ballarin@25739
   315
      (Conv.gconv_rule Drule.beta_eta_conversion 1 r)
ballarin@25739
   316
      (Conv.fconv_rule Drule.beta_eta_conversion
ballarin@25739
   317
        (Thm.instantiate (Thm.match (Thm.cprop_of th', A)) th'))
ballarin@25739
   318
  end;
wenzelm@25302
   319
haftmann@29578
   320
fun conclude_witness (Witness (_, th)) =
haftmann@29578
   321
  Thm.close_derivation (MetaSimplifier.norm_hhf_protect (Goal.conclude th));
wenzelm@19777
   322
ballarin@22658
   323
fun pretty_witness ctxt witn =
wenzelm@24920
   324
  let val prt_term = Pretty.quote o Syntax.pretty_term ctxt in
ballarin@22658
   325
    Pretty.block (prt_term (witness_prop witn) ::
wenzelm@39166
   326
      (if Config.get ctxt show_hyps then [Pretty.brk 2, Pretty.list "[" "]"
ballarin@22658
   327
         (map prt_term (witness_hyps witn))] else []))
ballarin@22658
   328
  end;
ballarin@22658
   329
wenzelm@19777
   330
wenzelm@19777
   331
(* derived rules *)
wenzelm@19777
   332
wenzelm@20007
   333
fun instantiate_tfrees thy subst th =
wenzelm@19777
   334
  let
wenzelm@19777
   335
    val certT = Thm.ctyp_of thy;
wenzelm@20007
   336
    val idx = Thm.maxidx_of th + 1;
wenzelm@20007
   337
    fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
wenzelm@20007
   338
wenzelm@20007
   339
    fun add_inst (a, S) insts =
wenzelm@20007
   340
      if AList.defined (op =) insts a then insts
wenzelm@20007
   341
      else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
wenzelm@20007
   342
    val insts =
wenzelm@20007
   343
      Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
wenzelm@20007
   344
        (Thm.full_prop_of th) [];
wenzelm@19777
   345
  in
wenzelm@20007
   346
    th
wenzelm@20007
   347
    |> Thm.generalize (map fst insts, []) idx
wenzelm@20007
   348
    |> Thm.instantiate (map cert_inst insts, [])
wenzelm@19777
   349
  end;
wenzelm@19777
   350
wenzelm@19777
   351
fun instantiate_frees thy subst =
wenzelm@19777
   352
  let val cert = Thm.cterm_of thy in
wenzelm@19777
   353
    Drule.forall_intr_list (map (cert o Free o fst) subst) #>
wenzelm@19777
   354
    Drule.forall_elim_list (map (cert o snd) subst)
wenzelm@19777
   355
  end;
wenzelm@19777
   356
wenzelm@19777
   357
fun hyps_rule rule th =
wenzelm@21521
   358
  let val {hyps, ...} = Thm.crep_thm th in
wenzelm@19777
   359
    Drule.implies_elim_list
wenzelm@19777
   360
      (rule (Drule.implies_intr_list hyps th))
wenzelm@21521
   361
      (map (Thm.assume o Drule.cterm_rule rule) hyps)
wenzelm@19777
   362
  end;
wenzelm@19777
   363
wenzelm@19777
   364
wenzelm@19777
   365
(* instantiate types *)
wenzelm@19777
   366
wenzelm@19777
   367
fun instT_type env =
wenzelm@19777
   368
  if Symtab.is_empty env then I
wenzelm@19777
   369
  else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
wenzelm@19777
   370
wenzelm@19777
   371
fun instT_term env =
wenzelm@19777
   372
  if Symtab.is_empty env then I
wenzelm@20548
   373
  else Term.map_types (instT_type env);
wenzelm@19777
   374
wenzelm@22691
   375
fun instT_subst env th = (Thm.fold_terms o Term.fold_types o Term.fold_atyps)
wenzelm@20304
   376
  (fn T as TFree (a, _) =>
wenzelm@20304
   377
    let val T' = the_default T (Symtab.lookup env a)
wenzelm@20304
   378
    in if T = T' then I else insert (op =) (a, T') end
wenzelm@20304
   379
  | _ => I) th [];
wenzelm@19777
   380
wenzelm@19777
   381
fun instT_thm thy env th =
wenzelm@19777
   382
  if Symtab.is_empty env then th
wenzelm@19777
   383
  else
wenzelm@19777
   384
    let val subst = instT_subst env th
wenzelm@19777
   385
    in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
wenzelm@19777
   386
wenzelm@22672
   387
fun instT_morphism thy env =
wenzelm@24137
   388
  let val thy_ref = Theory.check_thy thy in
wenzelm@22672
   389
    Morphism.morphism
wenzelm@29603
   390
     {binding = I,
wenzelm@22672
   391
      typ = instT_type env,
wenzelm@22672
   392
      term = instT_term env,
wenzelm@22672
   393
      fact = map (fn th => instT_thm (Theory.deref thy_ref) env th)}
wenzelm@22672
   394
  end;
wenzelm@19777
   395
wenzelm@19777
   396
wenzelm@19777
   397
(* instantiate types and terms *)
wenzelm@19777
   398
wenzelm@19777
   399
fun inst_term (envT, env) =
wenzelm@19777
   400
  if Symtab.is_empty env then instT_term envT
wenzelm@19777
   401
  else
wenzelm@19777
   402
    let
wenzelm@19777
   403
      val instT = instT_type envT;
wenzelm@19777
   404
      fun inst (Const (x, T)) = Const (x, instT T)
wenzelm@19777
   405
        | inst (Free (x, T)) =
wenzelm@19777
   406
            (case Symtab.lookup env x of
wenzelm@19777
   407
              NONE => Free (x, instT T)
wenzelm@19777
   408
            | SOME t => t)
wenzelm@19777
   409
        | inst (Var (xi, T)) = Var (xi, instT T)
wenzelm@19777
   410
        | inst (b as Bound _) = b
wenzelm@19777
   411
        | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
wenzelm@19777
   412
        | inst (t $ u) = inst t $ inst u;
wenzelm@19777
   413
    in Envir.beta_norm o inst end;
wenzelm@19777
   414
wenzelm@19777
   415
fun inst_thm thy (envT, env) th =
wenzelm@19777
   416
  if Symtab.is_empty env then instT_thm thy envT th
wenzelm@19777
   417
  else
wenzelm@19777
   418
    let
wenzelm@19777
   419
      val substT = instT_subst envT th;
wenzelm@22691
   420
      val subst = (Thm.fold_terms o Term.fold_aterms)
wenzelm@20304
   421
       (fn Free (x, T) =>
wenzelm@19777
   422
          let
wenzelm@19777
   423
            val T' = instT_type envT T;
wenzelm@19777
   424
            val t = Free (x, T');
wenzelm@19777
   425
            val t' = the_default t (Symtab.lookup env x);
wenzelm@20304
   426
          in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
wenzelm@20304
   427
       | _ => I) th [];
wenzelm@19777
   428
    in
wenzelm@19777
   429
      if null substT andalso null subst then th
wenzelm@19777
   430
      else th |> hyps_rule
wenzelm@19777
   431
       (instantiate_tfrees thy substT #>
wenzelm@19777
   432
        instantiate_frees thy subst #>
wenzelm@22900
   433
        Conv.fconv_rule (Thm.beta_conversion true))
wenzelm@19777
   434
    end;
wenzelm@19777
   435
wenzelm@22672
   436
fun inst_morphism thy envs =
wenzelm@24137
   437
  let val thy_ref = Theory.check_thy thy in
wenzelm@22672
   438
    Morphism.morphism
wenzelm@29603
   439
     {binding = I,
wenzelm@22672
   440
      typ = instT_type (#1 envs),
wenzelm@22672
   441
      term = inst_term envs,
wenzelm@22672
   442
      fact = map (fn th => inst_thm (Theory.deref thy_ref) envs th)}
wenzelm@22672
   443
  end;
wenzelm@19777
   444
wenzelm@19777
   445
wenzelm@19777
   446
(* satisfy hypotheses *)
wenzelm@19777
   447
wenzelm@19777
   448
fun satisfy_thm witns thm = thm |> fold (fn hyp =>
wenzelm@19777
   449
    (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
wenzelm@19777
   450
      NONE => I
wenzelm@25302
   451
    | SOME w => Thm.implies_intr hyp #> compose_witness w)) (#hyps (Thm.crep_thm thm));
wenzelm@19777
   452
wenzelm@29603
   453
val satisfy_morphism = Morphism.thm_morphism o satisfy_thm;
wenzelm@29603
   454
val satisfy_facts = facts_map o morph_ctxt o satisfy_morphism;
wenzelm@20264
   455
wenzelm@20264
   456
haftmann@29525
   457
(* rewriting with equalities *)
haftmann@29525
   458
haftmann@36674
   459
fun eq_morphism thy thms = if null thms then NONE else SOME (Morphism.morphism
wenzelm@29603
   460
 {binding = I,
wenzelm@29603
   461
  typ = I,
haftmann@29525
   462
  term = MetaSimplifier.rewrite_term thy thms [],
haftmann@36674
   463
  fact = map (MetaSimplifier.rewrite_rule thms)});
haftmann@29525
   464
haftmann@29525
   465
ballarin@29218
   466
(* transfer to theory using closure *)
ballarin@29218
   467
ballarin@29218
   468
fun transfer_morphism thy =
wenzelm@29603
   469
  let val thy_ref = Theory.check_thy thy
wenzelm@38709
   470
  in Morphism.thm_morphism (fn th => Thm.transfer (Theory.deref thy_ref) th) end;
wenzelm@29603
   471
ballarin@29218
   472
ballarin@29218
   473
wenzelm@30775
   474
(** activate in context **)
ballarin@28832
   475
wenzelm@30775
   476
(* init *)
ballarin@28832
   477
ballarin@38108
   478
fun generic_note_thmss kind facts context =
ballarin@38108
   479
  let
ballarin@38108
   480
    val facts' = Attrib.map_facts (Attrib.attribute_i (Context.theory_of context)) facts;
ballarin@38108
   481
  in
ballarin@38108
   482
    context |> Context.mapping_result
wenzelm@39557
   483
      (Global_Theory.note_thmss kind facts')
ballarin@38108
   484
      (ProofContext.note_thmss kind facts')
ballarin@38108
   485
  end;
ballarin@38108
   486
wenzelm@30775
   487
fun init (Fixes fixes) = Context.map_proof (ProofContext.add_fixes fixes #> #2)
wenzelm@30775
   488
  | init (Constrains _) = I
wenzelm@30775
   489
  | init (Assumes asms) = Context.map_proof (fn ctxt =>
ballarin@28832
   490
      let
ballarin@28832
   491
        val asms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) asms;
wenzelm@30775
   492
        val (_, ctxt') = ctxt
wenzelm@30775
   493
          |> fold Variable.auto_fixes (maps (map #1 o #2) asms')
wenzelm@30775
   494
          |> ProofContext.add_assms_i Assumption.assume_export asms';
wenzelm@30775
   495
      in ctxt' end)
wenzelm@30775
   496
  | init (Defines defs) = Context.map_proof (fn ctxt =>
ballarin@28832
   497
      let
ballarin@28832
   498
        val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs;
ballarin@28832
   499
        val asms = defs' |> map (fn ((name, atts), (t, ps)) =>
wenzelm@35624
   500
            let val ((c, _), t') = Local_Defs.cert_def ctxt t  (* FIXME adapt ps? *)
wenzelm@30434
   501
            in (t', ((Thm.def_binding_optional (Binding.name c) name, atts), [(t', ps)])) end);
wenzelm@30775
   502
        val (_, ctxt') = ctxt
wenzelm@30775
   503
          |> fold Variable.auto_fixes (map #1 asms)
wenzelm@35624
   504
          |> ProofContext.add_assms_i Local_Defs.def_export (map #2 asms);
wenzelm@30775
   505
      in ctxt' end)
ballarin@38108
   506
  | init (Notes (kind, facts)) = generic_note_thmss kind facts #> #2;
wenzelm@30775
   507
wenzelm@30775
   508
wenzelm@30775
   509
(* activate *)
wenzelm@30775
   510
wenzelm@30777
   511
fun activate_i elem ctxt =
ballarin@28832
   512
  let
wenzelm@30777
   513
    val elem' = map_ctxt_attrib Args.assignable elem;
wenzelm@30777
   514
    val ctxt' = Context.proof_map (init elem') ctxt;
wenzelm@30777
   515
  in (map_ctxt_attrib Args.closure elem', ctxt') end;
ballarin@28832
   516
wenzelm@30777
   517
fun activate raw_elem ctxt =
wenzelm@30777
   518
  let val elem = raw_elem |> map_ctxt
wenzelm@30775
   519
   {binding = tap Name.of_binding,
wenzelm@29603
   520
    typ = I,
wenzelm@29603
   521
    term = I,
wenzelm@29603
   522
    pattern = I,
wenzelm@30775
   523
    fact = ProofContext.get_fact ctxt,
wenzelm@30777
   524
    attrib = Attrib.intern_src (ProofContext.theory_of ctxt)}
wenzelm@30777
   525
  in activate_i elem ctxt end;
ballarin@28832
   526
wenzelm@19267
   527
end;