preliminary implementation of hierarchical module name space
authorhaftmann
Wed, 25 Aug 2010 16:33:05 +0200
changeset 39002317e64c886d2
parent 39001 ecc713816e33
child 39003 1c70a502c590
preliminary implementation of hierarchical module name space
src/Tools/Code/code_scala.ML
     1.1 --- a/src/Tools/Code/code_scala.ML	Wed Aug 25 16:33:05 2010 +0200
     1.2 +++ b/src/Tools/Code/code_scala.ML	Wed Aug 25 16:33:05 2010 +0200
     1.3 @@ -135,7 +135,7 @@
     1.4      fun print_context tyvars vs name = applify "[" "]"
     1.5        (fn (v, sort) => (Pretty.block o map str)
     1.6          (lookup_tyvar tyvars v :: maps (fn sort => [": ", deresolve sort]) sort))
     1.7 -          NOBR ((str o deresolve) name) vs;
     1.8 +          NOBR ((str o deresolve_base) name) vs;
     1.9      fun print_defhead tyvars vars name vs params tys ty =
    1.10        Pretty.block [str "def ", constraint (applify "(" ")" (fn (param, ty) =>
    1.11          constraint ((str o lookup_var vars) param) (print_typ tyvars NOBR ty))
    1.12 @@ -194,7 +194,8 @@
    1.13                  str "match", str "{"], str "}")
    1.14                (map print_clause eqs)
    1.15            end;
    1.16 -    val print_method = (str o Library.enclose "`" "+`" o deresolve_base);
    1.17 +    val print_method = str o Library.enclose "`" "`" o space_implode "+"
    1.18 +      o fst o split_last o Long_Name.explode;
    1.19      fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) =
    1.20            print_def name (vs, ty) (filter (snd o snd) raw_eqs)
    1.21        | print_stmt (name, Code_Thingol.Datatype (_, (vs, cos))) =
    1.22 @@ -240,7 +241,7 @@
    1.23                in
    1.24                  concat [str "def", constraint (Pretty.block [applify "(" ")"
    1.25                    (fn (aux, ty) => constraint ((str o lookup_var vars) aux)
    1.26 -                  (print_typ tyvars NOBR ty)) NOBR (add_typarg (deresolve classparam))
    1.27 +                  (print_typ tyvars NOBR ty)) NOBR (add_typarg (deresolve_base classparam))
    1.28                    (auxs ~~ tys), str "(implicit ", str implicit_name, str ": ",
    1.29                    add_typarg (deresolve name), str ")"]) (print_typ tyvars NOBR ty), str "=",
    1.30                    applify "(" ")" (str o lookup_var vars) NOBR
    1.31 @@ -281,67 +282,143 @@
    1.32            end;
    1.33    in print_stmt end;
    1.34  
    1.35 +local
    1.36 +
    1.37 +(* hierarchical module name space *)
    1.38 +
    1.39 +datatype node =
    1.40 +    Dummy
    1.41 +  | Stmt of Code_Thingol.stmt
    1.42 +  | Module of ((Name.context * Name.context) * Name.context) * (string list * (string * node) Graph.T);
    1.43 +
    1.44 +in
    1.45 +
    1.46  fun scala_program_of_program labelled_name module_name reserved raw_module_alias program =
    1.47    let
    1.48 -    val the_module_name = the_default "Program" module_name;
    1.49 -    val module_alias = K (SOME the_module_name);
    1.50 -    val reserved = Name.make_context reserved;
    1.51 -    fun prepare_stmt (name, stmt) (nsps, stmts) =
    1.52 +
    1.53 +    (* building module name hierarchy *)
    1.54 +    val module_alias = if is_some module_name then K module_name else raw_module_alias;
    1.55 +    fun alias_fragments name = case module_alias name
    1.56 +     of SOME name' => Long_Name.explode name'
    1.57 +      | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
    1.58 +          (Long_Name.explode name);
    1.59 +    val module_names = Graph.fold (insert (op =) o fst o dest_name o fst) program [];
    1.60 +    val fragments_tab = fold (fn name => Symtab.update
    1.61 +      (name, alias_fragments name)) module_names Symtab.empty;
    1.62 +    val dest_name = Code_Printer.dest_name #>> (the o Symtab.lookup fragments_tab);
    1.63 +
    1.64 +    (* building empty module hierarchy *)
    1.65 +    val empty_module = (((reserved, reserved), reserved), ([], Graph.empty));
    1.66 +    fun map_module f (Module content) = Module (f content);
    1.67 +    fun declare_module name_fragement ((nsp_class, nsp_object), nsp_common) =
    1.68        let
    1.69 -        val (_, base) = Code_Printer.dest_name name;
    1.70 -        val mk_name_stmt = yield_singleton Name.variants;
    1.71 -        fun add_class ((nsp_class, nsp_object), nsp_common) =
    1.72 +        val declare = Name.declare name_fragement;
    1.73 +      in ((declare nsp_class, declare nsp_object), declare nsp_common) end;
    1.74 +    fun ensure_module name_fragement (nsps, (implicits, nodes)) =
    1.75 +      if can (Graph.get_node nodes) name_fragement then (nsps, (implicits, nodes))
    1.76 +      else
    1.77 +        (nsps |> declare_module name_fragement, (implicits,
    1.78 +          nodes |> Graph.new_node (name_fragement, (name_fragement, Module empty_module))));
    1.79 +    fun allocate_module [] = I
    1.80 +      | allocate_module (name_fragment :: name_fragments) =
    1.81 +          ensure_module name_fragment
    1.82 +          #> (apsnd o apsnd o Graph.map_node name_fragment o apsnd o map_module o allocate_module) name_fragments;
    1.83 +    val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
    1.84 +      fragments_tab empty_module;
    1.85 +    fun change_module [] = I
    1.86 +      | change_module (name_fragment :: name_fragments) =
    1.87 +          apsnd o apsnd o Graph.map_node name_fragment o apsnd o map_module
    1.88 +            o change_module name_fragments;
    1.89 +
    1.90 +    (* statement declaration *)
    1.91 +    fun namify_class base ((nsp_class, nsp_object), nsp_common) =
    1.92 +      let
    1.93 +        val (base', nsp_class') = yield_singleton Name.variants base nsp_class
    1.94 +      in (base', ((nsp_class', nsp_object), Name.declare base' nsp_common)) end;
    1.95 +    fun namify_object base ((nsp_class, nsp_object), nsp_common) =
    1.96 +      let
    1.97 +        val (base', nsp_object') = yield_singleton Name.variants base nsp_object
    1.98 +      in (base', ((nsp_class, nsp_object'), Name.declare base' nsp_common)) end;
    1.99 +    fun namify_common upper base ((nsp_class, nsp_object), nsp_common) =
   1.100 +      let
   1.101 +        val (base', nsp_common') =
   1.102 +          yield_singleton Name.variants (if upper then first_upper base else base) nsp_common
   1.103 +      in
   1.104 +        (base',
   1.105 +          ((Name.declare base' nsp_class, Name.declare base' nsp_object), nsp_common'))
   1.106 +      end;
   1.107 +    fun declare_stmt name stmt =
   1.108 +      let
   1.109 +        val (name_fragments, base) = dest_name name;
   1.110 +        val namify = case stmt
   1.111 +         of Code_Thingol.Fun _ => namify_object
   1.112 +          | Code_Thingol.Datatype _ => namify_class
   1.113 +          | Code_Thingol.Datatypecons _ => namify_common true
   1.114 +          | Code_Thingol.Class _ => namify_class
   1.115 +          | Code_Thingol.Classrel _ => namify_object
   1.116 +          | Code_Thingol.Classparam _ => namify_object
   1.117 +          | Code_Thingol.Classinst _ => namify_common false;
   1.118 +        val stmt' = case stmt
   1.119 +         of Code_Thingol.Datatypecons _ => Dummy
   1.120 +          | Code_Thingol.Classrel _ => Dummy
   1.121 +          | Code_Thingol.Classparam _ => Dummy
   1.122 +          | _ => Stmt stmt;
   1.123 +        fun is_classinst stmt = case stmt
   1.124 +         of Code_Thingol.Classinst _ => true
   1.125 +          | _ => false;
   1.126 +        val implicit_deps = filter (is_classinst o Graph.get_node program)
   1.127 +          (Graph.imm_succs program name);
   1.128 +        fun declaration (nsps, (implicits, nodes)) =
   1.129            let
   1.130 -            val (base', nsp_class') = mk_name_stmt base nsp_class
   1.131 -          in (base', ((nsp_class', nsp_object), Name.declare base' nsp_common)) end;
   1.132 -        fun add_object ((nsp_class, nsp_object), nsp_common) =
   1.133 -          let
   1.134 -            val (base', nsp_object') = mk_name_stmt base nsp_object
   1.135 -          in (base', ((nsp_class, nsp_object'), Name.declare base' nsp_common)) end;
   1.136 -        fun add_common upper ((nsp_class, nsp_object), nsp_common) =
   1.137 -          let
   1.138 -            val (base', nsp_common') =
   1.139 -              mk_name_stmt (if upper then first_upper base else base) nsp_common
   1.140 -          in
   1.141 -            (base',
   1.142 -              ((Name.declare base' nsp_class, Name.declare base' nsp_object), nsp_common'))
   1.143 -          end;
   1.144 -        val add_name = case stmt
   1.145 -         of Code_Thingol.Fun _ => add_object
   1.146 -          | Code_Thingol.Datatype _ => add_class
   1.147 -          | Code_Thingol.Datatypecons _ => add_common true
   1.148 -          | Code_Thingol.Class _ => add_class
   1.149 -          | Code_Thingol.Classrel _ => add_object
   1.150 -          | Code_Thingol.Classparam _ => add_object
   1.151 -          | Code_Thingol.Classinst _ => add_common false;
   1.152 -        fun add_stmt base' = case stmt
   1.153 -         of Code_Thingol.Datatypecons _ => cons (name, (base', NONE))
   1.154 -          | Code_Thingol.Classrel _ => cons (name, (base', NONE))
   1.155 -          | Code_Thingol.Classparam _ => cons (name, (base', NONE))
   1.156 -          | _ => cons (name, (base', SOME stmt));
   1.157 -      in
   1.158 -        nsps
   1.159 -        |> add_name
   1.160 -        |-> (fn base' => rpair (add_stmt base' stmts))
   1.161 -      end;
   1.162 -    val stmts = AList.make (Graph.get_node program) (Graph.strong_conn program |> flat)
   1.163 -      |> filter_out (Code_Thingol.is_case o snd);
   1.164 -    val (_, sca_program) = fold prepare_stmt stmts (((reserved, reserved), reserved), []);
   1.165 -    fun deresolver name = (fst o the o AList.lookup (op =) sca_program) name
   1.166 -      handle Option => error ("Unknown statement name: " ^ labelled_name name);
   1.167 -  in (deresolver, (the_module_name, sca_program)) end;
   1.168 +            val (base', nsps') = namify base nsps;
   1.169 +            val implicits' = union (op =) implicit_deps implicits;
   1.170 +            val nodes' = Graph.new_node (name, (base', stmt')) nodes;
   1.171 +          in (nsps', (implicits', nodes')) end;
   1.172 +      in change_module name_fragments declaration end;
   1.173 +
   1.174 +    (* dependencies *)
   1.175 +    fun add_dependency name name' =
   1.176 +      let
   1.177 +        val (name_fragments, base) = dest_name name;
   1.178 +        val (name_fragments', base') = dest_name name';
   1.179 +        val (name_fragments_common, (diff, diff')) =
   1.180 +          chop_prefix (op =) (name_fragments, name_fragments');
   1.181 +        val dep = if null diff then (name, name') else (hd diff, hd diff')
   1.182 +      in (change_module name_fragments_common o apsnd o apsnd) (Graph.add_edge dep) end;
   1.183 +
   1.184 +    (* producing program *)
   1.185 +    val (_, (_, sca_program)) = empty_program
   1.186 +      |> Graph.fold (fn (name, (stmt, _)) => declare_stmt name stmt) program
   1.187 +      |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
   1.188 +
   1.189 +    (* deresolving *)
   1.190 +    fun deresolve name =
   1.191 +      let
   1.192 +        val (name_fragments, _) = dest_name name;
   1.193 +        val nodes = fold (fn name_fragement => fn nodes => case Graph.get_node nodes name_fragement
   1.194 +         of (_, Module (_, (_, nodes))) => nodes) name_fragments sca_program;
   1.195 +        val (base', _) = Graph.get_node nodes name;
   1.196 +      in Long_Name.implode (name_fragments @ [base']) end
   1.197 +        handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   1.198 +
   1.199 +  in (deresolve, sca_program) end;
   1.200  
   1.201  fun serialize_scala raw_module_name labelled_name
   1.202      raw_reserved includes raw_module_alias
   1.203      _ syntax_tyco syntax_const (code_of_pretty, code_writeln)
   1.204      program stmt_names destination =
   1.205    let
   1.206 +
   1.207 +    (* generic nonsense *)
   1.208      val presentation_stmt_names = Code_Target.stmt_names_of_destination destination;
   1.209      val module_name = if null presentation_stmt_names then raw_module_name else SOME "Code";
   1.210 +
   1.211 +    (* preprocess program *)
   1.212      val reserved = fold (insert (op =) o fst) includes raw_reserved;
   1.213 -    val (deresolver, (the_module_name, sca_program)) = scala_program_of_program labelled_name
   1.214 -      module_name reserved raw_module_alias program;
   1.215 -    val reserved = make_vars reserved;
   1.216 +    val (deresolve, sca_program) = scala_program_of_program labelled_name
   1.217 +      module_name (Name.make_context reserved) raw_module_alias program;
   1.218 +
   1.219 +    (* print statements *)
   1.220      fun lookup_constr tyco constr = case Graph.get_node program tyco
   1.221       of Code_Thingol.Datatype (_, (_, constrs)) =>
   1.222            the (AList.lookup (op = o apsnd fst) constrs constr);
   1.223 @@ -359,44 +436,42 @@
   1.224       of Code_Thingol.Datatypecons (_, tyco) => null (lookup_constr tyco c)
   1.225        | _ => false;
   1.226      val print_stmt = print_scala_stmt labelled_name syntax_tyco syntax_const
   1.227 -      reserved args_num is_singleton_constr deresolver;
   1.228 -    fun print_module name imports content =
   1.229 -      (name, Pretty.chunks (
   1.230 -        str ("object " ^ name ^ " {")
   1.231 -        :: (if null imports then []
   1.232 -          else str "" :: map (fn name => str ("import " ^ name ^ "._")) imports)
   1.233 -        @ [str "",
   1.234 -        content,
   1.235 -        str "",
   1.236 -        str "}"]
   1.237 -      ));
   1.238 -    fun serialize_module the_module_name sca_program =
   1.239 -      let
   1.240 -        val content = Pretty.chunks2 (map_filter
   1.241 -          (fn (name, (_, SOME stmt)) => SOME (print_stmt (name, stmt))
   1.242 -            | (_, (_, NONE)) => NONE) sca_program);
   1.243 -      in print_module the_module_name (map fst includes) content end;
   1.244 -    fun check_destination destination =
   1.245 -      (File.check destination; destination);
   1.246 -    fun write_module destination (modlname, content) =
   1.247 -      let
   1.248 -        val filename = case modlname
   1.249 -         of "" => Path.explode "Main.scala"
   1.250 -          | _ => (Path.ext "scala" o Path.explode o implode o separate "/"
   1.251 -                o Long_Name.explode) modlname;
   1.252 -        val pathname = Path.append destination filename;
   1.253 -        val _ = File.mkdir_leaf (Path.dir pathname);
   1.254 -      in File.write pathname (code_of_pretty content) end
   1.255 +      (make_vars reserved) args_num is_singleton_constr deresolve;
   1.256 +
   1.257 +    (* print nodes *)
   1.258 +    fun print_implicits [] = NONE
   1.259 +      | print_implicits implicits = (SOME o Pretty.block)
   1.260 +          (str "import /*implicits*/" :: Pretty.brk 1 :: Pretty.commas (map (str o deresolve) implicits));
   1.261 +    fun print_module base implicits p = Pretty.chunks2
   1.262 +      ([str ("object " ^ base ^ " {")] @ the_list (print_implicits implicits)
   1.263 +        @ [p, str ("} /* object " ^ base ^ " */")]);
   1.264 +    fun print_node (_, Dummy) = NONE
   1.265 +      | print_node (name, Stmt stmt) = if not (not (null presentation_stmt_names)
   1.266 +          andalso member (op =) presentation_stmt_names name)
   1.267 +          then SOME (print_stmt (name, stmt))
   1.268 +          else NONE
   1.269 +      | print_node (name, Module (_, (implicits, nodes))) = if null presentation_stmt_names
   1.270 +          then case print_nodes nodes
   1.271 +           of NONE => NONE
   1.272 +            | SOME p => SOME (print_module (Long_Name.base_name name) implicits p)
   1.273 +          else print_nodes nodes
   1.274 +    and print_nodes nodes = let
   1.275 +        val ps = map_filter (fn name => print_node (name,
   1.276 +          snd (Graph.get_node nodes name)))
   1.277 +            ((rev o flat o Graph.strong_conn) nodes);
   1.278 +      in if null ps then NONE else SOME (Pretty.chunks2 ps) end;
   1.279 +
   1.280 +    (* serialization *)
   1.281 +    val p = Pretty.chunks2 (map (fn (base, p) => print_module base [] p) includes
   1.282 +      @ the_list (print_nodes sca_program));
   1.283    in
   1.284      Code_Target.mk_serialization target
   1.285 -      (fn NONE => K () o map (code_writeln o snd) | SOME file => K () o map
   1.286 -        (write_module (check_destination file)))
   1.287 -      (rpair [] o cat_lines o map (code_of_pretty o snd))
   1.288 -      (map (fn (name, content) => print_module name [] content) includes
   1.289 -        @| serialize_module the_module_name sca_program)
   1.290 -      destination
   1.291 +      (fn NONE => code_writeln | SOME file => File.write file o code_of_pretty)
   1.292 +      (rpair [] o code_of_pretty) p destination
   1.293    end;
   1.294  
   1.295 +end; (*local*)
   1.296 +
   1.297  val literals = let
   1.298    fun char_scala c = if c = "'" then "\\'"
   1.299      else if c = "\"" then "\\\""
   1.300 @@ -429,10 +504,10 @@
   1.301  val setup =
   1.302    Code_Target.add_target
   1.303      (target, { serializer = isar_serializer, literals = literals,
   1.304 -      check = { env_var = "SCALA_HOME", make_destination = I,
   1.305 +      check = { env_var = "SCALA_HOME", make_destination = fn p => Path.append p (Path.explode "ROOT.scala"),
   1.306          make_command = fn scala_home => fn p => fn _ =>
   1.307            "export JAVA_OPTS='-Xms128m -Xmx512m -Xss2m' && "
   1.308 -            ^ Path.implode (Path.append (Path.explode scala_home) (Path.explode "bin/scalac")) ^ " *.scala" } })
   1.309 +            ^ Path.implode (Path.append (Path.explode scala_home) (Path.explode "bin/scalac")) ^ " " ^ File.shell_path p } })
   1.310    #> Code_Target.add_syntax_tyco target "fun"
   1.311       (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
   1.312          brackify_infix (1, R) fxy (