src/Pure/Isar/theory_target.ML
author wenzelm
Sat Oct 14 23:25:56 2006 +0200 (2006-10-14)
changeset 21037 4b52b23f50eb
parent 21006 ac2732072403
child 21276 2285cf5a7560
permissions -rw-r--r--
added peek;
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    ID:         $Id$
wenzelm@20894
     3
    Author:     Makarius
wenzelm@20894
     4
wenzelm@20894
     5
Common theory 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@20984
    14
  val mapping: xstring option -> (local_theory -> local_theory) -> theory -> Proof.context * theory
wenzelm@20894
    15
end;
wenzelm@20894
    16
wenzelm@20894
    17
structure TheoryTarget: THEORY_TARGET =
wenzelm@20894
    18
struct
wenzelm@20894
    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@20894
    42
    val assumes = map (fn A => (("", []), [(A, [])])) (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@20894
    60
fun consts loc depends decls lthy =
wenzelm@20894
    61
  let
wenzelm@20894
    62
    val xs = filter depends (#1 (ProofContext.inferred_fixes lthy));
wenzelm@20894
    63
    val ys = filter (Variable.newly_fixed lthy (LocalTheory.target_of lthy) o #1) xs;
wenzelm@20894
    64
wenzelm@20894
    65
    fun const ((c, T), mx) thy =
wenzelm@20894
    66
      let
wenzelm@20894
    67
        val U = map #2 xs ---> T;
wenzelm@20894
    68
        val mx' = if null ys then mx else NoSyn;
wenzelm@20894
    69
        val mx'' = Syntax.unlocalize_mixfix (loc <> "") mx';
wenzelm@20894
    70
wenzelm@20894
    71
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
wenzelm@20894
    72
        val defn = ((c, if loc <> "" then mx else NoSyn (* FIXME !? *)), (("", []), t));
wenzelm@20894
    73
        val abbr = ((c, mx'), fold_rev (lambda o Free) ys t);
wenzelm@20894
    74
        val thy' = Sign.add_consts_authentic [(c, U, mx'')] thy;
wenzelm@20894
    75
      in ((defn, abbr), thy') end;
wenzelm@20894
    76
wenzelm@20894
    77
    val ((defns, abbrs), lthy') = lthy
wenzelm@20894
    78
      |> LocalTheory.theory_result (fold_map const decls) |>> split_list;
wenzelm@20894
    79
  in
wenzelm@20894
    80
    lthy'
wenzelm@20894
    81
    |> K (loc <> "") ? LocalTheory.abbrevs Syntax.default_mode abbrs
wenzelm@20894
    82
    |> LocalDefs.add_defs defns |>> map (apsnd snd)
wenzelm@20894
    83
  end;
wenzelm@20894
    84
wenzelm@20894
    85
wenzelm@20894
    86
(* axioms *)
wenzelm@20894
    87
wenzelm@20894
    88
local
wenzelm@20894
    89
wenzelm@20894
    90
fun add_axiom hyps (name, prop) thy =
wenzelm@20894
    91
  let
wenzelm@20894
    92
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@20894
    93
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@20894
    94
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@20894
    95
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@20894
    96
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@20894
    97
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@20894
    98
wenzelm@20894
    99
in
wenzelm@20894
   100
wenzelm@20915
   101
fun axioms specs lthy =
wenzelm@20915
   102
  let
wenzelm@20915
   103
    val hyps = Assumption.assms_of lthy;
wenzelm@20915
   104
    fun axiom ((name, atts), props) thy = thy
wenzelm@20915
   105
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@20915
   106
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@20915
   107
  in
wenzelm@20915
   108
    lthy
wenzelm@20984
   109
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@20915
   110
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@20915
   111
    |-> LocalTheory.notes
wenzelm@20915
   112
  end;
wenzelm@20894
   113
wenzelm@20894
   114
end;
wenzelm@20894
   115
wenzelm@20894
   116
wenzelm@20894
   117
(* defs *)
wenzelm@20894
   118
wenzelm@20894
   119
local
wenzelm@20894
   120
wenzelm@20894
   121
fun add_def (name, prop) =
wenzelm@20894
   122
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
   123
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   124
wenzelm@20894
   125
fun expand_defs lthy =
wenzelm@20894
   126
  Drule.term_rule (ProofContext.theory_of lthy)
wenzelm@20894
   127
    (Assumption.export false lthy (LocalTheory.target_of lthy));
wenzelm@20894
   128
wenzelm@20894
   129
in
wenzelm@20894
   130
wenzelm@20894
   131
fun defs args lthy =
wenzelm@20894
   132
  let
wenzelm@20894
   133
    fun def ((x, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   134
      let
wenzelm@20894
   135
        val name' = Thm.def_name_optional x name;
wenzelm@20894
   136
        val T = Term.fastype_of rhs;
wenzelm@20894
   137
        val rhs' = expand_defs lthy1 rhs;
wenzelm@20894
   138
        val depends = member (op =) (Term.add_frees rhs' []);
wenzelm@20894
   139
        val defined = filter_out depends (Term.add_frees rhs []);
wenzelm@20894
   140
wenzelm@20894
   141
        val rhs_conv = rhs
wenzelm@20894
   142
          |> Thm.cterm_of (ProofContext.theory_of lthy1)
wenzelm@20894
   143
          |> Tactic.rewrite true (map_filter (LocalDefs.find_def lthy1 o #1) defined);
wenzelm@20894
   144
wenzelm@20894
   145
        val ([(lhs, local_def)], lthy2) = lthy1
wenzelm@20894
   146
          |> LocalTheory.consts depends [((x, T), mx)];
wenzelm@20894
   147
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of local_def));
wenzelm@20894
   148
wenzelm@20894
   149
        val (global_def, lthy3) = lthy2
wenzelm@20894
   150
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@20894
   151
wenzelm@20894
   152
        val eq = Thm.transitive (Thm.transitive local_def global_def) (Thm.symmetric rhs_conv);
wenzelm@20894
   153
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
wenzelm@20894
   154
wenzelm@20894
   155
    val ((lhss, facts), lthy') = lthy |> fold_map def args |>> split_list;
wenzelm@20894
   156
    val (res, lthy'') = lthy' |> LocalTheory.notes 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@20894
   164
fun context_notes facts lthy =
wenzelm@20894
   165
  let
wenzelm@20894
   166
    val facts' = facts
wenzelm@20894
   167
      |> Element.export_standard_facts lthy lthy
wenzelm@20894
   168
      |> Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of lthy));
wenzelm@20894
   169
  in
wenzelm@20894
   170
    lthy
wenzelm@20894
   171
    |> ProofContext.qualified_names
wenzelm@20894
   172
    |> ProofContext.note_thmss_i facts'
wenzelm@20894
   173
    ||> ProofContext.restore_naming lthy
wenzelm@20894
   174
  end;
wenzelm@20894
   175
wenzelm@20915
   176
fun theory_notes keep_atts facts lthy = lthy |> LocalTheory.theory (fn thy =>
wenzelm@20894
   177
  let
wenzelm@20894
   178
    val facts' = facts
wenzelm@20894
   179
      |> Element.export_standard_facts lthy (ProofContext.init thy)
wenzelm@20915
   180
      |> Attrib.map_facts (if keep_atts then Attrib.attribute_i thy else K I);
wenzelm@20894
   181
  in
wenzelm@20894
   182
    thy
wenzelm@20894
   183
    |> Sign.qualified_names
wenzelm@20894
   184
    |> PureThy.note_thmss_i "" facts' |> #2
wenzelm@20894
   185
    |> Sign.restore_naming thy
wenzelm@20894
   186
  end);
wenzelm@20894
   187
wenzelm@20894
   188
fun locale_notes loc facts lthy = lthy |> LocalTheory.target (fn ctxt =>
wenzelm@20894
   189
  #2 (Locale.add_thmss "" loc (Element.export_standard_facts lthy ctxt facts) ctxt));
wenzelm@20894
   190
wenzelm@20915
   191
fun notes "" facts = theory_notes true facts #> context_notes facts
wenzelm@20915
   192
  | notes loc facts = theory_notes false facts #> locale_notes loc facts #> context_notes facts;
wenzelm@20894
   193
wenzelm@20894
   194
wenzelm@20894
   195
(* declarations *)
wenzelm@20894
   196
wenzelm@20894
   197
fun term_syntax "" f = LocalTheory.theory (Context.theory_map f)
wenzelm@20894
   198
  | term_syntax loc f = LocalTheory.target (Locale.add_term_syntax loc (Context.proof_map f));
wenzelm@20894
   199
wenzelm@20894
   200
fun declaration "" f = LocalTheory.theory (Context.theory_map f)
wenzelm@20894
   201
  | declaration loc f = I;    (* FIXME *)
wenzelm@20894
   202
wenzelm@20894
   203
wenzelm@20894
   204
wenzelm@20894
   205
(* init and exit *)
wenzelm@20894
   206
wenzelm@20962
   207
fun begin loc =
wenzelm@21037
   208
  Data.put (if loc = "" then NONE else SOME loc) #>
wenzelm@20962
   209
  LocalTheory.init (SOME (NameSpace.base loc))
wenzelm@20962
   210
   {pretty = pretty loc,
wenzelm@20962
   211
    consts = consts loc,
wenzelm@20962
   212
    axioms = axioms,
wenzelm@20962
   213
    defs = defs,
wenzelm@20962
   214
    notes = notes loc,
wenzelm@20962
   215
    term_syntax = term_syntax loc,
wenzelm@20962
   216
    declaration = declaration loc,
wenzelm@21037
   217
    exit = K I};
wenzelm@20894
   218
wenzelm@20962
   219
fun init_i NONE thy = begin "" (ProofContext.init thy)
wenzelm@20962
   220
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   221
wenzelm@20894
   222
fun init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   223
wenzelm@20984
   224
fun mapping loc f = init loc #> f #> LocalTheory.exit false;
wenzelm@20894
   225
wenzelm@20894
   226
end;