src/Tools/Code/code_scala.ML
author haftmann
Thu, 02 Sep 2010 12:30:22 +0200
changeset 39034 ebeb48fd653b
parent 39030 2bb34f36db80
child 39056 fa197571676b
permissions -rw-r--r--
formal framework for presentation of selected statements
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37745
6315b6426200 checking generated code for various target languages
haftmann
parents: 37669
diff changeset
     1
(*  Title:      Tools/Code/code_scala.ML
6315b6426200 checking generated code for various target languages
haftmann
parents: 37669
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     3
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     4
Serializer for Scala.
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     5
*)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     6
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     7
signature CODE_SCALA =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     8
sig
37745
6315b6426200 checking generated code for various target languages
haftmann
parents: 37669
diff changeset
     9
  val target: string
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    10
  val setup: theory -> theory
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    11
end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    12
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    13
structure Code_Scala : CODE_SCALA =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    14
struct
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    15
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    16
val target = "Scala";
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    17
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    18
open Basic_Code_Thingol;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    19
open Code_Printer;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    20
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    21
infixr 5 @@;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    22
infixr 5 @|;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    23
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    24
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    25
(** Scala serializer **)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    26
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
    27
fun print_scala_stmt labelled_name tyco_syntax const_syntax reserved
38780
910cedb62327 stub for (later) correct deresolving of class method names
haftmann
parents: 38779
diff changeset
    28
    args_num is_singleton_constr (deresolve, deresolve_full) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    29
  let
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    30
    fun lookup_tyvar tyvars = lookup_var tyvars o first_upper;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    31
    fun intro_tyvars vs = intro_vars (map (first_upper o fst) vs);
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    32
    fun print_tyco_expr tyvars fxy (tyco, tys) = applify "[" "]"
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    33
          (print_typ tyvars NOBR) fxy ((str o deresolve) tyco) tys
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
    34
    and print_typ tyvars fxy (tyco `%% tys) = (case tyco_syntax tyco
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    35
         of NONE => print_tyco_expr tyvars fxy (tyco, tys)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    36
          | SOME (i, print) => print (print_typ tyvars) fxy tys)
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
    37
      | print_typ tyvars fxy (ITyVar v) = (str o lookup_tyvar tyvars) v;
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    38
    fun print_dicttyp tyvars (class, ty) = print_tyco_expr tyvars NOBR (class, [ty]);
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    39
    fun print_tupled_typ tyvars ([], ty) =
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    40
          print_typ tyvars NOBR ty
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    41
      | print_tupled_typ tyvars ([ty1], ty2) =
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    42
          concat [print_typ tyvars BR ty1, str "=>", print_typ tyvars NOBR ty2]
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    43
      | print_tupled_typ tyvars (tys, ty) =
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    44
          concat [enum "," "(" ")" (map (print_typ tyvars NOBR) tys),
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    45
            str "=>", print_typ tyvars NOBR ty];
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    46
    fun constraint p1 p2 = Pretty.block [p1, str ":", Pretty.brk 1, p2];
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    47
    fun print_var vars NONE = str "_"
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    48
      | print_var vars (SOME v) = (str o lookup_var vars) v
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    49
    fun print_term tyvars is_pat some_thm vars fxy (IConst c) =
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    50
          print_app tyvars is_pat some_thm vars fxy (c, [])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    51
      | print_term tyvars is_pat some_thm vars fxy (t as (t1 `$ t2)) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    52
          (case Code_Thingol.unfold_const_app t
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    53
           of SOME app => print_app tyvars is_pat some_thm vars fxy app
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    54
            | _ => applify "(" ")" (print_term tyvars is_pat some_thm vars NOBR) fxy
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    55
                (print_term tyvars is_pat some_thm vars BR t1) [t2])
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    56
      | print_term tyvars is_pat some_thm vars fxy (IVar v) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    57
          print_var vars v
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    58
      | print_term tyvars is_pat some_thm vars fxy ((v, ty) `|=> t) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    59
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    60
            val vars' = intro_vars (the_list v) vars;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    61
          in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    62
            concat [
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    63
              enclose "(" ")" [constraint (print_var vars' v) (print_typ tyvars NOBR ty)],
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    64
              str "=>",
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    65
              print_term tyvars false some_thm vars' NOBR t
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    66
            ]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    67
          end 
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    68
      | print_term tyvars is_pat some_thm vars fxy (ICase (cases as (_, t0))) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    69
          (case Code_Thingol.unfold_const_app t0
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
    70
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    71
                then print_case tyvars some_thm vars fxy cases
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    72
                else print_app tyvars is_pat some_thm vars fxy c_ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    73
            | NONE => print_case tyvars some_thm vars fxy cases)
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    74
    and print_app tyvars is_pat some_thm vars fxy
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    75
        (app as ((c, ((arg_typs, _), function_typs)), ts)) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    76
      let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    77
        val k = length ts;
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
    78
        val arg_typs' = if is_pat orelse
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
    79
          (is_none (const_syntax c) andalso is_singleton_constr c) then [] else arg_typs;
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
    80
        val (l, print') = case const_syntax c
38059
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
    81
         of NONE => (args_num c, fn fxy => fn ts => applify "(" ")"
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    82
              (print_term tyvars is_pat some_thm vars NOBR) fxy
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    83
                (applify "[" "]" (print_typ tyvars NOBR)
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
    84
                  NOBR ((str o deresolve) c) arg_typs') ts)
38059
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
    85
          | SOME (Plain_const_syntax (k, s)) => (k, fn fxy => fn ts => applify "(" ")"
37881
096c8397c989 distinguish different classes of const syntax
haftmann
parents: 37822
diff changeset
    86
              (print_term tyvars is_pat some_thm vars NOBR) fxy
096c8397c989 distinguish different classes of const syntax
haftmann
parents: 37822
diff changeset
    87
                (applify "[" "]" (print_typ tyvars NOBR)
096c8397c989 distinguish different classes of const syntax
haftmann
parents: 37822
diff changeset
    88
                  NOBR (str s) arg_typs') ts)
096c8397c989 distinguish different classes of const syntax
haftmann
parents: 37822
diff changeset
    89
          | SOME (Complex_const_syntax (k, print)) =>
38059
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
    90
              (k, fn fxy => fn ts => print (print_term tyvars is_pat some_thm) some_thm vars fxy
37881
096c8397c989 distinguish different classes of const syntax
haftmann
parents: 37822
diff changeset
    91
                (ts ~~ take k function_typs))
38059
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
    92
      in if k = l then print' fxy ts
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    93
      else if k < l then
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    94
        print_term tyvars is_pat some_thm vars fxy (Code_Thingol.eta_expand l app)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    95
      else let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    96
        val (ts1, ts23) = chop l ts;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    97
      in
38059
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
    98
        Pretty.block (print' BR ts1 :: map (fn t => Pretty.block
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    99
          [str ".apply(", print_term tyvars is_pat some_thm vars NOBR t, str ")"]) ts23)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   100
      end end
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   101
    and print_bind tyvars some_thm fxy p =
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   102
      gen_print_bind (print_term tyvars true) some_thm fxy p
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   103
    and print_case tyvars some_thm vars fxy (cases as ((_, [_]), _)) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   104
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   105
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
38059
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   106
            fun print_match ((IVar NONE, _), t) vars =
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   107
                  ((true, print_term tyvars false some_thm vars NOBR t), vars)
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   108
              | print_match ((pat, ty), t) vars =
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   109
                  vars
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   110
                  |> print_bind tyvars some_thm BR pat
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   111
                  |>> (fn p => (false, concat [str "val", constraint p (print_typ tyvars NOBR ty),
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   112
                      str "=", print_term tyvars false some_thm vars NOBR t]))
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   113
            val (seps_ps, vars') = fold_map print_match binds vars;
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   114
            val all_seps_ps = seps_ps @ [(true, print_term tyvars false some_thm vars' NOBR body)];
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   115
            fun insert_seps [(_, p)] = [p]
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   116
              | insert_seps ((_, p) :: (seps_ps as (sep, _) :: _)) =
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   117
                  (if sep then Pretty.block [p, str ";"] else p) :: insert_seps seps_ps
72f4630d4c43 tuned printing of applications and let cascades
haftmann
parents: 37958
diff changeset
   118
          in brackify_block fxy (str "{") (insert_seps all_seps_ps) (str "}")
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   119
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   120
      | print_case tyvars some_thm vars fxy (((t, ty), clauses as _ :: _), _) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   121
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   122
            fun print_select (pat, body) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   123
              let
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   124
                val (p_pat, vars') = print_bind tyvars some_thm NOBR pat vars;
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   125
                val p_body = print_term tyvars false some_thm vars' NOBR body
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   126
              in concat [str "case", p_pat, str "=>", p_body] end;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   127
          in brackify_block fxy
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   128
            (concat [print_term tyvars false some_thm vars NOBR t, str "match", str "{"])
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   129
            (map print_select clauses)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   130
            (str "}") 
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   131
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   132
      | print_case tyvars some_thm vars fxy ((_, []), _) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   133
          (brackify fxy o Pretty.breaks o map str) ["error(\"empty case\")"];
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   134
    fun print_context tyvars vs name = applify "[" "]"
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   135
      (fn (v, sort) => (Pretty.block o map str)
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   136
        (lookup_tyvar tyvars v :: maps (fn sort => [": ", deresolve sort]) sort))
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   137
          NOBR ((str o deresolve) name) vs;
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   138
    fun print_defhead tyvars vars name vs params tys ty =
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   139
      Pretty.block [str "def ", constraint (applify "(" ")" (fn (param, ty) =>
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   140
        constraint ((str o lookup_var vars) param) (print_typ tyvars NOBR ty))
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   141
          NOBR (print_context tyvars vs name) (params ~~ tys)) (print_typ tyvars NOBR ty),
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   142
            str " ="];
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   143
    fun print_def name (vs, ty) [] =
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   144
          let
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   145
            val (tys, ty') = Code_Thingol.unfold_fun ty;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   146
            val params = Name.invents (snd reserved) "a" (length tys);
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   147
            val tyvars = intro_tyvars vs reserved;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   148
            val vars = intro_vars params reserved;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   149
          in
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   150
            concat [print_defhead tyvars vars name vs params tys ty',
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   151
              str ("error(\"" ^ name ^ "\")")]
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   152
          end
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   153
      | print_def name (vs, ty) eqs =
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   154
          let
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   155
            val tycos = fold (fn ((ts, t), _) =>
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   156
              fold Code_Thingol.add_tyconames (t :: ts)) eqs [];
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   157
            val tyvars = reserved
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   158
              |> intro_base_names
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   159
                   (is_none o tyco_syntax) deresolve tycos
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   160
              |> intro_tyvars vs;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   161
            val simple = case eqs
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   162
             of [((ts, _), _)] => forall Code_Thingol.is_IVar ts
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   163
              | _ => false;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   164
            val consts = fold Code_Thingol.add_constnames
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   165
              (map (snd o fst) eqs) [];
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   166
            val vars1 = reserved
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   167
              |> intro_base_names
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   168
                   (is_none o const_syntax) deresolve consts
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   169
            val params = if simple
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   170
              then (map (fn IVar (SOME x) => x) o fst o fst o hd) eqs
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   171
              else aux_params vars1 (map (fst o fst) eqs);
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   172
            val vars2 = intro_vars params vars1;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   173
            val (tys', ty') = Code_Thingol.unfold_fun_n (length params) ty;
38922
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38916
diff changeset
   174
            fun tuplify [p] = p
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38916
diff changeset
   175
              | tuplify ps = enum "," "(" ")" ps;
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   176
            fun print_rhs vars' ((_, t), (some_thm, _)) =
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   177
              print_term tyvars false some_thm vars' NOBR t;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   178
            fun print_clause (eq as ((ts, _), (some_thm, _))) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   179
              let
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   180
                val vars' = intro_vars ((fold o Code_Thingol.fold_varnames)
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   181
                  (insert (op =)) ts []) vars1;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   182
              in
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   183
                concat [str "case",
38922
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38916
diff changeset
   184
                  tuplify (map (print_term tyvars true some_thm vars' NOBR) ts),
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   185
                  str "=>", print_rhs vars' eq]
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   186
              end;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   187
            val head = print_defhead tyvars vars2 name vs params tys' ty';
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   188
          in if simple then
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   189
            concat [head, print_rhs vars2 (hd eqs)]
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   190
          else
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   191
            Pretty.block_enclose
38922
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38916
diff changeset
   192
              (concat [head, tuplify (map (str o lookup_var vars2) params),
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   193
                str "match", str "{"], str "}")
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   194
              (map print_clause eqs)
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   195
          end;
39023
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   196
    val print_method = str o Library.enclose "`" "`" o deresolve_full;
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   197
    fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) =
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   198
          print_def name (vs, ty) (filter (snd o snd) raw_eqs)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   199
      | print_stmt (name, Code_Thingol.Datatype (_, (vs, cos))) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   200
          let
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   201
            val tyvars = intro_tyvars vs reserved;
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   202
            fun print_co ((co, _), []) =
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   203
                  concat [str "final", str "case", str "object", (str o deresolve) co,
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   204
                    str "extends", applify "[" "]" I NOBR ((str o deresolve) name)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   205
                      (replicate (length vs) (str "Nothing"))]
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   206
              | print_co ((co, vs_args), tys) =
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   207
                  concat [applify "(" ")"
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   208
                    (fn (v, arg) => constraint (str v) (print_typ tyvars NOBR arg)) NOBR
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   209
                    (applify "[" "]" (str o lookup_tyvar tyvars) NOBR ((concat o map str)
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   210
                      ["final", "case", "class", deresolve co]) vs_args)
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   211
                    (Name.names (snd reserved) "a" tys),
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   212
                    str "extends",
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   213
                    applify "[" "]" (str o lookup_tyvar tyvars o fst) NOBR
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   214
                      ((str o deresolve) name) vs
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   215
                  ];
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   216
          in
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   217
            Pretty.chunks (applify "[" "]" (str o prefix "+" o lookup_tyvar tyvars o fst)
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   218
              NOBR ((concat o map str) ["abstract", "sealed", "class", deresolve name]) vs
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   219
                :: map print_co cos)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   220
          end
37447
ad3e04f289b6 transitive superclasses were also only a misunderstanding
haftmann
parents: 37446
diff changeset
   221
      | print_stmt (name, Code_Thingol.Class (_, (v, (super_classes, classparams)))) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   222
          let
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   223
            val tyvars = intro_tyvars [(v, [name])] reserved;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   224
            fun add_typarg s = Pretty.block
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   225
              [str s, str "[", (str o lookup_tyvar tyvars) v, str "]"];
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37337
diff changeset
   226
            fun print_super_classes [] = NONE
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37337
diff changeset
   227
              | print_super_classes classes = SOME (concat (str "extends"
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   228
                  :: separate (str "with") (map (add_typarg o deresolve o fst) classes)));
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   229
            fun print_classparam_val (classparam, ty) =
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   230
              concat [str "val", constraint (print_method classparam)
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   231
                ((print_tupled_typ tyvars o Code_Thingol.unfold_fun) ty)];
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   232
            fun print_classparam_def (classparam, ty) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   233
              let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   234
                val (tys, ty) = Code_Thingol.unfold_fun ty;
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   235
                val [implicit_name] = Name.invents (snd reserved) (lookup_tyvar tyvars v) 1;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   236
                val proto_vars = intro_vars [implicit_name] reserved;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   237
                val auxs = Name.invents (snd proto_vars) "a" (length tys);
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   238
                val vars = intro_vars auxs proto_vars;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   239
              in
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   240
                concat [str "def", constraint (Pretty.block [applify "(" ")"
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   241
                  (fn (aux, ty) => constraint ((str o lookup_var vars) aux)
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   242
                  (print_typ tyvars NOBR ty)) NOBR (add_typarg (deresolve classparam))
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   243
                  (auxs ~~ tys), str "(implicit ", str implicit_name, str ": ",
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   244
                  add_typarg (deresolve name), str ")"]) (print_typ tyvars NOBR ty), str "=",
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   245
                  applify "(" ")" (str o lookup_var vars) NOBR
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   246
                  (Pretty.block [str implicit_name, str ".", print_method classparam]) auxs]
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   247
              end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   248
          in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   249
            Pretty.chunks (
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   250
              (Pretty.block_enclose
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   251
                (concat ([str "trait", (add_typarg o deresolve) name]
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37337
diff changeset
   252
                  @ the_list (print_super_classes super_classes) @ [str "{"]), str "}")
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   253
                (map print_classparam_val classparams))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   254
              :: map print_classparam_def classparams
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   255
            )
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   256
          end
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   257
      | print_stmt (name, Code_Thingol.Classinst ((class, (tyco, vs)),
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   258
            (super_instances, (classparam_instances, further_classparam_instances)))) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   259
          let
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   260
            val tyvars = intro_tyvars vs reserved;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   261
            val classtyp = (class, tyco `%% map (ITyVar o fst) vs);
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   262
            fun print_classparam_instance ((classparam, const as (_, (_, tys))), (thm, _)) =
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   263
              let
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   264
                val aux_tys = Name.names (snd reserved) "a" tys;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   265
                val auxs = map fst aux_tys;
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   266
                val vars = intro_vars auxs reserved;
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   267
                val aux_abstr = if null auxs then [] else [enum "," "(" ")"
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   268
                  (map (fn (aux, ty) => constraint ((str o lookup_var vars) aux)
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   269
                  (print_typ tyvars NOBR ty)) aux_tys), str "=>"];
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   270
              in 
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   271
                concat ([str "val", print_method classparam, str "="]
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   272
                  @ aux_abstr @| print_app tyvars false (SOME thm) vars NOBR
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   273
                    (const, map (IVar o SOME) auxs))
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   274
              end;
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   275
          in
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   276
            Pretty.block_enclose (concat [str "implicit def",
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   277
              constraint (print_context tyvars vs name) (print_dicttyp tyvars classtyp),
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   278
              str "=", str "new", print_dicttyp tyvars classtyp, str "{"], str "}")
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   279
                (map print_classparam_instance (classparam_instances @ further_classparam_instances))
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   280
          end;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   281
  in print_stmt end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   282
