src/Pure/theory.ML
author wenzelm
Fri Apr 12 14:54:14 2013 +0200 (2013-04-12)
changeset 51700 c8f2bad67dbb
parent 50201 c26369c9eda6
child 52696 38466f4f3483
permissions -rw-r--r--
tuned signature;
tuned comments;
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@48927
    23
  val get_markup: theory -> Markup.T
wenzelm@33095
    24
  val axiom_space: theory -> Name_Space.T
wenzelm@22684
    25
  val axiom_table: theory -> term Symtab.table
wenzelm@16339
    26
  val axioms_of: theory -> (string * term) list
wenzelm@16339
    27
  val all_axioms_of: theory -> (string * term) list
wenzelm@24666
    28
  val defs_of: theory -> Defs.T
wenzelm@24666
    29
  val at_begin: (theory -> theory option) -> theory -> theory
wenzelm@24666
    30
  val at_end: (theory -> theory option) -> theory -> theory
wenzelm@48927
    31
  val begin_theory: string * Position.T -> theory list -> theory
wenzelm@24666
    32
  val end_theory: theory -> theory
wenzelm@42375
    33
  val add_axiom: Proof.context -> binding * term -> theory -> theory
wenzelm@42375
    34
  val add_deps: Proof.context -> string -> string * typ -> (string * typ) list -> theory -> theory
wenzelm@42375
    35
  val add_deps_global: string -> string * typ -> (string * typ) list -> theory -> theory
wenzelm@42375
    36
  val add_def: Proof.context -> bool -> bool -> binding * term -> theory -> theory
wenzelm@33173
    37
  val specify_const: (binding * typ) * mixfix -> theory -> term * theory
wenzelm@46974
    38
  val check_overloading: Proof.context -> bool -> string * typ -> unit
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@48927
    84
 {pos: Position.T,
wenzelm@48927
    85
  id: serial,
wenzelm@48927
    86
  axioms: term Name_Space.table,
wenzelm@24666
    87
  defs: Defs.T,
wenzelm@24666
    88
  wrappers: wrapper list * wrapper list};
wenzelm@24666
    89
wenzelm@48927
    90
fun make_thy (pos, id, axioms, defs, wrappers) =
wenzelm@48927
    91
  Thy {pos = pos, id = id, axioms = axioms, defs = defs, wrappers = wrappers};
wenzelm@24666
    92
wenzelm@42016
    93
structure Thy = Theory_Data_PP
wenzelm@24666
    94
(
wenzelm@24666
    95
  type T = thy;
wenzelm@33159
    96
  val empty_axioms = Name_Space.empty_table "axiom" : term Name_Space.table;
wenzelm@48927
    97
  val empty = make_thy (Position.none, 0, empty_axioms, Defs.empty, ([], []));
wenzelm@24666
    98
wenzelm@48927
    99
  fun extend (Thy {pos = _, id = _, axioms = _, defs, wrappers}) =
wenzelm@48927
   100
    make_thy (Position.none, 0, empty_axioms, defs, wrappers);
wenzelm@24666
   101
wenzelm@24666
   102
  fun merge pp (thy1, thy2) =
wenzelm@24666
   103
    let
wenzelm@42389
   104
      val ctxt = Syntax.init_pretty pp;
wenzelm@48927
   105
      val Thy {pos = _, id = _, axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
wenzelm@48927
   106
      val Thy {pos = _, id = _, axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
wenzelm@24666
   107
wenzelm@33096
   108
      val axioms' = empty_axioms;
wenzelm@42389
   109
      val defs' = Defs.merge ctxt (defs1, defs2);
wenzelm@24666
   110
      val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
wenzelm@24666
   111
      val ens' = Library.merge (eq_snd op =) (ens1, ens2);
wenzelm@48927
   112
    in make_thy (Position.none, 0, axioms', defs', (bgs', ens')) end;
wenzelm@24666
   113
);
wenzelm@24666
   114
wenzelm@42016
   115
fun rep_theory thy = Thy.get thy |> (fn Thy args => args);
wenzelm@24666
   116
wenzelm@48927
   117
fun map_thy f = Thy.map (fn (Thy {pos, id, axioms, defs, wrappers}) =>
wenzelm@48927
   118
  make_thy (f (pos, id, axioms, defs, wrappers)));
wenzelm@48927
   119
wenzelm@48927
   120
fun map_axioms f =
wenzelm@48927
   121
  map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, f axioms, defs, wrappers));
