src/Pure/theory.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 2979 db6941221197
child 3767 e2bb53d8dd26
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
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@1539
    14
    {sign: Sign.sg, oraopt: (Sign.sg * exn -> term) option,
paulson@1539
    15
     new_axioms: term Symtab.table, parents: theory list}
paulson@1526
    16
  val sign_of           : theory -> Sign.sg
paulson@1526
    17
  val syn_of            : theory -> Syntax.syntax
paulson@1526
    18
  val stamps_of_thy     : theory -> string ref list
paulson@1526
    19
  val parents_of        : theory -> theory list
paulson@1526
    20
  val subthy            : theory * theory -> bool
paulson@1526
    21
  val eq_thy            : theory * theory -> bool
paulson@1526
    22
  val proto_pure_thy    : theory
paulson@1526
    23
  val pure_thy          : theory
paulson@1526
    24
  val cpure_thy         : theory
paulson@1526
    25
  (*theory primitives*)
paulson@1539
    26
  val add_classes	: (class * class list) list -> theory -> theory
paulson@1539
    27
  val add_classrel	: (class * class) list -> theory -> theory
paulson@1539
    28
  val add_defsort	: sort -> theory -> theory
paulson@1539
    29
  val add_types		: (string * int * mixfix) list -> theory -> theory
paulson@1539
    30
  val add_tyabbrs	: (string * string list * string * mixfix) list
paulson@1526
    31
    -> theory -> theory
paulson@1539
    32
  val add_tyabbrs_i	: (string * string list * typ * mixfix) list
paulson@1526
    33
    -> theory -> theory
paulson@1539
    34
  val add_arities	: (string * sort list * sort) list -> theory -> theory
paulson@1539
    35
  val add_consts	: (string * string * mixfix) list -> theory -> theory
paulson@1539
    36
  val add_consts_i	: (string * typ * mixfix) list -> theory -> theory
paulson@1539
    37
  val add_syntax	: (string * string * mixfix) list -> theory -> theory
paulson@1539
    38
  val add_syntax_i	: (string * typ * mixfix) list -> theory -> theory
wenzelm@2359
    39
  val add_modesyntax	: string * bool -> (string * string * mixfix) list -> theory -> theory
wenzelm@2359
    40
  val add_modesyntax_i	: string * bool -> (string * typ * mixfix) list -> theory -> theory
paulson@1539
    41
  val add_trfuns	:
paulson@1526
    42
    (string * (Syntax.ast list -> Syntax.ast)) list *
paulson@1526
    43
    (string * (term list -> term)) list *
paulson@1526
    44
    (string * (term list -> term)) list *
paulson@1526
    45
    (string * (Syntax.ast list -> Syntax.ast)) list -> theory -> theory
wenzelm@2385
    46
  val add_trfunsT	:
wenzelm@2385
    47
    (string * (typ -> term list -> term)) list -> theory -> theory
wenzelm@2693
    48
  val add_tokentrfuns:
wenzelm@2693
    49
    (string * string * (string -> string * int)) list -> theory -> theory
paulson@1526
    50
  val add_trrules     : (string * string)Syntax.trrule list -> theory -> theory
paulson@1539
    51
  val add_trrules_i	: Syntax.ast Syntax.trrule list -> theory -> theory
paulson@1526
    52
  val cert_axm          : Sign.sg -> string * term -> string * term
paulson@1526
    53
  val read_axm          : Sign.sg -> string * string -> string * term
paulson@1526
    54
  val inferT_axm        : Sign.sg -> string * term -> string * term
paulson@1539
    55
  val add_axioms	: (string * string) list -> theory -> theory
paulson@1539
    56
  val add_axioms_i	: (string * term) list -> theory -> theory
paulson@1539
    57
  val add_thyname	: string -> theory -> theory
paulson@1539
    58
paulson@1539
    59
  val set_oracle	: (Sign.sg * exn -> term) -> theory -> theory
paulson@1526
    60
paulson@1526
    61
  val merge_theories    : theory * theory -> theory
paulson@1526
    62
  val merge_thy_list    : bool -> theory list -> theory
paulson@1526
    63
end;
paulson@1526
    64
paulson@1526
    65
paulson@1526
    66
structure Theory : THEORY =
paulson@1526
    67
struct
wenzelm@2206
    68
wenzelm@2206
    69
(** datatype theory **)
paulson@1526
    70
paulson@1526
    71
datatype theory =
paulson@1526
    72
  Theory of {
paulson@1526
    73
    sign: Sign.sg,
paulson@1539
    74
    oraopt: (Sign.sg * exn -> term) option,
paulson@1526
    75
    new_axioms: term Symtab.table,
paulson@1526
    76
    parents: theory list};
paulson@1526
    77
paulson@1526
    78
fun rep_theory (Theory args) = args;
paulson@1526
    79
paulson@1526
    80
(*errors involving theories*)
paulson@1526
    81
exception THEORY of string * theory list;
paulson@1526
    82
paulson@1526
    83
paulson@1526
    84
val sign_of = #sign o rep_theory;
paulson@1526
    85
