src/Pure/theory.ML
author wenzelm
Thu Apr 13 12:01:06 2006 +0200 (2006-04-13)
changeset 19428 43bfe55759b0
parent 19135 2de31ba562d7
child 19482 9f11af8f7ef9
permissions -rw-r--r--
tuned;
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
paulson@1526
    10
  type theory
wenzelm@16443
    11
  type theory_ref
wenzelm@16495
    12
  val sign_of: theory -> theory    (*obsolete*)
wenzelm@3996
    13
  val rep_theory: theory ->
wenzelm@16443
    14
   {axioms: term NameSpace.table,
wenzelm@17706
    15
    defs: Defs.T,
wenzelm@16443
    16
    oracles: ((theory * Object.T -> term) * stamp) NameSpace.table}
wenzelm@3996
    17
  val parents_of: theory -> theory list
wenzelm@4019
    18
  val ancestors_of: theory -> theory list
wenzelm@16443
    19
  val eq_thy: theory * theory -> bool
wenzelm@3996
    20
  val subthy: theory * theory -> bool
wenzelm@16443
    21
  val cert_axm: theory -> string * term -> string * term
wenzelm@16443
    22
  val read_def_axm: theory * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@6311
    23
    string list -> string * string -> string * term
wenzelm@16443
    24
  val read_axm: theory -> string * string -> string * term
wenzelm@16443
    25
  val inferT_axm: theory -> string * term -> string * term
wenzelm@3767
    26
end
wenzelm@3767
    27
wenzelm@3767
    28
signature THEORY =
wenzelm@3767
    29
sig
wenzelm@3767
    30
  include BASIC_THEORY
wenzelm@16443
    31
  include SIGN_THEORY
wenzelm@16495
    32
  val begin_theory: string -> theory list -> theory
wenzelm@16495
    33
  val end_theory: theory -> theory
wenzelm@16495
    34
  val checkpoint: theory -> theory
wenzelm@16495
    35
  val copy: theory -> theory
wenzelm@16536
    36
  val init_data: theory -> theory
wenzelm@16443
    37
  val axiom_space: theory -> NameSpace.T
wenzelm@16443
    38
  val oracle_space: theory -> NameSpace.T
wenzelm@16339
    39
  val axioms_of: theory -> (string * term) list
wenzelm@16339
    40
  val all_axioms_of: theory -> (string * term) list
wenzelm@17706
    41
  val defs_of : theory -> Defs.T
wenzelm@16443
    42
  val self_ref: theory -> theory_ref
wenzelm@16443
    43
  val deref: theory_ref -> theory
wenzelm@16443
    44
  val merge: theory * theory -> theory                     (*exception TERM*)
wenzelm@16443
    45
  val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
wenzelm@16443
    46
  val requires: theory -> string -> string -> unit
wenzelm@16443
    47
  val assert_super: theory -> theory -> theory
wenzelm@3996
    48
  val add_axioms: (bstring * string) list -> theory -> theory
wenzelm@3996
    49
  val add_axioms_i: (bstring * term) list -> theory -> theory
wenzelm@9320
    50
  val add_defs: bool -> (bstring * string) list -> theory -> theory
wenzelm@9320
    51
  val add_defs_i: bool -> (bstring * term) list -> theory -> theory
wenzelm@16443
    52
  val add_finals: bool -> string list -> theory -> theory
wenzelm@16443
    53
  val add_finals_i: bool -> term list -> theory -> theory
wenzelm@16443
    54
  val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
wenzelm@16495
    55
end
paulson@1526
    56
wenzelm@16443
    57
structure Theory: THEORY =
wenzelm@16443
    58
struct
wenzelm@16443
    59
wenzelm@16443
    60
(** type theory **)
wenzelm@16443
    61
wenzelm@16443
    62
(* context operations *)
wenzelm@16443
    63
wenzelm@16443
    64
type theory = Context.theory;
wenzelm@16443
    65
type theory_ref = Context.theory_ref;
wenzelm@16443
    66
wenzelm@16443
    67
val eq_thy = Context.eq_thy;
wenzelm@16443
    68
val subthy = Context.subthy;
paulson@1526
    69
wenzelm@16443
    70
val parents_of = Context.parents_of;
wenzelm@16443
    71
val ancestors_of = Context.ancestors_of;
wenzelm@16443
    72
wenzelm@16443
    73
