--- a/src/Pure/sign.ML Fri Jun 17 18:33:26 2005 +0200
+++ b/src/Pure/sign.ML Fri Jun 17 18:33:27 2005 +0200
@@ -2,482 +2,256 @@
ID: $Id$
Author: Lawrence C Paulson and Markus Wenzel
-The abstract type "sg" of signatures.
+Logical signature content: naming conventions, concrete syntax, type
+signature, constant declarations.
*)
-signature SIGN =
+signature SIGN_THEORY =
sig
- type sg
- type sg_ref
- type data
- val rep_sg: sg ->
- {self: sg_ref,
- tsig: Type.tsig,
- consts: (typ * stamp) NameSpace.table,
- naming: NameSpace.naming,
- data: data}
- val stamp_names_of: sg -> string list
- val exists_stamp: string -> sg -> bool
- val pretty_sg: sg -> Pretty.T
- val pprint_sg: sg -> pprint_args -> unit
- val pretty_abbrev_sg: sg -> Pretty.T
- val str_of_sg: sg -> string
- val is_stale: sg -> bool
- val self_ref: sg -> sg_ref
- val deref: sg_ref -> sg
- val name_of: sg -> string
- val is_draft: sg -> bool
- val eq_sg: sg * sg -> bool
- val subsig: sg * sg -> bool
- val same_sg: sg * sg -> bool
- val joinable: sg * sg -> bool
- val prep_ext: sg -> sg
- val copy: sg -> sg
- val add_name: string -> sg -> sg
- val data_kinds: data -> string list
- val print_all_data: sg -> unit
- val pre_pure: sg
- val PureN: string
- val CPureN: string
- val naming_of: sg -> NameSpace.naming
- val base_name: string -> bstring
- val full_name: sg -> bstring -> string
- val full_name_path: sg -> string -> bstring -> string
- val declare_name: sg -> string -> NameSpace.T -> NameSpace.T
- val syn_of: sg -> Syntax.syntax
- val tsig_of: sg -> Type.tsig
- val classes: sg -> class list
- val defaultS: sg -> sort
- val subsort: sg -> sort * sort -> bool
- val of_sort: sg -> typ * sort -> bool
- val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
- val universal_witness: sg -> (typ * sort) option
- val typ_instance: sg -> typ * typ -> bool
- val is_logtype: sg -> string -> bool
- val const_type: sg -> string -> typ option
- val the_const_type: sg -> string -> typ
- val declared_tyname: sg -> string -> bool
- val declared_const: sg -> string -> bool
- val class_space: sg -> NameSpace.T
- val type_space: sg -> NameSpace.T
- val const_space: sg -> NameSpace.T
- val intern_class: sg -> xstring -> string
- val extern_class: sg -> string -> xstring
- val intern_type: sg -> xstring -> string
- val extern_type: sg -> string -> xstring
- val intern_const: sg -> xstring -> string
- val extern_const: sg -> string -> xstring
- val intern_sort: sg -> sort -> sort
- val extern_sort: sg -> sort -> sort
- val intern_typ: sg -> typ -> typ
- val extern_typ: sg -> typ -> typ
- val intern_term: sg -> term -> term
- val extern_term: sg -> term -> term
- val intern_tycons: sg -> typ -> typ
- val pretty_term': Syntax.syntax -> sg -> term -> Pretty.T
- val pretty_term: sg -> term -> Pretty.T
- val pretty_typ: sg -> typ -> Pretty.T
- val pretty_sort: sg -> sort -> Pretty.T
- val pretty_classrel: sg -> class list -> Pretty.T
- val pretty_arity: sg -> arity -> Pretty.T
- val string_of_term: sg -> term -> string
- val string_of_typ: sg -> typ -> string
- val string_of_sort: sg -> sort -> string
- val string_of_classrel: sg -> class list -> string
- val string_of_arity: sg -> arity -> string
- val pprint_term: sg -> term -> pprint_args -> unit
- val pprint_typ: sg -> typ -> pprint_args -> unit
- val pp: sg -> Pretty.pp
- val certify_class: sg -> class -> class
- val certify_sort: sg -> sort -> sort
- val certify_typ: sg -> typ -> typ
- val certify_typ_syntax: sg -> typ -> typ
- val certify_typ_abbrev: sg -> typ -> typ
- val certify_term: Pretty.pp -> sg -> term -> term * typ * int
- val read_sort': Syntax.syntax -> sg -> string -> sort
- val read_sort: sg -> string -> sort
- val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
- val read_typ_syntax': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
- val read_typ_abbrev': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
- val read_typ: sg * (indexname -> sort option) -> string -> typ
- val read_typ_syntax: sg * (indexname -> sort option) -> string -> typ
- val read_typ_abbrev: sg * (indexname -> sort option) -> string -> typ
- val read_tyname: sg -> string -> typ
- val read_const: sg -> string -> term
- val infer_types_simult: Pretty.pp -> sg -> (indexname -> typ option) ->
- (indexname -> sort option) -> string list -> bool
- -> (term list * typ) list -> term list * (indexname * typ) list
- val infer_types: Pretty.pp -> sg -> (indexname -> typ option) ->
- (indexname -> sort option) -> string list -> bool
- -> term list * typ -> term * (indexname * typ) list
- val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax ->
- sg * (indexname -> typ option) * (indexname -> sort option) ->
- string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
- val read_def_terms:
- sg * (indexname -> typ option) * (indexname -> sort option) ->
- string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
- val simple_read_term: sg -> typ -> string -> term
- val add_defsort: string -> sg -> sg
- val add_defsort_i: sort -> sg -> sg
- val add_types: (bstring * int * mixfix) list -> sg -> sg
- val add_nonterminals: bstring list -> sg -> sg
- val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg
- val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg
- val add_arities: (xstring * string list * string) list -> sg -> sg
- val add_arities_i: (string * sort list * sort) list -> sg -> sg
- val add_syntax: (bstring * string * mixfix) list -> sg -> sg
- val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
- val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
- val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
- val del_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
- val del_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
- val add_consts: (bstring * string * mixfix) list -> sg -> sg
- val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
- val const_of_class: class -> string
- val class_of_const: string -> class
- val add_classes: (bstring * xstring list) list -> sg -> sg
- val add_classes_i: (bstring * class list) list -> sg -> sg
- val add_classrel: (xstring * xstring) list -> sg -> sg
- val add_classrel_i: (class * class) list -> sg -> sg
+ val add_defsort: string -> theory -> theory
+ val add_defsort_i: sort -> theory -> theory
+ val add_types: (bstring * int * mixfix) list -> theory -> theory
+ val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
+ val add_nonterminals: bstring list -> theory -> theory
+ val add_tyabbrs: (bstring * string list * string * mixfix) list -> theory -> theory
+ val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> theory -> theory
+ val add_arities: (xstring * string list * string) list -> theory -> theory
+ val add_arities_i: (string * sort list * sort) list -> theory -> theory
+ val add_syntax: (bstring * string * mixfix) list -> theory -> theory
+ val add_syntax_i: (bstring * typ * mixfix) list -> theory -> theory
+ val add_modesyntax: (string * bool) -> (bstring * string * mixfix) list -> theory -> theory
+ val add_modesyntax_i: (string * bool) -> (bstring * typ * mixfix) list -> theory -> theory
+ val del_modesyntax: (string * bool) -> (bstring * string * mixfix) list -> theory -> theory
+ val del_modesyntax_i: (string * bool) -> (bstring * typ * mixfix) list -> theory -> theory
+ val add_consts: (bstring * string * mixfix) list -> theory -> theory
+ val add_consts_i: (bstring * typ * mixfix) list -> theory -> theory
+ val add_classes: (bstring * xstring list) list -> theory -> theory
+ val add_classes_i: (bstring * class list) list -> theory -> theory
+ val add_classrel: (xstring * xstring) list -> theory -> theory
+ val add_classrel_i: (class * class) list -> theory -> theory
val add_trfuns:
(string * (ast list -> ast)) list *
(string * (term list -> term)) list *
(string * (term list -> term)) list *
- (string * (ast list -> ast)) list -> sg -> sg
+ (string * (ast list -> ast)) list -> theory -> theory
val add_trfunsT:
- (string * (bool -> typ -> term list -> term)) list -> sg -> sg
+ (string * (bool -> typ -> term list -> term)) list -> theory -> theory
val add_advanced_trfuns:
- (string * (sg -> ast list -> ast)) list *
- (string * (sg -> term list -> term)) list *
- (string * (sg -> term list -> term)) list *
- (string * (sg -> ast list -> ast)) list -> sg -> sg
+ (string * (theory -> ast list -> ast)) list *
+ (string * (theory -> term list -> term)) list *
+ (string * (theory -> term list -> term)) list *
+ (string * (theory -> ast list -> ast)) list -> theory -> theory
val add_advanced_trfunsT:
- (string * (sg -> bool -> typ -> term list -> term)) list -> sg -> sg
+ (string * (theory -> bool -> typ -> term list -> term)) list -> theory -> theory
val add_tokentrfuns:
- (string * string * (string -> string * real)) list -> sg -> sg
- val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg
- val add_trrules_i: ast Syntax.trrule list -> sg -> sg
- val add_path: string -> sg -> sg
- val qualified_names: sg -> sg
- val no_base_names: sg -> sg
- val custom_accesses: (string list -> string list list) -> sg -> sg
- val set_policy: (string -> bstring -> string) * (string list -> string list list) -> sg -> sg
- val restore_naming: sg -> sg -> sg
- val hide_classes: bool -> xstring list -> sg -> sg
- val hide_classes_i: bool -> string list -> sg -> sg
- val hide_types: bool -> xstring list -> sg -> sg
- val hide_types_i: bool -> string list -> sg -> sg
- val hide_consts: bool -> xstring list -> sg -> sg
- val hide_consts_i: bool -> string list -> sg -> sg
- val merge_refs: sg_ref * sg_ref -> sg_ref
- val merge: sg * sg -> sg
- val prep_ext_merge: sg list -> sg
-end;
+ (string * string * (string -> string * real)) list -> theory -> theory
+ val add_mode_tokentrfuns: string -> (string * (string -> string * real)) list
+ -> theory -> theory
+ val add_trrules: (xstring * string) Syntax.trrule list -> theory -> theory
+ val add_trrules_i: ast Syntax.trrule list -> theory -> theory
+ val add_path: string -> theory -> theory
+ val parent_path: theory -> theory
+ val root_path: theory -> theory
+ val absolute_path: theory -> theory
+ val local_path: theory -> theory
+ val qualified_names: theory -> theory
+ val no_base_names: theory -> theory
+ val custom_accesses: (string list -> string list list) -> theory -> theory
+ val set_policy: (string -> bstring -> string) * (string list -> string list list) ->
+ theory -> theory
+ val restore_naming: theory -> theory -> theory
+ val hide_classes: bool -> xstring list -> theory -> theory
+ val hide_classes_i: bool -> string list -> theory -> theory
+ val hide_types: bool -> xstring list -> theory -> theory
+ val hide_types_i: bool -> string list -> theory -> theory
+ val hide_consts: bool -> xstring list -> theory -> theory
+ val hide_consts_i: bool -> string list -> theory -> theory
+end
-signature PRIVATE_SIGN =
+signature SIGN =
sig
- include SIGN
- val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) *
- (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg
- val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a
- val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
- val print_data: Object.kind -> sg -> unit
-end;
+ type syn
+ type sg (*obsolete*)
+ val init: theory -> theory
+ val rep_sg: theory ->
+ {naming: NameSpace.naming,
+ syn: syn,
+ tsig: Type.tsig,
+ consts: (typ * stamp) NameSpace.table}
+ val naming_of: theory -> NameSpace.naming
+ val base_name: string -> bstring
+ val full_name: theory -> bstring -> string
+ val full_name_path: theory -> string -> bstring -> string
+ val declare_name: theory -> string -> NameSpace.T -> NameSpace.T
+ val syn_of: theory -> Syntax.syntax
+ val tsig_of: theory -> Type.tsig
+ val classes: theory -> class list
+ val defaultS: theory -> sort
+ val subsort: theory -> sort * sort -> bool
+ val of_sort: theory -> typ * sort -> bool
+ val witness_sorts: theory -> sort list -> sort list -> (typ * sort) list
+ val universal_witness: theory -> (typ * sort) option
+ val typ_instance: theory -> typ * typ -> bool
+ val is_logtype: theory -> string -> bool
+ val const_type: theory -> string -> typ option
+ val the_const_type: theory -> string -> typ
+ val declared_tyname: theory -> string -> bool
+ val declared_const: theory -> string -> bool
+ val class_space: theory -> NameSpace.T
+ val type_space: theory -> NameSpace.T
+ val const_space: theory -> NameSpace.T
+ val intern_class: theory -> xstring -> string
+ val extern_class: theory -> string -> xstring
+ val intern_type: theory -> xstring -> string
+ val extern_type: theory -> string -> xstring
+ val intern_const: theory -> xstring -> string
+ val extern_const: theory -> string -> xstring
+ val intern_sort: theory -> sort -> sort
+ val extern_sort: theory -> sort -> sort
+ val intern_typ: theory -> typ -> typ
+ val extern_typ: theory -> typ -> typ
+ val intern_term: theory -> term -> term
+ val extern_term: theory -> term -> term
+ val intern_tycons: theory -> typ -> typ
+ val pretty_term': Syntax.syntax -> theory -> term -> Pretty.T
+ val pretty_term: theory -> term -> Pretty.T
+ val pretty_typ: theory -> typ -> Pretty.T
+ val pretty_sort: theory -> sort -> Pretty.T
+ val pretty_classrel: theory -> class list -> Pretty.T
+ val pretty_arity: theory -> arity -> Pretty.T
+ val string_of_term: theory -> term -> string
+ val string_of_typ: theory -> typ -> string
+ val string_of_sort: theory -> sort -> string
+ val string_of_classrel: theory -> class list -> string
+ val string_of_arity: theory -> arity -> string
+ val pprint_term: theory -> term -> pprint_args -> unit
+ val pprint_typ: theory -> typ -> pprint_args -> unit
+ val pp: theory -> Pretty.pp
+ val certify_class: theory -> class -> class
+ val certify_sort: theory -> sort -> sort
+ val certify_typ: theory -> typ -> typ
+ val certify_typ_syntax: theory -> typ -> typ
+ val certify_typ_abbrev: theory -> typ -> typ
+ val certify_term: Pretty.pp -> theory -> term -> term * typ * int
+ val read_sort': Syntax.syntax -> theory -> string -> sort
+ val read_sort: theory -> string -> sort
+ val read_typ': Syntax.syntax -> theory * (indexname -> sort option) -> string -> typ
+ val read_typ_syntax': Syntax.syntax -> theory * (indexname -> sort option) -> string -> typ
+ val read_typ_abbrev': Syntax.syntax -> theory * (indexname -> sort option) -> string -> typ
+ val read_typ: theory * (indexname -> sort option) -> string -> typ
+ val read_typ_syntax: theory * (indexname -> sort option) -> string -> typ
+ val read_typ_abbrev: theory * (indexname -> sort option) -> string -> typ
+ val read_tyname: theory -> string -> typ
+ val read_const: theory -> string -> term
+ val infer_types_simult: Pretty.pp -> theory -> (indexname -> typ option) ->
+ (indexname -> sort option) -> string list -> bool
+ -> (term list * typ) list -> term list * (indexname * typ) list
+ val infer_types: Pretty.pp -> theory -> (indexname -> typ option) ->
+ (indexname -> sort option) -> string list -> bool
+ -> term list * typ -> term * (indexname * typ) list
+ val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax ->
+ theory * (indexname -> typ option) * (indexname -> sort option) ->
+ string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
+ val read_def_terms:
+ theory * (indexname -> typ option) * (indexname -> sort option) ->
+ string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
+ val simple_read_term: theory -> typ -> string -> term
+ val const_of_class: class -> string
+ val class_of_const: string -> class
+ include SIGN_THEORY
+end
-structure Sign: PRIVATE_SIGN =
+structure Sign: SIGN =
struct
-(** datatype sg **)
-
-(* types sg, data, syn, sg_ref *)
-
-datatype sg =
- Sg of
- {id: string ref,
- stamps: string ref list, (*unique theory indentifier*)
- syn: syn} * (*syntax for parsing and printing*)
- {self: sg_ref, (*mutable self reference*)
- tsig: Type.tsig, (*order-sorted signature of types*)
- consts: (typ * stamp) NameSpace.table, (*type schemes of constants*)
- naming: NameSpace.naming,
- data: data}
-and data =
- Data of
- (Object.kind * (*kind (for authorization)*)
- (Object.T * (*value*)
- ((Object.T -> Object.T) * (*copy method*)
- (Object.T -> Object.T) * (*prepare extend method*)
- (Object.T * Object.T -> Object.T) * (*merge and prepare extend method*)
- (sg -> Object.T -> unit)))) (*print method*)
- Symtab.table
-and syn =
- Syn of
- Syntax.syntax *
- (((sg -> ast list -> ast) * stamp) Symtab.table *
- ((sg -> term list -> term) * stamp) Symtab.table *
- ((sg -> bool -> typ -> term list -> term) * stamp) list Symtab.table *
- ((sg -> ast list -> ast) * stamp) list Symtab.table)
-and sg_ref =
- SgRef of sg ref option;
-
-(* FIXME assign!??? *)
-fun make_sg (id, self, stamps) naming data (syn, tsig, consts) =
- Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
- consts = consts, naming = naming, data = data});
-
-fun rep_sg (Sg (_, args)) = args;
-
-
-(* stamps *)
-
-fun stamps_of (Sg ({stamps, ...}, _)) = stamps;
-val stamp_names_of = rev o map ! o stamps_of;
-fun exists_stamp name = exists (equal name o !) o stamps_of;
-
-fun is_stale (Sg ({id, ...}, {self = SgRef (SOME (ref (Sg ({id = id', ...}, _)))), ...})) =
- id <> id'
- | is_stale _ = false;
-
-fun pretty_sg sg =
- Pretty.str_list "{" "}" (stamp_names_of sg @ (if is_stale sg then ["!"] else []));
-
-val pprint_sg = Pretty.pprint o pretty_sg;
-
-fun pretty_abbrev_sg sg =
- let
- val stamps = map ! (stamps_of sg);
- val abbrev = if length stamps > 5 then "..." :: rev (List.take (stamps, 5)) else rev stamps;
- in Pretty.str_list "{" "}" abbrev end;
-
-val str_of_sg = Pretty.str_of o pretty_abbrev_sg;
-
-
-(* id and self *)
-
-fun check_stale pos sg =
- if is_stale sg then raise TERM ("Stale signature (in " ^ pos ^ "): " ^ str_of_sg sg, [])
- else sg;
-
-fun self_ref (sg as Sg (_, {self, ...})) = (check_stale "Sign.self_ref" sg; self);
-
-fun deref (SgRef (SOME (ref sg))) = sg
- | deref (SgRef NONE) = sys_error "Sign.deref";
-
-fun assign (SgRef (SOME r)) sg = r := sg
- | assign (SgRef NONE) _ = sys_error "Sign.assign";
-
-fun name_of (sg as Sg ({id = ref name, ...}, _)) =
- if name = "" orelse ord name = ord "#" then
- raise TERM ("Nameless signature " ^ str_of_sg sg, [])
- else name;
-
-fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) =
- name = "" orelse ord name = ord "#"
- | is_draft _ = sys_error "Sign.is_draft";
-
-
-(* inclusion and equality *)
-
-local
- (*avoiding polymorphic equality: factor 10 speedup*)
- fun mem_stamp (_: string ref, []) = false
- | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
-
- fun subset_stamp ([], ys) = true
- | subset_stamp (x :: xs, ys) =
- mem_stamp (x, ys) andalso subset_stamp (xs, ys);
-in
- fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
- (check_stale "Sign.eq_sg" sg1; check_stale "Sign.eq_sg" sg2; id1 = id2);
-
- fun subsig (sg1, sg2) =
- eq_sg (sg1, sg2) orelse mem_stamp (hd (stamps_of sg1), stamps_of sg2);
-
- fun subsig_internal (sg1, sg2) =
- eq_sg (sg1, sg2) orelse subset_stamp (stamps_of sg1, stamps_of sg2);
-end;
+type sg = theory;
-fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1);
-
-(*test if same theory names are contained in signatures' stamps,
- i.e. if signatures belong to same theory but not necessarily to the
- same version of it*)
-fun same_sg (sg1, sg2) =
- eq_sg (sg1, sg2) orelse eq_set_string (pairself (map ! o stamps_of) (sg1, sg2));
-
-
-(* data operations *)
-
-fun err_method name kind e = (* FIXME wrap exn msg!? *)
- (warning ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
-
-fun err_inconsistent kinds =
- error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
-
-val empty_data = Data Symtab.empty;
-
-fun merge_data (Data tab1, Data tab2) =
- let
- val data1 = map snd (Symtab.dest tab1);
- val data2 = map snd (Symtab.dest tab2);
- val all_data = data1 @ data2;
- val kinds = gen_distinct Object.eq_kind (map fst all_data);
-
- fun entry data kind =
- (case gen_assoc Object.eq_kind (data, kind) of
- NONE => []
- | SOME x => [(kind, x)]);
-
- fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] =
- (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths))
- | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] =
- (kind, (mrg (e1, e2)
- handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths))
- | merge_entries _ = sys_error "merge_entries";
-
- val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds;
- val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data;
- in
- Data (Symtab.make data_idx)
- handle Symtab.DUPS dups => err_inconsistent dups
- end;
-
-fun prep_ext_data data = merge_data (data, empty_data);
-
-fun copy_data (Data tab) =
- let
- fun cp_data (k, (e, mths as (cp, _, _, _))) =
- (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
- in Data (Symtab.map cp_data tab) end;
-
-
-
-(** build signatures **)
-
-fun create_sg name self stamps naming data sign =
- let
- val id = ref name;
- val stamps' = (case stamps of ref "#" :: ss => ss | ss => ss);
- val _ = conditional (exists (equal name o !) stamps')
- (fn () => error ("Theory already contains a " ^ quote name ^ " component"));
- val sg = make_sg (id, self, id :: stamps') naming data sign;
- in assign self sg; sg end;
-
-fun new_sg f (sg as Sg ({stamps, syn, ...}, {self = _, tsig, consts, naming, data})) =
- let
- val _ = check_stale "Sign.new_sg" sg;
- val self' = SgRef (SOME (ref sg));
- val data' = f data;
- in create_sg "#" self' stamps naming data' (syn, tsig, consts) end;
-
-val prep_ext = new_sg prep_ext_data;
-val copy = new_sg copy_data;
+(** datatype syn **) (* FIXME use Syntax.syntax here (include advanced trfuns there) *)
-fun add_name name (sg as Sg ({stamps, syn, ...}, {self, tsig, consts, naming, data})) =
- let
- val _ = check_stale "Sign.add_name" sg;
- val (self', data') =
- if is_draft sg then (self, data)
- else (SgRef (SOME (ref sg)), prep_ext_data data);
- in create_sg name self' stamps naming data' (syn, tsig, consts) end;
-
-fun map_sg keep f (sg as Sg ({stamps, syn, ...}, {self, tsig, consts, naming, data})) =
- let
- val _ = check_stale "Sign.map_sg" sg;
- val (self', data') =
- if is_draft sg andalso keep then (self, data)
- else (SgRef (SOME (ref sg)), prep_ext_data data);
- val (naming', data', sign') = f (naming, data', (syn, tsig, consts));
- in create_sg "#" self' stamps naming' data' sign' end;
-
-fun map_naming f = map_sg true (fn (naming, data, sign) => (f naming, data, sign));
-fun map_data f = map_sg true (fn (naming, data, sign) => (naming, f data, sign));
-fun map_sign f = map_sg true (fn (naming, data, sign) => (naming, data, f sign));
-fun map_syn f = map_sign (fn (syn, tsig, consts) => (f syn, tsig, consts));
-fun map_tsig f = map_sign (fn (syn, tsig, consts) => (syn, f tsig, consts));
-fun map_consts f = map_sign (fn (syn, tsig, consts) => (syn, tsig, f consts));
-
-
-
-(** signature data **)
-
-(* errors *)
-
-fun of_theory sg = "\nof theory " ^ str_of_sg sg;
-
-fun err_dup_init sg kind =
- error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
-
-fun err_uninit sg kind =
- error ("Tried to access uninitialized " ^ quote kind ^ " data" ^ of_theory sg);
+datatype syn =
+ Syn of
+ Syntax.syntax *
+ (((theory -> ast list -> ast) * stamp) Symtab.table *
+ ((theory -> term list -> term) * stamp) Symtab.table *
+ ((theory -> bool -> typ -> term list -> term) * stamp) list Symtab.table *
+ ((theory -> ast list -> ast) * stamp) list Symtab.table)
-fun err_access sg kind =
- error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
-
-
-(* access data *)
-
-fun data_kinds (Data tab) = Symtab.keys tab;
-
-fun lookup_data sg tab kind =
- let val name = Object.name_of_kind kind in
- (case Symtab.lookup (tab, name) of
- SOME (k, x) =>
- if Object.eq_kind (kind, k) then x
- else err_access sg name
- | NONE => err_uninit sg name)
- end;
-
-fun init_data (kind, (e, cp, ext, mrg, prt)) sg = sg |> map_data (fn Data tab =>
- Data (Symtab.update_new ((Object.name_of_kind kind, (kind, (e, (cp, ext, mrg, prt)))), tab))
- handle Symtab.DUP name => err_dup_init sg name);
-
-fun get_data kind dest (sg as Sg (_, {data = Data tab, ...})) =
- let val x = fst (lookup_data sg tab kind)
- in dest x handle Match => Object.kind_error kind end;
-
-fun put_data kind mk x sg = sg |> map_data (fn Data tab =>
- Data (Symtab.update ((Object.name_of_kind kind,
- (kind, (mk x, snd (lookup_data sg tab kind)))), tab)));
-
-fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
- let val (e, (_, _, _, prt)) = lookup_data sg tab kind in
- prt sg e handle exn =>
- err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn
- end;
-
-fun print_all_data (sg as Sg (_, {data = Data tab, ...})) =
- List.app (fn kind => print_data kind sg) (map (#1 o #2) (Symtab.dest tab));
-
-
-
-(** primitive signatures **)
+fun merge_trfuns
+ (parse_ast_trtab1, parse_trtab1, print_trtab1, print_ast_trtab1)
+ (parse_ast_trtab2, parse_trtab2, print_trtab2, print_ast_trtab2) =
+ (Syntax.merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
+ Syntax.merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
+ Syntax.merge_tr'tabs print_trtab1 print_trtab2,
+ Syntax.merge_tr'tabs print_ast_trtab1 print_ast_trtab2);
val pure_syn =
Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
-val dummy_sg = make_sg (ref "", SgRef NONE, []) NameSpace.default_naming empty_data
- (pure_syn, Type.empty_tsig, NameSpace.empty_table);
-
-val pre_pure =
- create_sg "#" (SgRef (SOME (ref dummy_sg))) [] NameSpace.default_naming empty_data
- (pure_syn, Type.empty_tsig, NameSpace.empty_table);
-
-val PureN = "Pure";
-val CPureN = "CPure";
+(** datatype sign **)
-(** signature content **)
+datatype sign = Sign of
+ {naming: NameSpace.naming, (*common naming conventions*)
+ syn: syn, (*concrete syntax for terms, types, sorts*)
+ tsig: Type.tsig, (*order-sorted signature of types*)
+ consts: (typ * stamp) NameSpace.table}; (*type schemes of term constants*)
+
+fun make_sign (naming, syn, tsig, consts) =
+ Sign {naming = naming, syn = syn, tsig = tsig, consts = consts};
+
+fun err_dup_consts cs =
+ error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
+
+structure SignData = TheoryDataFun
+(struct
+ val name = "Pure/sign";
+ type T = sign;
+ val copy = I;
+ val extend = I;
+
+ val empty =
+ make_sign (NameSpace.default_naming, pure_syn, Type.empty_tsig, NameSpace.empty_table);
+
+ fun merge pp (sign1, sign2) =
+ let
+ val Sign {naming = _, syn = Syn (syntax1, trfuns1), tsig = tsig1,
+ consts = consts1} = sign1;
+ val Sign {naming = _, syn = Syn (syntax2, trfuns2), tsig = tsig2,
+ consts = consts2} = sign2;
+
+ val naming = NameSpace.default_naming;
+ val syntax = Syntax.merge_syntaxes syntax1 syntax2;
+ val trfuns = merge_trfuns trfuns1 trfuns2;
+ val syn = Syn (syntax, trfuns);
+ val tsig = Type.merge_tsigs pp (tsig1, tsig2);
+ val consts = NameSpace.merge_tables eq_snd (consts1, consts2)
+ handle Symtab.DUPS cs => err_dup_consts cs;
+ in make_sign (naming, syn, tsig, consts) end;
+
+ fun print _ _ = ();
+end);
+
+val init = SignData.init;
+
+fun rep_sg thy = SignData.get thy |> (fn Sign args => args);
+
+fun map_sign f = SignData.map (fn Sign {naming, syn, tsig, consts} =>
+ make_sign (f (naming, syn, tsig, consts)));
+
+fun map_naming f = map_sign (fn (naming, syn, tsig, consts) => (f naming, syn, tsig, consts));
+fun map_syn f = map_sign (fn (naming, syn, tsig, consts) => (naming, f syn, tsig, consts));
+fun map_tsig f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, f tsig, consts));
+fun map_consts f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, tsig, f consts));
+
(* naming *)
-fun naming_of (Sg (_, {naming, ...})) = naming;
-
+val naming_of = #naming o rep_sg;
val base_name = NameSpace.base;
val full_name = NameSpace.full o naming_of;
-fun full_name_path sg elems = NameSpace.full (NameSpace.add_path elems (naming_of sg));
+fun full_name_path thy elems = NameSpace.full (NameSpace.add_path elems (naming_of thy));
val declare_name = NameSpace.declare o naming_of;
@@ -485,14 +259,14 @@
fun map_syntax f (Syn (syntax, trfuns)) = Syn (f syntax, trfuns);
-fun make_syntax sg (Syn (syntax, (atrs, trs, tr's, atr's))) =
+fun make_syntax thy (Syn (syntax, (atrs, trs, tr's, atr's))) =
let
- fun apply (c, (f, s)) = (c, (f sg, s));
+ fun apply (c, (f, s)) = (c, (f thy, s));
fun prep tab = map apply (Symtab.dest tab);
fun prep' tab = map apply (Symtab.dest_multi tab);
in syntax |> Syntax.extend_trfuns (prep atrs, prep trs, prep' tr's, prep' atr's) end;
-fun syn_of (sg as Sg ({syn, ...}, _)) = make_syntax sg syn;
+fun syn_of thy = make_syntax thy (#syn (rep_sg thy));
(* advanced translation functions *)
@@ -504,19 +278,10 @@
Syntax.extend_tr'tab tr's print_trtab,
Syntax.extend_tr'tab atr's print_ast_trtab));
-fun merge_trfuns
- (parse_ast_trtab1, parse_trtab1, print_trtab1, print_ast_trtab1)
- (parse_ast_trtab2, parse_trtab2, print_trtab2, print_ast_trtab2) =
- (Syntax.merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
- Syntax.merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
- Syntax.merge_tr'tabs print_trtab1 print_trtab2,
- Syntax.merge_tr'tabs print_ast_trtab1 print_ast_trtab2);
-
(* type signature *)
val tsig_of = #tsig o rep_sg;
-
val classes = Type.classes o tsig_of;
val defaultS = Type.defaultS o tsig_of;
val subsort = Type.subsort o tsig_of;
@@ -524,21 +289,21 @@
val witness_sorts = Type.witness_sorts o tsig_of;
val universal_witness = Type.universal_witness o tsig_of;
val typ_instance = Type.typ_instance o tsig_of;
-fun is_logtype sg c = c mem_string Type.logical_types (tsig_of sg);
+fun is_logtype thy c = c mem_string Type.logical_types (tsig_of thy);
(* consts signature *)
-fun const_type (Sg (_, {consts, ...})) c = Option.map #1 (Symtab.lookup (#2 consts, c));
+fun const_type thy c = Option.map #1 (Symtab.lookup (#2 (#consts (rep_sg thy)), c));
-fun the_const_type sg c =
- (case const_type sg c of SOME T => T
+fun the_const_type thy c =
+ (case const_type thy c of SOME T => T
| NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
-fun declared_tyname sg c =
- is_some (Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of sg))), c));
+fun declared_tyname thy c =
+ is_some (Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of thy))), c));
-fun declared_const sg c = is_some (const_type sg c);
+val declared_const = is_some oo const_type;
@@ -567,16 +332,16 @@
fun get x = if_none (assoc_string (tab, x)) x;
in get end;
-fun typ_mapping f g sg T =
+fun typ_mapping f g thy T =
T |> Term.map_typ
- (mapping add_typ_classes (f sg) T)
- (mapping add_typ_tycons (g sg) T);
+ (mapping add_typ_classes (f thy) T)
+ (mapping add_typ_tycons (g thy) T);
-fun term_mapping f g h sg t =
+fun term_mapping f g h thy t =
t |> Term.map_term
- (mapping add_term_classes (f sg) t)
- (mapping add_term_tycons (g sg) t)
- (mapping add_term_consts (h sg) t);
+ (mapping add_term_classes (f thy) t)
+ (mapping add_term_tycons (g thy) t)
+ (mapping add_term_consts (h thy) t);
in
@@ -592,21 +357,22 @@
(** pretty printing of terms, types etc. **)
-fun pretty_term' syn sg t = Syntax.pretty_term syn (exists_stamp CPureN sg) (extern_term sg t);
-fun pretty_term sg t = pretty_term' (syn_of sg) sg t;
-fun pretty_typ sg T = Syntax.pretty_typ (syn_of sg) (extern_typ sg T);
-fun pretty_sort sg S = Syntax.pretty_sort (syn_of sg) (extern_sort sg S);
+fun pretty_term' syn thy t =
+ Syntax.pretty_term syn (Context.exists_name Context.CPureN thy) (extern_term thy t);
+fun pretty_term thy t = pretty_term' (syn_of thy) thy t;
+fun pretty_typ thy T = Syntax.pretty_typ (syn_of thy) (extern_typ thy T);
+fun pretty_sort thy S = Syntax.pretty_sort (syn_of thy) (extern_sort thy S);
-fun pretty_classrel sg cs = Pretty.block (List.concat
- (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort sg o single) cs)));
+fun pretty_classrel thy cs = Pretty.block (List.concat
+ (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort thy o single) cs)));
-fun pretty_arity sg (a, Ss, S) =
+fun pretty_arity thy (a, Ss, S) =
let
- val a' = extern_type sg a;
+ val a' = extern_type thy a;
val dom =
if null Ss then []
- else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
- in Pretty.block ([Pretty.str (a' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S]) end;
+ else [Pretty.list "(" ")" (map (pretty_sort thy) Ss), Pretty.brk 1];
+ in Pretty.block ([Pretty.str (a' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort thy S]) end;
val string_of_term = Pretty.string_of oo pretty_term;
val string_of_typ = Pretty.string_of oo pretty_typ;
@@ -617,8 +383,8 @@
val pprint_term = (Pretty.pprint o Pretty.quote) oo pretty_term;
val pprint_typ = (Pretty.pprint o Pretty.quote) oo pretty_typ;
-fun pp sg = Pretty.pp (pretty_term sg, pretty_typ sg, pretty_sort sg,
- pretty_classrel sg, pretty_arity sg);
+fun pp thy = Pretty.pp (pretty_term thy, pretty_typ thy, pretty_sort thy,
+ pretty_classrel thy, pretty_arity thy);
@@ -626,7 +392,7 @@
(* certify wrt. type signature *)
-fun certify cert = cert o tsig_of o check_stale "Sign.certify";
+fun certify cert = cert o tsig_of o Context.check_thy "Sign.certify";
val certify_class = certify Type.cert_class;
val certify_sort = certify Type.cert_sort;
@@ -669,11 +435,11 @@
in
-fun certify_term pp sg tm =
+fun certify_term pp thy tm =
let
- val _ = check_stale "Sign.certify_term" sg;
+ val _ = Context.check_thy "Sign.certify_term" thy;
- val tm' = map_term_types (Type.cert_typ (tsig_of sg)) tm;
+ val tm' = map_term_types (certify_typ thy) tm;
val tm' = if tm = tm' then tm else tm'; (*avoid copying of already normal term*)
fun err msg = raise TYPE (msg, [], [tm']);
@@ -683,10 +449,10 @@
fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
| check_atoms (Abs (_, _, t)) = check_atoms t
| check_atoms (Const (a, T)) =
- (case const_type sg a of
+ (case const_type thy a of
NONE => err ("Undeclared constant " ^ show_const a T)
| SOME U =>
- if typ_instance sg (T, U) then ()
+ if typ_instance thy (T, U) then ()
else err ("Illegal type for constant " ^ show_const a T))
| check_atoms (Var ((x, i), _)) =
if i < 0 then err ("Malformed variable: " ^ quote x) else ()
@@ -700,60 +466,61 @@
-(** read and certify entities **) (*exception ERROR/TYPE*)
+(** read and certify entities **) (*exception ERROR*)
(* sorts *)
-fun read_sort' syn sg str =
+fun read_sort' syn thy str =
let
- val _ = check_stale "Sign.read_sort'" sg;
- val S = intern_sort sg (Syntax.read_sort syn str);
- in Type.cert_sort (tsig_of sg) S handle TYPE (msg, _, _) => error msg end;
+ val _ = Context.check_thy "Sign.read_sort'" thy;
+ val S = intern_sort thy (Syntax.read_sort syn str);
+ in certify_sort thy S handle TYPE (msg, _, _) => error msg end;
-fun read_sort sg str = read_sort' (syn_of sg) sg str;
+fun read_sort thy str = read_sort' (syn_of thy) thy str;
(* types *)
local
-fun gen_read_typ' cert syn (sg, def_sort) str =
+fun gen_read_typ' cert syn (thy, def_sort) str =
let
- val _ = check_stale "Sign.gen_read_typ'" sg;
- val get_sort = TypeInfer.get_sort (tsig_of sg) def_sort (intern_sort sg);
- val T = intern_tycons sg (Syntax.read_typ syn get_sort (intern_sort sg) str);
- in cert (tsig_of sg) T handle TYPE (msg, _, _) => error msg end
+ val _ = Context.check_thy "Sign.gen_read_typ'" thy;
+ val get_sort = TypeInfer.get_sort (tsig_of thy) def_sort (intern_sort thy);
+ val T = intern_tycons thy (Syntax.read_typ syn get_sort (intern_sort thy) str);
+ in cert thy T handle TYPE (msg, _, _) => error msg end
handle ERROR => error ("The error(s) above occurred in type " ^ quote str);
-fun gen_read_typ cert (sg, def_sort) str = gen_read_typ' cert (syn_of sg) (sg, def_sort) str;
+fun gen_read_typ cert (thy, def_sort) str =
+ gen_read_typ' cert (syn_of thy) (thy, def_sort) str;
in
-fun no_def_sort sg = (sg, K NONE);
+fun no_def_sort thy = (thy: theory, K NONE);
-val read_typ' = gen_read_typ' Type.cert_typ;
-val read_typ_syntax' = gen_read_typ' Type.cert_typ_syntax;
-val read_typ_abbrev' = gen_read_typ' Type.cert_typ_abbrev;
-val read_typ = gen_read_typ Type.cert_typ;
-val read_typ_syntax = gen_read_typ Type.cert_typ_syntax;
-val read_typ_abbrev = gen_read_typ Type.cert_typ_abbrev;
+val read_typ' = gen_read_typ' certify_typ;
+val read_typ_syntax' = gen_read_typ' certify_typ_syntax;
+val read_typ_abbrev' = gen_read_typ' certify_typ_abbrev;
+val read_typ = gen_read_typ certify_typ;
+val read_typ_syntax = gen_read_typ certify_typ_syntax;
+val read_typ_abbrev = gen_read_typ certify_typ_abbrev;
end;
(* type and constant names *)
-fun read_tyname sg raw_c =
- let val c = intern_type sg raw_c in
- (case Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of sg))), c) of
+fun read_tyname thy raw_c =
+ let val c = intern_type thy raw_c in
+ (case Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of thy))), c) of
SOME (Type.LogicalType n, _) => Type (c, replicate n dummyT)
| _ => error ("Undeclared type constructor: " ^ quote c))
end;
-fun read_const sg raw_c =
+fun read_const thy raw_c =
let
- val c = intern_const sg raw_c;
- val _ = the_const_type sg c handle TYPE (msg, _, _) => error msg;
+ val c = intern_const thy raw_c;
+ val _ = the_const_type thy c handle TYPE (msg, _, _) => error msg;
in Const (c, dummyT) end;
@@ -770,17 +537,15 @@
typs: expected types
*)
-fun infer_types_simult pp sg def_type def_sort used freeze args =
+fun infer_types_simult pp thy def_type def_sort used freeze args =
let
- val tsig = tsig_of sg;
-
val termss = foldr multiply [[]] (map fst args);
val typs =
- map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
+ map (fn (_, T) => certify_typ thy T handle TYPE (msg, _, _) => error msg) args;
- fun infer ts = OK (TypeInfer.infer_types (Syntax.pp_show_brackets pp) tsig
- (const_type sg) def_type def_sort (intern_const sg) (intern_tycons sg)
- (intern_sort sg) used freeze typs ts)
+ fun infer ts = OK (TypeInfer.infer_types (Syntax.pp_show_brackets pp) (tsig_of thy)
+ (const_type thy) def_type def_sort (intern_const thy) (intern_tycons thy)
+ (intern_sort thy) used freeze typs ts)
handle TYPE (msg, _, _) => Error msg;
val err_results = map infer termss;
@@ -806,26 +571,25 @@
cat_lines (map (Pretty.string_of_term pp) (List.concat (map fst results)))))
end;
-
-fun infer_types pp sg def_type def_sort used freeze tsT =
- apfst hd (infer_types_simult pp sg def_type def_sort used freeze [tsT]);
+fun infer_types pp thy def_type def_sort used freeze tsT =
+ apfst hd (infer_types_simult pp thy def_type def_sort used freeze [tsT]);
-(** read_def_terms -- read terms and infer types **)
+(** read_def_terms -- read terms and infer types **) (*exception ERROR*)
-fun read_def_terms' pp is_logtype syn (sg, types, sorts) used freeze sTs =
+fun read_def_terms' pp is_logtype syn (thy, types, sorts) used freeze sTs =
let
fun read (s, T) =
- let val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg
+ let val T' = certify_typ thy T handle TYPE (msg, _, _) => error msg
in (Syntax.read is_logtype syn T' s, T') end;
- in infer_types_simult pp sg types sorts used freeze (map read sTs) end;
+ in infer_types_simult pp thy types sorts used freeze (map read sTs) end;
-fun read_def_terms (sg, types, sorts) =
- read_def_terms' (pp sg) (is_logtype sg) (syn_of sg) (sg, types, sorts);
+fun read_def_terms (thy, types, sorts) =
+ read_def_terms' (pp thy) (is_logtype thy) (syn_of thy) (thy, types, sorts);
-fun simple_read_term sg T s =
- let val ([t], _) = read_def_terms (sg, K NONE, K NONE) [] true [(s, T)]
+fun simple_read_term thy T s =
+ let val ([t], _) = read_def_terms (thy, K NONE, K NONE) [] true [(s, T)]
in t end
handle ERROR => error ("The error(s) above occurred for term " ^ s);
@@ -835,8 +599,8 @@
(* add default sort *)
-fun gen_add_defsort prep_sort s sg =
- sg |> map_tsig (Type.set_defsort (prep_sort sg s));
+fun gen_add_defsort prep_sort s thy =
+ thy |> map_tsig (Type.set_defsort (prep_sort thy s));
val add_defsort = gen_add_defsort read_sort;
val add_defsort_i = gen_add_defsort certify_sort;
@@ -844,34 +608,41 @@
(* add type constructors *)
-fun add_types types sg = sg |> map_sign (fn (syn, tsig, consts) =>
+fun add_types types thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
let
val syn' = map_syntax (Syntax.extend_type_gram types) syn;
val decls = map (fn (a, n, mx) => (Syntax.type_name a mx, n)) types;
- val tsig' = Type.add_types (naming_of sg) decls tsig;
- in (syn', tsig', consts) end);
+ val tsig' = Type.add_types naming decls tsig;
+ in (naming, syn', tsig', consts) end);
+
+fun add_typedecls decls thy =
+ let
+ fun type_of (a, vs, mx) =
+ if null (duplicates vs) then (a, length vs, mx)
+ else error ("Duplicate parameters in type declaration: " ^ quote a);
+ in add_types (map type_of decls) thy end;
(* add nonterminals *)
-fun add_nonterminals bnames sg = sg |> map_sign (fn (syn, tsig, consts) =>
+fun add_nonterminals ns thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
let
- val syn' = map_syntax (Syntax.extend_consts bnames) syn;
- val tsig' = Type.add_nonterminals (naming_of sg) bnames tsig;
- in (syn', tsig', consts) end);
+ val syn' = map_syntax (Syntax.extend_consts ns) syn;
+ val tsig' = Type.add_nonterminals naming ns tsig;
+ in (naming, syn', tsig', consts) end);
(* add type abbreviations *)
-fun gen_add_tyabbr prep_typ (a, vs, rhs, mx) sg =
- sg |> map_sign (fn (syn, tsig, consts) =>
+fun gen_add_tyabbr prep_typ (a, vs, rhs, mx) thy =
+ thy |> map_sign (fn (naming, syn, tsig, consts) =>
let
val syn' = map_syntax (Syntax.extend_type_gram [(a, length vs, mx)]) syn;
val a' = Syntax.type_name a mx;
- val abbr = (a', vs, prep_typ sg rhs) handle ERROR =>
- error ("in type abbreviation " ^ quote a');
- val tsig' = Type.add_abbrevs (naming_of sg) [abbr] tsig;
- in (syn', tsig', consts) end);
+ val abbr = (a', vs, prep_typ thy rhs)
+ handle ERROR => error ("in type abbreviation " ^ quote a');
+ val tsig' = Type.add_abbrevs naming [abbr] tsig;
+ in (naming, syn', tsig', consts) end);
val add_tyabbrs = fold (gen_add_tyabbr (read_typ_syntax o no_def_sort));
val add_tyabbrs_i = fold (gen_add_tyabbr certify_typ_syntax);
@@ -879,11 +650,11 @@
(* add type arities *)
-fun gen_add_arities int_type prep_sort arities sg = sg |> map_tsig (fn tsig =>
+fun gen_add_arities int_type prep_sort arities thy = thy |> map_tsig (fn tsig =>
let
- fun prep_arity (a, Ss, S) = (int_type sg a, map (prep_sort sg) Ss, prep_sort sg S)
+ fun prep_arity (a, Ss, S) = (int_type thy a, map (prep_sort thy) Ss, prep_sort thy S)
handle ERROR => error ("in arity for type " ^ quote a);
- val tsig' = Type.add_arities (pp sg) (map prep_arity arities) tsig;
+ val tsig' = Type.add_arities (pp thy) (map prep_arity arities) tsig;
in tsig' end);
val add_arities = gen_add_arities intern_type read_sort;
@@ -892,37 +663,39 @@
(* modify syntax *)
-fun gen_syntax change_gram prep_typ (prmode, args) sg =
+fun gen_syntax change_gram prep_typ prmode args thy =
let
- fun prep (c, T, mx) = (c, prep_typ sg T, mx) handle ERROR =>
- error ("in syntax declaration " ^ quote (Syntax.const_name c mx));
- in sg |> map_syn (map_syntax (change_gram (is_logtype sg) prmode (map prep args))) end;
+ fun prep (c, T, mx) = (c, prep_typ thy T, mx)
+ handle ERROR => error ("in syntax declaration " ^ quote (Syntax.const_name c mx));
+ in thy |> map_syn (map_syntax (change_gram (is_logtype thy) prmode (map prep args))) end;
fun gen_add_syntax x = gen_syntax Syntax.extend_const_gram x;
val add_modesyntax = gen_add_syntax (read_typ_syntax o no_def_sort);
val add_modesyntax_i = gen_add_syntax certify_typ_syntax;
-val add_syntax = curry add_modesyntax Syntax.default_mode;
-val add_syntax_i = curry add_modesyntax_i Syntax.default_mode;
+val add_syntax = add_modesyntax Syntax.default_mode;
+val add_syntax_i = add_modesyntax_i Syntax.default_mode;
val del_modesyntax = gen_syntax Syntax.remove_const_gram (read_typ_syntax o no_def_sort);
val del_modesyntax_i = gen_syntax Syntax.remove_const_gram certify_typ_syntax;
(* add constants *)
-fun err_dup_consts cs =
- error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
-
-fun gen_add_consts prep_typ raw_args sg =
+fun gen_add_consts prep_typ raw_args thy =
let
- val prepT = compress_type o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ sg;
+ val prepT = compress_type o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ thy;
fun prep (c, T, mx) = ((c, prepT T, mx) handle TYPE (msg, _, _) => error msg)
handle ERROR => error ("in declaration of constant " ^ quote (Syntax.const_name c mx));
val args = map prep raw_args;
val decls = args |> map (fn (c, T, mx) => (Syntax.const_name c mx, (T, stamp ())));
- fun extend_consts consts = NameSpace.extend_table (naming_of sg) (consts, decls)
+
+ fun extend_consts consts = NameSpace.extend_table (naming_of thy) (consts, decls)
handle Symtab.DUPS cs => err_dup_consts cs;
- in sg |> map_consts extend_consts |> add_syntax_i args end;
+ in
+ thy
+ |> map_consts extend_consts
+ |> add_syntax_i args
+ end;
val add_consts = gen_add_consts (read_typ o no_def_sort);
val add_consts_i = gen_add_consts certify_typ;
@@ -936,13 +709,13 @@
fun class_of_const c = unsuffix classN c
handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
-fun gen_add_class int_class (bclass, raw_classes) sg =
- sg |> map_sign (fn (syn, tsig, consts) =>
+fun gen_add_class int_class (bclass, raw_classes) thy =
+ thy |> map_sign (fn (naming, syn, tsig, consts) =>
let
- val classes = map (int_class sg) raw_classes;
+ val classes = map (int_class thy) raw_classes;
val syn' = map_syntax (Syntax.extend_consts [bclass]) syn;
- val tsig' = Type.add_classes (pp sg) (naming_of sg) [(bclass, classes)] tsig;
- in (syn', tsig', consts) end)
+ val tsig' = Type.add_classes (pp thy) naming [(bclass, classes)] tsig;
+ in (naming, syn', tsig', consts) end)
|> add_consts_i [(const_of_class bclass, a_itselfT --> propT, Syntax.NoSyn)];
val add_classes = fold (gen_add_class intern_class);
@@ -951,10 +724,10 @@
(* add to classrel *)
-fun gen_add_classrel int_class raw_pairs sg = sg |> map_tsig (fn tsig =>
+fun gen_add_classrel int_class raw_pairs thy = thy |> map_tsig (fn tsig =>
let
- val pairs = map (pairself (int_class sg)) raw_pairs;
- val tsig' = Type.add_classrel (pp sg) pairs tsig;
+ val pairs = map (pairself (int_class thy)) raw_pairs;
+ val tsig' = Type.add_classrel (pp thy) pairs tsig;
in tsig' end);
val add_classrel = gen_add_classrel intern_class;
@@ -967,13 +740,13 @@
fun mk trs = map Syntax.mk_trfun trs;
-fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) sg = sg |> map_syn (fn syn =>
+fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) thy = thy |> map_syn (fn syn =>
let val syn' = syn |> ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's)
- in make_syntax sg syn'; syn' end);
+ in make_syntax thy syn'; syn' end);
-fun gen_add_trfunsT ext tr's sg = sg |> map_syn (fn syn =>
+fun gen_add_trfunsT ext tr's thy = thy |> map_syn (fn syn =>
let val syn' = syn |> ext ([], [], mk tr's, [])
- in make_syntax sg syn'; syn' end);
+ in make_syntax thy syn'; syn' end);
in
@@ -985,13 +758,14 @@
end;
val add_tokentrfuns = map_syn o map_syntax o Syntax.extend_tokentrfuns;
+fun add_mode_tokentrfuns m = add_tokentrfuns o map (fn (s, f) => (m, s, f));
(* add translation rules *)
-fun add_trrules args sg = sg |> map_syn (fn syn =>
- let val rules = map (Syntax.map_trrule (apfst (intern_type sg))) args
- in map_syntax (Syntax.extend_trrules (is_logtype sg) (make_syntax sg syn) rules) syn end);
+fun add_trrules args thy = thy |> map_syn (fn syn =>
+ let val rules = map (Syntax.map_trrule (apfst (intern_type thy))) args
+ in map_syntax (Syntax.extend_trrules (is_logtype thy) (make_syntax thy syn) rules) syn end);
val add_trrules_i = map_syn o map_syntax o Syntax.extend_trrules_i;
@@ -1005,89 +779,21 @@
val set_policy = map_naming o NameSpace.set_policy;
val restore_naming = map_naming o K o naming_of;
+val parent_path = add_path "..";
+val root_path = add_path "/";
+val absolute_path = add_path "//";
+
+fun local_path thy = thy |> root_path |> add_path (Context.theory_name thy);
+
(* hide names *)
-fun hide_classes b xs sg = sg |> map_tsig (Type.hide_classes b (map (intern_class sg) xs));
+fun hide_classes b xs thy = thy |> map_tsig (Type.hide_classes b (map (intern_class thy) xs));
val hide_classes_i = map_tsig oo Type.hide_classes;
-fun hide_types b xs sg = sg |> map_tsig (Type.hide_types b (map (intern_type sg) xs));
+fun hide_types b xs thy = thy |> map_tsig (Type.hide_types b (map (intern_type thy) xs));
val hide_types_i = map_tsig oo Type.hide_types;
-fun hide_consts b xs sg =
- sg |> map_consts (apfst (fold (NameSpace.hide b o intern_const sg) xs));
+fun hide_consts b xs thy =
+ thy |> map_consts (apfst (fold (NameSpace.hide b o intern_const thy) xs));
val hide_consts_i = (map_consts o apfst) oo (fold o NameSpace.hide);
-
-
-(** merge signatures **)
-
-fun merge_stamps stamps1 stamps2 =
- let val stamps = merge_lists' stamps1 stamps2 in
- (case duplicates (map ! stamps) of
- [] => stamps
- | dups => error ("Attempt to merge different versions of theories " ^ commas_quote dups))
- end;
-
-
-(* trivial merge *)
-
-fun merge_refs (sgr1 as SgRef (SOME (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
- sgr2 as SgRef (SOME (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
- if subsig (sg2, sg1) then sgr1
- else if subsig (sg1, sg2) then sgr2
- else (merge_stamps s1 s2; (*check for different versions*)
- error ("Attempt to do non-trivial merge of signature\n" ^
- str_of_sg sg1 ^ " and " ^ str_of_sg sg2))
- | merge_refs _ = sys_error "Sign.merge_refs";
-
-val merge = deref o merge_refs o pairself self_ref;
-
-
-(* non-trivial merge *) (*exception TERM/ERROR*)
-
-local
-
-fun nontriv_merge (sg1, sg2) =
- if subsig_internal (sg2, sg1) then sg1
- else if subsig_internal (sg1, sg2) then sg2
- else
- if exists_stamp CPureN sg1 <> exists_stamp CPureN sg2
- then error "Cannot merge Pure and CPure developments"
- else
- let
- val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
- {self = _, tsig = tsig1, consts = consts1, naming = _, data = data1}) = sg1;
- val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
- {self = _, tsig = tsig2, consts = consts2, naming = _, data = data2}) = sg2;
-
- val stamps = merge_stamps stamps1 stamps2;
- val syntax = Syntax.merge_syntaxes syntax1 syntax2;
- val trfuns = merge_trfuns trfuns1 trfuns2;
- val syn = Syn (syntax, trfuns);
- val consts = NameSpace.merge_tables eq_snd (consts1, consts2)
- handle Symtab.DUPS cs => err_dup_consts cs;
- val naming = NameSpace.default_naming;
- val data = merge_data (data1, data2);
-
- val pre_sg = make_sg (ref "", SgRef (SOME (ref dummy_sg)), ref "#" :: stamps1)
- naming data (syn, tsig1, consts);
- val tsig = Type.merge_tsigs (pp pre_sg) (tsig1, tsig2);
-
- val self = SgRef (SOME (ref dummy_sg));
- val sg = make_sg (ref "", self, stamps) naming data (syn, tsig, consts);
- in assign self sg; syn_of sg; sg end;
-
-in
-
-fun prep_ext_merge sgs =
- if null sgs then
- error "Merge: no parent theories"
- else if exists is_draft sgs then
- error "Attempt to merge draft theories"
- else
- Library.foldl nontriv_merge (hd sgs, tl sgs)
- |> prep_ext
- |> add_path "/";
-
end;
-
-end;