src/Pure/theory.ML
author wenzelm
Tue Jul 19 17:21:55 2005 +0200 (2005-07-19)
changeset 16883 a89fafe1cbd8
parent 16803 014090d1e64b
child 16944 83ea7e3c6ec9
permissions -rw-r--r--
tuned defs interface;
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@9320
   226
(* overloading *)
nipkow@9280
   227
wenzelm@16291
   228
datatype overloading = Clean | Implicit | Useless;
wenzelm@9320
   229
wenzelm@16443
   230
fun overloading thy overloaded declT defT =
wenzelm@16291
   231
  let
wenzelm@16883
   232
    val defT' = Logic.incr_tvar (maxidx_of_typ declT + 1) (Type.varifyT defT);
wenzelm@16291
   233
  in
wenzelm@16443
   234
    if Sign.typ_instance thy (declT, defT') then Clean
wenzelm@16443
   235
    else if Sign.typ_instance thy (Type.strip_sorts declT, Type.strip_sorts defT') then Useless
wenzelm@16291
   236
    else if overloaded then Clean
wenzelm@16291
   237
    else Implicit
nipkow@9280
   238
  end;
nipkow@9280
   239
wenzelm@3767
   240
wenzelm@16291
   241
(* dest_def *)
wenzelm@16291
   242
wenzelm@16291
   243
fun dest_def pp tm =
wenzelm@3767
   244
  let
wenzelm@3787
   245
    fun err msg = raise TERM (msg, [tm]);
wenzelm@3767
   246
wenzelm@3767
   247
    val (lhs, rhs) = Logic.dest_equals (Logic.strip_imp_concl tm)
wenzelm@3767
   248
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@16291
   249
    val (head, args) = Term.strip_comb lhs;
wenzelm@16291
   250
    val (c, T) = Term.dest_Const head
wenzelm@3767
   251
      handle TERM _ => err "Head of lhs not a constant";
wenzelm@3767
   252
wenzelm@4141
   253
    fun dest_free (Free (x, _)) = x
wenzelm@4141
   254
      | dest_free (Const ("TYPE", Type ("itself", [TFree (x, _)]))) = x
wenzelm@4141
   255
      | dest_free _ = raise Match;
wenzelm@4141
   256
wenzelm@16291
   257
    val show_terms = commas_quote o map (Pretty.string_of_term pp);
wenzelm@4141
   258
    val show_frees = commas_quote o map dest_free;
wenzelm@3767
   259
    val show_tfrees = commas_quote o map fst;
wenzelm@3767
   260
wenzelm@16291
   261
    val lhs_nofrees = filter (not o can dest_free) args;
wenzelm@3767
   262
    val lhs_dups = duplicates args;
wenzelm@16291
   263
    val rhs_extras = term_frees rhs |> fold (remove op =) args;
wenzelm@16291
   264
    val rhs_extrasT = term_tfrees rhs |> fold (remove op =) (typ_tfrees T);
wenzelm@3767
   265
  in
wenzelm@16291
   266
    if not (null lhs_nofrees) then
wenzelm@16291
   267
      err ("Non-variables as arguments on lhs: " ^ show_terms lhs_nofrees)
wenzelm@3767
   268
    else if not (null lhs_dups) then
wenzelm@3767
   269
      err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
wenzelm@3767
   270
    else if not (null rhs_extras) then
wenzelm@3767
   271
      err ("Extra variables on rhs: " ^ show_frees rhs_extras)
wenzelm@3767
   272
    else if not (null rhs_extrasT) then
wenzelm@3767
   273
      err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
wenzelm@16291
   274
    else if exists_Const (equal (c, T)) rhs then
wenzelm@3767
   275
      err ("Constant to be defined occurs on rhs")
wenzelm@16291
   276
    else ((c, T), rhs)
wenzelm@3767
   277
  end;
wenzelm@3767
   278
wenzelm@3767
   279
wenzelm@16291
   280
(* check_def *)
wenzelm@16291
   281
wenzelm@16883
   282
fun pretty_const pp (c, T) =
wenzelm@16883
   283
 [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@16883
   284
  Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
wenzelm@10494
   285
wenzelm@16443
   286
fun check_def thy overloaded (bname, tm) defs =
wenzelm@16291
   287
  let
wenzelm@16443
   288
    val pp = Sign.pp thy;
wenzelm@16443
   289
    fun prt_const (c, T) =
wenzelm@16443
   290
     [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@16443
   291
      Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
wenzelm@16443
   292
    fun string_of_def const txt =
wenzelm@16291
   293
      [Pretty.block (Pretty.str "Definition of " :: pretty_const pp const), Pretty.str txt]
wenzelm@16291
   294
      |> Pretty.chunks |> Pretty.string_of;
wenzelm@9320
   295
wenzelm@16883
   296
    fun typed_const c = (c, Sign.the_const_type thy c);
wenzelm@16883
   297
wenzelm@16883
   298
    val ((c, defT), rhs) = dest_def pp tm handle TERM (msg, _) => error msg;
wenzelm@16291
   299
    val rhs_consts = Term.term_constsT rhs;
wenzelm@16443
   300
    val declT = Sign.the_const_type thy c;
wenzelm@16291
   301
    val _ =
wenzelm@16443
   302
      (case overloading thy overloaded declT defT of
wenzelm@16291
   303
        Clean => ()
wenzelm@16443
   304
      | Implicit => warning (string_of_def (c, defT)
wenzelm@16443
   305
          ("is strictly less general than the declared type (see " ^ quote bname ^ ")"))
wenzelm@16883
   306
      | Useless => error (Library.setmp show_sorts true (string_of_def (c, defT))
wenzelm@16291
   307
          "imposes additional sort constraints on the declared type of the constant"));
wenzelm@3767
   308
  in
wenzelm@16883
   309
    defs |> Defs.declare (typed_const c) |> fold (Defs.declare o typed_const o #1) rhs_consts
wenzelm@16883
   310
    |> Defs.define pp (c, defT) (Sign.full_name thy bname) rhs_consts
wenzelm@16883
   311
  end
wenzelm@16883
   312
  handle ERROR => error (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@16291
   340
fun gen_add_finals prep_term overloaded raw_terms thy =
skalberg@14223
   341
  let
wenzelm@16291
   342
    fun finalize tm finals =
wenzelm@16291
   343
      let
wenzelm@16291
   344
        fun err msg = raise TERM (msg, [tm]);    (* FIXME error!? *)
wenzelm@16291
   345
        val (c, defT) =
wenzelm@16291
   346
          (case tm of Const x => x
wenzelm@16291
   347
          | Free _ => err "Attempt to finalize variable (or undeclared constant)"
wenzelm@16291
   348
          | _ => err "Attempt to finalize non-constant term");
wenzelm@16443
   349
        val declT = Sign.the_const_type thy c
wenzelm@16291
   350
          handle TYPE (msg, _, _) => err msg;
wenzelm@16443
   351
        val _ =    (* FIXME unify messages with defs *)
wenzelm@16443
   352
          (case overloading thy overloaded declT defT of
wenzelm@16291
   353
            Clean => ()
wenzelm@16443
   354
          | Implicit => warning ("Finalizing " ^ quote c ^
wenzelm@16291
   355
              " at a strictly less general type than declared")
wenzelm@16291
   356
          | Useless => err "Sort constraints stronger than declared");
wenzelm@16883
   357
      in finals |> Defs.declare (c, declT) |> Defs.finalize (c, defT) end;
wenzelm@16443
   358
  in thy |> map_defs (fold finalize (map (prep_term thy) raw_terms)) end;
skalberg@14223
   359
wenzelm@16443
   360
fun read_term thy = Sign.simple_read_term thy TypeInfer.logicT;
wenzelm@16443
   361
fun cert_term thy = #1 o Sign.certify_term (Sign.pp thy) thy;
wenzelm@16291
   362
skalberg@14223
   363
in
wenzelm@16291
   364
wenzelm@16291
   365
val add_finals = gen_add_finals read_term;
wenzelm@16291
   366
val add_finals_i = gen_add_finals cert_term;
wenzelm@16291
   367
skalberg@14223
   368
end;
skalberg@14223
   369
skalberg@14223
   370
wenzelm@3878
   371
wenzelm@16443
   372
(** add oracle **)
wenzelm@3814
   373
wenzelm@16443
   374
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
wenzelm@16443
   375
  NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
wenzelm@16443
   376
    handle Symtab.DUPS dups => err_dup_oras dups);
wenzelm@3885
   377
paulson@1526
   378
end;
paulson@1526
   379
wenzelm@3767
   380
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   381
open BasicTheory;