src/Pure/theory.ML
author wenzelm
Sun May 18 15:04:09 2008 +0200 (2008-05-18 ago)
changeset 26939 1035c89b4c02
parent 26668 65023d4fd226
child 28017 4919bd124a58
permissions -rw-r--r--
moved global pretty/string_of functions from Sign to Syntax;
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@26668
     8
signature THEORY =
wenzelm@3767
     9
sig
wenzelm@16443
    10
  val eq_thy: theory * theory -> bool
wenzelm@3996
    11
  val subthy: theory * theory -> bool
wenzelm@24666
    12
  val assert_super: theory -> theory -> theory
wenzelm@22684
    13
  val parents_of: theory -> theory list
wenzelm@22684
    14
  val ancestors_of: theory -> theory list
wenzelm@24666
    15
  val check_thy: theory -> theory_ref
wenzelm@24666
    16
  val deref: theory_ref -> theory
wenzelm@24666
    17
  val merge: theory * theory -> theory
wenzelm@24666
    18
  val merge_refs: theory_ref * theory_ref -> theory_ref
wenzelm@24666
    19
  val merge_list: theory list -> theory
wenzelm@16495
    20
  val checkpoint: theory -> theory
wenzelm@16495
    21
  val copy: theory -> theory
wenzelm@24666
    22
  val requires: theory -> string -> string -> unit
wenzelm@16443
    23
  val axiom_space: theory -> NameSpace.T
wenzelm@22684
    24
  val axiom_table: theory -> term Symtab.table
wenzelm@16443
    25
  val oracle_space: theory -> NameSpace.T
wenzelm@22684
    26
  val oracle_table: theory -> ((theory * Object.T -> term) * stamp) Symtab.table
wenzelm@16339
    27
  val axioms_of: theory -> (string * term) list
wenzelm@16339
    28
  val all_axioms_of: theory -> (string * term) list
wenzelm@24666
    29
  val defs_of: theory -> Defs.T
wenzelm@24666
    30
  val at_begin: (theory -> theory option) -> theory -> theory
wenzelm@24666
    31
  val at_end: (theory -> theory option) -> theory -> theory
wenzelm@24666
    32
  val begin_theory: string -> theory list -> theory
wenzelm@24666
    33
  val end_theory: theory -> theory
wenzelm@3996
    34
  val add_axioms: (bstring * string) list -> theory -> theory
wenzelm@3996
    35
  val add_axioms_i: (bstring * term) list -> theory -> theory
wenzelm@19708
    36
  val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
wenzelm@19630
    37
  val add_defs: bool -> bool -> (bstring * string) list -> theory -> theory
wenzelm@19630
    38
  val add_defs_i: bool -> bool -> (bstring * term) list -> theory -> theory
wenzelm@16443
    39
  val add_finals: bool -> string list -> theory -> theory
wenzelm@16443
    40
  val add_finals_i: bool -> term list -> theory -> theory
wenzelm@25017
    41
  val specify_const: Markup.property list -> bstring * typ * mixfix -> (string * typ) list ->
wenzelm@25017
    42
   theory -> term * theory
wenzelm@16443
    43
  val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
wenzelm@16495
    44
end
paulson@1526
    45
wenzelm@24666
    46
structure Theory: THEORY =
wenzelm@16443
    47
struct
wenzelm@16443
    48
wenzelm@19708
    49
wenzelm@24666
    50
(** theory context operations **)
wenzelm@16443
    51
wenzelm@16443
    52
val eq_thy = Context.eq_thy;
wenzelm@16443
    53
val subthy = Context.subthy;
paulson@1526
    54
haftmann@24626
    55
fun assert_super thy1 thy2 =
haftmann@24626
    56
  if subthy (thy1, thy2) then thy2
haftmann@24626
    57
  else raise THEORY ("Not a super theory", [thy1, thy2]);
haftmann@24626
    58
wenzelm@16443
    59
val parents_of = Context.parents_of;
wenzelm@16443
    60
val ancestors_of = Context.ancestors_of;
wenzelm@16443
    61
wenzelm@24137
    62
val check_thy = Context.check_thy;
wenzelm@16443
    63
val deref = Context.deref;
wenzelm@24666
    64
wenzelm@16443
    65
val merge = Context.merge;
wenzelm@16443
    66
val merge_refs = Context.merge_refs;
wenzelm@16443
    67
wenzelm@23600
    68
fun merge_list [] = raise THEORY ("Empty merge of theories", [])
wenzelm@21608
    69
  | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
wenzelm@21608
    70
wenzelm@16495
    71
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    72
val copy = Context.copy_thy;
wenzelm@16495
    73
wenzelm@24666
    74
