src/Pure/theory.ML
author wenzelm
Wed Jun 29 15:13:30 2005 +0200 (2005-06-29)
changeset 16600 55ffcee3b8f3
parent 16536 c5744af6b28a
child 16743 21dbff595bf6
permissions -rw-r--r--
Syntax.read thy;
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@16443
    15
    defs: Defs.graph,
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@16443
    41
  val self_ref: theory -> theory_ref
wenzelm@16443
    42
  val deref: theory_ref -> theory
wenzelm@16443
    43
  val merge: theory * theory -> theory                     (*exception TERM*)
wenzelm@16443
    44
  val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
wenzelm@16443
    45
  val requires: theory -> string -> string -> unit
wenzelm@16443
    46
  val assert_super: theory -> theory -> theory
wenzelm@3996
    47
  val add_axioms: (bstring * string) list -> theory -> theory
wenzelm@3996
    48
  val add_axioms_i: (bstring * term) list -> theory -> theory
wenzelm@9320
    49
  val add_defs: bool -> (bstring * string) list -> theory -> theory
wenzelm@9320
    50
  val add_defs_i: bool -> (bstring * term) list -> theory -> theory
wenzelm@16443
    51
  val add_finals: bool -> string list -> theory -> theory
wenzelm@16443
    52
  val add_finals_i: bool -> term list -> theory -> theory
wenzelm@16443
    53
  val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
wenzelm@16495
    54
end
paulson@1526
    55
wenzelm@16443
    56
structure Theory: THEORY =
wenzelm@16443
    57
struct
wenzelm@16443
    58
wenzelm@16443
    59
(** type theory **)
wenzelm@16443
    60
wenzelm@16443
    61
(* context operations *)
wenzelm@16443
    62
wenzelm@16443
    63
type theory = Context.theory;
wenzelm@16443
    64
type theory_ref = Context.theory_ref;
wenzelm@16443
    65
wenzelm@16443
    66
val eq_thy = Context.eq_thy;
wenzelm@16443
    67
val subthy = Context.subthy;
paulson@1526
    68
wenzelm@16443
    69
val parents_of = Context.parents_of;
wenzelm@16443
    70
val ancestors_of = Context.ancestors_of;
wenzelm@16443
    71
wenzelm@16443
    72
val self_ref = Context.self_ref;
wenzelm@16443
    73
val deref = Context.deref;
wenzelm@16443
    74
val merge = Context.merge;
wenzelm@16443
    75
val merge_refs = Context.merge_refs;
wenzelm@16443
    76
wenzelm@16495
    77
val begin_theory = Sign.local_path oo Context.begin_thy Sign.pp;
wenzelm@16495
    78
val end_theory = Context.finish_thy;
wenzelm@16495
    79
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    80
val copy = Context.copy_thy;
wenzelm@16495
    81
wenzelm@16443
    82
wenzelm@16443
    83
(* signature operations *)
wenzelm@16443
    84
wenzelm@16443
    85
val sign_of = I;
wenzelm@16443
    86
wenzelm@16443
    87
structure SignTheory: SIGN_THEORY = Sign;
wenzelm@16443
    88
open SignTheory;
wenzelm@16443
    89
wenzelm@2206
    90
wenzelm@3996
    91
wenzelm@16443
    92
(** diagnostics **)  (* FIXME belongs to defs.ML *)
wenzelm@16443
    93
wenzelm@16443
    94
fun pretty_const pp (c, T) =
wenzelm@16443
    95
 [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@16443
    96
  Pretty.quote (Pretty.typ pp (Type.freeze_type T))];    (* FIXME zero indexes!? *)
wenzelm@16443
    97
wenzelm@16443
    98
fun pretty_path pp path = fold_rev (fn (T, c, def) =>
wenzelm@16443
    99
  fn [] => [Pretty.block (pretty_const pp (c, T))]
wenzelm@16443
   100
   | prts => Pretty.block (pretty_const pp (c, T) @
wenzelm@16443
   101
      [Pretty.brk 1, Pretty.str ("depends via " ^ quote def ^ " on")]) :: prts) path [];