val syn_of = #syn o Sign.rep_sg o sign_of;
paulson@1526
    86
paulson@1526
    87
(*stamps associated with a theory*)
paulson@1526
    88
val stamps_of_thy = #stamps o Sign.rep_sg o sign_of;
paulson@1526
    89
paulson@1526
    90
(*return the immediate ancestors*)
paulson@1526
    91
val parents_of = #parents o rep_theory;
paulson@1526
    92
paulson@1526
    93
paulson@1526
    94
(*compare theories*)
paulson@1526
    95
val subthy = Sign.subsig o pairself sign_of;
paulson@1526
    96
val eq_thy = Sign.eq_sg o pairself sign_of;
paulson@1526
    97
paulson@1526
    98
paulson@1526
    99
(* the Pure theories *)
paulson@1526
   100
paulson@1526
   101
val proto_pure_thy =
paulson@1539
   102
  Theory {sign = Sign.proto_pure, oraopt = None, 
paulson@1539
   103
	  new_axioms = Symtab.null, parents = []};
paulson@1526
   104
paulson@1526
   105
val pure_thy =
paulson@1539
   106
  Theory {sign = Sign.pure, oraopt = None, 
paulson@1539
   107
	  new_axioms = Symtab.null, parents = []};
paulson@1526
   108
paulson@1526
   109
val cpure_thy =
paulson@1539
   110
  Theory {sign = Sign.cpure, oraopt = None, 
paulson@1539
   111
	  new_axioms = Symtab.null, parents = []};
paulson@1526
   112
paulson@1526
   113
paulson@1526
   114
paulson@1526
   115
(** extend theory **)
paulson@1526
   116
paulson@1526
   117
fun err_dup_axms names =
paulson@1526
   118
  error ("Duplicate axiom name(s) " ^ commas_quote names);
paulson@1526
   119
paulson@1539
   120
fun ext_thy (thy as Theory {sign, oraopt, new_axioms, parents}) 
paulson@1539
   121
            sign1 new_axms =
paulson@1526
   122
  let
paulson@1526
   123
    val draft = Sign.is_draft sign;
paulson@1526
   124
    val new_axioms1 =
paulson@1526
   125
      Symtab.extend_new (if draft then new_axioms else Symtab.null, new_axms)
paulson@1526
   126
        handle Symtab.DUPS names => err_dup_axms names;
paulson@1526
   127
    val parents1 = if draft then parents else [thy];
paulson@1526
   128
  in
paulson@1539
   129
    Theory {sign = sign1, oraopt = oraopt, 
paulson@1539
   130
	    new_axioms = new_axioms1, parents = parents1}
paulson@1526
   131
  end;
paulson@1526
   132
paulson@1526
   133
paulson@1526
   134
(* extend signature of a theory *)
paulson@1526
   135
paulson@1526
   136
fun ext_sg extfun decls (thy as Theory {sign, ...}) =
paulson@1526
   137
  ext_thy thy (extfun decls sign) [];
paulson@1526
   138
wenzelm@2206
   139
val add_classes      = ext_sg Sign.add_classes;
wenzelm@2206
   140
val add_classrel     = ext_sg Sign.add_classrel;
wenzelm@2206
   141
val add_defsort      = ext_sg Sign.add_defsort;
wenzelm@2206
   142
val add_types        = ext_sg Sign.add_types;
wenzelm@2206
   143
val add_tyabbrs      = ext_sg Sign.add_tyabbrs;
wenzelm@2206
   144
val add_tyabbrs_i    = ext_sg Sign.add_tyabbrs_i;
wenzelm@2206
   145
val add_arities      = ext_sg Sign.add_arities;
wenzelm@2206
   146
val add_consts       = ext_sg Sign.add_consts;
wenzelm@2206
   147
val add_consts_i     = ext_sg Sign.add_consts_i;
wenzelm@2206
   148
val add_syntax       = ext_sg Sign.add_syntax;
wenzelm@2206
   149
val add_syntax_i     = ext_sg Sign.add_syntax_i;
wenzelm@2206
   150
val add_modesyntax   = curry (ext_sg Sign.add_modesyntax);
wenzelm@2206
   151
val add_modesyntax_i = curry (ext_sg Sign.add_modesyntax_i);
wenzelm@2206
   152
val add_trfuns       = ext_sg Sign.add_trfuns;
wenzelm@2385
   153
val add_trfunsT      = ext_sg Sign.add_trfunsT;
wenzelm@2693
   154
val add_tokentrfuns  = ext_sg Sign.add_tokentrfuns;
wenzelm@2206
   155
val add_trrules      = ext_sg Sign.add_trrules;
wenzelm@2206
   156
val add_trrules_i    = ext_sg Sign.add_trrules_i;
wenzelm@2206
   157
val add_thyname      = ext_sg Sign.add_name;
paulson@1526
   158
paulson@1526
   159
paulson@1526
   160
(* prepare axioms *)
paulson@1526
   161
paulson@1526
   162
fun err_in_axm name =
paulson@1526
   163
  error ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   164
paulson@1526
   165
