src/Pure/theory.ML
author wenzelm
Wed Oct 01 17:43:42 1997 +0200 (1997-10-01)
changeset 3767 e2bb53d8dd26
parent 2979 db6941221197
child 3787 67571f49ebe3
permissions -rw-r--r--
moved theory stuff (add_defs etc.) here from drule.ML;
only BasicTheory opened;
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
wenzelm@3767
     6
Theories.
paulson@1526
     7
*)
paulson@1526
     8
wenzelm@3767
     9
(*this part made pervasive*)
wenzelm@3767
    10
signature BASIC_THEORY =
wenzelm@3767
    11
sig
paulson@1526
    12
  type theory
paulson@1526
    13
  exception THEORY of string * theory list
paulson@1526
    14
  val rep_theory        : theory ->
paulson@1539
    15
    {sign: Sign.sg, oraopt: (Sign.sg * exn -> term) option,
paulson@1539
    16
     new_axioms: term Symtab.table, parents: theory list}
paulson@1526
    17
  val sign_of           : theory -> Sign.sg
paulson@1526
    18
  val syn_of            : theory -> Syntax.syntax
paulson@1526
    19
  val stamps_of_thy     : theory -> string ref list
paulson@1526
    20
  val parents_of        : theory -> theory list
paulson@1526
    21
  val subthy            : theory * theory -> bool
paulson@1526
    22
  val eq_thy            : theory * theory -> bool
paulson@1526
    23
  val proto_pure_thy    : theory
paulson@1526
    24
  val pure_thy          : theory
paulson@1526
    25
  val cpure_thy         : theory
wenzelm@3767
    26
  val cert_axm          : Sign.sg -> string * term -> string * term
wenzelm@3767
    27
  val read_axm          : Sign.sg -> string * string -> string * term
wenzelm@3767
    28
  val inferT_axm        : Sign.sg -> string * term -> string * term
wenzelm@3767
    29
  val merge_theories    : theory * theory -> theory
wenzelm@3767
    30
end
wenzelm@3767
    31
wenzelm@3767
    32
signature THEORY =
wenzelm@3767
    33
sig
wenzelm@3767
    34
  include BASIC_THEORY
wenzelm@3767
    35
  (*theory extendsion primitives*)
paulson@1539
    36
  val add_classes	: (class * class list) list -> theory -> theory
paulson@1539
    37
  val add_classrel	: (class * class) list -> theory -> theory
paulson@1539
    38
  val add_defsort	: sort -> theory -> theory
paulson@1539
    39
  val add_types		: (string * int * mixfix) list -> theory -> theory
paulson@1539
    40
  val add_tyabbrs	: (string * string list * string * mixfix) list
paulson@1526
    41
    -> theory -> theory
paulson@1539
    42
  val add_tyabbrs_i	: (string * string list * typ * mixfix) list
paulson@1526
    43
    -> theory -> theory
paulson@1539
    44
  val add_arities	: (string * sort list * sort) list -> theory -> theory
paulson@1539
    45
  val add_consts	: (string * string * mixfix) list -> theory -> theory
paulson@1539
    46
  val add_consts_i	: (string * typ * mixfix) list -> theory -> theory
paulson@1539
    47
  val add_syntax	: (string * string * mixfix) list -> theory -> theory
paulson@1539
    48
  val add_syntax_i	: (string * typ * mixfix) list -> theory -> theory
wenzelm@2359
    49
  val add_modesyntax	: string * bool -> (string * string * mixfix) list -> theory -> theory
wenzelm@2359
    50
  val add_modesyntax_i	: string * bool -> (string * typ * mixfix) list -> theory -> theory
paulson@1539
    51
  val add_trfuns	:
paulson@1526
    52
    (string * (Syntax.ast list -> Syntax.ast)) list *
paulson@1526
    53
    (string * (term list -> term)) list *
paulson@1526
    54
    (string * (term list -> term)) list *
paulson@1526
    55
    (string * (Syntax.ast list -> Syntax.ast)) list -> theory -> theory
wenzelm@2385
    56
  val add_trfunsT	:
