src/Pure/theory.ML
author wenzelm
Fri Aug 03 16:28:15 2007 +0200 (2007-08-03)
changeset 24137 8d7896398147
parent 23655 d2d1138e0ddc
child 24199 8be734b5f59f
permissions -rw-r--r--
replaced Theory.self_ref by Theory.check_thy, which now produces a checked ref;
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
wenzelm@16443
    10
  val eq_thy: theory * theory -> bool
wenzelm@3996
    11
  val subthy: theory * theory -> bool
wenzelm@3767
    12
end
wenzelm@3767
    13
wenzelm@3767
    14
signature THEORY =
wenzelm@3767
    15
sig
wenzelm@3767
    16
  include BASIC_THEORY
wenzelm@16443
    17
  include SIGN_THEORY
wenzelm@22684
    18
  val parents_of: theory -> theory list
wenzelm@22684
    19
  val ancestors_of: theory -> theory list
wenzelm@16495
    20
  val begin_theory: string -> theory list -> theory
wenzelm@16495
    21
  val end_theory: theory -> theory
wenzelm@16495
    22
  val checkpoint: theory -> theory
wenzelm@16495
    23
  val copy: theory -> theory
wenzelm@22684
    24
  val rep_theory: theory ->
wenzelm@22684
    25
   {axioms: term NameSpace.table,
wenzelm@22684
    26
    defs: Defs.T,
wenzelm@22684
    27
    oracles: ((theory * Object.T -> term) * stamp) NameSpace.table}
wenzelm@16443
    28
  val axiom_space: theory -> NameSpace.T
wenzelm@22684
    29
  val axiom_table: theory -> term Symtab.table
wenzelm@16443
    30
  val oracle_space: theory -> NameSpace.T
wenzelm@22684
    31
  val oracle_table: theory -> ((theory * Object.T -> term) * stamp) Symtab.table
wenzelm@16339
    32
  val axioms_of: theory -> (string * term) list
wenzelm@16339
    33
  val all_axioms_of: theory -> (string * term) list
wenzelm@17706
    34
  val defs_of : theory -> Defs.T
wenzelm@24137
    35
  val check_thy: theory -> theory_ref
wenzelm@16443
    36
  val deref: theory_ref -> theory
wenzelm@23600
    37
  val merge: theory * theory -> theory
wenzelm@23600
    38
  val merge_refs: theory_ref * theory_ref -> theory_ref
wenzelm@23600
    39
  val merge_list: theory list -> theory
wenzelm@16443
    40
  val requires: theory -> string -> string -> unit
wenzelm@16443
    41
  val assert_super: theory -> theory -> theory
wenzelm@22684
    42
  val cert_axm: theory -> string * term -> string * term
wenzelm@22684
    43
  val read_axm: theory -> string * string -> string * term
wenzelm@3996
    44
  val add_axioms: (bstring * string) list -> theory -> theory
wenzelm@3996
    45
  val add_axioms_i: (bstring * term) list -> theory -> theory
wenzelm@19708
    46
  val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
wenzelm@19630
    47
  val add_defs: bool -> bool -> (bstring * string) list -> theory -> theory
wenzelm@19630
    48
  val add_defs_i: bool -> bool -> (bstring * term) list -> theory -> theory
wenzelm@16443
    49
  val add_finals: bool -> string list -> theory -> theory
wenzelm@16443
    50
  val add_finals_i: bool -> term list -> theory -> theory
wenzelm@16443
    51
  val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
wenzelm@16495
    52
end
paulson@1526
    53
wenzelm@16443
    54
structure Theory: THEORY =
wenzelm@16443
    55
struct
wenzelm@16443
    56
wenzelm@19708
    57
wenzelm@16443
    58
(** type theory **)
wenzelm@16443
    59
wenzelm@16443
    60
(* context operations *)
wenzelm@16443
    61
wenzelm@16443
    62
val eq_thy = Context.eq_thy;
wenzelm@16443
    63
val subthy = Context.subthy;
paulson@1526
    64
wenzelm@16443
    65
