src/Tools/Code/code_scala.ML
changeset 50626 e21485358c56
parent 48568 084cd758a8ab
child 51143 0a2371e7ced3
equal deleted inserted replaced
50625:e3d25e751d05 50626:e21485358c56
    23 
    23 
    24 
    24 
    25 (** Scala serializer **)
    25 (** Scala serializer **)
    26 
    26 
    27 fun print_scala_stmt tyco_syntax const_syntax reserved
    27 fun print_scala_stmt tyco_syntax const_syntax reserved
    28     args_num is_singleton_constr (deresolve, deresolve_full) =
    28     args_num is_constr (deresolve, deresolve_full) =
    29   let
    29   let
    30     fun lookup_tyvar tyvars = lookup_var tyvars o first_upper;
    30     fun lookup_tyvar tyvars = lookup_var tyvars o first_upper;
    31     fun intro_tyvars vs = intro_vars (map (first_upper o fst) vs);
    31     fun intro_tyvars vs = intro_vars (map (first_upper o fst) vs);
    32     fun print_tyco_expr tyvars fxy (tyco, tys) = applify "[" "]"
    32     fun print_tyco_expr tyvars fxy (tyco, tys) = applify "[" "]"
    33           (print_typ tyvars NOBR) fxy ((str o deresolve) tyco) tys
    33           (print_typ tyvars NOBR) fxy ((str o deresolve) tyco) tys
    73             | NONE => print_case tyvars some_thm vars fxy case_expr)
    73             | NONE => print_case tyvars some_thm vars fxy case_expr)
    74     and print_app tyvars is_pat some_thm vars fxy
    74     and print_app tyvars is_pat some_thm vars fxy
    75         (app as ({ name = c, typargs, dom, ... }, ts)) =
    75         (app as ({ name = c, typargs, dom, ... }, ts)) =
    76       let
    76       let
    77         val k = length ts;
    77         val k = length ts;
    78         val typargs' = if is_pat orelse
    78         val typargs' = if is_pat then [] else typargs;
    79           (is_none (const_syntax c) andalso is_singleton_constr c) then [] else typargs;
       
    80         val (l, print') = case const_syntax c
    79         val (l, print') = case const_syntax c
    81          of NONE => (args_num c, fn fxy => fn ts => applify "(" ")"
    80          of NONE => (args_num c, fn fxy => fn ts => gen_applify (is_constr c ) "(" ")"
    82               (print_term tyvars is_pat some_thm vars NOBR) fxy
    81               (print_term tyvars is_pat some_thm vars NOBR) fxy
    83                 (applify "[" "]" (print_typ tyvars NOBR)
    82                 (applify "[" "]" (print_typ tyvars NOBR)
    84                   NOBR ((str o deresolve) c) typargs') ts)
    83                   NOBR ((str o deresolve) c) typargs') ts)
    85           | SOME (Plain_const_syntax (k, s)) => (k, fn fxy => fn ts => applify "(" ")"
    84           | SOME (Plain_const_syntax (k, s)) => (k, fn fxy => fn ts => applify "(" ")"
    86               (print_term tyvars is_pat some_thm vars NOBR) fxy
    85               (print_term tyvars is_pat some_thm vars NOBR) fxy
   204     fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) =
   203     fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) =
   205           print_def name (vs, ty) (filter (snd o snd) raw_eqs)
   204           print_def name (vs, ty) (filter (snd o snd) raw_eqs)
   206       | print_stmt (name, Code_Thingol.Datatype (_, (vs, cos))) =
   205       | print_stmt (name, Code_Thingol.Datatype (_, (vs, cos))) =
   207           let
   206           let
   208             val tyvars = intro_tyvars (map (rpair []) vs) reserved;
   207             val tyvars = intro_tyvars (map (rpair []) vs) reserved;
   209             fun print_co ((co, _), []) =
   208             fun print_co ((co, vs_args), tys) =
   210                   concat [str "final", str "case", str "object", (str o deresolve) co,
   209               concat [Pretty.block ((applify "[" "]" (str o lookup_tyvar tyvars) NOBR
   211                     str "extends", applify "[" "]" I NOBR ((str o deresolve) name)
   210                 ((concat o map str) ["final", "case", "class", deresolve co]) vs_args)
   212                       (replicate (length vs) (str "Nothing"))]
   211                 @@ enum "," "(" ")" (map (fn (v, arg) => constraint (str v) (print_typ tyvars NOBR arg))
   213               | print_co ((co, vs_args), tys) =
   212                   (Name.invent_names (snd reserved) "a" tys))),
   214                   concat [applify "(" ")"
   213                 str "extends",
   215                     (fn (v, arg) => constraint (str v) (print_typ tyvars NOBR arg)) NOBR
   214                 applify "[" "]" (str o lookup_tyvar tyvars) NOBR
   216                     (applify "[" "]" (str o lookup_tyvar tyvars) NOBR ((concat o map str)
   215                   ((str o deresolve) name) vs
   217                       ["final", "case", "class", deresolve co]) vs_args)
   216               ];
   218                     (Name.invent_names (snd reserved) "a" tys),
   217           in
   219                     str "extends",
   218             Pretty.chunks (applify "[" "]" (str o lookup_tyvar tyvars)
   220                     applify "[" "]" (str o lookup_tyvar tyvars) NOBR
       
   221                       ((str o deresolve) name) vs
       
   222                   ];
       
   223           in
       
   224             Pretty.chunks (applify "[" "]" (str o prefix "+" o lookup_tyvar tyvars)
       
   225               NOBR ((concat o map str) ["abstract", "sealed", "class", deresolve name]) vs
   219               NOBR ((concat o map str) ["abstract", "sealed", "class", deresolve name]) vs
   226                 :: map print_co cos)
   220                 :: map print_co cos)
   227           end
   221           end
   228       | print_stmt (name, Code_Thingol.Class (_, (v, (super_classes, classparams)))) =
   222       | print_stmt (name, Code_Thingol.Class (_, (v, (super_classes, classparams)))) =
   229           let
   223           let
   357       | Code_Thingol.Fun (_, ((_, ((ts, _), _) :: _), _)) => length ts
   351       | Code_Thingol.Fun (_, ((_, ((ts, _), _) :: _), _)) => length ts
   358       | Code_Thingol.Datatypecons (_, tyco) => length (lookup_constr tyco c)
   352       | Code_Thingol.Datatypecons (_, tyco) => length (lookup_constr tyco c)
   359       | Code_Thingol.Classparam (_, class) =>
   353       | Code_Thingol.Classparam (_, class) =>
   360           (length o fst o Code_Thingol.unfold_fun o the o AList.lookup (op =)
   354           (length o fst o Code_Thingol.unfold_fun o the o AList.lookup (op =)
   361             (classparams_of_class class)) c;
   355             (classparams_of_class class)) c;
   362     fun is_singleton_constr c = case Graph.get_node program c
       
   363      of Code_Thingol.Datatypecons (_, tyco) => null (lookup_constr tyco c)
       
   364       | _ => false;
       
   365     fun print_stmt prefix_fragments = print_scala_stmt
   356     fun print_stmt prefix_fragments = print_scala_stmt
   366       tyco_syntax const_syntax (make_vars reserved_syms) args_num
   357       tyco_syntax const_syntax (make_vars reserved_syms) args_num
   367       is_singleton_constr (deresolver prefix_fragments, deresolver []);
   358       (Code_Thingol.is_constr program) (deresolver prefix_fragments, deresolver []);
   368 
   359 
   369     (* print modules *)
   360     (* print modules *)
   370     fun print_implicit prefix_fragments implicit =
   361     fun print_implicit prefix_fragments implicit =
   371       let
   362       let
   372         val s = deresolver prefix_fragments implicit;
   363         val s = deresolver prefix_fragments implicit;