src/Pure/Isar/local_theory.ML
author wenzelm
Sun, 12 Feb 2006 21:34:26 +0100
changeset 19032 d25dfb797612
parent 19017 5f06c37043e4
child 19059 b4ca3100e818
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/local_theory.ML
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
     4
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
     5
Local theory operations, with optional target locale.
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
     6
*)
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
     7
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
     8
type local_theory = Proof.context;
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
     9
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    10
signature LOCAL_THEORY =
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    11
sig
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    12
  val params_of: local_theory -> (string * typ) list
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    13
  val hyps_of: local_theory -> term list
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    14
  val standard: local_theory -> thm -> thm
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    15
  val pretty_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> Pretty.T
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    16
  val quiet_mode: bool ref
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    17
  val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    18
  val theory: (theory -> theory) -> local_theory -> local_theory
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    19
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    20
  val init: xstring option -> theory -> local_theory
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    21
  val init_i: string option -> theory -> local_theory
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
    22
  val restore: local_theory -> local_theory
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    23
  val exit: local_theory -> local_theory * theory
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    24
  val consts: ((bstring * typ) * mixfix) list -> local_theory -> term list * local_theory
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
    25
  val consts_restricted: (string * typ -> bool) ->
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    26
    ((bstring * typ) * mixfix) list -> local_theory -> term list * local_theory
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    27
  val axioms: ((bstring * Attrib.src list) * term list) list -> local_theory ->
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    28
    (bstring * thm list) list * local_theory
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    29
  val axioms_finish: (local_theory -> thm -> thm) ->
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    30
    ((bstring * Attrib.src list) * term list) list -> local_theory ->
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    31
    (bstring * thm list) list * local_theory
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    32
  val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    33
    local_theory -> (bstring * thm list) list * local_theory
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    34
  val note: (bstring * Attrib.src list) * thm list -> local_theory ->
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    35
    (bstring * thm list) * local_theory
18823
916c493b7f0c added print_consts;
wenzelm
parents: 18805
diff changeset
    36
  val def: (bstring * mixfix) * ((bstring * Attrib.src list) * term) ->
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    37
    local_theory -> (term * (bstring * thm)) * local_theory
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    38
  val def_finish: (local_theory -> term -> thm -> thm) ->
18823
916c493b7f0c added print_consts;
wenzelm
parents: 18805
diff changeset
    39
    (bstring * mixfix) * ((bstring * Attrib.src list) * term) ->
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    40
    local_theory -> (term * (bstring * thm)) * local_theory
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    41
end;
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    42
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    43
structure LocalTheory: LOCAL_THEORY =
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    44
struct
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    45
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    46
(** local context data **)
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    47
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    48
structure Data = ProofDataFun
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    49
(
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
    50
  val name = "Pure/local_theory";
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    51
  type T =
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    52
   {locale: (string * (cterm list * Proof.context)) option,
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    53
    params: (string * typ) list,
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    54
    hyps: term list,
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    55
    exit: theory -> theory};
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    56
  fun init thy = {locale = NONE, params = [], hyps = [], exit = I};
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    57
  fun print _ _ = ();
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    58
);
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    59
val _ = Context.add_setup Data.init;
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    60
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    61
fun map_locale f = Data.map (fn {locale, params, hyps, exit} =>
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    62
  {locale = Option.map (fn (loc, (view, ctxt)) => (loc, (view, f ctxt))) locale,
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    63
    params = params, hyps = hyps, exit = exit});
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    64
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    65
val locale_of = #locale o Data.get;
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    66
val params_of = #params o Data.get;
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    67
val hyps_of = #hyps o Data.get;
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    68
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    69
fun standard ctxt =
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    70
  (case #locale (Data.get ctxt) of
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    71
    NONE => Drule.standard
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
    72
  | SOME (_, (_, loc_ctxt)) => ProofContext.export_standard ctxt loc_ctxt);
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    73
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    74
18823
916c493b7f0c added print_consts;
wenzelm
parents: 18805
diff changeset
    75
(* print consts *)
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    76
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    77
val quiet_mode = ref false;
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    78
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    79
local
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    80
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    81
fun pretty_var ctxt (x, T) =
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    82
  Pretty.block [Pretty.str x, Pretty.str " ::", Pretty.brk 1,
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    83
    Pretty.quote (ProofContext.pretty_typ ctxt T)];
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
    84
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    85
fun pretty_vars ctxt kind vs = Pretty.big_list kind (map (pretty_var ctxt) vs);
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    86
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    87
in
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    88
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
    89