val parents_of = Context.parents_of;
wenzelm@16443
    66
val ancestors_of = Context.ancestors_of;
wenzelm@16443
    67
wenzelm@24137
    68
val check_thy = Context.check_thy;
wenzelm@16443
    69
val deref = Context.deref;
wenzelm@16443
    70
val merge = Context.merge;
wenzelm@16443
    71
val merge_refs = Context.merge_refs;
wenzelm@16443
    72
wenzelm@23600
    73
fun merge_list [] = raise THEORY ("Empty merge of theories", [])
wenzelm@21608
    74
  | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
wenzelm@21608
    75
wenzelm@16495
    76
val begin_theory = Sign.local_path oo Context.begin_thy Sign.pp;
wenzelm@16495
    77
val end_theory = Context.finish_thy;
wenzelm@16495
    78
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    79
val copy = Context.copy_thy;
wenzelm@16495
    80
wenzelm@16443
    81
wenzelm@16443
    82
(* signature operations *)
wenzelm@16443
    83
wenzelm@16443
    84
structure SignTheory: SIGN_THEORY = Sign;
wenzelm@16443
    85
open SignTheory;
wenzelm@16443
    86
wenzelm@2206
    87
wenzelm@3996
    88
wenzelm@16443
    89
(** datatype thy **)
wenzelm@16443
    90
wenzelm@16443
    91
datatype thy = Thy of
wenzelm@16443
    92
 {axioms: term NameSpace.table,
wenzelm@17706
    93
  defs: Defs.T,
wenzelm@16443
    94
  oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
wenzelm@16443
    95
wenzelm@16443
    96
fun make_thy (axioms, defs, oracles) =
wenzelm@16443
    97
  Thy {axioms = axioms, defs = defs, oracles = oracles};
paulson@1526
    98
wenzelm@23655
    99
fun err_dup_axm dup = error ("Duplicate axiom: " ^ quote dup);
wenzelm@23655
   100
fun err_dup_ora dup = error ("Duplicate oracle: " ^ quote dup);
wenzelm@3996
   101
wenzelm@16443
   102
structure ThyData = TheoryDataFun
wenzelm@22846
   103
(
wenzelm@16443
   104
  type T = thy;
wenzelm@16443
   105
  val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
wenzelm@16443
   106
  val copy = I;
wenzelm@16443
   107
wenzelm@16443
   108
  fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
wenzelm@16443
   109
wenzelm@16443
   110
  fun merge pp (thy1, thy2) =
wenzelm@16443
   111
    let
wenzelm@16443
   112
      val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
wenzelm@16443
   113
      val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
paulson@1526
   114
wenzelm@16443
   115
      val axioms = NameSpace.empty_table;
wenzelm@19693
   116
      val defs = Defs.merge pp (defs1, defs2);
haftmann@17496
   117
      val oracles = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
wenzelm@23655
   118
        handle Symtab.DUP dup => err_dup_ora dup;
wenzelm@16443
   119
    in make_thy (axioms, defs, oracles) end;
wenzelm@22846
   120
);
wenzelm@16443
   121
wenzelm@16443
   122
fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
wenzelm@16443
   123
wenzelm@16443
   124
fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
wenzelm@16443
   125
  make_thy (f (axioms, defs, oracles)));
wenzelm@16443
   126
wenzelm@16443
   127
fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
wenzelm@16443
   128
fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
wenzelm@16443
   129
fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
wenzelm@16443
   130
wenzelm@16443
   131
wenzelm@16443
   132
(* basic operations *)
wenzelm@16443
   133
wenzelm@16443
   134
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@22684
   135
val axiom_table = #2 o #axioms o rep_theory;
wenzelm@22684
   136
wenzelm@16443
   137
val oracle_space = #1 o #oracles o rep_theory;
wenzelm@22684
   138
val oracle_table = #2 o #oracles o rep_theory;
wenzelm@3996
   139
wenzelm@16339
   140
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@19482
   141
fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
wenzelm@16339
   142
wenzelm@16803
   143