fun requires thy name what =
wenzelm@24666
    75
  if Context.exists_name name thy then ()
wenzelm@24666
    76
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
wenzelm@24666
    77
wenzelm@24666
    78
wenzelm@24666
    79
wenzelm@25059
    80
(** datatype thy **)
wenzelm@24666
    81
wenzelm@24666
    82
type wrapper = (theory -> theory option) * stamp;
wenzelm@24666
    83
wenzelm@24666
    84
fun apply_wrappers (wrappers: wrapper list) =
wenzelm@25059
    85
  perhaps (perhaps_loop (perhaps_apply (map fst wrappers)));
wenzelm@24666
    86
wenzelm@24666
    87
datatype thy = Thy of
wenzelm@24666
    88
 {axioms: term NameSpace.table,
wenzelm@24666
    89
  defs: Defs.T,
wenzelm@24666
    90
  oracles: ((theory * Object.T -> term) * stamp) NameSpace.table,
wenzelm@24666
    91
  wrappers: wrapper list * wrapper list};
wenzelm@24666
    92
wenzelm@24666
    93
fun make_thy (axioms, defs, oracles, wrappers) =
wenzelm@24666
    94
  Thy {axioms = axioms, defs = defs, oracles = oracles, wrappers = wrappers};
wenzelm@24666
    95
wenzelm@24666
    96
fun err_dup_axm dup = error ("Duplicate axiom: " ^ quote dup);
wenzelm@24666
    97
fun err_dup_ora dup = error ("Duplicate oracle: " ^ quote dup);
wenzelm@24666
    98
wenzelm@24666
    99
structure ThyData = TheoryDataFun
wenzelm@24666
   100
(
wenzelm@24666
   101
  type T = thy;
wenzelm@24666
   102
  val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table, ([], []));
wenzelm@24666
   103
  val copy = I;
wenzelm@24666
   104
wenzelm@24666
   105
  fun extend (Thy {axioms, defs, oracles, wrappers}) =
wenzelm@24666
   106
    make_thy (NameSpace.empty_table, defs, oracles, wrappers);
wenzelm@24666
   107
wenzelm@24666
   108
  fun merge pp (thy1, thy2) =
wenzelm@24666
   109
    let
wenzelm@24666
   110
      val Thy {axioms = _, defs = defs1, oracles = oracles1, wrappers = (bgs1, ens1)} = thy1;
wenzelm@24666
   111
      val Thy {axioms = _, defs = defs2, oracles = oracles2, wrappers = (bgs2, ens2)} = thy2;
wenzelm@24666
   112
wenzelm@24666
   113
      val axioms' = NameSpace.empty_table;
wenzelm@24666
   114
      val defs' = Defs.merge pp (defs1, defs2);
wenzelm@24666
   115
      val oracles' = NameSpace.merge_tables (eq_snd (op =)) (oracles1, oracles2)
wenzelm@24666
   116
        handle Symtab.DUP dup => err_dup_ora dup;
wenzelm@24666
   117
      val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
wenzelm@24666
   118
      val ens' = Library.merge (eq_snd op =) (ens1, ens2);
wenzelm@24666
   119
    in make_thy (axioms', defs', oracles', (bgs', ens')) end;
wenzelm@24666
   120
);
wenzelm@24666
   121
wenzelm@24666
   122
fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
wenzelm@24666
   123
wenzelm@24666
   124
fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles, wrappers}) =>
wenzelm@24666
   125
  make_thy (f (axioms, defs, oracles, wrappers)));
wenzelm@24666
   126
wenzelm@24666
   127
wenzelm@24666
   128
fun map_axioms f = map_thy
wenzelm@24666
   129
  (fn (axioms, defs, oracles, wrappers) => (f axioms, defs, oracles, wrappers));
wenzelm@24666
   130
wenzelm@24666
   131
fun map_defs f = map_thy
wenzelm@24666
   132
  (fn (axioms, defs, oracles, wrappers) => (axioms, f defs, oracles, wrappers));
wenzelm@24666
   133
wenzelm@24666
   134
fun map_oracles f = map_thy
wenzelm@24666
   135
  (fn (axioms, defs, oracles, wrappers) => (axioms, defs, f oracles, wrappers));
wenzelm@24666
   136
wenzelm@24666
   137
fun map_wrappers f = map_thy
wenzelm@24666
   138
  (fn (axioms, defs, oracles, wrappers) => (axioms, defs, oracles, f wrappers));
wenzelm@24666
   139
wenzelm@24666
   140
wenzelm@24666
   141
(* basic operations *)
wenzelm@24666
   142
wenzelm@24666
   143
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@24666
   144
val axiom_table = #2 o #axioms o rep_theory;
wenzelm@24666
   145
wenzelm@24666
   146
