src/Tools/Code/code_namespace.ML
changeset 55684 ee49b4f7edc8
parent 55683 5732a55b9232
child 55776 7dd1971b39c1
     1.1 --- a/src/Tools/Code/code_namespace.ML	Sun Feb 23 10:33:43 2014 +0100
     1.2 +++ b/src/Tools/Code/code_namespace.ML	Sun Feb 23 10:33:43 2014 +0100
     1.3 @@ -9,6 +9,7 @@
     1.4    datatype export = Private | Opaque | Public
     1.5    val is_public: export -> bool
     1.6    val not_private: export -> bool
     1.7 +  val join_exports: export list -> export
     1.8  
     1.9    type flat_program
    1.10    val flat_program: Proof.context
    1.11 @@ -30,8 +31,10 @@
    1.12      reserved: Name.context, identifiers: Code_Target.identifier_data,
    1.13      empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    1.14      namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    1.15 -    cyclic_modules: bool, empty_data: 'b, memorize_data: Code_Symbol.T -> 'b -> 'b,
    1.16 -    modify_stmts: (Code_Symbol.T * Code_Thingol.stmt) list -> 'a option list }
    1.17 +    cyclic_modules: bool,
    1.18 +    class_transitive: bool, class_relation_public: bool,
    1.19 +    empty_data: 'b, memorize_data: Code_Symbol.T -> 'b -> 'b,
    1.20 +    modify_stmts: (Code_Symbol.T * (export * Code_Thingol.stmt)) list -> (export * 'a) option list }
    1.21        -> Code_Symbol.T list -> Code_Thingol.program
    1.22        -> { deresolver: string list -> Code_Symbol.T -> string,
    1.23             hierarchical_program: ('a, 'b) hierarchical_program }
    1.24 @@ -55,6 +58,94 @@
    1.25    | not_private Opaque = true
    1.26    | not_private _ = false;
    1.27  
    1.28 +fun mark_export Public _ = Public
    1.29 +  | mark_export _ Public = Public
    1.30 +  | mark_export Opaque _ = Opaque
    1.31 +  | mark_export _ Opaque = Opaque
    1.32 +  | mark_export _ _ = Private;
    1.33 +
    1.34 +fun join_exports exports = fold mark_export exports Private;
    1.35 +
    1.36 +fun dependent_exports { program = program, class_transitive = class_transitive } =
    1.37 +  let
    1.38 +    fun is_datatype_or_class (Code_Symbol.Type_Constructor _) = true
    1.39 +      | is_datatype_or_class (Code_Symbol.Type_Class _) = true
    1.40 +      | is_datatype_or_class _ = false;
    1.41 +    fun is_relevant (Code_Symbol.Class_Relation _) = true
    1.42 +      | is_relevant sym = is_datatype_or_class sym;
    1.43 +    val proto_gr = Code_Symbol.Graph.restrict is_relevant program;
    1.44 +    val gr =
    1.45 +      proto_gr
    1.46 +      |> Code_Symbol.Graph.fold
    1.47 +          (fn (sym, (_, (_, deps))) =>
    1.48 +            if is_relevant sym
    1.49 +            then I
    1.50 +            else
    1.51 +              Code_Symbol.Graph.new_node (sym, Code_Thingol.NoStmt)
    1.52 +              #> Code_Symbol.Graph.Keys.fold
    1.53 +               (fn sym' =>
    1.54 +                if is_relevant sym'
    1.55 +                then Code_Symbol.Graph.add_edge (sym, sym')
    1.56 +                else I) deps) program
    1.57 +      |> class_transitive ?
    1.58 +          Code_Symbol.Graph.fold (fn (sym as Code_Symbol.Type_Class _, _) =>
    1.59 +            fold (curry Code_Symbol.Graph.add_edge sym)
    1.60 +              ((remove (op =) sym o Code_Symbol.Graph.all_succs proto_gr) [sym]) | _ => I) proto_gr
    1.61 +    fun deps_of sym =
    1.62 +      let
    1.63 +        val succs = Code_Symbol.Graph.Keys.dest o Code_Symbol.Graph.imm_succs gr;
    1.64 +        val deps1 = succs sym;
    1.65 +        val deps2 = if class_transitive
    1.66 +          then []
    1.67 +          else [] |> fold (union (op =)) (map succs deps1) |> subtract (op =) deps1
    1.68 +      in (deps1, deps2) end;
    1.69 +  in
    1.70 +    { is_datatype_or_class = is_datatype_or_class,
    1.71 +      deps_of = deps_of }
    1.72 +  end;
    1.73 +
    1.74 +fun mark_exports_aux { program = program, prefix_of = prefix_of, map_export = map_export,
    1.75 +    is_datatype_or_class = is_datatype_or_class, deps_of = deps_of,
    1.76 +    class_relation_public = class_relation_public } prefix sym =
    1.77 +  let
    1.78 +    val export = (if is_datatype_or_class sym then Opaque else Public);
    1.79 +    val (dependent_export1, dependent_export2) =
    1.80 +      case Code_Symbol.Graph.get_node program sym of
    1.81 +          Code_Thingol.Fun _ => (SOME Opaque, NONE)
    1.82 +        | Code_Thingol.Classinst _ => (SOME Opaque, NONE)
    1.83 +        | Code_Thingol.Datatypecons _ => (SOME Public, SOME Opaque)
    1.84 +        | Code_Thingol.Classparam _ => (SOME Public, SOME Opaque)
    1.85 +        | Code_Thingol.Classrel _ =>
    1.86 +           (if class_relation_public
    1.87 +            then (SOME Public, SOME Opaque)
    1.88 +            else (SOME Opaque, NONE))
    1.89 +        | _ => (NONE, NONE);
    1.90 +    val dependent_exports =
    1.91 +      case dependent_export1 of
    1.92 +        SOME export1 => (case dependent_export2 of
    1.93 +          SOME export2 =>
    1.94 +            let
    1.95 +              val (deps1, deps2) = deps_of sym
    1.96 +            in map (rpair export1) deps1 @ map (rpair export2) deps2 end
    1.97 +        | NONE => map (rpair export1) (fst (deps_of sym)))
    1.98 +      | NONE => [];
    1.99 +  in 
   1.100 +    map_export prefix sym (mark_export export)
   1.101 +    #> fold (fn (sym, export) => map_export (prefix_of sym) sym (mark_export export))
   1.102 +      dependent_exports
   1.103 +  end;
   1.104 +
   1.105 +fun mark_exports { program = program, prefix_of = prefix_of, map_export = map_export,
   1.106 +    class_transitive = class_transitive, class_relation_public = class_relation_public } =
   1.107 +  let
   1.108 +    val { is_datatype_or_class, deps_of } =
   1.109 +      dependent_exports { program = program, class_transitive = class_transitive };
   1.110 +  in
   1.111 +    mark_exports_aux { program = program, prefix_of = prefix_of, map_export = map_export,
   1.112 +      is_datatype_or_class = is_datatype_or_class, deps_of = deps_of,
   1.113 +      class_relation_public = class_relation_public }
   1.114 +  end;
   1.115 +
   1.116  
   1.117  (** fundamental module name hierarchy **)
   1.118  
   1.119 @@ -113,13 +204,17 @@
   1.120      val sym_priority = has_priority identifiers;
   1.121  
   1.122      (* distribute statements over hierarchy *)
   1.123 +    val mark_exports = mark_exports { program = program, prefix_of = fst o prep_sym,
   1.124 +      map_export = fn module_name => fn sym =>
   1.125 +        Graph.map_node module_name o apfst o Code_Symbol.Graph.map_node sym o apsnd o apfst,
   1.126 +        class_transitive = false, class_relation_public = false };
   1.127      fun add_stmt sym stmt =
   1.128        let
   1.129          val (module_name, base) = prep_sym sym;
   1.130        in
   1.131          Graph.default_node (module_name, (Code_Symbol.Graph.empty, []))
   1.132          #> (Graph.map_node module_name o apfst)
   1.133 -          (Code_Symbol.Graph.new_node (sym, (base, (Public, stmt))))
   1.134 +          (Code_Symbol.Graph.new_node (sym, (base, (if null exports then Public else Private, stmt))))
   1.135        end;
   1.136      fun add_dep sym sym' =
   1.137        let
   1.138 @@ -129,9 +224,11 @@
   1.139          then (Graph.map_node module_name o apfst) (Code_Symbol.Graph.add_edge (sym, sym'))
   1.140          else (Graph.map_node module_name o apsnd)
   1.141            (AList.map_default (op =) (module_name', []) (insert (op =) sym'))
   1.142 +          #> mark_exports module_name' sym'
   1.143        end;
   1.144      val proto_program = build_proto_program
   1.145 -      { empty = Graph.empty, add_stmt = add_stmt, add_dep = add_dep } program;
   1.146 +      { empty = Graph.empty, add_stmt = add_stmt, add_dep = add_dep } program
   1.147 +      |> fold (fn sym => mark_exports ((fst o prep_sym) sym) sym) exports;
   1.148  
   1.149      (* name declarations and statement modifications *)
   1.150      fun declare sym (base, (_, stmt)) (gr, nsp) = 
   1.151 @@ -200,7 +297,7 @@
   1.152    let
   1.153      val some_modules =
   1.154        sym_base_nodes
   1.155 -      |> map (fn (sym, (base, Module content)) => SOME (base, content) | _ => NONE)
   1.156 +      |> map (fn (_, (base, Module content)) => SOME (base, content) | _ => NONE)
   1.157        |> (burrow_options o map o apsnd) f_module;
   1.158      val some_export_stmts =
   1.159        sym_base_nodes
   1.160 @@ -214,7 +311,9 @@
   1.161    end;
   1.162  
   1.163  fun hierarchical_program ctxt { module_name, reserved, identifiers, empty_nsp,
   1.164 -      namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts }
   1.165 +      namify_module, namify_stmt, cyclic_modules,
   1.166 +      class_transitive, class_relation_public,
   1.167 +      empty_data, memorize_data, modify_stmts }
   1.168        exports program =
   1.169    let
   1.170  
   1.171 @@ -242,12 +341,17 @@
   1.172            o Code_Symbol.lookup identifiers o fst) program;
   1.173  
   1.174      (* distribute statements over hierarchy *)
   1.175 +    val mark_exports = mark_exports { program = program, prefix_of = fst o prep_sym,
   1.176 +      map_export = fn name_fragments => fn sym => fn f =>
   1.177 +        (map_module name_fragments o apsnd o Code_Symbol.Graph.map_node sym o apsnd)
   1.178 +          (fn Stmt (export, stmt) => Stmt (f export, stmt)),
   1.179 +      class_transitive = class_transitive, class_relation_public = class_relation_public };
   1.180      fun add_stmt sym stmt =
   1.181        let
   1.182          val (name_fragments, base) = prep_sym sym;
   1.183        in
   1.184          (map_module name_fragments o apsnd)
   1.185 -          (Code_Symbol.Graph.new_node (sym, (base, Stmt (Public, stmt))))
   1.186 +          (Code_Symbol.Graph.new_node (sym, (base, Stmt (if null exports then Public else Private, stmt))))
   1.187        end;
   1.188      fun add_edge_acyclic_error error_msg dep gr =
   1.189        Code_Symbol.Graph.add_edge_acyclic dep gr
   1.190 @@ -266,9 +370,13 @@
   1.191              ^ Code_Symbol.quote ctxt sym'
   1.192              ^ " would result in module dependency cycle") dep
   1.193            else Code_Symbol.Graph.add_edge dep;
   1.194 -      in (map_module name_fragments_common o apsnd) add_edge end;
   1.195 +      in
   1.196 +        (map_module name_fragments_common o apsnd) add_edge
   1.197 +        #> (if is_cross_module then mark_exports name_fragments' sym' else I)
   1.198 +      end;
   1.199      val proto_program = build_proto_program
   1.200 -      { empty = empty_program, add_stmt = add_stmt, add_dep = add_dep } program;
   1.201 +      { empty = empty_program, add_stmt = add_stmt, add_dep = add_dep } program
   1.202 +      |> fold (fn sym => mark_exports ((fst o prep_sym) sym) sym) exports;
   1.203  
   1.204      (* name declarations, data and statement modifications *)
   1.205      fun make_declarations nsps (data, nodes) =
   1.206 @@ -292,14 +400,9 @@
   1.207            let
   1.208              val stmts' = modify_stmts syms_stmts
   1.209            in stmts' @ replicate (length syms_stmts - length stmts') NONE end;
   1.210 -        fun modify_stmts'' syms_exports_stmts =
   1.211 -          syms_exports_stmts
   1.212 -          |> map (fn (sym, (export, stmt)) => ((sym, stmt), export))
   1.213 -          |> burrow_fst modify_stmts'
   1.214 -          |> map (fn (SOME stmt, export) => SOME (export, stmt) | _ => NONE);
   1.215          val nodes'' =
   1.216            nodes'
   1.217 -          |> Code_Symbol.Graph.map_strong_conn (map_module_stmts (make_declarations nsps') modify_stmts'');
   1.218 +          |> Code_Symbol.Graph.map_strong_conn (map_module_stmts (make_declarations nsps') modify_stmts');
   1.219          val data' = fold memorize_data stmt_syms data;
   1.220        in (data', nodes'') end;
   1.221      val (_, hierarchical_program) = make_declarations empty_nsp proto_program;