src/Tools/Code/code_namespace.ML
author haftmann
Sun Feb 23 10:33:43 2014 +0100 (2014-02-23)
changeset 55683 5732a55b9232
parent 55681 7714287dc044
child 55684 ee49b4f7edc8
permissions -rw-r--r--
explicit option for "open" code generation
haftmann@38970
     1
(*  Title:      Tools/Code/code_namespace.ML
haftmann@38970
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@38970
     3
haftmann@38970
     4
Mastering target language namespaces.
haftmann@38970
     5
*)
haftmann@38970
     6
haftmann@38970
     7
signature CODE_NAMESPACE =
haftmann@38970
     8
sig
haftmann@55681
     9
  datatype export = Private | Opaque | Public
haftmann@55681
    10
  val is_public: export -> bool
haftmann@55681
    11
  val not_private: export -> bool
haftmann@55681
    12
haftmann@39208
    13
  type flat_program
haftmann@55147
    14
  val flat_program: Proof.context
haftmann@52138
    15
    -> { module_prefix: string, module_name: string,
haftmann@52138
    16
    reserved: Name.context, identifiers: Code_Target.identifier_data, empty_nsp: 'a,
haftmann@39208
    17
    namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
haftmann@39208
    18
    modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option }
haftmann@55683
    19
      -> Code_Symbol.T list -> Code_Thingol.program
haftmann@55150
    20
      -> { deresolver: string -> Code_Symbol.T -> string,
haftmann@39208
    21
           flat_program: flat_program }
haftmann@39208
    22
haftmann@39017
    23
  datatype ('a, 'b) node =
haftmann@38970
    24
      Dummy
haftmann@55681
    25
    | Stmt of export * 'a
haftmann@55147
    26
    | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T)
haftmann@39208
    27
  type ('a, 'b) hierarchical_program
haftmann@55147
    28
  val hierarchical_program: Proof.context
