src/Pure/theory.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (2017-09-26)
changeset 66695 91500c024c7f
parent 65508 a72ab197e681
child 67380 8bef51521f21
permissions -rw-r--r--
tuned;
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@22684
     9
  val parents_of: theory -> theory list
wenzelm@22684
    10
  val ancestors_of: theory -> theory list
wenzelm@42425
    11
  val nodes_of: theory -> theory list
wenzelm@53171
    12
  val setup: (theory -> theory) -> unit
wenzelm@59930
    13
  val local_setup: (Proof.context -> Proof.context) -> unit
wenzelm@48927
    14
  val get_markup: theory -> Markup.T
wenzelm@60099
    15
  val check: Proof.context -> string * Position.T -> theory
wenzelm@56025
    16
  val axiom_table: theory -> term Name_Space.table
wenzelm@33095
    17
  val axiom_space: theory -> Name_Space.T
wenzelm@16339
    18
  val axioms_of: theory -> (string * term) list
wenzelm@16339
    19
  val all_axioms_of: theory -> (string * term) list
wenzelm@24666
    20
  val defs_of: theory -> Defs.T
wenzelm@24666
    21
  val at_begin: (theory -> theory option) -> theory -> theory
wenzelm@24666
    22
  val at_end: (theory -> theory option) -> theory -> theory
wenzelm@48927
    23
  val begin_theory: string * Position.T -> theory list -> theory
wenzelm@24666
    24
  val end_theory: theory -> theory
wenzelm@42375
    25
  val add_axiom: Proof.context -> binding * term -> theory -> theory
wenzelm@61255
    26
  val const_dep: theory -> string * typ -> Defs.entry
wenzelm@61255
    27
  val type_dep: string * typ list -> Defs.entry
wenzelm@61261
    28
  val add_deps: Defs.context -> string -> Defs.entry -> Defs.entry list -> theory -> theory
wenzelm@61255
    29
  val add_deps_global: string -> Defs.entry -> Defs.entry list -> theory -> theory
wenzelm@61261
    30
  val add_def: Defs.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 parents_of = Context.parents_of;
wenzelm@16443
    42
val ancestors_of = Context.ancestors_of;
wenzelm@42425
    43
fun nodes_of thy = thy :: ancestors_of thy;
wenzelm@16443
    44
wenzelm@53171
    45
fun setup f = Context.>> (Context.map_theory f);
wenzelm@59930
    46
fun local_setup f = Context.>> (Context.map_proof f);
wenzelm@53171
    47
wenzelm@24666
    48
wenzelm@24666
    49
wenzelm@25059
    50
(** datatype thy **)
wenzelm@24666
    51
wenzelm@24666
    52
type wrapper = (theory -> theory option) * stamp;
wenzelm@24666
    53
wenzelm@24666
    54
fun apply_wrappers (wrappers: wrapper list) =
wenzelm@25059
    55
  perhaps (perhaps_loop (perhaps_apply (map fst wrappers)));
wenzelm@24666
    56
wenzelm@24666
    57
datatype thy = Thy of
wenzelm@48927
    58
 {pos: Position.T,
wenzelm@48927
    59
  id: serial,
wenzelm@48927
    60
  axioms: term Name_Space.table,
wenzelm@24666
    61
  defs: Defs.T,
wenzelm@24666
    62
  wrappers: wrapper list * wrapper list};
wenzelm@24666
    63
wenzelm@48927
    64
fun make_thy (pos, id, axioms, defs, wrappers) =
wenzelm@48927
    65
  Thy {pos = pos, id = id, axioms = axioms, defs = defs, wrappers = wrappers};
wenzelm@24666
    66
wenzelm@61262
    67
structure Thy = Theory_Data'
wenzelm@24666
    68
