src/Pure/Isar/theory_target.ML
author wenzelm
Wed Nov 29 15:45:02 2006 +0100 (2006-11-29)
changeset 21594 2859c94d67d4
parent 21585 2444f1d1127b
child 21611 fc95ff1fe738
permissions -rw-r--r--
reworked notes: towards proper import/export of proof terms;
tuned;
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    ID:         $Id$
wenzelm@20894
     3
    Author:     Makarius
wenzelm@20894
     4
wenzelm@21594
     5
Common theory/locale targets.
wenzelm@20894
     6
*)
wenzelm@20894
     7
wenzelm@20894
     8
signature THEORY_TARGET =
wenzelm@20894
     9
sig
wenzelm@21006
    10
  val peek: local_theory -> string option
wenzelm@20962
    11
  val begin: bstring -> Proof.context -> local_theory
wenzelm@20894
    12
  val init: xstring option -> theory -> local_theory
wenzelm@20894
    13
  val init_i: string option -> theory -> local_theory
wenzelm@20894
    14
end;
wenzelm@20894
    15
wenzelm@20894
    16
structure TheoryTarget: THEORY_TARGET =
wenzelm@20894
    17
struct
wenzelm@20894
    18
wenzelm@21594
    19
wenzelm@20894
    20
(** locale targets **)
wenzelm@20894
    21
wenzelm@21006
    22
(* context data *)
wenzelm@21006
    23
wenzelm@21006
    24
structure Data = ProofDataFun
wenzelm@21006
    25
(
wenzelm@21006
    26
  val name = "Isar/theory_target";
wenzelm@21006
    27
  type T = string option;
wenzelm@21006
    28
  fun init _ = NONE;
wenzelm@21006
    29
  fun print _ _ = ();
wenzelm@21006
    30
);
wenzelm@21006
    31
wenzelm@21006
    32
val _ = Context.add_setup Data.init;
wenzelm@21006
    33
val peek = Data.get;
wenzelm@21006
    34
wenzelm@21006
    35
wenzelm@20894
    36
(* pretty *)
wenzelm@20894
    37
wenzelm@20894
    38
fun pretty loc ctxt =
wenzelm@20894
    39
  let
wenzelm@20894
    40
    val thy = ProofContext.theory_of ctxt;
wenzelm@20894
    41
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@21585
    42
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
wenzelm@20894
    43
    val elems =
wenzelm@20894
    44
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    45
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    46
  in
wenzelm@20984
    47
    if loc = "" then
wenzelm@20984
    48
      [Pretty.block
wenzelm@20984
    49
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@20984
    50
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
wenzelm@20984
    51
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
wenzelm@20984
    52
    else
wenzelm@20984
    53
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
wenzelm@20984
    54
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    55
  end;
wenzelm@20894
    56
wenzelm@20894
    57
wenzelm@20894
    58
(* consts *)
wenzelm@20894
    59
wenzelm@21594
    60
fun consts is_loc depends decls lthy =
wenzelm@20894
    61
  let
wenzelm@21533
    62
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@20894
    63
wenzelm@20894
    64
    fun const ((c, T), mx) thy =
wenzelm@20894
    65
      let
wenzelm@20894
    66
        val U = map #2 xs ---> T;
wenzelm@21533
    67
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
wenzelm@21533
    68
        val thy' = Sign.add_consts_authentic [(c, U, Syntax.unlocalize_mixfix is_loc mx)] thy;
wenzelm@21533
    69
      in (((c, mx), t), thy') end;
wenzelm@20894
    70
wenzelm@21533
    71
    val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls);
wenzelm@21533
    72
    val defs = abbrs |> map (fn (x, t) => (x, (("", []), t)));
wenzelm@20894
    73
  in
wenzelm@20894
    74
    lthy'
wenzelm@21570
    75
    |> is_loc ? LocalTheory.abbrevs Syntax.default_mode abbrs
wenzelm@21594
    76
    |> LocalDefs.add_defs defs
wenzelm@21594
    77
    |>> map (apsnd snd)
wenzelm@20894
    78
  end;
wenzelm@20894
    79
wenzelm@20894
    80
wenzelm@20894
    81
(* axioms *)
wenzelm@20894
    82
wenzelm@20894
    83
local
wenzelm@20894
    84
wenzelm@20894
    85
fun add_axiom hyps (name, prop) thy =
wenzelm@20894
    86
  let
wenzelm@20894
    87
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@20894
    88
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@20894
    89
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@20894
    90
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@20894
    91
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@20894
    92
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@20894
    93
wenzelm@20894
    94
in
wenzelm@20894
    95
wenzelm@21433
    96
fun axioms kind specs lthy =
wenzelm@20915
    97
  let
wenzelm@21585
    98
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
wenzelm@20915
    99
    fun axiom ((name, atts), props) thy = thy
wenzelm@20915
   100
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@20915
   101
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@20915
   102
  in
wenzelm@20915
   103
    lthy
wenzelm@20984
   104
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@20915
   105
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@21433
   106
    |-> LocalTheory.notes kind
wenzelm@20915
   107
  end;
wenzelm@20894
   108
wenzelm@20894
   109
end;
wenzelm@20894
   110
wenzelm@20894
   111
wenzelm@20894
   112
(* defs *)
wenzelm@20894
   113
wenzelm@21594
   114
local
wenzelm@21594
   115
wenzelm@21570
   116
infix also;
wenzelm@21570
   117
fun eq1 also eq2 =
wenzelm@21570
   118
  eq2 COMP (eq1 COMP (Drule.incr_indexes2 eq1 eq2 transitive_thm));
wenzelm@21570
   119
wenzelm@21594
   120
fun expand_term ctxt t =
wenzelm@21570
   121
  let
wenzelm@21570
   122
    val thy = ProofContext.theory_of ctxt;
wenzelm@21570
   123
    val thy_ctxt = ProofContext.init thy;
wenzelm@21570
   124
    val ct = Thm.cterm_of thy t;
wenzelm@21594
   125
    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
wenzelm@21570
   126
  in (Thm.term_of ct', Tactic.rewrite true defs ct) end;
wenzelm@21570
   127
wenzelm@20894
   128
fun add_def (name, prop) =
wenzelm@20894
   129
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
   130
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   131
wenzelm@20894
   132
in
wenzelm@20894
   133
wenzelm@21570
   134
fun defs kind args lthy0 =
wenzelm@20894
   135
  let
wenzelm@21533
   136
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   137
      let
wenzelm@21594
   138
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
wenzelm@21570
   139
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
wenzelm@20894
   140
wenzelm@21570
   141
        val ([(lhs, lhs_def)], lthy2) = lthy1
wenzelm@21570
   142
          |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)];