val self_ref = Context.self_ref;
wenzelm@16443
    74
val deref = Context.deref;
wenzelm@16443
    75
val merge = Context.merge;
wenzelm@16443
    76
val merge_refs = Context.merge_refs;
wenzelm@16443
    77
wenzelm@16495
    78
val begin_theory = Sign.local_path oo Context.begin_thy Sign.pp;
wenzelm@16495
    79
val end_theory = Context.finish_thy;
wenzelm@16495
    80
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    81
val copy = Context.copy_thy;
wenzelm@16495
    82
wenzelm@16443
    83
wenzelm@16443
    84
(* signature operations *)
wenzelm@16443
    85
wenzelm@16443
    86
val sign_of = I;
wenzelm@16443
    87
wenzelm@16443
    88
structure SignTheory: SIGN_THEORY = Sign;
wenzelm@16443
    89
open SignTheory;
wenzelm@16443
    90
wenzelm@2206
    91
wenzelm@3996
    92
wenzelm@16443
    93
(** datatype thy **)
wenzelm@16443
    94
wenzelm@16443
    95
datatype thy = Thy of
wenzelm@16443
    96
 {axioms: term NameSpace.table,
wenzelm@17706
    97
  defs: Defs.T,
wenzelm@16443
    98
  oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
wenzelm@16443
    99
wenzelm@16443
   100
fun make_thy (axioms, defs, oracles) =
wenzelm@16443
   101
  Thy {axioms = axioms, defs = defs, oracles = oracles};
paulson@1526
   102
wenzelm@16443
   103
fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
wenzelm@16443
   104
fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
wenzelm@3996
   105
wenzelm@16443
   106
structure ThyData = TheoryDataFun
wenzelm@16443
   107
(struct
wenzelm@16536
   108
  val name = "Pure/theory";
wenzelm@16443
   109
  type T = thy;
wenzelm@16443
   110
  val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
wenzelm@16443
   111
  val copy = I;
wenzelm@16443
   112
wenzelm@16443
   113
  fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
wenzelm@16443
   114
wenzelm@16443
   115
  fun merge pp (thy1, thy2) =
wenzelm@16443
   116
    let
wenzelm@16443
   117
      val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
wenzelm@16443
   118
      val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
paulson@1526
   119
wenzelm@16443
   120
      val axioms = NameSpace.empty_table;
wenzelm@17706
   121
      val defs = Defs.merge pp (defs1, defs2);
haftmann@17496
   122
      val oracles = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
wenzelm@16443
   123
        handle Symtab.DUPS dups => err_dup_oras dups;
wenzelm@16443
   124
    in make_thy (axioms, defs, oracles) end;
wenzelm@16443
   125
wenzelm@16443
   126
  fun print _ _ = ();
wenzelm@16443
   127
end);
wenzelm@16443
   128
wenzelm@16536
   129
val init_data = ThyData.init;
wenzelm@16443
   130
wenzelm@16443
   131
fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
wenzelm@16443
   132
wenzelm@16443
   133
fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
wenzelm@16443
   134
  make_thy (f (axioms, defs, oracles)));
wenzelm@16443
   135
wenzelm@16443
   136
fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
wenzelm@16443
   137
fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
wenzelm@16443
   138
fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
wenzelm@16443
   139
wenzelm@16443
   140
wenzelm@16443
   141
(* basic operations *)
wenzelm@16443
   142
wenzelm@16443
   143
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@16443
   144
val oracle_space = #1 o #oracles o rep_theory;
wenzelm@3996
   145
wenzelm@16339
   146
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@16339
   147
fun all_axioms_of thy = List.concat (map axioms_of (thy :: ancestors_of thy));
wenzelm@16339
   148
wenzelm@16803
   149
val defs_of = #defs o rep_theory;
obua@16743
   150
paulson@4970
   151
fun requires thy name what =
wenzelm@16443
   152
  if Context.exists_name name thy then ()
wenzelm@4846
   153
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
paulson@1526
   154
wenzelm@6369
   155
fun assert_super thy1 thy2 =
wenzelm@6369
   156
  if subthy (thy1, thy2) then thy2
wenzelm@6369
   157
  else raise THEORY ("Not a super theory", [thy1, thy2]);
wenzelm@6369
   158
wenzelm@3996
   159
wenzelm@6311
   160
wenzelm@3814
   161