wenzelm@48927
   122
wenzelm@48927
   123
fun map_defs f =
wenzelm@48927
   124
  map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, axioms, f defs, wrappers));
wenzelm@48927
   125
wenzelm@48927
   126
fun map_wrappers f =
wenzelm@48927
   127
  map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, axioms, defs, f wrappers));
wenzelm@24666
   128
wenzelm@24666
   129
wenzelm@48927
   130
(* entity markup *)
wenzelm@48927
   131
wenzelm@48927
   132
fun theory_markup def name id pos =
wenzelm@48927
   133
  if id = 0 then Markup.empty
wenzelm@48927
   134
  else
wenzelm@48927
   135
    Markup.properties (Position.entity_properties_of def id pos)
wenzelm@50201
   136
      (Markup.entity Markup.theoryN name);
wenzelm@48927
   137
wenzelm@48929
   138
fun init_markup (name, pos) thy =
wenzelm@48929
   139
  let
wenzelm@48929
   140
    val id = serial ();
wenzelm@48929
   141
    val _ = Position.report pos (theory_markup true name id pos);
wenzelm@48929
   142
  in map_thy (fn (_, _, axioms, defs, wrappers) => (pos, id, axioms, defs, wrappers)) thy end;
wenzelm@48929
   143
wenzelm@48927
   144
fun get_markup thy =
wenzelm@48927
   145
  let val {pos, id, ...} = rep_theory thy
wenzelm@48927
   146
  in theory_markup false (Context.theory_name thy) id pos end;
wenzelm@24666
   147
wenzelm@24666
   148
wenzelm@24666
   149
(* basic operations *)
wenzelm@24666
   150
wenzelm@24666
   151
val axiom_space = #1 o #axioms o rep_theory;
wenzelm@24666
   152
val axiom_table = #2 o #axioms o rep_theory;
wenzelm@24666
   153
wenzelm@24666
   154
val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
wenzelm@42425
   155
fun all_axioms_of thy = maps axioms_of (nodes_of thy);
wenzelm@24666
   156
wenzelm@24666
   157
val defs_of = #defs o rep_theory;
wenzelm@24666
   158
wenzelm@24666
   159
wenzelm@24666
   160
(* begin/end theory *)
wenzelm@24666
   161
wenzelm@24666
   162
val begin_wrappers = rev o #1 o #wrappers o rep_theory;
wenzelm@24666
   163
val end_wrappers = rev o #2 o #wrappers o rep_theory;
wenzelm@24666
   164
wenzelm@24666
   165
fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
wenzelm@24666
   166
fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
wenzelm@24666
   167
wenzelm@48927
   168
fun begin_theory (name, pos) imports =
wenzelm@48638
   169
  if name = Context.PureN then
wenzelm@48929
   170
    (case imports of
wenzelm@48929
   171
      [thy] => init_markup (name, pos) thy
wenzelm@48929
   172
    | _ => error "Bad bootstrapping of theory Pure")
wenzelm@48638
   173
  else
wenzelm@48638
   174
    let
wenzelm@48929
   175
      val thy = Context.begin_thy Context.pretty_global name imports;
wenzelm@48638
   176
      val wrappers = begin_wrappers thy;
wenzelm@48638
   177
    in
wenzelm@48638
   178
      thy
wenzelm@48929
   179
      |> init_markup (name, pos)
