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