fun pretty_consts ctxt pred cs =
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
    90
  (case filter pred (params_of ctxt) of
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    91
    [] => pretty_vars ctxt "constants" cs
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    92
  | ps => Pretty.chunks [pretty_vars ctxt "parameters" ps, pretty_vars ctxt "constants" cs]);
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    93
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
    94
fun print_consts _ _ [] = ()
18951
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    95
  | print_consts ctxt pred cs =
4f5f6c632127 type local_theory = Proof.context;
wenzelm
parents: 18876
diff changeset
    96
      if ! quiet_mode then () else Pretty.writeln (pretty_consts ctxt pred cs);
18823
916c493b7f0c added print_consts;
wenzelm
parents: 18805
diff changeset
    97
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    98
end;
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
    99
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   100
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   101
(* theory/locale substructures *)
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   102
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   103
fun theory_result f ctxt =
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   104
  let val (res, thy') = f (ProofContext.theory_of ctxt)
19032
wenzelm
parents: 19017
diff changeset
   105
  in (res, ctxt |> map_locale (ProofContext.transfer thy') |> ProofContext.transfer thy') end;
wenzelm
parents: 19017
diff changeset
   106
wenzelm
parents: 19017
diff changeset
   107
fun theory f ctxt = #2 (theory_result (f #> pair ()) ctxt);
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   108
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   109
fun locale_result f ctxt =
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   110
  (case locale_of ctxt of
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   111
    NONE => error "Local theory: no locale context"
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   112
  | SOME (_, view_ctxt) =>
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   113
      let
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   114
        val (res, loc_ctxt') = f view_ctxt;
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   115
        val thy' = ProofContext.theory_of loc_ctxt';
19032
wenzelm
parents: 19017
diff changeset
   116
      in (res, ctxt |> map_locale (K loc_ctxt') |> ProofContext.transfer thy') end);
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   117
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   118
fun locale f ctxt =
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   119
  if is_none (locale_of ctxt) then ctxt
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   120
  else #2 (locale_result (f #> pair ()) ctxt);
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   121
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   122
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   123
(* init -- restore -- exit *)
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   124
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   125
fun init_i NONE thy = ProofContext.init thy
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   126
  | init_i (SOME loc) thy =
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   127
      thy
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   128
      |> Locale.init loc
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   129
      ||>> ProofContext.inferred_fixes
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   130
      |> (fn ((view, params), ctxt) => Data.put
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   131
          {locale = SOME (loc, (view, ctxt)),
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   132
           params = params,
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   133
           hyps = ProofContext.assms_of ctxt,
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   134
           exit = Sign.restore_naming thy} ctxt)
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   135
      |> theory (Sign.add_path (Sign.base_name loc) #> Sign.no_base_names);
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   136
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   137
fun init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   138
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   139
fun restore ctxt =
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   140
  (case locale_of ctxt of
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   141
    NONE => ctxt
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   142
  | SOME (_, (_, loc_ctxt)) => loc_ctxt |> Data.put (Data.get ctxt));
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   143
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   144
fun exit ctxt = (ctxt, #exit (Data.get ctxt) (ProofContext.theory_of ctxt));
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   145
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   146
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   147
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   148
(** local theory **)
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   149
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   150
(* consts *)
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
   151
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
   152
fun consts_restricted pred decls ctxt =
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
   153
  let
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
   154
    val thy = ProofContext.theory_of ctxt;
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
   155
    val params = filter pred (params_of ctxt);
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
   156
    val ps = map Free params;
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   157
    val Ps = map #2 params;
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   158
    val abbrevs = decls |> map (fn ((c, T), mx) =>
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   159
      let val t = Term.list_comb (Const (Sign.full_name thy c, Ps ---> T), ps)
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   160
      in (c, t, mx) end);
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
   161
  in
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   162
    ctxt |>
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   163
    (case locale_of ctxt of
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   164
      NONE => theory (Sign.add_consts_i (map (fn ((c, T), mx) => (c, T, mx)) decls))
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   165
    | SOME (loc, _) =>
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   166
        theory (Sign.add_consts_i (map (fn ((c, T), _) => (c, Ps ---> T, NoSyn)) decls)) #>
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   167
        locale (Locale.add_abbrevs loc true abbrevs) #>
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   168
        ProofContext.add_abbrevs_i true abbrevs)
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   169
    |> pair (map #2 abbrevs)
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   170
  end;
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   171
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
   172
val consts = consts_restricted (K true);
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
   173
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   174
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   175
(* fact definitions *)
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   176
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   177
fun notes kind facts ctxt =
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   178
  let
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   179
    val attrib = Attrib.attribute_i (ProofContext.theory_of ctxt);
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   180
    val facts' = map (apsnd (map (apfst (map (standard ctxt))))) facts;
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   181
  in
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   182
    ctxt |>
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   183
    (case locale_of ctxt of
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   184
      NONE => theory_result (PureThy.note_thmss_i kind (Attrib.map_facts attrib facts'))
19017
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   185
    | SOME (loc, _) =>
5f06c37043e4 added restore;
wenzelm
parents: 18951
diff changeset
   186
        locale_result (apfst #1 o Locale.add_thmss kind loc facts'))
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   187
    ||> (#2 o ProofContext.note_thmss_i (Attrib.map_facts attrib facts))
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   188
  end;
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   189
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   190
fun note (a, ths) ctxt =
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   191
  ctxt |> notes PureThy.theoremK [(a, [(ths, [])])] |>> hd;
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   192
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   193
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   194
(* axioms *)
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   195
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   196
local
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   197
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   198
fun add_axiom hyps (name, prop) thy =
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   199
  let
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   200
    val name' = if name = "" then "axiom_" ^ string_of_int (serial ()) else name;
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   201
    val frees = rev ([] |> Term.add_frees prop |> fold Term.add_frees hyps);
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   202
    val prop' = Term.list_all_free (frees, Logic.list_implies (hyps, prop));
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   203
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   204
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   205
    val cert = Thm.cterm_of thy';
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   206
    fun all_polymorphic (x, T) th =
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   207
      let val var = cert (Var ((x, 0), #1 (Logic.dest_all (Thm.prop_of th))))
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   208
      in ((var, cert (Free (x, T))), Thm.forall_elim var th) end;
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   209
    fun implies_polymorphic hyp th = Thm.assume (cert hyp) COMP th;
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   210
    val th =
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   211
      Thm.get_axiom_i thy' (Sign.full_name thy' name')
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   212
      |> fold_map all_polymorphic frees |-> Drule.cterm_instantiate
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   213
      |> fold implies_polymorphic hyps
18767
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   214
  in (th, thy') end;
2f064e6bea7e added actual operations;
wenzelm
parents: 18744
diff changeset
   215
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   216
in
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   217
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   218
fun axioms_finish finish = fold_map (fn ((name, atts), props) =>
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   219
  fold ProofContext.fix_frees props
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   220
  #> (fn ctxt => ctxt
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   221
    |> theory_result (fold_map (add_axiom (hyps_of ctxt)) (PureThy.name_multi name props))
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   222
    |-> (fn ths => note ((name, atts), map (finish ctxt) ths))));
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   223
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   224
val axioms = axioms_finish (K I);
18744
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
   225
a9a5ee0e43e2 Local theory operations, with optional target locale.
wenzelm
parents:
diff changeset
   226
end;
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   227
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   228
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   229
(* constant definitions *)
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   230
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   231
local
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   232
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   233
fun add_def (name, prop) =
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   234
  Theory.add_defs_i false [(name, prop)] #> (fn thy =>
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   235
    let
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   236
      val th = Thm.get_axiom_i thy (Sign.full_name thy name);
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   237
      val cert = Thm.cterm_of thy;
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   238
      val subst = map2 (fn var => fn free => (cert (Var var), cert (Free free)))
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   239
        (Term.add_vars (Thm.prop_of th) []) (Term.add_frees prop []);
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   240
    in (Drule.cterm_instantiate subst th, thy) end);
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   241
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   242
in
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   243
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   244
fun def_finish finish (var, spec) ctxt =
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   245
  let
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   246
    val (x, mx) = var;
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   247
    val ((name, atts), rhs) = spec;
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   248
    val name' = if name = "" then Thm.def_name x else name;
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
   249
    val rhs_frees = Term.add_frees rhs [];
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   250
  in
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   251
    ctxt
18876
ddb6803da197 added consts_retricted;
wenzelm
parents: 18823
diff changeset
   252
    |> consts_restricted (member (op =) rhs_frees) [((x, Term.fastype_of rhs), mx)]
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   253
    |-> (fn [lhs] =>
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   254
      theory_result (add_def (name', Logic.mk_equals (lhs, rhs)))
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   255
      #-> (fn th => fn ctxt' => note ((name', atts), [finish ctxt' lhs th]) ctxt')
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   256
      #> apfst (fn (b, [th]) => (lhs, (b, th))))
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   257
  end;
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   258
18805
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   259
val def = def_finish (K (K I));
18863b33c831 improved 'notes', including proper treatment of locale results;
wenzelm
parents: 18781
diff changeset
   260
18781
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   261
end;
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   262
ea3b5e22eab5 added constant definition;
wenzelm
parents: 18767
diff changeset
   263
end;