wenzelm@2385
    57
    (string * (typ -> term list -> term)) list -> theory -> theory
wenzelm@2693
    58
  val add_tokentrfuns:
wenzelm@2693
    59
    (string * string * (string -> string * int)) list -> theory -> theory
paulson@1526
    60
  val add_trrules     : (string * string)Syntax.trrule list -> theory -> theory
paulson@1539
    61
  val add_trrules_i	: Syntax.ast Syntax.trrule list -> theory -> theory
paulson@1539
    62
  val add_axioms	: (string * string) list -> theory -> theory
paulson@1539
    63
  val add_axioms_i	: (string * term) list -> theory -> theory
wenzelm@3767
    64
  val add_defs		: (string * string) list -> theory -> theory
wenzelm@3767
    65
  val add_defs_i	: (string * term) list -> theory -> theory
wenzelm@3767
    66
  val add_name		: string -> theory -> theory
paulson@1539
    67
paulson@1539
    68
  val set_oracle	: (Sign.sg * exn -> term) -> theory -> theory
paulson@1526
    69
paulson@1526
    70
  val merge_thy_list    : bool -> theory list -> theory
paulson@1526
    71
end;
paulson@1526
    72
paulson@1526
    73
wenzelm@3767
    74
structure Theory: THEORY =
paulson@1526
    75
struct
wenzelm@2206
    76
wenzelm@2206
    77
(** datatype theory **)
paulson@1526
    78
paulson@1526
    79
datatype theory =
paulson@1526
    80
  Theory of {
paulson@1526
    81
    sign: Sign.sg,
paulson@1539
    82
    oraopt: (Sign.sg * exn -> term) option,
paulson@1526
    83
    new_axioms: term Symtab.table,
paulson@1526
    84
    parents: theory list};
paulson@1526
    85
paulson@1526
    86
fun rep_theory (Theory args) = args;
paulson@1526
    87
paulson@1526
    88
(*errors involving theories*)
paulson@1526
    89
exception THEORY of string * theory list;
paulson@1526
    90
paulson@1526
    91
paulson@1526
    92
val sign_of = #sign o rep_theory;
paulson@1526
    93
val syn_of = #syn o Sign.rep_sg o sign_of;
paulson@1526
    94
paulson@1526
    95
(*stamps associated with a theory*)
paulson@1526
    96
val stamps_of_thy = #stamps o Sign.rep_sg o sign_of;
paulson@1526
    97
paulson@1526
    98
(*return the immediate ancestors*)
paulson@1526
    99
val parents_of = #parents o rep_theory;
paulson@1526
   100
paulson@1526
   101
paulson@1526
   102
(*compare theories*)
paulson@1526
   103
val subthy = Sign.subsig o pairself sign_of;
paulson@1526
   104
val eq_thy = Sign.eq_sg o pairself sign_of;
paulson@1526
   105
paulson@1526
   106
paulson@1526
   107
(* the Pure theories *)
paulson@1526
   108
paulson@1526
   109
val proto_pure_thy =
paulson@1539
   110
  Theory {sign = Sign.proto_pure, oraopt = None, 
paulson@1539
   111
	  new_axioms = Symtab.null, parents = []};
paulson@1526
   112
paulson@1526
   113
val pure_thy =
paulson@1539
   114
  Theory {sign = Sign.pure, oraopt = None, 
paulson@1539
   115
	  new_axioms = Symtab.null, parents = []};
paulson@1526
   116
paulson@1526
   117
val cpure_thy =
paulson@1539
   118
  Theory {sign = Sign.cpure, oraopt = None, 
paulson@1539
   119
	  new_axioms = Symtab.null, parents = []};
paulson@1526
   120
paulson@1526
   121
paulson@1526
   122
paulson@1526
   123
(** extend theory **)
paulson@1526
   124
paulson@1526
   125
fun err_dup_axms names =
paulson@1526
   126
  error ("Duplicate axiom name(s) " ^ commas_quote names);
paulson@1526
   127
paulson@1539
   128
