src/Tools/Code/code_scala.ML
changeset 82380 ceb4f33d3073
parent 80086 1b986e5f9764
child 82447 741f6f6df144
equal deleted inserted replaced
82379:3f875966c3e1 82380:ceb4f33d3073
    55     val deresolve_tyco = deresolve o Type_Constructor;
    55     val deresolve_tyco = deresolve o Type_Constructor;
    56     val deresolve_class = deresolve o Type_Class;
    56     val deresolve_class = deresolve o Type_Class;
    57     fun lookup_tyvar tyvars = lookup_var tyvars o Name.enforce_case true;
    57     fun lookup_tyvar tyvars = lookup_var tyvars o Name.enforce_case true;
    58     fun intro_tyvars vs = intro_vars (map (Name.enforce_case true o fst) vs);
    58     fun intro_tyvars vs = intro_vars (map (Name.enforce_case true o fst) vs);
    59     fun print_tyco_expr tyvars fxy (sym, tys) = applify "[" "]"
    59     fun print_tyco_expr tyvars fxy (sym, tys) = applify "[" "]"
    60           (print_typ tyvars NOBR) fxy ((str o deresolve) sym) tys
    60           (print_typ tyvars NOBR) fxy ((Pretty.str o deresolve) sym) tys
    61     and print_typ tyvars fxy (tyco `%% tys) = (case tyco_syntax tyco
    61     and print_typ tyvars fxy (tyco `%% tys) = (case tyco_syntax tyco
    62          of NONE => print_tyco_expr tyvars fxy (Type_Constructor tyco, tys)
    62          of NONE => print_tyco_expr tyvars fxy (Type_Constructor tyco, tys)
    63           | SOME (_, print) => print (print_typ tyvars) fxy tys)
    63           | SOME (_, print) => print (print_typ tyvars) fxy tys)
    64       | print_typ tyvars fxy (ITyVar v) = (str o lookup_tyvar tyvars) v;
    64       | print_typ tyvars fxy (ITyVar v) = (Pretty.str o lookup_tyvar tyvars) v;
    65     fun print_dicttyp tyvars (class, ty) = print_tyco_expr tyvars NOBR (Type_Class class, [ty]);
    65     fun print_dicttyp tyvars (class, ty) = print_tyco_expr tyvars NOBR (Type_Class class, [ty]);
    66     fun print_tupled_typ tyvars ([], ty) =
    66     fun print_tupled_typ tyvars ([], ty) =
    67           print_typ tyvars NOBR ty
    67           print_typ tyvars NOBR ty
    68       | print_tupled_typ tyvars ([ty1], ty2) =
    68       | print_tupled_typ tyvars ([ty1], ty2) =
    69           concat [print_typ tyvars BR ty1, str "=>", print_typ tyvars NOBR ty2]
    69           concat [print_typ tyvars BR ty1, Pretty.str "=>", print_typ tyvars NOBR ty2]
    70       | print_tupled_typ tyvars (tys, ty) =
    70       | print_tupled_typ tyvars (tys, ty) =
    71           concat [enum "," "(" ")" (map (print_typ tyvars NOBR) tys),
    71           concat [Pretty.enum "," "(" ")" (map (print_typ tyvars NOBR) tys),
    72             str "=>", print_typ tyvars NOBR ty];
    72             Pretty.str "=>", print_typ tyvars NOBR ty];
    73     fun constraint p1 p2 = Pretty.block [p1, str " : ", p2];
    73     fun constraint p1 p2 = Pretty.block [p1, Pretty.str " : ", p2];
    74     fun print_var vars NONE = str "_"
    74     fun print_var vars NONE = Pretty.str "_"
    75       | print_var vars (SOME v) = (str o lookup_var vars) v;
    75       | print_var vars (SOME v) = (Pretty.str o lookup_var vars) v;
    76     fun applify_dict tyvars (Dict (_, d)) = applify_plain_dict tyvars d
    76     fun applify_dict tyvars (Dict (_, d)) = applify_plain_dict tyvars d
    77     and applify_plain_dict tyvars (Dict_Const (inst, dss)) =
    77     and applify_plain_dict tyvars (Dict_Const (inst, dss)) =
    78           applify_dictss tyvars ((str o deresolve o Class_Instance) inst) (map snd dss)
    78           applify_dictss tyvars ((Pretty.str o deresolve o Class_Instance) inst) (map snd dss)
    79       | applify_plain_dict tyvars (Dict_Var { var, class, ... }) =
    79       | applify_plain_dict tyvars (Dict_Var { var, class, ... }) =
    80           Pretty.block [str "implicitly",
    80           Pretty.block [Pretty.str "implicitly",
    81             enclose "[" "]" [Pretty.block [(str o deresolve_class) class,
    81             Pretty.enclose "[" "]" [Pretty.block [(Pretty.str o deresolve_class) class,
    82               enclose "[" "]" [(str o lookup_tyvar tyvars) var]]]]
    82               Pretty.enclose "[" "]" [(Pretty.str o lookup_tyvar tyvars) var]]]]
    83     and applify_dictss tyvars p dss =
    83     and applify_dictss tyvars p dss =
    84       applify "(" ")" (applify_dict tyvars) NOBR p (flat dss)
    84       applify "(" ")" (applify_dict tyvars) NOBR p (flat dss)
    85     fun print_term tyvars is_pat some_thm vars fxy (IConst const) =
    85     fun print_term tyvars is_pat some_thm vars fxy (IConst const) =
    86           print_app tyvars is_pat some_thm vars fxy (const, [])
    86           print_app tyvars is_pat some_thm vars fxy (const, [])
    87       | print_term tyvars is_pat some_thm vars fxy (t as (t1 `$ t2)) =
    87       | print_term tyvars is_pat some_thm vars fxy (t as (t1 `$ t2)) =
   108     and print_abs_head tyvars (some_v, ty) vars =
   108     and print_abs_head tyvars (some_v, ty) vars =
   109            let
   109            let
   110              val vars' = intro_vars (the_list some_v) vars;
   110              val vars' = intro_vars (the_list some_v) vars;
   111            in
   111            in
   112              (concat [
   112              (concat [
   113                enclose "(" ")" [constraint (print_var vars' some_v) (print_typ tyvars NOBR ty)],
   113                Pretty.enclose "(" ")" [constraint (print_var vars' some_v) (print_typ tyvars NOBR ty)],
   114                str "=>"
   114                Pretty.str "=>"
   115              ], vars')
   115              ], vars')
   116            end
   116            end
   117     and print_app tyvars is_pat some_thm vars fxy
   117     and print_app tyvars is_pat some_thm vars fxy
   118         (app as (const as { sym, typargs, dom, dicts, ... }, ts)) =
   118         (app as (const as { sym, typargs, dom, dicts, ... }, ts)) =
   119       let
   119       let
   129         val (wanted, print') = case syntax of
   129         val (wanted, print') = case syntax of
   130             NONE => (args_num sym, fn fxy => fn ts => applify_dicts
   130             NONE => (args_num sym, fn fxy => fn ts => applify_dicts
   131               (gen_applify (is_constr sym) "(" ")"
   131               (gen_applify (is_constr sym) "(" ")"
   132               (print_term tyvars is_pat some_thm vars NOBR) fxy
   132               (print_term tyvars is_pat some_thm vars NOBR) fxy
   133                 (applify "[" "]" (print_typ tyvars NOBR)
   133                 (applify "[" "]" (print_typ tyvars NOBR)
   134                   NOBR ((str o deresolve) sym) typargs') ts) dicts)
   134                   NOBR ((Pretty.str o deresolve) sym) typargs') ts) dicts)
   135           | SOME (k, Plain_printer s) => (k, fn fxy => fn ts => applify_dicts
   135           | SOME (k, Plain_printer s) => (k, fn fxy => fn ts => applify_dicts
   136               (applify "(" ")"
   136               (applify "(" ")"
   137               (print_term tyvars is_pat some_thm vars NOBR) fxy
   137               (print_term tyvars is_pat some_thm vars NOBR) fxy
   138                 (applify "[" "]" (print_typ tyvars NOBR)
   138                 (applify "[" "]" (print_typ tyvars NOBR)
   139                   NOBR (str s) typargs') ts) dicts)
   139                   NOBR (Pretty.str s) typargs') ts) dicts)
   140           | SOME (k, Complex_printer print) =>
   140           | SOME (k, Complex_printer print) =>
   141               (k, fn fxy => fn ts => print (print_term tyvars is_pat some_thm) some_thm vars fxy
   141               (k, fn fxy => fn ts => print (print_term tyvars is_pat some_thm) some_thm vars fxy
   142                 (ts ~~ take k dom))
   142                 (ts ~~ take k dom))
   143         val ((vs_tys, (ts1, rty)), ts2) =
   143         val ((vs_tys, (ts1, rty)), ts2) =
   144           Code_Thingol.satisfied_application wanted app;
   144           Code_Thingol.satisfied_application wanted app;
   146         if null vs_tys then
   146         if null vs_tys then
   147           if null ts2 then
   147           if null ts2 then
   148             print' fxy ts
   148             print' fxy ts
   149           else
   149           else
   150             Pretty.block (print' BR ts1 :: map (fn t => Pretty.block
   150             Pretty.block (print' BR ts1 :: map (fn t => Pretty.block
   151               [str ".apply(", print_term tyvars is_pat some_thm vars NOBR t, str ")"]) ts2)
   151               [Pretty.str ".apply(", print_term tyvars is_pat some_thm vars NOBR t, Pretty.str ")"]) ts2)
   152         else
   152         else
   153           print_term tyvars is_pat some_thm vars fxy (vs_tys `|==> (IConst const `$$ ts1, rty))
   153           print_term tyvars is_pat some_thm vars fxy (vs_tys `|==> (IConst const `$$ ts1, rty))
   154       end
   154       end
   155     and print_bind tyvars some_thm fxy p =
   155     and print_bind tyvars some_thm fxy p =
   156       gen_print_bind (print_term tyvars true) some_thm fxy p
   156       gen_print_bind (print_term tyvars true) some_thm fxy p
   157     and print_case tyvars some_thm vars fxy { clauses = [], ... } =
   157     and print_case tyvars some_thm vars fxy { clauses = [], ... } =
   158           (brackify fxy o Pretty.breaks o map str) ["sys.error(\"empty case\")"]
   158           (brackify fxy o Pretty.breaks o map Pretty.str) ["sys.error(\"empty case\")"]
   159       | print_case tyvars some_thm vars fxy (case_expr as { clauses = [_], ... }) =
   159       | print_case tyvars some_thm vars fxy (case_expr as { clauses = [_], ... }) =
   160           let
   160           let
   161             val (bind :: binds, body) = Code_Thingol.unfold_let (ICase case_expr);
   161             val (bind :: binds, body) = Code_Thingol.unfold_let (ICase case_expr);
   162             fun print_match_val ((pat, ty), t) vars =
   162             fun print_match_val ((pat, ty), t) vars =
   163               vars
   163               vars
   164               |> print_bind tyvars some_thm BR pat
   164               |> print_bind tyvars some_thm BR pat
   165               |>> (fn p => (false, concat [str "val", p, str "=",
   165               |>> (fn p => (false, concat [Pretty.str "val", p, Pretty.str "=",
   166                 constraint (print_term tyvars false some_thm vars NOBR t) (print_typ tyvars BR ty)]));
   166                 constraint (print_term tyvars false some_thm vars NOBR t) (print_typ tyvars BR ty)]));
   167             fun print_match_seq t vars =
   167             fun print_match_seq t vars =
   168               ((true, print_term tyvars false some_thm vars NOBR t), vars);
   168               ((true, print_term tyvars false some_thm vars NOBR t), vars);
   169             fun print_match is_first ((IVar NONE, ty), t) =
   169             fun print_match is_first ((IVar NONE, ty), t) =
   170                   if Code_Thingol.is_IAbs t andalso is_first
   170                   if Code_Thingol.is_IAbs t andalso is_first
   175             val (seps_ps, vars') =
   175             val (seps_ps, vars') =
   176               vars |> print_match true bind ||>> fold_map (print_match false) binds |>> uncurry cons;
   176               vars |> print_match true bind ||>> fold_map (print_match false) binds |>> uncurry cons;
   177             val all_seps_ps = seps_ps @ [(true, print_term tyvars false some_thm vars' NOBR body)];
   177             val all_seps_ps = seps_ps @ [(true, print_term tyvars false some_thm vars' NOBR body)];
   178             fun insert_seps [(_, p)] = [p]
   178             fun insert_seps [(_, p)] = [p]
   179               | insert_seps ((_, p) :: (seps_ps as (sep, _) :: _)) =
   179               | insert_seps ((_, p) :: (seps_ps as (sep, _) :: _)) =
   180                   (if sep then Pretty.block [p, str ";"] else p) :: insert_seps seps_ps
   180                   (if sep then Pretty.block [p, Pretty.str ";"] else p) :: insert_seps seps_ps
   181           in brackify_block fxy (str "{") (insert_seps all_seps_ps) (str "}") end
   181           in brackify_block fxy (Pretty.str "{") (insert_seps all_seps_ps) (Pretty.str "}") end
   182       | print_case tyvars some_thm vars fxy { term = t, typ = ty, clauses = clauses as _ :: _, ... } =
   182       | print_case tyvars some_thm vars fxy { term = t, typ = ty, clauses = clauses as _ :: _, ... } =
   183           let
   183           let
   184             fun print_select (pat, body) =
   184             fun print_select (pat, body) =
   185               let
   185               let
   186                 val (p_pat, vars') = print_bind tyvars some_thm NOBR pat vars;
   186                 val (p_pat, vars') = print_bind tyvars some_thm NOBR pat vars;
   187                 val p_body = print_term tyvars false some_thm vars' NOBR body
   187                 val p_body = print_term tyvars false some_thm vars' NOBR body
   188               in concat [str "case", p_pat, str "=>", p_body] end;
   188               in concat [Pretty.str "case", p_pat, Pretty.str "=>", p_body] end;
   189           in
   189           in
   190             map print_select clauses
   190             map print_select clauses
   191             |> Pretty.block_enclose (concat [print_term tyvars false some_thm vars NOBR t, str "match {"], str "}")
   191             |> Pretty.block_enclose (concat [print_term tyvars false some_thm vars NOBR t, Pretty.str "match {"], Pretty.str "}")
   192             |> single
   192             |> single
   193             |> enclose "(" ")"
   193             |> Pretty.enclose "(" ")"
   194           end;
   194           end;
   195     fun print_context tyvars vs s = applify "[" "]"
   195     fun print_context tyvars vs s = applify "[" "]"
   196       (fn (v, sort) => (Pretty.block o map str)
   196       (fn (v, sort) => (Pretty.block o map Pretty.str)
   197         (lookup_tyvar tyvars v :: maps (fn class => [" : ", deresolve_class class]) sort))
   197         (lookup_tyvar tyvars v :: maps (fn class => [" : ", deresolve_class class]) sort))
   198           NOBR (str s) vs;
   198           NOBR (Pretty.str s) vs;
   199     fun print_defhead tyvars vars const vs params tys ty =
   199     fun print_defhead tyvars vars const vs params tys ty =
   200       concat [str "def", constraint (applify "(" ")" (fn (param, ty) =>
   200       concat [Pretty.str "def", constraint (applify "(" ")" (fn (param, ty) =>
   201         constraint ((str o lookup_var vars) param) (print_typ tyvars NOBR ty))
   201         constraint ((Pretty.str o lookup_var vars) param) (print_typ tyvars NOBR ty))
   202           NOBR (print_context tyvars vs (deresolve_const const)) (params ~~ tys)) (print_typ tyvars NOBR ty),
   202           NOBR (print_context tyvars vs (deresolve_const const)) (params ~~ tys)) (print_typ tyvars NOBR ty),
   203             str "="];
   203             Pretty.str "="];
   204     fun print_def const (vs, ty) [] =
   204     fun print_def const (vs, ty) [] =
   205           let
   205           let
   206             val (tys, ty') = Code_Thingol.unfold_fun ty;
   206             val (tys, ty') = Code_Thingol.unfold_fun ty;
   207             val params = Name.invent (snd reserved) "a" (length tys);
   207             val params = Name.invent (snd reserved) "a" (length tys);
   208             val tyvars = intro_tyvars vs reserved;
   208             val tyvars = intro_tyvars vs reserved;
   209             val vars = intro_vars params reserved;
   209             val vars = intro_vars params reserved;
   210           in
   210           in
   211             concat [print_defhead tyvars vars const vs params tys ty',
   211             concat [print_defhead tyvars vars const vs params tys ty',
   212               str ("sys.error(" ^ print_scala_string const ^ ")")]
   212               Pretty.str ("sys.error(" ^ print_scala_string const ^ ")")]
   213           end
   213           end
   214       | print_def const (vs, ty) eqs =
   214       | print_def const (vs, ty) eqs =
   215           let
   215           let
   216             val tycos = build (fold (fn ((ts, t), _) =>
   216             val tycos = build (fold (fn ((ts, t), _) =>
   217               fold Code_Thingol.add_tyconames (t :: ts)) eqs);
   217               fold Code_Thingol.add_tyconames (t :: ts)) eqs);
   229               then (map (fn IVar (SOME x) => x) o fst o fst o hd) eqs
   229               then (map (fn IVar (SOME x) => x) o fst o fst o hd) eqs
   230               else aux_params vars1 (map (fst o fst) eqs);
   230               else aux_params vars1 (map (fst o fst) eqs);
   231             val vars2 = intro_vars params vars1;
   231             val vars2 = intro_vars params vars1;
   232             val (tys', ty') = Code_Thingol.unfold_fun_n (length params) ty;
   232             val (tys', ty') = Code_Thingol.unfold_fun_n (length params) ty;
   233             fun tuplify [p] = p
   233             fun tuplify [p] = p
   234               | tuplify ps = enum "," "(" ")" ps;
   234               | tuplify ps = Pretty.enum "," "(" ")" ps;
   235             fun print_rhs vars' ((_, t), (some_thm, _)) =
   235             fun print_rhs vars' ((_, t), (some_thm, _)) =
   236               print_term tyvars false some_thm vars' NOBR t;
   236               print_term tyvars false some_thm vars' NOBR t;
   237             fun print_clause (eq as ((ts, _), (some_thm, _))) =
   237             fun print_clause (eq as ((ts, _), (some_thm, _))) =
   238               let
   238               let
   239                 val vars' =
   239                 val vars' =
   240                   intro_vars (build (fold Code_Thingol.add_varnames ts)) vars1;
   240                   intro_vars (build (fold Code_Thingol.add_varnames ts)) vars1;
   241               in
   241               in
   242                 concat [str "case",
   242                 concat [Pretty.str "case",
   243                   tuplify (map (print_term tyvars true some_thm vars' NOBR) ts),
   243                   tuplify (map (print_term tyvars true some_thm vars' NOBR) ts),
   244                   str "=>", print_rhs vars' eq]
   244                   Pretty.str "=>", print_rhs vars' eq]
   245               end;
   245               end;
   246             val head = print_defhead tyvars vars2 const vs params tys' ty';
   246             val head = print_defhead tyvars vars2 const vs params tys' ty';
   247           in if simple then
   247           in if simple then
   248             concat [head, print_rhs vars2 (hd eqs)]
   248             concat [head, print_rhs vars2 (hd eqs)]
   249           else
   249           else
   250             Pretty.block_enclose
   250             Pretty.block_enclose
   251               (concat [head, tuplify (map (str o lookup_var vars2) params),
   251               (concat [head, tuplify (map (Pretty.str o lookup_var vars2) params),
   252                 str "match {"], str "}")
   252                 Pretty.str "match {"], Pretty.str "}")
   253               (map print_clause eqs)
   253               (map print_clause eqs)
   254           end;
   254           end;
   255     val print_method = str o Library.enclose "`" "`" o deresolve_full o Constant;
   255     val print_method = Pretty.str o enclose "`" "`" o deresolve_full o Constant;
   256     fun print_inst class (tyco, { vs, inst_params, superinst_params }) =
   256     fun print_inst class (tyco, { vs, inst_params, superinst_params }) =
   257       let
   257       let
   258         val tyvars = intro_tyvars vs reserved;
   258         val tyvars = intro_tyvars vs reserved;
   259         val classtyp = (class, tyco `%% map (ITyVar o fst) vs);
   259         val classtyp = (class, tyco `%% map (ITyVar o fst) vs);
   260         fun print_classparam_instance ((classparam, (const as { dom, ... }, dom_length)), (thm, _)) =
   260         fun print_classparam_instance ((classparam, (const as { dom, ... }, dom_length)), (thm, _)) =
   262             val aux_dom = Name.invent_names (snd reserved) "a" dom;
   262             val aux_dom = Name.invent_names (snd reserved) "a" dom;
   263             val auxs = map fst aux_dom;
   263             val auxs = map fst aux_dom;
   264             val vars = intro_vars auxs reserved;
   264             val vars = intro_vars auxs reserved;
   265             val (aux_dom1, aux_dom2) = chop dom_length aux_dom;
   265             val (aux_dom1, aux_dom2) = chop dom_length aux_dom;
   266             fun abstract_using [] = []
   266             fun abstract_using [] = []
   267               | abstract_using aux_dom = [enum "," "(" ")"
   267               | abstract_using aux_dom = [Pretty.enum "," "(" ")"
   268                   (map (fn (aux, ty) => constraint ((str o lookup_var vars) aux)
   268                   (map (fn (aux, ty) => constraint ((Pretty.str o lookup_var vars) aux)
   269                   (print_typ tyvars NOBR ty)) aux_dom), str "=>"];
   269                   (print_typ tyvars NOBR ty)) aux_dom), Pretty.str "=>"];
   270             val aux_abstr1 = abstract_using aux_dom1;
   270             val aux_abstr1 = abstract_using aux_dom1;
   271             val aux_abstr2 = abstract_using aux_dom2;
   271             val aux_abstr2 = abstract_using aux_dom2;
   272           in
   272           in
   273             concat ([str "val", print_method classparam, str "="]
   273             concat ([Pretty.str "val", print_method classparam, Pretty.str "="]
   274               @ aux_abstr1 @ aux_abstr2 @| print_app tyvars false (SOME thm) vars NOBR
   274               @ aux_abstr1 @ aux_abstr2 @| print_app tyvars false (SOME thm) vars NOBR
   275                 (const, map (IVar o SOME) auxs))
   275                 (const, map (IVar o SOME) auxs))
   276           end;
   276           end;
   277       in
   277       in
   278         Pretty.block_enclose (concat [str "implicit def",
   278         Pretty.block_enclose (concat [Pretty.str "implicit def",
   279           constraint (print_context tyvars vs
   279           constraint (print_context tyvars vs
   280             ((Library.enclose "`" "`" o deresolve_full o Class_Instance) (tyco, class)))
   280             ((Library.enclose "`" "`" o deresolve_full o Class_Instance) (tyco, class)))
   281           (print_dicttyp tyvars classtyp),
   281           (print_dicttyp tyvars classtyp),
   282           str "=", str "new", print_dicttyp tyvars classtyp, str "{"], str "}")
   282           Pretty.str "=", Pretty.str "new", print_dicttyp tyvars classtyp, Pretty.str "{"], Pretty.str "}")
   283             (map print_classparam_instance (inst_params @ superinst_params))
   283             (map print_classparam_instance (inst_params @ superinst_params))
   284       end;
   284       end;
   285     fun print_stmt (Constant const, (_, Fun ((vs, ty), raw_eqs))) =
   285     fun print_stmt (Constant const, (_, Fun ((vs, ty), raw_eqs))) =
   286           print_def const (vs, ty) (filter (snd o snd) raw_eqs)
   286           print_def const (vs, ty) (filter (snd o snd) raw_eqs)
   287       | print_stmt (Type_Constructor tyco, (_, Datatype (vs, cos))) =
   287       | print_stmt (Type_Constructor tyco, (_, Datatype (vs, cos))) =
   288           let
   288           let
   289             val tyvars = intro_tyvars (map (rpair []) vs) reserved;
   289             val tyvars = intro_tyvars (map (rpair []) vs) reserved;
   290             fun print_co ((co, vs_args), tys) =
   290             fun print_co ((co, vs_args), tys) =
   291               concat [Pretty.block ((applify "[" "]" (str o lookup_tyvar tyvars) NOBR
   291               concat [Pretty.block ((applify "[" "]" (Pretty.str o lookup_tyvar tyvars) NOBR
   292                 (str ("final case class " ^ deresolve_const co)) vs_args)
   292                 (Pretty.str ("final case class " ^ deresolve_const co)) vs_args)
   293                 @@ enum "," "(" ")" (map (fn (v, arg) => constraint (str v) (print_typ tyvars NOBR arg))
   293                 @@ Pretty.enum "," "(" ")" (map (fn (v, arg) => constraint (Pretty.str v) (print_typ tyvars NOBR arg))
   294                   (Name.invent_names (snd reserved) "a" tys))),
   294                   (Name.invent_names (snd reserved) "a" tys))),
   295                 str "extends",
   295                 Pretty.str "extends",
   296                 applify "[" "]" (str o lookup_tyvar tyvars) NOBR
   296                 applify "[" "]" (Pretty.str o lookup_tyvar tyvars) NOBR
   297                   ((str o deresolve_tyco) tyco) vs
   297                   ((Pretty.str o deresolve_tyco) tyco) vs
   298               ];
   298               ];
   299           in
   299           in
   300             Pretty.chunks (applify "[" "]" (str o lookup_tyvar tyvars)
   300             Pretty.chunks (applify "[" "]" (Pretty.str o lookup_tyvar tyvars)
   301               NOBR (str ("abstract sealed class " ^ deresolve_tyco tyco)) vs
   301               NOBR (Pretty.str ("abstract sealed class " ^ deresolve_tyco tyco)) vs
   302                 :: map print_co cos)
   302                 :: map print_co cos)
   303           end
   303           end
   304       | print_stmt (Type_Class class, (_, Class ((v, (classrels, classparams)), insts))) =
   304       | print_stmt (Type_Class class, (_, Class ((v, (classrels, classparams)), insts))) =
   305           let
   305           let
   306             val tyvars = intro_tyvars [(v, [class])] reserved;
   306             val tyvars = intro_tyvars [(v, [class])] reserved;
   307             fun add_typarg s = Pretty.block
   307             fun add_typarg s = Pretty.block
   308               [str s, str "[", (str o lookup_tyvar tyvars) v, str "]"];
   308               [Pretty.str s, Pretty.str "[", (Pretty.str o lookup_tyvar tyvars) v, Pretty.str "]"];
   309             fun print_super_classes [] = NONE
   309             fun print_super_classes [] = NONE
   310               | print_super_classes classrels = SOME (concat (str "extends"
   310               | print_super_classes classrels = SOME (concat (Pretty.str "extends"
   311                   :: separate (str "with") (map (add_typarg o deresolve_class o snd) classrels)));
   311                   :: separate (Pretty.str "with") (map (add_typarg o deresolve_class o snd) classrels)));
   312             fun print_classparam_val (classparam, ty) =
   312             fun print_classparam_val (classparam, ty) =
   313               concat [str "val", constraint (print_method classparam)
   313               concat [Pretty.str "val", constraint (print_method classparam)
   314                 ((print_tupled_typ tyvars o Code_Thingol.unfold_fun) ty)];
   314                 ((print_tupled_typ tyvars o Code_Thingol.unfold_fun) ty)];
   315             fun print_classparam_def (classparam, ty) =
   315             fun print_classparam_def (classparam, ty) =
   316               let
   316               let
   317                 val (tys, ty) = Code_Thingol.unfold_fun ty;
   317                 val (tys, ty) = Code_Thingol.unfold_fun ty;
   318                 val [implicit_name] = Name.invent (snd reserved) (lookup_tyvar tyvars v) 1;
   318                 val [implicit_name] = Name.invent (snd reserved) (lookup_tyvar tyvars v) 1;
   319                 val proto_vars = intro_vars [implicit_name] reserved;
   319                 val proto_vars = intro_vars [implicit_name] reserved;
   320                 val auxs = Name.invent (snd proto_vars) "a" (length tys);
   320                 val auxs = Name.invent (snd proto_vars) "a" (length tys);
   321                 val vars = intro_vars auxs proto_vars;
   321                 val vars = intro_vars auxs proto_vars;
   322               in
   322               in
   323                 concat [str "def", constraint (Pretty.block [applify "(" ")"
   323                 concat [Pretty.str "def", constraint (Pretty.block [applify "(" ")"
   324                   (fn (aux, ty) => constraint ((str o lookup_var vars) aux)
   324                   (fn (aux, ty) => constraint ((Pretty.str o lookup_var vars) aux)
   325                   (print_typ tyvars NOBR ty)) NOBR (add_typarg (deresolve_const classparam))
   325                   (print_typ tyvars NOBR ty)) NOBR (add_typarg (deresolve_const classparam))
   326                   (auxs ~~ tys), str "(implicit ", str implicit_name, str ": ",
   326                   (auxs ~~ tys), Pretty.str "(implicit ", Pretty.str implicit_name, Pretty.str ": ",
   327                   add_typarg (deresolve_class class), str ")"]) (print_typ tyvars NOBR ty), str "=",
   327                   add_typarg (deresolve_class class), Pretty.str ")"]) (print_typ tyvars NOBR ty), Pretty.str "=",
   328                   applify "(" ")" (str o lookup_var vars) NOBR
   328                   applify "(" ")" (Pretty.str o lookup_var vars) NOBR
   329                   (Pretty.block [str implicit_name, str ".", print_method classparam]) auxs]
   329                   (Pretty.block [Pretty.str implicit_name, Pretty.str ".", print_method classparam]) auxs]
   330               end;
   330               end;
   331           in
   331           in
   332             Pretty.chunks (
   332             Pretty.chunks (
   333               (Pretty.block_enclose
   333               (Pretty.block_enclose
   334                 (concat ([str "trait", (add_typarg o deresolve_class) class]
   334                 (concat ([Pretty.str "trait", (add_typarg o deresolve_class) class]
   335                   @ the_list (print_super_classes classrels) @ [str "{"]), str "}")
   335                   @ the_list (print_super_classes classrels) @ [Pretty.str "{"]), Pretty.str "}")
   336                 (map print_classparam_val classparams))
   336                 (map print_classparam_val classparams))
   337               :: map print_classparam_def classparams
   337               :: map print_classparam_def classparams
   338               @| Pretty.block_enclose
   338               @| Pretty.block_enclose
   339                 (str ("object " ^ deresolve_class class ^ " {"), str "}")
   339                 (Pretty.str ("object " ^ deresolve_class class ^ " {"), Pretty.str "}")
   340                 (map (print_inst class) insts)
   340                 (map (print_inst class) insts)
   341             )
   341             )
   342           end;
   342           end;
   343   in print_stmt end;
   343   in print_stmt end;
   344 
   344 
   430       tyco_syntax const_syntax (make_vars reserved_syms) args_num
   430       tyco_syntax const_syntax (make_vars reserved_syms) args_num
   431       (Code_Thingol.is_constr program) (deresolver prefix_fragments, deresolver []);
   431       (Code_Thingol.is_constr program) (deresolver prefix_fragments, deresolver []);
   432 
   432 
   433     (* print modules *)
   433     (* print modules *)
   434     fun print_module _ base _ ps = Pretty.chunks2
   434     fun print_module _ base _ ps = Pretty.chunks2
   435       (str ("object " ^ base ^ " {")
   435       (Pretty.str ("object " ^ base ^ " {")
   436         :: ps @| str ("} /* object " ^ base ^ " */"));
   436         :: ps @| Pretty.str ("} /* object " ^ base ^ " */"));
   437 
   437 
   438     (* serialization *)
   438     (* serialization *)
   439     val p = Pretty.chunks2 (map snd includes
   439     val p = Pretty.chunks2 (map snd includes
   440       @ Code_Namespace.print_hierarchical {
   440       @ Code_Namespace.print_hierarchical {
   441         print_module = print_module, print_stmt = print_stmt,
   441         print_module = print_module, print_stmt = print_stmt,
   454     then signed_string_of_int k
   454     then signed_string_of_int k
   455     else quote (signed_string_of_int k)
   455     else quote (signed_string_of_int k)
   456 in Literals {
   456 in Literals {
   457   literal_string = print_scala_string,
   457   literal_string = print_scala_string,
   458   literal_numeral = fn k => "BigInt(" ^ numeral_scala k ^ ")",
   458   literal_numeral = fn k => "BigInt(" ^ numeral_scala k ^ ")",
   459   literal_list = fn [] => str "Nil" | ps => Pretty.block [str "List", enum "," "(" ")" ps],
   459   literal_list = fn [] => Pretty.str "Nil" | ps => Pretty.block [Pretty.str "List", Pretty.enum "," "(" ")" ps],
   460   infix_cons = (6, "::")
   460   infix_cons = (6, "::")
   461 } end;
   461 } end;
   462 
   462 
   463 
   463 
   464 (** Isar setup **)
   464 (** Isar setup **)
   473       evaluation_args = Token.explode0 Keyword.empty_keywords "case_insensitive"})
   473       evaluation_args = Token.explode0 Keyword.empty_keywords "case_insensitive"})
   474   #> Code_Target.set_printings (Type_Constructor ("fun",
   474   #> Code_Target.set_printings (Type_Constructor ("fun",
   475     [(target, SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
   475     [(target, SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
   476       brackify_infix (1, R) fxy (
   476       brackify_infix (1, R) fxy (
   477         print_typ BR ty1 (*product type vs. tupled arguments!*),
   477         print_typ BR ty1 (*product type vs. tupled arguments!*),
   478         str "=>",
   478         Pretty.str "=>",
   479         print_typ (INFX (1, R)) ty2
   479         print_typ (INFX (1, R)) ty2
   480       )))]))
   480       )))]))
   481   #> fold (Code_Target.add_reserved target) [
   481   #> fold (Code_Target.add_reserved target) [
   482       "abstract", "case", "catch", "class", "def", "do", "else", "enum", "export", "extends",
   482       "abstract", "case", "catch", "class", "def", "do", "else", "enum", "export", "extends",
   483       "false", "final", "finally", "for", "forSome", "given", "if", "implicit", "import", "lazy",
   483       "false", "final", "finally", "for", "forSome", "given", "if", "implicit", "import", "lazy",