src/Pure/Isar/element.ML
author wenzelm
Fri Apr 27 22:47:30 2012 +0200 (2012-04-27)
changeset 47815 43f677b3ae91
parent 47249 c0481c3c2a6c
child 49750 444cfaa331c9
permissions -rw-r--r--
clarified signature;
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@29603
    23
  val map_ctxt: {binding: binding -> binding, typ: 'typ -> 'a, term: 'term -> 'b,
wenzelm@29603
    24
    pattern: 'term -> 'b, fact: 'fact -> 'c, attrib: Attrib.src -> Attrib.src} ->
wenzelm@29603
    25
    ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
wenzelm@21528
    26
  val map_ctxt_attrib: (Attrib.src -> Attrib.src) ->
wenzelm@21528
    27
    ('typ, 'term, 'fact) ctxt -> ('typ, 'term, 'fact) ctxt
wenzelm@45290
    28
  val transform_ctxt: morphism -> context_i -> context_i
wenzelm@45601
    29
  val transform_facts: morphism ->
wenzelm@45601
    30
    (Attrib.binding * (thm list * Args.src list) list) list ->
wenzelm@45601
    31
    (Attrib.binding * (thm list * Args.src list) list) list
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
wenzelm@30775
    54
  val init: context_i -> Context.generic -> Context.generic
wenzelm@30777
    55
  val activate_i: context_i -> Proof.context -> context_i * Proof.context
wenzelm@30777
    56
  val activate: (typ, term, Facts.ref) ctxt -> Proof.context -> context_i * Proof.context
wenzelm@18140
    57
end;
wenzelm@18140
    58
wenzelm@18140
    59
structure Element: ELEMENT =
wenzelm@18140
    60
struct
wenzelm@18140
    61
wenzelm@19777
    62
(** language elements **)
wenzelm@19777
    63
wenzelm@19777
    64
(* statement *)
wenzelm@19259
    65
wenzelm@19259
    66
datatype ('typ, 'term) stmt =
wenzelm@28084
    67
  Shows of (Attrib.binding * ('term * 'term list) list) list |
haftmann@29578
    68
  Obtains of (binding * ((binding * 'typ option) list * 'term list)) list;
wenzelm@19259
    69
wenzelm@19259
    70
type statement = (string, string) stmt;
wenzelm@19259
    71
type statement_i = (typ, term) stmt;
wenzelm@19259
    72
wenzelm@19259
    73
wenzelm@19777
    74
(* context *)
wenzelm@18140
    75
wenzelm@18140
    76
datatype ('typ, 'term, 'fact) ctxt =
haftmann@29578
    77
  Fixes of (binding * 'typ option * mixfix) list |
wenzelm@18140
    78
  Constrains of (string * 'typ) list |
wenzelm@28084
    79
  Assumes of (Attrib.binding * ('term * 'term list) list) list |
wenzelm@28084
    80
  Defines of (Attrib.binding * ('term * 'term list)) list |
wenzelm@28084
    81
  Notes of string * (Attrib.binding * ('fact * Attrib.src list) list) list;
wenzelm@18140
    82
wenzelm@26336
    83
type context = (string, string, Facts.ref) ctxt;
wenzelm@18140
    84
type context_i = (typ, term, thm list) ctxt;
wenzelm@18140
    85
wenzelm@29603
    86
fun map_ctxt {binding, typ, term, pattern, fact, attrib} =
wenzelm@29603
    87
  fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) => (binding x, Option.map typ T, mx)))
wenzelm@28079
    88
   | Constrains xs => Constrains (xs |> map (fn (x, T) =>
wenzelm@42494
    89
      (Variable.check_name (binding (Binding.name x)), typ T)))
wenzelm@18140
    90
   | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
wenzelm@29603
    91
      ((binding a, map attrib atts), propps |> map (fn (t, ps) => (term t, map pattern ps)))))
wenzelm@18140
    92
   | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
wenzelm@29603
    93
      ((binding a, map attrib atts), (term t, map pattern ps))))
wenzelm@21440
    94
   | Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) =>
