src/Pure/theory.ML
author wenzelm
Sat Jan 14 17:14:06 2006 +0100 (2006-01-14)
changeset 18678 dd0c569fa43d
parent 18338 ed2d0e60fbcc
child 18763 e2b4ba340ff1
permissions -rw-r--r--
sane ERROR handling;
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
haftmann@17038
    48
  val dest_def: Pretty.pp -> term -> (string * typ) * term
wenzelm@3996
    49
  val add_axioms: (bstring * string) list -> theory -> theory
wenzelm@3996
    50
  val add_axioms_i: (bstring * term) list -> theory -> theory
wenzelm@9320
    51
  val add_defs: bool -> (bstring * string) list -> theory -> theory
wenzelm@9320
    52
  val add_defs_i: bool -> (bstring * term) list -> theory -> theory
wenzelm@16443
    53
  val add_finals: bool -> string list -> theory -> theory
wenzelm@16443
    54
  val add_finals_i: bool -> term list -> theory -> theory
wenzelm@16443
    55
  val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
wenzelm@16495
    56
end
paulson@1526
    57
wenzelm@16443
    58
structure Theory: THEORY =
wenzelm@16443
    59
struct
wenzelm@16443
    60
wenzelm@16443
    61
(** type theory **)
wenzelm@16443
    62
wenzelm@16443
    63
(* context operations *)
wenzelm@16443
    64
wenzelm@16443
    65
type theory = Context.theory;
wenzelm@16443
    66
type theory_ref = Context.theory_ref;
wenzelm@16443
    67
wenzelm@16443
    68
val eq_thy = Context.eq_thy;
wenzelm@16443
    69
val subthy = Context.subthy;
paulson@1526
    70
wenzelm@16443
    71
val parents_of = Context.parents_of;
wenzelm@16443
    72
val ancestors_of = Context.ancestors_of;
wenzelm@16443
    73
wenzelm@16443
    74
val self_ref = Context.self_ref;
wenzelm@16443
    75
val deref = Context.deref;
wenzelm@16443
    76
val merge = Context.merge;
wenzelm@16443
    77
val merge_refs = Context.merge_refs;
wenzelm@16443
    78
wenzelm@16495
    79
val begin_theory = Sign.local_path oo Context.begin_thy Sign.pp;
wenzelm@16495
    80
val end_theory = Context.finish_thy;
wenzelm@16495
    81
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    82
val copy = Context.copy_thy;
wenzelm@16495
    83
wenzelm@16443
    84
wenzelm@16443
    85
(* signature operations *)
wenzelm@16443
    86
wenzelm@16443
    87
val sign_of = I;
wenzelm@16443
    88
wenzelm@16443
    89
structure SignTheory: SIGN_THEORY = Sign;
wenzelm@16443
    90
open SignTheory;
wenzelm@16443
    91
wenzelm@2206
    92
wenzelm@3996
    93
wenzelm@16443
    94
(** datatype thy **)
wenzelm@16443
    95
wenzelm@16443
    96
datatype thy = Thy of
wenzelm@16443
    97
 {axioms: term NameSpace.table,
wenzelm@17706
    98
  defs: Defs.T,
wenzelm@16443
    99
  oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
wenzelm@16443
   100
wenzelm@16443
   101
fun make_thy (axioms, defs, oracles) =
wenzelm@16443
   102
  Thy {axioms = axioms, defs = defs, oracles = oracles};
paulson@1526
   103
wenzelm@16443
   104
fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
wenzelm@16443
   105
fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
wenzelm@3996
   106
wenzelm@16443
   107
structure ThyData = TheoryDataFun
wenzelm@16443
   108
(struct
wenzelm@16536
   109
  val name = "Pure/theory";
wenzelm@16443
   110
  type T = thy;
wenzelm@16443
   111
  val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
wenzelm@16443
   112
  val copy = I;
wenzelm@16443
   113
wenzelm@16443
   114
  fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
wenzelm@16443
   115
wenzelm@16443
   116
  fun merge pp (thy1, thy2) =
wenzelm@16443
   117
    let
wenzelm@16443
   118
      val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
wenzelm@16443
   119
      val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
paulson@1526
   120
wenzelm@16443
   121
      val axioms = NameSpace.empty_table;
wenzelm@17706
   122
      val defs = Defs.merge pp (defs1, defs2);
haftmann@17496
   123
      val oracles = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
wenzelm@16443
   124
        handle Symtab.DUPS dups => err_dup_oras dups;
wenzelm@16443
   125
    in make_thy (axioms, defs, oracles) end;
wenzelm@16443
   126
wenzelm@16443
   127
  fun print _ _ = ();
wenzelm@16443
   128
end);
wenzelm@16443
   129
wenzelm@16536
   130
val init_data = ThyData.init;
wenzelm@16443
   131
wenzelm@16443
   132
fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
wenzelm@16443
   133
wenzelm@16443
   134
fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
wenzelm@16443
   135
  make_thy (f (axioms, defs, oracles)));
wenzelm@16443
   136
wenzelm@16443
   137
fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
wenzelm@16443
   138
fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
wenzelm@16443
   139
fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
wenzelm@16443
   140
wenzelm@16443
   141
wenzelm@16443
   142
(* basic operations *)
wenzelm@16443
   143
wenzelm@16443
   144
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@16443
   145
val oracle_space = #1 o #oracles o rep_theory;
wenzelm@3996
   146
wenzelm@16339
   147
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@16339
   148
fun all_axioms_of thy = List.concat (map axioms_of (thy :: ancestors_of thy));
wenzelm@16339
   149
wenzelm@16803
   150
val defs_of = #defs o rep_theory;
obua@16743
   151
paulson@4970
   152
fun requires thy name what =
wenzelm@16443
   153
  if Context.exists_name name thy then ()
wenzelm@4846
   154
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
paulson@1526
   155
wenzelm@6369
   156
fun assert_super thy1 thy2 =
wenzelm@6369
   157
  if subthy (thy1, thy2) then thy2
wenzelm@6369
   158
  else raise THEORY ("Not a super theory", [thy1, thy2]);
wenzelm@6369
   159
wenzelm@3996
   160
wenzelm@6311
   161
wenzelm@3814
   162
(** add axioms **)
wenzelm@3814
   163
paulson@1526
   164
(* prepare axioms *)
paulson@1526
   165
wenzelm@18678
   166
fun err_in_axm msg name =
wenzelm@18678
   167
  cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   168
wenzelm@16291
   169
fun no_vars pp tm =
wenzelm@16291
   170
  (case (Term.term_vars tm, Term.term_tvars tm) of
berghofe@14184
   171
    ([], []) => tm
berghofe@14184
   172
  | (ts, ixns) => error (Pretty.string_of (Pretty.block (Pretty.breaks
berghofe@14184
   173
      (Pretty.str "Illegal schematic variable(s) in term:" ::
wenzelm@16291
   174
       map (Pretty.term pp) ts @ map (Pretty.typ pp o TVar) ixns)))));
paulson@1526
   175
wenzelm@16443
   176
fun cert_axm thy (name, raw_tm) =
paulson@1526
   177
  let
wenzelm@16443
   178
    val pp = Sign.pp thy;
wenzelm@16443
   179
    val (t, T, _) = Sign.certify_term pp thy raw_tm
wenzelm@2979
   180
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   181
        | TERM (msg, _) => error msg;
paulson@1526
   182
  in
wenzelm@9537
   183
    Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
paulson@1526
   184
    assert (T = propT) "Term not of type prop";
wenzelm@16291
   185
    (name, no_vars pp t)
wenzelm@9629
   186
  end;
paulson@1526
   187
wenzelm@16443
   188
fun read_def_axm (thy, types, sorts) used (name, str) =
wenzelm@3814
   189
  let
