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