val defs_of = #defs o rep_theory;
obua@16743
   144
paulson@4970
   145
fun requires thy name what =
wenzelm@16443
   146
  if Context.exists_name name thy then ()
wenzelm@4846
   147
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
paulson@1526
   148
wenzelm@6369
   149
fun assert_super thy1 thy2 =
wenzelm@6369
   150
  if subthy (thy1, thy2) then thy2
wenzelm@6369
   151
  else raise THEORY ("Not a super theory", [thy1, thy2]);
wenzelm@6369
   152
wenzelm@3996
   153
wenzelm@6311
   154
wenzelm@3814
   155
(** add axioms **)
wenzelm@3814
   156
paulson@1526
   157
(* prepare axioms *)
paulson@1526
   158
wenzelm@18678
   159
fun err_in_axm msg name =
wenzelm@18678
   160
  cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   161
wenzelm@16443
   162
fun cert_axm thy (name, raw_tm) =
paulson@1526
   163
  let
wenzelm@18968
   164
    val (t, T, _) = Sign.certify_prop thy raw_tm
wenzelm@2979
   165
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   166
        | TERM (msg, _) => error msg;
paulson@1526
   167
  in
wenzelm@9537
   168
    Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
wenzelm@18968
   169
    (name, Sign.no_vars (Sign.pp thy) t)
wenzelm@9629
   170
  end;
paulson@1526
   171
wenzelm@22684
   172
fun read_axm thy (name, str) =
wenzelm@22689
   173
  cert_axm thy (name, Sign.read_prop thy str)
wenzelm@22689
   174
    handle ERROR msg => err_in_axm msg name;
paulson@1526
   175
paulson@1526
   176
wenzelm@16443
   177
(* add_axioms(_i) *)
paulson@1526
   178
wenzelm@16291
   179
local
wenzelm@16291
   180
wenzelm@16443
   181
fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
paulson@1526
   182
  let
wenzelm@16991
   183
    val axms = map (apsnd (Compress.term thy o Logic.varify) o prep_axm thy) raw_axms;
haftmann@23086
   184
    val axioms' = NameSpace.extend_table (Sign.naming_of thy) axms axioms
wenzelm@23655
   185
      handle Symtab.DUP dup => err_dup_axm dup;
wenzelm@16443
   186
  in axioms' end);
paulson@1526
   187
wenzelm@16291
   188
in
wenzelm@16291
   189
wenzelm@16291
   190
val add_axioms = gen_add_axioms read_axm;
wenzelm@16291
   191
val add_axioms_i = gen_add_axioms cert_axm;
wenzelm@16291
   192
wenzelm@16291
   193
end;
paulson@1526
   194
paulson@1526
   195
wenzelm@3767
   196
wenzelm@3767
   197
(** add constant definitions **)
wenzelm@3767
   198
wenzelm@19708
   199
(* dependencies *)
wenzelm@19708
   200
wenzelm@19708
   201
fun dependencies thy unchecked is_def name lhs rhs =
wenzelm@19708
   202
  let
wenzelm@19708
   203
    val pp = Sign.pp thy;
wenzelm@19708
   204
    val consts = Sign.consts_of thy;
wenzelm@19727
   205
    fun prep const =
wenzelm@19727
   206
      let val Const (c, T) = Sign.no_vars pp (Const const)
wenzelm@19806
   207
      in (c, Consts.typargs consts (c, Compress.typ thy (Logic.varifyT T))) end;
wenzelm@19708
   208
