type theory, theory_ref, exception THEORY and related operations imported from Context;
authorwenzelm
Fri Jun 17 18:33:28 2005 +0200 (2005-06-17)
changeset 1644382a116532e3e
parent 16442 1171ecf7fb7e
child 16444 80c8f742c6fc
type theory, theory_ref, exception THEORY and related operations imported from Context;
actual theory content declared as theory data;
removed syn_of;
import theory operations in SIGN_THEORY from Sign;
tuned;
src/Pure/theory.ML
     1.1 --- a/src/Pure/theory.ML	Fri Jun 17 18:33:27 2005 +0200
     1.2 +++ b/src/Pure/theory.ML	Fri Jun 17 18:33:28 2005 +0200
     1.3 @@ -2,250 +2,185 @@
     1.4      ID:         $Id$
     1.5      Author:     Lawrence C Paulson and Markus Wenzel
     1.6  
     1.7 -The abstract type "theory" of theories.
     1.8 +Logical theory content: axioms, definitions, oracles.
     1.9  *)
    1.10  
    1.11  signature BASIC_THEORY =
    1.12  sig
    1.13    type theory
    1.14 -  exception THEORY of string * theory list
    1.15 +  type theory_ref
    1.16 +  val sign_of: theory -> theory    (*dummy*)
    1.17    val rep_theory: theory ->
    1.18 -    {sign: Sign.sg,
    1.19 -      defs: Defs.graph,
    1.20 -      axioms: term NameSpace.table,
    1.21 -      oracles: ((Sign.sg * Object.T -> term) * stamp) NameSpace.table,
    1.22 -      parents: theory list,
    1.23 -      ancestors: theory list}
    1.24 -  val sign_of: theory -> Sign.sg
    1.25 -  val is_draft: theory -> bool
    1.26 -  val syn_of: theory -> Syntax.syntax
    1.27 +   {axioms: term NameSpace.table,
    1.28 +    defs: Defs.graph,
    1.29 +    oracles: ((theory * Object.T -> term) * stamp) NameSpace.table}
    1.30    val parents_of: theory -> theory list
    1.31    val ancestors_of: theory -> theory list
    1.32 +  val eq_thy: theory * theory -> bool
    1.33    val subthy: theory * theory -> bool
    1.34 -  val eq_thy: theory * theory -> bool
    1.35 -  val cert_axm: Sign.sg -> string * term -> string * term
    1.36 -  val read_def_axm: Sign.sg * (indexname -> typ option) * (indexname -> sort option) ->
    1.37 +  val cert_axm: theory -> string * term -> string * term
    1.38 +  val read_def_axm: theory * (indexname -> typ option) * (indexname -> sort option) ->
    1.39      string list -> string * string -> string * term
    1.40 -  val read_axm: Sign.sg -> string * string -> string * term
    1.41 -  val inferT_axm: Sign.sg -> string * term -> string * term
    1.42 +  val read_axm: theory -> string * string -> string * term
    1.43 +  val inferT_axm: theory -> string * term -> string * term
    1.44  end
    1.45  
    1.46  signature THEORY =
    1.47  sig
    1.48    include BASIC_THEORY
    1.49 +  include SIGN_THEORY
    1.50 +  val init: theory -> theory
    1.51 +  val axiom_space: theory -> NameSpace.T
    1.52 +  val oracle_space: theory -> NameSpace.T
    1.53    val axioms_of: theory -> (string * term) list
    1.54    val all_axioms_of: theory -> (string * term) list
    1.55 -  val add_classes: (bstring * xstring list) list -> theory -> theory
    1.56 -  val add_classes_i: (bstring * class list) list -> theory -> theory
    1.57 -  val add_classrel: (xstring * xstring) list -> theory -> theory
    1.58 -  val add_classrel_i: (class * class) list -> theory -> theory
    1.59 -  val add_defsort: string -> theory -> theory
    1.60 -  val add_defsort_i: sort -> theory -> theory
    1.61 -  val add_types: (bstring * int * mixfix) list -> theory -> theory
    1.62 -  val add_nonterminals: bstring list -> theory -> theory
    1.63 -  val add_tyabbrs: (bstring * string list * string * mixfix) list
    1.64 -    -> theory -> theory
    1.65 -  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list
    1.66 -    -> theory -> theory
    1.67 -  val add_arities: (xstring * string list * string) list -> theory -> theory
    1.68 -  val add_arities_i: (string * sort list * sort) list -> theory -> theory
    1.69 -  val add_consts: (bstring * string * mixfix) list -> theory -> theory
    1.70 -  val add_consts_i: (bstring * typ * mixfix) list -> theory -> theory
    1.71 -  val add_syntax: (bstring * string * mixfix) list -> theory -> theory
    1.72 -  val add_syntax_i: (bstring * typ * mixfix) list -> theory -> theory
    1.73 -  val add_modesyntax: string * bool -> (bstring * string * mixfix) list -> theory -> theory
    1.74 -  val add_modesyntax_i: string * bool -> (bstring * typ * mixfix) list -> theory -> theory
    1.75 -  val del_modesyntax: string * bool -> (bstring * string * mixfix) list -> theory -> theory
    1.76 -  val del_modesyntax_i: string * bool -> (bstring * typ * mixfix) list -> theory -> theory
    1.77 -  val add_trfuns:
    1.78 -    (string * (Syntax.ast list -> Syntax.ast)) list *
    1.79 -    (string * (term list -> term)) list *
    1.80 -    (string * (term list -> term)) list *
    1.81 -    (string * (Syntax.ast list -> Syntax.ast)) list -> theory -> theory
    1.82 -  val add_trfunsT:
    1.83 -    (string * (bool -> typ -> term list -> term)) list -> theory -> theory
    1.84 -  val add_advanced_trfuns:
    1.85 -    (string * (Sign.sg -> Syntax.ast list -> Syntax.ast)) list *
    1.86 -    (string * (Sign.sg -> term list -> term)) list *
    1.87 -    (string * (Sign.sg -> term list -> term)) list *
    1.88 -    (string * (Sign.sg -> Syntax.ast list -> Syntax.ast)) list -> theory -> theory
    1.89 -  val add_advanced_trfunsT:
    1.90 -    (string * (Sign.sg -> bool -> typ -> term list -> term)) list -> theory -> theory
    1.91 -  val add_tokentrfuns:
    1.92 -    (string * string * (string -> string * real)) list -> theory -> theory
    1.93 -  val add_mode_tokentrfuns: string -> (string * (string -> string * real)) list
    1.94 -    -> theory -> theory
    1.95 -  val add_trrules: (xstring * string) Syntax.trrule list -> theory -> theory
    1.96 -  val add_trrules_i: Syntax.ast Syntax.trrule list -> theory -> theory
    1.97 +  val self_ref: theory -> theory_ref
    1.98 +  val deref: theory_ref -> theory
    1.99 +  val merge: theory * theory -> theory                     (*exception TERM*)
   1.100 +  val merge_refs: theory_ref * theory_ref -> theory_ref    (*exception TERM*)
   1.101 +  val requires: theory -> string -> string -> unit
   1.102 +  val assert_super: theory -> theory -> theory
   1.103 +  val copy: theory -> theory
   1.104 +  val checkpoint: theory -> theory
   1.105    val add_axioms: (bstring * string) list -> theory -> theory
   1.106    val add_axioms_i: (bstring * term) list -> theory -> theory
   1.107 -  val add_oracle: bstring * (Sign.sg * Object.T -> term) -> theory -> theory
   1.108 -  val add_finals: bool -> string list -> theory -> theory
   1.109 -  val add_finals_i: bool -> term list -> theory -> theory
   1.110    val add_defs: bool -> (bstring * string) list -> theory -> theory
   1.111    val add_defs_i: bool -> (bstring * term) list -> theory -> theory
   1.112 -  val add_path: string -> theory -> theory
   1.113 -  val parent_path: theory -> theory
   1.114 -  val root_path: theory -> theory
   1.115 -  val absolute_path: theory -> theory
   1.116 -  val qualified_names: theory -> theory
   1.117 -  val no_base_names: theory -> theory
   1.118 -  val custom_accesses: (string list -> string list list) -> theory -> theory
   1.119 -  val set_policy: (string -> bstring -> string) * (string list -> string list list) ->
   1.120 -    theory -> theory
   1.121 -  val restore_naming: theory -> theory -> theory
   1.122 -  val hide_classes: bool -> xstring list -> theory -> theory
   1.123 -  val hide_classes_i: bool -> string list -> theory -> theory
   1.124 -  val hide_types: bool -> xstring list -> theory -> theory
   1.125 -  val hide_types_i: bool -> string list -> theory -> theory
   1.126 -  val hide_consts: bool -> xstring list -> theory -> theory
   1.127 -  val hide_consts_i: bool -> string list -> theory -> theory
   1.128 -  val add_name: string -> theory -> theory
   1.129 -  val copy: theory -> theory
   1.130 -  val prep_ext: theory -> theory
   1.131 -  val prep_ext_merge: theory list -> theory
   1.132 -  val requires: theory -> string -> string -> unit
   1.133 -  val assert_super: theory -> theory -> theory
   1.134 -  val pre_pure: theory
   1.135 +  val add_finals: bool -> string list -> theory -> theory
   1.136 +  val add_finals_i: bool -> term list -> theory -> theory
   1.137 +  val add_oracle: bstring * (theory * Object.T -> term) -> theory -> theory
   1.138  end;
   1.139  
   1.140 -signature PRIVATE_THEORY =
   1.141 -sig
   1.142 -  include THEORY
   1.143 -  val init_data: Object.kind -> (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
   1.144 -    (Object.T * Object.T -> Object.T) * (Sign.sg -> Object.T -> unit)) -> theory -> theory
   1.145 -  val print_data: Object.kind -> theory -> unit
   1.146 -  val get_data: Object.kind -> (Object.T -> 'a) -> theory -> 'a
   1.147 -  val put_data: Object.kind -> ('a -> Object.T) -> 'a -> theory -> theory
   1.148 -end;
   1.149 +structure Theory: THEORY =
   1.150 +struct
   1.151 +
   1.152 +(** type theory **)
   1.153 +
   1.154 +(* context operations *)
   1.155 +
   1.156 +type theory = Context.theory;
   1.157 +type theory_ref = Context.theory_ref;
   1.158 +
   1.159 +val eq_thy = Context.eq_thy;
   1.160 +val subthy = Context.subthy;
   1.161 +val copy = Context.copy_thy;
   1.162 +val checkpoint = Context.checkpoint_thy;
   1.163  
   1.164 -structure Theory: PRIVATE_THEORY =
   1.165 -struct
   1.166 +val parents_of = Context.parents_of;
   1.167 +val ancestors_of = Context.ancestors_of;
   1.168 +
   1.169 +val self_ref = Context.self_ref;
   1.170 +val deref = Context.deref;
   1.171 +val merge = Context.merge;
   1.172 +val merge_refs = Context.merge_refs;
   1.173 +
   1.174 +
   1.175 +(* signature operations *)
   1.176 +
   1.177 +val sign_of = I;
   1.178 +
   1.179 +structure SignTheory: SIGN_THEORY = Sign;
   1.180 +open SignTheory;
   1.181 +
   1.182  
   1.183  
   1.184 -(** datatype theory **)
   1.185 +(** diagnostics **)  (* FIXME belongs to defs.ML *)
   1.186 +
   1.187 +fun pretty_const pp (c, T) =
   1.188 + [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
   1.189 +  Pretty.quote (Pretty.typ pp (Type.freeze_type T))];    (* FIXME zero indexes!? *)
   1.190 +
   1.191 +fun pretty_path pp path = fold_rev (fn (T, c, def) =>
   1.192 +  fn [] => [Pretty.block (pretty_const pp (c, T))]
   1.193 +   | prts => Pretty.block (pretty_const pp (c, T) @
   1.194 +      [Pretty.brk 1, Pretty.str ("depends via " ^ quote def ^ " on")]) :: prts) path [];
   1.195 +
   1.196 +fun chain_history_msg s =    (* FIXME huh!? *)
   1.197 +  if Defs.chain_history () then s ^ ": "
   1.198 +  else s ^ " (set DEFS_CHAIN_HISTORY=ON for full history): ";
   1.199 +
   1.200 +fun defs_circular pp path =
   1.201 +  Pretty.str (chain_history_msg "Cyclic dependency of definitions") :: pretty_path pp path
   1.202 +  |> Pretty.chunks |> Pretty.string_of;
   1.203  
   1.204 -datatype theory =
   1.205 -  Theory of {
   1.206 -    sign: Sign.sg,
   1.207 -    defs: Defs.graph,
   1.208 -    axioms: term NameSpace.table,
   1.209 -    oracles: ((Sign.sg * Object.T -> term) * stamp) NameSpace.table,
   1.210 -    parents: theory list,
   1.211 -    ancestors: theory list};
   1.212 +fun defs_infinite_chain pp path =
   1.213 +  Pretty.str (chain_history_msg "Infinite chain of definitions") :: pretty_path pp path
   1.214 +  |> Pretty.chunks |> Pretty.string_of;
   1.215 +
   1.216 +fun defs_clash def1 def2 = "Type clash in definitions " ^ quote def1 ^ " and " ^ quote def2;
   1.217 +
   1.218 +fun defs_final pp const =
   1.219 +  (Pretty.str "Attempt to define final constant" :: Pretty.brk 1 :: pretty_const pp const)
   1.220 +  |> Pretty.block |> Pretty.string_of;
   1.221 +
   1.222 +
   1.223 +
   1.224 +(** datatype thy **)
   1.225 +
   1.226 +datatype thy = Thy of
   1.227 + {axioms: term NameSpace.table,
   1.228 +  defs: Defs.graph,
   1.229 +  oracles: ((theory * Object.T -> term) * stamp) NameSpace.table};
   1.230 +
   1.231 +fun make_thy (axioms, defs, oracles) =
   1.232 +  Thy {axioms = axioms, defs = defs, oracles = oracles};
   1.233  
   1.234 -fun make_theory sign defs axioms oracles parents ancestors =
   1.235 -  Theory {sign = sign, defs = defs, axioms = axioms,
   1.236 -    oracles = oracles, parents = parents, ancestors = ancestors};
   1.237 +fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
   1.238 +fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
   1.239  
   1.240 -fun rep_theory (Theory args) = args;
   1.241 +structure ThyData = TheoryDataFun
   1.242 +(struct
   1.243 +  val name = "Pure/thy";
   1.244 +  type T = thy;
   1.245 +  val empty = make_thy (NameSpace.empty_table, Defs.empty, NameSpace.empty_table);
   1.246 +  val copy = I;
   1.247 +
   1.248 +  fun extend (Thy {axioms, defs, oracles}) = make_thy (NameSpace.empty_table, defs, oracles);
   1.249 +
   1.250 +  fun merge pp (thy1, thy2) =
   1.251 +    let
   1.252 +      val Thy {axioms = _, defs = defs1, oracles = oracles1} = thy1;
   1.253 +      val Thy {axioms = _, defs = defs2, oracles = oracles2} = thy2;
   1.254  
   1.255 -val sign_of = #sign o rep_theory;
   1.256 -val is_draft = Sign.is_draft o sign_of;
   1.257 -val syn_of = Sign.syn_of o sign_of;
   1.258 -val parents_of = #parents o rep_theory;
   1.259 -val ancestors_of = #ancestors o rep_theory;
   1.260 +      val axioms = NameSpace.empty_table;
   1.261 +      val defs = Defs.merge defs1 defs2  (* FIXME produce errors in defs.ML *)
   1.262 +        handle Defs.CIRCULAR namess => error (defs_circular pp namess)
   1.263 +          | Defs.INFINITE_CHAIN namess => error (defs_infinite_chain pp namess);
   1.264 +      val oracles = NameSpace.merge_tables eq_snd (oracles1, oracles2)
   1.265 +        handle Symtab.DUPS dups => err_dup_oras dups;
   1.266 +    in make_thy (axioms, defs, oracles) end;
   1.267 +
   1.268 +  fun print _ _ = ();
   1.269 +end);
   1.270 +
   1.271 +val init = ThyData.init;
   1.272 +
   1.273 +fun rep_theory thy = ThyData.get thy |> (fn Thy args => args);
   1.274 +
   1.275 +fun map_thy f = ThyData.map (fn (Thy {axioms, defs, oracles}) =>
   1.276 +  make_thy (f (axioms, defs, oracles)));
   1.277 +
   1.278 +fun map_axioms f = map_thy (fn (axioms, defs, oracles) => (f axioms, defs, oracles));
   1.279 +fun map_defs f = map_thy (fn (axioms, defs, oracles) => (axioms, f defs, oracles));
   1.280 +fun map_oracles f = map_thy (fn (axioms, defs, oracles) => (axioms, defs, f oracles));
   1.281 +
   1.282 +
   1.283 +(* basic operations *)
   1.284 +
   1.285 +val axiom_space = #1 o #axioms o rep_theory;
   1.286 +val oracle_space = #1 o #oracles o rep_theory;
   1.287  
   1.288  val axioms_of = Symtab.dest o #2 o #axioms o rep_theory;
   1.289  fun all_axioms_of thy = List.concat (map axioms_of (thy :: ancestors_of thy));
   1.290  
   1.291 -(*errors involving theories*)
   1.292 -exception THEORY of string * theory list;
   1.293 -
   1.294 -(*compare theories*)
   1.295 -val subthy = Sign.subsig o pairself sign_of;
   1.296 -val eq_thy = Sign.eq_sg o pairself sign_of;
   1.297 -
   1.298 -(*check for some theory*)
   1.299  fun requires thy name what =
   1.300 -  if Sign.exists_stamp name (sign_of thy) then ()
   1.301 +  if Context.exists_name name thy then ()
   1.302    else error ("Require theory " ^ quote name ^ " as an ancestor for " ^ what);
   1.303  
   1.304  fun assert_super thy1 thy2 =
   1.305    if subthy (thy1, thy2) then thy2
   1.306    else raise THEORY ("Not a super theory", [thy1, thy2]);
   1.307  
   1.308 -(*partial Pure theory*)
   1.309 -val pre_pure =
   1.310 -  make_theory Sign.pre_pure Defs.empty NameSpace.empty_table NameSpace.empty_table [] [];
   1.311 -
   1.312 -
   1.313 -
   1.314 -(** extend theory **)
   1.315 -
   1.316 -(* extend logical part of a theory *)
   1.317 -
   1.318 -fun err_dup_axms dups = error ("Duplicate axiom(s): " ^ commas_quote dups);
   1.319 -fun err_dup_oras dups = error ("Duplicate oracle(s): " ^ commas_quote dups);
   1.320 -
   1.321 -fun ext_theory thy ext_sg axms oras =
   1.322 -  let
   1.323 -    val Theory {sign, defs, axioms, oracles, parents, ancestors} = thy;
   1.324 -    val draft = Sign.is_draft sign;
   1.325 -    val naming = Sign.naming_of sign;
   1.326 -
   1.327 -    val axioms' = NameSpace.extend_table naming
   1.328 -        (if draft then axioms else NameSpace.empty_table, axms)
   1.329 -      handle Symtab.DUPS dups => err_dup_axms dups;
   1.330 -    val oracles' = NameSpace.extend_table naming (oracles, oras)
   1.331 -      handle Symtab.DUPS dups => err_dup_oras dups;
   1.332 -
   1.333 -    val (parents', ancestors') =
   1.334 -      if draft then (parents, ancestors) else ([thy], thy :: ancestors);
   1.335 -  in make_theory (ext_sg sign) defs axioms' oracles' parents' ancestors' end;
   1.336 -
   1.337 -
   1.338 -(* extend signature of a theory *)
   1.339 -
   1.340 -fun ext_sign ext_sg args thy = ext_theory thy (ext_sg args) [] [];
   1.341 -
   1.342 -val add_classes            = ext_sign Sign.add_classes;
   1.343 -val add_classes_i          = ext_sign Sign.add_classes_i;
   1.344 -val add_classrel           = ext_sign Sign.add_classrel;
   1.345 -val add_classrel_i         = ext_sign Sign.add_classrel_i;
   1.346 -val add_defsort            = ext_sign Sign.add_defsort;
   1.347 -val add_defsort_i          = ext_sign Sign.add_defsort_i;
   1.348 -val add_types              = ext_sign Sign.add_types;
   1.349 -val add_nonterminals       = ext_sign Sign.add_nonterminals;
   1.350 -val add_tyabbrs            = ext_sign Sign.add_tyabbrs;
   1.351 -val add_tyabbrs_i          = ext_sign Sign.add_tyabbrs_i;
   1.352 -val add_arities            = ext_sign Sign.add_arities;
   1.353 -val add_arities_i          = ext_sign Sign.add_arities_i;
   1.354 -val add_consts             = ext_sign Sign.add_consts;
   1.355 -val add_consts_i           = ext_sign Sign.add_consts_i;
   1.356 -val add_syntax             = ext_sign Sign.add_syntax;
   1.357 -val add_syntax_i           = ext_sign Sign.add_syntax_i;
   1.358 -val add_modesyntax         = curry (ext_sign Sign.add_modesyntax);
   1.359 -val add_modesyntax_i       = curry (ext_sign Sign.add_modesyntax_i);
   1.360 -val del_modesyntax         = curry (ext_sign Sign.del_modesyntax);
   1.361 -val del_modesyntax_i       = curry (ext_sign Sign.del_modesyntax_i);
   1.362 -val add_trfuns             = ext_sign Sign.add_trfuns;
   1.363 -val add_trfunsT            = ext_sign Sign.add_trfunsT;
   1.364 -val add_advanced_trfuns    = ext_sign Sign.add_advanced_trfuns;
   1.365 -val add_advanced_trfunsT   = ext_sign Sign.add_advanced_trfunsT;
   1.366 -val add_tokentrfuns        = ext_sign Sign.add_tokentrfuns;
   1.367 -fun add_mode_tokentrfuns m = add_tokentrfuns o map (fn (s, f) => (m, s, f));
   1.368 -val add_trrules            = ext_sign Sign.add_trrules;
   1.369 -val add_trrules_i          = ext_sign Sign.add_trrules_i;
   1.370 -val add_path               = ext_sign Sign.add_path;
   1.371 -val parent_path            = add_path "..";
   1.372 -val root_path              = add_path "/";
   1.373 -val absolute_path          = add_path "//";
   1.374 -val qualified_names        = ext_sign (K Sign.qualified_names) ();
   1.375 -val no_base_names          = ext_sign (K Sign.no_base_names) ();
   1.376 -val custom_accesses        = ext_sign Sign.custom_accesses;
   1.377 -val set_policy             = ext_sign Sign.set_policy;
   1.378 -val restore_naming         = ext_sign Sign.restore_naming o sign_of;
   1.379 -val hide_classes           = ext_sign o Sign.hide_classes;
   1.380 -val hide_classes_i         = ext_sign o Sign.hide_classes_i;
   1.381 -val hide_types             = ext_sign o Sign.hide_types;
   1.382 -val hide_types_i           = ext_sign o Sign.hide_types_i;
   1.383 -val hide_consts            = ext_sign o Sign.hide_consts;
   1.384 -val hide_consts_i          = ext_sign o Sign.hide_consts_i;
   1.385 -val add_name               = ext_sign Sign.add_name;
   1.386 -val copy                   = ext_sign (K Sign.copy) ();
   1.387 -val prep_ext               = ext_sign (K Sign.prep_ext) ();
   1.388 -
   1.389  
   1.390  
   1.391  (** add axioms **)
   1.392 @@ -262,10 +197,10 @@
   1.393        (Pretty.str "Illegal schematic variable(s) in term:" ::
   1.394         map (Pretty.term pp) ts @ map (Pretty.typ pp o TVar) ixns)))));
   1.395  
   1.396 -fun cert_axm sg (name, raw_tm) =
   1.397 +fun cert_axm thy (name, raw_tm) =
   1.398    let
   1.399 -    val pp = Sign.pp sg;
   1.400 -    val (t, T, _) = Sign.certify_term pp sg raw_tm
   1.401 +    val pp = Sign.pp thy;
   1.402 +    val (t, T, _) = Sign.certify_term pp thy raw_tm
   1.403        handle TYPE (msg, _, _) => error msg
   1.404          | TERM (msg, _) => error msg;
   1.405    in
   1.406 @@ -274,33 +209,33 @@
   1.407      (name, no_vars pp t)
   1.408    end;
   1.409  
   1.410 -(*some duplication of code with read_def_cterm*)
   1.411 -fun read_def_axm (sg, types, sorts) used (name, str) =
   1.412 +fun read_def_axm (thy, types, sorts) used (name, str) =
   1.413    let
   1.414 -    val ts = Syntax.read (Sign.is_logtype sg) (Sign.syn_of sg) propT str;
   1.415 -    val (t, _) = Sign.infer_types (Sign.pp sg) sg types sorts used true (ts, propT);
   1.416 -  in cert_axm sg (name, t) end
   1.417 +    val ts = Syntax.read (Sign.is_logtype thy) (Sign.syn_of thy) propT str;
   1.418 +    val (t, _) = Sign.infer_types (Sign.pp thy) thy types sorts used true (ts, propT);
   1.419 +  in cert_axm thy (name, t) end
   1.420    handle ERROR => err_in_axm name;
   1.421  
   1.422 -fun read_axm sg name_str = read_def_axm (sg, K NONE, K NONE) [] name_str;
   1.423 +fun read_axm thy name_str = read_def_axm (thy, K NONE, K NONE) [] name_str;
   1.424  
   1.425 -fun inferT_axm sg (name, pre_tm) =
   1.426 +fun inferT_axm thy (name, pre_tm) =
   1.427    let
   1.428 -    val pp = Sign.pp sg;
   1.429 -    val (t, _) = Sign.infer_types pp sg (K NONE) (K NONE) [] true ([pre_tm], propT);
   1.430 +    val pp = Sign.pp thy;
   1.431 +    val (t, _) = Sign.infer_types pp thy (K NONE) (K NONE) [] true ([pre_tm], propT);
   1.432    in (name, no_vars pp t) end
   1.433    handle ERROR => err_in_axm name;
   1.434  
   1.435  
   1.436 -(* extend axioms of a theory *)
   1.437 +(* add_axioms(_i) *)
   1.438  
   1.439  local
   1.440  
   1.441 -fun gen_add_axioms prep_axm raw_axms thy =
   1.442 +fun gen_add_axioms prep_axm raw_axms thy = thy |> map_axioms (fn axioms =>
   1.443    let
   1.444 -    val sg = sign_of thy;
   1.445 -    val axms = map (apsnd (Term.compress_term o Logic.varify) o prep_axm sg) raw_axms;
   1.446 -  in ext_theory thy I axms [] end;
   1.447 +    val axms = map (apsnd (Term.compress_term o Logic.varify) o prep_axm thy) raw_axms;
   1.448 +    val axioms' = NameSpace.extend_table (Sign.naming_of thy) (axioms, axms)
   1.449 +      handle Symtab.DUPS dups => err_dup_axms dups;
   1.450 +  in axioms' end);
   1.451  
   1.452  in
   1.453  
   1.454 @@ -310,12 +245,6 @@
   1.455  end;
   1.456  
   1.457  
   1.458 -(* add oracle **)
   1.459 -
   1.460 -fun add_oracle (bname, oracle) thy =
   1.461 -  ext_theory thy I [] [(bname, (oracle, stamp ()))];
   1.462 -
   1.463 -
   1.464  
   1.465  (** add constant definitions **)
   1.466  
   1.467 @@ -323,12 +252,12 @@
   1.468  
   1.469  datatype overloading = Clean | Implicit | Useless;
   1.470  
   1.471 -fun overloading sg overloaded declT defT =
   1.472 +fun overloading thy overloaded declT defT =
   1.473    let
   1.474      val defT' = Term.incr_tvar (maxidx_of_typ declT + 1) (Type.varifyT defT);
   1.475    in
   1.476 -    if Sign.typ_instance sg (declT, defT') then Clean
   1.477 -    else if Sign.typ_instance sg (Type.strip_sorts declT, Type.strip_sorts defT') then Useless
   1.478 +    if Sign.typ_instance thy (declT, defT') then Clean
   1.479 +    else if Sign.typ_instance thy (Type.strip_sorts declT, Type.strip_sorts defT') then Useless
   1.480      else if overloaded then Clean
   1.481      else Implicit
   1.482    end;
   1.483 @@ -375,70 +304,42 @@
   1.484  
   1.485  (* check_def *)
   1.486  
   1.487 -fun pretty_const pp (c, T) =
   1.488 - [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
   1.489 -  Pretty.quote (Pretty.typ pp (Type.freeze_type T))];    (* FIXME zero indexes!? *)
   1.490 -
   1.491 -(* FIXME move error messages to defs.ML !? *)
   1.492 -
   1.493 -fun pretty_path pp path = fold_rev (fn (T, c, def) =>
   1.494 -  fn [] => [Pretty.block (pretty_const pp (c, T))]
   1.495 -   | prts => Pretty.block (pretty_const pp (c, T) @
   1.496 -      [Pretty.brk 1, Pretty.str ("depends via " ^ quote def ^ " on")]) :: prts) path [];
   1.497 -
   1.498 -fun chain_history_msg s = 
   1.499 -    if Defs.chain_history () then s^": " 
   1.500 -    else s^" (set DEFS_CHAIN_HISTORY=ON for full history): "
   1.501 -
   1.502 -fun defs_circular pp path =
   1.503 -  Pretty.str (chain_history_msg "Cyclic dependency of definitions") :: pretty_path pp path
   1.504 -  |> Pretty.chunks |> Pretty.string_of;
   1.505 -
   1.506 -fun defs_infinite_chain pp path =
   1.507 -  Pretty.str (chain_history_msg "Infinite chain of definitions") :: pretty_path pp path
   1.508 -  |> Pretty.chunks |> Pretty.string_of;
   1.509 -
   1.510 -fun defs_clash def1 def2 = "Type clash in definitions " ^ quote def1 ^ " and " ^ quote def2;
   1.511 -
   1.512 -fun defs_final pp const =
   1.513 -  (Pretty.str "Attempt to define final constant" :: Pretty.brk 1 :: pretty_const pp const)
   1.514 -  |> Pretty.block |> Pretty.string_of;
   1.515 -
   1.516 -(* FIXME move to defs.ML; avoid exceptions *)
   1.517 -fun declare sg c defs =
   1.518 -  let val T = Sign.the_const_type sg c
   1.519 +fun declare thy c defs =      (* FIXME move to defs.ML *)
   1.520 +  let val T = Sign.the_const_type thy c
   1.521    in if_none (try (Defs.declare defs) (c, T)) defs end;
   1.522  
   1.523 -
   1.524 -fun check_def sg overloaded (bname, tm) defs =
   1.525 +fun check_def thy overloaded (bname, tm) defs =
   1.526    let
   1.527 -    val pp = Sign.pp sg;
   1.528 -    val name = Sign.full_name sg bname;
   1.529 +    val pp = Sign.pp thy;
   1.530  
   1.531      fun err msg = error (Pretty.string_of (Pretty.chunks
   1.532 -     [Pretty.str msg,
   1.533 -      Pretty.block [Pretty.str ("The error(s) above occurred in definition " ^ quote name ^ ":"),
   1.534 +     [Pretty.str msg, Pretty.block
   1.535 +      [Pretty.str ("The error(s) above occurred in definition " ^ quote bname ^ ":"),
   1.536          Pretty.fbrk, Pretty.quote (Pretty.term pp tm)]]));
   1.537  
   1.538 -    fun show_def const txt =
   1.539 +    fun prt_const (c, T) =
   1.540 +     [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
   1.541 +      Pretty.quote (Pretty.typ pp (Type.freeze_type T))];
   1.542 +    fun string_of_def const txt =
   1.543        [Pretty.block (Pretty.str "Definition of " :: pretty_const pp const), Pretty.str txt]
   1.544        |> Pretty.chunks |> Pretty.string_of;
   1.545  
   1.546      val ((c, defT), rhs) = dest_def pp tm handle TERM (msg, _) => err msg;
   1.547      val rhs_consts = Term.term_constsT rhs;
   1.548 -    val declT = Sign.the_const_type sg c;
   1.549 +    val declT = Sign.the_const_type thy c;
   1.550  
   1.551      val _ =
   1.552 -      (case overloading sg overloaded declT defT of
   1.553 +      (case overloading thy overloaded declT defT of
   1.554          Clean => ()
   1.555 -      | Implicit => warning (show_def (c, defT)
   1.556 -          ("is strictly less general than the declared type (see " ^ quote name ^ ")"))
   1.557 -      | Useless => err (Library.setmp show_sorts true (show_def (c, defT))
   1.558 +      | Implicit => warning (string_of_def (c, defT)
   1.559 +          ("is strictly less general than the declared type (see " ^ quote bname ^ ")"))
   1.560 +      | Useless => err (Library.setmp show_sorts true (string_of_def (c, defT))
   1.561            "imposes additional sort constraints on the declared type of the constant"));
   1.562  
   1.563 -    val decl_defs = defs |> declare sg c |> fold (declare sg) (map #1 rhs_consts);
   1.564 +    val decl_defs = defs |> declare thy c |> fold (declare thy) (map #1 rhs_consts);
   1.565    in
   1.566 -    Defs.define decl_defs (c, defT) name rhs_consts
   1.567 +    Defs.define decl_defs (c, defT) (Sign.full_name thy bname) rhs_consts
   1.568 +      (* FIXME produce errors in defs.ML *)
   1.569        handle Defs.DEFS msg => err ("DEFS: " ^ msg)   (* FIXME sys_error!? *)
   1.570          | Defs.CIRCULAR path => err (defs_circular pp path)
   1.571          | Defs.INFINITE_CHAIN path => err (defs_infinite_chain pp path)
   1.572 @@ -447,17 +348,14 @@
   1.573    end;
   1.574  
   1.575  
   1.576 -(* add_defs *)
   1.577 +(* add_defs(_i) *)
   1.578  
   1.579  local
   1.580  
   1.581  fun gen_add_defs prep_axm overloaded raw_axms thy =
   1.582 -  let
   1.583 -    val Theory {sign, defs, axioms, oracles, parents, ancestors} = thy;
   1.584 -    val axms = map (prep_axm sign) raw_axms;
   1.585 -    val defs' = fold (check_def sign overloaded) axms defs;
   1.586 -  in
   1.587 -    make_theory sign defs' axioms oracles parents ancestors
   1.588 +  let val axms = map (prep_axm thy) raw_axms in
   1.589 +    thy
   1.590 +    |> map_defs (fold (check_def thy overloaded) axms)
   1.591      |> add_axioms_i axms
   1.592    end;
   1.593  
   1.594 @@ -469,13 +367,12 @@
   1.595  end;
   1.596  
   1.597  
   1.598 -(* add_finals *)
   1.599 +(* add_finals(_i) *)
   1.600  
   1.601  local
   1.602  
   1.603  fun gen_add_finals prep_term overloaded raw_terms thy =
   1.604    let
   1.605 -    val Theory {sign, defs, axioms, oracles, parents, ancestors} = thy;
   1.606      fun finalize tm finals =
   1.607        let
   1.608          fun err msg = raise TERM (msg, [tm]);    (* FIXME error!? *)
   1.609 @@ -483,22 +380,19 @@
   1.610            (case tm of Const x => x
   1.611            | Free _ => err "Attempt to finalize variable (or undeclared constant)"
   1.612            | _ => err "Attempt to finalize non-constant term");
   1.613 -        val declT = Sign.the_const_type sign c
   1.614 +        val declT = Sign.the_const_type thy c
   1.615            handle TYPE (msg, _, _) => err msg;
   1.616 -        val _ =
   1.617 -          (case overloading sign overloaded declT defT of
   1.618 +        val _ =    (* FIXME unify messages with defs *)
   1.619 +          (case overloading thy overloaded declT defT of
   1.620              Clean => ()
   1.621 -          | Implcit => warning ("Finalizing " ^ quote c ^
   1.622 +          | Implicit => warning ("Finalizing " ^ quote c ^
   1.623                " at a strictly less general type than declared")
   1.624            | Useless => err "Sort constraints stronger than declared");
   1.625 -      in
   1.626 -        Defs.finalize (if_none (try (Defs.declare finals) (c, declT)) finals) (c, defT)
   1.627 -      end;
   1.628 -    val defs' = fold finalize (map (prep_term sign) raw_terms) defs;
   1.629 -  in make_theory sign defs' axioms oracles parents ancestors end;
   1.630 +      in Defs.finalize (if_none (try (Defs.declare finals) (c, declT)) finals) (c, defT) end;
   1.631 +  in thy |> map_defs (fold finalize (map (prep_term thy) raw_terms)) end;
   1.632  
   1.633 -fun read_term sg = Sign.simple_read_term sg TypeInfer.logicT;
   1.634 -fun cert_term sg = #1 o Sign.certify_term (Sign.pp sg) sg;
   1.635 +fun read_term thy = Sign.simple_read_term thy TypeInfer.logicT;
   1.636 +fun cert_term thy = #1 o Sign.certify_term (Sign.pp thy) thy;
   1.637  
   1.638  in
   1.639  
   1.640 @@ -509,43 +403,11 @@
   1.641  
   1.642  
   1.643  
   1.644 -(** additional theory data **)
   1.645 -
   1.646 -val init_data = curry (ext_sign Sign.init_data);
   1.647 -fun print_data kind = Sign.print_data kind o sign_of;
   1.648 -fun get_data kind f = Sign.get_data kind f o sign_of;
   1.649 -fun put_data kind f = ext_sign (Sign.put_data kind f);
   1.650 -
   1.651 -
   1.652 -
   1.653 -(** merge theories **)          (*exception ERROR*)
   1.654 -
   1.655 -(*merge list of theories from left to right, preparing extend*)
   1.656 -fun prep_ext_merge thys =
   1.657 -  let
   1.658 -    val sign' = Sign.prep_ext_merge (map sign_of thys);
   1.659 +(** add oracle **)
   1.660  
   1.661 -    val defss = map (#defs o rep_theory) thys;
   1.662 -    val defs' = foldl (uncurry Defs.merge) (hd defss) (tl defss)
   1.663 -      handle Defs.CIRCULAR namess => error (defs_circular (Sign.pp sign') namess)
   1.664 -        | Defs.INFINITE_CHAIN namess => error (defs_infinite_chain (Sign.pp sign') namess);
   1.665 -
   1.666 -    val axioms' = NameSpace.empty_table;
   1.667 -
   1.668 -    val oras_spaces = map (#1 o #oracles o rep_theory) thys;
   1.669 -    val oras_space' = Library.foldl NameSpace.merge (hd oras_spaces, tl oras_spaces);
   1.670 -    fun eq_ora ((_, (_, s1: stamp)), (_, (_, s2))) = s1 = s2;
   1.671 -    val oras' =
   1.672 -      Symtab.make (gen_distinct eq_ora
   1.673 -        (List.concat (map (Symtab.dest o #2 o #oracles o rep_theory) thys)))
   1.674 -      handle Symtab.DUPS names => err_dup_oras names;
   1.675 -    val oracles' = (oras_space', oras');
   1.676 -
   1.677 -    val parents' = gen_distinct eq_thy thys;
   1.678 -    val ancestors' =
   1.679 -      gen_distinct eq_thy (parents' @ List.concat (map ancestors_of thys));
   1.680 -  in make_theory sign' defs' axioms' oracles' parents' ancestors' end;
   1.681 -
   1.682 +fun add_oracle (bname, oracle) thy = thy |> map_oracles (fn oracles =>
   1.683 +  NameSpace.extend_table (Sign.naming_of thy) (oracles, [(bname, (oracle, stamp ()))])
   1.684 +    handle Symtab.DUPS dups => err_dup_oras dups);
   1.685  
   1.686  end;
   1.687