fun no_vars tm =
paulson@1526
   166
  if null (term_vars tm) andalso null (term_tvars tm) then tm
paulson@1526
   167
  else error "Illegal schematic variable(s) in term";
paulson@1526
   168
paulson@1526
   169
fun cert_axm sg (name, raw_tm) =
paulson@1526
   170
  let
paulson@1526
   171
    val (t, T, _) = Sign.certify_term sg raw_tm
wenzelm@2979
   172
      handle TYPE (msg, _, _) => error msg
paulson@1526
   173
	   | TERM (msg, _) => error msg;
paulson@1526
   174
  in
paulson@1526
   175
    assert (T = propT) "Term not of type prop";
paulson@1526
   176
    (name, no_vars t)
paulson@1526
   177
  end
paulson@1526
   178
  handle ERROR => err_in_axm name;
paulson@1526
   179
paulson@1526
   180
(*Some duplication of code with read_def_cterm*)
paulson@1526
   181
fun read_axm sg (name, str) = 
paulson@1526
   182
  let val ts = Syntax.read (#syn (Sign.rep_sg sg)) propT str;
paulson@1526
   183
      val (_, t, _) =
paulson@1526
   184
          Sign.infer_types sg (K None) (K None) [] true (ts,propT);
paulson@1960
   185
  in cert_axm sg (name,t) end
paulson@1960
   186
  handle ERROR => err_in_axm name;
paulson@1526
   187
paulson@1526
   188
fun inferT_axm sg (name, pre_tm) =
paulson@1526
   189
  let val t = #2(Sign.infer_types sg (K None) (K None) [] true
paulson@1526
   190
                                     ([pre_tm], propT))
paulson@1526
   191
  in  (name, no_vars t) end
paulson@1526
   192
  handle ERROR => err_in_axm name;
paulson@1526
   193
paulson@1526
   194
paulson@1526
   195
(* extend axioms of a theory *)
paulson@1526
   196
paulson@1526
   197
fun ext_axms prep_axm axms (thy as Theory {sign, ...}) =
paulson@1526
   198
  let
paulson@1526
   199
    val sign1 = Sign.make_draft sign;
paulson@1526
   200
    val axioms = map (apsnd (Term.compress_term o Logic.varify) o 
paulson@1526
   201
		      prep_axm sign) 
paulson@1526
   202
	         axms;
paulson@1526
   203
  in
paulson@1526
   204
    ext_thy thy sign1 axioms
paulson@1526
   205
  end;
paulson@1526
   206
paulson@1526
   207
val add_axioms = ext_axms read_axm;
paulson@1526
   208
val add_axioms_i = ext_axms cert_axm;
paulson@1526
   209
paulson@1526
   210
paulson@1539
   211
(** Set oracle of theory **)
paulson@1539
   212
paulson@1539
   213
fun set_oracle oracle 
paulson@1539
   214
               (thy as Theory {sign, oraopt = None, new_axioms, parents}) =
paulson@1539
   215
      if Sign.is_draft sign then
paulson@1539
   216
	Theory {sign = sign, 
paulson@1539
   217
		oraopt = Some oracle, 
paulson@1539
   218
		new_axioms = new_axioms, 
paulson@1539
   219
		parents = parents}
paulson@1539
   220
      else raise THEORY ("Can only set oracle of a draft", [thy])
paulson@1539
   221
  | set_oracle _ thy = raise THEORY ("Oracle already set", [thy]);
paulson@1539
   222
paulson@1526
   223
paulson@1526
   224
(** merge theories **)
paulson@1526
   225
paulson@1526
   226
fun merge_thy_list mk_draft thys =
paulson@1526
   227
  let
paulson@1526
   228
    fun is_union thy = forall (fn t => subthy (t, thy)) thys;
paulson@1526
   229
    val is_draft = Sign.is_draft o sign_of;
paulson@1526
   230
paulson@1526
   231
    fun add_sign (sg, Theory {sign, ...}) =
paulson@1526
   232
      Sign.merge (sg, sign) handle TERM (msg, _) => error msg;
paulson@1526
   233
  in
paulson@1539
   234
    case (find_first is_union thys, exists is_draft thys) of
paulson@1526
   235
      (Some thy, _) => thy
paulson@1526
   236
    | (None, true) => raise THEORY ("Illegal merge of draft theories", thys)
paulson@1526
   237
    | (None, false) => Theory {
paulson@1526
   238
        sign =
paulson@1526
   239
          (if mk_draft then Sign.make_draft else I)
paulson@1526
   240
          (foldl add_sign (Sign.proto_pure, thys)),
paulson@1539
   241
	oraopt = None,
paulson@1526
   242
        new_axioms = Symtab.null,
paulson@1539
   243
        parents = thys}
paulson@1526
   244
  end;
paulson@1526
   245
paulson@1526
   246
fun merge_theories (thy1, thy2) = merge_thy_list false [thy1, thy2];
paulson@1526
   247
paulson@1526
   248
paulson@1526
   249
end;
paulson@1526
   250
paulson@1526
   251
open Theory;