haftmann@28965
    95
      ((binding a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
wenzelm@18140
    96
wenzelm@21528
    97
fun map_ctxt_attrib attrib =
wenzelm@29603
    98
  map_ctxt {binding = I, typ = I, term = I, pattern = I, fact = I, attrib = attrib};
wenzelm@21528
    99
wenzelm@45290
   100
fun transform_ctxt phi = map_ctxt
haftmann@28965
   101
 {binding = Morphism.binding phi,
wenzelm@21481
   102
  typ = Morphism.typ phi,
wenzelm@21481
   103
  term = Morphism.term phi,
wenzelm@29603
   104
  pattern = Morphism.term phi,
wenzelm@21521
   105
  fact = Morphism.fact phi,
wenzelm@45290
   106
  attrib = Args.transform_values phi};
wenzelm@18140
   107
wenzelm@45601
   108
fun transform_facts phi facts =
wenzelm@45601
   109
  Notes ("", facts) |> transform_ctxt phi |> (fn Notes (_, facts') => facts');
wenzelm@45601
   110
wenzelm@19808
   111
wenzelm@18894
   112
wenzelm@19259
   113
(** pretty printing **)
wenzelm@19259
   114
wenzelm@19267
   115
fun pretty_items _ _ [] = []
wenzelm@19267
   116
  | pretty_items keyword sep (x :: ys) =
wenzelm@19267
   117
      Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] ::
wenzelm@19267
   118
        map (fn y => Pretty.block [Pretty.str "  ", Pretty.keyword sep, Pretty.brk 1, y]) ys;
wenzelm@19259
   119
haftmann@28862
   120
fun pretty_name_atts ctxt (b, atts) sep =
wenzelm@45584
   121
  if Attrib.is_empty_binding (b, atts) then []
wenzelm@43547
   122
  else
wenzelm@43547
   123
    [Pretty.block (Pretty.breaks
wenzelm@43547
   124
      (Binding.pretty b :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))];
wenzelm@19259
   125
wenzelm@19259
   126
wenzelm@19259
   127
(* pretty_stmt *)
wenzelm@19259
   128
wenzelm@19259
   129
fun pretty_stmt ctxt =
wenzelm@19259
   130
  let
wenzelm@24920
   131
    val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt;
wenzelm@24920
   132
    val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
wenzelm@19267
   133
    val prt_terms = separate (Pretty.keyword "and") o map prt_term;
wenzelm@19259
   134
    val prt_name_atts = pretty_name_atts ctxt;
wenzelm@19259
   135
wenzelm@19259
   136
    fun prt_show (a, ts) =
wenzelm@19267
   137
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts)));
wenzelm@19259
   138
wenzelm@28079
   139
    fun prt_var (x, SOME T) = Pretty.block
wenzelm@30223
   140
          [Pretty.str (Binding.name_of x ^ " ::"), Pretty.brk 1, prt_typ T]
wenzelm@30223
   141
      | prt_var (x, NONE) = Pretty.str (Binding.name_of x);
wenzelm@26721
   142
    val prt_vars = separate (Pretty.keyword "and") o map prt_var;
wenzelm@19259
   143
wenzelm@19267
   144
    fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts))
wenzelm@19259
   145
      | prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks
wenzelm@19585
   146
          (prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts));
wenzelm@19259
   147
  in
wenzelm@19267
   148
    fn Shows shows => pretty_items "shows" "and" (map prt_show shows)
wenzelm@19267
   149
     | Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains)
wenzelm@19259
   150
  end;
wenzelm@19259
   151
wenzelm@18894
   152
wenzelm@19259
   153
(* pretty_ctxt *)
wenzelm@19259
   154
wenzelm@19259
   155
fun pretty_ctxt ctxt =
wenzelm@19259
   156
  let
wenzelm@24920
   157
    val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt;
wenzelm@24920
   158
    val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
wenzelm@32091
   159
    val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
wenzelm@19259
   160
    val prt_name_atts = pretty_name_atts ctxt;
wenzelm@19259
   161
wenzelm@19267
   162
    fun prt_mixfix NoSyn = []
wenzelm@42287
   163
      | prt_mixfix mx = [Pretty.brk 2, Mixfix.pretty_mixfix mx];
wenzelm@19267
   164
wenzelm@30223
   165
    fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (Binding.name_of x ^ " ::") ::
wenzelm@28079
   166
          Pretty.brk 1 :: prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
wenzelm@30223
   167
      | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str (Binding.name_of x) ::
wenzelm@28079
   168
          Pretty.brk 1 :: prt_mixfix mx);
haftmann@28965
   169
    fun prt_constrain (x, T) = prt_fix (Binding.name x, SOME T, NoSyn);
wenzelm@18894
   170
wenzelm@19259
   171
    fun prt_asm (a, ts) =
