src/Pure/theory.ML
author wenzelm
Mon Jan 04 23:20:35 2010 +0100 (2010-01-04 ago)
changeset 34259 2ba492b8b6e8
parent 34245 25bd3ed2ac9f
child 35845 e5980f0ad025
permissions -rw-r--r--
discontinued old TheoryDataFun, but retain Theory_Data_PP with is Pretty.pp argument to merge (still required in exotic situations -- hard to get rid of);
paulson@1526
     1
(*  Title:      Pure/theory.ML
paulson@1526
     2
    Author:     Lawrence C Paulson and Markus Wenzel
paulson@1526
     3
wenzelm@28290
     4
Logical theory content: axioms, definitions, and begin/end wrappers.
paulson@1526
     5
*)
wenzelm@16291
     6
wenzelm@26668
     7
signature THEORY =
wenzelm@3767
     8
sig
wenzelm@16443
     9
  val eq_thy: theory * theory -> bool
wenzelm@3996
    10
  val subthy: theory * theory -> bool
wenzelm@24666
    11
  val assert_super: theory -> theory -> theory
wenzelm@22684
    12
  val parents_of: theory -> theory list
wenzelm@22684
    13
  val ancestors_of: theory -> theory list
wenzelm@24666
    14
  val check_thy: theory -> theory_ref
wenzelm@24666
    15
  val deref: theory_ref -> theory
wenzelm@24666
    16
  val merge: theory * theory -> theory
wenzelm@24666
    17
  val merge_refs: theory_ref * theory_ref -> theory_ref
wenzelm@24666
    18
  val merge_list: theory list -> theory
wenzelm@16495
    19
  val checkpoint: theory -> theory
wenzelm@16495
    20
  val copy: theory -> theory
wenzelm@24666
    21
  val requires: theory -> string -> string -> unit
wenzelm@33095
    22
  val axiom_space: theory -> Name_Space.T
wenzelm@22684
    23
  val axiom_table: theory -> term Symtab.table
wenzelm@16339
    24
  val axioms_of: theory -> (string * term) list
wenzelm@16339
    25
  val all_axioms_of: theory -> (string * term) list
wenzelm@24666
    26
  val defs_of: theory -> Defs.T
wenzelm@24666
    27
  val at_begin: (theory -> theory option) -> theory -> theory
wenzelm@24666
    28
  val at_end: (theory -> theory option) -> theory -> theory
wenzelm@24666
    29
  val begin_theory: string -> theory list -> theory
wenzelm@24666
    30
  val end_theory: theory -> theory
haftmann@29581
    31
  val add_axioms_i: (binding * term) list -> theory -> theory
wenzelm@30337
    32
  val add_axioms: (binding * string) list -> theory -> theory
wenzelm@19708
    33
  val add_deps: string -> string * typ -> (string * typ) list -> theory -> theory
haftmann@29581
    34
  val add_defs_i: bool -> bool -> (binding * term) list -> theory -> theory
wenzelm@30337
    35
  val add_defs: bool -> bool -> (binding * string) list -> theory -> theory
haftmann@29581
    36
  val add_finals_i: bool -> term list -> theory -> theory
wenzelm@16443
    37
  val add_finals: bool -> string list -> theory -> theory
wenzelm@33173
    38
  val specify_const: (binding * typ) * mixfix -> theory -> term * theory
wenzelm@16495
    39
end
paulson@1526
    40
wenzelm@24666
    41
structure Theory: THEORY =
wenzelm@16443
    42
struct
wenzelm@16443
    43
wenzelm@19708
    44
wenzelm@24666
    45
(** theory context operations **)
wenzelm@16443
    46
wenzelm@16443
    47
val eq_thy = Context.eq_thy;
wenzelm@16443
    48
val subthy = Context.subthy;
paulson@1526
    49
haftmann@24626
    50
fun assert_super thy1 thy2 =
haftmann@24626
    51
  if subthy (thy1, thy2) then thy2
haftmann@24626
    52
  else raise THEORY ("Not a super theory", [thy1, thy2]);
haftmann@24626
    53
wenzelm@16443
    54
val parents_of = Context.parents_of;
wenzelm@16443
    55
val ancestors_of = Context.ancestors_of;
wenzelm@16443
    56
wenzelm@24137
    57
val check_thy = Context.check_thy;
wenzelm@16443
    58
val deref = Context.deref;
wenzelm@24666
    59
wenzelm@16443
    60
val merge = Context.merge;
wenzelm@16443
    61
val merge_refs = Context.merge_refs;
wenzelm@16443
    62
wenzelm@23600
    63
