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