src/Pure/theory.ML
author paulson
Thu Feb 29 18:53:34 1996 +0100 (1996-02-29)
changeset 1526 6be6ea6f8b5d
child 1539 f21c8fab7c3c
permissions -rw-r--r--
New file of just the theory primitives
paulson@1526
     1
(*  Title:      Pure/theory.ML
paulson@1526
     2
    ID:         $Id$
paulson@1526
     3
    Author:     Lawrence C Paulson and Markus Wenzel
paulson@1526
     4
    Copyright   1996  University of Cambridge
paulson@1526
     5
paulson@1526
     6
Theories
paulson@1526
     7
*)
paulson@1526
     8
paulson@1526
     9
signature THEORY =
paulson@1526
    10
  sig
paulson@1526
    11
  type theory
paulson@1526
    12
  exception THEORY of string * theory list
paulson@1526
    13
  val rep_theory        : theory ->
paulson@1526
    14
    {sign: Sign.sg, new_axioms: term Symtab.table, parents: theory list}
paulson@1526
    15
  val sign_of           : theory -> Sign.sg
paulson@1526
    16
  val syn_of            : theory -> Syntax.syntax
paulson@1526
    17
  val stamps_of_thy     : theory -> string ref list
paulson@1526
    18
  val parents_of        : theory -> theory list
paulson@1526
    19
  val subthy            : theory * theory -> bool
paulson@1526
    20
  val eq_thy            : theory * theory -> bool
paulson@1526
    21
  val proto_pure_thy    : theory
paulson@1526
    22
  val pure_thy          : theory
paulson@1526
    23
  val cpure_thy         : theory
paulson@1526
    24
  (*theory primitives*)
paulson@1526
    25
  val add_classes     : (class * class list) list -> theory -> theory
paulson@1526
    26
  val add_classrel    : (class * class) list -> theory -> theory
paulson@1526
    27
  val add_defsort     : sort -> theory -> theory
paulson@1526
    28
  val add_types       : (string * int * mixfix) list -> theory -> theory
paulson@1526
    29
  val add_tyabbrs     : (string * string list * string * mixfix) list
paulson@1526
    30
    -> theory -> theory
paulson@1526
    31
  val add_tyabbrs_i   : (string * string list * typ * mixfix) list
paulson@1526
    32
    -> theory -> theory
paulson@1526
    33
  val add_arities     : (string * sort list * sort) list -> theory -> theory
paulson@1526
    34
  val add_consts      : (string * string * mixfix) list -> theory -> theory
paulson@1526
    35
  val add_consts_i    : (string * typ * mixfix) list -> theory -> theory
paulson@1526
    36
  val add_syntax      : (string * string * mixfix) list -> theory -> theory
paulson@1526
    37
  val add_syntax_i    : (string * typ * mixfix) list -> theory -> theory
paulson@1526
    38
  val add_trfuns      :
paulson@1526
    39
    (string * (Syntax.ast list -> Syntax.ast)) list *
paulson@1526
    40
    (string * (term list -> term)) list *
paulson@1526
    41
    (string * (term list -> term)) list *
paulson@1526
    42
    (string * (Syntax.ast list -> Syntax.ast)) list -> theory -> theory
paulson@1526
    43
  val add_trrules     : (string * string)Syntax.trrule list -> theory -> theory
paulson@1526
    44
  val add_trrules_i   : Syntax.ast Syntax.trrule list -> theory -> theory
paulson@1526
    45
  val cert_axm          : Sign.sg -> string * term -> string * term
paulson@1526
    46
  val read_axm          : Sign.sg -> string * string -> string * term
paulson@1526
    47
  val inferT_axm        : Sign.sg -> string * term -> string * term
paulson@1526
    48
  val add_axioms      : (string * string) list -> theory -> theory
paulson@1526
    49
  val add_axioms_i    : (string * term) list -> theory -> theory
paulson@1526
    50
  val add_thyname     : string -> theory -> theory
paulson@1526
    51
paulson@1526
    52
  val merge_theories    : theory * theory -> theory
paulson@1526
    53
  val merge_thy_list    : bool -> theory list -> theory
paulson@1526
    54
end;
paulson@1526
    55
paulson@1526
    56
paulson@1526
    57
structure Theory : THEORY =
paulson@1526
    58
struct
paulson@1526
    59
(*** Theories ***)
paulson@1526
    60
paulson@1526
    61