fun merge_list [] = raise THEORY ("Empty merge of theories", [])
wenzelm@21608
    64
  | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
wenzelm@21608
    65
wenzelm@16495
    66
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    67
val copy = Context.copy_thy;
wenzelm@16495
    68
wenzelm@24666
    69
fun requires thy name what =
wenzelm@29092
    70
  if exists (fn thy' => Context.theory_name thy' = name) (thy :: ancestors_of thy) then ()
wenzelm@24666
    71
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
wenzelm@24666
    72
wenzelm@24666
    73
wenzelm@24666
    74
wenzelm@25059
    75
(** datatype thy **)
wenzelm@24666
    76
wenzelm@24666
    77
type wrapper = (theory -> theory option) * stamp;
wenzelm@24666
    78
wenzelm@24666
    79
fun apply_wrappers (wrappers: wrapper list) =
wenzelm@25059
    80
  perhaps (perhaps_loop (perhaps_apply (map fst wrappers)));
wenzelm@24666
    81
wenzelm@24666
    82
datatype thy = Thy of
wenzelm@33095
    83
 {axioms: term Name_Space.table,
wenzelm@24666
    84
  defs: Defs.T,
wenzelm@24666
    85
  wrappers: wrapper list * wrapper list};
wenzelm@24666
    86
wenzelm@28290
    87
fun make_thy (axioms, defs, wrappers) = Thy {axioms = axioms, defs = defs, wrappers = wrappers};
wenzelm@24666
    88
wenzelm@34259
    89
structure ThyData = Theory_Data_PP
wenzelm@24666
    90
