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