src/Tools/Code/code_symbol.ML
changeset 55147 bce3dbc11f95
parent 55042 29e1657b7389
child 55149 626d8f08d479
equal deleted inserted replaced
55146:525309c2e4ee 55147:bce3dbc11f95
    11     Constant of 'a | Type_Constructor of 'b | Type_Class of 'c |
    11     Constant of 'a | Type_Constructor of 'b | Type_Class of 'c |
    12     Class_Relation of 'd  | Class_Instance of 'e | Module of 'f
    12     Class_Relation of 'd  | Class_Instance of 'e | Module of 'f
    13   type symbol = (string, string, class, class * class, string * class, string) attr
    13   type symbol = (string, string, class, class * class, string * class, string) attr
    14   structure Table: TABLE;
    14   structure Table: TABLE;
    15   structure Graph: GRAPH;
    15   structure Graph: GRAPH;
    16   val default_name: Proof.context -> symbol -> string * string
    16   val namespace_prefix: Proof.context -> symbol -> string
    17   val quote_symbol: Proof.context -> symbol -> string
    17   val base_name: symbol -> string
    18   val tyco_fun: string
    18   val quote: Proof.context -> symbol -> string
       
    19   val marker: symbol -> string
       
    20   val value: symbol
       
    21   val is_value: symbol -> bool
    19   val map_attr: ('a -> 'g) -> ('b -> 'h) -> ('c -> 'i) -> ('d -> 'j) -> ('e -> 'k) -> ('f -> 'l)
    22   val map_attr: ('a -> 'g) -> ('b -> 'h) -> ('c -> 'i) -> ('d -> 'j) -> ('e -> 'k) -> ('f -> 'l)
    20     -> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr
    23     -> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr
    21   val maps_attr: ('a -> 'g list) -> ('b -> 'h list)
    24   val maps_attr: ('a -> 'g list) -> ('b -> 'h list)
    22     -> ('c -> 'i list) -> ('d -> 'j list) -> ('e -> 'k list) -> ('f -> 'l list)
    25     -> ('c -> 'i list) -> ('d -> 'j list) -> ('e -> 'k list) -> ('f -> 'l list)
    23     -> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr list
    26     -> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr list
    36   val lookup_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class -> 'c option
    39   val lookup_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class -> 'c option
    37   val lookup_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class * class -> 'd option
    40   val lookup_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class * class -> 'd option
    38   val lookup_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string * class -> 'e option
    41   val lookup_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string * class -> 'e option
    39   val lookup_module_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'f option
    42   val lookup_module_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'f option
    40   val lookup: ('a, 'a, 'a, 'a, 'a, 'a) data -> symbol -> 'a option
    43   val lookup: ('a, 'a, 'a, 'a, 'a, 'a) data -> symbol -> 'a option
       
    44   val symbols_of: ('a, 'b, 'c, 'd, 'e, 'f) data -> symbol list
       
    45   val mapped_const_data: (string -> 'a -> 'g) -> ('a, 'b, 'c, 'd, 'e, 'f) data -> 'g Symtab.table
    41   val dest_constant_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'a) list
    46   val dest_constant_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'a) list
    42   val dest_type_constructor_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'b) list
    47   val dest_type_constructor_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'b) list
    43   val dest_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (class * 'c) list
    48   val dest_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (class * 'c) list
    44   val dest_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> ((class * class) * 'd) list
    49   val dest_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> ((class * class) * 'd) list
    45   val dest_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> ((string * class) * 'e) list
    50   val dest_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> ((string * class) * 'e) list
    87   fun thyname_of_const thy c = case Axclass.class_of_param thy c
    92   fun thyname_of_const thy c = case Axclass.class_of_param thy c
    88    of SOME class => thyname_of_class thy class
    93    of SOME class => thyname_of_class thy class
    89     | NONE => (case Code.get_type_of_constr_or_abstr thy c
    94     | NONE => (case Code.get_type_of_constr_or_abstr thy c
    90        of SOME (tyco, _) => thyname_of_type thy tyco
    95        of SOME (tyco, _) => thyname_of_type thy tyco
    91         | NONE => #theory_name (Name_Space.the_entry (Sign.const_space thy) c));
    96         | NONE => #theory_name (Name_Space.the_entry (Sign.const_space thy) c));
    92   fun base_rel (x, y) = Long_Name.base_name y ^ "_" ^ Long_Name.base_name x;
    97   fun prefix thy (Constant "") = "Code"
    93   fun plainify ctxt get_prefix get_basename thing =
    98     | prefix thy (Constant c) = thyname_of_const thy c
    94     (get_prefix (Proof_Context.theory_of ctxt) thing,
    99     | prefix thy (Type_Constructor tyco) = thyname_of_type thy tyco
    95       Name.desymbolize false (get_basename thing));
   100     | prefix thy (Type_Class class) = thyname_of_class thy class
       
   101     | prefix thy (Class_Relation (class, _)) = thyname_of_class thy class
       
   102     | prefix thy (Class_Instance inst) = thyname_of_instance thy inst;
       
   103   val base = Name.desymbolize false o Long_Name.base_name;
       
   104   fun base_rel (x, y) = base y ^ "_" ^ base x;
    96 in
   105 in
    97 
   106 
    98 fun default_name ctxt (Constant "==>") =
   107 fun base_name (Constant "") = "value"
    99       plainify ctxt thyname_of_const (K "follows") "==>"
   108   | base_name (Constant "==>") = "follows"
   100   | default_name ctxt (Constant "==") =
   109   | base_name (Constant "==") = "meta_eq"
   101       plainify ctxt thyname_of_const (K "meta_eq") "=="
   110   | base_name (Constant c) = base c
   102   | default_name ctxt (Constant c) =
   111   | base_name (Type_Constructor tyco) = base tyco
   103       plainify ctxt thyname_of_const Long_Name.base_name c
   112   | base_name (Type_Class class) = base class
   104   | default_name ctxt (Type_Constructor "fun") =
   113   | base_name (Class_Relation classrel) = base_rel classrel
   105       plainify ctxt thyname_of_type (K "fun") "fun"
   114   | base_name (Class_Instance inst) = base_rel inst;
   106   | default_name ctxt (Type_Constructor tyco) =
   115 
   107       plainify ctxt thyname_of_type Long_Name.base_name tyco
   116 fun namespace_prefix ctxt = prefix (Proof_Context.theory_of ctxt);
   108   | default_name ctxt (Type_Class class) =
   117 
   109       plainify ctxt thyname_of_class Long_Name.base_name class
   118 fun default_name ctxt sym = (namespace_prefix ctxt sym, base_name sym);
   110   | default_name ctxt (Class_Relation classrel) =
   119 
   111       plainify ctxt (fn thy => fn (class, _) => thyname_of_class thy class) base_rel classrel
   120 val value = Constant "";
   112   | default_name ctxt (Class_Instance inst) =
   121 fun is_value (Constant "") = true
   113       plainify ctxt thyname_of_instance base_rel inst;
   122   | is_value _ = false;
   114 
       
   115 val tyco_fun = (uncurry Long_Name.append o default_name @{context}) (Type_Constructor "fun");
       
   116 
   123 
   117 end;
   124 end;
   118 
   125 
   119 fun quote_symbol ctxt (Constant c) = quote (Code.string_of_const (Proof_Context.theory_of ctxt) c)
   126 fun quote ctxt (Constant c) = Library.quote (Code.string_of_const (Proof_Context.theory_of ctxt) c)
   120   | quote_symbol ctxt (Type_Constructor tyco) = "type " ^ quote (Proof_Context.extern_type ctxt tyco)
   127   | quote ctxt (Type_Constructor tyco) = "type " ^ Library.quote (Proof_Context.extern_type ctxt tyco)
   121   | quote_symbol ctxt (Type_Class class) = "class " ^ quote (Proof_Context.extern_class ctxt class)
   128   | quote ctxt (Type_Class class) = "class " ^ Library.quote (Proof_Context.extern_class ctxt class)
   122   | quote_symbol ctxt (Class_Relation (sub, super)) =
   129   | quote ctxt (Class_Relation (sub, super)) =
   123       quote (Proof_Context.extern_class ctxt sub) ^ " < " ^ quote (Proof_Context.extern_class ctxt super)
   130       Library.quote (Proof_Context.extern_class ctxt sub) ^ " < " ^ Library.quote (Proof_Context.extern_class ctxt super)
   124   | quote_symbol ctxt (Class_Instance (tyco, class)) =
   131   | quote ctxt (Class_Instance (tyco, class)) =
   125       quote (Proof_Context.extern_type ctxt tyco) ^ " :: " ^ quote (Proof_Context.extern_class ctxt class);
   132       Library.quote (Proof_Context.extern_type ctxt tyco) ^ " :: " ^ Library.quote (Proof_Context.extern_class ctxt class);
       
   133 
       
   134 fun marker (Constant c) = "CONST " ^ c
       
   135   | marker (Type_Constructor tyco) = "TYPE " ^ tyco
       
   136   | marker (Type_Class class) = "CLASS " ^ class
       
   137   | marker (Class_Relation (sub, super)) = "CLASSREL " ^ sub ^ " < " ^ super
       
   138   | marker (Class_Instance (tyco, class)) = "INSTANCE " ^ tyco ^ " :: " ^ class;
   126 
   139 
   127 fun map_attr const tyco class classrel inst module (Constant x) = Constant (const x)
   140 fun map_attr const tyco class classrel inst module (Constant x) = Constant (const x)
   128   | map_attr const tyco class classrel inst module (Type_Constructor x) = Type_Constructor (tyco x)
   141   | map_attr const tyco class classrel inst module (Type_Constructor x) = Type_Constructor (tyco x)
   129   | map_attr const tyco class classrel inst module (Type_Class x) = Type_Class (class x)
   142   | map_attr const tyco class classrel inst module (Type_Class x) = Type_Class (class x)
   130   | map_attr const tyco class classrel inst module (Class_Relation x) = Class_Relation (classrel x)
   143   | map_attr const tyco class classrel inst module (Class_Relation x) = Class_Relation (classrel x)
   197   | lookup data (Type_Class x) = lookup_type_class_data data x
   210   | lookup data (Type_Class x) = lookup_type_class_data data x
   198   | lookup data (Class_Relation x) = lookup_class_relation_data data x
   211   | lookup data (Class_Relation x) = lookup_class_relation_data data x
   199   | lookup data (Class_Instance x) = lookup_class_instance_data data x
   212   | lookup data (Class_Instance x) = lookup_class_instance_data data x
   200   | lookup data (Module x) = lookup_module_data data x;
   213   | lookup data (Module x) = lookup_module_data data x;
   201 
   214 
       
   215 fun symbols_of x = (map Constant o Symtab.keys o #constant o dest_data) x
       
   216   @ (map Type_Constructor o Symtab.keys o #type_constructor o dest_data) x
       
   217   @ (map Type_Class o Symtab.keys o #type_class o dest_data) x
       
   218   @ (map Class_Relation o Symreltab.keys o #class_relation o dest_data) x
       
   219   @ (map Class_Instance o Symreltab.keys o #class_instance o dest_data) x
       
   220   @ (map Module o Symtab.keys o #module o dest_data) x;
       
   221 
       
   222 fun mapped_const_data f x = Symtab.map f ((#constant o dest_data) x);
       
   223 
   202 fun dest_constant_data x = (Symtab.dest o #constant o dest_data) x;
   224 fun dest_constant_data x = (Symtab.dest o #constant o dest_data) x;
   203 fun dest_type_constructor_data x = (Symtab.dest o #type_constructor o dest_data) x;
   225 fun dest_type_constructor_data x = (Symtab.dest o #type_constructor o dest_data) x;
   204 fun dest_type_class_data x = (Symtab.dest o #type_class o dest_data) x;
   226 fun dest_type_class_data x = (Symtab.dest o #type_class o dest_data) x;
   205 fun dest_class_relation_data x = (Symreltab.dest o #class_relation o dest_data) x;
   227 fun dest_class_relation_data x = (Symreltab.dest o #class_relation o dest_data) x;
   206 fun dest_class_instance_data x = (Symreltab.dest o #class_instance o dest_data) x;
   228 fun dest_class_instance_data x = (Symreltab.dest o #class_instance o dest_data) x;