wenzelm@48638
   180
      |> Sign.local_path
wenzelm@48638
   181
      |> Sign.map_naming (Name_Space.set_theory_name name)
wenzelm@48638
   182
      |> apply_wrappers wrappers
wenzelm@48638
   183
      |> tap (Syntax.force_syntax o Sign.syn_of)
wenzelm@48638
   184
    end;
wenzelm@24666
   185
wenzelm@24666
   186
fun end_theory thy =
wenzelm@24666
   187
  thy |> apply_wrappers (end_wrappers thy) |> Context.finish_thy;
wenzelm@24666
   188
wenzelm@16443
   189
wenzelm@3996
   190
wenzelm@35985
   191
(** primitive specifications **)
wenzelm@3814
   192
wenzelm@35985
   193
(* raw axioms *)
paulson@1526
   194
wenzelm@42375
   195
fun cert_axm ctxt (b, raw_tm) =
paulson@1526
   196
  let
wenzelm@42375
   197
    val thy = Proof_Context.theory_of ctxt;
wenzelm@32789
   198
    val t = Sign.cert_prop thy raw_tm
wenzelm@2979
   199
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   200
        | TERM (msg, _) => error msg;
wenzelm@35987
   201
    val _ = Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
wenzelm@35987
   202
wenzelm@35987
   203
    val bad_sorts =
wenzelm@35987
   204
      rev ((fold_types o fold_atyps_sorts) (fn (_, []) => I | (T, _) => insert (op =) T) t []);
wenzelm@35987
   205
    val _ = null bad_sorts orelse
wenzelm@35987
   206
      error ("Illegal sort constraints in primitive specification: " ^
wenzelm@42375
   207
        commas (map (Syntax.string_of_typ (Config.put show_sorts true ctxt)) bad_sorts));
wenzelm@42375
   208
  in (b, Sign.no_vars ctxt t) end
wenzelm@42381
   209
  handle ERROR msg => cat_error msg ("The error(s) above occurred in axiom " ^ Binding.print b);
paulson@1526
   210
wenzelm@42375
   211