(** add axioms **)
wenzelm@3814
   162
paulson@1526
   163
(* prepare axioms *)
paulson@1526
   164
wenzelm@18678
   165
fun err_in_axm msg name =
wenzelm@18678
   166
  cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   167
wenzelm@16443
   168
fun cert_axm thy (name, raw_tm) =
paulson@1526
   169
  let
wenzelm@18968
   170
    val (t, T, _) = Sign.certify_prop thy raw_tm
wenzelm@2979
   171
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   172
        | TERM (msg, _) => error msg;
paulson@1526
   173
  in
wenzelm@9537
   174
    Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
wenzelm@18968
   175
    (name, Sign.no_vars (Sign.pp thy) t)
wenzelm@9629
   176
  end;
paulson@1526
   177
wenzelm@16443
   178
fun read_def_axm (thy, types, sorts) used (name, str) =
wenzelm@3814
   179
  let
wenzelm@18857
   180
    val ts = Syntax.read (Context.Theory thy) (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
wenzelm@18968
   181
    val (t, _) =
wenzelm@18968
   182
      Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy) types sorts used true (ts, propT);
wenzelm@16443
   183
  in cert_axm thy (name, t) end
wenzelm@18678
   184
  handle ERROR msg => err_in_axm msg name;
paulson@1526
   185
wenzelm@16443
   186
fun read_axm thy name_str = read_def_axm (thy, K NONE, K NONE) [] name_str;
wenzelm@5057
   187
wenzelm@16443
   188
fun inferT_axm thy (name, pre_tm) =
wenzelm@16291
   189
  let
wenzelm@16443
   190
    val pp = Sign.pp thy;
wenzelm@18968
   191
    val (t, _) =
wenzelm@18968
   192
      Sign.infer_types pp thy (Sign.consts_of thy) (K NONE) (K NONE) [] true ([pre_tm], propT);
wenzelm@18943
   193
  in (name, Sign.no_vars pp t) end
wenzelm@18678
   194
  handle ERROR msg => err_in_axm msg name;
paulson@1526
   195
paulson@1526
   196
wenzelm@16443
   197
(* add_axioms(_i) *)
paulson@1526
   198
wenzelm@16291
   199
local
wenzelm@16291
   200
wenzelm@16443
   201
fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
paulson@1526
   202
  let
wenzelm@16991
   203
    val axms = map (apsnd (Compress.term thy o Logic.varify) o prep_axm thy) raw_axms;
wenzelm@16443
   204
    val axioms' = NameSpace.extend_table (Sign.naming_of thy) (axioms, axms)
wenzelm@16443
   205
      handle Symtab.DUPS dups => err_dup_axms dups;
wenzelm@16443
   206
  in axioms' end);
paulson@1526
   207
wenzelm@16291
   208
in
wenzelm@16291
   209
wenzelm@16291
   210
val add_axioms = gen_add_axioms read_axm;
wenzelm@16291
   211
val add_axioms_i = gen_add_axioms cert_axm;
wenzelm@16291
   212
wenzelm@16291
   213
end;
paulson@1526
   214
paulson@1526
   215
wenzelm@3767
   216
wenzelm@3767
   217
(** add constant definitions **)
wenzelm@3767
   218
wenzelm@17706
   219
fun prep_const thy (c, T) = (c, Compress.typ thy (Type.varifyT T));
wenzelm@17706
   220
wenzelm@17706
   221
wenzelm@16944
   222
(* check_overloading *)
nipkow@9280
   223
wenzelm@16944
   224
fun check_overloading thy overloaded (c, T) =
wenzelm@16291
   225
  let
wenzelm@16944
   226
    val declT =
wenzelm@16944
   227
      (case Sign.const_constraint thy c of
wenzelm@16944
   228
        NONE => error ("Undeclared constant " ^ quote c)
wenzelm@16944
   229
      | SOME declT => declT);
wenzelm@16944
   230
    val T' = Type.varifyT T;
wenzelm@16944
   231
wenzelm@16944
   232
    fun message txt =
wenzelm@16944
   233
      [Pretty.block [Pretty.str "Specification of constant ",
wenzelm@16944
   234
        Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy T)],
wenzelm@16944
   235
        Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
wenzelm@16291
   236
  in