(
wenzelm@24666
    69
  type T = thy;
wenzelm@33159
    70
  val empty_axioms = Name_Space.empty_table "axiom" : term Name_Space.table;
wenzelm@48927
    71
  val empty = make_thy (Position.none, 0, empty_axioms, Defs.empty, ([], []));
wenzelm@24666
    72
wenzelm@48927
    73
  fun extend (Thy {pos = _, id = _, axioms = _, defs, wrappers}) =
wenzelm@48927
    74
    make_thy (Position.none, 0, empty_axioms, defs, wrappers);
wenzelm@24666
    75
wenzelm@61262
    76
  fun merge old_thys (thy1, thy2) =
wenzelm@24666
    77
    let
wenzelm@48927
    78
      val Thy {pos = _, id = _, axioms = _, defs = defs1, wrappers = (bgs1, ens1)} = thy1;
wenzelm@48927
    79
      val Thy {pos = _, id = _, axioms = _, defs = defs2, wrappers = (bgs2, ens2)} = thy2;
wenzelm@24666
    80
wenzelm@33096
    81
      val axioms' = empty_axioms;
wenzelm@61262
    82
      val defs' = Defs.merge (Defs.global_context (fst old_thys)) (defs1, defs2);
wenzelm@24666
    83
      val bgs' = Library.merge (eq_snd op =) (bgs1, bgs2);
wenzelm@24666
    84
      val ens' = Library.merge (eq_snd op =) (ens1, ens2);
wenzelm@48927
    85
    in make_thy (Position.none, 0, axioms', defs', (bgs', ens')) end;
wenzelm@24666
    86
);
wenzelm@24666
    87
wenzelm@42016
    88
fun rep_theory thy = Thy.get thy |> (fn Thy args => args);
wenzelm@24666
    89
wenzelm@48927
    90
fun map_thy f = Thy.map (fn (Thy {pos, id, axioms, defs, wrappers}) =>
wenzelm@48927
    91
  make_thy (f (pos, id, axioms, defs, wrappers)));
wenzelm@48927
    92
wenzelm@48927
    93
fun map_axioms f =
wenzelm@48927
    94
  map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, f axioms, defs, wrappers));
wenzelm@48927
    95
wenzelm@48927
    96
fun map_defs f =
wenzelm@48927
    97
  map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, axioms, f defs, wrappers));
wenzelm@48927
    98
wenzelm@48927
    99
fun map_wrappers f =
wenzelm@48927
   100
  map_thy (fn (pos, id, axioms, defs, wrappers) => (pos, id, axioms, defs, f wrappers));
wenzelm@24666
   101
wenzelm@24666
   102
wenzelm@48927
   103
(* entity markup *)
wenzelm@48927
   104
wenzelm@48927
   105
fun theory_markup def name id pos =
wenzelm@48927
   106
  if id = 0 then Markup.empty
wenzelm@48927
   107
  else
wenzelm@48927
   108
    Markup.properties (Position.entity_properties_of def id pos)
wenzelm@50201
   109
      (Markup.entity Markup.theoryN name);
wenzelm@48927
   110
wenzelm@48929
   111
fun init_markup (name, pos) thy =
wenzelm@48929
   112
  let
wenzelm@48929
   113
    val id = serial ();
wenzelm@48929
   114
    val _ = Position.report pos (theory_markup true name id pos);
wenzelm@48929
   115
  in map_thy (fn (_, _, axioms, defs, wrappers) => (pos, id, axioms, defs, wrappers)) thy end;
wenzelm@48929
   116
wenzelm@48927
   117
fun get_markup thy =
wenzelm@48927
   118
  let val {pos, id, ...} = rep_theory thy
wenzelm@48927
   119
  in theory_markup false (Context.theory_name thy) id pos end;
wenzelm@24666
   120
wenzelm@60099
   121
fun check ctxt (name, pos) =
wenzelm@60099
   122
  let
wenzelm@60099
   123
    val thy = Proof_Context.theory_of ctxt;
wenzelm@60099
   124
    val thy' =
wenzelm@60099
   125
      Context.get_theory thy name
wenzelm@60099
   126
        handle ERROR msg =>
wenzelm@60099
   127
          let
wenzelm@60099
   128
            val completion =
wenzelm@60099
   129
              Completion.make (name, pos)
wenzelm@60099
   130
                (fn completed =>
wenzelm@60099
   131
                  map Context.theory_name (ancestors_of thy)
wenzelm@60099
   132
                  |> filter completed
wenzelm@60099
   133
                  |> sort_strings
wenzelm@60099
   134
                  |> map (fn a => (a, (Markup.theoryN, a))));
wenzelm@60099
   135
            val report = Markup.markup_report (Completion.reported_text completion);
wenzelm@60099
   136
          in error (msg ^ Position.here pos ^ report) end;