fun ext_thy (thy as Theory {sign, oraopt, new_axioms, parents}) 
paulson@1539
   129
            sign1 new_axms =
paulson@1526
   130
  let
paulson@1526
   131
    val draft = Sign.is_draft sign;
paulson@1526
   132
    val new_axioms1 =
paulson@1526
   133
      Symtab.extend_new (if draft then new_axioms else Symtab.null, new_axms)
paulson@1526
   134
        handle Symtab.DUPS names => err_dup_axms names;
paulson@1526
   135
    val parents1 = if draft then parents else [thy];
paulson@1526
   136
  in
paulson@1539
   137
    Theory {sign = sign1, oraopt = oraopt, 
paulson@1539
   138
	    new_axioms = new_axioms1, parents = parents1}
paulson@1526
   139
  end;
paulson@1526
   140
paulson@1526
   141
paulson@1526
   142
(* extend signature of a theory *)
paulson@1526
   143
paulson@1526
   144
fun ext_sg extfun decls (thy as Theory {sign, ...}) =
paulson@1526
   145
  ext_thy thy (extfun decls sign) [];
paulson@1526
   146
wenzelm@2206
   147
val add_classes      = ext_sg Sign.add_classes;
wenzelm@2206
   148
val add_classrel     = ext_sg Sign.add_classrel;
wenzelm@2206
   149
val add_defsort      = ext_sg Sign.add_defsort;
wenzelm@2206
   150
val add_types        = ext_sg Sign.add_types;
wenzelm@2206
   151
val add_tyabbrs      = ext_sg Sign.add_tyabbrs;
wenzelm@2206
   152
val add_tyabbrs_i    = ext_sg Sign.add_tyabbrs_i;
wenzelm@2206
   153
val add_arities      = ext_sg Sign.add_arities;
wenzelm@2206
   154
val add_consts       = ext_sg Sign.add_consts;
wenzelm@2206
   155
val add_consts_i     = ext_sg Sign.add_consts_i;
wenzelm@2206
   156
val add_syntax       = ext_sg Sign.add_syntax;
wenzelm@2206
   157
val add_syntax_i     = ext_sg Sign.add_syntax_i;
wenzelm@2206
   158
val add_modesyntax   = curry (ext_sg Sign.add_modesyntax);
wenzelm@2206
   159
val add_modesyntax_i = curry (ext_sg Sign.add_modesyntax_i);
wenzelm@2206
   160
val add_trfuns       = ext_sg Sign.add_trfuns;
wenzelm@2385
   161
val add_trfunsT      = ext_sg Sign.add_trfunsT;
wenzelm@2693
   162
val add_tokentrfuns  = ext_sg Sign.add_tokentrfuns;
wenzelm@2206
   163
val add_trrules      = ext_sg Sign.add_trrules;
wenzelm@2206
   164
val add_trrules_i    = ext_sg Sign.add_trrules_i;
wenzelm@3767
   165
val add_name         = ext_sg Sign.add_name;
paulson@1526
   166
paulson@1526
   167
paulson@1526
   168
(* prepare axioms *)
paulson@1526
   169
paulson@1526
   170
fun err_in_axm name =
paulson@1526
   171
  error ("The error(s) above occurred in axiom " ^ quote name);
paulson@1526
   172
paulson@1526
   173
fun no_vars tm =
paulson@1526
   174
  if null (term_vars tm) andalso null (term_tvars tm) then tm
paulson@1526
   175
  else error "Illegal schematic variable(s) in term";
paulson@1526
   176
paulson@1526
   177
fun cert_axm sg (name, raw_tm) =
paulson@1526
   178
  let
paulson@1526
   179
    val (t, T, _) = Sign.certify_term sg raw_tm
wenzelm@2979
   180
      handle TYPE (msg, _, _) => error msg
paulson@1526
   181
	   | TERM (msg, _) => error msg;
paulson@1526
   182
  in
paulson@1526
   183
    assert (T = propT) "Term not of type prop";
paulson@1526
   184
    (name, no_vars t)
paulson@1526
   185
  end
