src/Pure/theory.ML
author wenzelm
Thu Sep 20 20:56:34 2007 +0200 (2007-09-20)
changeset 24666 9885a86f14a8
parent 24626 85eceef2edc7
child 24708 d9b00117365e
permissions -rw-r--r--
tuned signature;
moved generic interpretation to interpretation.ML;
added abstract at_begin/end wrappers;
paulson@1526
     1
(*  Title:      Pure/theory.ML
paulson@1526
     2
    ID:         $Id$
paulson@1526
     3
    Author:     Lawrence C Paulson and Markus Wenzel
paulson@1526
     4
wenzelm@16443
     5
Logical theory content: axioms, definitions, oracles.
paulson@1526
     6
*)
wenzelm@16291
     7
wenzelm@3767
     8
signature BASIC_THEORY =
wenzelm@3767
     9
sig
wenzelm@16443
    10
  val eq_thy: theory * theory -> bool
wenzelm@3996
    11
  val subthy: theory * theory -> bool
wenzelm@3767
    12
end
wenzelm@3767
    13
wenzelm@3767
    14
signature THEORY =
wenzelm@3767
    15
sig
wenzelm@3767
    16
  include BASIC_THEORY
wenzelm@16443
    17
  include SIGN_THEORY
wenzelm@24666
    18
  val assert_super: theory -> theory -> theory
wenzelm@22684
    19
  val parents_of: theory -> theory list
wenzelm@22684
    20
  val ancestors_of: theory -> theory list
wenzelm@24666
    21
  val check_thy: theory -> theory_ref
wenzelm@24666
    22
  val deref: theory_ref -> theory
wenzelm@24666
    23
  val merge: theory * theory -> theory
wenzelm@24666
    24
  val merge_refs: theory_ref * theory_ref -> theory_ref
wenzelm@24666
    25
  val merge_list: theory list -> theory
wenzelm@16495
    26
  val checkpoint: theory -> theory
wenzelm@16495
    27
  val copy: theory -> theory
wenzelm@24666
    28
  val requires: theory -> string -> string -> unit
wenzelm@16443
    29
  val axiom_space: theory -> NameSpace.T
wenzelm@22684
    30
  val axiom_table: theory -> term Symtab.table
wenzelm@16443
    31
  val oracle_space: theory -> NameSpace.T
wenzelm@22684
    32
  val oracle_table: theory -> ((theory * Object.T -> term) * stamp) Symtab.table
wenzelm@16339
    33
  val axioms_of: theory -> (string * term) list
wenzelm@16339
    34
  val all_axioms_of: theory -> (string * term) list
wenzelm@24666
    35
  val defs_of: theory -> Defs.T
wenzelm@24666
    36
  val at_begin: (theory -> theory option) -> theory -> theory
wenzelm@24666
    37
  val at_end: (theory -> theory option) -> theory -> theory
wenzelm@24666
    38
  val begin_theory: string -> theory list -> theory
wenzelm@24666
    39
  val end_theory: theory -> theory
wenzelm@22684
    40
  val cert_axm: theory -> string * term -> string * term
wenzelm@22684
    41
  val read_axm: theory -> string * string -> string * term
wenzelm@3996
    42
  val add_axioms: (bstring * string) list -> theory -> theory
wenzelm@3996
    43
  val add_axioms_i: (bstring * term) list -> theory -> theory
wenzelm@19708
    44
  val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
wenzelm@19630
    45
  val add_defs: bool -> bool -> (bstring * string) list -> theory -> theory
wenzelm@19630
    46
  val add_defs_i: bool -> bool -> (bstring * term) list -> theory -> theory
wenzelm@16443
    47
  val add_finals: bool -> string list -> theory -> theory
wenzelm@16443
    48
  val add_finals_i: bool -> term list -> theory -> theory
wenzelm@16443
    49
  val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
wenzelm@16495
    50
end
paulson@1526
    51
wenzelm@24666
    52
structure Theory: THEORY =
wenzelm@16443
    53
struct
wenzelm@16443
    54
wenzelm@19708
    55
wenzelm@24666
    56
(** theory context operations **)
wenzelm@16443
    57
wenzelm@16443
    58
val eq_thy = Context.eq_thy;
wenzelm@16443
    59
val subthy = Context.subthy;
paulson@1526
    60