(
wenzelm@24666
    91
  type T = thy;
wenzelm@33159
    92
  val empty_axioms = Name_Space.empty_table "axiom" : term Name_Space.table;
wenzelm@33096
    93
  val empty = make_thy (empty_axioms, Defs.empty, ([], []));
wenzelm@24666
    94
wenzelm@33096
    95
  fun extend (Thy {axioms = _, defs, wrappers}) = make_thy (empty_axioms, defs, wrappers);
wenzelm@24666
    96
wenzelm@24666
    97
  fun merge pp (thy1, thy2) =
wenzelm@24666
    98
    let
wenzelm@28290
    99
      val Thy {axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
wenzelm@28290
   100
      val Thy {axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
wenzelm@24666
   101
wenzelm@33096
   102
      val axioms' = empty_axioms;
wenzelm@24666
   103
      val defs' = Defs.merge pp (defs1, defs2);
wenzelm@24666
   104
      val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
wenzelm@24666
   105
      val ens' = Library.merge (eq_snd op =) (ens1, ens2);
wenzelm@28290
   106
    in make_thy (axioms', defs', (bgs', ens')) end;
wenzelm@24666
   107
);
wenzelm@24666
   108
wenzelm@24666
   109
fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
wenzelm@24666
   110
wenzelm@28290
   111
fun map_thy f = ThyData.map (fn (Thy {axioms, defs, wrappers}) =>
wenzelm@28290
   112
  make_thy (f (axioms, defs, wrappers)));
wenzelm@24666
   113
wenzelm@24666
   114
wenzelm@28290
   115
fun map_axioms f = map_thy (fn (axioms, defs, wrappers) => (f axioms, defs, wrappers));
wenzelm@28290
   116
fun map_defs f = map_thy (fn (axioms, defs, wrappers) => (axioms, f defs, wrappers));
wenzelm@28290
   117
fun map_wrappers f = map_thy (fn (axioms, defs, wrappers) => (axioms, defs, f wrappers));
wenzelm@24666
   118
wenzelm@24666
   119
wenzelm@24666
   120
(* basic operations *)
wenzelm@24666
   121
wenzelm@24666
   122
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@24666
   123
val axiom_table = #2 o #axioms o rep_theory;
wenzelm@24666
   124
wenzelm@24666
   125
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@24666
   126
fun all_axioms_of thy = maps axioms_of (thy :: ancestors_of thy);
wenzelm@24666
   127
wenzelm@24666
   128
val defs_of = #defs o rep_theory;
wenzelm@24666
   129
wenzelm@24666
   130
wenzelm@24666
   131
(* begin/end theory *)
wenzelm@24666
   132
wenzelm@24666
   133
val begin_wrappers = rev o #1 o #wrappers o rep_theory;
wenzelm@24666
   134
val end_wrappers = rev o #2 o #wrappers o rep_theory;
wenzelm@24666
   135
wenzelm@24666
   136
fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
wenzelm@24666
   137
fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
wenzelm@24666
   138
wenzelm@24666
   139
fun begin_theory name imports =
wenzelm@24666
   140
  let
wenzelm@26939
   141
    val thy = Context.begin_thy Syntax.pp_global name imports;
wenzelm@24666
   142
    val wrappers = begin_wrappers thy;
wenzelm@33168
   143
  in
wenzelm@33168
   144
    thy
wenzelm@33168
   145
    |> Sign.local_path
wenzelm@33168
   146
    |> Sign.map_naming (Name_Space.set_theory_name name)
wenzelm@33168
   147
    |> apply_wrappers wrappers
wenzelm@33168
   148
  end;
wenzelm@24666
   149
wenzelm@24666
   150
fun end_theory thy =
wenzelm@24666
   151
  thy |> apply_wrappers (end_wrappers thy) |> Context.finish_thy;
wenzelm@24666
   152
wenzelm@16443
   153
wenzelm@3996
   154
wenzelm@24666
   155
(** add axioms **)
wenzelm@3814
   156
paulson@1526
   157
(* prepare axioms *)
paulson@1526
   158
haftmann@29581
   159
fun cert_axm thy (b, raw_tm) =
paulson@1526
   160
  let
wenzelm@32789
   161
    val t = Sign.cert_prop thy raw_tm
wenzelm@2979
   162
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   163
        | TERM (msg, _) => error msg;
paulson@1526
   164
  in
wenzelm@9537
   165
    Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
haftmann@29581
   166
    (b, Sign.no_vars (Syntax.pp_global thy) t)
wenzelm@9629
   167
  end;
paulson@1526
   168
wenzelm@30337
   169
fun read_axm thy (b, str) =
wenzelm@30337
   170
  cert_axm thy (b, Syntax.read_prop_global thy str) handle ERROR msg =>
wenzelm@33092
   171
    cat_error msg ("The error(s) above occurred in axiom " ^ quote (Binding.str_of b));
paulson@1526
   172
paulson@1526
   173
wenzelm@16443
   174
(* add_axioms(_i) *)
paulson@1526
   175
wenzelm@16291
   176
local
wenzelm@16291
   177
wenzelm@16443
   178
fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
paulson@1526
   179
  let
haftmann@29581
   180
    val axms = map (apsnd Logic.varify o prep_axm thy) raw_axms;
wenzelm@33095
   181
    val axioms' = fold (#2 oo Name_Space.define true (Sign.naming_of thy)) axms axioms;
wenzelm@16443
   182
  in axioms' end);
paulson@1526
   183
wenzelm@16291
   184
in
wenzelm@16291
   185
haftmann@29581
   186
val add_axioms_i = gen_add_axioms cert_axm;
wenzelm@16291
   187
val add_axioms = gen_add_axioms read_axm;
wenzelm@16291
   188
wenzelm@16291
   189
end;
paulson@1526
   190
paulson@1526
   191
wenzelm@3767
   192
wenzelm@3767
   193
(** add constant definitions **)
wenzelm@3767
   194
wenzelm@19708
   195
(* dependencies *)
wenzelm@19708
   196
wenzelm@33701
   197
fun dependencies thy unchecked def description lhs rhs =
wenzelm@19708
   198
  let
wenzelm@26939
   199
    val pp = Syntax.pp_global thy;
wenzelm@19708
   200
    val consts = Sign.consts_of thy;
wenzelm@19727
   201
    fun prep const =
wenzelm@19727
   202
      let val Const (c, T) = Sign.no_vars pp (Const const)
wenzelm@26631
   203
      in (c, Consts.typargs consts (c, Logic.varifyT T)) end;
wenzelm@19708
   204
wenzelm@19708
   205
    val lhs_vars = Term.add_tfreesT (#2 lhs) [];
wenzelm@19708
   206
    val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
wenzelm@19708
   207
      if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
wenzelm@19708
   208
    val _ =
wenzelm@19708
   209
      if null rhs_extras then ()
wenzelm@19708
   210
      else error ("Specification depends on extra type variables: " ^
wenzelm@19708
   211
        commas_quote (map (Pretty.string_of_typ pp o TFree) rhs_extras) ^
wenzelm@33701
   212
        "\nThe error(s) above occurred in " ^ quote description);
wenzelm@33701
   213
  in Defs.define pp unchecked def description (prep lhs) (map prep rhs) end;
wenzelm@19708
   214
wenzelm@19708
   215
fun add_deps a raw_lhs raw_rhs thy =
wenzelm@19708
   216
  let
wenzelm@19708
   217
    val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
wenzelm@33701
   218
    val description = if a = "" then #1 lhs ^ " axiom" else a;
wenzelm@33701
   219
  in thy |> map_defs (dependencies thy false NONE description lhs rhs) end;
wenzelm@17706
   220
wenzelm@33173
   221
fun specify_const decl thy =
wenzelm@33173
   222
  let val (t as Const const, thy') = Sign.declare_const decl thy
wenzelm@28112
   223
  in (t, add_deps "" const [] thy') end;
wenzelm@25017
   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@24763
   230
    val declT = Sign.the_const_constraint thy c
wenzelm@24763
   231
      handle TYPE (msg, _, _) => error msg;
wenzelm@19806
   232
    val T' = Logic.varifyT T;
wenzelm@16944
   233
wenzelm@16944
   234
    fun message txt =
wenzelm@16944
   235
      [Pretty.block [Pretty.str "Specification of constant ",
wenzelm@26939
   236
        Pretty.str c, Pretty.str " ::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ_global thy T)],
wenzelm@16944
   237
        Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
wenzelm@16291
   238
  in
wenzelm@16944
   239
    if Sign.typ_instance thy (declT, T') then ()
wenzelm@16944
   240
    else if Type.raw_instance (declT, T') then
wenzelm@32966
   241
      error (setmp_CRITICAL show_sorts true
wenzelm@16944
   242
        message "imposes additional sort constraints on the constant declaration")
wenzelm@16944
   243
    else if overloaded then ()
wenzelm@16944
   244
    else warning (message "is strictly less general than the declared type");
wenzelm@16944
   245
    (c, T)
nipkow@9280
   246
  end;
nipkow@9280
   247
wenzelm@3767
   248
wenzelm@16291
   249
(* check_def *)
wenzelm@16291
   250
haftmann@29581
   251
fun check_def thy unchecked overloaded (b, tm) defs =
wenzelm@16291
   252
  let
wenzelm@24981
   253
    val ctxt = ProofContext.init thy;
haftmann@29581
   254
    val name = Sign.full_name thy b;
wenzelm@24981
   255
    val (lhs_const, rhs) = Sign.cert_def ctxt tm;
wenzelm@16944
   256
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@18943
   257
    val _ = check_overloading thy overloaded lhs_const;
wenzelm@33701
   258
  in defs |> dependencies thy unchecked (SOME name) name lhs_const rhs_consts end
wenzelm@18678
   259
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@30218
   260
   [Pretty.str ("The error(s) above occurred in definition " ^ quote (Binding.str_of b) ^ ":"),
wenzelm@26939
   261
    Pretty.fbrk, Pretty.quote (Syntax.pretty_term_global thy tm)]));
wenzelm@3767
   262
wenzelm@3767
   263
wenzelm@16443
   264
(* add_defs(_i) *)
wenzelm@3767
   265
wenzelm@16291
   266
local
wenzelm@9320
   267
wenzelm@19630
   268
fun gen_add_defs prep_axm unchecked overloaded raw_axms thy =
wenzelm@16443
   269
  let val axms = map (prep_axm thy) raw_axms in
wenzelm@16443
   270
    thy
wenzelm@19630
   271
    |> map_defs (fold (check_def thy unchecked overloaded) axms)
wenzelm@9320
   272
    |> add_axioms_i axms
wenzelm@3767
   273
  end;
wenzelm@3767
   274
wenzelm@16291
   275
in
wenzelm@16291
   276
wenzelm@16291
   277
val add_defs_i = gen_add_defs cert_axm;
wenzelm@16291
   278
val add_defs = gen_add_defs read_axm;
wenzelm@16291
   279
wenzelm@16291
   280
end;
wenzelm@3767
   281
wenzelm@3767
   282
wenzelm@16443
   283
(* add_finals(_i) *)
skalberg@14223
   284
wenzelm@16291
   285
local
wenzelm@16291
   286
wenzelm@17706
   287
fun gen_add_finals prep_term overloaded args thy =
skalberg@14223
   288
  let
wenzelm@17706
   289
    fun const_of (Const const) = const
wenzelm@17706
   290
      | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
wenzelm@17706
   291
      | const_of _ = error "Attempt to finalize non-constant term";
wenzelm@33701
   292
    fun specify (c, T) = dependencies thy false NONE (c ^ " axiom") (c, T) [];
wenzelm@24708
   293
    val finalize = specify o check_overloading thy overloaded o const_of o
wenzelm@24708
   294
      Sign.cert_term thy 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
haftmann@29581
   299
val add_finals_i = gen_add_finals (K I);
wenzelm@24708
   300
val add_finals = gen_add_finals Syntax.read_term_global;
wenzelm@16291
   301
skalberg@14223
   302
end;
skalberg@14223
   303
paulson@1526
   304
end;