wenzelm@16600
   190
    val ts = Syntax.read thy (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
wenzelm@16443
   191
    val (t, _) = Sign.infer_types (Sign.pp thy) thy types sorts used true (ts, propT);
wenzelm@16443
   192
  in cert_axm thy (name, t) end
wenzelm@18678
   193
  handle ERROR msg => err_in_axm msg name;
paulson@1526
   194
wenzelm@16443
   195
fun read_axm thy name_str = read_def_axm (thy, K NONE, K NONE) [] name_str;
wenzelm@5057
   196
wenzelm@16443
   197
fun inferT_axm thy (name, pre_tm) =
wenzelm@16291
   198
  let
wenzelm@16443
   199
    val pp = Sign.pp thy;
wenzelm@16443
   200
    val (t, _) = Sign.infer_types pp thy (K NONE) (K NONE) [] true ([pre_tm], propT);
wenzelm@16291
   201
  in (name, 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
(* dest_def *)
wenzelm@16291
   256
wenzelm@16291
   257
fun dest_def pp tm =
wenzelm@3767
   258
  let
wenzelm@3787
   259
    fun err msg = raise TERM (msg, [tm]);
wenzelm@3767
   260
wenzelm@3767
   261
    val (lhs, rhs) = Logic.dest_equals (Logic.strip_imp_concl tm)
wenzelm@3767
   262
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@18338
   263
    val (head, args) = Term.strip_comb (Pattern.beta_eta_contract lhs);
wenzelm@16291
   264
    val (c, T) = Term.dest_Const head
wenzelm@3767
   265
      handle TERM _ => err "Head of lhs not a constant";
wenzelm@3767
   266
wenzelm@4141
   267
    fun dest_free (Free (x, _)) = x
wenzelm@4141
   268
      | dest_free (Const ("TYPE", Type ("itself", [TFree (x, _)]))) = x
wenzelm@4141
   269
      | dest_free _ = raise Match;
wenzelm@4141
   270
wenzelm@16291
   271
    val show_terms = commas_quote o map (Pretty.string_of_term pp);
wenzelm@4141
   272
    val show_frees = commas_quote o map dest_free;
wenzelm@3767
   273
    val show_tfrees = commas_quote o map fst;
wenzelm@3767
   274
wenzelm@16291
   275
    val lhs_nofrees = filter (not o can dest_free) args;
wenzelm@18139
   276
    val lhs_dups = gen_duplicates (op aconv) args;
wenzelm@18139
   277
    val rhs_extras = term_frees rhs |> fold (remove op aconv) args;
wenzelm@16291
   278
    val rhs_extrasT = term_tfrees rhs |> fold (remove op =) (typ_tfrees T);
wenzelm@3767
   279
  in
wenzelm@16291
   280
    if not (null lhs_nofrees) then
wenzelm@16291
   281
      err ("Non-variables as arguments on lhs: " ^ show_terms lhs_nofrees)
wenzelm@3767
   282
    else if not (null lhs_dups) then
wenzelm@3767
   283
      err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
wenzelm@3767
   284
    else if not (null rhs_extras) then
wenzelm@3767
   285
      err ("Extra variables on rhs: " ^ show_frees rhs_extras)
wenzelm@3767
   286
    else if not (null rhs_extrasT) then
wenzelm@3767
   287
      err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
wenzelm@16291
   288
    else if exists_Const (equal (c, T)) rhs then
wenzelm@3767
   289
      err ("Constant to be defined occurs on rhs")
wenzelm@16291
   290
    else ((c, T), rhs)
wenzelm@3767
   291
  end;
wenzelm@3767
   292
wenzelm@3767
   293
wenzelm@16291
   294
(* check_def *)
wenzelm@16291
   295
wenzelm@16443
   296
fun check_def thy overloaded (bname, tm) defs =
wenzelm@16291
   297
  let
wenzelm@16443
   298
    val pp = Sign.pp thy;
wenzelm@16443
   299
    fun prt_const (c, T) =
wenzelm@16443
   300
     [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@16443
   301
      Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
wenzelm@16883
   302
wenzelm@16944
   303
    val _ = no_vars pp tm;
wenzelm@17706
   304
    val name = Sign.full_name thy bname;
wenzelm@16944
   305
    val (const, rhs) = dest_def pp tm handle TERM (msg, _) => error msg;
wenzelm@16944
   306
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@16944
   307
    val _ = check_overloading thy overloaded const;
wenzelm@3767
   308
  in
wenzelm@17706
   309
    defs |> Defs.define (Sign.the_const_type thy)
wenzelm@17706
   310
      name (prep_const thy const) (map (prep_const thy) rhs_consts)
wenzelm@16883
   311
  end
wenzelm@18678
   312
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@16883
   313
   [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
wenzelm@16883
   314
    Pretty.fbrk, Pretty.quote (Pretty.term (Sign.pp thy) tm)]));
wenzelm@3767
   315
wenzelm@3767
   316
wenzelm@16443
   317
(* add_defs(_i) *)
wenzelm@3767
   318
wenzelm@16291
   319
local
wenzelm@9320
   320
wenzelm@16291
   321
fun gen_add_defs prep_axm overloaded raw_axms thy =
wenzelm@16443
   322
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   323
    thy
wenzelm@16443
   324
    |> map_defs (fold (check_def thy overloaded) axms)
wenzelm@9320
   325
    |> add_axioms_i axms
wenzelm@3767
   326
  end;
wenzelm@3767
   327
wenzelm@16291
   328
in
wenzelm@16291
   329
wenzelm@16291
   330
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   331
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   332
wenzelm@16291
   333
end;
wenzelm@3767
   334
wenzelm@3767
   335
wenzelm@16443
   336
(* add_finals(_i) *)
skalberg@14223
   337
wenzelm@16291
   338
local
wenzelm@16291
   339
wenzelm@17706
   340
fun gen_add_finals prep_term overloaded args thy =
skalberg@14223
   341
  let
wenzelm@17706
   342
    fun const_of (Const const) = const
wenzelm@17706
   343
      | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
wenzelm@17706
   344
      | const_of _ = error "Attempt to finalize non-constant term";
wenzelm@17706
   345
    fun specify (c, T) = Defs.define (Sign.the_const_type thy) (c ^ " axiom") (c, T) [];
wenzelm@17706
   346
    val finalize = specify o check_overloading thy overloaded o
wenzelm@17706
   347
      const_of o no_vars (Sign.pp thy) o prep_term thy;
wenzelm@17706
   348
  in thy |> map_defs (fold finalize args) end;
wenzelm@16291
   349
skalberg@14223
   350
in
wenzelm@16291
   351
wenzelm@17706
   352
val add_finals = gen_add_finals Sign.read_term;
wenzelm@17706
   353
val add_finals_i = gen_add_finals Sign.cert_term;
wenzelm@16291
   354
skalberg@14223
   355
end;
skalberg@14223
   356
skalberg@14223
   357
wenzelm@3878
   358
wenzelm@16443
   359
(** add oracle **)
wenzelm@3814
   360
wenzelm@16443
   361
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
wenzelm@16443
   362
  NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
wenzelm@16443
   363
    handle Symtab.DUPS dups => err_dup_oras dups);
wenzelm@3885
   364
paulson@1526
   365
end;
paulson@1526
   366
wenzelm@3767
   367
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   368
open BasicTheory;