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