src/Pure/Isar/theory_target.ML
author wenzelm
Fri Nov 10 22:18:53 2006 +0100 (2006-11-10)
changeset 21293 5f5162f40ac7
parent 21276 2285cf5a7560
child 21433 89104dca628e
permissions -rw-r--r--
removed mapping;
added reinit;
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@20894
    14
end;
wenzelm@20894
    15
wenzelm@20894
    16
structure TheoryTarget: THEORY_TARGET =
wenzelm@20894
    17
struct
wenzelm@20894
    18
wenzelm@20894
    19
(** locale targets **)
wenzelm@20894
    20
wenzelm@21006
    21
(* context data *)
wenzelm@21006
    22
wenzelm@21006
    23
structure Data = ProofDataFun
wenzelm@21006
    24
(
wenzelm@21006
    25
  val name = "Isar/theory_target";
wenzelm@21006
    26
  type T = string option;
wenzelm@21006
    27
  fun init _ = NONE;
wenzelm@21006
    28
  fun print _ _ = ();
wenzelm@21006
    29
);
wenzelm@21006
    30
wenzelm@21006
    31
val _ = Context.add_setup Data.init;
wenzelm@21006
    32
val peek = Data.get;
wenzelm@21006
    33
wenzelm@21006
    34
wenzelm@20894
    35
(* pretty *)
wenzelm@20894
    36
wenzelm@20894
    37
fun pretty loc ctxt =
wenzelm@20894
    38
  let
wenzelm@20894
    39
    val thy = ProofContext.theory_of ctxt;
wenzelm@20894
    40
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@20894
    41
    val assumes = map (fn A => (("", []), [(A, [])])) (Assumption.assms_of ctxt);
wenzelm@20894
    42
    val elems =
wenzelm@20894
    43
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    44
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    45
  in
wenzelm@20984
    46
    if loc = "" then