paulson@1526
   186
  handle ERROR => err_in_axm name;
paulson@1526
   187
paulson@1526
   188
(*Some duplication of code with read_def_cterm*)
paulson@1526
   189
fun read_axm sg (name, str) = 
paulson@1526
   190
  let val ts = Syntax.read (#syn (Sign.rep_sg sg)) propT str;
paulson@1526
   191
      val (_, t, _) =
paulson@1526
   192
          Sign.infer_types sg (K None) (K None) [] true (ts,propT);
paulson@1960
   193
  in cert_axm sg (name,t) end
paulson@1960
   194
  handle ERROR => err_in_axm name;
paulson@1526
   195
paulson@1526
   196
fun inferT_axm sg (name, pre_tm) =
paulson@1526
   197
  let val t = #2(Sign.infer_types sg (K None) (K None) [] true
paulson@1526
   198
                                     ([pre_tm], propT))
paulson@1526
   199
  in  (name, no_vars t) end
paulson@1526
   200
  handle ERROR => err_in_axm name;
paulson@1526
   201
paulson@1526
   202
paulson@1526
   203
(* extend axioms of a theory *)
paulson@1526
   204
paulson@1526
   205
fun ext_axms prep_axm axms (thy as Theory {sign, ...}) =
paulson@1526
   206
  let
paulson@1526
   207
    val sign1 = Sign.make_draft sign;
paulson@1526
   208
    val axioms = map (apsnd (Term.compress_term o Logic.varify) o 
paulson@1526
   209
		      prep_axm sign) 
paulson@1526
   210
	         axms;
paulson@1526
   211
  in
paulson@1526
   212
    ext_thy thy sign1 axioms
paulson@1526
   213
  end;
paulson@1526
   214
paulson@1526
   215
val add_axioms = ext_axms read_axm;
paulson@1526
   216
val add_axioms_i = ext_axms cert_axm;
paulson@1526
   217
paulson@1526
   218
wenzelm@3767
   219
wenzelm@3767
   220
(** add constant definitions **)
wenzelm@3767
   221
wenzelm@3767
   222
(* all_axioms_of *)
wenzelm@3767
   223
wenzelm@3767
   224
(*results may contain duplicates!*)
wenzelm@3767
   225
wenzelm@3767
   226
fun ancestry_of thy =
wenzelm@3767
   227
  thy :: List.concat (map ancestry_of (parents_of thy));
wenzelm@3767
   228
wenzelm@3767
   229
val all_axioms_of =
wenzelm@3767
   230
  List.concat o map (Symtab.dest o #new_axioms o rep_theory) o ancestry_of;
wenzelm@3767
   231
wenzelm@3767
   232
wenzelm@3767
   233
(* clash_types, clash_consts *)
wenzelm@3767
   234
wenzelm@3767
   235
(*check if types have common instance (ignoring sorts)*)
wenzelm@3767
   236
wenzelm@3767
   237
fun clash_types ty1 ty2 =
wenzelm@3767
   238
  let
wenzelm@3767
   239
    val ty1' = Type.varifyT ty1;
wenzelm@3767
   240
    val ty2' = incr_tvar (maxidx_of_typ ty1' + 1) (Type.varifyT ty2);
wenzelm@3767
   241
  in
wenzelm@3767
   242
    Type.raw_unify (ty1', ty2')
wenzelm@3767
   243
  end;
wenzelm@3767
   244
wenzelm@3767
   245
fun clash_consts (c1, ty1) (c2, ty2) =
wenzelm@3767
   246
  c1 = c2 andalso clash_types ty1 ty2;
wenzelm@3767
   247
wenzelm@3767
   248
wenzelm@3767
   249
(* clash_defns *)
wenzelm@3767
   250
wenzelm@3767
   251