wenzelm@19708
   209
    val lhs_vars = Term.add_tfreesT (#2 lhs) [];
wenzelm@19708
   210
    val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
wenzelm@19708
   211
      if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
wenzelm@19708
   212
    val _ =
wenzelm@19708
   213
      if null rhs_extras then ()
wenzelm@19708
   214
      else error ("Specification depends on extra type variables: " ^
wenzelm@19708
   215
        commas_quote (map (Pretty.string_of_typ pp o TFree) rhs_extras) ^
wenzelm@19708
   216
        "\nThe error(s) above occurred in " ^ quote name);
wenzelm@19727
   217
  in Defs.define pp unchecked is_def (Context.theory_name thy) name (prep lhs) (map prep rhs) end;
wenzelm@19708
   218
wenzelm@19708
   219
fun add_deps a raw_lhs raw_rhs thy =
wenzelm@19708
   220
  let
wenzelm@19708
   221
    val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
wenzelm@19708
   222
    val name = if a = "" then (#1 lhs ^ " axiom") else a;
wenzelm@19708
   223
  in thy |> map_defs (dependencies thy false false name lhs rhs) end;
wenzelm@17706
   224
wenzelm@17706
   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@19806
   234
    val T' = Logic.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
(* check_def *)
wenzelm@16291
   252
wenzelm@19630
   253
fun check_def thy unchecked overloaded (bname, tm) defs =
wenzelm@16291
   254
  let
wenzelm@17706
   255
    val name = Sign.full_name thy bname;
wenzelm@19693
   256
    val (lhs_const, rhs) = Sign.cert_def (Sign.pp thy) tm;
wenzelm@16944
   257
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@18943
   258
    val _ = check_overloading thy overloaded lhs_const;
wenzelm@19708
   259
  in defs |> dependencies thy unchecked true name lhs_const rhs_consts end
wenzelm@18678
   260
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@16883
   261
   [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
wenzelm@19693
   262
    Pretty.fbrk, Pretty.quote (Sign.pretty_term thy tm)]));
wenzelm@3767
   263
wenzelm@3767
   264
wenzelm@16443
   265
(* add_defs(_i) *)
wenzelm@3767
   266
wenzelm@16291
   267
local
wenzelm@9320
   268
wenzelm@19630
   269
fun gen_add_defs prep_axm unchecked overloaded raw_axms thy =
wenzelm@16443
   270
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   271
    thy
wenzelm@19630
   272
    |> map_defs (fold (check_def thy unchecked overloaded) axms)
wenzelm@9320
   273
    |> add_axioms_i axms
wenzelm@3767
   274
  end;
wenzelm@3767
   275
wenzelm@16291
   276
in
wenzelm@16291
   277
wenzelm@16291
   278
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   279
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   280
wenzelm@16291
   281
end;
wenzelm@3767
   282
wenzelm@3767
   283
wenzelm@16443
   284
(* add_finals(_i) *)
skalberg@14223
   285
wenzelm@16291
   286
local
wenzelm@16291
   287
wenzelm@17706
   288
fun gen_add_finals prep_term overloaded args thy =
skalberg@14223
   289
  let
wenzelm@17706
   290
    fun const_of (Const const) = const
wenzelm@17706
   291
      | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
wenzelm@17706
   292
      | const_of _ = error "Attempt to finalize non-constant term";
wenzelm@19708
   293
    fun specify (c, T) = dependencies thy false false (c ^ " axiom") (c, T) [];
wenzelm@19708
   294
    val finalize = specify o check_overloading thy overloaded o const_of o prep_term thy;
wenzelm@17706
   295
  in thy |> map_defs (fold finalize args) end;
wenzelm@16291
   296
skalberg@14223
   297
in
wenzelm@16291
   298
wenzelm@17706
   299
val add_finals = gen_add_finals Sign.read_term;
wenzelm@17706
   300
val add_finals_i = gen_add_finals Sign.cert_term;
wenzelm@16291
   301
skalberg@14223
   302
end;
skalberg@14223
   303
skalberg@14223
   304
wenzelm@3878
   305
wenzelm@16443
   306
(** add oracle **)
wenzelm@3814
   307
wenzelm@16443
   308
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
haftmann@23086
   309
  NameSpace.extend_table (Sign.naming_of thy) [(bname, (oracle, stamp ()))] oracles
wenzelm@23655
   310
    handle Symtab.DUP dup => err_dup_ora dup);
wenzelm@3885
   311
paulson@1526
   312
end;
paulson@1526
   313
wenzelm@3767
   314
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   315
open BasicTheory;