val oracle_space = #1 o #oracles o rep_theory;
wenzelm@24666
   147
val oracle_table = #2 o #oracles o rep_theory;
wenzelm@24666
   148
wenzelm@24666
   149
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@24666
   150
fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
wenzelm@24666
   151
wenzelm@24666
   152
val defs_of = #defs o rep_theory;
wenzelm@24666
   153
wenzelm@24666
   154
wenzelm@24666
   155
(* begin/end theory *)
wenzelm@24666
   156
wenzelm@24666
   157
val begin_wrappers = rev o #1 o #wrappers o rep_theory;
wenzelm@24666
   158
val end_wrappers = rev o #2 o #wrappers o rep_theory;
wenzelm@24666
   159
wenzelm@24666
   160
fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
wenzelm@24666
   161
fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
wenzelm@24666
   162
wenzelm@24666
   163
fun begin_theory name imports =
wenzelm@24666
   164
  let
wenzelm@26939
   165
    val thy = Context.begin_thy Syntax.pp_global name imports;
wenzelm@24666
   166
    val wrappers = begin_wrappers thy;
wenzelm@24666
   167
  in thy |> Sign.local_path |> apply_wrappers wrappers end;
wenzelm@24666
   168
wenzelm@24666
   169
fun end_theory thy =
wenzelm@24666
   170
  thy |> apply_wrappers (end_wrappers thy) |> Context.finish_thy;
wenzelm@24666
   171
wenzelm@16443
   172
wenzelm@3996
   173
wenzelm@24666
   174
(** add axioms **)
wenzelm@3814
   175
paulson@1526
   176
(* prepare axioms *)
paulson@1526
   177
wenzelm@18678
   178
fun err_in_axm msg name =
wenzelm@18678
   179
  cat_error msg ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   180
wenzelm@16443
   181
fun cert_axm thy (name, raw_tm) =
paulson@1526
   182
  let
wenzelm@18968
   183
    val (t, T, _) = Sign.certify_prop thy raw_tm
wenzelm@2979
   184
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   185
        | TERM (msg, _) => error msg;
paulson@1526
   186
  in
wenzelm@9537
   187
    Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
wenzelm@26939
   188
    (name, Sign.no_vars (Syntax.pp_global thy) t)
wenzelm@9629
   189
  end;
paulson@1526
   190
wenzelm@22684
   191
fun read_axm thy (name, str) =
wenzelm@24708
   192
  cert_axm thy (name, Syntax.read_prop_global thy str)
wenzelm@22689
   193
    handle ERROR msg => err_in_axm msg name;
paulson@1526
   194
paulson@1526
   195
wenzelm@16443
   196
(* add_axioms(_i) *)
paulson@1526
   197
wenzelm@16291
   198
local
wenzelm@16291
   199
wenzelm@16443
   200
fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
paulson@1526
   201
  let
wenzelm@26631
   202
    val axms = map (apsnd Logic.varify o prep_axm thy) raw_axms;
haftmann@23086
   203
    val axioms' = NameSpace.extend_table (Sign.naming_of thy) axms axioms
wenzelm@23655
   204
      handle Symtab.DUP dup => err_dup_axm dup;
wenzelm@16443
   205
  in axioms' end);
paulson@1526
   206
wenzelm@16291
   207
in
wenzelm@16291
   208
wenzelm@16291
   209
val add_axioms = gen_add_axioms read_axm;
wenzelm@16291
   210
val add_axioms_i = gen_add_axioms cert_axm;
wenzelm@16291
   211
wenzelm@16291
   212
end;
paulson@1526
   213
paulson@1526
   214
wenzelm@3767
   215
wenzelm@3767
   216
(** add constant definitions **)
wenzelm@3767
   217
wenzelm@19708
   218
(* dependencies *)
wenzelm@19708
   219
wenzelm@19708
   220
fun dependencies thy unchecked is_def name lhs rhs =
wenzelm@19708
   221
  let
wenzelm@26939
   222
    val pp = Syntax.pp_global thy;
wenzelm@19708
   223
    val consts = Sign.consts_of thy;
wenzelm@19727
   224
    fun prep const =
wenzelm@19727
   225
      let val Const (c, T) = Sign.no_vars pp (Const const)
wenzelm@26631
   226
      in (c, Consts.typargs consts (c, Logic.varifyT T)) end;
wenzelm@19708
   227