wenzelm@16443
   102
wenzelm@16443
   103
fun chain_history_msg s =    (* FIXME huh!? *)
wenzelm@16443
   104
  if Defs.chain_history () then s ^ ": "
wenzelm@16443
   105
  else s ^ " (set DEFS_CHAIN_HISTORY=ON for full history): ";
wenzelm@16443
   106
wenzelm@16443
   107
fun defs_circular pp path =
wenzelm@16443
   108
  Pretty.str (chain_history_msg "Cyclic dependency of definitions") :: pretty_path pp path
wenzelm@16443
   109
  |> Pretty.chunks |> Pretty.string_of;
paulson@1526
   110
wenzelm@16443
   111
fun defs_infinite_chain pp path =
wenzelm@16443
   112
  Pretty.str (chain_history_msg "Infinite chain of definitions") :: pretty_path pp path
wenzelm@16443
   113
  |> Pretty.chunks |> Pretty.string_of;
wenzelm@16443
   114
wenzelm@16443
   115
fun defs_clash def1 def2 = "Type clash in definitions " ^ quote def1 ^ " and " ^ quote def2;
wenzelm@16443
   116
wenzelm@16443
   117
fun defs_final pp const =
wenzelm@16443
   118
  (Pretty.str "Attempt to define final constant" :: Pretty.brk 1 :: pretty_const pp const)
wenzelm@16443
   119
  |> Pretty.block |> Pretty.string_of;
wenzelm@16443
   120
wenzelm@16443
   121
wenzelm@16443
   122
wenzelm@16443
   123
(** datatype thy **)
wenzelm@16443
   124
wenzelm@16443
   125
datatype thy = Thy of
wenzelm@16443
   126
 {axioms: term NameSpace.table,
wenzelm@16443
   127
  defs: Defs.graph,
wenzelm@16443
   128
  oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
wenzelm@16443
   129
wenzelm@16443
   130
fun make_thy (axioms, defs, oracles) =
wenzelm@16443
   131
  Thy {axioms = axioms, defs = defs, oracles = oracles};
paulson@1526
   132
wenzelm@16443
   133
fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
wenzelm@16443
   134
fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
wenzelm@3996
   135
wenzelm@16443
   136
structure ThyData = TheoryDataFun
wenzelm@16443
   137
(struct
wenzelm@16536
   138
  val name = "Pure/theory";
wenzelm@16443
   139
  type T = thy;
wenzelm@16443
   140
  val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
wenzelm@16443
   141
  val copy = I;
wenzelm@16443
   142
wenzelm@16443
   143
  fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
wenzelm@16443
   144
wenzelm@16443
   145
  fun merge pp (thy1, thy2) =
wenzelm@16443
   146
    let
wenzelm@16443
   147
      val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
wenzelm@16443
   148
      val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
paulson@1526
   149
wenzelm@16443
   150
      val axioms = NameSpace.empty_table;
wenzelm@16443
   151
      val defs = Defs.merge defs1 defs2  (* FIXME produce errors in defs.ML *)
wenzelm@16443
   152
        handle Defs.CIRCULAR namess => error (defs_circular pp namess)
wenzelm@16443
   153
          | Defs.INFINITE_CHAIN namess => error (defs_infinite_chain pp namess);
wenzelm@16443
   154
      val oracles = NameSpace.merge_tables eq_snd (oracles1, oracles2)
wenzelm@16443
   155
        handle Symtab.DUPS dups => err_dup_oras dups;
wenzelm@16443
   156
    in make_thy (axioms, defs, oracles) end;
wenzelm@16443
   157
wenzelm@16443
   158
  fun print _ _ = ();
wenzelm@16443
   159
end);
wenzelm@16443
   160
wenzelm@16536
   161
val init_data = ThyData.init;
wenzelm@16443
   162
wenzelm@16443
   163
fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
wenzelm@16443
   164
wenzelm@16443
   165
fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
wenzelm@16443
   166
  make_thy (f (axioms, defs, oracles)));