haftmann@24626
    61
fun assert_super thy1 thy2 =
haftmann@24626
    62
  if subthy (thy1, thy2) then thy2
haftmann@24626
    63
  else raise THEORY ("Not a super theory", [thy1, thy2]);
haftmann@24626
    64
wenzelm@16443
    65
val parents_of = Context.parents_of;
wenzelm@16443
    66
val ancestors_of = Context.ancestors_of;
wenzelm@16443
    67
wenzelm@24137
    68
val check_thy = Context.check_thy;
wenzelm@16443
    69
val deref = Context.deref;
wenzelm@24666
    70
wenzelm@16443
    71
val merge = Context.merge;
wenzelm@16443
    72
val merge_refs = Context.merge_refs;
wenzelm@16443
    73
wenzelm@23600
    74
fun merge_list [] = raise THEORY ("Empty merge of theories", [])
wenzelm@21608
    75
  | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
wenzelm@21608
    76
wenzelm@16495
    77
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    78
val copy = Context.copy_thy;
wenzelm@16495
    79
wenzelm@24666
    80
fun requires thy name what =
wenzelm@24666
    81
  if Context.exists_name name thy then ()
wenzelm@24666
    82
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
wenzelm@24666
    83
wenzelm@24666
    84
wenzelm@24666
    85
wenzelm@24666
    86
(** theory wrappers **)
wenzelm@24666
    87
wenzelm@24666
    88
type wrapper = (theory -> theory option) * stamp;
wenzelm@24666
    89
wenzelm@24666
    90
fun apply_wrappers (wrappers: wrapper list) =
wenzelm@24666
    91
  let
wenzelm@24666
    92
    fun app [] res = res
wenzelm@24666
    93
      | app ((f, _) :: fs) (changed, thy) =
wenzelm@24666
    94
          (case f thy of
wenzelm@24666
    95
            NONE => app fs (changed, thy)
wenzelm@24666
    96
          | SOME thy' => app fs (true, thy'));
wenzelm@24666
    97
    fun app_fixpoint thy =
wenzelm@24666
    98
      (case app wrappers (false, thy) of
wenzelm@24666
    99
        (false, _) => thy
wenzelm@24666
   100
      | (true, thy') => app_fixpoint thy');
wenzelm@24666
   101
  in app_fixpoint end;
wenzelm@24666
   102
wenzelm@24666
   103
wenzelm@24666
   104
wenzelm@24666
   105
(** datatype thy **)
wenzelm@24666
   106
wenzelm@24666
   107
datatype thy = Thy of
wenzelm@24666
   108
 {axioms: term NameSpace.table,
wenzelm@24666
   109
  defs: Defs.T,
wenzelm@24666
   110
  oracles: ((theory * Object.T -> term) * stamp) NameSpace.table,
wenzelm@24666
   111
  wrappers: wrapper list * wrapper list};
wenzelm@24666
   112
wenzelm@24666
   113
fun make_thy (axioms, defs, oracles, wrappers) =
wenzelm@24666
   114
  Thy {axioms = axioms, defs = defs, oracles = oracles, wrappers = wrappers};
wenzelm@24666
   115
wenzelm@24666
   116
fun err_dup_axm dup = error ("Duplicate axiom: " ^ quote dup);
wenzelm@24666
   117
fun err_dup_ora dup = error ("Duplicate oracle: " ^ quote dup);
wenzelm@24666
   118
wenzelm@24666
   119
structure ThyData = TheoryDataFun
wenzelm@24666
   120
(
wenzelm@24666
   121
  type T = thy;
wenzelm@24666
   122
  val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table, ([], []));
wenzelm@24666
   123
  val copy = I;
wenzelm@24666
   124
wenzelm@24666
   125
  fun extend (Thy {axioms, defs, oracles, wrappers}) =
wenzelm@24666
   126
    make_thy (NameSpace.empty_table, defs, oracles, wrappers);
wenzelm@24666
   127
wenzelm@24666
   128
  fun merge pp (thy1, thy2) =
wenzelm@24666
   129
    let
wenzelm@24666
   130
      val Thy {axioms = _, defs = defs1, oracles = oracles1, wrappers = (bgs1, ens1)} = thy1;
wenzelm@24666
   131
      val Thy {axioms = _, defs = defs2, oracles = oracles2, wrappers = (bgs2, ens2)} = thy2;
wenzelm@24666
   132
wenzelm@24666
   133
      val axioms' = NameSpace.empty_table;
wenzelm@24666
   134
      val defs' = Defs.merge pp (defs1, defs2);
wenzelm@24666
   135
      val oracles' = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
wenzelm@24666
   136
        handle Symtab.DUP dup => err_dup_ora dup;
wenzelm@24666
   137
      val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
wenzelm@24666
   138
      val ens' = Library.merge (eq_snd op =) (ens1, ens2);
wenzelm@24666
   139
    in make_thy (axioms', defs', oracles', (bgs', ens')) end;
wenzelm@24666
   140
);
wenzelm@24666
   141
wenzelm@24666
   142
fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
wenzelm@24666
   143
wenzelm@24666
   144
fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles, wrappers}) =>
wenzelm@24666
   145
  make_thy (f (axioms, defs, oracles, wrappers)));