wenzelm@60099
   137
    val _ = Context_Position.report ctxt pos (get_markup thy');
wenzelm@60099
   138
  in thy' end;
wenzelm@60099
   139
wenzelm@24666
   140
wenzelm@24666
   141
(* basic operations *)
wenzelm@24666
   142
wenzelm@56025
   143
val axiom_table = #axioms o rep_theory;
wenzelm@56025
   144
val axiom_space = Name_Space.space_of_table o axiom_table;
wenzelm@24666
   145
wenzelm@56025
   146
fun axioms_of thy = rev (Name_Space.fold_table cons (axiom_table thy) []);
wenzelm@42425
   147
fun all_axioms_of thy = maps axioms_of (nodes_of thy);
wenzelm@24666
   148
wenzelm@24666
   149
val defs_of = #defs o rep_theory;
wenzelm@24666
   150
wenzelm@24666
   151
wenzelm@24666
   152
(* begin/end theory *)
wenzelm@24666
   153
wenzelm@24666
   154
val begin_wrappers = rev o #1 o #wrappers o rep_theory;
wenzelm@24666
   155
val end_wrappers = rev o #2 o #wrappers o rep_theory;
wenzelm@24666
   156
wenzelm@24666
   157
fun at_begin f = map_wrappers (apfst (cons (f, stamp ())));
wenzelm@24666
   158
fun at_end f = map_wrappers (apsnd (cons (f, stamp ())));
wenzelm@24666
   159
wenzelm@48927
   160
fun begin_theory (name, pos) imports =
wenzelm@48638
   161
  if name = Context.PureN then
wenzelm@48929
   162
    (case imports of
wenzelm@48929
   163
      [thy] => init_markup (name, pos) thy
wenzelm@48929
   164
    | _ => error "Bad bootstrapping of theory Pure")
wenzelm@48638
   165
  else
wenzelm@48638
   166
    let
wenzelm@61262
   167
      val thy = Context.begin_thy name imports;
wenzelm@48638
   168
      val wrappers = begin_wrappers thy;
wenzelm@48638
   169
    in
wenzelm@48638
   170
      thy
wenzelm@48929
   171
      |> init_markup (name, pos)
wenzelm@48638
   172
      |> Sign.local_path
wenzelm@65508
   173
      |> Sign.map_naming (Name_Space.set_theory_name (Long_Name.base_name name))
wenzelm@48638
   174
      |> apply_wrappers wrappers
wenzelm@48638
   175
      |> tap (Syntax.force_syntax o Sign.syn_of)
wenzelm@48638
   176
    end;
wenzelm@24666
   177
wenzelm@24666
   178
fun end_theory thy =
wenzelm@56057
   179
  thy
wenzelm@56057
   180
  |> apply_wrappers (end_wrappers thy)
wenzelm@56057
   181
  |> Sign.change_check
wenzelm@56057
   182
  |> Context.finish_thy;
wenzelm@24666
   183
wenzelm@16443
   184
wenzelm@3996
   185
wenzelm@35985
   186
(** primitive specifications **)
wenzelm@3814
   187
wenzelm@35985
   188
(* raw axioms *)
paulson@1526
   189
wenzelm@42375
   190
fun cert_axm ctxt (b, raw_tm) =
paulson@1526
   191
  let
wenzelm@42375
   192
    val thy = Proof_Context.theory_of ctxt;
wenzelm@32789
   193
    val t = Sign.cert_prop thy raw_tm
wenzelm@2979
   194
      handle TYPE (msg, _, _) => error msg
wenzelm@16291
   195
        | TERM (msg, _) => error msg;
wenzelm@35987
   196
    val _ = Term.no_dummy_patterns t handle TERM (msg, _) => error msg;
wenzelm@35987
   197
wenzelm@35987
   198
    val bad_sorts =
wenzelm@35987
   199
      rev ((fold_types o fold_atyps_sorts) (fn (_, []) => I | (T, _) => insert (op =) T) t []);
wenzelm@35987
   200
    val _ = null bad_sorts orelse
wenzelm@35987
   201
      error ("Illegal sort constraints in primitive specification: " ^
wenzelm@42375
   202
        commas (map (Syntax.string_of_typ (Config.put show_sorts true ctxt)) bad_sorts));
wenzelm@42375
   203
  in (b, Sign.no_vars ctxt t) end
wenzelm@42381
   204
  handle ERROR msg => cat_error msg ("The error(s) above occurred in axiom " ^ Binding.print b);
paulson@1526
   205
wenzelm@42375
   206
fun add_axiom ctxt raw_axm thy = thy |> map_axioms (fn axioms =>
paulson@1526
   207
  let
wenzelm@42375
   208
    val axm = apsnd Logic.varify_global (cert_axm ctxt raw_axm);
wenzelm@61949
   209
    val context = ctxt
wenzelm@61949
   210
      |> Sign.inherit_naming thy
wenzelm@61949
   211
      |> Context_Position.set_visible_generic false;
wenzelm@61949
   212
    val (_, axioms') = Name_Space.define context true axm axioms;
wenzelm@16443
   213
  in axioms' end);
paulson@1526
   214
paulson@1526
   215
wenzelm@19708
   216
(* dependencies *)
wenzelm@19708
   217
wenzelm@61256
   218
fun const_dep thy (c, T) = ((Defs.Const, c), Sign.const_typargs thy (c, T));
wenzelm@61255
   219
fun type_dep (c, args) = ((Defs.Type, c), args);
wenzelm@61246
   220
wenzelm@61261
   221
fun dependencies (context as (ctxt, _)) unchecked def description lhs rhs =
wenzelm@19708
   222
  let
wenzelm@61255
   223
    fun prep (item, args) =
wenzelm@61255
   224
      (case fold Term.add_tvarsT args [] of
wenzelm@61255
   225
        [] => (item, map Logic.varifyT_global args)
wenzelm@61255
   226
      | vs => raise TYPE ("Illegal schematic type variable(s)", map TVar vs, []));
wenzelm@61249
   227
wenzelm@61255
   228
    val lhs_vars = fold Term.add_tfreesT (snd lhs) [];
wenzelm@61255
   229
    val rhs_extras =
wenzelm@61255
   230
      fold (fn (_, args) => args |> (fold o Term.fold_atyps) (fn TFree v =>
wenzelm@61255
   231
        if member (op =) lhs_vars v then I else insert (op =) v)) rhs [];
wenzelm@19708
   232
    val _ =
wenzelm@19708
   233
      if null rhs_extras then ()
wenzelm@19708
   234
      else error ("Specification depends on extra type variables: " ^
wenzelm@39133
   235
        commas_quote (map (Syntax.string_of_typ ctxt o TFree) rhs_extras) ^
wenzelm@33701
   236
        "\nThe error(s) above occurred in " ^ quote description);
wenzelm@61261
   237
  in Defs.define context unchecked def description (prep lhs) (map prep rhs) end;
wenzelm@19708
   238
wenzelm@61256
   239
fun cert_entry thy ((Defs.Const, c), args) =
wenzelm@61255
   240
      Sign.cert_term thy (Const (c, Sign.const_instance thy (c, args)))
wenzelm@61255
   241
      |> dest_Const |> const_dep thy
wenzelm@61255
   242
  | cert_entry thy ((Defs.Type, c), args) =
wenzelm@61255
   243
      Sign.certify_typ thy (Type (c, args)) |> dest_Type |> type_dep;
wenzelm@61255
   244
wenzelm@61261
   245
fun add_deps context a raw_lhs raw_rhs thy =
wenzelm@19708
   246
  let
wenzelm@61255
   247
    val (lhs as ((_, lhs_name), _)) :: rhs = map (cert_entry thy) (raw_lhs :: raw_rhs);
wenzelm@61255
   248
    val description = if a = "" then lhs_name ^ " axiom" else a;
wenzelm@61261
   249
  in thy |> map_defs (dependencies context false NONE description lhs rhs) end;
wenzelm@42375
   250
wenzelm@61255
   251
fun add_deps_global a x y thy =
wenzelm@61262
   252
  add_deps (Defs.global_context thy) a x y thy;
wenzelm@17706
   253
wenzelm@33173
   254
fun specify_const decl thy =
wenzelm@61255
   255
  let val (t, thy') = Sign.declare_const_global decl thy;
wenzelm@61255
   256
  in (t, add_deps_global "" (const_dep thy' (dest_Const t)) [] thy') end;
wenzelm@25017
   257
wenzelm@17706
   258
wenzelm@35985
   259
(* overloading *)
nipkow@9280
   260
wenzelm@42375
   261
fun check_overloading ctxt overloaded (c, T) =
wenzelm@16291
   262
  let
wenzelm@42375
   263
    val thy = Proof_Context.theory_of ctxt;
wenzelm@42375
   264
wenzelm@24763
   265
    val declT = Sign.the_const_constraint thy c
wenzelm@24763
   266
      handle TYPE (msg, _, _) => error msg;
wenzelm@35845
   267
    val T' = Logic.varifyT_global T;
wenzelm@16944
   268
wenzelm@39134
   269
    fun message sorts txt =
wenzelm@16944
   270
      [Pretty.block [Pretty.str "Specification of constant ",
wenzelm@39134
   271
        Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@39134
   272
        Pretty.quote (Syntax.pretty_typ (Config.put show_sorts sorts ctxt) T)],
wenzelm@16944
   273
        Pretty.str txt] |> Pretty.chunks |> Pretty.string_of;
wenzelm@16291
   274
  in
wenzelm@16944
   275
    if Sign.typ_instance thy (declT, T') then ()
wenzelm@16944
   276
    else if Type.raw_instance (declT, T') then
wenzelm@39134
   277
      error (message true "imposes additional sort constraints on the constant declaration")
wenzelm@16944
   278
    else if overloaded then ()
wenzelm@57510
   279
    else
wenzelm@57510
   280
      error (message false "is strictly less general than the declared type (overloading required)")
nipkow@9280
   281
  end;
nipkow@9280
   282
wenzelm@3767
   283
wenzelm@35985
   284
(* definitional axioms *)
wenzelm@35985
   285
wenzelm@35985
   286
local
wenzelm@16291
   287
wenzelm@61261
   288
fun check_def (context as (ctxt, _)) thy unchecked overloaded (b, tm) defs =
wenzelm@16291
   289
  let
haftmann@29581
   290
    val name = Sign.full_name thy b;
wenzelm@63395
   291
    val ((lhs, rhs), _, _) =
wenzelm@63042
   292
      Primitive_Defs.dest_def ctxt
wenzelm@63042
   293
        {check_head = Term.is_Const,
wenzelm@63042
   294
         check_free_lhs = K true,
wenzelm@63042
   295
         check_free_rhs = K false,
wenzelm@63042
   296
         check_tfree = K false} tm
wenzelm@35988
   297
      handle TERM (msg, _) => error msg;
wenzelm@35988
   298
    val lhs_const = Term.dest_Const (Term.head_of lhs);
wenzelm@61248
   299
wenzelm@61255
   300
    val rhs_consts =
wenzelm@61255
   301
      fold_aterms (fn Const const => insert (op =) (const_dep thy const) | _ => I) rhs [];
wenzelm@61248
   302
    val rhs_types =
wenzelm@61255
   303
      (fold_types o fold_subtypes) (fn Type t => insert (op =) (type_dep t) | _ => I) rhs [];
wenzelm@61248
   304
    val rhs_deps = rhs_consts @ rhs_types;
wenzelm@61248
   305
wenzelm@42375
   306
    val _ = check_overloading ctxt overloaded lhs_const;
wenzelm@61261
   307
  in defs |> dependencies context unchecked (SOME name) name (const_dep thy lhs_const) rhs_deps end
wenzelm@18678
   308
  handle ERROR msg => cat_error msg (Pretty.string_of (Pretty.block
wenzelm@42381
   309
   [Pretty.str ("The error(s) above occurred in definition " ^ Binding.print b ^ ":"),
wenzelm@42375
   310
    Pretty.fbrk, Pretty.quote (Syntax.pretty_term ctxt tm)]));
wenzelm@3767
   311
wenzelm@16291
   312
in
wenzelm@16291
   313
wenzelm@61261
   314
fun add_def (context as (ctxt, _)) unchecked overloaded raw_axm thy =
wenzelm@42375
   315
  let val axm = cert_axm ctxt raw_axm in
wenzelm@35985
   316
    thy
wenzelm@61261
   317
    |> map_defs (check_def context thy unchecked overloaded axm)
wenzelm@42375
   318
    |> add_axiom ctxt axm
wenzelm@35985
   319
  end;
wenzelm@16291
   320
wenzelm@16291
   321
end;
wenzelm@3767
   322
skalberg@14223
   323
end;