haftmann@52138
    29
    -> { module_name: string,
haftmann@52138
    30
    reserved: Name.context, identifiers: Code_Target.identifier_data,
haftmann@52138
    31
    empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
haftmann@39022
    32
    namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
haftmann@55150
    33
    cyclic_modules: bool, empty_data: 'b, memorize_data: Code_Symbol.T -> 'b -> 'b,
haftmann@55150
    34
    modify_stmts: (Code_Symbol.T * Code_Thingol.stmt) list -> 'a option list }
haftmann@55683
    35
      -> Code_Symbol.T list -> Code_Thingol.program
haftmann@55150
    36
      -> { deresolver: string list -> Code_Symbol.T -> string,
haftmann@39147
    37
           hierarchical_program: ('a, 'b) hierarchical_program }
haftmann@39147
    38
  val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
haftmann@55681
    39
    print_stmt: string list -> Code_Symbol.T * (export * 'a) -> 'c,
haftmann@39147
    40
    lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
haftmann@39147
    41
      -> ('a, 'b) hierarchical_program -> 'c list
haftmann@38970
    42
end;
haftmann@38970
    43
haftmann@38970
    44
structure Code_Namespace : CODE_NAMESPACE =
haftmann@38970
    45
struct
haftmann@38970
    46
haftmann@55681
    47
(** export **)
haftmann@55681
    48
haftmann@55681
    49
datatype export = Private | Opaque | Public;
haftmann@55681
    50
haftmann@55681
    51
fun is_public Public = true
haftmann@55681
    52
  | is_public _ = false;
haftmann@55681
    53
haftmann@55681
    54
fun not_private Public = true
haftmann@55681
    55
  | not_private Opaque = true
haftmann@55681
    56
  | not_private _ = false;
haftmann@55681
    57
haftmann@55681
    58
haftmann@52138
    59
(** fundamental module name hierarchy **)
haftmann@52138
    60
haftmann@55291
    61
fun module_fragments' { identifiers, reserved } name =
haftmann@55291
    62
  case Code_Symbol.lookup_module_data identifiers name of
haftmann@55291
    63
      SOME (fragments, _) => fragments
haftmann@55291
    64
    | NONE => map (fn fragment => fst (Name.variant fragment reserved)) (Long_Name.explode name);
haftmann@55291
    65
haftmann@55291
    66
fun module_fragments { module_name, identifiers, reserved } =
haftmann@55291
    67
  if module_name = ""
haftmann@55291
    68
  then module_fragments' { identifiers = identifiers, reserved = reserved }
haftmann@55291
    69
  else K (Long_Name.explode module_name);
haftmann@39055
    70
haftmann@55291
    71
fun build_module_namespace ctxt { module_prefix, module_name, identifiers, reserved } program =
haftmann@55291
    72
  let
haftmann@55291
    73
    val module_names = Code_Symbol.Graph.fold (insert (op =) o Code_Symbol.default_prefix ctxt o fst) program [];
haftmann@55291
    74
    val module_fragments' = module_fragments
haftmann@55291
    75
      { module_name = module_name, identifiers = identifiers, reserved = reserved };
haftmann@55291
    76
  in
haftmann@55291
    77
    fold (fn name => Symtab.update (name, Long_Name.explode module_prefix @ module_fragments' name))
haftmann@55291
    78
      module_names Symtab.empty
haftmann@55291
    79
  end;
haftmann@55291
    80
haftmann@55292
    81
fun prep_symbol ctxt { module_namespace, force_module, identifiers } sym =
haftmann@55291
    82
  case Code_Symbol.lookup identifiers sym of
haftmann@55292
    83
      NONE => ((the o Symtab.lookup module_namespace o Code_Symbol.default_prefix ctxt) sym,
haftmann@55292
    84
        Code_Symbol.default_base sym)
haftmann@53414
    85
    | SOME prefix_name => if null force_module then prefix_name
haftmann@53414
    86
        else (force_module, snd prefix_name);
haftmann@39055
    87
haftmann@55293
    88
fun has_priority identifiers = is_some o Code_Symbol.lookup identifiers;
haftmann@55293
    89
haftmann@55292
    90
fun build_proto_program { empty, add_stmt, add_dep } program =
haftmann@55292
    91
  empty
haftmann@55292
    92
  |> Code_Symbol.Graph.fold (fn (sym, (stmt, _)) => add_stmt sym stmt) program
haftmann@55292
    93
  |> Code_Symbol.Graph.fold (fn (sym, (_, (_, syms))) =>
haftmann@55292
    94
      Code_Symbol.Graph.Keys.fold (add_dep sym) syms) program;
haftmann@55292
    95
haftmann@55293
    96
fun prioritize has_priority = uncurry append o List.partition has_priority;
haftmann@55293
    97
haftmann@39055
    98
haftmann@39208
    99
(** flat program structure **)
haftmann@39208
   100
haftmann@55681
   101
type flat_program = ((string * (export * Code_Thingol.stmt) option) Code_Symbol.Graph.T * (string * Code_Symbol.T list) list) Graph.T;
haftmann@39208
   102
haftmann@55147
   103
fun flat_program ctxt { module_prefix, module_name, reserved,
haftmann@55683
   104
    identifiers, empty_nsp, namify_stmt, modify_stmt } exports program =
haftmann@39208
   105
  let
haftmann@39208
   106
haftmann@39208
   107
    (* building module name hierarchy *)
haftmann@55291
   108
    val module_namespace = build_module_namespace ctxt { module_prefix = module_prefix,
haftmann@55291
   109
      module_name = module_name, identifiers = identifiers, reserved = reserved } program;
haftmann@55292
   110
    val prep_sym = prep_symbol ctxt { module_namespace = module_namespace,
haftmann@55292
   111
      force_module = Long_Name.explode module_name, identifiers = identifiers }
haftmann@52138
   112
      #>> Long_Name.implode;
haftmann@55293
   113
    val sym_priority = has_priority identifiers;
haftmann@39208
   114
haftmann@39208
   115
    (* distribute statements over hierarchy *)
haftmann@55147
   116
    fun add_stmt sym stmt =
haftmann@39208
   117
      let
haftmann@55147
   118
        val (module_name, base) = prep_sym sym;
haftmann@39208
   119
      in
haftmann@55147
   120
        Graph.default_node (module_name, (Code_Symbol.Graph.empty, []))
haftmann@55680
   121
        #> (Graph.map_node module_name o apfst)
haftmann@55681
   122
          (Code_Symbol.Graph.new_node (sym, (base, (Public, stmt))))
haftmann@39208
   123
      end;
haftmann@55292
   124
    fun add_dep sym sym' =
haftmann@39208
   125
      let
haftmann@55147
   126
        val (module_name, _) = prep_sym sym;
haftmann@55147
   127
        val (module_name', _) = prep_sym sym';
haftmann@39208
   128
      in if module_name = module_name'
haftmann@55147
   129
        then (Graph.map_node module_name o apfst) (Code_Symbol.Graph.add_edge (sym, sym'))
haftmann@55680
   130
        else (Graph.map_node module_name o apsnd)
haftmann@55680
   131
          (AList.map_default (op =) (module_name', []) (insert (op =) sym'))
haftmann@39208
   132
      end;
haftmann@55292
   133
    val proto_program = build_proto_program
haftmann@55292
   134
      { empty = Graph.empty, add_stmt = add_stmt, add_dep = add_dep } program;
haftmann@39208
   135
haftmann@39208
   136
    (* name declarations and statement modifications *)
haftmann@55679
   137
    fun declare sym (base, (_, stmt)) (gr, nsp) = 
haftmann@39208
   138
      let
haftmann@39208
   139
        val (base', nsp') = namify_stmt stmt base nsp;
haftmann@55147
   140
        val gr' = (Code_Symbol.Graph.map_node sym o apfst) (K base') gr;
haftmann@39208
   141
      in (gr', nsp') end;
haftmann@39208
   142
    fun declarations gr = (gr, empty_nsp)
haftmann@55293
   143
      |> fold (fn sym => declare sym (Code_Symbol.Graph.get_node gr sym))
haftmann@55293
   144
          (prioritize sym_priority (Code_Symbol.Graph.keys gr))
haftmann@39208
   145
      |> fst
haftmann@55680
   146
      |> Code_Symbol.Graph.map_strong_conn (fn syms_bases_exports_stmts =>
haftmann@55680
   147
        map snd syms_bases_exports_stmts
haftmann@55680
   148
        |> (map o apsnd) (fn (export, stmt) => Option.map (pair export) (modify_stmt stmt)));
haftmann@39208
   149
    val flat_program = proto_program
haftmann@39208
   150
      |> (Graph.map o K o apfst) declarations;
haftmann@39208
   151
haftmann@39208
   152
    (* qualified and unqualified imports, deresolving *)
haftmann@55147
   153
    fun base_deresolver sym = fst (Code_Symbol.Graph.get_node
haftmann@55147
   154
      (fst (Graph.get_node flat_program (fst (prep_sym sym)))) sym);
haftmann@39208
   155
    fun classify_names gr imports =
haftmann@39208
   156
      let
haftmann@39208
   157
        val import_tab = maps
haftmann@55147
   158
          (fn (module_name, syms) => map (rpair module_name) syms) imports;
haftmann@55147
   159
        val imported_syms = map fst import_tab;
haftmann@55147
   160
        val here_syms = Code_Symbol.Graph.keys gr;
haftmann@39208
   161
      in
haftmann@55147
   162
        Code_Symbol.Table.empty
haftmann@55147
   163
        |> fold (fn sym => Code_Symbol.Table.update (sym, base_deresolver sym)) here_syms
haftmann@55147
   164
        |> fold (fn sym => Code_Symbol.Table.update (sym,
haftmann@55147
   165
            Long_Name.append (the (AList.lookup (op =) import_tab sym))
haftmann@55147
   166
              (base_deresolver sym))) imported_syms
haftmann@39208
   167
      end;
haftmann@40630
   168
    val deresolver_tab = Symtab.make (AList.make
haftmann@40630
   169
      (uncurry classify_names o Graph.get_node flat_program)
haftmann@40630
   170
        (Graph.keys flat_program));
haftmann@55147
   171
    fun deresolver "" sym =
haftmann@55147
   172
          Long_Name.append (fst (prep_sym sym)) (base_deresolver sym)
haftmann@55147
   173
      | deresolver module_name sym =
haftmann@55147
   174
          the (Code_Symbol.Table.lookup (the (Symtab.lookup deresolver_tab module_name)) sym)
haftmann@52138
   175
          handle Option.Option => error ("Unknown statement name: "
haftmann@55147
   176
            ^ Code_Symbol.quote ctxt sym);
haftmann@39208
   177
haftmann@39208
   178
  in { deresolver = deresolver, flat_program = flat_program } end;
haftmann@39208
   179
haftmann@39208
   180
haftmann@39055
   181
(** hierarchical program structure **)
haftmann@38970
   182
haftmann@39017
   183
datatype ('a, 'b) node =
haftmann@38970
   184
    Dummy
haftmann@55681
   185
  | Stmt of export * 'a
haftmann@55147
   186
  | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T);
haftmann@38970
   187
haftmann@55147
   188
type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Code_Symbol.Graph.T;
haftmann@39147
   189
haftmann@55679
   190
fun the_stmt (Stmt (export, stmt)) = (export, stmt);
haftmann@55679
   191
haftmann@39018
   192
fun map_module_content f (Module content) = Module (f content);
haftmann@39018
   193
haftmann@39018
   194
fun map_module [] = I
haftmann@39018
   195
  | map_module (name_fragment :: name_fragments) =
haftmann@55147
   196
      apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content
haftmann@39018
   197
        o map_module name_fragments;
haftmann@39018
   198
haftmann@55680
   199
fun map_module_stmts f_module f_stmts sym_base_nodes =
haftmann@55680
   200
  let
haftmann@55680
   201
    val some_modules =
haftmann@55680
   202
      sym_base_nodes
haftmann@55680
   203
      |> map (fn (sym, (base, Module content)) => SOME (base, content) | _ => NONE)
haftmann@55680
   204
      |> (burrow_options o map o apsnd) f_module;
haftmann@55680
   205
    val some_export_stmts =
haftmann@55680
   206
      sym_base_nodes
haftmann@55680
   207
      |> map (fn (sym, (base, Stmt export_stmt)) => SOME ((sym, export_stmt), base) | _ => NONE)
haftmann@55680
   208
      |> (burrow_options o burrow_fst) (fn [] => [] | xs => f_stmts xs)
haftmann@55680
   209
  in
haftmann@55680
   210
    map2 (fn SOME (base, content) => (K (base, Module content))
haftmann@55680
   211
      | NONE => fn SOME (some_export_stmt, base) =>
haftmann@55680
   212
          (base, case some_export_stmt of SOME export_stmt => Stmt export_stmt | NONE => Dummy))
haftmann@55680
   213
      some_modules some_export_stmts
haftmann@55680
   214
  end;
haftmann@55680
   215
haftmann@55147
   216
fun hierarchical_program ctxt { module_name, reserved, identifiers, empty_nsp,
haftmann@55683
   217
      namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts }
haftmann@55683
   218
      exports program =
haftmann@38970
   219
  let
haftmann@38970
   220
haftmann@38970
   221
    (* building module name hierarchy *)
haftmann@55291
   222
    val module_namespace = build_module_namespace ctxt { module_prefix = "",
haftmann@55291
   223
      module_name = module_name, identifiers = identifiers, reserved = reserved } program;
haftmann@55292
   224
    val prep_sym = prep_symbol ctxt { module_namespace = module_namespace,
haftmann@55293
   225
      force_module = Long_Name.explode module_name, identifiers = identifiers }
haftmann@55293
   226
    val sym_priority = has_priority identifiers;
haftmann@38970
   227
haftmann@38970
   228
    (* building empty module hierarchy *)
haftmann@55147
   229
    val empty_module = (empty_data, Code_Symbol.Graph.empty);
haftmann@38970
   230
    fun ensure_module name_fragment (data, nodes) =
haftmann@55147
   231
      if can (Code_Symbol.Graph.get_node nodes) (Code_Symbol.Module name_fragment) then (data, nodes)
haftmann@38970
   232
      else (data,
haftmann@55147
   233
        nodes |> Code_Symbol.Graph.new_node (Code_Symbol.Module name_fragment, (name_fragment, Module empty_module)));
haftmann@38970
   234
    fun allocate_module [] = I
haftmann@38970
   235
      | allocate_module (name_fragment :: name_fragments) =
haftmann@38970
   236
          ensure_module name_fragment
haftmann@55147
   237
          #> (apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content o allocate_module) name_fragments;
haftmann@52138
   238
    val empty_program =
haftmann@52138
   239
      empty_module
haftmann@55291
   240
      |> Symtab.fold (fn (_, fragments) => allocate_module fragments) module_namespace
haftmann@55147
   241
      |> Code_Symbol.Graph.fold (allocate_module o these o Option.map fst
haftmann@55291
   242
          o Code_Symbol.lookup identifiers o fst) program;
haftmann@38970
   243
haftmann@38970
   244
    (* distribute statements over hierarchy *)
haftmann@55147
   245
    fun add_stmt sym stmt =
haftmann@38970
   246
      let
haftmann@55147
   247
        val (name_fragments, base) = prep_sym sym;
haftmann@38970
   248
      in
haftmann@55680
   249
        (map_module name_fragments o apsnd)
haftmann@55681
   250
          (Code_Symbol.Graph.new_node (sym, (base, Stmt (Public, stmt))))
haftmann@38970
   251
      end;
haftmann@55680
   252
    fun add_edge_acyclic_error error_msg dep gr =
haftmann@55680
   253
      Code_Symbol.Graph.add_edge_acyclic dep gr
haftmann@55680
   254
        handle Graph.CYCLES _ => error (error_msg ())
haftmann@55292
   255
    fun add_dep sym sym' =
haftmann@38970
   256
      let
haftmann@55147
   257
        val (name_fragments, _) = prep_sym sym;
haftmann@55147
   258
        val (name_fragments', _) = prep_sym sym';
haftmann@38970
   259
        val (name_fragments_common, (diff, diff')) =
haftmann@38970
   260
          chop_prefix (op =) (name_fragments, name_fragments');
haftmann@55680
   261
        val is_cross_module = not (null diff andalso null diff');
haftmann@55147
   262
        val dep = pairself hd (map Code_Symbol.Module diff @ [sym], map Code_Symbol.Module diff' @ [sym']);
haftmann@55680
   263
        val add_edge = if is_cross_module andalso not cyclic_modules
haftmann@55680
   264
          then add_edge_acyclic_error (fn _ => "Dependency "
haftmann@55680
   265
            ^ Code_Symbol.quote ctxt sym ^ " -> "
haftmann@55680
   266
            ^ Code_Symbol.quote ctxt sym'
haftmann@55680
   267
            ^ " would result in module dependency cycle") dep
haftmann@55680
   268
          else Code_Symbol.Graph.add_edge dep;
haftmann@39018
   269
      in (map_module name_fragments_common o apsnd) add_edge end;
haftmann@55292
   270
    val proto_program = build_proto_program
haftmann@55292
   271
      { empty = empty_program, add_stmt = add_stmt, add_dep = add_dep } program;
haftmann@38970
   272
haftmann@39022
   273
    (* name declarations, data and statement modifications *)
haftmann@38970
   274
    fun make_declarations nsps (data, nodes) =
haftmann@38970
   275
      let
haftmann@55293
   276
        val (module_fragments, stmt_syms) =
haftmann@55293
   277
          Code_Symbol.Graph.keys nodes
haftmann@55293
   278
          |> List.partition
haftmann@55293
   279
              (fn sym => case Code_Symbol.Graph.get_node nodes sym
haftmann@55293
   280
                of (_, Module _) => true | _ => false)
haftmann@55293
   281
          |> pairself (prioritize sym_priority)
haftmann@55147
   282
        fun declare namify sym (nsps, nodes) =
haftmann@38970
   283
          let
haftmann@55147
   284
            val (base, node) = Code_Symbol.Graph.get_node nodes sym;
haftmann@38970
   285
            val (base', nsps') = namify node base nsps;
haftmann@55147
   286
            val nodes' = Code_Symbol.Graph.map_node sym (K (base', node)) nodes;
haftmann@38970
   287
          in (nsps', nodes') end;
haftmann@38970
   288
        val (nsps', nodes') = (nsps, nodes)
haftmann@39022
   289
          |> fold (declare (K namify_module)) module_fragments
haftmann@55679
   290
          |> fold (declare (namify_stmt o snd o the_stmt)) stmt_syms;
haftmann@55680
   291
        fun modify_stmts' syms_stmts =
haftmann@55605
   292
          let
haftmann@55680
   293
            val stmts' = modify_stmts syms_stmts
haftmann@55680
   294
          in stmts' @ replicate (length syms_stmts - length stmts') NONE end;
haftmann@55680
   295
        fun modify_stmts'' syms_exports_stmts =
haftmann@55680
   296
          syms_exports_stmts
haftmann@55680
   297
          |> map (fn (sym, (export, stmt)) => ((sym, stmt), export))
haftmann@55680
   298
          |> burrow_fst modify_stmts'
haftmann@55680
   299
          |> map (fn (SOME stmt, export) => SOME (export, stmt) | _ => NONE);
haftmann@55680
   300
        val nodes'' =
haftmann@55680
   301
          nodes'
haftmann@55680
   302
          |> Code_Symbol.Graph.map_strong_conn (map_module_stmts (make_declarations nsps') modify_stmts'');
haftmann@55147
   303
        val data' = fold memorize_data stmt_syms data;
haftmann@39018
   304
      in (data', nodes'') end;
haftmann@38970
   305
    val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
haftmann@38970
   306
haftmann@38970
   307
    (* deresolving *)
haftmann@55147
   308
    fun deresolver prefix_fragments sym =
haftmann@38970
   309
      let
haftmann@55147
   310
        val (name_fragments, _) = prep_sym sym;
haftmann@38970
   311
        val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
haftmann@55147
   312
        val nodes = fold (fn name_fragment => fn nodes => case Code_Symbol.Graph.get_node nodes (Code_Symbol.Module name_fragment)
haftmann@38970
   313
         of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
haftmann@55147
   314
        val (base', _) = Code_Symbol.Graph.get_node nodes sym;
haftmann@38970
   315
      in Long_Name.implode (remainder @ [base']) end
haftmann@55147
   316
        handle Code_Symbol.Graph.UNDEF _ => error ("Unknown statement name: "
haftmann@55147
   317
          ^ Code_Symbol.quote ctxt sym);
haftmann@38970
   318
haftmann@38970
   319
  in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
haftmann@38970
   320
haftmann@39147
   321
fun print_hierarchical { print_module, print_stmt, lift_markup } =
haftmann@39147
   322
  let
haftmann@39147
   323
    fun print_node _ (_, Dummy) =
haftmann@39147
   324
          NONE
haftmann@55147
   325
      | print_node prefix_fragments (sym, Stmt stmt) =
haftmann@55147
   326
          SOME (lift_markup (Code_Printer.markup_stmt sym)
haftmann@55147
   327
            (print_stmt prefix_fragments (sym, stmt)))
haftmann@55147
   328
      | print_node prefix_fragments (Code_Symbol.Module name_fragment, Module (data, nodes)) =
haftmann@39147
   329
          let
haftmann@39147
   330
            val prefix_fragments' = prefix_fragments @ [name_fragment]
haftmann@39147
   331
          in
haftmann@39147
   332
            Option.map (print_module prefix_fragments'
haftmann@39147
   333
              name_fragment data) (print_nodes prefix_fragments' nodes)
haftmann@39147
   334
          end
haftmann@39147
   335
    and print_nodes prefix_fragments nodes =
haftmann@39147
   336
      let
haftmann@55147
   337
        val xs = (map_filter (fn sym => print_node prefix_fragments
haftmann@55147
   338
          (sym, snd (Code_Symbol.Graph.get_node nodes sym))) o rev o flat o Code_Symbol.Graph.strong_conn) nodes
haftmann@39147
   339
      in if null xs then NONE else SOME xs end;
haftmann@39147
   340
  in these o print_nodes [] end;
haftmann@39147
   341
haftmann@55147
   342
end;