wenzelm@24666
   146
wenzelm@24666
   147
wenzelm@24666
   148
fun map_axioms f = map_thy
wenzelm@24666
   149
  (fn (axioms, defs, oracles, wrappers) => (f axioms, defs, oracles, wrappers));
wenzelm@24666
   150
wenzelm@24666
   151
fun map_defs f = map_thy
wenzelm@24666
   152
  (fn (axioms, defs, oracles, wrappers) => (axioms, f defs, oracles, wrappers));
wenzelm@24666
   153
wenzelm@24666
   154
fun map_oracles f = map_thy
wenzelm@24666
   155
  (fn (axioms, defs, oracles, wrappers) => (axioms, defs, f oracles, wrappers));
wenzelm@24666
   156
wenzelm@24666
   157
fun map_wrappers f = map_thy
wenzelm@24666
   158
  (fn (axioms, defs, oracles, wrappers) => (axioms, defs, oracles, f wrappers));
wenzelm@24666
   159
wenzelm@24666
   160
wenzelm@24666
   161
(* basic operations *)
wenzelm@24666
   162
wenzelm@24666
   163
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@24666
   164
val axiom_table = #2 o #axioms o rep_theory;
wenzelm@24666
   165
wenzelm@24666
   166
val oracle_space = #1 o #oracles o rep_theory;
wenzelm@24666
   167
val oracle_table = #2 o #oracles o rep_theory;
wenzelm@24666
   168
wenzelm@24666
   169
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@24666
   170
fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
wenzelm@24666
   171
wenzelm@24666
   172
val defs_of = #defs o rep_theory;
wenzelm@24666
   173
wenzelm@24666
   174
wenzelm@24666
   175
(* begin/end theory *)
wenzelm@24666
   176
wenzelm@24666
   177
val begin_wrappers = rev o #1 o #wrappers o rep_theory;
wenzelm@24666
   178
val end_wrappers = rev o #2 o #wrappers o rep_theory;
wenzelm@24666
   179
wenzelm@24666
   180
fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
wenzelm@24666
   181
fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
wenzelm@24666
   182
wenzelm@24666
   183
fun begin_theory name imports =
wenzelm@24666
   184
  let
wenzelm@24666
   185
    val thy = Context.begin_thy Sign.pp name imports;
wenzelm@24666
   186
    val wrappers = begin_wrappers thy;
wenzelm@24666
   187
  in thy |> Sign.local_path |> apply_wrappers wrappers end;
wenzelm@24666
   188
wenzelm@24666
   189
fun end_theory thy =
wenzelm@24666
   190
  thy |> apply_wrappers (end_wrappers thy) |> Context.finish_thy;
wenzelm@24666
   191
wenzelm@16443
   192
wenzelm@16443
   193
(* signature operations *)
wenzelm@16443
   194
wenzelm@16443
   195
structure SignTheory: SIGN_THEORY = Sign;
wenzelm@16443
   196
open SignTheory;
wenzelm@16443
   197
wenzelm@2206
   198
wenzelm@3996
   199
wenzelm@24666
   200
(** add axioms **)
wenzelm@3814
   201
paulson@1526
   202
(* prepare axioms *)
paulson@1526
   203
wenzelm@18678
   204
fun err_in_axm msg name =
wenzelm@18678
   205
  cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   206
