diff --git a/src/Pure/General/name_space.ML b/src/Pure/General/name_space.ML --- a/src/Pure/General/name_space.ML +++ b/src/Pure/General/name_space.ML @@ -1,633 +1,644 @@ (* Title: Pure/General/name_space.ML Author: Markus Wenzel, TU Muenchen Generic name spaces with declared and hidden entries; no support for absolute addressing. *) type xstring = string; (*external names*) signature NAME_SPACE = sig type T val empty: string -> T val kind_of: T -> string val markup: T -> string -> Markup.T val markup_def: T -> string -> Markup.T val the_entry: T -> string -> - {concealed: bool, group: serial option, theory_name: string, pos: Position.T, serial: serial} + {concealed: bool, + group: serial option, + theory_long_name: string, + pos: Position.T, + serial: serial} + val the_entry_theory_name: T -> string -> string val entry_ord: T -> string ord val is_concealed: T -> string -> bool val intern: T -> xstring -> string val names_long: bool Config.T val names_short: bool Config.T val names_unique: bool Config.T val extern: Proof.context -> T -> string -> xstring val extern_ord: Proof.context -> T -> string ord val extern_shortest: Proof.context -> T -> string -> xstring val markup_extern: Proof.context -> T -> string -> Markup.T * xstring val pretty: Proof.context -> T -> string -> Pretty.T val completion: Context.generic -> T -> (string -> bool) -> xstring * Position.T -> Completion.T val merge: T * T -> T type naming val get_scopes: naming -> Binding.scope list val get_scope: naming -> Binding.scope option val new_scope: naming -> Binding.scope * naming val restricted: bool -> Position.T -> naming -> naming val private_scope: Binding.scope -> naming -> naming val private: Position.T -> naming -> naming val qualified_scope: Binding.scope -> naming -> naming val qualified: Position.T -> naming -> naming val concealed: naming -> naming val get_group: naming -> serial option val set_group: serial option -> naming -> naming - val set_theory_name: string -> naming -> naming + val set_theory_long_name: string -> naming -> naming val new_group: naming -> naming val reset_group: naming -> naming val add_path: string -> naming -> naming val root_path: naming -> naming val parent_path: naming -> naming val mandatory_path: string -> naming -> naming val qualified_path: bool -> binding -> naming -> naming val global_naming: naming val local_naming: naming val transform_naming: naming -> naming -> naming val transform_binding: naming -> binding -> binding val full_name: naming -> binding -> string val base_name: binding -> string val hide: bool -> string -> T -> T val alias: naming -> binding -> string -> T -> T val naming_of: Context.generic -> naming val map_naming: (naming -> naming) -> Context.generic -> Context.generic val declared: T -> string -> bool val declare: Context.generic -> bool -> binding -> T -> string * T type 'a table val change_base: bool -> 'a table -> 'a table val change_ignore: 'a table -> 'a table val space_of_table: 'a table -> T val check_reports: Context.generic -> 'a table -> xstring * Position.T list -> (string * Position.report list) * 'a val check: Context.generic -> 'a table -> xstring * Position.T -> string * 'a val defined: 'a table -> string -> bool val lookup: 'a table -> string -> 'a option val lookup_key: 'a table -> string -> (string * 'a) option val get: 'a table -> string -> 'a val define: Context.generic -> bool -> binding * 'a -> 'a table -> string * 'a table val alias_table: naming -> binding -> string -> 'a table -> 'a table val hide_table: bool -> string -> 'a table -> 'a table val del_table: string -> 'a table -> 'a table val map_table_entry: string -> ('a -> 'a) -> 'a table -> 'a table val fold_table: (string * 'a -> 'b -> 'b) -> 'a table -> 'b -> 'b val dest_table: 'a table -> (string * 'a) list val empty_table: string -> 'a table val merge_tables: 'a table * 'a table -> 'a table val join_tables: (string -> 'a * 'a -> 'a) (*exception Change_Table.SAME*) -> 'a table * 'a table -> 'a table val extern_entries: bool -> Proof.context -> T -> (string * 'a) list -> ((string * xstring) * 'a) list val markup_entries: bool -> Proof.context -> T -> (string * 'a) list -> ((Markup.T * xstring) * 'a) list val extern_table: bool -> Proof.context -> 'a table -> ((string * xstring) * 'a) list val markup_table: bool -> Proof.context -> 'a table -> ((Markup.T * xstring) * 'a) list end; structure Name_Space: NAME_SPACE = struct (** name spaces **) (* datatype entry *) type entry = {concealed: bool, group: serial option, - theory_name: string, + theory_long_name: string, pos: Position.T, serial: serial}; fun entry_markup def kind (name, {pos, serial, ...}: entry) = Markup.properties (Position.entity_properties_of def serial pos) (Markup.entity kind name); fun print_entry_ref kind (name, entry) = quote (Markup.markup (entry_markup false kind (name, entry)) name); fun err_dup kind entry1 entry2 pos = error ("Duplicate " ^ plain_words kind ^ " declaration " ^ print_entry_ref kind entry1 ^ " vs. " ^ print_entry_ref kind entry2 ^ Position.here pos); (* internal names *) type internals = (string list * string list) Change_Table.T; (*xname -> visible, hidden*) fun map_internals f xname : internals -> internals = Change_Table.map_default (xname, ([], [])) f; val del_name = map_internals o apfst o remove (op =); fun del_name_extra name = map_internals (apfst (fn [] => [] | x :: xs => x :: remove (op =) name xs)); val add_name = map_internals o apfst o update (op =); fun hide_name name = map_internals (apsnd (update (op =) name)) name; (* external accesses *) type accesses = (xstring list * xstring list); (*input / output fragments*) type entries = (accesses * entry) Change_Table.T; (*name -> accesses, entry*) (* datatype T *) datatype T = Name_Space of {kind: string, internals: internals, entries: entries}; fun make_name_space (kind, internals, entries) = Name_Space {kind = kind, internals = internals, entries = entries}; fun map_name_space f (Name_Space {kind = kind, internals = internals, entries = entries}) = make_name_space (f (kind, internals, entries)); fun change_base_space begin = map_name_space (fn (kind, internals, entries) => (kind, Change_Table.change_base begin internals, Change_Table.change_base begin entries)); val change_ignore_space = map_name_space (fn (kind, internals, entries) => (kind, Change_Table.change_ignore internals, Change_Table.change_ignore entries)); fun empty kind = make_name_space (kind, Change_Table.empty, Change_Table.empty); fun kind_of (Name_Space {kind, ...}) = kind; fun gen_markup def (Name_Space {kind, entries, ...}) name = (case Change_Table.lookup entries name of NONE => Markup.intensify | SOME (_, entry) => entry_markup def kind (name, entry)); val markup = gen_markup false; val markup_def = gen_markup true; fun undefined (space as Name_Space {kind, entries, ...}) bad = let val (prfx, sfx) = (case Long_Name.dest_hidden bad of SOME name => if Change_Table.defined entries name then ("Inaccessible", Markup.markup (markup space name) (quote name)) else ("Undefined", quote name) | NONE => ("Undefined", quote bad)); in prfx ^ " " ^ plain_words kind ^ ": " ^ sfx end; fun the_entry (space as Name_Space {entries, ...}) name = (case Change_Table.lookup entries name of NONE => error (undefined space name) | SOME (_, entry) => entry); +fun the_entry_theory_name space name = + Long_Name.base_name (#theory_long_name (the_entry space name)); + fun entry_ord space = int_ord o apply2 (#serial o the_entry space); fun is_concealed space name = #concealed (the_entry space name) handle ERROR _ => false; (* intern *) fun intern' (Name_Space {internals, ...}) xname = (case the_default ([], []) (Change_Table.lookup internals xname) of ([name], _) => (name, true) | (name :: _, _) => (name, false) | ([], []) => (Long_Name.hidden xname, true) | ([], name' :: _) => (Long_Name.hidden name', true)); val intern = #1 oo intern'; fun get_accesses (Name_Space {entries, ...}) name = (case Change_Table.lookup entries name of NONE => ([], []) | SOME (accesses, _) => accesses); fun is_valid_access (Name_Space {internals, ...}) name xname = (case Change_Table.lookup internals xname of SOME (name' :: _, _) => name = name' | _ => false); (* extern *) val names_long = Config.declare_option_bool ("names_long", \<^here>); val names_short = Config.declare_option_bool ("names_short", \<^here>); val names_unique = Config.declare_option_bool ("names_unique", \<^here>); fun extern ctxt space name = let val names_long = Config.get ctxt names_long; val names_short = Config.get ctxt names_short; val names_unique = Config.get ctxt names_unique; fun valid require_unique xname = let val (name', is_unique) = intern' space xname in name = name' andalso (not require_unique orelse is_unique) end; fun ext [] = if valid false name then name else Long_Name.hidden name | ext (nm :: nms) = if valid names_unique nm then nm else ext nms; in if names_long then name else if names_short then Long_Name.base_name name else ext (#2 (get_accesses space name)) end; fun extern_ord ctxt space = string_ord o apply2 (extern ctxt space); fun extern_shortest ctxt = extern (ctxt |> Config.put names_long false |> Config.put names_short false |> Config.put names_unique false); fun markup_extern ctxt space name = (markup space name, extern ctxt space name); fun pretty ctxt space name = Pretty.mark_str (markup_extern ctxt space name); (* completion *) fun completion context space pred (xname, pos) = Completion.make (xname, pos) (fn completed => let fun result_ord ((pri1, (xname1, (_, name1))), (pri2, (xname2, (_, name2)))) = (case int_ord (pri2, pri1) of EQUAL => (case bool_ord (apply2 (is_some o Long_Name.dest_local) (name2, name1)) of EQUAL => (case int_ord (apply2 Long_Name.qualification (xname1, xname2)) of EQUAL => string_ord (xname1, xname2) | ord => ord) | ord => ord) | ord => ord); val Name_Space {kind, internals, ...} = space; val ext = extern_shortest (Context.proof_of context) space; val full = Name.clean xname = ""; fun complete xname' name = if (completed xname' orelse exists completed (Long_Name.explode xname')) andalso not (is_concealed space name) andalso pred name then let val xname'' = ext name; val pri = (if xname' = xname'' then 1 else 0) + (if completed xname' then 1 else 0); in if xname' <> xname'' andalso full then I else cons (pri, (xname', (kind, name))) end else I; in Change_Table.fold (fn (xname', (name :: _, _)) => complete xname' name | _ => I) internals [] |> sort_distinct result_ord |> map #2 end); (* merge *) fun merge (Name_Space {kind = kind1, internals = internals1, entries = entries1}, Name_Space {kind = kind2, internals = internals2, entries = entries2}) = let val kind' = if kind1 = kind2 then kind1 else error ("Attempt to merge different kinds of name spaces " ^ quote kind1 ^ " vs. " ^ quote kind2); val internals' = (internals1, internals2) |> Change_Table.join (K (fn ((names1, names1'), (names2, names2')) => if pointer_eq (names1, names2) andalso pointer_eq (names1', names2') then raise Change_Table.SAME else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2')))); val entries' = (entries1, entries2) |> Change_Table.join (fn name => fn ((_, entry1), (_, entry2)) => if #serial entry1 = #serial entry2 then raise Change_Table.SAME else err_dup kind' (name, entry1) (name, entry2) Position.none); in make_name_space (kind', internals', entries') end; (** naming context **) (* datatype naming *) datatype naming = Naming of {scopes: Binding.scope list, restricted: (bool * Binding.scope) option, concealed: bool, group: serial option, - theory_name: string, + theory_long_name: string, path: (string * bool) list}; -fun make_naming (scopes, restricted, concealed, group, theory_name, path) = +fun make_naming (scopes, restricted, concealed, group, theory_long_name, path) = Naming {scopes = scopes, restricted = restricted, concealed = concealed, - group = group, theory_name = theory_name, path = path}; + group = group, theory_long_name = theory_long_name, path = path}; -fun map_naming f (Naming {scopes, restricted, concealed, group, theory_name, path}) = - make_naming (f (scopes, restricted, concealed, group, theory_name, path)); +fun map_naming f (Naming {scopes, restricted, concealed, group, theory_long_name, path}) = + make_naming (f (scopes, restricted, concealed, group, theory_long_name, path)); (* scope and access restriction *) fun get_scopes (Naming {scopes, ...}) = scopes; val get_scope = try hd o get_scopes; fun new_scope naming = let val scope = Binding.new_scope (); val naming' = - naming |> map_naming (fn (scopes, restricted, concealed, group, theory_name, path) => - (scope :: scopes, restricted, concealed, group, theory_name, path)); + naming |> map_naming (fn (scopes, restricted, concealed, group, theory_long_name, path) => + (scope :: scopes, restricted, concealed, group, theory_long_name, path)); in (scope, naming') end; fun restricted_scope strict scope = - map_naming (fn (scopes, _, concealed, group, theory_name, path) => - (scopes, SOME (strict, scope), concealed, group, theory_name, path)); + map_naming (fn (scopes, _, concealed, group, theory_long_name, path) => + (scopes, SOME (strict, scope), concealed, group, theory_long_name, path)); fun restricted strict pos naming = (case get_scope naming of SOME scope => restricted_scope strict scope naming | NONE => error ("Missing local scope -- cannot restrict name space accesses" ^ Position.here pos)); val private_scope = restricted_scope true; val private = restricted true; val qualified_scope = restricted_scope false; val qualified = restricted false; -val concealed = map_naming (fn (scopes, restricted, _, group, theory_name, path) => - (scopes, restricted, true, group, theory_name, path)); +val concealed = map_naming (fn (scopes, restricted, _, group, theory_long_name, path) => + (scopes, restricted, true, group, theory_long_name, path)); (* additional structural info *) -fun set_theory_name theory_name = map_naming (fn (scopes, restricted, concealed, group, _, path) => - (scopes, restricted, concealed, group, theory_name, path)); +fun set_theory_long_name theory_long_name = + map_naming (fn (scopes, restricted, concealed, group, _, path) => + (scopes, restricted, concealed, group, theory_long_name, path)); fun get_group (Naming {group, ...}) = group; -fun set_group group = map_naming (fn (scopes, restricted, concealed, _, theory_name, path) => - (scopes, restricted, concealed, group, theory_name, path)); +fun set_group group = + map_naming (fn (scopes, restricted, concealed, _, theory_long_name, path) => + (scopes, restricted, concealed, group, theory_long_name, path)); fun new_group naming = set_group (SOME (serial ())) naming; val reset_group = set_group NONE; (* name entry path *) fun get_path (Naming {path, ...}) = path; -fun map_path f = map_naming (fn (scopes, restricted, concealed, group, theory_name, path) => - (scopes, restricted, concealed, group, theory_name, f path)); +fun map_path f = + map_naming (fn (scopes, restricted, concealed, group, theory_long_name, path) => + (scopes, restricted, concealed, group, theory_long_name, f path)); fun add_path elems = map_path (fn path => path @ [(elems, false)]); val root_path = map_path (fn _ => []); val parent_path = map_path (perhaps (try (#1 o split_last))); fun mandatory_path elems = map_path (fn path => path @ [(elems, true)]); fun qualified_path mandatory binding = map_path (fn path => path @ Binding.path_of (Binding.qualify_name mandatory binding "")); val global_naming = make_naming ([], NONE, false, NONE, "", []); val local_naming = global_naming |> add_path Long_Name.localN; (* transform *) fun transform_naming (Naming {restricted = restricted', concealed = concealed', ...}) = (case restricted' of SOME (strict, scope) => restricted_scope strict scope | NONE => I) #> concealed' ? concealed; fun transform_binding (Naming {restricted, concealed, ...}) = Binding.restricted restricted #> concealed ? Binding.concealed; (* full name *) fun name_spec naming binding = Binding.name_spec (get_scopes naming) (get_path naming) (transform_binding naming binding); fun full_name naming = name_spec naming #> #spec #> map #1 #> Long_Name.implode; val base_name = full_name global_naming #> Long_Name.base_name; (* accesses *) fun mandatory xs = map_filter (fn (x, true) => SOME x | _ => NONE) xs; fun mandatory_prefixes xs = mandatory xs :: mandatory_prefixes1 xs and mandatory_prefixes1 [] = [] | mandatory_prefixes1 ((x, true) :: xs) = map (cons x) (mandatory_prefixes1 xs) | mandatory_prefixes1 ((x, false) :: xs) = map (cons x) (mandatory_prefixes xs); fun mandatory_suffixes xs = map rev (mandatory_prefixes (rev xs)); fun make_accesses naming binding = (case name_spec naming binding of {restriction = SOME true, ...} => ([], []) | {restriction, spec, ...} => let val restrict = is_some restriction ? filter (fn [_] => false | _ => true); val sfxs = restrict (mandatory_suffixes spec); val pfxs = restrict (mandatory_prefixes spec); in apply2 (map Long_Name.implode) (sfxs @ pfxs, sfxs) end); (* hide *) fun hide fully name space = space |> map_name_space (fn (kind, internals, entries) => let val _ = the_entry space name; val (accs, accs') = get_accesses space name; val xnames = filter (is_valid_access space name) accs; val internals' = internals |> hide_name name |> fold (del_name name) (if fully then xnames else inter (op =) [Long_Name.base_name name] xnames) |> fold (del_name_extra name) accs'; in (kind, internals', entries) end); (* alias *) fun alias naming binding name space = space |> map_name_space (fn (kind, internals, entries) => let val _ = the_entry space name; val (more_accs, more_accs') = make_accesses naming binding; val internals' = internals |> fold (add_name name) more_accs; val entries' = entries |> Change_Table.map_entry name (apfst (fn (accs, accs') => (fold_rev (update op =) more_accs accs, fold_rev (update op =) more_accs' accs'))) in (kind, internals', entries') end); (** context naming **) structure Data_Args = struct type T = naming; val empty = global_naming; fun extend _ = global_naming; fun merge _ = global_naming; fun init _ = local_naming; end; structure Global_Naming = Theory_Data(Data_Args); structure Local_Naming = Proof_Data(Data_Args); fun naming_of (Context.Theory thy) = Global_Naming.get thy | naming_of (Context.Proof ctxt) = Local_Naming.get ctxt; fun map_naming f (Context.Theory thy) = Context.Theory (Global_Naming.map f thy) | map_naming f (Context.Proof ctxt) = Context.Proof (Local_Naming.map f ctxt); (** entry definition **) (* declaration *) fun declared (Name_Space {entries, ...}) = Change_Table.defined entries; fun declare context strict binding space = let val naming = naming_of context; - val Naming {group, theory_name, ...} = naming; + val Naming {group, theory_long_name, ...} = naming; val {concealed, spec, ...} = name_spec naming binding; val accesses = make_accesses naming binding; val name = Long_Name.implode (map fst spec); val _ = name = "" andalso error (Binding.bad binding); val (proper_pos, pos) = Position.default (Binding.pos_of binding); val entry = {concealed = concealed, group = group, - theory_name = theory_name, + theory_long_name = theory_long_name, pos = pos, serial = serial ()}; val space' = space |> map_name_space (fn (kind, internals, entries) => let val internals' = internals |> fold (add_name name) (#1 accesses); val entries' = (if strict then Change_Table.update_new else Change_Table.update) (name, (accesses, entry)) entries handle Change_Table.DUP dup => err_dup kind (dup, #2 (the (Change_Table.lookup entries dup))) (name, entry) (#pos entry); in (kind, internals', entries') end); val _ = if proper_pos andalso Context_Position.is_reported_generic context pos then Position.report pos (entry_markup true (kind_of space) (name, entry)) else (); in (name, space') end; (* definition in symbol table *) datatype 'a table = Table of T * 'a Change_Table.T; fun change_base begin (Table (space, tab)) = Table (change_base_space begin space, Change_Table.change_base begin tab); fun change_ignore (Table (space, tab)) = Table (change_ignore_space space, Change_Table.change_ignore tab); fun space_of_table (Table (space, _)) = space; fun check_reports context (Table (space, tab)) (xname, ps) = let val name = intern space xname in (case Change_Table.lookup tab name of SOME x => let val reports = filter (Context_Position.is_reported_generic context) ps |> map (fn pos => (pos, markup space name)); in ((name, reports), x) end | NONE => error (undefined space name ^ Position.here_list ps ^ Completion.markup_report (map (fn pos => completion context space (K true) (xname, pos)) ps))) end; fun check context table (xname, pos) = let val ((name, reports), x) = check_reports context table (xname, [pos]); val _ = Position.reports reports; in (name, x) end; fun defined (Table (_, tab)) name = Change_Table.defined tab name; fun lookup (Table (_, tab)) name = Change_Table.lookup tab name; fun lookup_key (Table (_, tab)) name = Change_Table.lookup_key tab name; fun get table name = (case lookup_key table name of SOME (_, x) => x | NONE => error (undefined (space_of_table table) name)); fun define context strict (binding, x) (Table (space, tab)) = let val (name, space') = declare context strict binding space; val tab' = Change_Table.update (name, x) tab; in (name, Table (space', tab')) end; (* derived table operations *) fun alias_table naming binding name (Table (space, tab)) = Table (alias naming binding name space, tab); fun hide_table fully name (Table (space, tab)) = Table (hide fully name space, tab); fun del_table name (Table (space, tab)) = let val space' = hide true name space handle ERROR _ => space; val tab' = Change_Table.delete_safe name tab; in Table (space', tab') end; fun map_table_entry name f (Table (space, tab)) = Table (space, Change_Table.map_entry name f tab); fun fold_table f (Table (_, tab)) = Change_Table.fold f tab; fun dest_table (Table (_, tab)) = Change_Table.dest tab; fun empty_table kind = Table (empty kind, Change_Table.empty); fun merge_tables (Table (space1, tab1), Table (space2, tab2)) = Table (merge (space1, space2), Change_Table.merge (K true) (tab1, tab2)); fun join_tables f (Table (space1, tab1), Table (space2, tab2)) = Table (merge (space1, space2), Change_Table.join f (tab1, tab2)); (* present table content *) fun extern_entries verbose ctxt space entries = fold (fn (name, x) => (verbose orelse not (is_concealed space name)) ? cons ((name, extern ctxt space name), x)) entries [] |> sort_by (#2 o #1); fun markup_entries verbose ctxt space entries = extern_entries verbose ctxt space entries |> map (fn ((name, xname), x) => ((markup space name, xname), x)); fun extern_table verbose ctxt (Table (space, tab)) = extern_entries verbose ctxt space (Change_Table.dest tab); fun markup_table verbose ctxt (Table (space, tab)) = markup_entries verbose ctxt space (Change_Table.dest tab); end; diff --git a/src/Pure/sign.ML b/src/Pure/sign.ML --- a/src/Pure/sign.ML +++ b/src/Pure/sign.ML @@ -1,543 +1,544 @@ (* Title: Pure/sign.ML Author: Lawrence C Paulson and Markus Wenzel Logical signature content: naming conventions, concrete syntax, type signature, polymorphic constants. *) signature SIGN = sig val change_begin: theory -> theory val change_end: theory -> theory val change_end_local: Proof.context -> Proof.context val change_check: theory -> theory val syn_of: theory -> Syntax.syntax val tsig_of: theory -> Type.tsig val classes_of: theory -> Sorts.algebra val all_classes: theory -> class list val super_classes: theory -> class -> class list val minimize_sort: theory -> sort -> sort val complete_sort: theory -> sort -> sort val set_defsort: sort -> theory -> theory val defaultS: theory -> sort val subsort: theory -> sort * sort -> bool val of_sort: theory -> typ * sort -> bool val inter_sort: theory -> sort * sort -> sort val witness_sorts: theory -> (typ * sort) list -> sort list -> (typ * sort) list val logical_types: theory -> string list val typ_instance: theory -> typ * typ -> bool val typ_equiv: theory -> typ * typ -> bool val typ_match: theory -> typ * typ -> Type.tyenv -> Type.tyenv val typ_unify: theory -> typ * typ -> Type.tyenv * int -> Type.tyenv * int val consts_of: theory -> Consts.T val the_const_constraint: theory -> string -> typ 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 naming_of: theory -> Name_Space.naming val map_naming: (Name_Space.naming -> Name_Space.naming) -> theory -> theory val restore_naming: theory -> theory -> theory val inherit_naming: theory -> Proof.context -> Context.generic val full_name: theory -> binding -> string val full_name_path: theory -> string -> binding -> string val full_bname: theory -> bstring -> string val full_bname_path: theory -> string -> bstring -> string val const_monomorphic: theory -> string -> bool val const_typargs: theory -> string * typ -> typ list val const_instance: theory -> string * typ list -> typ val mk_const: theory -> string * typ list -> term val class_space: theory -> Name_Space.T val type_space: theory -> Name_Space.T val const_space: theory -> Name_Space.T val intern_class: theory -> xstring -> string val intern_type: theory -> xstring -> string val intern_const: theory -> xstring -> string val type_alias: binding -> string -> theory -> theory val const_alias: binding -> string -> theory -> theory val arity_number: theory -> string -> int val arity_sorts: theory -> string -> sort -> sort list val certify_class: theory -> class -> class val certify_sort: theory -> sort -> sort val certify_typ: theory -> typ -> typ val certify_typ_mode: Type.mode -> theory -> typ -> typ val certify': bool -> Context.generic -> bool -> Consts.T -> theory -> term -> term * typ * int val certify_term: theory -> term -> term * typ * int val cert_term: theory -> term -> term val cert_prop: theory -> term -> term val no_frees: Proof.context -> term -> term val no_vars: Proof.context -> term -> term val add_type: Proof.context -> binding * int * mixfix -> theory -> theory val add_types_global: (binding * int * mixfix) list -> theory -> theory val add_nonterminals: Proof.context -> binding list -> theory -> theory val add_nonterminals_global: binding list -> theory -> theory val add_type_abbrev: Proof.context -> binding * string list * typ -> theory -> theory val add_syntax: Syntax.mode -> (string * typ * mixfix) list -> theory -> theory val add_syntax_cmd: Syntax.mode -> (string * string * mixfix) list -> theory -> theory val del_syntax: Syntax.mode -> (string * typ * mixfix) list -> theory -> theory val del_syntax_cmd: Syntax.mode -> (string * string * mixfix) list -> theory -> theory val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> theory -> theory val notation: bool -> Syntax.mode -> (term * mixfix) list -> theory -> theory val declare_const: Proof.context -> (binding * typ) * mixfix -> theory -> term * theory val declare_const_global: (binding * typ) * mixfix -> theory -> term * theory val add_consts: (binding * typ * mixfix) list -> theory -> theory val add_consts_cmd: (binding * string * mixfix) list -> theory -> theory val add_abbrev: string -> binding * term -> theory -> (term * term) * theory val revert_abbrev: string -> string -> theory -> theory val add_const_constraint: string * typ option -> theory -> theory val primitive_class: binding * class list -> theory -> theory val primitive_classrel: class * class -> theory -> theory val primitive_arity: arity -> theory -> theory val parse_ast_translation: (string * (Proof.context -> Ast.ast list -> Ast.ast)) list -> theory -> theory val parse_translation: (string * (Proof.context -> term list -> term)) list -> theory -> theory val print_translation: (string * (Proof.context -> term list -> term)) list -> theory -> theory val typed_print_translation: (string * (Proof.context -> typ -> term list -> term)) list -> theory -> theory val print_ast_translation: (string * (Proof.context -> Ast.ast list -> Ast.ast)) list -> theory -> theory val add_trrules: Ast.ast Syntax.trrule list -> theory -> theory val del_trrules: Ast.ast Syntax.trrule list -> theory -> theory val get_scope: theory -> Binding.scope option val new_scope: theory -> Binding.scope * theory val new_group: theory -> theory val reset_group: theory -> theory val add_path: string -> theory -> theory val root_path: theory -> theory val parent_path: theory -> theory val mandatory_path: string -> theory -> theory val qualified_path: bool -> binding -> theory -> theory val local_path: theory -> theory val theory_naming: theory -> theory val private_scope: Binding.scope -> theory -> theory val private: Position.T -> theory -> theory val qualified_scope: Binding.scope -> theory -> theory val qualified: Position.T -> theory -> theory val concealed: theory -> theory val hide_class: bool -> string -> theory -> theory val hide_type: bool -> string -> theory -> theory val hide_const: bool -> string -> theory -> theory end structure Sign: SIGN = struct (** datatype sign **) datatype sign = Sign of {syn: Syntax.syntax, (*concrete syntax for terms, types, sorts*) tsig: Type.tsig, (*order-sorted signature of types*) consts: Consts.T}; (*polymorphic constants*) fun make_sign (syn, tsig, consts) = Sign {syn = syn, tsig = tsig, consts = consts}; structure Data = Theory_Data' ( type T = sign; fun extend (Sign {syn, tsig, consts, ...}) = make_sign (syn, tsig, consts); val empty = make_sign (Syntax.empty_syntax, Type.empty_tsig, Consts.empty); fun merge old_thys (sign1, sign2) = let val Sign {syn = syn1, tsig = tsig1, consts = consts1} = sign1; val Sign {syn = syn2, tsig = tsig2, consts = consts2} = sign2; val syn = Syntax.merge_syntax (syn1, syn2); val tsig = Type.merge_tsig (Context.Theory (fst old_thys)) (tsig1, tsig2); val consts = Consts.merge (consts1, consts2); in make_sign (syn, tsig, consts) end; ); fun rep_sg thy = Data.get thy |> (fn Sign args => args); fun map_sign f = Data.map (fn Sign {syn, tsig, consts} => make_sign (f (syn, tsig, consts))); 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)); (* linear change discipline *) fun change_base begin = map_sign (fn (syn, tsig, consts) => (syn, Type.change_base begin tsig, Consts.change_base begin consts)); val change_begin = change_base true; val change_end = change_base false; fun change_end_local ctxt = Context.raw_transfer (change_end (Proof_Context.theory_of ctxt)) ctxt; fun change_check thy = if can change_end thy then raise Fail "Unfinished linear change of theory content" else thy; (* syntax *) val syn_of = #syn o rep_sg; (* type signature *) val tsig_of = #tsig o rep_sg; val classes_of = #2 o #classes o Type.rep_tsig o tsig_of; val all_classes = Sorts.all_classes o classes_of; val super_classes = Sorts.super_classes o classes_of; val minimize_sort = Sorts.minimize_sort o classes_of; val complete_sort = Sorts.complete_sort o classes_of; val set_defsort = map_tsig o Type.set_defsort; val defaultS = Type.defaultS o tsig_of; val subsort = Type.subsort o tsig_of; val of_sort = Type.of_sort o tsig_of; val inter_sort = Type.inter_sort o tsig_of; val witness_sorts = Type.witness_sorts o tsig_of; val logical_types = Type.logical_types o tsig_of; val typ_instance = Type.typ_instance o tsig_of; fun typ_equiv thy (T, U) = typ_instance thy (T, U) andalso typ_instance thy (U, T); val typ_match = Type.typ_match o tsig_of; val typ_unify = Type.unify o tsig_of; (* polymorphic constants *) val consts_of = #consts o rep_sg; val the_const_constraint = Consts.the_constraint o consts_of; val the_const_type = #2 oo (Consts.the_const o consts_of); val const_type = try o the_const_type; val const_monomorphic = Consts.is_monomorphic o consts_of; val const_typargs = Consts.typargs o consts_of; val const_instance = Consts.instance o consts_of; fun mk_const thy (c, Ts) = Const (c, const_instance thy (c, Ts)); fun declared_tyname ctxt c = can (Type.the_decl (tsig_of ctxt)) (c, Position.none); val declared_const = can o the_const_constraint; (* naming *) val naming_of = Name_Space.naming_of o Context.Theory; val map_naming = Context.theory_map o Name_Space.map_naming; val restore_naming = map_naming o K o naming_of; fun inherit_naming thy = Name_Space.map_naming (K (naming_of thy)) o Context.Proof; val full_name = Name_Space.full_name o naming_of; fun full_name_path thy path = Name_Space.full_name (Name_Space.add_path path (naming_of thy)); fun full_bname thy = Name_Space.full_name (naming_of thy) o Binding.name; fun full_bname_path thy path = full_name_path thy path o Binding.name; (** name spaces **) val class_space = Type.class_space o tsig_of; val type_space = Type.type_space o tsig_of; val const_space = Consts.space_of o consts_of; val intern_class = Name_Space.intern o class_space; val intern_type = Name_Space.intern o type_space; val intern_const = Name_Space.intern o const_space; fun type_alias b c thy = map_tsig (Type.type_alias (naming_of thy) b c) thy; fun const_alias b c thy = map_consts (Consts.alias (naming_of thy) b c) thy; (** certify entities **) (*exception TYPE*) (* certify wrt. type signature *) val arity_number = Type.arity_number o tsig_of; fun arity_sorts thy = Type.arity_sorts (Context.Theory thy) (tsig_of thy); val certify_class = Type.cert_class o tsig_of; val certify_sort = Type.cert_sort o tsig_of; val certify_typ = Type.cert_typ o tsig_of; fun certify_typ_mode mode = Type.cert_typ_mode mode o tsig_of; (* certify term/prop *) local fun type_check context tm = let fun err_appl bs t T u U = let val xs = map Free bs; (*we do not rename here*) val t' = subst_bounds (xs, t); val u' = subst_bounds (xs, u); val msg = Type.appl_error (Syntax.init_pretty context) t' T u' U; in raise TYPE (msg, [T, U], [t', u']) end; fun typ_of (_, Const (_, T)) = T | typ_of (_, Free (_, T)) = T | typ_of (_, Var (_, T)) = T | typ_of (bs, Bound i) = snd (nth bs i handle General.Subscript => raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i])) | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body) | typ_of (bs, t $ u) = let val T = typ_of (bs, t) and U = typ_of (bs, u) in (case T of Type ("fun", [T1, T2]) => if T1 = U then T2 else err_appl bs t T u U | _ => err_appl bs t T u U) end; in typ_of ([], tm) end; fun err msg = raise TYPE (msg, [], []); fun check_vars (t $ u) = (check_vars t; check_vars u) | check_vars (Abs (_, _, t)) = check_vars t | check_vars (Free (x, _)) = if Long_Name.is_qualified x then err ("Malformed variable: " ^ quote x) else () | check_vars (Var (xi as (_, i), _)) = if i < 0 then err ("Malformed variable: " ^ quote (Term.string_of_vname xi)) else () | check_vars _ = (); in fun certify' prop context do_expand consts thy tm = let val _ = check_vars tm; val tm' = Term.map_types (certify_typ thy) tm; val T = type_check context tm'; val _ = if prop andalso T <> propT then err "Term not of type prop" else (); val tm'' = tm' |> Consts.certify context (tsig_of thy) do_expand consts |> Soft_Type_System.global_purge thy; in (if tm = tm'' then tm else tm'', T, Term.maxidx_of_term tm'') end; fun certify_term thy = certify' false (Context.Theory thy) true (consts_of thy) thy; fun cert_term_abbrev thy = #1 o certify' false (Context.Theory thy) false (consts_of thy) thy; val cert_term = #1 oo certify_term; fun cert_prop thy = #1 o certify' true (Context.Theory thy) true (consts_of thy) thy; end; (* specifications *) fun no_variables kind add addT mk mkT ctxt tm = (case (add tm [], addT tm []) of ([], []) => tm | (frees, tfrees) => error (Pretty.string_of (Pretty.block (Pretty.str ("Illegal " ^ kind ^ " variable(s) in term:") :: Pretty.brk 1 :: Pretty.commas (map (Syntax.pretty_term ctxt o mk) frees @ map (Syntax.pretty_typ ctxt o mkT) tfrees))))); val no_frees = no_variables "free" Term.add_frees Term.add_tfrees Free TFree; val no_vars = no_variables "schematic" Term.add_vars Term.add_tvars Var TVar; (** signature extension functions **) (*exception ERROR/TYPE*) (* add type constructors *) fun add_type ctxt (b, n, mx) thy = thy |> map_sign (fn (syn, tsig, consts) => let val type_syntax = (Lexicon.mark_type (full_name thy b), Mixfix.make_type n, mx); val syn' = Syntax.update_type_gram true Syntax.mode_default [type_syntax] syn; val tsig' = Type.add_type (inherit_naming thy ctxt) (b, n) tsig; in (syn', tsig', consts) end); fun add_types_global types thy = fold (add_type (Syntax.init_pretty_global thy)) types thy; (* add nonterminals *) fun add_nonterminals ctxt ns thy = thy |> map_sign (fn (syn, tsig, consts) => (syn, fold (Type.add_nonterminal (inherit_naming thy ctxt)) ns tsig, consts)); fun add_nonterminals_global ns thy = add_nonterminals (Syntax.init_pretty_global thy) ns thy; (* add type abbreviations *) fun add_type_abbrev ctxt abbr thy = thy |> map_sign (fn (syn, tsig, consts) => (syn, Type.add_abbrev (inherit_naming thy ctxt) abbr tsig, consts)); (* modify syntax *) fun gen_syntax change_gram parse_typ mode args thy = let val ctxt = Type.set_mode Type.mode_syntax (Proof_Context.init_global thy); fun prep (c, T, mx) = (c, certify_typ_mode Type.mode_syntax thy (parse_typ ctxt T), mx) handle ERROR msg => cat_error msg ("in syntax declaration " ^ quote c); in thy |> map_syn (change_gram (logical_types thy) mode (map prep args)) end; fun gen_add_syntax x = gen_syntax (Syntax.update_const_gram true) x; val add_syntax = gen_add_syntax (K I); val add_syntax_cmd = gen_add_syntax Syntax.read_typ; val del_syntax = gen_syntax (Syntax.update_const_gram false) (K I); val del_syntax_cmd = gen_syntax (Syntax.update_const_gram false) Syntax.read_typ; fun type_notation add mode args = let fun type_syntax (Type (c, args), mx) = SOME (Lexicon.mark_type c, Mixfix.make_type (length args), mx) | type_syntax _ = NONE; in map_syn (Syntax.update_type_gram add mode (map_filter type_syntax args)) end; fun notation add mode args thy = let fun const_syntax (Const (c, _), mx) = (case try (Consts.type_scheme (consts_of thy)) c of SOME T => SOME (Lexicon.mark_const c, T, mx) | NONE => NONE) | const_syntax _ = NONE; in gen_syntax (Syntax.update_const_gram add) (K I) mode (map_filter const_syntax args) thy end; (* add constants *) local fun gen_add_consts prep_typ ctxt raw_args thy = let val prepT = Type.no_tvars o Term.no_dummyT o certify_typ thy o prep_typ ctxt; fun prep (b, raw_T, mx) = let val c = full_name thy b; val T = (prepT raw_T handle TYPE (msg, _, _) => error msg) handle ERROR msg => cat_error msg ("in declaration of constant " ^ Binding.print b); val T' = Logic.varifyT_global T; in ((b, T'), (Lexicon.mark_const c, T', mx), Const (c, T)) end; val args = map prep raw_args; in thy |> map_consts (fold (Consts.declare (inherit_naming thy ctxt) o #1) args) |> add_syntax Syntax.mode_default (map #2 args) |> pair (map #3 args) end; in fun add_consts args thy = #2 (gen_add_consts (K I) (Proof_Context.init_global thy) args thy); fun add_consts_cmd args thy = #2 (gen_add_consts Syntax.read_typ (Proof_Context.init_global thy) args thy); fun declare_const ctxt ((b, T), mx) = yield_singleton (gen_add_consts (K I) ctxt) (b, T, mx); fun declare_const_global arg thy = declare_const (Proof_Context.init_global thy) arg thy; end; (* abbreviations *) fun add_abbrev mode (b, raw_t) thy = (* FIXME proper ctxt (?) *) let val ctxt = Syntax.init_pretty_global thy; val prep_tm = no_frees ctxt o Term.no_dummy_patterns o cert_term_abbrev thy; val t = (prep_tm raw_t handle TYPE (msg, _, _) => error msg | TERM (msg, _) => error msg) handle ERROR msg => cat_error msg ("in constant abbreviation " ^ Binding.print b); val (res, consts') = consts_of thy |> Consts.abbreviate (inherit_naming thy ctxt) (tsig_of thy) mode (b, t); in (res, thy |> map_consts (K consts')) end; fun revert_abbrev mode c = map_consts (Consts.revert_abbrev mode c); (* add constraints *) fun add_const_constraint (c, opt_T) thy = let fun prepT raw_T = let val T = Logic.varifyT_global (Type.no_tvars (Term.no_dummyT (certify_typ thy raw_T))) in cert_term thy (Const (c, T)); T end handle TYPE (msg, _, _) => error msg; in thy |> map_consts (Consts.constrain (c, Option.map prepT opt_T)) end; (* primitive classes and arities *) fun primitive_class (bclass, classes) thy = thy |> map_sign (fn (syn, tsig, consts) => let val tsig' = Type.add_class (Context.Theory thy) (bclass, classes) tsig; in (syn, tsig', consts) end) |> add_consts [(Binding.map_name Logic.const_of_class bclass, Term.a_itselfT --> propT, NoSyn)]; fun primitive_classrel arg thy = thy |> map_tsig (Type.add_classrel (Context.Theory thy) arg); fun primitive_arity arg thy = thy |> map_tsig (Type.add_arity (Context.Theory thy) arg); (* add translation functions *) local fun mk trs = map Syntax_Ext.mk_trfun trs; in fun parse_ast_translation atrs = map_syn (Syntax.update_trfuns (mk atrs, [], [], [])); fun parse_translation trs = map_syn (Syntax.update_trfuns ([], mk trs, [], [])); fun print_translation tr's = map_syn (Syntax.update_trfuns ([], [], mk (map (apsnd Syntax_Trans.non_typed_tr') tr's), [])); fun typed_print_translation tr's = map_syn (Syntax.update_trfuns ([], [], mk tr's, [])); fun print_ast_translation atr's = map_syn (Syntax.update_trfuns ([], [], [], mk atr's)); end; (* translation rules *) val add_trrules = map_syn o Syntax.update_trrules; val del_trrules = map_syn o Syntax.remove_trrules; (* naming *) val get_scope = Name_Space.get_scope o naming_of; fun new_scope thy = let val (scope, naming') = Name_Space.new_scope (naming_of thy); val thy' = map_naming (K naming') thy; in (scope, thy') end; val new_group = map_naming Name_Space.new_group; val reset_group = map_naming Name_Space.reset_group; val add_path = map_naming o Name_Space.add_path; val root_path = map_naming Name_Space.root_path; val parent_path = map_naming Name_Space.parent_path; val mandatory_path = map_naming o Name_Space.mandatory_path; val qualified_path = map_naming oo Name_Space.qualified_path; fun local_path thy = thy |> root_path |> add_path (Context.theory_name thy); -fun theory_naming thy = thy |> map_naming (Name_Space.set_theory_name (Context.theory_name thy)); +fun theory_naming thy = thy + |> map_naming (Name_Space.set_theory_long_name (Context.theory_long_name thy)); val private_scope = map_naming o Name_Space.private_scope; val private = map_naming o Name_Space.private; val qualified_scope = map_naming o Name_Space.qualified_scope; val qualified = map_naming o Name_Space.qualified; val concealed = map_naming Name_Space.concealed; (* hide names *) val hide_class = map_tsig oo Type.hide_class; val hide_type = map_tsig oo Type.hide_type; val hide_const = map_consts oo Consts.hide; end; diff --git a/src/Tools/Code/code_symbol.ML b/src/Tools/Code/code_symbol.ML --- a/src/Tools/Code/code_symbol.ML +++ b/src/Tools/Code/code_symbol.ML @@ -1,232 +1,232 @@ (* Title: Tools/Code/code_symbol.ML Author: Florian Haftmann, TU Muenchen Data related to symbols in programs: constants, type constructors, classes, class relations, class instances, modules. *) signature BASIC_CODE_SYMBOL = sig datatype ('a, 'b, 'c, 'd, 'e, 'f) attr = Constant of 'a | Type_Constructor of 'b | Type_Class of 'c | Class_Relation of 'd | Class_Instance of 'e | Module of 'f end signature CODE_SYMBOL = sig include BASIC_CODE_SYMBOL type T = (string, string, class, class * class, string * class, string) attr structure Table: TABLE structure Graph: GRAPH val default_base: T -> string val default_prefix: Proof.context -> T -> string val quote: Proof.context -> T -> string val marker: T -> string val value: T val is_value: T -> bool val map_attr: ('a -> 'g) -> ('b -> 'h) -> ('c -> 'i) -> ('d -> 'j) -> ('e -> 'k) -> ('f -> 'l) -> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr val maps_attr: ('a -> 'g list) -> ('b -> 'h list) -> ('c -> 'i list) -> ('d -> 'j list) -> ('e -> 'k list) -> ('f -> 'l list) -> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr list val maps_attr': ('a -> ('m * 'g) list) -> ('b -> ('m * 'h) list) -> ('c -> ('m * 'i) list) -> ('d -> ('m * 'j) list) -> ('e -> ('m * 'k) list) -> ('f -> ('m * 'l) list) -> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('m * ('g, 'h, 'i, 'j, 'k, 'l) attr) list type ('a, 'b, 'c, 'd, 'e, 'f) data val empty_data: ('a, 'b, 'c, 'd, 'e, 'f) data val merge_data: ('a, 'b, 'c, 'd, 'e, 'f) data * ('a, 'b, 'c, 'd, 'e, 'f) data -> ('a, 'b, 'c, 'd, 'e, 'f) data val set_data: (string * 'a option, string * 'b option, string * 'c option, (class * class) * 'd option, (string * class) * 'e option, string * 'f option) attr -> ('a, 'b, 'c, 'd, 'e, 'f) data -> ('a, 'b, 'c, 'd, 'e, 'f) data val lookup_constant_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'a option val lookup_type_constructor_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'b option val lookup_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class -> 'c option val lookup_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class * class -> 'd option val lookup_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string * class -> 'e option val lookup_module_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'f option val lookup: ('a, 'a, 'a, 'a, 'a, 'a) data -> T -> 'a option val symbols_of: ('a, 'b, 'c, 'd, 'e, 'f) data -> T list val dest_module_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'f) list end; structure Code_Symbol : CODE_SYMBOL = struct (* data for symbols *) datatype ('a, 'b, 'c, 'd, 'e, 'f) attr = Constant of 'a | Type_Constructor of 'b | Type_Class of 'c | Class_Relation of 'd | Class_Instance of 'e | Module of 'f; type T = (string, string, class, string * class, class * class, string) attr; fun symbol_ord (Constant c1, Constant c2) = fast_string_ord (c1, c2) | symbol_ord (Constant _, _) = GREATER | symbol_ord (_, Constant _) = LESS | symbol_ord (Type_Constructor tyco1, Type_Constructor tyco2) = fast_string_ord (tyco1, tyco2) | symbol_ord (Type_Constructor _, _) = GREATER | symbol_ord (_, Type_Constructor _) = LESS | symbol_ord (Type_Class class1, Type_Class class2) = fast_string_ord (class1, class2) | symbol_ord (Type_Class _, _) = GREATER | symbol_ord (_, Type_Class _) = LESS | symbol_ord (Class_Relation classrel1, Class_Relation classrel2) = prod_ord fast_string_ord fast_string_ord (classrel1, classrel2) | symbol_ord (Class_Relation _, _) = GREATER | symbol_ord (_, Class_Relation _) = LESS | symbol_ord (Class_Instance inst1, Class_Instance inst2) = prod_ord fast_string_ord fast_string_ord (inst1, inst2) | symbol_ord (Class_Instance _, _) = GREATER | symbol_ord (_, Class_Instance _) = LESS | symbol_ord (Module name1, Module name2) = fast_string_ord (name1, name2); structure Table = Table(type key = T val ord = symbol_ord); structure Graph = Graph(type key = T val ord = symbol_ord); local val base = Name.desymbolize NONE o Long_Name.base_name; fun base_rel (x, y) = base y ^ "_" ^ base x; in fun default_base (Constant "") = "value" | default_base (Constant c) = base c | default_base (Type_Constructor tyco) = base tyco | default_base (Type_Class class) = base class | default_base (Class_Relation classrel) = base_rel classrel | default_base (Class_Instance inst) = base_rel inst; end; local - fun thyname_of_type thy = #theory_name o Name_Space.the_entry (Sign.type_space thy); - fun thyname_of_class thy = #theory_name o Name_Space.the_entry (Sign.class_space thy); + val thyname_of_type = Name_Space.the_entry_theory_name o Sign.type_space; + val thyname_of_class = Name_Space.the_entry_theory_name o Sign.class_space; fun thyname_of_instance thy inst = case Thm.thynames_of_arity thy inst of [] => error ("No such instance: " ^ quote (fst inst ^ " :: " ^ snd inst)) | thyname :: _ => thyname; fun thyname_of_const thy c = case Axclass.class_of_param thy c of SOME class => thyname_of_class thy class | NONE => (case Code.get_type_of_constr_or_abstr thy c of SOME (tyco, _) => thyname_of_type thy tyco - | NONE => #theory_name (Name_Space.the_entry (Sign.const_space thy) c)); + | NONE => Name_Space.the_entry_theory_name (Sign.const_space thy) c); fun prefix thy (Constant "") = "Code" | prefix thy (Constant c) = thyname_of_const thy c | prefix thy (Type_Constructor tyco) = thyname_of_type thy tyco | prefix thy (Type_Class class) = thyname_of_class thy class | prefix thy (Class_Relation (class, _)) = thyname_of_class thy class | prefix thy (Class_Instance inst) = thyname_of_instance thy inst; in fun default_prefix ctxt = prefix (Proof_Context.theory_of ctxt); end; val value = Constant ""; fun is_value (Constant "") = true | is_value _ = false; fun quote ctxt (Constant c) = Library.quote (Code.string_of_const (Proof_Context.theory_of ctxt) c) | quote ctxt (Type_Constructor tyco) = "type " ^ Library.quote (Proof_Context.markup_type ctxt tyco) | quote ctxt (Type_Class class) = "class " ^ Library.quote (Proof_Context.markup_class ctxt class) | quote ctxt (Class_Relation (sub, super)) = Library.quote (Proof_Context.markup_class ctxt sub) ^ " < " ^ Library.quote (Proof_Context.markup_class ctxt super) | quote ctxt (Class_Instance (tyco, class)) = Library.quote (Proof_Context.markup_type ctxt tyco) ^ " :: " ^ Library.quote (Proof_Context.markup_class ctxt class); fun marker (Constant c) = "CONST " ^ c | marker (Type_Constructor tyco) = "TYPE " ^ tyco | marker (Type_Class class) = "CLASS " ^ class | marker (Class_Relation (sub, super)) = "CLASSREL " ^ sub ^ " < " ^ super | marker (Class_Instance (tyco, class)) = "INSTANCE " ^ tyco ^ " :: " ^ class; fun map_attr const tyco class classrel inst module (Constant x) = Constant (const x) | map_attr const tyco class classrel inst module (Type_Constructor x) = Type_Constructor (tyco x) | map_attr const tyco class classrel inst module (Type_Class x) = Type_Class (class x) | map_attr const tyco class classrel inst module (Class_Relation x) = Class_Relation (classrel x) | map_attr const tyco class classrel inst module (Class_Instance x) = Class_Instance (inst x) | map_attr const tyco class classrel inst module (Module x) = Module (module x); fun maps_attr const tyco class classrel inst module (Constant x) = map Constant (const x) | maps_attr const tyco class classrel inst module (Type_Constructor x) = map Type_Constructor (tyco x) | maps_attr const tyco class classrel inst module (Type_Class x) = map Type_Class (class x) | maps_attr const tyco class classrel inst module (Class_Relation x) = map Class_Relation (classrel x) | maps_attr const tyco class classrel inst module (Class_Instance x) = map Class_Instance (inst x) | maps_attr const tyco class classrel inst module (Module x) = map Module (module x); fun maps_attr' const tyco class classrel inst module (Constant x) = (map o apsnd) Constant (const x) | maps_attr' const tyco class classrel inst module (Type_Constructor x) = (map o apsnd) Type_Constructor (tyco x) | maps_attr' const tyco class classrel inst module (Type_Class x) = (map o apsnd) Type_Class (class x) | maps_attr' const tyco class classrel inst module (Class_Relation x) = (map o apsnd) Class_Relation (classrel x) | maps_attr' const tyco class classrel inst module (Class_Instance x) = (map o apsnd) Class_Instance (inst x) | maps_attr' const tyco class classrel inst module (Module x) = (map o apsnd) Module (module x); datatype ('a, 'b, 'c, 'd, 'e, 'f) data = Data of { constant: 'a Symtab.table, type_constructor: 'b Symtab.table, type_class: 'c Symtab.table, class_relation: 'd Symreltab.table, class_instance: 'e Symreltab.table, module: 'f Symtab.table }; fun make_data constant type_constructor type_class class_relation class_instance module = Data { constant = constant, type_constructor = type_constructor, type_class = type_class, class_relation = class_relation, class_instance = class_instance, module = module }; fun dest_data (Data x) = x; fun map_data map_constant map_type_constructor map_type_class map_class_relation map_class_instance map_module (Data { constant, type_constructor, type_class, class_relation, class_instance, module }) = Data { constant = map_constant constant, type_constructor = map_type_constructor type_constructor, type_class = map_type_class type_class, class_relation = map_class_relation class_relation, class_instance = map_class_instance class_instance, module = map_module module }; val empty_data = Data { constant = Symtab.empty, type_constructor = Symtab.empty, type_class = Symtab.empty, class_relation = Symreltab.empty, class_instance = Symreltab.empty, module = Symtab.empty }; fun merge_data (Data { constant = constant1, type_constructor = type_constructor1, type_class = type_class1, class_instance = class_instance1, class_relation = class_relation1, module = module1 }, Data { constant = constant2, type_constructor = type_constructor2, type_class = type_class2, class_instance = class_instance2, class_relation = class_relation2, module = module2 }) = make_data (Symtab.join (K snd) (constant1, constant2)) (Symtab.join (K snd) (type_constructor1, type_constructor2)) (Symtab.join (K snd) (type_class1, type_class2)) (Symreltab.join (K snd) (class_relation1, class_relation2)) (Symreltab.join (K snd) (class_instance1, class_instance2)) (Symtab.join (K snd) (module1, module2)); (*prefer later entries: K snd*) fun set_sym (sym, NONE) = Symtab.delete_safe sym | set_sym (sym, SOME y) = Symtab.update (sym, y); fun set_symrel (symrel, NONE) = Symreltab.delete_safe symrel | set_symrel (symrel, SOME y) = Symreltab.update (symrel, y); fun set_data (Constant x) = map_data (set_sym x) I I I I I | set_data (Type_Constructor x) = map_data I (set_sym x) I I I I | set_data (Type_Class x) = map_data I I (set_sym x) I I I | set_data (Class_Relation x) = map_data I I I (set_symrel x) I I | set_data (Class_Instance x) = map_data I I I I (set_symrel x) I | set_data (Module x) = map_data I I I I I (set_sym x); fun lookup_constant_data x = (Symtab.lookup o #constant o dest_data) x; fun lookup_type_constructor_data x = (Symtab.lookup o #type_constructor o dest_data) x; fun lookup_type_class_data x = (Symtab.lookup o #type_class o dest_data) x; fun lookup_class_relation_data x = (Symreltab.lookup o #class_relation o dest_data) x; fun lookup_class_instance_data x = (Symreltab.lookup o #class_instance o dest_data) x; fun lookup_module_data x = (Symtab.lookup o #module o dest_data) x; fun lookup data (Constant x) = lookup_constant_data data x | lookup data (Type_Constructor x) = lookup_type_constructor_data data x | lookup data (Type_Class x) = lookup_type_class_data data x | lookup data (Class_Relation x) = lookup_class_relation_data data x | lookup data (Class_Instance x) = lookup_class_instance_data data x | lookup data (Module x) = lookup_module_data data x; fun symbols_of x = (map Constant o Symtab.keys o #constant o dest_data) x @ (map Type_Constructor o Symtab.keys o #type_constructor o dest_data) x @ (map Type_Class o Symtab.keys o #type_class o dest_data) x @ (map Class_Relation o Symreltab.keys o #class_relation o dest_data) x @ (map Class_Instance o Symreltab.keys o #class_instance o dest_data) x @ (map Module o Symtab.keys o #module o dest_data) x; fun dest_module_data x = (Symtab.dest o #module o dest_data) x; end; structure Basic_Code_Symbol: BASIC_CODE_SYMBOL = Code_Symbol;