wenzelm@21570
   143
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
wenzelm@21533
   144
wenzelm@21570
   145
        val name' = Thm.def_name_optional c name;
wenzelm@21570
   146
        val (def, lthy3) = lthy2
wenzelm@20894
   147
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@20894
   148
wenzelm@21533
   149
        val eq =
wenzelm@21570
   150
          (*c == loc.c xs*) lhs_def
wenzelm@21570
   151
          (*lhs' == rhs'*)  also def
wenzelm@21570
   152
          (*rhs' == rhs*)   also Thm.symmetric rhs_conv;
wenzelm@20894
   153
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
wenzelm@20894
   154
wenzelm@21570
   155
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
wenzelm@21433
   156
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
wenzelm@20894
   157
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
wenzelm@20894
   158
wenzelm@20894
   159
end;
wenzelm@20894
   160
wenzelm@20894
   161
wenzelm@20894
   162
(* notes *)
wenzelm@20894
   163
wenzelm@21594
   164
(* FIXME
wenzelm@21594
   165
fun import_export inner outer (name, raw_th) =
wenzelm@21594
   166
  let
wenzelm@21594
   167
    val th = norm_hhf_protect raw_th;
wenzelm@21594
   168
    val (defs, th') = LocalDefs.export inner outer th;
wenzelm@21594
   169
    val n = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21594
   170
wenzelm@21594
   171
    val result = PureThy.name_thm true (name, th');  (* FIXME proper thm definition!? *)
wenzelm@21585
   172
wenzelm@21594
   173
    val concl_conv = Tactic.rewrite true defs (Thm.cprop_of th);
wenzelm@21594
   174
    val assms = map (Tactic.rewrite_rule defs o Thm.assume) (Assumption.assms_of inner);
wenzelm@21594
   175
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21594
   176
    val reimported_result =
wenzelm@21594
   177
      (case SINGLE (Seq.INTERVAL assm_tac 1 n) result of
wenzelm@21594
   178
        NONE => raise THM ("Failed to re-import result", 0, [result])
wenzelm@21594
   179
      | SOME res => res) COMP (concl_conv COMP_INCR Drule.equal_elim_rule2);
wenzelm@21594
   180
    val _ = reimported_result COMP (th COMP_INCR Drule.remdups_rl) handle THM _ =>
wenzelm@21594
   181
      (warning "FIXME"; asm_rl);
wenzelm@21594
   182
  in (reimported_result, result) end;
wenzelm@21594
   183
*)
wenzelm@21594
   184
fun import_export inner outer (_, th) =
wenzelm@21594
   185
  (singleton (ProofContext.standard inner) th, Assumption.export false inner outer th);
wenzelm@21585
   186
wenzelm@21594
   187
fun notes loc kind facts lthy =
wenzelm@21585
   188
  let
wenzelm@21594
   189
    val is_loc = loc <> "";
wenzelm@21594
   190
    val thy = ProofContext.theory_of lthy;
wenzelm@21594
   191
    val thy_ctxt = ProofContext.init thy;
wenzelm@21585
   192
wenzelm@21594
   193
    val facts' = facts
wenzelm@21594
   194
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (fst a)) bs))
wenzelm@21594
   195
      |> PureThy.map_facts (import_export lthy thy_ctxt);
