src/Pure/theory.ML
author wenzelm
Wed Apr 20 13:54:07 2011 +0200 (2011-04-20)
changeset 42425 2aa907d5ee4f
parent 42394 c65c07d9967a
child 44802 65c397cc44ec
permissions -rw-r--r--
added Theory.nodes_of convenience;
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@42425
    14
  val nodes_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@33095
    23
  val axiom_space: theory -> Name_Space.T
wenzelm@22684
    24
  val axiom_table: theory -> term Symtab.table
wenzelm@16339
    25
  val axioms_of: theory -> (string * term) list
wenzelm@16339
    26
  val all_axioms_of: theory -> (string * term) list
wenzelm@24666
    27
  val defs_of: theory -> Defs.T
wenzelm@24666
    28
  val at_begin: (theory -> theory option) -> theory -> theory
wenzelm@24666
    29
  val at_end: (theory -> theory option) -> theory -> theory
wenzelm@24666
    30
  val begin_theory: string -> theory list -> theory
wenzelm@24666
    31
  val end_theory: theory -> theory
wenzelm@42375
    32
  val add_axiom: Proof.context -> binding * term -> theory -> theory
wenzelm@42375
    33
  val add_deps: Proof.context -> string -> string * typ -> (string * typ) list -> theory -> theory
wenzelm@42375
    34
  val add_deps_global: string -> string * typ -> (string * typ) list -> theory -> theory
wenzelm@42375
    35
  val add_def: Proof.context -> bool -> bool -> binding * term -> 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@42425
    56
fun nodes_of thy = thy :: ancestors_of thy;
wenzelm@16443
    57
wenzelm@24137
    58
val check_thy = Context.check_thy;
wenzelm@16443
    59
val deref = Context.deref;
wenzelm@24666
    60
wenzelm@16443
    61
val merge = Context.merge;
wenzelm@16443
    62
val merge_refs = Context.merge_refs;
wenzelm@16443
    63
wenzelm@23600
    64
fun merge_list [] = raise THEORY ("Empty merge of theories", [])
wenzelm@21608
    65
  | merge_list (thy :: thys) = Library.foldl merge (thy, thys);
wenzelm@21608
    66
wenzelm@16495
    67
val checkpoint = Context.checkpoint_thy;
wenzelm@16495
    68
val copy = Context.copy_thy;
wenzelm@16495
    69
wenzelm@24666
    70
fun requires thy name what =
wenzelm@42425
    71
  if exists (fn thy' => Context.theory_name thy' = name) (nodes_of thy) then ()
wenzelm@24666
    72
  else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
wenzelm@24666
    73
wenzelm@24666
    74
wenzelm@24666
    75
wenzelm@25059
    76
(** datatype thy **)
wenzelm@24666
    77
wenzelm@24666
    78
type wrapper = (theory -> theory option) * stamp;
wenzelm@24666
    79
wenzelm@24666
    80
fun apply_wrappers (wrappers: wrapper list) =
wenzelm@25059
    81
  perhaps (perhaps_loop (perhaps_apply (map fst wrappers)));
wenzelm@24666
    82
wenzelm@24666
    83
datatype thy = Thy of
wenzelm@33095
    84
 {axioms: term Name_Space.table,
wenzelm@24666
    85
  defs: Defs.T,
wenzelm@24666
    86
  wrappers: wrapper list * wrapper list};
wenzelm@24666
    87
wenzelm@28290
    88
fun make_thy (axioms, defs, wrappers) = Thy {axioms = axioms, defs = defs, wrappers = wrappers};
wenzelm@24666
    89
wenzelm@42016
    90
structure Thy = Theory_Data_PP
wenzelm@24666
    91
