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