src/Pure/theory.ML
author wenzelm
Wed May 24 01:05:02 2006 +0200 (2006-05-24 ago)
changeset 19708 a508bde37a81
parent 19700 e02af528ceef
child 19727 f5895f998402
permissions -rw-r--r--
added add_deps, which actually records dependencies of consts (unlike add_finals);
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@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@19570
    42
  val definitions_of: theory -> string ->
wenzelm@19570
    43
    {module: string, name: string, lhs: typ, rhs: (string * typ) list} list
wenzelm@16443
    44
  val self_ref: theory -> theory_ref
wenzelm@16443
    45
  val deref: theory_ref -> theory
wenzelm@16443
    46
  val merge: theory * theory -> theory                     (*exception TERM*)
wenzelm@16443
    47
  val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
wenzelm@16443
    48
  val requires: theory -> string -> string -> unit
wenzelm@16443
    49
  val assert_super: theory -> theory -> theory
wenzelm@3996
    50
  val add_axioms: (bstring * string) list -> theory -> theory
wenzelm@3996
    51
  val add_axioms_i: (bstring * term) list -> theory -> theory
wenzelm@19708
    52
  val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
wenzelm@19630
    53
  val add_defs: bool -> bool -> (bstring * string) list -> theory -> theory
wenzelm@19630
    54
  val add_defs_i: bool -> bool -> (bstring * term) list -> theory -> theory
wenzelm@16443
    55
  val add_finals: bool -> string list -> theory -> theory
wenzelm@16443
    56
  val add_finals_i: bool -> term list -> theory -> theory
wenzelm@16443
    57
  val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
wenzelm@16495
    58
end
paulson@1526
    59
wenzelm@16443
    60
structure Theory: THEORY =
wenzelm@16443
    61
struct
wenzelm@16443
    62
wenzelm@19708
    63
wenzelm@16443
    64
(** type theory **)
wenzelm@16443
    65
wenzelm@16443
    66
(* context operations *)
wenzelm@16443
    67
wenzelm@16443
    68
type theory = Context.theory;
wenzelm@16443
    69
type theory_ref = Context.theory_ref;
wenzelm@16443
    70
wenzelm@16443
    71
val eq_thy = Context.eq_thy;
wenzelm@16443
    72
val subthy = Context.subthy;
paulson@1526
    73
wenzelm@16443
    74
val parents_of = Context.parents_of;
wenzelm@16443
    75
val ancestors_of = Context.ancestors_of;
wenzelm@16443
    76
wenzelm@16443
    77
val self_ref = Context.self_ref;
wenzelm@16443
    78
val deref = Context.deref;
wenzelm@16443
    79
val merge = Context.merge;
wenzelm@16443
    80
val merge_refs = Context.merge_refs;
wenzelm@16443
    81
wenzelm@16495
    82
val begin_theory = Sign.local_path oo Context.begin_thy Sign.pp;
wenzelm@16495
    83
val end_theory = Context.finish_thy;
wenzelm@16495
    84
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    85
val copy = Context.copy_thy;
wenzelm@16495
    86
wenzelm@16443
    87
wenzelm@16443
    88
(* signature operations *)
wenzelm@16443
    89
wenzelm@16443
    90
val sign_of = I;
wenzelm@16443
    91
wenzelm@16443
    92
structure SignTheory: SIGN_THEORY = Sign;
wenzelm@16443
    93
open SignTheory;
wenzelm@16443
    94
wenzelm@2206
    95
wenzelm@3996
    96
wenzelm@16443
    97
(** datatype thy **)
wenzelm@16443
    98
wenzelm@16443
    99
