src/Pure/theory.ML
author wenzelm
Thu Jul 28 15:20:01 2005 +0200 (2005-07-28)
changeset 16944 83ea7e3c6ec9
parent 16883 a89fafe1cbd8
child 16991 39f5760f72d7
permissions -rw-r--r--
check_overloading replaces datatype overloading;
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@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
obua@16743
    41
  val defs_of : theory -> Defs.graph
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@16443
    97
  defs: Defs.graph,
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@16883
   121
      val defs = Defs.merge pp defs1 defs2;
wenzelm@16443
   122
      val oracles = NameSpace.merge_tables eq_snd (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
paulson@1526
   165
fun err_in_axm name =
paulson@1526
   166
  error ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   167
wenzelm@16291
   168
fun no_vars pp tm =
wenzelm@16291
   169
  (case (Term.term_vars tm, Term.term_tvars tm) of
berghofe@14184
   170
    ([], []) => tm
berghofe@14184
   171
  | (ts, ixns) => error (Pretty.string_of (Pretty.block (Pretty.breaks
berghofe@14184
   172
      (Pretty.str "Illegal schematic variable(s) in term:" ::
wenzelm@16291
   173
       map (Pretty.term pp) ts @ map (Pretty.typ pp o TVar) ixns)))));
paulson@1526
   174
wenzelm@16443
   175
fun cert_axm thy (name, raw_tm) =
paulson@1526
   176
  let
wenzelm@16443
   177
    val pp = Sign.pp thy;
wenzelm@16443
   178
    val (t, T, _) = Sign.certify_term pp 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;
paulson@1526
   183
    assert (T = propT) "Term not of type prop";
wenzelm@16291
   184
    (name, no_vars pp t)
wenzelm@9629
   185
  end;
paulson@1526
   186
wenzelm@16443
   187
fun read_def_axm (thy, types, sorts) used (name, str) =
wenzelm@3814
   188
  let
wenzelm@16600
   189
    val ts = Syntax.read thy (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
wenzelm@16443
   190
    val (t, _) = Sign.infer_types (Sign.pp thy) thy types sorts used true (ts, propT);
wenzelm@16443
   191
  in cert_axm thy (name, t) end
paulson@1960
   192
  handle ERROR => err_in_axm 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@16443
   199
    val (t, _) = Sign.infer_types pp thy (K NONE) (K NONE) [] true ([pre_tm], propT);
wenzelm@16291
   200
  in (name, no_vars pp t) end
paulson@1526
   201
  handle ERROR => err_in_axm name;
paulson@1526
   202
paulson@1526
   203
wenzelm@16443
   204
(* add_axioms(_i) *)
paulson@1526
   205
wenzelm@16291
   206
local
wenzelm@16291
   207
wenzelm@16443
   208
fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
paulson@1526
   209
  let
wenzelm@16443
   210
    val axms = map (apsnd (Term.compress_term o Logic.varify) o prep_axm thy) raw_axms;
wenzelm@16443
   211
    val axioms' = NameSpace.extend_table (Sign.naming_of thy) (axioms, axms)
wenzelm@16443
   212
      handle Symtab.DUPS dups => err_dup_axms dups;
wenzelm@16443
   213
  in axioms' end);
paulson@1526
   214
wenzelm@16291
   215
in
wenzelm@16291
   216
wenzelm@16291
   217
val add_axioms = gen_add_axioms read_axm;
wenzelm@16291
   218
val add_axioms_i = gen_add_axioms cert_axm;
wenzelm@16291
   219
wenzelm@16291
   220
end;
paulson@1526
   221
paulson@1526
   222
wenzelm@3767
   223
wenzelm@3767
   224
(** add constant definitions **)
wenzelm@3767
   225
wenzelm@16944
   226
(* check_overloading *)
nipkow@9280
   227
wenzelm@16944
   228
fun check_overloading thy overloaded (c, T) =
wenzelm@16291
   229
  let
wenzelm@16944
   230
    val declT =
wenzelm@16944
   231
      (case Sign.const_constraint thy c of
wenzelm@16944
   232
        NONE => error ("Undeclared constant " ^ quote c)
wenzelm@16944
   233
      | SOME declT => declT);
wenzelm@16944
   234
    val T' = Type.varifyT T;
wenzelm@16944
   235
wenzelm@16944
   236
    fun message txt =
wenzelm@16944
   237
      [Pretty.block [Pretty.str "Specification of constant ",
wenzelm@16944
   238
        Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy T)],
wenzelm@16944
   239
        Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
wenzelm@16291
   240
  in
wenzelm@16944
   241
    if Sign.typ_instance thy (declT, T') then ()
wenzelm@16944
   242
    else if Type.raw_instance (declT, T') then
wenzelm@16944
   243
      error (Library.setmp show_sorts true
wenzelm@16944
   244
        message "imposes additional sort constraints on the constant declaration")
wenzelm@16944
   245
    else if overloaded then ()
wenzelm@16944
   246
    else warning (message "is strictly less general than the declared type");
wenzelm@16944
   247
    (c, T)
nipkow@9280
   248
  end;
nipkow@9280
   249
wenzelm@3767
   250
wenzelm@16291
   251
(* dest_def *)
wenzelm@16291
   252
wenzelm@16291
   253
fun dest_def pp tm =
wenzelm@3767
   254
  let
wenzelm@3787
   255
    fun err msg = raise TERM (msg, [tm]);
wenzelm@3767
   256
wenzelm@3767
   257
    val (lhs, rhs) = Logic.dest_equals (Logic.strip_imp_concl tm)
wenzelm@3767
   258
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@16291
   259
    val (head, args) = Term.strip_comb lhs;
wenzelm@16291
   260
    val (c, T) = Term.dest_Const head
wenzelm@3767
   261
      handle TERM _ => err "Head of lhs not a constant";
wenzelm@3767
   262
wenzelm@4141
   263
    fun dest_free (Free (x, _)) = x
wenzelm@4141
   264
      | dest_free (Const ("TYPE", Type ("itself", [TFree (x, _)]))) = x
wenzelm@4141
   265
      | dest_free _ = raise Match;
wenzelm@4141
   266
wenzelm@16291
   267
    val show_terms = commas_quote o map (Pretty.string_of_term pp);
wenzelm@4141
   268
    val show_frees = commas_quote o map dest_free;
wenzelm@3767
   269
    val show_tfrees = commas_quote o map fst;
wenzelm@3767
   270
wenzelm@16291
   271
    val lhs_nofrees = filter (not o can dest_free) args;
wenzelm@3767
   272
    val lhs_dups = duplicates args;
wenzelm@16291
   273
    val rhs_extras = term_frees rhs |> fold (remove op =) args;
wenzelm@16291
   274
    val rhs_extrasT = term_tfrees rhs |> fold (remove op =) (typ_tfrees T);
wenzelm@3767
   275
  in
wenzelm@16291
   276
    if not (null lhs_nofrees) then
wenzelm@16291
   277
      err ("Non-variables as arguments on lhs: " ^ show_terms lhs_nofrees)
wenzelm@3767
   278
    else if not (null lhs_dups) then
wenzelm@3767
   279
      err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
wenzelm@3767
   280
    else if not (null rhs_extras) then
wenzelm@3767
   281
      err ("Extra variables on rhs: " ^ show_frees rhs_extras)
wenzelm@3767
   282
    else if not (null rhs_extrasT) then
wenzelm@3767
   283
      err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
wenzelm@16291
   284
    else if exists_Const (equal (c, T)) rhs then
wenzelm@3767
   285
      err ("Constant to be defined occurs on rhs")
wenzelm@16291
   286
    else ((c, T), rhs)
wenzelm@3767
   287
  end;
wenzelm@3767
   288
wenzelm@3767
   289
wenzelm@16291
   290
(* check_def *)
wenzelm@16291
   291
wenzelm@16443
   292
fun check_def thy overloaded (bname, tm) defs =
wenzelm@16291
   293
  let
wenzelm@16443
   294
    val pp = Sign.pp thy;
wenzelm@16443
   295
    fun prt_const (c, T) =
wenzelm@16443
   296
     [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@16443
   297
      Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
wenzelm@16944
   298
    fun declared (c, _) = (c, Sign.the_const_type thy c);
wenzelm@16883
   299
wenzelm@16944
   300
    val _ = no_vars pp tm;
wenzelm@16944
   301
    val (const, rhs) = dest_def pp tm handle TERM (msg, _) => error msg;
wenzelm@16944
   302
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@16944
   303
    val _ = check_overloading thy overloaded const;
wenzelm@3767
   304
  in
wenzelm@16944
   305
    defs
wenzelm@16944
   306
    |> Defs.declare (declared const)
wenzelm@16944
   307
    |> fold (Defs.declare o declared) rhs_consts
wenzelm@16944
   308
    |> Defs.define pp const (Sign.full_name thy bname) rhs_consts
wenzelm@16883
   309
  end
wenzelm@16883
   310
  handle ERROR => error (Pretty.string_of (Pretty.block
wenzelm@16883
   311
   [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
wenzelm@16883
   312
    Pretty.fbrk, Pretty.quote (Pretty.term (Sign.pp thy) tm)]));
wenzelm@3767
   313
wenzelm@3767
   314
wenzelm@16443
   315
(* add_defs(_i) *)
wenzelm@3767
   316
wenzelm@16291
   317
local
wenzelm@9320
   318
wenzelm@16291
   319
fun gen_add_defs prep_axm overloaded raw_axms thy =
wenzelm@16443
   320
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   321
    thy
wenzelm@16443
   322
    |> map_defs (fold (check_def thy overloaded) axms)
wenzelm@9320
   323
    |> add_axioms_i axms
wenzelm@3767
   324
  end;
wenzelm@3767
   325
wenzelm@16291
   326
in
wenzelm@16291
   327
wenzelm@16291
   328
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   329
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   330
wenzelm@16291
   331
end;
wenzelm@3767
   332
wenzelm@3767
   333
wenzelm@16443
   334
(* add_finals(_i) *)
skalberg@14223
   335
wenzelm@16291
   336
local
wenzelm@16291
   337
wenzelm@16291
   338
fun gen_add_finals prep_term overloaded raw_terms thy =
skalberg@14223
   339
  let
wenzelm@16944
   340
    val pp = Sign.pp thy;
wenzelm@16291
   341
    fun finalize tm finals =
wenzelm@16291
   342
      let
wenzelm@16944
   343
        val _ = no_vars pp tm;
wenzelm@16944
   344
        val const as (c, _) =
wenzelm@16291
   345
          (case tm of Const x => x
wenzelm@16944
   346
          | Free _ => error "Attempt to finalize variable (or undeclared constant)"
wenzelm@16944
   347
          | _ => error "Attempt to finalize non-constant term")
wenzelm@16944
   348
          |> check_overloading thy overloaded;
wenzelm@16944
   349
      in finals |> Defs.declare (c, Sign.the_const_type thy c) |> Defs.finalize const end;
wenzelm@16443
   350
  in thy |> map_defs (fold finalize (map (prep_term thy) raw_terms)) end;
skalberg@14223
   351
wenzelm@16443
   352
fun read_term thy = Sign.simple_read_term thy TypeInfer.logicT;
wenzelm@16443
   353
fun cert_term thy = #1 o Sign.certify_term (Sign.pp thy) thy;
wenzelm@16291
   354
skalberg@14223
   355
in
wenzelm@16291
   356
wenzelm@16291
   357
val add_finals = gen_add_finals read_term;
wenzelm@16291
   358
val add_finals_i = gen_add_finals cert_term;
wenzelm@16291
   359
skalberg@14223
   360
end;
skalberg@14223
   361
skalberg@14223
   362
wenzelm@3878
   363
wenzelm@16443
   364
(** add oracle **)
wenzelm@3814
   365
wenzelm@16443
   366
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
wenzelm@16443
   367
  NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
wenzelm@16443
   368
    handle Symtab.DUPS dups => err_dup_oras dups);
wenzelm@3885
   369
paulson@1526
   370
end;
paulson@1526
   371
wenzelm@3767
   372
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   373
open BasicTheory;