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