src/Pure/Isar/element.ML
author wenzelm
Fri Apr 08 16:34:14 2011 +0200 (2011-04-08)
changeset 42290 b1f544c84040
parent 42287 d98eb048a2e4
child 42357 3305f573294e
permissions -rw-r--r--
discontinued special treatment of structure Lexicon;
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@42287
   172
      | prt_mixfix mx = [Pretty.brk 2, Mixfix.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@41581
   204
fun standard_elim th =
wenzelm@41581
   205
  (case Object_Logic.elim_concl th of
wenzelm@41581
   206
    SOME C =>
wenzelm@41581
   207
      let
wenzelm@41581
   208
        val cert = Thm.cterm_of (Thm.theory_of_thm th);
wenzelm@41581
   209
        val thesis = Var ((Auto_Bind.thesisN, Thm.maxidx_of th + 1), fastype_of C);
wenzelm@41581
   210
        val th' = Thm.instantiate ([], [(cert C, cert thesis)]) th;
wenzelm@41581
   211
      in (th', true) end
wenzelm@41581
   212
  | NONE => (th, false));
wenzelm@41581
   213
wenzelm@19267
   214
fun thm_name kind th prts =
wenzelm@19267
   215
  let val head =
wenzelm@27865
   216
    if Thm.has_name_hint th then
wenzelm@21965
   217
      Pretty.block [Pretty.command kind,
wenzelm@30364
   218
        Pretty.brk 1, Pretty.str (Long_Name.base_name (Thm.get_name_hint th) ^ ":")]
wenzelm@21965
   219
    else Pretty.command kind
wenzelm@19267
   220
  in Pretty.block (Pretty.fbreaks (head :: prts)) end;
wenzelm@19267
   221
wenzelm@19267
   222
fun obtain prop ctxt =
wenzelm@19267
   223
  let
wenzelm@41581
   224
    val ((ps, prop'), ctxt') = Variable.focus prop ctxt;
wenzelm@41581
   225
    fun fix (x, T) = (Binding.name (ProofContext.revert_skolem ctxt' x), SOME T);
wenzelm@41581
   226
    val xs = map (fix o Term.dest_Free o Thm.term_of o #2) ps;
wenzelm@20150
   227
    val As = Logic.strip_imp_prems (Thm.term_of prop');
wenzelm@41581
   228
  in ((Binding.empty, (xs, As)), ctxt') end;
wenzelm@19267
   229
wenzelm@19267
   230
in
wenzelm@19267
   231
wenzelm@19267
   232
fun pretty_statement ctxt kind raw_th =
wenzelm@19267
   233
  let
wenzelm@19267
   234
    val thy = ProofContext.theory_of ctxt;
wenzelm@20150
   235
    val cert = Thm.cterm_of thy;
wenzelm@20150
   236
wenzelm@41581
   237
    val (th, is_elim) = standard_elim (Raw_Simplifier.norm_hhf raw_th);
wenzelm@41581
   238
    val ((_, [th']), ctxt') = Variable.import true [th] (Variable.set_body true ctxt);
wenzelm@20150
   239
    val prop = Thm.prop_of th';
wenzelm@20150
   240
    val (prems, concl) = Logic.strip_horn prop;
wenzelm@35625
   241
    val concl_term = Object_Logic.drop_judgment thy concl;
wenzelm@19267
   242
wenzelm@20150
   243
    val fixes = fold_aterms (fn v as Free (x, T) =>
wenzelm@20150
   244
        if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term)
wenzelm@41581
   245
        then insert (op =) (ProofContext.revert_skolem ctxt' x, T) else I | _ => I) prop [] |> rev;
wenzelm@20150
   246
    val (assumes, cases) = take_suffix (fn prem =>
wenzelm@20150
   247
      is_elim andalso concl aconv Logic.strip_assums_concl prem) prems;
wenzelm@19267
   248
  in
haftmann@28965
   249
    pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) fixes)) @
haftmann@28965
   250
    pretty_ctxt ctxt' (Assumes (map (fn t => (Attrib.empty_binding, [(t, [])])) assumes)) @
haftmann@28965
   251
     (if null cases then pretty_stmt ctxt' (Shows [(Attrib.empty_binding, [(concl, [])])])
wenzelm@26716
   252
      else
wenzelm@26716
   253
        let val (clauses, ctxt'') = fold_map (obtain o cert) cases ctxt'
wenzelm@26716
   254
        in pretty_stmt ctxt'' (Obtains clauses) end)
wenzelm@19267
   255
  end |> thm_name kind raw_th;
wenzelm@19267
   256
wenzelm@18140
   257
end;
wenzelm@19267
   258
wenzelm@19777
   259
wenzelm@19777
   260
wenzelm@19777
   261
(** logical operations **)
wenzelm@19777
   262
wenzelm@19777
   263
(* witnesses -- hypotheses as protected facts *)
wenzelm@19777
   264
wenzelm@19777
   265
datatype witness = Witness of term * thm;
wenzelm@19777
   266
haftmann@29578
   267
val mark_witness = Logic.protect;
haftmann@29578
   268
fun witness_prop (Witness (t, _)) = t;
haftmann@29578
   269
fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
wenzelm@19777
   270
fun map_witness f (Witness witn) = Witness (f witn);
wenzelm@19777
   271
wenzelm@21481
   272
fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th));
wenzelm@21481
   273
wenzelm@20058
   274
fun prove_witness ctxt t tac =
haftmann@29578
   275
  Witness (t, Thm.close_derivation (Goal.prove ctxt [] [] (mark_witness t) (fn _ =>
wenzelm@25202
   276
    Tactic.rtac Drule.protectI 1 THEN tac)));
wenzelm@19777
   277
wenzelm@29603
   278
haftmann@29578
   279
local
haftmann@29578
   280
haftmann@29578
   281
val refine_witness =
wenzelm@30510
   282
  Proof.refine (Method.Basic (K (RAW_METHOD
haftmann@29578
   283
    (K (ALLGOALS
haftmann@29578
   284
      (CONJUNCTS (ALLGOALS
wenzelm@32194
   285
        (CONJUNCTS (TRYALL (Tactic.rtac Drule.protectI))))))))));
wenzelm@25624
   286
haftmann@29578
   287
fun gen_witness_proof proof after_qed wit_propss eq_props =
haftmann@29578
   288
  let
haftmann@29578
   289
    val propss = (map o map) (fn prop => (mark_witness prop, [])) wit_propss
haftmann@29578
   290
      @ [map (rpair []) eq_props];
haftmann@29578
   291
    fun after_qed' thmss =
wenzelm@29603
   292
      let val (wits, eqs) = split_last ((map o map) Thm.close_derivation thmss);
haftmann@29578
   293
      in after_qed ((map2 o map2) (curry Witness) wit_propss wits) eqs end;
haftmann@29578
   294
  in proof after_qed' propss #> refine_witness #> Seq.hd end;
haftmann@29578
   295
ballarin@38108
   296
fun proof_local cmd goal_ctxt int after_qed' propss =
ballarin@38108
   297
    Proof.map_context (K goal_ctxt)
ballarin@38108
   298
    #> Proof.local_goal (Proof_Display.print_results int) (K I) ProofContext.bind_propp_i
ballarin@38108
   299
      cmd NONE after_qed' (map (pair Thm.empty_binding) propss);
haftmann@41425
   300
haftmann@29578
   301
in
haftmann@29578
   302
haftmann@29578
   303
fun witness_proof after_qed wit_propss =
wenzelm@36323
   304
  gen_witness_proof (Proof.theorem NONE) (fn wits => fn _ => after_qed wits)
haftmann@29578
   305
    wit_propss [];
haftmann@29578
   306
wenzelm@36323
   307
val witness_proof_eqs = gen_witness_proof (Proof.theorem NONE);
haftmann@29578
   308
haftmann@29578
   309
fun witness_local_proof after_qed cmd wit_propss goal_ctxt int =
ballarin@38108
   310
  gen_witness_proof (proof_local cmd goal_ctxt int)
haftmann@29578
   311
    (fn wits => fn _ => after_qed wits) wit_propss [];
haftmann@29578
   312
ballarin@38108
   313
fun witness_local_proof_eqs after_qed cmd wit_propss eq_props goal_ctxt int =
ballarin@38108
   314
  gen_witness_proof (proof_local cmd goal_ctxt int) after_qed wit_propss eq_props;
haftmann@41425
   315
wenzelm@29603
   316
end;
wenzelm@29603
   317
wenzelm@19777
   318
wenzelm@25302
   319
fun compose_witness (Witness (_, th)) r =
wenzelm@25302
   320
  let
wenzelm@25302
   321
    val th' = Goal.conclude th;
wenzelm@25302
   322
    val A = Thm.cprem_of r 1;
ballarin@25739
   323
  in
ballarin@25739
   324
    Thm.implies_elim
ballarin@25739
   325
      (Conv.gconv_rule Drule.beta_eta_conversion 1 r)
ballarin@25739
   326
      (Conv.fconv_rule Drule.beta_eta_conversion
ballarin@25739
   327
        (Thm.instantiate (Thm.match (Thm.cprop_of th', A)) th'))
ballarin@25739
   328
  end;
wenzelm@25302
   329
haftmann@29578
   330
fun conclude_witness (Witness (_, th)) =
wenzelm@41228
   331
  Thm.close_derivation (Raw_Simplifier.norm_hhf_protect (Goal.conclude th));
wenzelm@19777
   332
ballarin@22658
   333
fun pretty_witness ctxt witn =
wenzelm@24920
   334
  let val prt_term = Pretty.quote o Syntax.pretty_term ctxt in
ballarin@22658
   335
    Pretty.block (prt_term (witness_prop witn) ::
wenzelm@39166
   336
      (if Config.get ctxt show_hyps then [Pretty.brk 2, Pretty.list "[" "]"
ballarin@22658
   337
         (map prt_term (witness_hyps witn))] else []))
ballarin@22658
   338
  end;
ballarin@22658
   339
wenzelm@19777
   340
wenzelm@19777
   341
(* derived rules *)
wenzelm@19777
   342
wenzelm@20007
   343
fun instantiate_tfrees thy subst th =
wenzelm@19777
   344
  let
wenzelm@19777
   345
    val certT = Thm.ctyp_of thy;
wenzelm@20007
   346
    val idx = Thm.maxidx_of th + 1;
wenzelm@20007
   347
    fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
wenzelm@20007
   348
wenzelm@20007
   349
    fun add_inst (a, S) insts =
wenzelm@20007
   350
      if AList.defined (op =) insts a then insts
wenzelm@20007
   351
      else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
wenzelm@20007
   352
    val insts =
wenzelm@20007
   353
      Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
wenzelm@20007
   354
        (Thm.full_prop_of th) [];
wenzelm@19777
   355
  in
wenzelm@20007
   356
    th
wenzelm@20007
   357
    |> Thm.generalize (map fst insts, []) idx
wenzelm@20007
   358
    |> Thm.instantiate (map cert_inst insts, [])
wenzelm@19777
   359
  end;
wenzelm@19777
   360
wenzelm@19777
   361
fun instantiate_frees thy subst =
wenzelm@19777
   362
  let val cert = Thm.cterm_of thy in
wenzelm@19777
   363
    Drule.forall_intr_list (map (cert o Free o fst) subst) #>
wenzelm@19777
   364
    Drule.forall_elim_list (map (cert o snd) subst)
wenzelm@19777
   365
  end;
wenzelm@19777
   366
wenzelm@19777
   367
fun hyps_rule rule th =
wenzelm@21521
   368
  let val {hyps, ...} = Thm.crep_thm th in
wenzelm@19777
   369
    Drule.implies_elim_list
wenzelm@19777
   370
      (rule (Drule.implies_intr_list hyps th))
wenzelm@21521
   371
      (map (Thm.assume o Drule.cterm_rule rule) hyps)
wenzelm@19777
   372
  end;
wenzelm@19777
   373
wenzelm@19777
   374
wenzelm@19777
   375
(* instantiate types *)
wenzelm@19777
   376
wenzelm@19777
   377
fun instT_type env =
wenzelm@19777
   378
  if Symtab.is_empty env then I
wenzelm@19777
   379
  else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
wenzelm@19777
   380
wenzelm@19777
   381
fun instT_term env =
wenzelm@19777
   382
  if Symtab.is_empty env then I
wenzelm@20548
   383
  else Term.map_types (instT_type env);
wenzelm@19777
   384
wenzelm@22691
   385
fun instT_subst env th = (Thm.fold_terms o Term.fold_types o Term.fold_atyps)
wenzelm@20304
   386
  (fn T as TFree (a, _) =>
wenzelm@20304
   387
    let val T' = the_default T (Symtab.lookup env a)
wenzelm@20304
   388
    in if T = T' then I else insert (op =) (a, T') end
wenzelm@20304
   389
  | _ => I) th [];
wenzelm@19777
   390
wenzelm@19777
   391
fun instT_thm thy env th =
wenzelm@19777
   392
  if Symtab.is_empty env then th
wenzelm@19777
   393
  else
wenzelm@19777
   394
    let val subst = instT_subst env th
wenzelm@19777
   395
    in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
wenzelm@19777
   396
wenzelm@22672
   397
fun instT_morphism thy env =
wenzelm@24137
   398
  let val thy_ref = Theory.check_thy thy in
wenzelm@22672
   399
    Morphism.morphism
wenzelm@29603
   400
     {binding = I,
wenzelm@22672
   401
      typ = instT_type env,
wenzelm@22672
   402
      term = instT_term env,
wenzelm@22672
   403
      fact = map (fn th => instT_thm (Theory.deref thy_ref) env th)}
wenzelm@22672
   404
  end;
wenzelm@19777
   405
wenzelm@19777
   406
wenzelm@19777
   407
(* instantiate types and terms *)
wenzelm@19777
   408
wenzelm@19777
   409
fun inst_term (envT, env) =
wenzelm@19777
   410
  if Symtab.is_empty env then instT_term envT
wenzelm@19777
   411
  else
wenzelm@19777
   412
    let
wenzelm@19777
   413
      val instT = instT_type envT;
wenzelm@19777
   414
      fun inst (Const (x, T)) = Const (x, instT T)
wenzelm@19777
   415
        | inst (Free (x, T)) =
wenzelm@19777
   416
            (case Symtab.lookup env x of
wenzelm@19777
   417
              NONE => Free (x, instT T)
wenzelm@19777
   418
            | SOME t => t)
wenzelm@19777
   419
        | inst (Var (xi, T)) = Var (xi, instT T)
wenzelm@19777
   420
        | inst (b as Bound _) = b
wenzelm@19777
   421
        | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
wenzelm@19777
   422
        | inst (t $ u) = inst t $ inst u;
wenzelm@19777
   423
    in Envir.beta_norm o inst end;
wenzelm@19777
   424
wenzelm@19777
   425
fun inst_thm thy (envT, env) th =
wenzelm@19777
   426
  if Symtab.is_empty env then instT_thm thy envT th
wenzelm@19777
   427
  else
wenzelm@19777
   428
    let
wenzelm@19777
   429
      val substT = instT_subst envT th;
wenzelm@22691
   430
      val subst = (Thm.fold_terms o Term.fold_aterms)
wenzelm@20304
   431
       (fn Free (x, T) =>
wenzelm@19777
   432
          let
wenzelm@19777
   433
            val T' = instT_type envT T;
wenzelm@19777
   434
            val t = Free (x, T');
wenzelm@19777
   435
            val t' = the_default t (Symtab.lookup env x);
wenzelm@20304
   436
          in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
wenzelm@20304
   437
       | _ => I) th [];
wenzelm@19777
   438
    in
wenzelm@19777
   439
      if null substT andalso null subst then th
wenzelm@19777
   440
      else th |> hyps_rule
wenzelm@19777
   441
       (instantiate_tfrees thy substT #>
wenzelm@19777
   442
        instantiate_frees thy subst #>
wenzelm@22900
   443
        Conv.fconv_rule (Thm.beta_conversion true))
wenzelm@19777
   444
    end;
wenzelm@19777
   445
wenzelm@22672
   446
fun inst_morphism thy envs =
wenzelm@24137
   447
  let val thy_ref = Theory.check_thy thy in
wenzelm@22672
   448
    Morphism.morphism
wenzelm@29603
   449
     {binding = I,
wenzelm@22672
   450
      typ = instT_type (#1 envs),
wenzelm@22672
   451
      term = inst_term envs,
wenzelm@22672
   452
      fact = map (fn th => inst_thm (Theory.deref thy_ref) envs th)}
wenzelm@22672
   453
  end;
wenzelm@19777
   454
wenzelm@19777
   455
wenzelm@19777
   456
(* satisfy hypotheses *)
wenzelm@19777
   457
wenzelm@19777
   458
fun satisfy_thm witns thm = thm |> fold (fn hyp =>
wenzelm@19777
   459
    (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
wenzelm@19777
   460
      NONE => I
wenzelm@25302
   461
    | SOME w => Thm.implies_intr hyp #> compose_witness w)) (#hyps (Thm.crep_thm thm));
wenzelm@19777
   462
wenzelm@29603
   463
val satisfy_morphism = Morphism.thm_morphism o satisfy_thm;
wenzelm@29603
   464
val satisfy_facts = facts_map o morph_ctxt o satisfy_morphism;
wenzelm@20264
   465
wenzelm@20264
   466
haftmann@29525
   467
(* rewriting with equalities *)
haftmann@29525
   468
haftmann@36674
   469
fun eq_morphism thy thms = if null thms then NONE else SOME (Morphism.morphism
wenzelm@29603
   470
 {binding = I,
wenzelm@29603
   471
  typ = I,
wenzelm@41228
   472
  term = Raw_Simplifier.rewrite_term thy thms [],
wenzelm@41228
   473
  fact = map (Raw_Simplifier.rewrite_rule thms)});
haftmann@29525
   474
haftmann@29525
   475
ballarin@29218
   476
(* transfer to theory using closure *)
ballarin@29218
   477
ballarin@29218
   478
fun transfer_morphism thy =
wenzelm@29603
   479
  let val thy_ref = Theory.check_thy thy
wenzelm@38709
   480
  in Morphism.thm_morphism (fn th => Thm.transfer (Theory.deref thy_ref) th) end;
wenzelm@29603
   481
ballarin@29218
   482
ballarin@29218
   483
wenzelm@30775
   484
(** activate in context **)
ballarin@28832
   485
wenzelm@30775
   486
(* init *)
ballarin@28832
   487
ballarin@38108
   488
fun generic_note_thmss kind facts context =
ballarin@38108
   489
  let
ballarin@38108
   490
    val facts' = Attrib.map_facts (Attrib.attribute_i (Context.theory_of context)) facts;
ballarin@38108
   491
  in
ballarin@38108
   492
    context |> Context.mapping_result
wenzelm@39557
   493
      (Global_Theory.note_thmss kind facts')
ballarin@38108
   494
      (ProofContext.note_thmss kind facts')
ballarin@38108
   495
  end;
ballarin@38108
   496
wenzelm@30775
   497
fun init (Fixes fixes) = Context.map_proof (ProofContext.add_fixes fixes #> #2)
wenzelm@30775
   498
  | init (Constrains _) = I
wenzelm@30775
   499
  | init (Assumes asms) = Context.map_proof (fn ctxt =>
ballarin@28832
   500
      let
ballarin@28832
   501
        val asms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) asms;
wenzelm@30775
   502
        val (_, ctxt') = ctxt
wenzelm@30775
   503
          |> fold Variable.auto_fixes (maps (map #1 o #2) asms')
wenzelm@30775
   504
          |> ProofContext.add_assms_i Assumption.assume_export asms';
wenzelm@30775
   505
      in ctxt' end)
wenzelm@30775
   506
  | init (Defines defs) = Context.map_proof (fn ctxt =>
ballarin@28832
   507
      let
ballarin@28832
   508
        val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs;
ballarin@28832
   509
        val asms = defs' |> map (fn ((name, atts), (t, ps)) =>
wenzelm@35624
   510
            let val ((c, _), t') = Local_Defs.cert_def ctxt t  (* FIXME adapt ps? *)
wenzelm@30434
   511
            in (t', ((Thm.def_binding_optional (Binding.name c) name, atts), [(t', ps)])) end);
wenzelm@30775
   512
        val (_, ctxt') = ctxt
wenzelm@30775
   513
          |> fold Variable.auto_fixes (map #1 asms)
wenzelm@35624
   514
          |> ProofContext.add_assms_i Local_Defs.def_export (map #2 asms);
wenzelm@30775
   515
      in ctxt' end)
ballarin@38108
   516
  | init (Notes (kind, facts)) = generic_note_thmss kind facts #> #2;
wenzelm@30775
   517
wenzelm@30775
   518
wenzelm@30775
   519
(* activate *)
wenzelm@30775
   520
wenzelm@30777
   521
fun activate_i elem ctxt =
ballarin@28832
   522
  let
wenzelm@30777
   523
    val elem' = map_ctxt_attrib Args.assignable elem;
wenzelm@30777
   524
    val ctxt' = Context.proof_map (init elem') ctxt;
wenzelm@30777
   525
  in (map_ctxt_attrib Args.closure elem', ctxt') end;
ballarin@28832
   526
wenzelm@30777
   527
fun activate raw_elem ctxt =
wenzelm@30777
   528
  let val elem = raw_elem |> map_ctxt
wenzelm@30775
   529
   {binding = tap Name.of_binding,
wenzelm@29603
   530
    typ = I,
wenzelm@29603
   531
    term = I,
wenzelm@29603
   532
    pattern = I,
wenzelm@30775
   533
    fact = ProofContext.get_fact ctxt,
wenzelm@30777
   534
    attrib = Attrib.intern_src (ProofContext.theory_of ctxt)}
wenzelm@30777
   535
  in activate_i elem ctxt end;
ballarin@28832
   536
wenzelm@19267
   537
end;