wenzelm@16443
   207
fun cert_axm thy (name, raw_tm) =
paulson@1526
   208
  let
wenzelm@18968
   209
    val (t, T, _) = Sign.certify_prop thy raw_tm
wenzelm@2979
   210
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   211
        | TERM (msg, _) => error msg;
paulson@1526
   212
  in
wenzelm@9537
   213
    Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
wenzelm@18968
   214
    (name, Sign.no_vars (Sign.pp thy) t)
wenzelm@9629
   215
  end;
paulson@1526
   216
wenzelm@22684
   217
fun read_axm thy (name, str) =
wenzelm@22689
   218
  cert_axm thy (name, Sign.read_prop thy str)
wenzelm@22689
   219
    handle ERROR msg => err_in_axm msg name;
paulson@1526
   220
paulson@1526
   221
wenzelm@16443
   222
(* add_axioms(_i) *)
paulson@1526
   223
wenzelm@16291
   224
local
wenzelm@16291
   225
wenzelm@16443
   226
fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
paulson@1526
   227
  let
wenzelm@16991
   228
    val axms = map (apsnd (Compress.term thy o Logic.varify) o prep_axm thy) raw_axms;
haftmann@23086
   229
    val axioms' = NameSpace.extend_table (Sign.naming_of thy) axms axioms
wenzelm@23655
   230
      handle Symtab.DUP dup => err_dup_axm dup;
wenzelm@16443
   231
  in axioms' end);
paulson@1526
   232
wenzelm@16291
   233
in
wenzelm@16291
   234
wenzelm@16291
   235
val add_axioms = gen_add_axioms read_axm;
wenzelm@16291
   236
val add_axioms_i = gen_add_axioms cert_axm;
wenzelm@16291
   237
wenzelm@16291
   238
end;
paulson@1526
   239
paulson@1526
   240
wenzelm@3767
   241
wenzelm@3767
   242
(** add constant definitions **)
wenzelm@3767
   243
wenzelm@19708
   244
(* dependencies *)
wenzelm@19708
   245
wenzelm@19708
   246
fun dependencies thy unchecked is_def name lhs rhs =
wenzelm@19708
   247
  let
wenzelm@19708
   248
    val pp = Sign.pp thy;
wenzelm@19708
   249
    val consts = Sign.consts_of thy;
wenzelm@19727
   250
    fun prep const =
wenzelm@19727
   251
      let val Const (c, T) = Sign.no_vars pp (Const const)
wenzelm@19806
   252
      in (c, Consts.typargs consts (c, Compress.typ thy (Logic.varifyT T))) end;
wenzelm@19708
   253
