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