wenzelm@20984
    47
      [Pretty.block
wenzelm@20984
    48
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@20984
    49
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
wenzelm@20984
    50
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
wenzelm@20984
    51
    else
wenzelm@20984
    52
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
wenzelm@20984
    53
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    54
  end;
wenzelm@20894
    55
wenzelm@20894
    56
wenzelm@20894
    57
(* consts *)
wenzelm@20894
    58
wenzelm@20894
    59
fun consts loc depends decls lthy =
wenzelm@20894
    60
  let
wenzelm@20894
    61
    val xs = filter depends (#1 (ProofContext.inferred_fixes lthy));
wenzelm@20894
    62
    val ys = filter (Variable.newly_fixed lthy (LocalTheory.target_of lthy) o #1) xs;
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@20894
    67
        val mx' = if null ys then mx else NoSyn;
wenzelm@20894
    68
        val mx'' = Syntax.unlocalize_mixfix (loc <> "") mx';
wenzelm@20894
    69
wenzelm@20894
    70
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
wenzelm@20894
    71
        val defn = ((c, if loc <> "" then mx else NoSyn (* FIXME !? *)), (("", []), t));
wenzelm@20894
    72
        val abbr = ((c, mx'), fold_rev (lambda o Free) ys t);
wenzelm@20894
    73
        val thy' = Sign.add_consts_authentic [(c, U, mx'')] thy;
wenzelm@20894
    74
      in ((defn, abbr), thy') end;
wenzelm@20894
    75
wenzelm@20894
    76
    val ((defns, abbrs), lthy') = lthy
wenzelm@20894
    77
      |> LocalTheory.theory_result (fold_map const decls) |>> split_list;
wenzelm@20894
    78
  in
wenzelm@20894
    79
    lthy'
wenzelm@21276
    80
    |> K (loc <> "") ? (snd o LocalTheory.abbrevs Syntax.default_mode abbrs)
wenzelm@20894
    81
    |> LocalDefs.add_defs defns |>> map (apsnd snd)
wenzelm@20894
    82
  end;
wenzelm@20894
    83
wenzelm@20894
    84
wenzelm@20894
    85
(* axioms *)
wenzelm@20894
    86
wenzelm@20894
    87
local
wenzelm@20894
    88
wenzelm@20894
    89
fun add_axiom hyps (name, prop) thy =
wenzelm@20894
    90
  let
wenzelm@20894
    91
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@20894
    92
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@20894
    93
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@20894
    94
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@20894
    95
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@20894
    96
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@20894
    97
wenzelm@20894
    98
in
wenzelm@20894
    99
wenzelm@20915
   100
fun axioms specs lthy =
wenzelm@20915
   101
  let
wenzelm@20915
   102
    val hyps = Assumption.assms_of lthy;
wenzelm@20915
   103
    fun axiom ((name, atts), props) thy = thy
wenzelm@20915
   104
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@20915
   105
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@20915
   106
  in
wenzelm@20915
   107
    lthy
wenzelm@20984
   108
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@20915
   109
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@20915
   110
    |-> LocalTheory.notes
wenzelm@20915
   111
  end;
wenzelm@20894
   112
wenzelm@20894
   113
end;
wenzelm@20894
   114
wenzelm@20894
   115
wenzelm@20894
   116
(* defs *)
wenzelm@20894
   117
wenzelm@20894
   118
local
wenzelm@20894
   119
wenzelm@20894
   120
fun add_def (name, prop) =
wenzelm@20894
   121
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
   122
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   123
wenzelm@20894
   124
fun expand_defs lthy =
wenzelm@20894
   125
  Drule.term_rule (ProofContext.theory_of lthy)
wenzelm@20894
   126
    (Assumption.export false lthy (LocalTheory.target_of lthy));
wenzelm@20894
   127
wenzelm@20894
   128
in
wenzelm@20894
   129
wenzelm@20894
   130
fun defs args lthy =
wenzelm@20894
   131
  let
wenzelm@20894
   132
    fun def ((x, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   133
      let
wenzelm@20894
   134
        val name' = Thm.def_name_optional x name;
wenzelm@20894
   135
        val T = Term.fastype_of rhs;
wenzelm@20894
   136
        val rhs' = expand_defs lthy1 rhs;
wenzelm@20894
   137
        val depends = member (op =) (Term.add_frees rhs' []);
wenzelm@20894
   138
        val defined = filter_out depends (Term.add_frees rhs []);
wenzelm@20894
   139
wenzelm@20894
   140
        val rhs_conv = rhs
wenzelm@20894
   141
          |> Thm.cterm_of (ProofContext.theory_of lthy1)
wenzelm@20894
   142
          |> Tactic.rewrite true (map_filter (LocalDefs.find_def lthy1 o #1) defined);
wenzelm@20894
   143
wenzelm@20894
   144
        val ([(lhs, local_def)], lthy2) = lthy1
wenzelm@20894
   145
          |> LocalTheory.consts depends [((x, T), mx)];
wenzelm@20894
   146
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of local_def));
wenzelm@20894
   147
wenzelm@20894
   148
        val (global_def, lthy3) = lthy2
wenzelm@20894
   149
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@20894
   150
wenzelm@20894
   151
        val eq = Thm.transitive (Thm.transitive local_def global_def) (Thm.symmetric rhs_conv);
wenzelm@20894
   152
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
wenzelm@20894
   153
wenzelm@20894
   154
    val ((lhss, facts), lthy') = lthy |> fold_map def args |>> split_list;
wenzelm@20894
   155
    val (res, lthy'') = lthy' |> LocalTheory.notes facts;
wenzelm@20894
   156
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
wenzelm@20894
   157
wenzelm@20894
   158
end;
wenzelm@20894
   159
wenzelm@20894
   160
wenzelm@20894
   161
(* notes *)
wenzelm@20894
   162
wenzelm@20894
   163
fun context_notes facts lthy =
wenzelm@20894
   164
  let
wenzelm@20894
   165
    val facts' = facts
wenzelm@20894
   166
      |> Element.export_standard_facts lthy lthy
wenzelm@20894
   167
      |> Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of lthy));
wenzelm@20894
   168
  in
wenzelm@20894
   169
    lthy
wenzelm@20894
   170
    |> ProofContext.qualified_names
wenzelm@20894
   171
    |> ProofContext.note_thmss_i facts'
wenzelm@20894
   172
    ||> ProofContext.restore_naming lthy
wenzelm@20894
   173
  end;
wenzelm@20894
   174
wenzelm@20915
   175
fun theory_notes keep_atts facts lthy = lthy |> LocalTheory.theory (fn thy =>
wenzelm@20894
   176
  let
wenzelm@20894
   177
    val facts' = facts
wenzelm@20894
   178
      |> Element.export_standard_facts lthy (ProofContext.init thy)
wenzelm@20915
   179
      |> Attrib.map_facts (if keep_atts then Attrib.attribute_i thy else K I);
wenzelm@20894
   180
  in
wenzelm@20894
   181
    thy
wenzelm@20894
   182
    |> Sign.qualified_names
wenzelm@20894
   183
    |> PureThy.note_thmss_i "" facts' |> #2
wenzelm@20894
   184
    |> Sign.restore_naming thy
wenzelm@20894
   185
  end);
wenzelm@20894
   186
wenzelm@20894
   187
fun locale_notes loc facts lthy = lthy |> LocalTheory.target (fn ctxt =>
wenzelm@20894
   188
  #2 (Locale.add_thmss "" loc (Element.export_standard_facts lthy ctxt facts) ctxt));
wenzelm@20894
   189
wenzelm@20915
   190
fun notes "" facts = theory_notes true facts #> context_notes facts
wenzelm@20915
   191
  | notes loc facts = theory_notes false facts #> locale_notes loc facts #> context_notes facts;
wenzelm@20894
   192
wenzelm@20894
   193
wenzelm@20894
   194
(* declarations *)
wenzelm@20894
   195
wenzelm@20894
   196
fun term_syntax "" f = LocalTheory.theory (Context.theory_map f)
wenzelm@20894
   197
  | term_syntax loc f = LocalTheory.target (Locale.add_term_syntax loc (Context.proof_map f));
wenzelm@20894
   198
wenzelm@20894
   199
fun declaration "" f = LocalTheory.theory (Context.theory_map f)
wenzelm@20894
   200
  | declaration loc f = I;    (* FIXME *)
wenzelm@20894
   201
wenzelm@20894
   202
wenzelm@20894
   203
(* init and exit *)
wenzelm@20894
   204
wenzelm@21293
   205
fun begin loc =
wenzelm@21293
   206
  Data.put (if loc = "" then NONE else SOME loc) #>
wenzelm@21293
   207
  LocalTheory.init (SOME (NameSpace.base loc))
wenzelm@21293
   208
   {pretty = pretty loc,
wenzelm@21293
   209
    consts = consts loc,
wenzelm@21293
   210
    axioms = axioms,
wenzelm@21293
   211
    defs = defs,
wenzelm@21293
   212
    notes = notes loc,
wenzelm@21293
   213
    term_syntax = term_syntax loc,
wenzelm@21293
   214
    declaration = declaration loc,
wenzelm@21293
   215
    reinit = fn _ => begin loc o (if loc = "" then ProofContext.init else Locale.init loc),
wenzelm@21293
   216
    exit = LocalTheory.target_of}
wenzelm@20894
   217
wenzelm@20962
   218
fun init_i NONE thy = begin "" (ProofContext.init thy)
wenzelm@20962
   219
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   220
wenzelm@21276
   221
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   222
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   223
wenzelm@20894
   224
end;