bookkeeping and input syntax for exact specification of names of symbols in generated code
authorhaftmann
Fri May 24 23:57:24 2013 +0200 (2013-05-24)
changeset 52138e21426f244aa
parent 52137 7f7337447b1b
child 52139 40fe6b80b481
bookkeeping and input syntax for exact specification of names of symbols in generated code
src/Tools/Code/code_haskell.ML
src/Tools/Code/code_ml.ML
src/Tools/Code/code_namespace.ML
src/Tools/Code/code_scala.ML
src/Tools/Code/code_symbol.ML
src/Tools/Code/code_target.ML
src/Tools/Code/code_thingol.ML
src/Tools/Code_Generator.thy
     1.1 --- a/src/Tools/Code/code_haskell.ML	Fri May 24 23:57:24 2013 +0200
     1.2 +++ b/src/Tools/Code/code_haskell.ML	Fri May 24 23:57:24 2013 +0200
     1.3 @@ -269,7 +269,7 @@
     1.4            end;
     1.5    in print_stmt end;
     1.6  
     1.7 -fun haskell_program_of_program labelled_name module_alias module_prefix reserved =
     1.8 +fun haskell_program_of_program ctxt symbol_of module_prefix module_name reserved identifiers =
     1.9    let
    1.10      fun namify_fun upper base (nsp_fun, nsp_typ) =
    1.11        let
    1.12 @@ -297,9 +297,9 @@
    1.13        | select_stmt (Code_Thingol.Classparam _) = false
    1.14        | select_stmt (Code_Thingol.Classinst _) = true;
    1.15    in
    1.16 -    Code_Namespace.flat_program labelled_name
    1.17 -      { module_alias = module_alias, module_prefix = module_prefix,
    1.18 -        reserved = reserved, empty_nsp = (reserved, reserved), namify_stmt = namify_stmt,
    1.19 +    Code_Namespace.flat_program ctxt symbol_of
    1.20 +      { module_prefix = module_prefix, module_name = module_name, reserved = reserved,
    1.21 +        identifiers = identifiers, empty_nsp = (reserved, reserved), namify_stmt = namify_stmt,
    1.22          modify_stmt = fn stmt => if select_stmt stmt then SOME stmt else NONE }
    1.23    end;
    1.24  
    1.25 @@ -324,14 +324,14 @@
    1.26    ("Maybe", ["Nothing", "Just"])
    1.27  ];
    1.28  
    1.29 -fun serialize_haskell module_prefix string_classes { labelled_name, reserved_syms,
    1.30 -    includes, module_alias, class_syntax, tyco_syntax, const_syntax } program =
    1.31 +fun serialize_haskell module_prefix string_classes ctxt { symbol_of, module_name,
    1.32 +    reserved_syms, identifiers, includes, class_syntax, tyco_syntax, const_syntax } program =
    1.33    let
    1.34  
    1.35      (* build program *)
    1.36      val reserved = fold (insert (op =) o fst) includes reserved_syms;
    1.37      val { deresolver, flat_program = haskell_program } = haskell_program_of_program
    1.38 -      labelled_name module_alias module_prefix (Name.make_context reserved) program;
    1.39 +      ctxt symbol_of module_prefix module_name (Name.make_context reserved) identifiers program;
    1.40  
    1.41      (* print statements *)
    1.42      fun deriving_show tyco =
     2.1 --- a/src/Tools/Code/code_ml.ML	Fri May 24 23:57:24 2013 +0200
     2.2 +++ b/src/Tools/Code/code_ml.ML	Fri May 24 23:57:24 2013 +0200
     2.3 @@ -698,7 +698,7 @@
     2.4  
     2.5  (** SML/OCaml generic part **)
     2.6  
     2.7 -fun ml_program_of_program labelled_name reserved module_alias program =
     2.8 +fun ml_program_of_program ctxt symbol_of module_name reserved identifiers program =
     2.9    let
    2.10      fun namify_const upper base (nsp_const, nsp_type) =
    2.11        let
    2.12 @@ -729,7 +729,8 @@
    2.13        | ml_binding_of_stmt (name, Code_Thingol.Classinst (stmt as { vs, ... })) =
    2.14            (ML_Instance (name, stmt), if forall (null o snd) vs then SOME (name, false) else NONE)
    2.15        | ml_binding_of_stmt (name, _) =
    2.16 -          error ("Binding block containing illegal statement: " ^ labelled_name name)
    2.17 +          error ("Binding block containing illegal statement: " ^ 
    2.18 +            (Code_Symbol.quote_symbol ctxt o symbol_of) name)
    2.19      fun modify_fun (name, stmt) =
    2.20        let
    2.21          val (binding, some_value_name) = ml_binding_of_stmt (name, stmt);
    2.22 @@ -769,21 +770,22 @@
    2.23        | modify_stmts ((stmts as (_, Code_Thingol.Classinst _)::_)) =
    2.24            modify_funs stmts
    2.25        | modify_stmts stmts = error ("Illegal mutual dependencies: " ^
    2.26 -          (Library.commas o map (labelled_name o fst)) stmts);
    2.27 +          (Library.commas o map (Code_Symbol.quote_symbol ctxt o symbol_of o fst)) stmts);
    2.28    in
    2.29 -    Code_Namespace.hierarchical_program labelled_name { module_alias = module_alias, reserved = reserved,
    2.30 +    Code_Namespace.hierarchical_program ctxt symbol_of {
    2.31 +      module_name = module_name, reserved = reserved, identifiers = identifiers,
    2.32        empty_nsp = (reserved, reserved), namify_module = pair, namify_stmt = namify_stmt,
    2.33        cyclic_modules = false, empty_data = (), memorize_data = K I, modify_stmts = modify_stmts } program
    2.34    end;
    2.35  
    2.36 -fun serialize_ml print_ml_module print_ml_stmt with_signatures
    2.37 -    { labelled_name, reserved_syms, includes, module_alias,
    2.38 +fun serialize_ml print_ml_module print_ml_stmt with_signatures ctxt
    2.39 +    { symbol_of, module_name, reserved_syms, identifiers, includes,
    2.40        class_syntax, tyco_syntax, const_syntax } program =
    2.41    let
    2.42  
    2.43      (* build program *)
    2.44      val { deresolver, hierarchical_program = ml_program } =
    2.45 -      ml_program_of_program labelled_name (Name.make_context reserved_syms) module_alias program;
    2.46 +      ml_program_of_program ctxt symbol_of module_name (Name.make_context reserved_syms) identifiers program;
    2.47  
    2.48      (* print statements *)
    2.49      fun print_stmt prefix_fragments (_, stmt) = print_ml_stmt
     3.1 --- a/src/Tools/Code/code_namespace.ML	Fri May 24 23:57:24 2013 +0200
     3.2 +++ b/src/Tools/Code/code_namespace.ML	Fri May 24 23:57:24 2013 +0200
     3.3 @@ -7,8 +7,9 @@
     3.4  signature CODE_NAMESPACE =
     3.5  sig
     3.6    type flat_program
     3.7 -  val flat_program: (string -> string) -> { module_alias: string -> string option,
     3.8 -    module_prefix: string, reserved: Name.context, empty_nsp: 'a,
     3.9 +  val flat_program: Proof.context -> (string -> Code_Symbol.symbol)
    3.10 +    -> { module_prefix: string, module_name: string,
    3.11 +    reserved: Name.context, identifiers: Code_Target.identifier_data, empty_nsp: 'a,
    3.12      namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
    3.13      modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option }
    3.14        -> Code_Thingol.program
    3.15 @@ -20,8 +21,10 @@
    3.16      | Stmt of 'a
    3.17      | Module of ('b * (string * ('a, 'b) node) Graph.T)
    3.18    type ('a, 'b) hierarchical_program
    3.19 -  val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
    3.20 -    reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    3.21 +  val hierarchical_program: Proof.context -> (string -> Code_Symbol.symbol)
    3.22 +    -> { module_name: string,
    3.23 +    reserved: Name.context, identifiers: Code_Target.identifier_data,
    3.24 +    empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    3.25      namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    3.26      cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
    3.27      modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
    3.28 @@ -37,17 +40,25 @@
    3.29  structure Code_Namespace : CODE_NAMESPACE =
    3.30  struct
    3.31  
    3.32 -(** building module name hierarchy **)
    3.33 +(** fundamental module name hierarchy **)
    3.34 +
    3.35 +val split_name = apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
    3.36 +
    3.37 +fun lookup_identifier symbol_of identifiers name =
    3.38 +  Code_Symbol.lookup identifiers (symbol_of name)
    3.39 +  |> Option.map (split_last o Long_Name.explode);
    3.40  
    3.41 -val dest_name =
    3.42 -  apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
    3.43 +fun force_identifier symbol_of fragments_tab identifiers name =
    3.44 +  case lookup_identifier symbol_of identifiers name of
    3.45 +      NONE => (apfst (the o Symtab.lookup fragments_tab) o split_name) name
    3.46 +    | SOME name' => name';
    3.47  
    3.48 -fun build_module_namespace { module_alias, module_prefix, reserved } program =
    3.49 +fun build_module_namespace { module_prefix, module_identifiers, reserved } program =
    3.50    let
    3.51 -    fun alias_fragments name = case module_alias name
    3.52 +    fun alias_fragments name = case module_identifiers name
    3.53       of SOME name' => Long_Name.explode name'
    3.54        | NONE => map (fn name => fst (Name.variant name reserved)) (Long_Name.explode name);
    3.55 -    val module_names = Graph.fold (insert (op =) o fst o dest_name o fst) program [];
    3.56 +    val module_names = Graph.fold (insert (op =) o fst o split_name o fst) program [];
    3.57    in
    3.58      fold (fn name => Symtab.update (name, Long_Name.explode module_prefix @ alias_fragments name))
    3.59        module_names Symtab.empty
    3.60 @@ -58,28 +69,31 @@
    3.61  
    3.62  type flat_program = ((string * Code_Thingol.stmt option) Graph.T * (string * string list) list) Graph.T;
    3.63  
    3.64 -fun flat_program labelled_name { module_alias, module_prefix, reserved,
    3.65 -      empty_nsp, namify_stmt, modify_stmt } program =
    3.66 +fun flat_program ctxt symbol_of { module_prefix, module_name, reserved,
    3.67 +    identifiers, empty_nsp, namify_stmt, modify_stmt } program =
    3.68    let
    3.69  
    3.70      (* building module name hierarchy *)
    3.71 -    val fragments_tab = build_module_namespace { module_alias = module_alias,
    3.72 -      module_prefix = module_prefix, reserved = reserved } program;
    3.73 -    val dest_name = dest_name
    3.74 -      #>> (Long_Name.implode o the o Symtab.lookup fragments_tab);
    3.75 +    val module_identifiers = if module_name = ""
    3.76 +      then Code_Symbol.lookup_module_data identifiers
    3.77 +      else K (SOME module_name);
    3.78 +    val fragments_tab = build_module_namespace { module_prefix = module_prefix,
    3.79 +      module_identifiers = module_identifiers, reserved = reserved } program;
    3.80 +    val prep_name = force_identifier symbol_of fragments_tab identifiers
    3.81 +      #>> Long_Name.implode;
    3.82  
    3.83      (* distribute statements over hierarchy *)
    3.84      fun add_stmt name stmt =
    3.85        let
    3.86 -        val (module_name, base) = dest_name name;
    3.87 +        val (module_name, base) = prep_name name;
    3.88        in
    3.89          Graph.default_node (module_name, (Graph.empty, []))
    3.90          #> (Graph.map_node module_name o apfst) (Graph.new_node (name, (base, stmt)))
    3.91        end;
    3.92      fun add_dependency name name' =
    3.93        let
    3.94 -        val (module_name, _) = dest_name name;
    3.95 -        val (module_name', _) = dest_name name';
    3.96 +        val (module_name, _) = prep_name name;
    3.97 +        val (module_name', _) = prep_name name';
    3.98        in if module_name = module_name'
    3.99          then (Graph.map_node module_name o apfst) (Graph.add_edge (name, name'))
   3.100          else (Graph.map_node module_name o apsnd) (AList.map_default (op =) (module_name', []) (insert (op =) name'))
   3.101 @@ -104,7 +118,7 @@
   3.102  
   3.103      (* qualified and unqualified imports, deresolving *)
   3.104      fun base_deresolver name = fst (Graph.get_node
   3.105 -      (fst (Graph.get_node flat_program (fst (dest_name name)))) name);
   3.106 +      (fst (Graph.get_node flat_program (fst (prep_name name)))) name);
   3.107      fun classify_names gr imports =
   3.108        let
   3.109          val import_tab = maps
   3.110 @@ -122,10 +136,11 @@
   3.111        (uncurry classify_names o Graph.get_node flat_program)
   3.112          (Graph.keys flat_program));
   3.113      fun deresolver "" name =
   3.114 -          Long_Name.append (fst (dest_name name)) (base_deresolver name)
   3.115 +          Long_Name.append (fst (prep_name name)) (base_deresolver name)
   3.116        | deresolver module_name name =
   3.117            the (Symtab.lookup (the (Symtab.lookup deresolver_tab module_name)) name)
   3.118 -          handle Option.Option => error ("Unknown statement name: " ^ labelled_name name);
   3.119 +          handle Option.Option => error ("Unknown statement name: "
   3.120 +            ^ (Code_Symbol.quote_symbol ctxt o symbol_of) name);
   3.121  
   3.122    in { deresolver = deresolver, flat_program = flat_program } end;
   3.123  
   3.124 @@ -146,14 +161,17 @@
   3.125        apsnd o Graph.map_node name_fragment o apsnd o map_module_content
   3.126          o map_module name_fragments;
   3.127  
   3.128 -fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
   3.129 +fun hierarchical_program ctxt symbol_of { module_name, reserved, identifiers, empty_nsp,
   3.130        namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
   3.131    let
   3.132  
   3.133      (* building module name hierarchy *)
   3.134 -    val fragments_tab = build_module_namespace { module_alias = module_alias,
   3.135 -      module_prefix = "", reserved = reserved } program;
   3.136 -    val dest_name = dest_name #>> (the o Symtab.lookup fragments_tab);
   3.137 +    val module_identifiers = if module_name = ""
   3.138 +      then Code_Symbol.lookup_module_data identifiers
   3.139 +      else K (SOME module_name);
   3.140 +    val fragments_tab = build_module_namespace { module_prefix = "",
   3.141 +      module_identifiers = module_identifiers, reserved = reserved } program;
   3.142 +    val prep_name = force_identifier symbol_of fragments_tab identifiers;
   3.143  
   3.144      (* building empty module hierarchy *)
   3.145      val empty_module = (empty_data, Graph.empty);
   3.146 @@ -165,20 +183,23 @@
   3.147        | allocate_module (name_fragment :: name_fragments) =
   3.148            ensure_module name_fragment
   3.149            #> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
   3.150 -    val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
   3.151 -      fragments_tab empty_module;
   3.152 +    val empty_program =
   3.153 +      empty_module
   3.154 +      |> Symtab.fold (fn (_, fragments) => allocate_module fragments) fragments_tab
   3.155 +      |> Graph.fold (allocate_module o these o Option.map fst
   3.156 +          o lookup_identifier symbol_of identifiers o fst) program;
   3.157  
   3.158      (* distribute statements over hierarchy *)
   3.159      fun add_stmt name stmt =
   3.160        let
   3.161 -        val (name_fragments, base) = dest_name name;
   3.162 +        val (name_fragments, base) = prep_name name;
   3.163        in
   3.164          (map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
   3.165        end;
   3.166      fun add_dependency name name' =
   3.167        let
   3.168 -        val (name_fragments, _) = dest_name name;
   3.169 -        val (name_fragments', _) = dest_name name';
   3.170 +        val (name_fragments, _) = prep_name name;
   3.171 +        val (name_fragments', _) = prep_name name';
   3.172          val (name_fragments_common, (diff, diff')) =
   3.173            chop_prefix (op =) (name_fragments, name_fragments');
   3.174          val is_module = not (null diff andalso null diff');
   3.175 @@ -186,7 +207,8 @@
   3.176          val add_edge = if is_module andalso not cyclic_modules
   3.177            then (fn node => Graph.add_edge_acyclic dep node
   3.178              handle Graph.CYCLES _ => error ("Dependency "
   3.179 -              ^ quote name ^ " -> " ^ quote name'
   3.180 +              ^ (Code_Symbol.quote_symbol ctxt o symbol_of) name ^ " -> "
   3.181 +              ^ (Code_Symbol.quote_symbol ctxt o symbol_of) name'
   3.182                ^ " would result in module dependency cycle"))
   3.183            else Graph.add_edge dep
   3.184        in (map_module name_fragments_common o apsnd) add_edge end;
   3.185 @@ -228,13 +250,14 @@
   3.186      (* deresolving *)
   3.187      fun deresolver prefix_fragments name =
   3.188        let
   3.189 -        val (name_fragments, _) = dest_name name;
   3.190 +        val (name_fragments, _) = prep_name name;
   3.191          val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
   3.192          val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
   3.193           of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
   3.194          val (base', _) = Graph.get_node nodes name;
   3.195        in Long_Name.implode (remainder @ [base']) end
   3.196 -        handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   3.197 +        handle Graph.UNDEF _ => error ("Unknown statement name: "
   3.198 +          ^ (Code_Symbol.quote_symbol ctxt o symbol_of) name);
   3.199  
   3.200    in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   3.201  
     4.1 --- a/src/Tools/Code/code_scala.ML	Fri May 24 23:57:24 2013 +0200
     4.2 +++ b/src/Tools/Code/code_scala.ML	Fri May 24 23:57:24 2013 +0200
     4.3 @@ -281,7 +281,7 @@
     4.4            end;
     4.5    in print_stmt end;
     4.6  
     4.7 -fun scala_program_of_program labelled_name reserved module_alias program =
     4.8 +fun scala_program_of_program ctxt symbol_of module_name reserved identifiers program =
     4.9    let
    4.10      fun namify_module name_fragment ((nsp_class, nsp_object), nsp_common) =
    4.11        let
    4.12 @@ -324,20 +324,20 @@
    4.13        | modify_stmt (_, Code_Thingol.Classparam _) = NONE
    4.14        | modify_stmt (_, stmt) = SOME stmt;
    4.15    in
    4.16 -    Code_Namespace.hierarchical_program labelled_name
    4.17 -      { module_alias = module_alias, reserved = reserved,
    4.18 +    Code_Namespace.hierarchical_program ctxt symbol_of
    4.19 +      { module_name = module_name, reserved = reserved, identifiers = identifiers,
    4.20          empty_nsp = ((reserved, reserved), reserved), namify_module = namify_module,
    4.21          namify_stmt = namify_stmt, cyclic_modules = true, empty_data = [],
    4.22          memorize_data = memorize_implicits, modify_stmts = map modify_stmt } program
    4.23    end;
    4.24  
    4.25 -fun serialize_scala { labelled_name, reserved_syms, includes,
    4.26 -    module_alias, class_syntax, tyco_syntax, const_syntax } program =
    4.27 +fun serialize_scala ctxt { symbol_of, module_name, reserved_syms, identifiers,
    4.28 +    includes, class_syntax, tyco_syntax, const_syntax } program =
    4.29    let
    4.30  
    4.31      (* build program *)
    4.32      val { deresolver, hierarchical_program = scala_program } =
    4.33 -      scala_program_of_program labelled_name (Name.make_context reserved_syms) module_alias program;
    4.34 +      scala_program_of_program ctxt symbol_of module_name (Name.make_context reserved_syms) identifiers program;
    4.35  
    4.36      (* print statements *)
    4.37      fun lookup_constr tyco constr = case Graph.get_node program tyco
     5.1 --- a/src/Tools/Code/code_symbol.ML	Fri May 24 23:57:24 2013 +0200
     5.2 +++ b/src/Tools/Code/code_symbol.ML	Fri May 24 23:57:24 2013 +0200
     5.3 @@ -10,6 +10,7 @@
     5.4    datatype ('a, 'b, 'c, 'd, 'e, 'f) attr =
     5.5      Constant of 'a | Type_Constructor of 'b | Type_Class of 'c |
     5.6      Class_Relation of 'd  | Class_Instance of 'e | Module of 'f
     5.7 +  type symbol = (string, string, class, string * class, class * class, string) attr
     5.8    val map_attr: ('a -> 'g) -> ('b -> 'h) -> ('c -> 'i) -> ('d -> 'j) -> ('e -> 'k) -> ('f -> 'l)
     5.9      -> ('a, 'b, 'c, 'd, 'e, 'f) attr -> ('g, 'h, 'i, 'j, 'k, 'l) attr
    5.10    val maps_attr: ('a -> 'g list) -> ('b -> 'h list)
    5.11 @@ -22,22 +23,23 @@
    5.12    val empty_data: ('a, 'b, 'c, 'd, 'e, 'f) data
    5.13    val merge_data: ('a, 'b, 'c, 'd, 'e, 'f) data * ('a, 'b, 'c, 'd, 'e, 'f) data
    5.14      -> ('a, 'b, 'c, 'd, 'e, 'f) data
    5.15 +  val set_data: (string * 'a option, string * 'b option, string * 'c option,
    5.16 +      (class * class) * 'd option, (string * class) * 'e option, string * 'f option) attr
    5.17 +    -> ('a, 'b, 'c, 'd, 'e, 'f) data -> ('a, 'b, 'c, 'd, 'e, 'f) data
    5.18    val lookup_constant_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'a option
    5.19    val lookup_type_constructor_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'b option
    5.20    val lookup_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class -> 'c option
    5.21    val lookup_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> class * class -> 'd option
    5.22    val lookup_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string * class -> 'e option
    5.23    val lookup_module_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> string -> 'f option
    5.24 +  val lookup: ('a, 'a, 'a, 'a, 'a, 'a) data -> symbol -> 'a option
    5.25    val dest_constant_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'a) list
    5.26    val dest_type_constructor_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'b) list
    5.27    val dest_type_class_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (class * 'c) list
    5.28    val dest_class_relation_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> ((class * class) * 'd) list
    5.29    val dest_class_instance_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> ((string * class) * 'e) list
    5.30    val dest_module_data: ('a, 'b, 'c, 'd, 'e, 'f) data -> (string * 'f) list
    5.31 -  val set_data: (string * 'a option, string * 'b option, string * 'c option,
    5.32 -      (class * class) * 'd option, (string * class) * 'e option, string * 'f option) attr
    5.33 -    -> ('a, 'b, 'c, 'd, 'e, 'f) data -> ('a, 'b, 'c, 'd, 'e, 'f) data
    5.34 -  val quote_symbol: Proof.context -> (string, string, class, string * class, class * class, string) attr -> string
    5.35 +  val quote_symbol: Proof.context -> symbol -> string
    5.36  end;
    5.37  
    5.38  structure Code_Symbol : CODE_SYMBOL =
    5.39 @@ -48,6 +50,8 @@
    5.40  datatype ('a, 'b, 'c, 'd, 'e, 'f) attr =
    5.41    Constant of 'a | Type_Constructor of 'b | Type_Class of 'c | Class_Relation of 'd  | Class_Instance of 'e | Module of 'f;
    5.42  
    5.43 +type symbol = (string, string, class, string * class, class * class, string) attr;
    5.44 +
    5.45  fun map_attr const tyco class classrel inst module (Constant x) = Constant (const x)
    5.46    | map_attr const tyco class classrel inst module (Type_Constructor x) = Type_Constructor (tyco x)
    5.47    | map_attr const tyco class classrel inst module (Type_Class x) = Type_Class (class x)
    5.48 @@ -97,20 +101,6 @@
    5.49      (Symreltab.join (K snd) (class_instance1, class_instance2))
    5.50      (Symtab.join (K snd) (module1, module2)); (*prefer later entries: K snd*)
    5.51  
    5.52 -fun lookup_constant_data x = (Symtab.lookup o #constant o dest_data) x;
    5.53 -fun lookup_type_constructor_data x = (Symtab.lookup o #type_constructor o dest_data) x;
    5.54 -fun lookup_type_class_data x = (Symtab.lookup o #type_class o dest_data) x;
    5.55 -fun lookup_class_relation_data x = (Symreltab.lookup o #class_relation o dest_data) x;
    5.56 -fun lookup_class_instance_data x = (Symreltab.lookup o #class_instance o dest_data) x;
    5.57 -fun lookup_module_data x = (Symtab.lookup o #module o dest_data) x;
    5.58 -
    5.59 -fun dest_constant_data x = (Symtab.dest o #constant o dest_data) x;
    5.60 -fun dest_type_constructor_data x = (Symtab.dest o #type_constructor o dest_data) x;
    5.61 -fun dest_type_class_data x = (Symtab.dest o #type_class o dest_data) x;
    5.62 -fun dest_class_relation_data x = (Symreltab.dest o #class_relation o dest_data) x;
    5.63 -fun dest_class_instance_data x = (Symreltab.dest o #class_instance o dest_data) x;
    5.64 -fun dest_module_data x = (Symtab.dest o #module o dest_data) x;
    5.65 -
    5.66  fun set_sym (sym, NONE) = Symtab.delete_safe sym
    5.67    | set_sym (sym, SOME y) = Symtab.update (sym, y);
    5.68  fun set_symrel (symrel, NONE) = Symreltab.delete_safe symrel
    5.69 @@ -123,6 +113,27 @@
    5.70    | set_data (Class_Instance x) = map_data I I I I (set_symrel x) I
    5.71    | set_data (Module x) = map_data I I I I I (set_sym x);
    5.72  
    5.73 +fun lookup_constant_data x = (Symtab.lookup o #constant o dest_data) x;
    5.74 +fun lookup_type_constructor_data x = (Symtab.lookup o #type_constructor o dest_data) x;
    5.75 +fun lookup_type_class_data x = (Symtab.lookup o #type_class o dest_data) x;
    5.76 +fun lookup_class_relation_data x = (Symreltab.lookup o #class_relation o dest_data) x;
    5.77 +fun lookup_class_instance_data x = (Symreltab.lookup o #class_instance o dest_data) x;
    5.78 +fun lookup_module_data x = (Symtab.lookup o #module o dest_data) x;
    5.79 +
    5.80 +fun lookup data (Constant x) = lookup_constant_data data x
    5.81 +  | lookup data (Type_Constructor x) = lookup_type_constructor_data data x
    5.82 +  | lookup data (Type_Class x) = lookup_type_class_data data x
    5.83 +  | lookup data (Class_Relation x) = lookup_class_relation_data data x
    5.84 +  | lookup data (Class_Instance x) = lookup_class_instance_data data x
    5.85 +  | lookup data (Module x) = lookup_module_data data x;
    5.86 +
    5.87 +fun dest_constant_data x = (Symtab.dest o #constant o dest_data) x;
    5.88 +fun dest_type_constructor_data x = (Symtab.dest o #type_constructor o dest_data) x;
    5.89 +fun dest_type_class_data x = (Symtab.dest o #type_class o dest_data) x;
    5.90 +fun dest_class_relation_data x = (Symreltab.dest o #class_relation o dest_data) x;
    5.91 +fun dest_class_instance_data x = (Symreltab.dest o #class_instance o dest_data) x;
    5.92 +fun dest_module_data x = (Symtab.dest o #module o dest_data) x;
    5.93 +
    5.94  fun quote_symbol ctxt (Constant c) = quote (Code.string_of_const (Proof_Context.theory_of ctxt) c)
    5.95    | quote_symbol ctxt (Type_Constructor tyco) = "type " ^ quote (Proof_Context.extern_type ctxt tyco)
    5.96    | quote_symbol ctxt (Type_Class class) = "class " ^ quote (Proof_Context.extern_class ctxt class)
     6.1 --- a/src/Tools/Code/code_target.ML	Fri May 24 23:57:24 2013 +0200
     6.2 +++ b/src/Tools/Code/code_target.ML	Fri May 24 23:57:24 2013 +0200
     6.3 @@ -51,6 +51,9 @@
     6.4    val set_default_code_width: int -> theory -> theory
     6.5  
     6.6    type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
     6.7 +  type identifier_data
     6.8 +  val set_identifiers: (string, string, string, string, string, string) symbol_attr_decl
     6.9 +    -> theory -> theory
    6.10    type const_syntax = Code_Printer.const_syntax
    6.11    type tyco_syntax = Code_Printer.tyco_syntax
    6.12    val set_printings: (const_syntax, tyco_syntax, string, unit, unit, (string * string list)) symbol_attr_decl
    6.13 @@ -79,6 +82,7 @@
    6.14      class * (string * 'c option) list, (class * class) * (string * 'd option) list,
    6.15      (class * string) * (string * 'e option) list,
    6.16      string * (string * 'f option) list) Code_Symbol.attr;
    6.17 +type identifier_data = (string, string, string, string, string, string) Code_Symbol.data;
    6.18  
    6.19  type tyco_syntax = Code_Printer.tyco_syntax;
    6.20  type const_syntax = Code_Printer.const_syntax;
    6.21 @@ -108,11 +112,13 @@
    6.22  (* serializers: functions producing serializations *)
    6.23  
    6.24  type serializer = Token.T list
    6.25 +  -> Proof.context
    6.26    -> {
    6.27 -    labelled_name: string -> string,
    6.28 +    symbol_of: string -> Code_Symbol.symbol,
    6.29 +    module_name: string,
    6.30      reserved_syms: string list,
    6.31 +    identifiers: identifier_data,
    6.32      includes: (string * Pretty.T) list,
    6.33 -    module_alias: string -> string option,
    6.34      class_syntax: string -> string option,
    6.35      tyco_syntax: string -> Code_Printer.tyco_syntax option,
    6.36      const_syntax: string -> Code_Printer.activated_const_syntax option }
    6.37 @@ -134,32 +140,30 @@
    6.38    serial: serial,
    6.39    description: description,
    6.40    reserved: string list,
    6.41 -  module_alias: string Symtab.table,
    6.42 +  identifiers: identifier_data,
    6.43    printings: (Code_Printer.const_syntax, Code_Printer.tyco_syntax, string, unit, unit,
    6.44      (Pretty.T * string list)) Code_Symbol.data
    6.45  };
    6.46  
    6.47 -fun make_target ((serial, description), ((reserved, module_alias), printings)) =
    6.48 +fun make_target ((serial, description), (reserved, (identifiers, printings))) =
    6.49    Target { serial = serial, description = description, reserved = reserved,
    6.50 -    module_alias = module_alias, printings = printings };
    6.51 -fun map_target f ( Target { serial, description, reserved, module_alias, printings } ) =
    6.52 -  make_target (f ((serial, description), ((reserved, module_alias), printings)));
    6.53 +    identifiers = identifiers, printings = printings };
    6.54 +fun map_target f (Target { serial, description, reserved, identifiers, printings }) =
    6.55 +  make_target (f ((serial, description), (reserved, (identifiers, printings))));
    6.56  fun merge_target strict target (Target { serial = serial1, description = description,
    6.57 -  reserved = reserved1, module_alias = module_alias1, printings = printings1 },
    6.58 +  reserved = reserved1, identifiers = identifiers1, printings = printings1 },
    6.59      Target { serial = serial2, description = _,
    6.60 -      reserved = reserved2, module_alias = module_alias2, printings = printings2 }) =
    6.61 +      reserved = reserved2, identifiers = identifiers2, printings = printings2 }) =
    6.62    if serial1 = serial2 orelse not strict then
    6.63 -    make_target ((serial1, description),
    6.64 -      ((merge (op =) (reserved1, reserved2),
    6.65 -         Symtab.join (K snd) (module_alias1, module_alias2)),
    6.66 -        (Code_Symbol.merge_data (printings1, printings2))
    6.67 -    ))
    6.68 +    make_target ((serial1, description), (merge (op =) (reserved1, reserved2),
    6.69 +      (Code_Symbol.merge_data (identifiers1, identifiers2),
    6.70 +        Code_Symbol.merge_data (printings1, printings2))))
    6.71    else
    6.72      error ("Incompatible targets: " ^ quote target);
    6.73  
    6.74  fun the_description (Target { description, ... }) = description;
    6.75  fun the_reserved (Target { reserved, ... }) = reserved;
    6.76 -fun the_module_alias (Target { module_alias , ... }) = module_alias;
    6.77 +fun the_identifiers (Target { identifiers , ... }) = identifiers;
    6.78  fun the_printings (Target { printings, ... }) = printings;
    6.79  
    6.80  structure Targets = Theory_Data
    6.81 @@ -197,8 +201,8 @@
    6.82    in
    6.83      thy
    6.84      |> (Targets.map o apfst o apfst o Symtab.update)
    6.85 -          (target, make_target ((serial (), seri), (([], Symtab.empty),
    6.86 -            Code_Symbol.empty_data)))
    6.87 +        (target, make_target ((serial (), seri),
    6.88 +          ([], (Code_Symbol.empty_data, Code_Symbol.empty_data))))
    6.89    end;
    6.90  
    6.91  fun add_target (target, seri) = put_target (target, Fundamental seri);
    6.92 @@ -210,13 +214,13 @@
    6.93      val _ = assert_target thy target;
    6.94    in
    6.95      thy
    6.96 -    |> (Targets.map o apfst o apfst o Symtab.map_entry target o map_target) f
    6.97 +    |> (Targets.map o apfst o apfst o Symtab.map_entry target o map_target o apsnd) f
    6.98    end;
    6.99  
   6.100  fun map_reserved target =
   6.101 -  map_target_data target o apsnd o apfst o apfst;
   6.102 -fun map_module_alias target =
   6.103 -  map_target_data target o apsnd o apfst o apsnd;
   6.104 +  map_target_data target o apfst;
   6.105 +fun map_identifiers target =
   6.106 +  map_target_data target o apsnd o apfst;
   6.107  fun map_printings target =
   6.108    map_target_data target o apsnd o apsnd;
   6.109  
   6.110 @@ -311,7 +315,7 @@
   6.111      val program4 = Graph.restrict (member (op =) names4) program3;
   6.112    in (names4, program4) end;
   6.113  
   6.114 -fun prepare_serializer thy abortable (serializer : serializer) literals reserved module_alias
   6.115 +fun prepare_serializer thy abortable (serializer : serializer) literals reserved identifiers
   6.116      printings module_name args naming proto_program names =
   6.117    let
   6.118      val (names_hidden, (const_syntax, tyco_syntax, class_syntax)) =
   6.119 @@ -324,11 +328,12 @@
   6.120        then SOME (name, content) else NONE;
   6.121      val includes = map_filter select_include (Code_Symbol.dest_module_data printings);
   6.122    in
   6.123 -    (serializer args {
   6.124 -      labelled_name = Code_Thingol.labelled_name thy proto_program,
   6.125 +    (serializer args (Proof_Context.init_global thy) {
   6.126 +      symbol_of = Code_Thingol.symbol_of proto_program,
   6.127 +      module_name = module_name,
   6.128        reserved_syms = reserved,
   6.129 +      identifiers = identifiers,
   6.130        includes = includes,
   6.131 -      module_alias = if module_name = "" then Symtab.lookup module_alias else K (SOME module_name),
   6.132        const_syntax = Symtab.lookup const_syntax,
   6.133        tyco_syntax = Symtab.lookup tyco_syntax,
   6.134        class_syntax = Symtab.lookup class_syntax },
   6.135 @@ -340,11 +345,9 @@
   6.136      val (default_width, abortable, data, modify) = activate_target thy target;
   6.137      val serializer = case the_description data
   6.138       of Fundamental seri => #serializer seri;
   6.139 -    val reserved = the_reserved data;
   6.140 -    val module_alias = the_module_alias data
   6.141 -    val literals = the_literals thy target;
   6.142 -    val (prepared_serializer, prepared_program) = prepare_serializer thy
   6.143 -      abortable serializer literals reserved module_alias (the_printings data)
   6.144 +    val (prepared_serializer, prepared_program) =
   6.145 +      prepare_serializer thy abortable serializer (the_literals thy target)
   6.146 +        (the_reserved data) (the_identifiers data) (the_printings data)
   6.147          module_name args naming (modify naming program) names
   6.148      val width = the_default default_width some_width;
   6.149    in (fn program => prepared_serializer program width, prepared_program) end;
   6.150 @@ -355,7 +358,7 @@
   6.151        target some_width module_name args naming program names;
   6.152    in mounted_serializer prepared_program end;
   6.153  
   6.154 -fun assert_module_name "" = error ("Empty module name not allowed.")
   6.155 +fun assert_module_name "" = error "Empty module name not allowed here"
   6.156    | assert_module_name module_name = module_name;
   6.157  
   6.158  fun using_master_directory thy =
   6.159 @@ -522,6 +525,20 @@
   6.160  
   6.161  (** serializer configuration **)
   6.162  
   6.163 +(* reserved symbol names *)
   6.164 +
   6.165 +fun add_reserved target sym thy =
   6.166 +  let
   6.167 +    val (_, data) = collapse_hierarchy thy target;
   6.168 +    val _ = if member (op =) (the_reserved data) sym
   6.169 +      then error ("Reserved symbol " ^ quote sym ^ " already declared")
   6.170 +      else ();
   6.171 +  in
   6.172 +    thy
   6.173 +    |> map_reserved target (insert (op =) sym)
   6.174 +  end;
   6.175 +
   6.176 +
   6.177  (* checking and parsing *)
   6.178  
   6.179  fun cert_const thy const =
   6.180 @@ -563,6 +580,26 @@
   6.181    Code_Symbol.map_attr (apfst (Code.read_const thy)) (apfst (read_tyco thy))
   6.182      (apfst (read_class thy)) ((apfst o pairself) (read_class thy)) (apfst (read_inst thy)) I;
   6.183  
   6.184 +fun check_name is_module s =
   6.185 +  let
   6.186 +    val _ = if s = "" then error "Bad empty code name" else ();
   6.187 +    val xs = Long_Name.explode s;
   6.188 +    val xs' = if is_module
   6.189 +        then map (Name.desymbolize true) xs
   6.190 +      else if length xs < 2
   6.191 +        then error ("Bad code name without module component: " ^ quote s)
   6.192 +      else
   6.193 +        let
   6.194 +          val (ys, y) = split_last xs;
   6.195 +          val ys' = map (Name.desymbolize true) ys;
   6.196 +          val y' = Name.desymbolize false y;
   6.197 +        in ys' @ [y'] end;
   6.198 +  in if xs' = xs
   6.199 +    then s
   6.200 +    else error ("Invalid code name: " ^ quote s ^ "\n"
   6.201 +      ^ "better try " ^ quote (Long_Name.implode xs'))
   6.202 +  end;
   6.203 +
   6.204  fun check_const_syntax thy c syn =
   6.205    if Code_Printer.requires_args syn > Code.args_number thy c
   6.206    then error ("Too many arguments in syntax for constant " ^ quote c)
   6.207 @@ -573,6 +610,36 @@
   6.208    then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
   6.209    else syn;
   6.210  
   6.211 +
   6.212 +(* custom symbol names *)
   6.213 +
   6.214 +val arrange_name_decls =
   6.215 +  let
   6.216 +    fun arrange is_module (sym, target_names) = map (fn (target, some_name) =>
   6.217 +      (target, (sym, Option.map (check_name is_module) some_name))) target_names;
   6.218 +  in
   6.219 +    Code_Symbol.maps_attr' (arrange false) (arrange false) (arrange false)
   6.220 +      (arrange false) (arrange false) (arrange true)
   6.221 +  end;
   6.222 +
   6.223 +fun cert_name_decls thy = cert_syms thy #> arrange_name_decls;
   6.224 +
   6.225 +fun read_name_decls thy = read_syms thy #> arrange_name_decls;
   6.226 +
   6.227 +fun set_identifier (target, sym_name) = map_identifiers target (Code_Symbol.set_data sym_name);
   6.228 +
   6.229 +fun gen_set_identifiers prep_name_decl raw_name_decls thy =
   6.230 +  fold set_identifier (prep_name_decl thy raw_name_decls) thy;
   6.231 +
   6.232 +val set_identifiers = gen_set_identifiers cert_name_decls;
   6.233 +val set_identifiers_cmd = gen_set_identifiers read_name_decls;
   6.234 +
   6.235 +fun add_module_alias_cmd target = fold (fn (sym, name) =>
   6.236 +  set_identifier (target, Code_Symbol.Module (sym, if name = "" then NONE else SOME (check_name true name))));
   6.237 +
   6.238 +
   6.239 +(* custom printings *)
   6.240 +
   6.241  fun arrange_printings prep_const thy =
   6.242    let
   6.243      fun arrange check (sym, target_syns) =
   6.244 @@ -589,35 +656,6 @@
   6.245  
   6.246  fun read_printings thy = read_syms thy #> arrange_printings Code.read_const thy;
   6.247  
   6.248 -
   6.249 -(* custom symbol names *)
   6.250 -
   6.251 -fun add_module_alias target (thyname, "") =
   6.252 -      map_module_alias target (Symtab.delete thyname)
   6.253 -  | add_module_alias target (thyname, modlname) =
   6.254 -      let
   6.255 -        val xs = Long_Name.explode modlname;
   6.256 -        val xs' = map (Name.desymbolize true) xs;
   6.257 -      in if xs' = xs
   6.258 -        then map_module_alias target (Symtab.update (thyname, modlname))
   6.259 -        else error ("Invalid module name: " ^ quote modlname ^ "\n"
   6.260 -          ^ "perhaps try " ^ quote (Long_Name.implode xs'))
   6.261 -      end;
   6.262 -
   6.263 -fun add_reserved target sym thy =
   6.264 -  let
   6.265 -    val (_, data) = collapse_hierarchy thy target;
   6.266 -    val _ = if member (op =) (the_reserved data) sym
   6.267 -      then error ("Reserved symbol " ^ quote sym ^ " already declared")
   6.268 -      else ();
   6.269 -  in
   6.270 -    thy
   6.271 -    |> map_reserved target (insert (op =) sym)
   6.272 -  end;
   6.273 -
   6.274 -
   6.275 -(* custom printings *)
   6.276 -
   6.277  fun set_printing (target, sym_syn) = map_printings target (Code_Symbol.set_data sym_syn);
   6.278  
   6.279  fun gen_set_printings prep_print_decl raw_print_decls thy =
   6.280 @@ -699,18 +737,6 @@
   6.281  
   6.282  (** Isar setup **)
   6.283  
   6.284 -val code_expr_argsP = Scan.optional (@{keyword "("} |-- Args.parse --| @{keyword ")"}) [];
   6.285 -
   6.286 -val code_exprP =
   6.287 -  Scan.repeat1 Parse.term_group :|-- (fn raw_cs =>
   6.288 -    ((@{keyword "checking"} |-- Scan.repeat (Parse.name
   6.289 -      -- ((@{keyword "?"} |-- Scan.succeed false) || Scan.succeed true) -- code_expr_argsP))
   6.290 -      >> (fn seris => check_code_cmd raw_cs seris)
   6.291 -    || Scan.repeat (@{keyword "in"} |-- Parse.name
   6.292 -       -- Scan.optional (@{keyword "module_name"} |-- Parse.name) ""
   6.293 -       -- Scan.optional (@{keyword "file"} |-- Parse.name) ""
   6.294 -       -- code_expr_argsP) >> (fn seris => export_code_cmd raw_cs seris)));
   6.295 -
   6.296  fun parse_single_symbol_pragma parse_keyword parse_isa parse_target =
   6.297    parse_keyword |-- Parse.!!! parse_isa --| (@{keyword "\<rightharpoonup>"} || @{keyword "=>"})
   6.298      -- Parse.and_list1 (@{keyword "("} |-- (Parse.name --| @{keyword ")"} -- Scan.option parse_target));
   6.299 @@ -733,6 +759,34 @@
   6.300    Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma")
   6.301      (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module));
   6.302  
   6.303 +val code_expr_argsP = Scan.optional (@{keyword "("} |-- Args.parse --| @{keyword ")"}) [];
   6.304 +
   6.305 +val code_exprP =
   6.306 +  Scan.repeat1 Parse.term_group :|-- (fn raw_cs =>
   6.307 +    ((@{keyword "checking"} |-- Scan.repeat (Parse.name
   6.308 +      -- ((@{keyword "?"} |-- Scan.succeed false) || Scan.succeed true) -- code_expr_argsP))
   6.309 +      >> (fn seris => check_code_cmd raw_cs seris)
   6.310 +    || Scan.repeat (@{keyword "in"} |-- Parse.name
   6.311 +       -- Scan.optional (@{keyword "module_name"} |-- Parse.name) ""
   6.312 +       -- Scan.optional (@{keyword "file"} |-- Parse.name) ""
   6.313 +       -- code_expr_argsP) >> (fn seris => export_code_cmd raw_cs seris)));
   6.314 +
   6.315 +val _ =
   6.316 +  Outer_Syntax.command @{command_spec "code_reserved"}
   6.317 +    "declare words as reserved for target language"
   6.318 +    (Parse.name -- Scan.repeat1 Parse.name
   6.319 +      >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds));
   6.320 +
   6.321 +val _ =
   6.322 +  Outer_Syntax.command @{command_spec "code_identifier"} "declare mandatory names for code symbols"
   6.323 +    (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name
   6.324 +      >> (Toplevel.theory o fold set_identifiers_cmd));
   6.325 +
   6.326 +val _ =
   6.327 +  Outer_Syntax.command @{command_spec "code_modulename"} "alias module to other name"
   6.328 +    (Parse.name -- Scan.repeat1 (Parse.name -- Parse.name)
   6.329 +      >> (fn (target, modlnames) => (Toplevel.theory o add_module_alias_cmd target) modlnames));
   6.330 +
   6.331  val _ =
   6.332    Outer_Syntax.command @{command_spec "code_printing"} "declare dedicated printing for code symbols"
   6.333      (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
   6.334 @@ -761,12 +815,6 @@
   6.335        add_instance_syntax_cmd);
   6.336  
   6.337  val _ =
   6.338 -  Outer_Syntax.command @{command_spec "code_reserved"}
   6.339 -    "declare words as reserved for target language"
   6.340 -    (Parse.name -- Scan.repeat1 Parse.name
   6.341 -      >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds));
   6.342 -
   6.343 -val _ =
   6.344    Outer_Syntax.command @{command_spec "code_include"}
   6.345      "declare piece of code to be included in generated code"
   6.346      (Parse.name -- Parse.name -- (Parse.text :|--
   6.347 @@ -776,11 +824,6 @@
   6.348            (Toplevel.theory o add_include_cmd target) (name, content_consts)));
   6.349  
   6.350  val _ =
   6.351 -  Outer_Syntax.command @{command_spec "code_modulename"} "alias module to other name"
   6.352 -    (Parse.name -- Scan.repeat1 (Parse.name -- Parse.name)
   6.353 -      >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames));
   6.354 -
   6.355 -val _ =
   6.356    Outer_Syntax.command @{command_spec "code_abort"}
   6.357      "permit constant to be implemented as program abort"
   6.358      (Scan.repeat1 Parse.term_group >> (Toplevel.theory o fold allow_abort_cmd));
     7.1 --- a/src/Tools/Code/code_thingol.ML	Fri May 24 23:57:24 2013 +0200
     7.2 +++ b/src/Tools/Code/code_thingol.ML	Fri May 24 23:57:24 2013 +0200
     7.3 @@ -86,7 +86,7 @@
     7.4    val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt
     7.5    val is_constr: program -> string -> bool
     7.6    val is_case: stmt -> bool
     7.7 -  val labelled_name: theory -> program -> string -> string
     7.8 +  val symbol_of: program -> string -> Code_Symbol.symbol;
     7.9    val group_stmts: theory -> program
    7.10      -> ((string * stmt) list * (string * stmt) list
    7.11        * ((string * stmt) list * (string * stmt) list)) list
    7.12 @@ -471,29 +471,30 @@
    7.13  fun is_case (Fun (_, (_, SOME _))) = true
    7.14    | is_case _ = false;
    7.15  
    7.16 -fun labelled_name thy program name =
    7.17 -  let val ctxt = Proof_Context.init_global thy in
    7.18 -    case Graph.get_node program name of
    7.19 -      Fun (c, _) => quote (Code.string_of_const thy c)
    7.20 -    | Datatype (tyco, _) => "type " ^ quote (Proof_Context.extern_type ctxt tyco)
    7.21 -    | Datatypecons (c, _) => quote (Code.string_of_const thy c)
    7.22 -    | Class (class, _) => "class " ^ quote (Proof_Context.extern_class ctxt class)
    7.23 -    | Classrel (sub, super) =>
    7.24 -        let
    7.25 -          val Class (sub, _) = Graph.get_node program sub;
    7.26 -          val Class (super, _) = Graph.get_node program super;
    7.27 -        in
    7.28 -          quote (Proof_Context.extern_class ctxt sub ^ " < " ^ Proof_Context.extern_class ctxt super)
    7.29 -        end
    7.30 -    | Classparam (c, _) => quote (Code.string_of_const thy c)
    7.31 -    | Classinst { class, tyco, ... } =>
    7.32 -        let
    7.33 -          val Class (class, _) = Graph.get_node program class;
    7.34 -          val Datatype (tyco, _) = Graph.get_node program tyco;
    7.35 -        in
    7.36 -          quote (Proof_Context.extern_type ctxt tyco ^ " :: " ^ Proof_Context.extern_class ctxt class)
    7.37 -        end
    7.38 -  end;
    7.39 +fun symbol_of program name = 
    7.40 +  case try (Graph.get_node program) name of
    7.41 +      NONE => Code_Symbol.Module "(unknown)"
    7.42 +        (*FIXME: need to be liberal due to ad-hoc introduction of value for evaluation*)
    7.43 +    | SOME stmt => case stmt of
    7.44 +          Fun (c, _) => Code_Symbol.Constant c
    7.45 +        | Datatype (tyco, _) => Code_Symbol.Type_Constructor tyco
    7.46 +        | Datatypecons (c, _) => Code_Symbol.Constant c
    7.47 +        | Class (class, _) => Code_Symbol.Type_Class class
    7.48 +        | Classrel (sub, super) =>
    7.49 +            let
    7.50 +              val Class (sub, _) = Graph.get_node program sub;
    7.51 +              val Class (super, _) = Graph.get_node program super;
    7.52 +            in
    7.53 +              Code_Symbol.Class_Relation (sub, super)
    7.54 +            end
    7.55 +        | Classparam (c, _) => Code_Symbol.Constant c
    7.56 +        | Classinst { class, tyco, ... } =>
    7.57 +            let
    7.58 +              val Class (class, _) = Graph.get_node program class;
    7.59 +              val Datatype (tyco, _) = Graph.get_node program tyco;
    7.60 +            in
    7.61 +              Code_Symbol.Class_Instance (tyco, class)
    7.62 +            end;
    7.63  
    7.64  fun linear_stmts program =
    7.65    rev (Graph.strong_conn program)
    7.66 @@ -515,8 +516,9 @@
    7.67        then ([], filter is_class stmts, ([], []))
    7.68        else if forall (is_fun orf is_classinst) stmts
    7.69        then ([], [], List.partition is_fun stmts)
    7.70 -      else error ("Illegal mutual dependencies: " ^
    7.71 -        (commas o map (labelled_name thy program o fst)) stmts)
    7.72 +      else error ("Illegal mutual dependencies: " ^ (commas
    7.73 +        o map (Code_Symbol.quote_symbol (Proof_Context.init_global thy)
    7.74 +        o symbol_of program o fst)) stmts);
    7.75    in
    7.76      linear_stmts program
    7.77      |> map group
     8.1 --- a/src/Tools/Code_Generator.thy	Fri May 24 23:57:24 2013 +0200
     8.2 +++ b/src/Tools/Code_Generator.thy	Fri May 24 23:57:24 2013 +0200
     8.3 @@ -8,7 +8,7 @@
     8.4  imports Pure
     8.5  keywords
     8.6    "value" "print_codeproc" "code_thms" "code_deps" :: diag and
     8.7 -  "export_code" "code_printing" "code_class" "code_instance" "code_type"
     8.8 +  "export_code" "code_identifier" "code_printing" "code_class" "code_instance" "code_type"
     8.9      "code_const" "code_reserved" "code_include" "code_modulename"
    8.10      "code_abort" "code_monad" "code_reflect" :: thy_decl and
    8.11    "datatypes" "functions" "module_name" "file" "checking"