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