fun add_axiom ctxt raw_axm thy = thy |> map_axioms (fn axioms =>
paulson@1526
   212
  let
wenzelm@42375
   213
    val axm = apsnd Logic.varify_global (cert_axm ctxt raw_axm);
wenzelm@47005
   214
    val (_, axioms') = Name_Space.define (Sign.inherit_naming thy ctxt) true axm axioms;
wenzelm@16443
   215
  in axioms' end);
paulson@1526
   216
paulson@1526
   217
wenzelm@19708
   218
(* dependencies *)
wenzelm@19708
   219
wenzelm@42375
   220
fun dependencies ctxt unchecked def description lhs rhs =
wenzelm@19708
   221
  let
wenzelm@42375
   222
    val thy = Proof_Context.theory_of ctxt;
wenzelm@19708
   223
    val consts = Sign.consts_of thy;
wenzelm@19727
   224
    fun prep const =
wenzelm@39133
   225
      let val Const (c, T) = Sign.no_vars ctxt (Const const)
wenzelm@35845
   226
      in (c, Consts.typargs consts (c, Logic.varifyT_global 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@39133
   234
        commas_quote (map (Syntax.string_of_typ ctxt o TFree) rhs_extras) ^
wenzelm@33701
   235
        "\nThe error(s) above occurred in " ^ quote description);
wenzelm@42384
   236
  in Defs.define ctxt unchecked def description (prep lhs) (map prep rhs) end;
wenzelm@19708
   237
wenzelm@42375
   238
fun add_deps ctxt 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@33701
   241
    val description = if a = "" then #1 lhs ^ " axiom" else a;
wenzelm@42375
   242
  in thy |> map_defs (dependencies ctxt false NONE description lhs rhs) end;
wenzelm@42375
   243
wenzelm@42375
   244
fun add_deps_global a x y thy = add_deps (Syntax.init_pretty_global thy) a x y thy;
wenzelm@17706
   245
wenzelm@33173
   246
fun specify_const decl thy =
wenzelm@42375
   247
  let val (t as Const const, thy') = Sign.declare_const_global decl thy;
wenzelm@42375
   248
  in (t, add_deps_global "" const [] thy') end;
wenzelm@25017
   249
wenzelm@17706
   250
wenzelm@35985
   251
(* overloading *)
nipkow@9280
   252
wenzelm@42375
   253
fun check_overloading ctxt overloaded (c, T) =
wenzelm@16291
   254
  let
wenzelm@42375
   255
    val thy = Proof_Context.theory_of ctxt;
wenzelm@42375
   256
wenzelm@24763
   257
    val declT = Sign.the_const_constraint thy c
wenzelm@24763
   258
      handle TYPE (msg, _, _) => error msg;
wenzelm@35845
   259
    val T' = Logic.varifyT_global T;
wenzelm@16944
   260
wenzelm@39134
   261
    fun message sorts txt =
wenzelm@16944
   262
      [Pretty.block [Pretty.str "Specification of constant ",
wenzelm@39134
   263
        Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@39134
   264
        Pretty.quote (Syntax.pretty_typ (Config.put show_sorts sorts ctxt) T)],
wenzelm@16944
   265
        Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
wenzelm@16291
   266
  in
wenzelm@16944
   267
    if Sign.typ_instance thy (declT, T') then ()
wenzelm@16944
   268
    else if Type.raw_instance (declT, T') then
wenzelm@39134
   269
      error (message true "imposes additional sort constraints on the constant declaration")
wenzelm@16944
   270
    else if overloaded then ()
wenzelm@39134
   271
    else warning (message false "is strictly less general than the declared type")
nipkow@9280
   272
  end;
nipkow@9280
   273
wenzelm@3767
   274
wenzelm@35985
   275
(* definitional axioms *)
wenzelm@35985
   276
wenzelm@35985
   277
local
wenzelm@16291
   278
wenzelm@42394
   279
fun check_def ctxt thy unchecked overloaded (b, tm) defs =
wenzelm@16291
   280
  let
haftmann@29581
   281
    val name = Sign.full_name thy b;
wenzelm@35988
   282
    val ((lhs, rhs), _) = Primitive_Defs.dest_def ctxt Term.is_Const (K false) (K false) tm
wenzelm@35988
   283
      handle TERM (msg, _) => error msg;
wenzelm@35988
   284
    val lhs_const = Term.dest_Const (Term.head_of lhs);
wenzelm@16944
   285
    val rhs_consts = fold_aterms (fn Const const => insert (op =) const | _ => I) rhs [];
wenzelm@42375
   286
    val _ = check_overloading ctxt overloaded lhs_const;
wenzelm@42375
   287
  in defs |> dependencies ctxt unchecked (SOME name) name lhs_const rhs_consts end
wenzelm@18678
   288
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@42381
   289
   [Pretty.str ("The error(s) above occurred in definition " ^ Binding.print b ^ ":"),
wenzelm@42375
   290
    Pretty.fbrk, Pretty.quote (Syntax.pretty_term ctxt tm)]));
wenzelm@3767
   291
wenzelm@16291
   292
in
wenzelm@16291
   293
wenzelm@42375
   294
fun add_def ctxt unchecked overloaded raw_axm thy =
wenzelm@42375
   295
  let val axm = cert_axm ctxt raw_axm in
wenzelm@35985
   296
    thy
wenzelm@42394
   297
    |> map_defs (check_def ctxt thy unchecked overloaded axm)
wenzelm@42375
   298
    |> add_axiom ctxt axm
wenzelm@35985
   299
  end;
wenzelm@16291
   300
wenzelm@16291
   301
end;
wenzelm@3767
   302
skalberg@14223
   303
end;