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