datatype thy = Thy of
wenzelm@16443
   100
 {axioms: term NameSpace.table,
wenzelm@17706
   101
  defs: Defs.T,
wenzelm@16443
   102
  oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
wenzelm@16443
   103
wenzelm@16443
   104
fun make_thy (axioms, defs, oracles) =
wenzelm@16443
   105
  Thy {axioms = axioms, defs = defs, oracles = oracles};
paulson@1526
   106
wenzelm@16443
   107
fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
wenzelm@16443
   108
fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
wenzelm@3996
   109
wenzelm@16443
   110
structure ThyData = TheoryDataFun
wenzelm@16443
   111
(struct
wenzelm@16536
   112
  val name = "Pure/theory";
wenzelm@16443
   113
  type T = thy;
wenzelm@16443
   114
  val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
wenzelm@16443
   115
  val copy = I;
wenzelm@16443
   116
wenzelm@16443
   117
  fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
wenzelm@16443
   118
wenzelm@16443
   119
  fun merge pp (thy1, thy2) =
wenzelm@16443
   120
    let
wenzelm@16443
   121
      val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
wenzelm@16443
   122
      val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
paulson@1526
   123
wenzelm@16443
   124
      val axioms = NameSpace.empty_table;
wenzelm@19693
   125
      val defs = Defs.merge pp (defs1, defs2);
haftmann@17496
   126
      val oracles = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
wenzelm@16443
   127
        handle Symtab.DUPS dups => err_dup_oras dups;
wenzelm@16443
   128
    in make_thy (axioms, defs, oracles) end;
wenzelm@16443
   129
wenzelm@16443
   130
  fun print _ _ = ();
wenzelm@16443
   131
end);
wenzelm@16443
   132
wenzelm@16536
   133
val init_data = ThyData.init;
wenzelm@16443
   134
wenzelm@16443
   135
fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
wenzelm@16443
   136
wenzelm@16443
   137
fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
wenzelm@16443
   138
  make_thy (f (axioms, defs, oracles)));
wenzelm@16443
   139
wenzelm@16443
   140
fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
wenzelm@16443
   141
fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
wenzelm@16443
   142
fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
wenzelm@16443
   143
wenzelm@16443
   144
wenzelm@16443
   145
(* basic operations *)
wenzelm@16443
   146
wenzelm@16443
   147
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@16443
   148
val oracle_space = #1 o #oracles o rep_theory;
wenzelm@3996
   149
wenzelm@16339
   150
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@19482
   151
fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
wenzelm@16339
   152
wenzelm@16803
   153
val defs_of = #defs o rep_theory;
obua@16743
   154
wenzelm@19570
   155
fun definitions_of thy c =
wenzelm@19700
   156
  let
wenzelm@19700
   157
    val inst = Consts.instance (Sign.consts_of thy);
wenzelm@19700
   158
    val defs = defs_of thy;
wenzelm@19700
   159
  in
wenzelm@19700
   160
    Defs.specifications_of defs c
wenzelm@19700
   161
    |> map_filter (fn (_, {is_def, module, name, lhs, rhs}) =>
wenzelm@19700
   162
      if is_def then SOME {module = module, name = name, lhs = inst (c, lhs),
wenzelm@19700
   163
        rhs = map (fn (d, Us) => (d, inst (d, Us))) rhs}
wenzelm@19700
   164
      else NONE)
wenzelm@19700
   165
  end;
wenzelm@19570
   166
paulson@4970
   167
fun requires thy name what =
wenzelm@16443
   168
  if Context.exists_name name thy then ()
wenzelm@4846
   169
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
paulson@1526
   170
wenzelm@6369
   171
fun assert_super thy1 thy2 =
wenzelm@6369
   172
  if subthy (thy1, thy2) then thy2
wenzelm@6369
   173
  else raise THEORY ("Not a super theory", [thy1, thy2]);
wenzelm@6369
   174
wenzelm@3996
   175
wenzelm@6311
   176
wenzelm@3814
   177
(** add axioms **)
wenzelm@3814
   178
paulson@1526
   179
(* prepare axioms *)
paulson@1526
   180
wenzelm@18678
   181
fun err_in_axm msg name =
wenzelm@18678
   182
  cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   183
wenzelm@16443
   184
fun cert_axm thy (name, raw_tm) =
paulson@1526
   185
  let
wenzelm@18968
   186
    val (t, T, _) = Sign.certify_prop thy raw_tm
wenzelm@2979
   187
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   188
        | TERM (msg, _) => error msg;
paulson@1526
   189
  in
wenzelm@9537
   190
    Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
wenzelm@18968
   191
    (name, Sign.no_vars (Sign.pp thy) t)
wenzelm@9629
   192
  end;
paulson@1526
   193
wenzelm@16443
   194
fun read_def_axm (thy, types, sorts) used (name, str) =
wenzelm@3814
   195
  let
wenzelm@18857
   196
    val ts = Syntax.read (Context.Theory thy) (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
wenzelm@18968
   197
    val (t, _) =
wenzelm@18968
   198
      Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy) types sorts used true (ts, propT);