datatype theory =
paulson@1526
    62
  Theory of {
paulson@1526
    63
    sign: Sign.sg,
paulson@1526
    64
    new_axioms: term Symtab.table,
paulson@1526
    65
    parents: theory list};
paulson@1526
    66
paulson@1526
    67
fun rep_theory (Theory args) = args;
paulson@1526
    68
paulson@1526
    69
(*errors involving theories*)
paulson@1526
    70
exception THEORY of string * theory list;
paulson@1526
    71
paulson@1526
    72
paulson@1526
    73
val sign_of = #sign o rep_theory;
paulson@1526
    74
val syn_of = #syn o Sign.rep_sg o sign_of;
paulson@1526
    75
paulson@1526
    76
(*stamps associated with a theory*)
paulson@1526
    77
val stamps_of_thy = #stamps o Sign.rep_sg o sign_of;
paulson@1526
    78
paulson@1526
    79
(*return the immediate ancestors*)
paulson@1526
    80
val parents_of = #parents o rep_theory;
paulson@1526
    81
paulson@1526
    82
paulson@1526
    83
(*compare theories*)
paulson@1526
    84
val subthy = Sign.subsig o pairself sign_of;
paulson@1526
    85
val eq_thy = Sign.eq_sg o pairself sign_of;
paulson@1526
    86
paulson@1526
    87
paulson@1526
    88
(* the Pure theories *)
paulson@1526
    89
paulson@1526
    90
val proto_pure_thy =
paulson@1526
    91
  Theory {sign = Sign.proto_pure, new_axioms = Symtab.null, parents = []};
paulson@1526
    92
paulson@1526
    93
val pure_thy =
paulson@1526
    94
  Theory {sign = Sign.pure, new_axioms = Symtab.null, parents = []};
paulson@1526
    95
paulson@1526
    96
val cpure_thy =
paulson@1526
    97
  Theory {sign = Sign.cpure, new_axioms = Symtab.null, parents = []};
paulson@1526
    98
paulson@1526
    99
paulson@1526
   100
paulson@1526
   101
(** extend theory **)
paulson@1526
   102
paulson@1526
   103
fun err_dup_axms names =
paulson@1526
   104
  error ("Duplicate axiom name(s) " ^ commas_quote names);
paulson@1526
   105
paulson@1526
   106
fun ext_thy (thy as Theory {sign, new_axioms, parents}) sign1 new_axms =
paulson@1526
   107
  let
paulson@1526
   108
    val draft = Sign.is_draft sign;
paulson@1526
   109
    val new_axioms1 =
paulson@1526
   110
      Symtab.extend_new (if draft then new_axioms else Symtab.null, new_axms)
paulson@1526
   111
        handle Symtab.DUPS names => err_dup_axms names;
paulson@1526
   112
    val parents1 = if draft then parents else [thy];
paulson@1526
   113
  in
paulson@1526
   114
    Theory {sign = sign1, new_axioms = new_axioms1, parents = parents1}
paulson@1526
   115
  end;
paulson@1526
   116
paulson@1526
   117
paulson@1526
   118
(* extend signature of a theory *)
paulson@1526
   119
paulson@1526
   120
fun ext_sg extfun decls (thy as Theory {sign, ...}) =
paulson@1526
   121
  ext_thy thy (extfun decls sign) [];
paulson@1526
   122
paulson@1526
   123
val add_classes   = ext_sg Sign.add_classes;
paulson@1526
   124
val add_classrel  = ext_sg Sign.add_classrel;
paulson@1526
   125
val add_defsort   = ext_sg Sign.add_defsort;
paulson@1526
   126
val add_types     = ext_sg Sign.add_types;
paulson@1526
   127
val add_tyabbrs   = ext_sg Sign.add_tyabbrs;
paulson@1526
   128
val add_tyabbrs_i = ext_sg Sign.add_tyabbrs_i;
paulson@1526
   129
val add_arities   = ext_sg Sign.add_arities;
paulson@1526
   130
val add_consts    = ext_sg Sign.add_consts;
paulson@1526
   131
val add_consts_i  = ext_sg Sign.add_consts_i;
paulson@1526
   132
val add_syntax    = ext_sg Sign.add_syntax;
paulson@1526
   133
val add_syntax_i  = ext_sg Sign.add_syntax_i;
paulson@1526
   134
val add_trfuns    = ext_sg Sign.add_trfuns;
paulson@1526
   135
val add_trrules   = ext_sg Sign.add_trrules;
paulson@1526
   136
val add_trrules_i = ext_sg Sign.add_trrules_i;
paulson@1526
   137
