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