wenzelm@16443
   199
  in cert_axm thy (name, t) end
wenzelm@18678
   200
  handle ERROR msg => err_in_axm msg name;
paulson@1526
   201
wenzelm@16443
   202
fun read_axm thy name_str = read_def_axm (thy, K NONE, K NONE) [] name_str;
wenzelm@5057
   203
wenzelm@16443
   204
fun inferT_axm thy (name, pre_tm) =
wenzelm@16291
   205
  let
wenzelm@16443
   206
    val pp = Sign.pp thy;
wenzelm@18968
   207
    val (t, _) =
wenzelm@18968
   208
      Sign.infer_types pp thy (Sign.consts_of thy) (K NONE) (K NONE) [] true ([pre_tm], propT);
wenzelm@18943
   209
  in (name, Sign.no_vars pp t) end
wenzelm@18678
   210
  handle ERROR msg => err_in_axm msg name;
paulson@1526
   211
paulson@1526
   212
wenzelm@16443
   213
(* add_axioms(_i) *)
paulson@1526
   214
wenzelm@16291
   215
local
wenzelm@16291
   216
wenzelm@16443
   217
fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
paulson@1526
   218
  let
wenzelm@16991
   219
    val axms = map (apsnd (Compress.term thy o Logic.varify) o prep_axm thy) raw_axms;
wenzelm@16443
   220
    val axioms' = NameSpace.extend_table (Sign.naming_of thy) (axioms, axms)
wenzelm@16443
   221
      handle Symtab.DUPS dups => err_dup_axms dups;
wenzelm@16443
   222
  in axioms' end);
paulson@1526
   223
wenzelm@16291
   224
in
wenzelm@16291
   225
wenzelm@16291
   226
val add_axioms = gen_add_axioms read_axm;
wenzelm@16291
   227
val add_axioms_i = gen_add_axioms cert_axm;
wenzelm@16291
   228
wenzelm@16291
   229
end;
paulson@1526
   230
paulson@1526
   231
wenzelm@3767
   232
wenzelm@3767
   233
(** add constant definitions **)
wenzelm@3767
   234
wenzelm@19708
   235
(* dependencies *)
wenzelm@19708
   236
wenzelm@19708
   237
fun dependencies thy unchecked is_def name lhs rhs =
wenzelm@19708
   238
  let
wenzelm@19708
   239
    val pp = Sign.pp thy;
wenzelm@19708
   240
    val consts = Sign.consts_of thy;
wenzelm@19708
   241