fun clash_defn c_ty (name, tm) =
wenzelm@3767
   252
  let val (c, ty') = dest_Const (head_of (fst (Logic.dest_equals tm))) in
wenzelm@3767
   253
    if clash_consts c_ty (c, ty') then Some (name, ty') else None
wenzelm@3767
   254
  end handle TERM _ => None;
wenzelm@3767
   255
wenzelm@3767
   256
fun clash_defns c_ty axms =
wenzelm@3767
   257
  distinct (mapfilter (clash_defn c_ty) axms);
wenzelm@3767
   258
wenzelm@3767
   259
wenzelm@3767
   260
(* dest_defn *)
wenzelm@3767
   261
wenzelm@3767
   262
fun dest_defn tm =
wenzelm@3767
   263
  let
wenzelm@3767
   264
    fun err msg = raise_term msg [tm];
wenzelm@3767
   265
wenzelm@3767
   266
    val (lhs, rhs) = Logic.dest_equals (Logic.strip_imp_concl tm)
wenzelm@3767
   267
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@3767
   268
    val (head, args) = strip_comb lhs;
wenzelm@3767
   269
    val (c, ty) = dest_Const head
wenzelm@3767
   270
      handle TERM _ => err "Head of lhs not a constant";
wenzelm@3767
   271
wenzelm@3767
   272
    fun occs_const (Const c_ty') = (c_ty' = (c, ty))
wenzelm@3767
   273
      | occs_const (Abs (_, _, t)) = occs_const t
wenzelm@3767
   274
      | occs_const (t $ u) = occs_const t orelse occs_const u
wenzelm@3767
   275
      | occs_const _ = false;
wenzelm@3767
   276
wenzelm@3767
   277
    val show_frees = commas_quote o map (fst o dest_Free);
wenzelm@3767
   278
    val show_tfrees = commas_quote o map fst;
wenzelm@3767
   279
wenzelm@3767
   280
    val lhs_dups = duplicates args;
wenzelm@3767
   281
    val rhs_extras = gen_rems (op =) (term_frees rhs, args);
wenzelm@3767
   282
    val rhs_extrasT = gen_rems (op =) (term_tfrees rhs, typ_tfrees ty);
wenzelm@3767
   283
  in
wenzelm@3767
   284
    if not (forall is_Free args) then
wenzelm@3767
   285
      err "Arguments (on lhs) must be variables"
wenzelm@3767
   286
    else if not (null lhs_dups) then
wenzelm@3767
   287
      err ("Duplicate variables on lhs: " ^ show_frees lhs_dups)
wenzelm@3767
   288
    else if not (null rhs_extras) then
wenzelm@3767
   289
      err ("Extra variables on rhs: " ^ show_frees rhs_extras)
wenzelm@3767
   290
    else if not (null rhs_extrasT) then
wenzelm@3767
   291
      err ("Extra type variables on rhs: " ^ show_tfrees rhs_extrasT)
wenzelm@3767
   292
    else if occs_const rhs then
wenzelm@3767
   293
      err ("Constant to be defined occurs on rhs")
wenzelm@3767
   294
    else (c, ty)
wenzelm@3767
   295
  end;
wenzelm@3767
   296
wenzelm@3767
   297
wenzelm@3767
   298
(* check_defn *)
wenzelm@3767
   299
wenzelm@3767
   300
fun err_in_defn name msg =
wenzelm@3767
   301
  (writeln msg; error ("The error(s) above occurred in definition " ^ quote name));
wenzelm@3767
   302
wenzelm@3767
   303
fun check_defn sign (axms, (name, tm)) =
wenzelm@3767
   304
  let
wenzelm@3767
   305
    fun show_const (c, ty) = quote (Pretty.string_of (Pretty.block
wenzelm@3767
   306
      [Pretty.str (c ^ " ::"), Pretty.brk 1, Sign.pretty_typ sign ty]));
wenzelm@3767
   307
wenzelm@3767
   308
    fun show_defn c (dfn, ty') = show_const (c, ty') ^ " in " ^ dfn;
wenzelm@3767
   309
    fun show_defns c = cat_lines o map (show_defn c);
wenzelm@3767
   310
wenzelm@3767
   311
    val (c, ty) = dest_defn tm
wenzelm@3767
   312
      handle TERM (msg, _) => err_in_defn name msg;
wenzelm@3767
   313
    val defns = clash_defns (c, ty) axms;
wenzelm@3767
   314
  in
wenzelm@3767
   315
    if not (null defns) then
wenzelm@3767
   316
      err_in_defn name ("Definition of " ^ show_const (c, ty) ^
wenzelm@3767
   317
        "\nclashes with " ^ show_defns c defns)
wenzelm@3767
   318
    else (name, tm) :: axms
wenzelm@3767
   319
  end;
wenzelm@3767
   320
wenzelm@3767
   321
wenzelm@3767
   322
(* add_defs *)
wenzelm@3767
   323
wenzelm@3767
   324
fun ext_defns prep_axm raw_axms thy =
wenzelm@3767
   325
  let
wenzelm@3767
   326
    val axms = map (prep_axm (sign_of thy)) raw_axms;
wenzelm@3767
   327
    val all_axms = all_axioms_of thy;
wenzelm@3767
   328
  in
wenzelm@3767
   329
    foldl (check_defn (sign_of thy)) (all_axms, axms);
wenzelm@3767
   330
    add_axioms_i axms thy
wenzelm@3767
   331
  end;
wenzelm@3767
   332
wenzelm@3767
   333
val add_defs_i = ext_defns cert_axm;
wenzelm@3767
   334
val add_defs = ext_defns read_axm;
wenzelm@3767
   335
wenzelm@3767
   336
wenzelm@3767
   337
paulson@1539
   338
(** Set oracle of theory **)
paulson@1539
   339
wenzelm@3767
   340
(* FIXME support more than one oracle (!?) *)
wenzelm@3767
   341
wenzelm@3767
   342
fun set_oracle oracle
paulson@1539
   343
               (thy as Theory {sign, oraopt = None, new_axioms, parents}) =
paulson@1539
   344
      if Sign.is_draft sign then
paulson@1539
   345
	Theory {sign = sign, 
paulson@1539
   346
		oraopt = Some oracle, 
paulson@1539
   347
		new_axioms = new_axioms, 
paulson@1539
   348
		parents = parents}
paulson@1539
   349
      else raise THEORY ("Can only set oracle of a draft", [thy])
paulson@1539
   350
  | set_oracle _ thy = raise THEORY ("Oracle already set", [thy]);
paulson@1539
   351
paulson@1526
   352
wenzelm@3767
   353
paulson@1526
   354
(** merge theories **)
paulson@1526
   355
paulson@1526
   356
fun merge_thy_list mk_draft thys =
paulson@1526
   357
  let
paulson@1526
   358
    fun is_union thy = forall (fn t => subthy (t, thy)) thys;
paulson@1526
   359
    val is_draft = Sign.is_draft o sign_of;
paulson@1526
   360
paulson@1526
   361
    fun add_sign (sg, Theory {sign, ...}) =
paulson@1526
   362
      Sign.merge (sg, sign) handle TERM (msg, _) => error msg;
paulson@1526
   363
  in
paulson@1539
   364
    case (find_first is_union thys, exists is_draft thys) of
paulson@1526
   365
      (Some thy, _) => thy
paulson@1526
   366
    | (None, true) => raise THEORY ("Illegal merge of draft theories", thys)
paulson@1526
   367
    | (None, false) => Theory {
paulson@1526
   368
        sign =
paulson@1526
   369
          (if mk_draft then Sign.make_draft else I)
paulson@1526
   370
          (foldl add_sign (Sign.proto_pure, thys)),
paulson@1539
   371
	oraopt = None,
paulson@1526
   372
        new_axioms = Symtab.null,
paulson@1539
   373
        parents = thys}
paulson@1526
   374
  end;
paulson@1526
   375
paulson@1526
   376
fun merge_theories (thy1, thy2) = merge_thy_list false [thy1, thy2];
paulson@1526
   377
paulson@1526
   378
paulson@1526
   379
end;
paulson@1526
   380
wenzelm@3767
   381
structure BasicTheory: BASIC_THEORY = Theory;
wenzelm@3767
   382
open BasicTheory;