val add_thyname   = ext_sg Sign.add_name;
paulson@1526
   138
paulson@1526
   139
paulson@1526
   140
(* prepare axioms *)
paulson@1526
   141
paulson@1526
   142
fun err_in_axm name =
paulson@1526
   143
  error ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   144
paulson@1526
   145
fun no_vars tm =
paulson@1526
   146
  if null (term_vars tm) andalso null (term_tvars tm) then tm
paulson@1526
   147
  else error "Illegal schematic variable(s) in term";
paulson@1526
   148
paulson@1526
   149
fun cert_axm sg (name, raw_tm) =
paulson@1526
   150
  let
paulson@1526
   151
    val (t, T, _) = Sign.certify_term sg raw_tm
paulson@1526
   152
      handle TYPE arg => error (Sign.exn_type_msg sg arg)
paulson@1526
   153
	   | TERM (msg, _) => error msg;
paulson@1526
   154
  in
paulson@1526
   155
    assert (T = propT) "Term not of type prop";
paulson@1526
   156
    (name, no_vars t)
paulson@1526
   157
  end
paulson@1526
   158
  handle ERROR => err_in_axm name;
paulson@1526
   159
paulson@1526
   160
(*Some duplication of code with read_def_cterm*)
paulson@1526
   161
fun read_axm sg (name, str) = 
paulson@1526
   162
  let val ts = Syntax.read (#syn (Sign.rep_sg sg)) propT str;
paulson@1526
   163
      val (_, t, _) =
paulson@1526
   164
          Sign.infer_types sg (K None) (K None) [] true (ts,propT);
paulson@1526
   165
  in cert_axm sg (name,t) end;
paulson@1526
   166
paulson@1526
   167
fun inferT_axm sg (name, pre_tm) =
paulson@1526
   168
  let val t = #2(Sign.infer_types sg (K None) (K None) [] true
paulson@1526
   169
                                     ([pre_tm], propT))
paulson@1526
   170
  in  (name, no_vars t) end
paulson@1526
   171
  handle ERROR => err_in_axm name;
paulson@1526
   172
paulson@1526
   173
paulson@1526
   174
(* extend axioms of a theory *)
paulson@1526
   175
paulson@1526
   176
fun ext_axms prep_axm axms (thy as Theory {sign, ...}) =
paulson@1526
   177
  let
paulson@1526
   178
    val sign1 = Sign.make_draft sign;
paulson@1526
   179
    val axioms = map (apsnd (Term.compress_term o Logic.varify) o 
paulson@1526
   180
		      prep_axm sign) 
paulson@1526
   181
	         axms;
paulson@1526
   182
  in
paulson@1526
   183
    ext_thy thy sign1 axioms
paulson@1526
   184
  end;
paulson@1526
   185
paulson@1526
   186
val add_axioms = ext_axms read_axm;
paulson@1526
   187
val add_axioms_i = ext_axms cert_axm;
paulson@1526
   188
paulson@1526
   189
paulson@1526
   190
paulson@1526
   191
(** merge theories **)
paulson@1526
   192
paulson@1526
   193
fun merge_thy_list mk_draft thys =
paulson@1526
   194
  let
paulson@1526
   195
    fun is_union thy = forall (fn t => subthy (t, thy)) thys;
paulson@1526
   196
    val is_draft = Sign.is_draft o sign_of;
paulson@1526
   197
paulson@1526
   198
    fun add_sign (sg, Theory {sign, ...}) =
paulson@1526
   199
      Sign.merge (sg, sign) handle TERM (msg, _) => error msg;
paulson@1526
   200
  in
paulson@1526
   201
    (case (find_first is_union thys, exists is_draft thys) of
paulson@1526
   202
      (Some thy, _) => thy
paulson@1526
   203
    | (None, true) => raise THEORY ("Illegal merge of draft theories", thys)
paulson@1526
   204
    | (None, false) => Theory {
paulson@1526
   205
        sign =
paulson@1526
   206
          (if mk_draft then Sign.make_draft else I)
paulson@1526
   207
          (foldl add_sign (Sign.proto_pure, thys)),
paulson@1526
   208
        new_axioms = Symtab.null,
paulson@1526
   209
        parents = thys})
paulson@1526
   210
  end;
paulson@1526
   211
paulson@1526
   212
fun merge_theories (thy1, thy2) = merge_thy_list false [thy1, thy2];
paulson@1526
   213
paulson@1526
   214
paulson@1526
   215
end;
paulson@1526
   216
paulson@1526
   217
open Theory;