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