wenzelm@19708
   254
    val lhs_vars = Term.add_tfreesT (#2 lhs) [];
wenzelm@19708
   255
    val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
wenzelm@19708
   256
      if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
wenzelm@19708
   257
    val _ =
wenzelm@19708
   258
      if null rhs_extras then ()
wenzelm@19708
   259
      else error ("Specification depends on extra type variables: " ^
wenzelm@19708
   260
        commas_quote (map (Pretty.string_of_typ pp o TFree) rhs_extras) ^
wenzelm@19708
   261
        "\nThe error(s) above occurred in " ^ quote name);
haftmann@24199
   262
  in Defs.define pp unchecked is_def name (prep lhs) (map prep rhs) end;
wenzelm@19708
   263
wenzelm@19708
   264
fun add_deps a raw_lhs raw_rhs thy =
wenzelm@19708
   265
  let
wenzelm@19708
   266
    val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
wenzelm@19708
   267
    val name = if a = "" then (#1 lhs ^ " axiom") else a;
wenzelm@19708
   268
  in thy |> map_defs (dependencies thy false false name lhs rhs) end;
wenzelm@17706
   269
wenzelm@17706
   270
wenzelm@16944
   271
(* check_overloading *)
nipkow@9280
   272
wenzelm@16944
   273
fun check_overloading thy overloaded (c, T) =
wenzelm@16291
   274
  let
wenzelm@16944
   275
    val declT =
wenzelm@16944
   276
      (case Sign.const_constraint thy c of
wenzelm@16944
   277
        NONE => error ("Undeclared constant " ^ quote c)
wenzelm@16944
   278
      | SOME declT => declT);
wenzelm@19806
   279
    val T' = Logic.varifyT T;
wenzelm@16944
   280
wenzelm@16944
   281
    fun message txt =
wenzelm@16944
   282
      [Pretty.block [Pretty.str "Specification of constant ",
wenzelm@16944
   283
        Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy T)],
wenzelm@16944
   284
        Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
wenzelm@16291
   285
  in
wenzelm@16944
   286
    if Sign.typ_instance thy (declT, T') then ()
wenzelm@16944
   287
    else if Type.raw_instance (declT, T') then
wenzelm@16944
   288
      error (Library.setmp show_sorts true
wenzelm@16944
   289
        message "imposes additional sort constraints on the constant declaration")
wenzelm@16944
   290
    else if overloaded then ()
wenzelm@16944
   291
    else warning (message "is strictly less general than the declared type");
wenzelm@16944
   292
    (c, T)
nipkow@9280
   293
  end;
nipkow@9280
   294
wenzelm@3767
   295
wenzelm@16291
   296
(* check_def *)
wenzelm@16291
   297
wenzelm@19630
   298
fun check_def thy unchecked overloaded (bname, tm) defs =
wenzelm@16291
   299
  let
wenzelm@17706
   300
    val name = Sign.full_name thy bname;
wenzelm@19693
   301
    val (lhs_const, rhs) = Sign.cert_def (Sign.pp thy) tm;
wenzelm@16944
   302
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@18943
   303
    val _ = check_overloading thy overloaded lhs_const;
wenzelm@19708
   304
  in defs |> dependencies thy unchecked true name lhs_const rhs_consts end
wenzelm@18678
   305
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@16883
   306
   [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
wenzelm@19693
   307
    Pretty.fbrk, Pretty.quote (Sign.pretty_term thy tm)]));
wenzelm@3767
   308
wenzelm@3767
   309
wenzelm@16443
   310
(* add_defs(_i) *)
wenzelm@3767
   311
wenzelm@16291
   312
local
wenzelm@9320
   313
wenzelm@19630
   314
fun gen_add_defs prep_axm unchecked overloaded raw_axms thy =
wenzelm@16443
   315
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   316
    thy
wenzelm@19630
   317
    |> map_defs (fold (check_def thy unchecked overloaded) axms)
wenzelm@9320
   318
    |> add_axioms_i axms
wenzelm@3767
   319
  end;
wenzelm@3767
   320
wenzelm@16291
   321
in
wenzelm@16291
   322
wenzelm@16291
   323
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   324
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   325
wenzelm@16291
   326
end;
wenzelm@3767
   327
wenzelm@3767
   328
wenzelm@16443
   329
(* add_finals(_i) *)
skalberg@14223
   330
wenzelm@16291
   331
local
wenzelm@16291
   332
wenzelm@17706
   333
fun gen_add_finals prep_term overloaded args thy =
skalberg@14223
   334
  let
wenzelm@17706
   335
    fun const_of (Const const) = const
wenzelm@17706
   336
      | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
wenzelm@17706
   337
      | const_of _ = error "Attempt to finalize non-constant term";
wenzelm@19708
   338
    fun specify (c, T) = dependencies thy false false (c ^ " axiom") (c, T) [];
wenzelm@19708
   339
    val finalize = specify o check_overloading thy overloaded o const_of o prep_term thy;
wenzelm@17706
   340
  in thy |> map_defs (fold finalize args) end;
wenzelm@16291
   341
skalberg@14223
   342
in
wenzelm@16291
   343
wenzelm@17706
   344
val add_finals = gen_add_finals Sign.read_term;
wenzelm@17706
   345
val add_finals_i = gen_add_finals Sign.cert_term;
wenzelm@16291
   346
skalberg@14223
   347
end;
skalberg@14223
   348
skalberg@14223
   349
wenzelm@3878
   350
wenzelm@16443
   351
(** add oracle **)
wenzelm@3814
   352
wenzelm@16443
   353
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
haftmann@23086
   354
  NameSpace.extend_table (Sign.naming_of thy) [(bname, (oracle, stamp ()))] oracles
wenzelm@23655
   355
    handle Symtab.DUP dup => err_dup_ora dup);
wenzelm@3885
   356
paulson@1526
   357
end;
paulson@1526
   358
wenzelm@3767
   359
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   360
open BasicTheory;