wenzelm@21594
   196
    val local_facts = facts'
wenzelm@21594
   197
      |> PureThy.map_facts #1
wenzelm@21594
   198
      |> Element.facts_map (Element.morph_ctxt (Morphism.thm_morphism Drule.local_standard));
wenzelm@21594
   199
    val global_facts = facts'
wenzelm@21594
   200
      |> PureThy.map_facts #2
wenzelm@21594
   201
      |> Element.generalize_facts lthy thy_ctxt
wenzelm@21594
   202
      |> PureThy.map_facts Drule.local_standard
wenzelm@21594
   203
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
wenzelm@21594
   204
  in
wenzelm@21594
   205
    lthy |> LocalTheory.theory (fn thy => thy
wenzelm@21594
   206
      |> Sign.qualified_names
wenzelm@21594
   207
      |> PureThy.note_thmss_i kind global_facts |> #2
wenzelm@21594
   208
      |> Sign.restore_naming thy)
wenzelm@21585
   209
wenzelm@21594
   210
    |> is_loc ? (fn lthy' => lthy' |> LocalTheory.target
wenzelm@21594
   211
      (Locale.add_thmss loc kind
wenzelm@21594
   212
        (Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy')) local_facts)))
wenzelm@21594
   213
wenzelm@21433
   214
    |> ProofContext.set_stmt true
wenzelm@20894
   215
    |> ProofContext.qualified_names
wenzelm@21594
   216
    |> ProofContext.note_thmss_i kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts)
wenzelm@20894
   217
    ||> ProofContext.restore_naming lthy
wenzelm@21433
   218
    ||> ProofContext.restore_stmt lthy
wenzelm@20894
   219
  end;
wenzelm@20894
   220
wenzelm@20894
   221
wenzelm@21533
   222
(* target declarations *)
wenzelm@20894
   223
wenzelm@21533
   224
fun decl _ "" f =
wenzelm@21533
   225
      LocalTheory.theory (Context.theory_map (f Morphism.identity)) #>
wenzelm@21533
   226
      LocalTheory.target (Context.proof_map (f Morphism.identity))
wenzelm@21533
   227
  | decl add loc f =
wenzelm@21533
   228
      LocalTheory.target (add loc (Context.proof_map o f));
wenzelm@20894
   229
wenzelm@21498
   230
val type_syntax = decl Locale.add_type_syntax;
wenzelm@21498
   231
val term_syntax = decl Locale.add_term_syntax;
wenzelm@21498
   232
val declaration = decl Locale.add_declaration;
wenzelm@20894
   233
wenzelm@21533
   234
fun target_morphism loc lthy =
wenzelm@21594
   235
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21594
   236
  Morphism.thm_morphism Drule.local_standard;
wenzelm@21533
   237
wenzelm@21533
   238
fun target_name "" lthy = Sign.full_name (ProofContext.theory_of lthy)
wenzelm@21533
   239
  | target_name _ lthy = ProofContext.full_name (LocalTheory.target_of lthy);
wenzelm@21533
   240
wenzelm@20894
   241
wenzelm@20894
   242
(* init and exit *)
wenzelm@20894
   243
wenzelm@21293
   244
fun begin loc =
wenzelm@21293
   245
  Data.put (if loc = "" then NONE else SOME loc) #>
wenzelm@21533
   246
  LocalTheory.init (NameSpace.base loc)
wenzelm@21293
   247
   {pretty = pretty loc,
wenzelm@21594
   248
    consts = consts (loc <> ""),
wenzelm@21293
   249
    axioms = axioms,
wenzelm@21293
   250
    defs = defs,
wenzelm@21293
   251
    notes = notes loc,
wenzelm@21498
   252
    type_syntax = type_syntax loc,
wenzelm@21293
   253
    term_syntax = term_syntax loc,
wenzelm@21293
   254
    declaration = declaration loc,
wenzelm@21533
   255
    target_morphism = target_morphism loc,
wenzelm@21533
   256
    target_name = target_name loc,
wenzelm@21293
   257
    reinit = fn _ => begin loc o (if loc = "" then ProofContext.init else Locale.init loc),
wenzelm@21533
   258
    exit = LocalTheory.target_of};
wenzelm@20894
   259
wenzelm@20962
   260
fun init_i NONE thy = begin "" (ProofContext.init thy)
wenzelm@20962
   261
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   262
wenzelm@21276
   263
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   264
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   265
wenzelm@20894
   266
end;