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