wenzelm@16944
   237
    if Sign.typ_instance thy (declT, T') then ()
wenzelm@16944
   238
    else if Type.raw_instance (declT, T') then
wenzelm@16944
   239
      error (Library.setmp show_sorts true
wenzelm@16944
   240
        message "imposes additional sort constraints on the constant declaration")
wenzelm@16944
   241
    else if overloaded then ()
wenzelm@16944
   242
    else warning (message "is strictly less general than the declared type");
wenzelm@16944
   243
    (c, T)
nipkow@9280
   244
  end;
nipkow@9280
   245
wenzelm@3767
   246
wenzelm@16291
   247
(* check_def *)
wenzelm@16291
   248
wenzelm@16443
   249
fun check_def thy overloaded (bname, tm) defs =
wenzelm@16291
   250
  let
wenzelm@16443
   251
    val pp = Sign.pp thy;
wenzelm@16443
   252
    fun prt_const (c, T) =
wenzelm@16443
   253
     [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@16443
   254
      Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
wenzelm@16883
   255
wenzelm@17706
   256
    val name = Sign.full_name thy bname;
wenzelm@18943
   257
    val (lhs_const, rhs) = Sign.cert_def pp tm;
wenzelm@16944
   258
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@18943
   259
    val _ = check_overloading thy overloaded lhs_const;
wenzelm@3767
   260
  in
wenzelm@17706
   261
    defs |> Defs.define (Sign.the_const_type thy)
haftmann@19135
   262
      ((true, Context.theory_name thy), name) (prep_const thy lhs_const)
haftmann@19135
   263
      (map (prep_const thy) rhs_consts)
wenzelm@16883
   264
  end
wenzelm@18678
   265
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@16883
   266
   [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
wenzelm@16883
   267
    Pretty.fbrk, Pretty.quote (Pretty.term (Sign.pp thy) tm)]));
wenzelm@3767
   268
wenzelm@3767
   269
wenzelm@16443
   270
(* add_defs(_i) *)
wenzelm@3767
   271
wenzelm@16291
   272
local
wenzelm@9320
   273
wenzelm@16291
   274
fun gen_add_defs prep_axm overloaded raw_axms thy =
wenzelm@16443
   275
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   276
    thy
wenzelm@16443
   277
    |> map_defs (fold (check_def thy overloaded) axms)
wenzelm@9320
   278
    |> add_axioms_i axms
wenzelm@3767
   279
  end;
wenzelm@3767
   280
wenzelm@16291
   281
in
wenzelm@16291
   282
wenzelm@16291
   283
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   284
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   285
wenzelm@16291
   286
end;
wenzelm@3767
   287
wenzelm@3767
   288
wenzelm@16443
   289
(* add_finals(_i) *)
skalberg@14223
   290
wenzelm@16291
   291
local
wenzelm@16291
   292
wenzelm@17706
   293
fun gen_add_finals prep_term overloaded args thy =
skalberg@14223
   294
  let
wenzelm@17706
   295
    fun const_of (Const const) = const
wenzelm@17706
   296
      | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
wenzelm@17706
   297
      | const_of _ = error "Attempt to finalize non-constant term";
wenzelm@19428
   298
    fun specify (c, T) = Defs.define (Sign.the_const_type thy)
wenzelm@19428
   299
      ((false, Context.theory_name thy), c ^ " axiom") (prep_const thy (c, T)) [];
wenzelm@17706
   300
    val finalize = specify o check_overloading thy overloaded o
wenzelm@18943
   301
      const_of o Sign.no_vars (Sign.pp thy) o prep_term thy;
wenzelm@17706
   302
  in thy |> map_defs (fold finalize args) end;
wenzelm@16291
   303
skalberg@14223
   304
in
wenzelm@16291
   305
wenzelm@17706
   306
val add_finals = gen_add_finals Sign.read_term;
wenzelm@17706
   307
val add_finals_i = gen_add_finals Sign.cert_term;
wenzelm@16291
   308
skalberg@14223
   309
end;
skalberg@14223
   310
skalberg@14223
   311
wenzelm@3878
   312
wenzelm@16443
   313
(** add oracle **)
wenzelm@3814
   314
wenzelm@16443
   315
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
wenzelm@16443
   316
  NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
wenzelm@16443
   317
    handle Symtab.DUPS dups => err_dup_oras dups);
wenzelm@3885
   318
paulson@1526
   319
end;
paulson@1526
   320
wenzelm@3767
   321
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   322
open BasicTheory;