wenzelm@19708
   228
    val lhs_vars = Term.add_tfreesT (#2 lhs) [];
wenzelm@19708
   229
    val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
wenzelm@19708
   230
      if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
wenzelm@19708
   231
    val _ =
wenzelm@19708
   232
      if null rhs_extras then ()
wenzelm@19708
   233
      else error ("Specification depends on extra type variables: " ^
wenzelm@19708
   234
        commas_quote (map (Pretty.string_of_typ pp o TFree) rhs_extras) ^
wenzelm@19708
   235
        "\nThe error(s) above occurred in " ^ quote name);
haftmann@24199
   236
  in Defs.define pp unchecked is_def name (prep lhs) (map prep rhs) end;
wenzelm@19708
   237
wenzelm@19708
   238
fun add_deps a raw_lhs raw_rhs thy =
wenzelm@19708
   239
  let
wenzelm@19708
   240
    val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
wenzelm@19708
   241
    val name = if a = "" then (#1 lhs ^ " axiom") else a;
wenzelm@19708
   242
  in thy |> map_defs (dependencies thy false false name lhs rhs) end;
wenzelm@17706
   243
wenzelm@25017
   244
fun specify_const tags decl deps thy =
wenzelm@25017
   245
  let val (t as Const const, thy') = Sign.declare_const tags decl thy
wenzelm@25017
   246
  in (t, add_deps "" const deps thy') end;
wenzelm@25017
   247
wenzelm@17706
   248
wenzelm@16944
   249
(* check_overloading *)
nipkow@9280
   250
wenzelm@16944
   251
fun check_overloading thy overloaded (c, T) =
wenzelm@16291
   252
  let
wenzelm@24763
   253
    val declT = Sign.the_const_constraint thy c
wenzelm@24763
   254
      handle TYPE (msg, _, _) => error msg;
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@26939
   259
        Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ_global 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@24981
   276
    val ctxt = ProofContext.init thy;
wenzelm@17706
   277
    val name = Sign.full_name thy bname;
wenzelm@24981
   278
    val (lhs_const, rhs) = Sign.cert_def ctxt tm;
wenzelm@16944
   279
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@18943
   280
    val _ = check_overloading thy overloaded lhs_const;
wenzelm@19708
   281
  in defs |> dependencies thy unchecked true name lhs_const rhs_consts end
wenzelm@18678
   282
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@16883
   283
   [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
wenzelm@26939
   284
    Pretty.fbrk, Pretty.quote (Syntax.pretty_term_global thy tm)]));
wenzelm@3767
   285
wenzelm@3767
   286
wenzelm@16443
   287
(* add_defs(_i) *)
wenzelm@3767
   288
wenzelm@16291
   289
local
wenzelm@9320
   290
wenzelm@19630
   291
fun gen_add_defs prep_axm unchecked overloaded raw_axms thy =
wenzelm@16443
   292
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   293
    thy
wenzelm@19630
   294
    |> map_defs (fold (check_def thy unchecked overloaded) axms)
wenzelm@9320
   295
    |> add_axioms_i axms
wenzelm@3767
   296
  end;
wenzelm@3767
   297
wenzelm@16291
   298
in
wenzelm@16291
   299
wenzelm@16291
   300
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   301
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   302
wenzelm@16291
   303
end;
wenzelm@3767
   304
wenzelm@3767
   305
wenzelm@16443
   306
(* add_finals(_i) *)
skalberg@14223
   307
wenzelm@16291
   308
local
wenzelm@16291
   309
wenzelm@17706
   310
fun gen_add_finals prep_term overloaded args thy =
skalberg@14223
   311
  let
wenzelm@17706
   312
    fun const_of (Const const) = const
wenzelm@17706
   313
      | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
wenzelm@17706
   314
      | const_of _ = error "Attempt to finalize non-constant term";
wenzelm@19708
   315
    fun specify (c, T) = dependencies thy false false (c ^ " axiom") (c, T) [];
wenzelm@24708
   316
    val finalize = specify o check_overloading thy overloaded o const_of o
wenzelm@24708
   317
      Sign.cert_term thy o prep_term thy;
wenzelm@17706
   318
  in thy |> map_defs (fold finalize args) end;
wenzelm@16291
   319
skalberg@14223
   320
in
wenzelm@16291
   321
wenzelm@24708
   322
val add_finals = gen_add_finals Syntax.read_term_global;
wenzelm@24708
   323
val add_finals_i = gen_add_finals (K I);
wenzelm@16291
   324
skalberg@14223
   325
end;
skalberg@14223
   326
skalberg@14223
   327
wenzelm@3878
   328
wenzelm@16443
   329
(** add oracle **)
wenzelm@3814
   330
wenzelm@16443
   331
fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
haftmann@23086
   332
  NameSpace.extend_table (Sign.naming_of thy) [(bname, (oracle, stamp ()))] oracles
wenzelm@23655
   333
    handle Symtab.DUP dup => err_dup_ora dup);
wenzelm@3885
   334
paulson@1526
   335
end;
paulson@1526
   336