src/Tools/Code/code_namespace.ML
changeset 55681 7714287dc044
parent 55680 bc5edc5dbf18
child 55683 5732a55b9232
equal deleted inserted replaced
55680:bc5edc5dbf18 55681:7714287dc044
     4 Mastering target language namespaces.
     4 Mastering target language namespaces.
     5 *)
     5 *)
     6 
     6 
     7 signature CODE_NAMESPACE =
     7 signature CODE_NAMESPACE =
     8 sig
     8 sig
       
     9   datatype export = Private | Opaque | Public
       
    10   val is_public: export -> bool
       
    11   val not_private: export -> bool
       
    12 
     9   type flat_program
    13   type flat_program
    10   val flat_program: Proof.context
    14   val flat_program: Proof.context
    11     -> { module_prefix: string, module_name: string,
    15     -> { module_prefix: string, module_name: string,
    12     reserved: Name.context, identifiers: Code_Target.identifier_data, empty_nsp: 'a,
    16     reserved: Name.context, identifiers: Code_Target.identifier_data, empty_nsp: 'a,
    13     namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
    17     namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
    16       -> { deresolver: string -> Code_Symbol.T -> string,
    20       -> { deresolver: string -> Code_Symbol.T -> string,
    17            flat_program: flat_program }
    21            flat_program: flat_program }
    18 
    22 
    19   datatype ('a, 'b) node =
    23   datatype ('a, 'b) node =
    20       Dummy
    24       Dummy
    21     | Stmt of bool * 'a
    25     | Stmt of export * 'a
    22     | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T)
    26     | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T)
    23   type ('a, 'b) hierarchical_program
    27   type ('a, 'b) hierarchical_program
    24   val hierarchical_program: Proof.context
    28   val hierarchical_program: Proof.context
    25     -> { module_name: string,
    29     -> { module_name: string,
    26     reserved: Name.context, identifiers: Code_Target.identifier_data,
    30     reserved: Name.context, identifiers: Code_Target.identifier_data,
    30     modify_stmts: (Code_Symbol.T * Code_Thingol.stmt) list -> 'a option list }
    34     modify_stmts: (Code_Symbol.T * Code_Thingol.stmt) list -> 'a option list }
    31       -> Code_Thingol.program
    35       -> Code_Thingol.program
    32       -> { deresolver: string list -> Code_Symbol.T -> string,
    36       -> { deresolver: string list -> Code_Symbol.T -> string,
    33            hierarchical_program: ('a, 'b) hierarchical_program }
    37            hierarchical_program: ('a, 'b) hierarchical_program }
    34   val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
    38   val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
    35     print_stmt: string list -> Code_Symbol.T * (bool * 'a) -> 'c,
    39     print_stmt: string list -> Code_Symbol.T * (export * 'a) -> 'c,
    36     lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
    40     lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
    37       -> ('a, 'b) hierarchical_program -> 'c list
    41       -> ('a, 'b) hierarchical_program -> 'c list
    38 end;
    42 end;
    39 
    43 
    40 structure Code_Namespace : CODE_NAMESPACE =
    44 structure Code_Namespace : CODE_NAMESPACE =
    41 struct
    45 struct
       
    46 
       
    47 (** export **)
       
    48 
       
    49 datatype export = Private | Opaque | Public;
       
    50 
       
    51 fun is_public Public = true
       
    52   | is_public _ = false;
       
    53 
       
    54 fun not_private Public = true
       
    55   | not_private Opaque = true
       
    56   | not_private _ = false;
       
    57 
    42 
    58 
    43 (** fundamental module name hierarchy **)
    59 (** fundamental module name hierarchy **)
    44 
    60 
    45 fun module_fragments' { identifiers, reserved } name =
    61 fun module_fragments' { identifiers, reserved } name =
    46   case Code_Symbol.lookup_module_data identifiers name of
    62   case Code_Symbol.lookup_module_data identifiers name of
    80 fun prioritize has_priority = uncurry append o List.partition has_priority;
    96 fun prioritize has_priority = uncurry append o List.partition has_priority;
    81 
    97 
    82 
    98 
    83 (** flat program structure **)
    99 (** flat program structure **)
    84 
   100 
    85 type flat_program = ((string * (bool * Code_Thingol.stmt) option) Code_Symbol.Graph.T * (string * Code_Symbol.T list) list) Graph.T;
   101 type flat_program = ((string * (export * Code_Thingol.stmt) option) Code_Symbol.Graph.T * (string * Code_Symbol.T list) list) Graph.T;
    86 
   102 
    87 fun flat_program ctxt { module_prefix, module_name, reserved,
   103 fun flat_program ctxt { module_prefix, module_name, reserved,
    88     identifiers, empty_nsp, namify_stmt, modify_stmt } program =
   104     identifiers, empty_nsp, namify_stmt, modify_stmt } program =
    89   let
   105   let
    90 
   106 
   101       let
   117       let
   102         val (module_name, base) = prep_sym sym;
   118         val (module_name, base) = prep_sym sym;
   103       in
   119       in
   104         Graph.default_node (module_name, (Code_Symbol.Graph.empty, []))
   120         Graph.default_node (module_name, (Code_Symbol.Graph.empty, []))
   105         #> (Graph.map_node module_name o apfst)
   121         #> (Graph.map_node module_name o apfst)
   106           (Code_Symbol.Graph.new_node (sym, (base, (true, stmt))))
   122           (Code_Symbol.Graph.new_node (sym, (base, (Public, stmt))))
   107       end;
   123       end;
   108     fun add_dep sym sym' =
   124     fun add_dep sym sym' =
   109       let
   125       let
   110         val (module_name, _) = prep_sym sym;
   126         val (module_name, _) = prep_sym sym;
   111         val (module_name', _) = prep_sym sym';
   127         val (module_name', _) = prep_sym sym';
   164 
   180 
   165 (** hierarchical program structure **)
   181 (** hierarchical program structure **)
   166 
   182 
   167 datatype ('a, 'b) node =
   183 datatype ('a, 'b) node =
   168     Dummy
   184     Dummy
   169   | Stmt of bool * 'a
   185   | Stmt of export * 'a
   170   | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T);
   186   | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T);
   171 
   187 
   172 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Code_Symbol.Graph.T;
   188 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Code_Symbol.Graph.T;
   173 
   189 
   174 fun the_stmt (Stmt (export, stmt)) = (export, stmt);
   190 fun the_stmt (Stmt (export, stmt)) = (export, stmt);
   228     fun add_stmt sym stmt =
   244     fun add_stmt sym stmt =
   229       let
   245       let
   230         val (name_fragments, base) = prep_sym sym;
   246         val (name_fragments, base) = prep_sym sym;
   231       in
   247       in
   232         (map_module name_fragments o apsnd)
   248         (map_module name_fragments o apsnd)
   233           (Code_Symbol.Graph.new_node (sym, (base, Stmt (true, stmt))))
   249           (Code_Symbol.Graph.new_node (sym, (base, Stmt (Public, stmt))))
   234       end;
   250       end;
   235     fun add_edge_acyclic_error error_msg dep gr =
   251     fun add_edge_acyclic_error error_msg dep gr =
   236       Code_Symbol.Graph.add_edge_acyclic dep gr
   252       Code_Symbol.Graph.add_edge_acyclic dep gr
   237         handle Graph.CYCLES _ => error (error_msg ())
   253         handle Graph.CYCLES _ => error (error_msg ())
   238     fun add_dep sym sym' =
   254     fun add_dep sym sym' =