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