wenzelm@16443
   167
wenzelm@16443
   168
fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
wenzelm@16443
   169
fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
wenzelm@16443
   170
fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
wenzelm@16443
   171
wenzelm@16443
   172
wenzelm@16443
   173
(* basic operations *)
wenzelm@16443
   174
wenzelm@16443
   175
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@16443
   176
val oracle_space = #1 o #oracles o rep_theory;
wenzelm@3996
   177
wenzelm@16339
   178
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@16339
   179
fun all_axioms_of thy = List.concat (map axioms_of (thy :: ancestors_of thy));
wenzelm@16339
   180
paulson@4970
   181
fun requires thy name what =
wenzelm@16443
   182
  if Context.exists_name name thy then ()
wenzelm@4846
   183
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
paulson@1526
   184
wenzelm@6369
   185
fun assert_super thy1 thy2 =
wenzelm@6369
   186
  if subthy (thy1, thy2) then thy2
wenzelm@6369
   187
  else raise THEORY ("Not a super theory", [thy1, thy2]);
wenzelm@6369
   188
wenzelm@3996
   189
wenzelm@6311
   190
wenzelm@3814
   191
(** add axioms **)
wenzelm@3814
   192
paulson@1526
   193
(* prepare axioms *)
paulson@1526
   194
paulson@1526
   195
fun err_in_axm name =
paulson@1526
   196
  error ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   197
wenzelm@16291
   198
fun no_vars pp tm =
wenzelm@16291
   199
  (case (Term.term_vars tm, Term.term_tvars tm) of
berghofe@14184
   200
    ([], []) => tm
berghofe@14184
   201
  | (ts, ixns) => error (Pretty.string_of (Pretty.block (Pretty.breaks
berghofe@14184
   202
      (Pretty.str "Illegal schematic variable(s) in term:" ::
wenzelm@16291
   203
       map (Pretty.term pp) ts @ map (Pretty.typ pp o TVar) ixns)))));
paulson@1526
   204
wenzelm@16443
   205
fun cert_axm thy (name, raw_tm) =
paulson@1526
   206
  let
wenzelm@16443
   207
    val pp = Sign.pp thy;
wenzelm@16443
   208
    val (t, T, _) = Sign.certify_term pp thy raw_tm
wenzelm@2979
   209
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   210
        | TERM (msg, _) => error msg;
paulson@1526
   211
  in
wenzelm@9537
   212
    Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
paulson@1526
   213
    assert (T = propT) "Term not of type prop";
wenzelm@16291
   214
    (name, no_vars pp t)
wenzelm@9629
   215
  end;
paulson@1526
   216
wenzelm@16443
   217
fun read_def_axm (thy, types, sorts) used (name, str) =
wenzelm@3814
   218
  let