wenzelm@19708
   242
    val lhs_vars = Term.add_tfreesT (#2 lhs) [];
wenzelm@19708
   243
    val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
wenzelm@19708
   244
      if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
wenzelm@19708
   245
    val _ =
wenzelm@19708
   246
      if null rhs_extras then ()
wenzelm@19708
   247
      else error ("Specification depends on extra type variables: " ^
wenzelm@19708
   248
        commas_quote (map (Pretty.string_of_typ pp o TFree) rhs_extras) ^
wenzelm@19708
   249
        "\nThe error(s) above occurred in " ^ quote name);
wenzelm@19708
   250
wenzelm@19708
   251
    fun prep const =
wenzelm@19708
   252
      let val Const (c, T) = Sign.no_vars pp (Const const)
wenzelm@19708
   253
      in (c, Compress.typ thy (Type.varifyT T)) end;
wenzelm@19708
   254
  in
wenzelm@19708
   255
    Defs.define pp consts unchecked is_def (Context.theory_name thy) name (prep lhs) (map prep rhs)
wenzelm@19708
   256
  end;
wenzelm@19708
   257
wenzelm@19708
   258
fun add_deps a raw_lhs raw_rhs thy =
wenzelm@19708
   259
  let
wenzelm@19708
   260
    val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
wenzelm@19708
   261
    val name = if a = "" then (#1 lhs ^ " axiom") else a;
wenzelm@19708
   262
  in thy |> map_defs (dependencies thy false false name lhs rhs) end;
wenzelm@17706
   263
wenzelm@17706
   264
wenzelm@16944
   265
(* check_overloading *)
nipkow@9280
   266
wenzelm@16944
   267
fun check_overloading thy overloaded (c, T) =
wenzelm@16291
   268
  let
wenzelm@16944
   269
    val declT =
wenzelm@16944
   270
      (case Sign.const_constraint thy c of
wenzelm@16944
   271
        NONE => error ("Undeclared constant " ^ quote c)
wenzelm@16944
   272
      | SOME declT => declT);
wenzelm@16944
   273
    val T' = Type.varifyT T;
wenzelm@16944
   274
wenzelm@16944
   275
    fun message txt =
wenzelm@16944
   276
      [Pretty.block [Pretty.str "Specification of constant ",
wenzelm@16944
   277
        Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy T)],
wenzelm@16944
   278
        Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
wenzelm@16291
   279
  in
wenzelm@16944
   280
    if Sign.typ_instance thy (declT, T') then ()
wenzelm@16944
   281
    else if Type.raw_instance (declT, T') then
wenzelm@16944
   282
      error (Library.setmp show_sorts true
wenzelm@16944
   283
        message "imposes additional sort constraints on the constant declaration")
wenzelm@16944
   284
    else if overloaded then ()
wenzelm@16944
   285
    else warning (message "is strictly less general than the declared type");
wenzelm@16944
   286
    (c, T)
nipkow@9280
   287
  end;
nipkow@9280
   288
wenzelm@3767
   289
wenzelm@16291
   290
(* check_def *)
wenzelm@16291
   291
wenzelm@19630
   292
fun check_def thy unchecked overloaded (bname, tm) defs =
wenzelm@16291
   293
  let
wenzelm@17706
   294
    val name = Sign.full_name thy bname;
wenzelm@19693
   295
    val (lhs_const, rhs) = Sign.cert_def (Sign.pp thy) tm;
wenzelm@16944
   296
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@18943
   297
    val _ = check_overloading thy overloaded lhs_const;
wenzelm@19708
   298
  in defs |> dependencies thy unchecked true name lhs_const rhs_consts end
wenzelm@18678
   299
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@16883
   300
   [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
wenzelm@19693
   301
    Pretty.fbrk, Pretty.quote (Sign.pretty_term thy tm)]));
wenzelm@3767
   302
wenzelm@3767
   303
wenzelm@16443
   304
(* add_defs(_i) *)
wenzelm@3767
   305
wenzelm@16291
   306
local
wenzelm@9320
   307
wenzelm@19630
   308
fun gen_add_defs prep_axm unchecked overloaded raw_axms thy =
wenzelm@16443
   309
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   310
    thy
wenzelm@19630
   311
    |> map_defs (fold (check_def thy unchecked overloaded) axms)
wenzelm@9320
   312
    |> add_axioms_i axms
wenzelm@3767
   313
  end;
wenzelm@3767
   314
wenzelm@16291
   315
in
wenzelm@16291
   316
wenzelm@16291
   317
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   318
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   319
wenzelm@16291
   320
end;
wenzelm@3767
   321
wenzelm@3767
   322
wenzelm@16443
   323
(* add_finals(_i) *)
skalberg@14223
   324
wenzelm@16291
   325
local
wenzelm@16291
   326
wenzelm@17706
   327
fun gen_add_finals prep_term overloaded args thy =
skalberg@14223
   328
  let
wenzelm@17706
   329
    fun const_of (Const const) = const
wenzelm@17706
   330
      | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
wenzelm@17706
   331
      | const_of _ = error "Attempt to finalize non-constant term";
wenzelm@19708
   332
    fun specify (c, T) = dependencies thy false false (c ^ " axiom") (c, T) [];
wenzelm@19708
   333
    val finalize = specify o check_overloading thy overloaded o const_of o prep_term thy;
wenzelm@17706
   334
  in thy |> map_defs (fold finalize args) end;
wenzelm@16291
   335
skalberg@14223
   336
in
wenzelm@16291
   337
wenzelm@17706
   338
val add_finals = gen_add_finals Sign.read_term;
wenzelm@17706
   339
val add_finals_i = gen_add_finals Sign.cert_term;
wenzelm@16291
   340
skalberg@14223
   341
end;
skalberg@14223
   342
skalberg@14223
   343
wenzelm@3878
   344
wenzelm@16443
   345
(** add oracle **)
wenzelm@3814
   346
wenzelm@16443
   347
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
wenzelm@16443
   348
  NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
wenzelm@16443
   349
    handle Symtab.DUPS dups => err_dup_oras dups);
wenzelm@3885
   350
paulson@1526
   351
end;
paulson@1526
   352
wenzelm@3767
   353
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   354
open BasicTheory;