(
wenzelm@24666
    92
  type T = thy;
wenzelm@33159
    93
  val empty_axioms = Name_Space.empty_table "axiom" : term Name_Space.table;
wenzelm@33096
    94
  val empty = make_thy (empty_axioms, Defs.empty, ([], []));
wenzelm@24666
    95
wenzelm@33096
    96
  fun extend (Thy {axioms = _, defs, wrappers}) = make_thy (empty_axioms, defs, wrappers);
wenzelm@24666
    97
wenzelm@24666
    98
  fun merge pp (thy1, thy2) =
wenzelm@24666
    99
    let
wenzelm@42389
   100
      val ctxt = Syntax.init_pretty pp;
wenzelm@28290
   101
      val Thy {axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
wenzelm@28290
   102
      val Thy {axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
wenzelm@24666
   103
wenzelm@33096
   104
      val axioms' = empty_axioms;
wenzelm@42389
   105
      val defs' = Defs.merge ctxt (defs1, defs2);
wenzelm@24666
   106
      val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
wenzelm@24666
   107
      val ens' = Library.merge (eq_snd op =) (ens1, ens2);
wenzelm@28290
   108
    in make_thy (axioms', defs', (bgs', ens')) end;
wenzelm@24666
   109
);
wenzelm@24666
   110
wenzelm@42016
   111
fun rep_theory thy = Thy.get thy |> (fn Thy args => args);
wenzelm@24666
   112
wenzelm@42016
   113
fun map_thy f = Thy.map (fn (Thy {axioms, defs, wrappers}) =>
wenzelm@28290
   114
  make_thy (f (axioms, defs, wrappers)));
wenzelm@24666
   115
wenzelm@24666
   116
wenzelm@28290
   117
fun map_axioms f = map_thy (fn (axioms, defs, wrappers) => (f axioms, defs, wrappers));
wenzelm@28290
   118
fun map_defs f = map_thy (fn (axioms, defs, wrappers) => (axioms, f defs, wrappers));
wenzelm@28290
   119
fun map_wrappers f = map_thy (fn (axioms, defs, wrappers) => (axioms, defs, f wrappers));
wenzelm@24666
   120
wenzelm@24666
   121
wenzelm@24666
   122
(* basic operations *)
wenzelm@24666
   123
wenzelm@24666
   124
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@24666
   125
val axiom_table = #2 o #axioms o rep_theory;
wenzelm@24666
   126
wenzelm@24666
   127
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@42425
   128
fun all_axioms_of thy = maps axioms_of (nodes_of thy);
wenzelm@24666
   129
wenzelm@24666
   130
val defs_of = #defs o rep_theory;
wenzelm@24666
   131
wenzelm@24666
   132
wenzelm@24666
   133
(* begin/end theory *)
wenzelm@24666
   134
wenzelm@24666
   135
val begin_wrappers = rev o #1 o #wrappers o rep_theory;
wenzelm@24666
   136
val end_wrappers = rev o #2 o #wrappers o rep_theory;
wenzelm@24666
   137
wenzelm@24666
   138
fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
wenzelm@24666
   139
fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
wenzelm@24666
   140
wenzelm@24666
   141
fun begin_theory name imports =
wenzelm@24666
   142
  let
wenzelm@42383
   143
    val thy = Context.begin_thy Context.pretty_global name imports;
wenzelm@24666
   144
    val wrappers = begin_wrappers thy;
wenzelm@33168
   145
  in
wenzelm@33168
   146
    thy
wenzelm@33168
   147
    |> Sign.local_path
wenzelm@33168
   148
    |> Sign.map_naming (Name_Space.set_theory_name name)
wenzelm@33168
   149
    |> apply_wrappers wrappers
wenzelm@33168
   150
  end;
wenzelm@24666
   151
wenzelm@24666
   152
fun end_theory thy =
wenzelm@24666
   153
  thy |> apply_wrappers (end_wrappers thy) |> Context.finish_thy;
wenzelm@24666
   154
wenzelm@16443
   155
wenzelm@3996
   156
wenzelm@35985
   157
(** primitive specifications **)
wenzelm@3814
   158
wenzelm@35985
   159
(* raw axioms *)
paulson@1526
   160
wenzelm@42375
   161
fun cert_axm ctxt (b, raw_tm) =
paulson@1526
   162
  let
wenzelm@42375
   163
    val thy = Proof_Context.theory_of ctxt;
wenzelm@32789
   164
    val t = Sign.cert_prop thy raw_tm
wenzelm@2979
   165
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   166
        | TERM (msg, _) => error msg;
wenzelm@35987
   167
    val _ = Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
wenzelm@35987
   168
wenzelm@35987
   169
    val bad_sorts =
wenzelm@35987
   170
      rev ((fold_types o fold_atyps_sorts) (fn (_, []) => I | (T, _) => insert (op =) T) t []);
wenzelm@35987
   171
    val _ = null bad_sorts orelse
wenzelm@35987
   172
      error ("Illegal sort constraints in primitive specification: " ^
wenzelm@42375
   173
        commas (map (Syntax.string_of_typ (Config.put show_sorts true ctxt)) bad_sorts));
wenzelm@42375
   174
  in (b, Sign.no_vars ctxt t) end
wenzelm@42381
   175
  handle ERROR msg => cat_error msg ("The error(s) above occurred in axiom " ^ Binding.print b);
paulson@1526
   176
wenzelm@42375
   177
fun add_axiom ctxt raw_axm thy = thy |> map_axioms (fn axioms =>
paulson@1526
   178
  let
wenzelm@42375
   179
    val axm = apsnd Logic.varify_global (cert_axm ctxt raw_axm);
wenzelm@42375
   180
    val (_, axioms') = Name_Space.define ctxt true (Sign.naming_of thy) axm axioms;
wenzelm@16443
   181
  in axioms' end);
paulson@1526
   182
paulson@1526
   183
wenzelm@19708
   184
(* dependencies *)
wenzelm@19708
   185
wenzelm@42375
   186
fun dependencies ctxt unchecked def description lhs rhs =
wenzelm@19708
   187
  let
wenzelm@42375
   188
    val thy = Proof_Context.theory_of ctxt;
wenzelm@19708
   189
    val consts = Sign.consts_of thy;
wenzelm@19727
   190
    fun prep const =
wenzelm@39133
   191
      let val Const (c, T) = Sign.no_vars ctxt (Const const)
wenzelm@35845
   192
      in (c, Consts.typargs consts (c, Logic.varifyT_global T)) end;
wenzelm@19708
   193
wenzelm@19708
   194
    val lhs_vars = Term.add_tfreesT (#2 lhs) [];
wenzelm@19708
   195
    val rhs_extras = fold (#2 #> Term.fold_atyps (fn TFree v =>
wenzelm@19708
   196
      if member (op =) lhs_vars v then I else insert (op =) v | _ => I)) rhs [];
wenzelm@19708
   197
    val _ =
wenzelm@19708
   198
      if null rhs_extras then ()
wenzelm@19708
   199
      else error ("Specification depends on extra type variables: " ^
wenzelm@39133
   200
        commas_quote (map (Syntax.string_of_typ ctxt o TFree) rhs_extras) ^
wenzelm@33701
   201
        "\nThe error(s) above occurred in " ^ quote description);
wenzelm@42384
   202
  in Defs.define ctxt unchecked def description (prep lhs) (map prep rhs) end;
wenzelm@19708
   203
wenzelm@42375
   204
fun add_deps ctxt a raw_lhs raw_rhs thy =
wenzelm@19708
   205
  let
wenzelm@19708
   206
    val lhs :: rhs = map (dest_Const o Sign.cert_term thy o Const) (raw_lhs :: raw_rhs);
wenzelm@33701
   207
    val description = if a = "" then #1 lhs ^ " axiom" else a;
wenzelm@42375
   208
  in thy |> map_defs (dependencies ctxt false NONE description lhs rhs) end;
wenzelm@42375
   209
wenzelm@42375
   210
fun add_deps_global a x y thy = add_deps (Syntax.init_pretty_global thy) a x y thy;
wenzelm@17706
   211
wenzelm@33173
   212
fun specify_const decl thy =
wenzelm@42375
   213
  let val (t as Const const, thy') = Sign.declare_const_global decl thy;
wenzelm@42375
   214
  in (t, add_deps_global "" const [] thy') end;
wenzelm@25017
   215
wenzelm@17706
   216
wenzelm@35985
   217
(* overloading *)
nipkow@9280
   218
wenzelm@42375
   219
fun check_overloading ctxt overloaded (c, T) =
wenzelm@16291
   220
  let
wenzelm@42375
   221
    val thy = Proof_Context.theory_of ctxt;
wenzelm@42375
   222
wenzelm@24763
   223
    val declT = Sign.the_const_constraint thy c
wenzelm@24763
   224
      handle TYPE (msg, _, _) => error msg;
wenzelm@35845
   225
    val T' = Logic.varifyT_global T;
wenzelm@16944
   226
wenzelm@39134
   227
    fun message sorts txt =
wenzelm@16944
   228
      [Pretty.block [Pretty.str "Specification of constant ",
wenzelm@39134
   229
        Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@39134
   230
        Pretty.quote (Syntax.pretty_typ (Config.put show_sorts sorts ctxt) T)],
wenzelm@16944
   231
        Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
wenzelm@16291
   232
  in
wenzelm@16944
   233
    if Sign.typ_instance thy (declT, T') then ()
wenzelm@16944
   234
    else if Type.raw_instance (declT, T') then
wenzelm@39134
   235
      error (message true "imposes additional sort constraints on the constant declaration")
wenzelm@16944
   236
    else if overloaded then ()
wenzelm@39134
   237
    else warning (message false "is strictly less general than the declared type")
nipkow@9280
   238
  end;
nipkow@9280
   239
wenzelm@3767
   240
wenzelm@35985
   241
(* definitional axioms *)
wenzelm@35985
   242
wenzelm@35985
   243
local
wenzelm@16291
   244
wenzelm@42394
   245
fun check_def ctxt thy unchecked overloaded (b, tm) defs =
wenzelm@16291
   246
  let
haftmann@29581
   247
    val name = Sign.full_name thy b;
wenzelm@35988
   248
    val ((lhs, rhs), _) = Primitive_Defs.dest_def ctxt Term.is_Const (K false) (K false) tm
wenzelm@35988
   249
      handle TERM (msg, _) => error msg;
wenzelm@35988
   250
    val lhs_const = Term.dest_Const (Term.head_of lhs);
wenzelm@16944
   251
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@42375
   252
    val _ = check_overloading ctxt overloaded lhs_const;
wenzelm@42375
   253
  in defs |> dependencies ctxt unchecked (SOME name) name lhs_const rhs_consts end
wenzelm@18678
   254
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@42381
   255
   [Pretty.str ("The error(s) above occurred in definition " ^ Binding.print b ^ ":"),
wenzelm@42375
   256
    Pretty.fbrk, Pretty.quote (Syntax.pretty_term ctxt tm)]));
wenzelm@3767
   257
wenzelm@16291
   258
in
wenzelm@16291
   259
wenzelm@42375
   260
fun add_def ctxt unchecked overloaded raw_axm thy =
wenzelm@42375
   261
  let val axm = cert_axm ctxt raw_axm in
wenzelm@35985
   262
    thy
wenzelm@42394
   263
    |> map_defs (check_def ctxt thy unchecked overloaded axm)
wenzelm@42375
   264
    |> add_axiom ctxt axm
wenzelm@35985
   265
  end;
wenzelm@16291
   266
wenzelm@16291
   267
end;
wenzelm@3767
   268
wenzelm@3767
   269
wenzelm@16443
   270
(* add_finals(_i) *)
skalberg@14223
   271
wenzelm@16291
   272
local
wenzelm@16291
   273
wenzelm@17706
   274
fun gen_add_finals prep_term overloaded args thy =
skalberg@14223
   275
  let
wenzelm@42375
   276
    val ctxt = Syntax.init_pretty_global thy;
wenzelm@17706
   277
    fun const_of (Const const) = const
wenzelm@17706
   278
      | const_of (Free _) = error "Attempt to finalize variable (or undeclared constant)"
wenzelm@17706
   279
      | const_of _ = error "Attempt to finalize non-constant term";
wenzelm@42375
   280
    fun specify (c, T) = dependencies ctxt false NONE (c ^ " axiom") (c, T) [];
wenzelm@42375
   281
    val finalize =
wenzelm@42375
   282
      specify o tap (check_overloading ctxt overloaded) o const_of o
wenzelm@42375
   283
        Sign.cert_term thy o prep_term ctxt;
wenzelm@17706
   284
  in thy |> map_defs (fold finalize args) end;
wenzelm@16291
   285
skalberg@14223
   286
in
wenzelm@16291
   287
haftmann@29581
   288
val add_finals_i = gen_add_finals (K I);
wenzelm@42375
   289
val add_finals = gen_add_finals Syntax.read_term;
wenzelm@16291
   290
skalberg@14223
   291
end;
skalberg@14223
   292
paulson@1526
   293
end;