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