wenzelm@19259
   172
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
wenzelm@19259
   173
    fun prt_def (a, (t, _)) =
wenzelm@19259
   174
      Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
wenzelm@19259
   175
wenzelm@19259
   176
    fun prt_fact (ths, []) = map prt_thm ths
wenzelm@19259
   177
      | prt_fact (ths, atts) = Pretty.enclose "(" ")"
wenzelm@21032
   178
          (Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts;
wenzelm@19259
   179
    fun prt_note (a, ths) =
wenzelm@19482
   180
      Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths)));
wenzelm@19259
   181
  in
wenzelm@19267
   182
    fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes)
wenzelm@19267
   183
     | Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs)
wenzelm@19267
   184
     | Assumes asms => pretty_items "assumes" "and" (map prt_asm asms)
wenzelm@19267
   185
     | Defines defs => pretty_items "defines" "and" (map prt_def defs)
wenzelm@21440
   186
     | Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts)
wenzelm@21440
   187
     | Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts)
wenzelm@19259
   188
  end;
wenzelm@18894
   189
wenzelm@19267
   190
wenzelm@19267
   191
(* pretty_statement *)
wenzelm@19267
   192
wenzelm@19267
   193
local
wenzelm@19267
   194
wenzelm@41581
   195
fun standard_elim th =
wenzelm@41581
   196
  (case Object_Logic.elim_concl th of
wenzelm@41581
   197
    SOME C =>
wenzelm@41581
   198
      let
wenzelm@41581
   199
        val cert = Thm.cterm_of (Thm.theory_of_thm th);
wenzelm@41581
   200
        val thesis = Var ((Auto_Bind.thesisN, Thm.maxidx_of th + 1), fastype_of C);
wenzelm@41581
   201
        val th' = Thm.instantiate ([], [(cert C, cert thesis)]) th;
wenzelm@41581
   202
      in (th', true) end
wenzelm@41581
   203
  | NONE => (th, false));
wenzelm@41581
   204
wenzelm@19267
   205
fun thm_name kind th prts =
wenzelm@19267
   206
  let val head =
wenzelm@27865
   207
    if Thm.has_name_hint th then
wenzelm@21965
   208
      Pretty.block [Pretty.command kind,
wenzelm@30364
   209
        Pretty.brk 1, Pretty.str (Long_Name.base_name (Thm.get_name_hint th) ^ ":")]
wenzelm@21965
   210
    else Pretty.command kind
wenzelm@19267
   211
  in Pretty.block (Pretty.fbreaks (head :: prts)) end;
wenzelm@19267
   212
wenzelm@19267
   213
fun obtain prop ctxt =
wenzelm@19267
   214
  let
wenzelm@41581
   215
    val ((ps, prop'), ctxt') = Variable.focus prop ctxt;
wenzelm@42488
   216
    fun fix (x, T) = (Binding.name (Variable.revert_fixed ctxt' x), SOME T);
wenzelm@42495
   217
    val xs = map (fix o #2) ps;
wenzelm@42495
   218
    val As = Logic.strip_imp_prems prop';
wenzelm@41581
   219
  in ((Binding.empty, (xs, As)), ctxt') end;
wenzelm@19267
   220
wenzelm@19267
   221
in
wenzelm@19267
   222
wenzelm@19267
   223
fun pretty_statement ctxt kind raw_th =
wenzelm@19267
   224
  let
wenzelm@42360
   225
    val thy = Proof_Context.theory_of ctxt;
wenzelm@20150
   226
wenzelm@41581
   227
    val (th, is_elim) = standard_elim (Raw_Simplifier.norm_hhf raw_th);
wenzelm@41581
   228
    val ((_, [th']), ctxt') = Variable.import true [th] (Variable.set_body true 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@42488
   235
        then insert (op =) (Variable.revert_fixed ctxt' 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@42495
   243
        let val (clauses, ctxt'') = fold_map obtain 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;
wenzelm@44058
   259
fun witness_hyps (Witness (_, th)) = Thm.hyps_of th;
wenzelm@19777
   260
fun map_witness f (Witness witn) = Witness (f witn);
wenzelm@19777
   261
wenzelm@45290
   262
fun transform_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
wenzelm@46896
   279
    val propss =
wenzelm@46896
   280
      (map o map) (fn prop => (mark_witness prop, [])) wit_propss @
wenzelm@46896
   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@46899
   289
  Proof.local_goal (K (K ())) (K I) Proof_Context.bind_propp_i cmd NONE
wenzelm@46899
   290
    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
wenzelm@46856
   463
fun eq_morphism _ [] = NONE
wenzelm@46856
   464
  | eq_morphism thy thms =
wenzelm@46856
   465
      SOME (Morphism.morphism
wenzelm@46856
   466
       {binding = [],
wenzelm@46856
   467
        typ = [],
wenzelm@46856
   468
        term = [Raw_Simplifier.rewrite_term thy thms []],
wenzelm@46856
   469
        fact = [map (Raw_Simplifier.rewrite_rule thms)]});
haftmann@29525
   470
haftmann@29525
   471
ballarin@29218
   472
(* transfer to theory using closure *)
ballarin@29218
   473
ballarin@29218
   474
fun transfer_morphism thy =
wenzelm@29603
   475
  let val thy_ref = Theory.check_thy thy
wenzelm@38709
   476
  in Morphism.thm_morphism (fn th => Thm.transfer (Theory.deref thy_ref) th) end;
wenzelm@29603
   477
ballarin@29218
   478
ballarin@29218
   479
wenzelm@30775
   480
(** activate in context **)
ballarin@28832
   481
wenzelm@30775
   482
(* init *)
ballarin@28832
   483
wenzelm@42360
   484
fun init (Fixes fixes) = Context.map_proof (Proof_Context.add_fixes fixes #> #2)
wenzelm@30775
   485
  | init (Constrains _) = I
wenzelm@30775
   486
  | init (Assumes asms) = Context.map_proof (fn ctxt =>
ballarin@28832
   487
      let
wenzelm@47815
   488
        val asms' = Attrib.map_specs (map (Attrib.attribute ctxt)) asms;
wenzelm@30775
   489
        val (_, ctxt') = ctxt
wenzelm@30775
   490
          |> fold Variable.auto_fixes (maps (map #1 o #2) asms')
wenzelm@42360
   491
          |> Proof_Context.add_assms_i Assumption.assume_export asms';
wenzelm@30775
   492
      in ctxt' end)
wenzelm@30775
   493
  | init (Defines defs) = Context.map_proof (fn ctxt =>
ballarin@28832
   494
      let
wenzelm@47815
   495
        val defs' = Attrib.map_specs (map (Attrib.attribute ctxt)) defs;
ballarin@28832
   496
        val asms = defs' |> map (fn ((name, atts), (t, ps)) =>
wenzelm@35624
   497
            let val ((c, _), t') = Local_Defs.cert_def ctxt t  (* FIXME adapt ps? *)
wenzelm@30434
   498
            in (t', ((Thm.def_binding_optional (Binding.name c) name, atts), [(t', ps)])) end);
wenzelm@30775
   499
        val (_, ctxt') = ctxt
wenzelm@30775
   500
          |> fold Variable.auto_fixes (map #1 asms)
wenzelm@42360
   501
          |> Proof_Context.add_assms_i Local_Defs.def_export (map #2 asms);
wenzelm@30775
   502
      in ctxt' end)
wenzelm@47249
   503
  | init (Notes (kind, facts)) = Attrib.generic_notes kind facts #> #2;
wenzelm@30775
   504
wenzelm@30775
   505
wenzelm@30775
   506
(* activate *)
wenzelm@30775
   507
wenzelm@30777
   508
fun activate_i elem ctxt =
ballarin@28832
   509
  let
wenzelm@30777
   510
    val elem' = map_ctxt_attrib Args.assignable elem;
wenzelm@30777
   511
    val ctxt' = Context.proof_map (init elem') ctxt;
wenzelm@30777
   512
  in (map_ctxt_attrib Args.closure elem', ctxt') end;
ballarin@28832
   513
wenzelm@30777
   514
fun activate raw_elem ctxt =
wenzelm@30777
   515
  let val elem = raw_elem |> map_ctxt
wenzelm@43842
   516
   {binding = I,
wenzelm@29603
   517
    typ = I,
wenzelm@29603
   518
    term = I,
wenzelm@29603
   519
    pattern = I,
wenzelm@42360
   520
    fact = Proof_Context.get_fact ctxt,
wenzelm@42360
   521
    attrib = Attrib.intern_src (Proof_Context.theory_of ctxt)}
wenzelm@30777
   522
  in activate_i elem ctxt end;
ballarin@28832
   523
wenzelm@19267
   524
end;