1.1 --- a/src/Tools/Code/code_haskell.ML Wed Aug 25 18:46:22 2010 +0200
1.2 +++ b/src/Tools/Code/code_haskell.ML Wed Aug 25 22:47:04 2010 +0200
1.3 @@ -344,11 +344,9 @@
1.4 contr_classparam_typs
1.5 (if string_classes then deriving_show else K false);
1.6 fun print_module name content =
1.7 - (name, Pretty.chunks [
1.8 + (name, Pretty.chunks2 [
1.9 str ("module " ^ name ^ " where {"),
1.10 - str "",
1.11 content,
1.12 - str "",
1.13 str "}"
1.14 ]);
1.15 fun serialize_module1 (module_name', (deps, (stmts, _))) =
2.1 --- a/src/Tools/Code/code_scala.ML Wed Aug 25 18:46:22 2010 +0200
2.2 +++ b/src/Tools/Code/code_scala.ML Wed Aug 25 22:47:04 2010 +0200
2.3 @@ -135,7 +135,7 @@
2.4 fun print_context tyvars vs name = applify "[" "]"
2.5 (fn (v, sort) => (Pretty.block o map str)
2.6 (lookup_tyvar tyvars v :: maps (fn sort => [": ", deresolve sort]) sort))
2.7 - NOBR ((str o deresolve) name) vs;
2.8 + NOBR ((str o deresolve_base) name) vs;
2.9 fun print_defhead tyvars vars name vs params tys ty =
2.10 Pretty.block [str "def ", constraint (applify "(" ")" (fn (param, ty) =>
2.11 constraint ((str o lookup_var vars) param) (print_typ tyvars NOBR ty))
2.12 @@ -194,7 +194,8 @@
2.13 str "match", str "{"], str "}")
2.14 (map print_clause eqs)
2.15 end;
2.16 - val print_method = (str o Library.enclose "`" "+`" o deresolve_base);
2.17 + val print_method = str o Library.enclose "`" "`" o space_implode "+"
2.18 + o fst o split_last o Long_Name.explode;
2.19 fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) =
2.20 print_def name (vs, ty) (filter (snd o snd) raw_eqs)
2.21 | print_stmt (name, Code_Thingol.Datatype (_, (vs, cos))) =
2.22 @@ -240,7 +241,7 @@
2.23 in
2.24 concat [str "def", constraint (Pretty.block [applify "(" ")"
2.25 (fn (aux, ty) => constraint ((str o lookup_var vars) aux)
2.26 - (print_typ tyvars NOBR ty)) NOBR (add_typarg (deresolve classparam))
2.27 + (print_typ tyvars NOBR ty)) NOBR (add_typarg (deresolve_base classparam))
2.28 (auxs ~~ tys), str "(implicit ", str implicit_name, str ": ",
2.29 add_typarg (deresolve name), str ")"]) (print_typ tyvars NOBR ty), str "=",
2.30 applify "(" ")" (str o lookup_var vars) NOBR
2.31 @@ -281,67 +282,143 @@
2.32 end;
2.33 in print_stmt end;
2.34
2.35 +local
2.36 +
2.37 +(* hierarchical module name space *)
2.38 +
2.39 +datatype node =
2.40 + Dummy
2.41 + | Stmt of Code_Thingol.stmt
2.42 + | Module of ((Name.context * Name.context) * Name.context) * (string list * (string * node) Graph.T);
2.43 +
2.44 +in
2.45 +
2.46 fun scala_program_of_program labelled_name module_name reserved raw_module_alias program =
2.47 let
2.48 - val the_module_name = the_default "Program" module_name;
2.49 - val module_alias = K (SOME the_module_name);
2.50 - val reserved = Name.make_context reserved;
2.51 - fun prepare_stmt (name, stmt) (nsps, stmts) =
2.52 +
2.53 + (* building module name hierarchy *)
2.54 + val module_alias = if is_some module_name then K module_name else raw_module_alias;
2.55 + fun alias_fragments name = case module_alias name
2.56 + of SOME name' => Long_Name.explode name'
2.57 + | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
2.58 + (Long_Name.explode name);
2.59 + val module_names = Graph.fold (insert (op =) o fst o dest_name o fst) program [];
2.60 + val fragments_tab = fold (fn name => Symtab.update
2.61 + (name, alias_fragments name)) module_names Symtab.empty;
2.62 + val dest_name = Code_Printer.dest_name #>> (the o Symtab.lookup fragments_tab);
2.63 +
2.64 + (* building empty module hierarchy *)
2.65 + val empty_module = (((reserved, reserved), reserved), ([], Graph.empty));
2.66 + fun map_module f (Module content) = Module (f content);
2.67 + fun declare_module name_fragement ((nsp_class, nsp_object), nsp_common) =
2.68 let
2.69 - val (_, base) = Code_Printer.dest_name name;
2.70 - val mk_name_stmt = yield_singleton Name.variants;
2.71 - fun add_class ((nsp_class, nsp_object), nsp_common) =
2.72 + val declare = Name.declare name_fragement;
2.73 + in ((declare nsp_class, declare nsp_object), declare nsp_common) end;
2.74 + fun ensure_module name_fragement (nsps, (implicits, nodes)) =
2.75 + if can (Graph.get_node nodes) name_fragement then (nsps, (implicits, nodes))
2.76 + else
2.77 + (nsps |> declare_module name_fragement, (implicits,
2.78 + nodes |> Graph.new_node (name_fragement, (name_fragement, Module empty_module))));
2.79 + fun allocate_module [] = I
2.80 + | allocate_module (name_fragment :: name_fragments) =
2.81 + ensure_module name_fragment
2.82 + #> (apsnd o apsnd o Graph.map_node name_fragment o apsnd o map_module o allocate_module) name_fragments;
2.83 + val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
2.84 + fragments_tab empty_module;
2.85 + fun change_module [] = I
2.86 + | change_module (name_fragment :: name_fragments) =
2.87 + apsnd o apsnd o Graph.map_node name_fragment o apsnd o map_module
2.88 + o change_module name_fragments;
2.89 +
2.90 + (* statement declaration *)
2.91 + fun namify_class base ((nsp_class, nsp_object), nsp_common) =
2.92 + let
2.93 + val (base', nsp_class') = yield_singleton Name.variants base nsp_class
2.94 + in (base', ((nsp_class', nsp_object), Name.declare base' nsp_common)) end;
2.95 + fun namify_object base ((nsp_class, nsp_object), nsp_common) =
2.96 + let
2.97 + val (base', nsp_object') = yield_singleton Name.variants base nsp_object
2.98 + in (base', ((nsp_class, nsp_object'), Name.declare base' nsp_common)) end;
2.99 + fun namify_common upper base ((nsp_class, nsp_object), nsp_common) =
2.100 + let
2.101 + val (base', nsp_common') =
2.102 + yield_singleton Name.variants (if upper then first_upper base else base) nsp_common
2.103 + in
2.104 + (base',
2.105 + ((Name.declare base' nsp_class, Name.declare base' nsp_object), nsp_common'))
2.106 + end;
2.107 + fun declare_stmt name stmt =
2.108 + let
2.109 + val (name_fragments, base) = dest_name name;
2.110 + val namify = case stmt
2.111 + of Code_Thingol.Fun _ => namify_object
2.112 + | Code_Thingol.Datatype _ => namify_class
2.113 + | Code_Thingol.Datatypecons _ => namify_common true
2.114 + | Code_Thingol.Class _ => namify_class
2.115 + | Code_Thingol.Classrel _ => namify_object
2.116 + | Code_Thingol.Classparam _ => namify_object
2.117 + | Code_Thingol.Classinst _ => namify_common false;
2.118 + val stmt' = case stmt
2.119 + of Code_Thingol.Datatypecons _ => Dummy
2.120 + | Code_Thingol.Classrel _ => Dummy
2.121 + | Code_Thingol.Classparam _ => Dummy
2.122 + | _ => Stmt stmt;
2.123 + fun is_classinst stmt = case stmt
2.124 + of Code_Thingol.Classinst _ => true
2.125 + | _ => false;
2.126 + val implicit_deps = filter (is_classinst o Graph.get_node program)
2.127 + (Graph.imm_succs program name);
2.128 + fun declaration (nsps, (implicits, nodes)) =
2.129 let
2.130 - val (base', nsp_class') = mk_name_stmt base nsp_class
2.131 - in (base', ((nsp_class', nsp_object), Name.declare base' nsp_common)) end;
2.132 - fun add_object ((nsp_class, nsp_object), nsp_common) =
2.133 - let
2.134 - val (base', nsp_object') = mk_name_stmt base nsp_object
2.135 - in (base', ((nsp_class, nsp_object'), Name.declare base' nsp_common)) end;
2.136 - fun add_common upper ((nsp_class, nsp_object), nsp_common) =
2.137 - let
2.138 - val (base', nsp_common') =
2.139 - mk_name_stmt (if upper then first_upper base else base) nsp_common
2.140 - in
2.141 - (base',
2.142 - ((Name.declare base' nsp_class, Name.declare base' nsp_object), nsp_common'))
2.143 - end;
2.144 - val add_name = case stmt
2.145 - of Code_Thingol.Fun _ => add_object
2.146 - | Code_Thingol.Datatype _ => add_class
2.147 - | Code_Thingol.Datatypecons _ => add_common true
2.148 - | Code_Thingol.Class _ => add_class
2.149 - | Code_Thingol.Classrel _ => add_object
2.150 - | Code_Thingol.Classparam _ => add_object
2.151 - | Code_Thingol.Classinst _ => add_common false;
2.152 - fun add_stmt base' = case stmt
2.153 - of Code_Thingol.Datatypecons _ => cons (name, (base', NONE))
2.154 - | Code_Thingol.Classrel _ => cons (name, (base', NONE))
2.155 - | Code_Thingol.Classparam _ => cons (name, (base', NONE))
2.156 - | _ => cons (name, (base', SOME stmt));
2.157 - in
2.158 - nsps
2.159 - |> add_name
2.160 - |-> (fn base' => rpair (add_stmt base' stmts))
2.161 - end;
2.162 - val stmts = AList.make (Graph.get_node program) (Graph.strong_conn program |> flat)
2.163 - |> filter_out (Code_Thingol.is_case o snd);
2.164 - val (_, sca_program) = fold prepare_stmt stmts (((reserved, reserved), reserved), []);
2.165 - fun deresolver name = (fst o the o AList.lookup (op =) sca_program) name
2.166 - handle Option => error ("Unknown statement name: " ^ labelled_name name);
2.167 - in (deresolver, (the_module_name, sca_program)) end;
2.168 + val (base', nsps') = namify base nsps;
2.169 + val implicits' = union (op =) implicit_deps implicits;
2.170 + val nodes' = Graph.new_node (name, (base', stmt')) nodes;
2.171 + in (nsps', (implicits', nodes')) end;
2.172 + in change_module name_fragments declaration end;
2.173 +
2.174 + (* dependencies *)
2.175 + fun add_dependency name name' =
2.176 + let
2.177 + val (name_fragments, base) = dest_name name;
2.178 + val (name_fragments', base') = dest_name name';
2.179 + val (name_fragments_common, (diff, diff')) =
2.180 + chop_prefix (op =) (name_fragments, name_fragments');
2.181 + val dep = if null diff then (name, name') else (hd diff, hd diff')
2.182 + in (change_module name_fragments_common o apsnd o apsnd) (Graph.add_edge dep) end;
2.183 +
2.184 + (* producing program *)
2.185 + val (_, (_, sca_program)) = empty_program
2.186 + |> Graph.fold (fn (name, (stmt, _)) => declare_stmt name stmt) program
2.187 + |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
2.188 +
2.189 + (* deresolving *)
2.190 + fun deresolve name =
2.191 + let
2.192 + val (name_fragments, _) = dest_name name;
2.193 + val nodes = fold (fn name_fragement => fn nodes => case Graph.get_node nodes name_fragement
2.194 + of (_, Module (_, (_, nodes))) => nodes) name_fragments sca_program;
2.195 + val (base', _) = Graph.get_node nodes name;
2.196 + in Long_Name.implode (name_fragments @ [base']) end
2.197 + handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
2.198 +
2.199 + in (deresolve, sca_program) end;
2.200
2.201 fun serialize_scala raw_module_name labelled_name
2.202 raw_reserved includes raw_module_alias
2.203 _ syntax_tyco syntax_const (code_of_pretty, code_writeln)
2.204 program stmt_names destination =
2.205 let
2.206 +
2.207 + (* generic nonsense *)
2.208 val presentation_stmt_names = Code_Target.stmt_names_of_destination destination;
2.209 val module_name = if null presentation_stmt_names then raw_module_name else SOME "Code";
2.210 +
2.211 + (* preprocess program *)
2.212 val reserved = fold (insert (op =) o fst) includes raw_reserved;
2.213 - val (deresolver, (the_module_name, sca_program)) = scala_program_of_program labelled_name
2.214 - module_name reserved raw_module_alias program;
2.215 - val reserved = make_vars reserved;
2.216 + val (deresolve, sca_program) = scala_program_of_program labelled_name
2.217 + module_name (Name.make_context reserved) raw_module_alias program;
2.218 +
2.219 + (* print statements *)
2.220 fun lookup_constr tyco constr = case Graph.get_node program tyco
2.221 of Code_Thingol.Datatype (_, (_, constrs)) =>
2.222 the (AList.lookup (op = o apsnd fst) constrs constr);
2.223 @@ -359,44 +436,42 @@
2.224 of Code_Thingol.Datatypecons (_, tyco) => null (lookup_constr tyco c)
2.225 | _ => false;
2.226 val print_stmt = print_scala_stmt labelled_name syntax_tyco syntax_const
2.227 - reserved args_num is_singleton_constr deresolver;
2.228 - fun print_module name imports content =
2.229 - (name, Pretty.chunks (
2.230 - str ("object " ^ name ^ " {")
2.231 - :: (if null imports then []
2.232 - else str "" :: map (fn name => str ("import " ^ name ^ "._")) imports)
2.233 - @ [str "",
2.234 - content,
2.235 - str "",
2.236 - str "}"]
2.237 - ));
2.238 - fun serialize_module the_module_name sca_program =
2.239 - let
2.240 - val content = Pretty.chunks2 (map_filter
2.241 - (fn (name, (_, SOME stmt)) => SOME (print_stmt (name, stmt))
2.242 - | (_, (_, NONE)) => NONE) sca_program);
2.243 - in print_module the_module_name (map fst includes) content end;
2.244 - fun check_destination destination =
2.245 - (File.check destination; destination);
2.246 - fun write_module destination (modlname, content) =
2.247 - let
2.248 - val filename = case modlname
2.249 - of "" => Path.explode "Main.scala"
2.250 - | _ => (Path.ext "scala" o Path.explode o implode o separate "/"
2.251 - o Long_Name.explode) modlname;
2.252 - val pathname = Path.append destination filename;
2.253 - val _ = File.mkdir_leaf (Path.dir pathname);
2.254 - in File.write pathname (code_of_pretty content) end
2.255 + (make_vars reserved) args_num is_singleton_constr deresolve;
2.256 +
2.257 + (* print nodes *)
2.258 + fun print_implicits [] = NONE
2.259 + | print_implicits implicits = (SOME o Pretty.block)
2.260 + (str "import /*implicits*/" :: Pretty.brk 1 :: Pretty.commas (map (str o deresolve) implicits));
2.261 + fun print_module base implicits p = Pretty.chunks2
2.262 + ([str ("object " ^ base ^ " {")] @ the_list (print_implicits implicits)
2.263 + @ [p, str ("} /* object " ^ base ^ " */")]);
2.264 + fun print_node (_, Dummy) = NONE
2.265 + | print_node (name, Stmt stmt) = if not (not (null presentation_stmt_names)
2.266 + andalso member (op =) presentation_stmt_names name)
2.267 + then SOME (print_stmt (name, stmt))
2.268 + else NONE
2.269 + | print_node (name, Module (_, (implicits, nodes))) = if null presentation_stmt_names
2.270 + then case print_nodes nodes
2.271 + of NONE => NONE
2.272 + | SOME p => SOME (print_module (Long_Name.base_name name) implicits p)
2.273 + else print_nodes nodes
2.274 + and print_nodes nodes = let
2.275 + val ps = map_filter (fn name => print_node (name,
2.276 + snd (Graph.get_node nodes name)))
2.277 + ((rev o flat o Graph.strong_conn) nodes);
2.278 + in if null ps then NONE else SOME (Pretty.chunks2 ps) end;
2.279 +
2.280 + (* serialization *)
2.281 + val p = Pretty.chunks2 (map (fn (base, p) => print_module base [] p) includes
2.282 + @ the_list (print_nodes sca_program));
2.283 in
2.284 Code_Target.mk_serialization target
2.285 - (fn NONE => K () o map (code_writeln o snd) | SOME file => K () o map
2.286 - (write_module (check_destination file)))
2.287 - (rpair [] o cat_lines o map (code_of_pretty o snd))
2.288 - (map (fn (name, content) => print_module name [] content) includes
2.289 - @| serialize_module the_module_name sca_program)
2.290 - destination
2.291 + (fn NONE => code_writeln | SOME file => File.write file o code_of_pretty)
2.292 + (rpair [] o code_of_pretty) p destination
2.293 end;
2.294
2.295 +end; (*local*)
2.296 +
2.297 val literals = let
2.298 fun char_scala c = if c = "'" then "\\'"
2.299 else if c = "\"" then "\\\""
2.300 @@ -429,10 +504,10 @@
2.301 val setup =
2.302 Code_Target.add_target
2.303 (target, { serializer = isar_serializer, literals = literals,
2.304 - check = { env_var = "SCALA_HOME", make_destination = I,
2.305 + check = { env_var = "SCALA_HOME", make_destination = fn p => Path.append p (Path.explode "ROOT.scala"),
2.306 make_command = fn scala_home => fn p => fn _ =>
2.307 "export JAVA_OPTS='-Xms128m -Xmx512m -Xss2m' && "
2.308 - ^ Path.implode (Path.append (Path.explode scala_home) (Path.explode "bin/scalac")) ^ " *.scala" } })
2.309 + ^ Path.implode (Path.append (Path.explode scala_home) (Path.explode "bin/scalac")) ^ " " ^ File.shell_path p } })
2.310 #> Code_Target.add_syntax_tyco target "fun"
2.311 (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
2.312 brackify_infix (1, R) fxy (