38779
89f654951200 tuned serializer interface
haftmann
parents: 38778
diff changeset
   283
fun scala_program_of_program labelled_name reserved module_alias program =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   284
  let
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   285
    fun namify_module name_fragment ((nsp_class, nsp_object), nsp_common) =
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   286
      let
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   287
        val declare = Name.declare name_fragment;
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   288
      in (name_fragment, ((declare nsp_class, declare nsp_object), declare nsp_common)) end;
38769
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   289
    fun namify_class base ((nsp_class, nsp_object), nsp_common) =
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   290
      let
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   291
        val (base', nsp_class') = yield_singleton Name.variants base nsp_class
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   292
      in (base', ((nsp_class', nsp_object), Name.declare base' nsp_common)) end;
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   293
    fun namify_object base ((nsp_class, nsp_object), nsp_common) =
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   294
      let
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   295
        val (base', nsp_object') = yield_singleton Name.variants base nsp_object
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   296
      in (base', ((nsp_class, nsp_object'), Name.declare base' nsp_common)) end;
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   297
    fun namify_common upper base ((nsp_class, nsp_object), nsp_common) =
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   298
      let
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   299
        val (base', nsp_common') =
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   300
          yield_singleton Name.variants (if upper then first_upper base else base) nsp_common
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   301
      in
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   302
        (base',
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   303
          ((Name.declare base' nsp_class, Name.declare base' nsp_object), nsp_common'))
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   304
      end;
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   305
    fun namify_stmt (Code_Thingol.Fun _) = namify_object
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   306
      | namify_stmt (Code_Thingol.Datatype _) = namify_class
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   307
      | namify_stmt (Code_Thingol.Datatypecons _) = namify_common true
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   308
      | namify_stmt (Code_Thingol.Class _) = namify_class
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   309
      | namify_stmt (Code_Thingol.Classrel _) = namify_object
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   310
      | namify_stmt (Code_Thingol.Classparam _) = namify_object
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   311
      | namify_stmt (Code_Thingol.Classinst _) = namify_common false;
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   312
    fun memorize_implicits name =
38769
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   313
      let
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   314
        fun is_classinst stmt = case stmt
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   315
         of Code_Thingol.Classinst _ => true
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   316
          | _ => false;
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   317
        val implicits = filter (is_classinst o Graph.get_node program)
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   318
          (Graph.imm_succs program name);
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   319
      in union (op =) implicits end;
39024
30d5dd2f30b6 simultaneous modification of statements: statement names
haftmann
parents: 39023
diff changeset
   320
    fun modify_stmt (_, Code_Thingol.Datatypecons _) = NONE
30d5dd2f30b6 simultaneous modification of statements: statement names
haftmann
parents: 39023
diff changeset
   321
      | modify_stmt (_, Code_Thingol.Classrel _) = NONE
30d5dd2f30b6 simultaneous modification of statements: statement names
haftmann
parents: 39023
diff changeset
   322
      | modify_stmt (_, Code_Thingol.Classparam _) = NONE
30d5dd2f30b6 simultaneous modification of statements: statement names
haftmann
parents: 39023
diff changeset
   323
      | modify_stmt (_, stmt) = SOME stmt;
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   324
  in
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   325
    Code_Namespace.hierarchical_program labelled_name { module_alias = module_alias, reserved = reserved,
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   326
      empty_nsp = ((reserved, reserved), reserved), namify_module = namify_module, namify_stmt = namify_stmt,
39023
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   327
      cyclic_modules = true, empty_data = [], memorize_data = memorize_implicits, modify_stmts = map modify_stmt } program
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   328
  end;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   329
38928
0e6f54c9d201 dropped single_module parameter
haftmann
parents: 38926
diff changeset
   330
fun serialize_scala { labelled_name, reserved_syms, includes,
38926
24f82786cc57 record argument for serializers
haftmann
parents: 38924
diff changeset
   331
    module_alias, class_syntax, tyco_syntax, const_syntax, program,
24f82786cc57 record argument for serializers
haftmann
parents: 38924
diff changeset
   332
    names, presentation_names } =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   333
  let
38769
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   334
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   335
    (* build program *)
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   336
    val { deresolver, hierarchical_program = sca_program } =
39030
2bb34f36db80 include names need not be considered as reserved any longer
haftmann
parents: 39024
diff changeset
   337
      scala_program_of_program labelled_name (Name.make_context reserved_syms) module_alias program;
38769
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   338
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   339
    (* print statements *)
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   340
    fun lookup_constr tyco constr = case Graph.get_node program tyco
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   341
     of Code_Thingol.Datatype (_, (_, constrs)) =>
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   342
          the (AList.lookup (op = o apsnd fst) constrs constr);
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   343
    fun classparams_of_class class = case Graph.get_node program class
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   344
     of Code_Thingol.Class (_, (_, (_, classparams))) => classparams;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   345
    fun args_num c = case Graph.get_node program c
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   346
     of Code_Thingol.Fun (_, (((_, ty), []), _)) =>
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   347
          (length o fst o Code_Thingol.unfold_fun) ty
37437
4202e11ae7dc formal introduction of case cong
haftmann
parents: 37384
diff changeset
   348
      | Code_Thingol.Fun (_, ((_, ((ts, _), _) :: _), _)) => length ts
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   349
      | Code_Thingol.Datatypecons (_, tyco) => length (lookup_constr tyco c)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   350
      | Code_Thingol.Classparam (_, class) =>
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   351
          (length o fst o Code_Thingol.unfold_fun o the o AList.lookup (op =)
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   352
            (classparams_of_class class)) c;
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   353
    fun is_singleton_constr c = case Graph.get_node program c
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   354
     of Code_Thingol.Datatypecons (_, tyco) => null (lookup_constr tyco c)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   355
      | _ => false;
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   356
    val print_stmt = print_scala_stmt labelled_name tyco_syntax const_syntax
39030
2bb34f36db80 include names need not be considered as reserved any longer
haftmann
parents: 39024
diff changeset
   357
      (make_vars reserved_syms) args_num is_singleton_constr;
38769
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   358
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   359
    (* print nodes *)
38856
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   360
    fun print_module base implicit_ps p = Pretty.chunks2
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   361
      ([str ("object " ^ base ^ " {")]
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   362
        @ (if null implicit_ps then [] else (single o Pretty.block)
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   363
            (str "import /*implicits*/" :: Pretty.brk 1 :: commas implicit_ps))
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   364
        @ [p, str ("} /* object " ^ base ^ " */")]);
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   365
    fun print_implicit prefix_fragments implicit =
38782
3865cbe5d2be only print qualified implicits
haftmann
parents: 38780
diff changeset
   366
      let
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   367
        val s = deresolver prefix_fragments implicit;
38782
3865cbe5d2be only print qualified implicits
haftmann
parents: 38780
diff changeset
   368
      in if length (Long_Name.explode s) = 1 then NONE else SOME (str s) end;
39023
3f70c03e8282 simultaneous modification of statements
haftmann
parents: 39022
diff changeset
   369
    fun print_node _ (_, Code_Namespace.Dummy) = NONE
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   370
      | print_node prefix_fragments (name, Code_Namespace.Stmt stmt) =
38926
24f82786cc57 record argument for serializers
haftmann
parents: 38924
diff changeset
   371
          if null presentation_names
24f82786cc57 record argument for serializers
haftmann
parents: 38924
diff changeset
   372
          orelse member (op =) presentation_names name
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   373
          then SOME (print_stmt (deresolver prefix_fragments, deresolver []) (name, stmt))
38769
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   374
          else NONE
38970
53d1ee3d98b8 factored out generic part of Scala serializer into code_namespace.ML
haftmann
parents: 38966
diff changeset
   375
      | print_node prefix_fragments (name_fragment, Code_Namespace.Module (implicits, nodes)) =
38926
24f82786cc57 record argument for serializers
haftmann
parents: 38924
diff changeset
   376
          if null presentation_names
38856
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   377
          then
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   378
            let
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   379
              val prefix_fragments' = prefix_fragments @ [name_fragment];
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   380
            in
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   381
              Option.map (print_module name_fragment
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   382
                (map_filter (print_implicit prefix_fragments') implicits))
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   383
                  (print_nodes prefix_fragments' nodes)
168dba35ecf3 improved deresolving of implicits
haftmann
parents: 38809
diff changeset
   384
            end
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   385
          else print_nodes [] nodes
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   386
    and print_nodes prefix_fragments nodes = let
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   387
        val ps = map_filter (fn name => print_node prefix_fragments (name,
38769
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   388
          snd (Graph.get_node nodes name)))
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   389
            ((rev o flat o Graph.strong_conn) nodes);
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   390
      in if null ps then NONE else SOME (Pretty.chunks2 ps) end;
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   391
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   392
    (* serialization *)
38968
e55deaa22fff do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents: 38966
diff changeset
   393
    val p_includes = if null presentation_names then map snd includes else [];
38809
7dc73a208722 proper namespace administration for hierarchical modules
haftmann
parents: 38782
diff changeset
   394
    val p = Pretty.chunks2 (p_includes @ the_list (print_nodes [] sca_program));
39034
ebeb48fd653b formal framework for presentation of selected statements
haftmann
parents: 39030
diff changeset
   395
    fun write width NONE = writeln o format false width
ebeb48fd653b formal framework for presentation of selected statements
haftmann
parents: 39030
diff changeset
   396
      | write width (SOME p) = File.write p o format false width;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   397
  in
39034
ebeb48fd653b formal framework for presentation of selected statements
haftmann
parents: 39030
diff changeset
   398
    Code_Target.serialization write (rpair [] ooo format) p
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   399
  end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   400
38966
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38928
diff changeset
   401
val serializer : Code_Target.serializer =
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38928
diff changeset
   402
  Code_Target.parse_args (Scan.succeed ()) #> K serialize_scala;
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38928
diff changeset
   403
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   404
val literals = let
37224
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   405
  fun char_scala c = if c = "'" then "\\'"
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   406
    else if c = "\"" then "\\\""
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   407
    else if c = "\\" then "\\\\"
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   408
    else let val k = ord c
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   409
    in if k < 32 orelse k > 126 then "\\" ^ radixstring (8, "0", k) else c end
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   410
  fun numeral_scala k = if k < 0
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37932
diff changeset
   411
    then if k > ~ 2147483647 then "- " ^ string_of_int (~ k)
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   412
      else quote ("- " ^ string_of_int (~ k))
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   413
    else if k <= 2147483647 then string_of_int k
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   414
      else quote (string_of_int k)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   415
in Literals {
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   416
  literal_char = Library.enclose "'" "'" o char_scala,
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   417
  literal_string = quote o translate_string char_scala,
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   418
  literal_numeral = fn k => "BigInt(" ^ numeral_scala k ^ ")",
38968
e55deaa22fff do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents: 38966
diff changeset
   419
  literal_positive_numeral = fn k => "Nat(" ^ numeral_scala k ^ ")",
e55deaa22fff do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents: 38966
diff changeset
   420
  literal_alternative_numeral = fn k => "Natural(" ^ numeral_scala k ^ ")",
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37932
diff changeset
   421
  literal_naive_numeral = fn k => "BigInt(" ^ numeral_scala k ^ ")",
34888
460ec1a99aa2 being more accurate wrt. list syntax
haftmann
parents: 34308
diff changeset
   422
  literal_list = fn [] => str "Nil" | ps => Pretty.block [str "List", enum "," "(" ")" ps],
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   423
  infix_cons = (6, "::")
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   424
} end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   425
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   426
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   427
(** Isar setup **)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   428
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   429
val setup =
37821
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   430
  Code_Target.add_target
38966
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38928
diff changeset
   431
    (target, { serializer = serializer, literals = literals,
38769
317e64c886d2 preliminary implementation of hierarchical module name space
haftmann
parents: 38059
diff changeset
   432
      check = { env_var = "SCALA_HOME", make_destination = fn p => Path.append p (Path.explode "ROOT.scala"),
38863
9070a7c356c9 code checking: compiler invocation happens in same directory as generated file -- avoid problem with different path representations on cygwin
haftmann
parents: 38856
diff changeset
   433
        make_command = fn scala_home => fn _ =>
37932
d00a3f47b607 more generous memory settings for scala check
haftmann
parents: 37893
diff changeset
   434
          "export JAVA_OPTS='-Xms128m -Xmx512m -Xss2m' && "
38863
9070a7c356c9 code checking: compiler invocation happens in same directory as generated file -- avoid problem with different path representations on cygwin
haftmann
parents: 38856
diff changeset
   435
            ^ Path.implode (Path.append (Path.explode scala_home) (Path.explode "bin/scalac")) ^ " ROOT.scala" } })
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   436
  #> Code_Target.add_tyco_syntax target "fun"
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   437
     (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   438
        brackify_infix (1, R) fxy (
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   439
          print_typ BR ty1 (*product type vs. tupled arguments!*),
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   440
          str "=>",
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   441
          print_typ (INFX (1, R)) ty2
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   442
        )))
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   443
  #> fold (Code_Target.add_reserved target) [
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   444
      "abstract", "case", "catch", "class", "def", "do", "else", "extends", "false",
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   445
      "final", "finally", "for", "forSome", "if", "implicit", "import", "lazy",
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   446
      "match", "new", "null", "object", "override", "package", "private", "protected",
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   447
      "requires", "return", "sealed", "super", "this", "throw", "trait", "try",
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
   448
      "true", "type", "val", "var", "while", "with", "yield"
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   449
    ]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   450
  #> fold (Code_Target.add_reserved target) [
37639
5b6733e6e033 pervasive tuning of code
haftmann
parents: 37464
diff changeset
   451
      "apply", "error", "BigInt", "Nil", "List"
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   452
    ];
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   453
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   454
end; (*struct*)