wenzelm@16600
   219
    val ts = Syntax.read thy (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
wenzelm@16443
   220
    val (t, _) = Sign.infer_types (Sign.pp thy) thy types sorts used true (ts, propT);
wenzelm@16443
   221
  in cert_axm thy (name, t) end
paulson@1960
   222
  handle ERROR => err_in_axm name;
paulson@1526
   223
wenzelm@16443
   224
fun read_axm thy name_str = read_def_axm (thy, K NONE, K NONE) [] name_str;
wenzelm@5057
   225
wenzelm@16443
   226
fun inferT_axm thy (name, pre_tm) =
wenzelm@16291
   227
  let
wenzelm@16443
   228
    val pp = Sign.pp thy;
wenzelm@16443
   229
    val (t, _) = Sign.infer_types pp thy (K NONE) (K NONE) [] true ([pre_tm], propT);
wenzelm@16291
   230
  in (name, no_vars pp t) end
paulson@1526
   231
  handle ERROR => err_in_axm name;
paulson@1526
   232
paulson@1526
   233
wenzelm@16443
   234
(* add_axioms(_i) *)
paulson@1526
   235
wenzelm@16291
   236
local
wenzelm@16291
   237
wenzelm@16443
   238
fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
paulson@1526
   239
  let
wenzelm@16443
   240
    val axms = map (apsnd (Term.compress_term o Logic.varify) o prep_axm thy) raw_axms;
wenzelm@16443
   241
    val axioms' = NameSpace.extend_table (Sign.naming_of thy) (axioms, axms)
wenzelm@16443
   242
      handle Symtab.DUPS dups => err_dup_axms dups;
wenzelm@16443
   243
  in axioms' end);
paulson@1526
   244
wenzelm@16291
   245
in
wenzelm@16291
   246
wenzelm@16291
   247
val add_axioms = gen_add_axioms read_axm;
wenzelm@16291
   248
val add_axioms_i = gen_add_axioms cert_axm;
wenzelm@16291
   249
wenzelm@16291
   250
end;
paulson@1526
   251
paulson@1526
   252
wenzelm@3767
   253
wenzelm@3767
   254
(** add constant definitions **)
wenzelm@3767
   255
wenzelm@9320
   256
(* overloading *)
nipkow@9280
   257
wenzelm@16291
   258
datatype overloading = Clean | Implicit | Useless;
wenzelm@9320
   259
wenzelm@16443
   260
fun overloading thy overloaded declT defT =
wenzelm@16291
   261
  let
wenzelm@16291
   262
    val defT' = Term.incr_tvar (maxidx_of_typ declT + 1) (Type.varifyT defT);
wenzelm@16291
   263
  in
wenzelm@16443
   264
    if Sign.typ_instance thy (declT, defT') then Clean
wenzelm@16443
   265
    else if Sign.typ_instance thy (Type.strip_sorts declT, Type.strip_sorts defT') then Useless
wenzelm@16291
   266
    else if overloaded then Clean
wenzelm@16291
   267
    else Implicit
nipkow@9280
   268
  end;
nipkow@9280
   269
wenzelm@3767
   270
wenzelm@16291
   271
(* dest_def *)
wenzelm@16291
   272
wenzelm@16291
   273
fun dest_def pp tm =
wenzelm@3767
   274
  let
wenzelm@3787
   275
    fun err msg = raise TERM (msg, [tm]);
wenzelm@3767
   276
wenzelm@3767
   277
    val (lhs, rhs) = Logic.dest_equals (Logic.strip_imp_concl tm)
wenzelm@3767
   278
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@16291
   279
    val (head, args) = Term.strip_comb lhs;
wenzelm@16291
   280
    val (c, T) = Term.dest_Const head
wenzelm@3767
   281
      handle TERM _ => err "Head of lhs not a constant";
wenzelm@3767
   282
wenzelm@4141
   283
    fun dest_free (Free (x, _)) = x
wenzelm@4141
   284
      | dest_free (Const ("TYPE", Type ("itself", [TFree (x, _)]))) = x
wenzelm@4141
   285
      | dest_free _ = raise Match;
wenzelm@4141
   286
wenzelm@16291
   287
    val show_terms = commas_quote o map (Pretty.string_of_term pp);
wenzelm@4141
   288
    val show_frees = commas_quote o map dest_free;
wenzelm@3767
   289
    val show_tfrees = commas_quote o map fst;
wenzelm@3767
   290
wenzelm@16291
   291
    val lhs_nofrees = filter (not o can dest_free) args;
wenzelm@3767
   292
    val lhs_dups = duplicates args;
wenzelm@16291
   293
    val rhs_extras = term_frees rhs |> fold (remove op =) args;
wenzelm@16291
   294
    val rhs_extrasT = term_tfrees rhs |> fold (remove op =) (typ_tfrees T);
wenzelm@3767
   295
  in
wenzelm@16291
   296
    if not (null lhs_nofrees) then
wenzelm@16291
   297
      err ("Non-variables as arguments on lhs: " ^ show_terms lhs_nofrees)
wenzelm@3767
   298
    else if not (null lhs_dups) then
wenzelm@3767
   299
      err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
wenzelm@3767
   300
    else if not (null rhs_extras) then
wenzelm@3767
   301
      err ("Extra variables on rhs: " ^ show_frees rhs_extras)
wenzelm@3767
   302
    else if not (null rhs_extrasT) then
wenzelm@3767
   303
      err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
wenzelm@16291
   304
    else if exists_Const (equal (c, T)) rhs then
wenzelm@3767
   305
      err ("Constant to be defined occurs on rhs")
wenzelm@16291
   306
    else ((c, T), rhs)
wenzelm@3767
   307
  end;
wenzelm@3767
   308
wenzelm@3767
   309
wenzelm@16291
   310
(* check_def *)
wenzelm@16291
   311
wenzelm@16443
   312
fun declare thy c defs =      (* FIXME move to defs.ML *)
wenzelm@16443
   313
  let val T = Sign.the_const_type thy c
wenzelm@16291
   314
  in if_none (try (Defs.declare defs) (c, T)) defs end;
wenzelm@10494
   315
wenzelm@16443
   316
fun check_def thy overloaded (bname, tm) defs =
wenzelm@16291
   317
  let
wenzelm@16443
   318
    val pp = Sign.pp thy;
wenzelm@16291
   319
wenzelm@16291
   320
    fun err msg = error (Pretty.string_of (Pretty.chunks
wenzelm@16443
   321
     [Pretty.str msg, Pretty.block
wenzelm@16443
   322
      [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
wenzelm@16291
   323
        Pretty.fbrk, Pretty.quote (Pretty.term pp tm)]]));
wenzelm@16291
   324
wenzelm@16443
   325
    fun prt_const (c, T) =
wenzelm@16443
   326
     [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@16443
   327
      Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
wenzelm@16443
   328
    fun string_of_def const txt =
wenzelm@16291
   329
      [Pretty.block (Pretty.str "Definition of " :: pretty_const pp const), Pretty.str txt]
wenzelm@16291
   330
      |> Pretty.chunks |> Pretty.string_of;
wenzelm@9320
   331
wenzelm@16291
   332
    val ((c, defT), rhs) = dest_def pp tm handle TERM (msg, _) => err msg;
wenzelm@16291
   333
    val rhs_consts = Term.term_constsT rhs;
wenzelm@16443
   334
    val declT = Sign.the_const_type thy c;
obua@16108
   335
wenzelm@16291
   336
    val _ =
wenzelm@16443
   337
      (case overloading thy overloaded declT defT of
wenzelm@16291
   338
        Clean => ()
wenzelm@16443
   339
      | Implicit => warning (string_of_def (c, defT)
wenzelm@16443
   340
          ("is strictly less general than the declared type (see " ^ quote bname ^ ")"))
wenzelm@16443
   341
      | Useless => err (Library.setmp show_sorts true (string_of_def (c, defT))
wenzelm@16291
   342
          "imposes additional sort constraints on the declared type of the constant"));
obua@16108
   343
wenzelm@16443
   344
    val decl_defs = defs |> declare thy c |> fold (declare thy) (map #1 rhs_consts);
wenzelm@3767
   345
  in
wenzelm@16443
   346
    Defs.define decl_defs (c, defT) (Sign.full_name thy bname) rhs_consts
wenzelm@16443
   347
      (* FIXME produce errors in defs.ML *)
wenzelm@16291
   348
      handle Defs.DEFS msg => err ("DEFS: " ^ msg)   (* FIXME sys_error!? *)
wenzelm@16291
   349
        | Defs.CIRCULAR path => err (defs_circular pp path)
wenzelm@16291
   350
        | Defs.INFINITE_CHAIN path => err (defs_infinite_chain pp path)
wenzelm@16291
   351
        | Defs.CLASH (_, def1, def2) => err (defs_clash def1 def2)
wenzelm@16291
   352
        | Defs.FINAL const => err (defs_final pp const)
wenzelm@3767
   353
  end;
wenzelm@3767
   354
wenzelm@3767
   355
wenzelm@16443
   356
(* add_defs(_i) *)
wenzelm@3767
   357
wenzelm@16291
   358
local
wenzelm@9320
   359
wenzelm@16291
   360
fun gen_add_defs prep_axm overloaded raw_axms thy =
wenzelm@16443
   361
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   362
    thy
wenzelm@16443
   363
    |> map_defs (fold (check_def thy overloaded) axms)
wenzelm@9320
   364
    |> add_axioms_i axms
wenzelm@3767
   365
  end;
wenzelm@3767
   366
wenzelm@16291
   367
in
wenzelm@16291
   368
wenzelm@16291
   369
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   370
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   371
wenzelm@16291
   372
end;
wenzelm@3767
   373
wenzelm@3767
   374
wenzelm@16443
   375
(* add_finals(_i) *)
skalberg@14223
   376
wenzelm@16291
   377
local
wenzelm@16291
   378
wenzelm@16291
   379
fun gen_add_finals prep_term overloaded raw_terms thy =
skalberg@14223
   380
  let
wenzelm@16291
   381
    fun finalize tm finals =
wenzelm@16291
   382
      let
wenzelm@16291
   383
        fun err msg = raise TERM (msg, [tm]);    (* FIXME error!? *)
wenzelm@16291
   384
        val (c, defT) =
wenzelm@16291
   385
          (case tm of Const x => x
wenzelm@16291
   386
          | Free _ => err "Attempt to finalize variable (or undeclared constant)"
wenzelm@16291
   387
          | _ => err "Attempt to finalize non-constant term");
wenzelm@16443
   388
        val declT = Sign.the_const_type thy c
wenzelm@16291
   389
          handle TYPE (msg, _, _) => err msg;
wenzelm@16443
   390
        val _ =    (* FIXME unify messages with defs *)
wenzelm@16443
   391
          (case overloading thy overloaded declT defT of
wenzelm@16291
   392
            Clean => ()
wenzelm@16443
   393
          | Implicit => warning ("Finalizing " ^ quote c ^
wenzelm@16291
   394
              " at a strictly less general type than declared")
wenzelm@16291
   395
          | Useless => err "Sort constraints stronger than declared");
wenzelm@16443
   396
      in Defs.finalize (if_none (try (Defs.declare finals) (c, declT)) finals) (c, defT) end;
wenzelm@16443
   397
  in thy |> map_defs (fold finalize (map (prep_term thy) raw_terms)) end;
skalberg@14223
   398
wenzelm@16443
   399
fun read_term thy = Sign.simple_read_term thy TypeInfer.logicT;
wenzelm@16443
   400
fun cert_term thy = #1 o Sign.certify_term (Sign.pp thy) thy;
wenzelm@16291
   401
skalberg@14223
   402
in
wenzelm@16291
   403
wenzelm@16291
   404
val add_finals = gen_add_finals read_term;
wenzelm@16291
   405
val add_finals_i = gen_add_finals cert_term;
wenzelm@16291
   406
skalberg@14223
   407
end;
skalberg@14223
   408
skalberg@14223
   409
wenzelm@3878
   410
wenzelm@16443
   411
(** add oracle **)
wenzelm@3814
   412
wenzelm@16443
   413
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
wenzelm@16443
   414
  NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
wenzelm@16443
   415
    handle Symtab.DUPS dups => err_dup_oras dups);
wenzelm@3885
   416
paulson@1526
   417
end;
paulson@1526
   418
wenzelm@3767
   419
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   420
open BasicTheory;