src/Tools/Code/code_scala.ML
author haftmann
Fri, 18 Jun 2010 15:59:51 +0200
changeset 37464 9250ad1b98e0
parent 37453 44a307746163
child 37639 5b6733e6e033
permissions -rw-r--r--
tuned whitespace; dropped dead code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     1
(*  Author:     Florian Haftmann, TU Muenchen
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     2
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     3
Serializer for Scala.
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     4
*)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     5
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     6
signature CODE_SCALA =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     7
sig
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     8
  val setup: theory -> theory
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
     9
end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    10
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    11
structure Code_Scala : CODE_SCALA =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    12
struct
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    13
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    14
val target = "Scala";
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    15
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    16
open Basic_Code_Thingol;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    17
open Code_Printer;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    18
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    19
infixr 5 @@;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    20
infixr 5 @|;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    21
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    22
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    23
(** Scala serializer **)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    24
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    25
fun print_scala_stmt labelled_name syntax_tyco syntax_const reserved
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    26
    args_num is_singleton deresolve =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    27
  let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    28
    val deresolve_base = Long_Name.base_name o deresolve;
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
    29
    val lookup_tyvar = first_upper oo lookup_var;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    30
    fun print_typ tyvars fxy (tycoexpr as tyco `%% tys) = (case syntax_tyco tyco
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    31
         of NONE => applify "[" "]" fxy
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    32
              ((str o deresolve) tyco) (map (print_typ tyvars NOBR) tys)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    33
          | SOME (i, print) => print (print_typ tyvars) fxy tys)
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
    34
      | print_typ tyvars fxy (ITyVar v) = (str o lookup_tyvar tyvars) v;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    35
    fun print_typed tyvars p ty =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    36
      Pretty.block [p, str ":", Pretty.brk 1, print_typ tyvars NOBR ty]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    37
    fun print_var vars NONE = str "_"
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    38
      | print_var vars (SOME v) = (str o lookup_var vars) v
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    39
    fun print_term tyvars is_pat some_thm vars fxy (IConst c) =
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    40
          print_app tyvars is_pat some_thm vars fxy (c, [])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    41
      | print_term tyvars is_pat some_thm vars fxy (t as (t1 `$ t2)) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    42
          (case Code_Thingol.unfold_const_app t
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    43
           of SOME app => print_app tyvars is_pat some_thm vars fxy app
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    44
            | _ => applify "(" ")" fxy
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    45
                (print_term tyvars is_pat some_thm vars BR t1)
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    46
                [print_term tyvars is_pat some_thm vars NOBR t2])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    47
      | print_term tyvars is_pat some_thm vars fxy (IVar v) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    48
          print_var vars v
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    49
      | print_term tyvars is_pat some_thm vars fxy ((v, ty) `|=> t) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    50
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    51
            val vars' = intro_vars (the_list v) vars;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    52
          in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    53
            concat [
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    54
              Pretty.block [str "(", print_typed tyvars (print_var vars' v) ty, str ")"],
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    55
              str "=>",
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    56
              print_term tyvars false some_thm vars' NOBR t
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    57
            ]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    58
          end 
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    59
      | print_term tyvars is_pat some_thm vars fxy (ICase (cases as (_, t0))) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    60
          (case Code_Thingol.unfold_const_app t0
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    61
           of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    62
                then print_case tyvars some_thm vars fxy cases
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    63
                else print_app tyvars is_pat some_thm vars fxy c_ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    64
            | NONE => print_case tyvars some_thm vars fxy cases)
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    65
    and print_app tyvars is_pat some_thm vars fxy
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    66
        (app as ((c, ((arg_typs, _), function_typs)), ts)) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    67
      let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    68
        val k = length ts;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    69
        val l = case syntax_const c of NONE => args_num c | SOME (l, _) => l;
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
    70
        val arg_typs' = if is_pat orelse
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
    71
          (is_none (syntax_const c) andalso is_singleton c) then [] else arg_typs;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    72
        val (no_syntax, print') = case syntax_const c
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    73
         of NONE => (true, fn ts => applify "(" ")" fxy
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    74
              (applify "[" "]" NOBR ((str o deresolve) c)
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    75
                (map (print_typ tyvars NOBR) arg_typs'))
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    76
                  (map (print_term tyvars is_pat some_thm vars NOBR) ts))
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    77
          | SOME (_, print) => (false, fn ts =>
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    78
              print (print_term tyvars is_pat some_thm) some_thm vars fxy
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    79
                (ts ~~ take l function_typs));
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    80
      in if k = l then print' ts
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    81
      else if k < l then
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    82
        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
    83
      else let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    84
        val (ts1, ts23) = chop l ts;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    85
      in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    86
        Pretty.block (print' ts1 :: map (fn t => Pretty.block
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    87
          [str ".apply(", print_term tyvars is_pat some_thm vars NOBR t, str ")"]) ts23)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    88
      end end
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    89
    and print_bind tyvars some_thm fxy p =
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
    90
      gen_print_bind (print_term tyvars true) some_thm fxy p
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    91
    and print_case tyvars some_thm vars fxy (cases as ((_, [_]), _)) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    92
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    93
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    94
            fun print_match ((pat, ty), t) vars =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    95
              vars
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    96
              |> print_bind tyvars some_thm BR pat
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    97
              |>> (fn p => semicolon [Pretty.block [str "val", Pretty.brk 1, p,
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
    98
                str ":", Pretty.brk 1, print_typ tyvars NOBR ty],
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    99
                  str "=", print_term tyvars false some_thm vars NOBR t])
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   100
            val (ps, vars') = fold_map print_match binds vars;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   101
          in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   102
            brackify_block fxy
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   103
              (str "{")
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   104
              (ps @| print_term tyvars false some_thm vars' NOBR body)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   105
              (str "}")
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   106
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   107
      | print_case tyvars some_thm vars fxy (((t, ty), clauses as _ :: _), _) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   108
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   109
            fun print_select (pat, body) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   110
              let
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   111
                val (p_pat, vars') = print_bind tyvars some_thm NOBR pat vars;
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   112
                val p_body = print_term tyvars false some_thm vars' NOBR body
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   113
              in concat [str "case", p_pat, str "=>", p_body] end;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   114
          in brackify_block fxy
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   115
            (concat [print_term tyvars false some_thm vars NOBR t, str "match", str "{"])
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   116
            (map print_select clauses)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   117
            (str "}") 
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   118
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   119
      | print_case tyvars some_thm vars fxy ((_, []), _) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   120
          (brackify fxy o Pretty.breaks o map str) ["error(\"empty case\")"];
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   121
    fun print_defhead tyvars vars p vs params tys implicits (*FIXME simplify*) ty =
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
   122
      Pretty.block [str "def ", print_typed tyvars (applify "(implicit " ")" NOBR
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   123
        (applify "(" ")" NOBR
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   124
          (applify "[" "]" NOBR p (map (fn (v, sort) =>
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   125
              (str o implode)
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   126
                (lookup_tyvar tyvars v :: map (prefix ": " o deresolve) sort)) vs))
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   127
            (map2 (fn param => fn ty => print_typed tyvars
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   128
                ((str o lookup_var vars) param) ty)
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
   129
              params tys)) implicits) ty, str " ="]
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   130
    fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) =
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   131
        (case filter (snd o snd) raw_eqs
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   132
         of [] =>
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   133
              let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   134
                val (tys, ty') = Code_Thingol.unfold_fun ty;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   135
                val params = Name.invents (snd reserved) "a" (length tys);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   136
                val tyvars = intro_vars (map fst vs) reserved;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   137
                val vars = intro_vars params reserved;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   138
              in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   139
                concat [print_defhead tyvars vars ((str o deresolve) name) vs params tys [] ty',
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   140
                  str ("error(\"" ^ name ^ "\")")]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   141
              end
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   142
          | eqs =>
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   143
              let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   144
                val tycos = fold (fn ((ts, t), _) =>
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   145
                  fold Code_Thingol.add_tyconames (t :: ts)) eqs [];
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   146
                val tyvars = reserved
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   147
                  |> intro_base_names
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   148
                       (is_none o syntax_tyco) deresolve tycos
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   149
                  |> intro_vars (map fst vs);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   150
                val simple = case eqs
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   151
                 of [((ts, _), _)] => forall Code_Thingol.is_IVar ts
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   152
                  | _ => false;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   153
                val consts = fold Code_Thingol.add_constnames
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   154
                  (map (snd o fst) eqs) [];
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   155
                val vars1 = reserved
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   156
                  |> intro_base_names
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   157
                       (is_none o syntax_const) deresolve consts
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   158
                val params = if simple
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   159
                  then (map (fn IVar (SOME x) => x) o fst o fst o hd) eqs
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   160
                  else aux_params vars1 (map (fst o fst) eqs);
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   161
                val vars2 = intro_vars params vars1;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   162
                val (tys, ty1) = Code_Thingol.unfold_fun ty;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   163
                val (tys1, tys2) = chop (length params) tys;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   164
                val ty2 = Library.foldr
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   165
                  (fn (ty1, ty2) => Code_Thingol.fun_tyco `%% [ty1, ty2]) (tys2, ty1);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   166
                fun print_tuple [p] = p
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   167
                  | print_tuple ps = enum "," "(" ")" ps;
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   168
                fun print_rhs vars' ((_, t), (some_thm, _)) =
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   169
                  print_term tyvars false some_thm vars' NOBR t;
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   170
                fun print_clause (eq as ((ts, _), (some_thm, _))) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   171
                  let
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   172
                    val vars' = intro_vars ((fold o Code_Thingol.fold_varnames)
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   173
                      (insert (op =)) ts []) vars1;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   174
                  in
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   175
                    concat [str "case",
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   176
                      print_tuple (map (print_term tyvars true some_thm vars' NOBR) ts),
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   177
                      str "=>", print_rhs vars' eq]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   178
                  end;
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   179
                val head = print_defhead tyvars vars2 ((str o deresolve) name) vs params tys1 [] ty2;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   180
              in if simple then
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   181
                concat [head, print_rhs vars2 (hd eqs)]
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   182
              else
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   183
                Pretty.block_enclose
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   184
                  (concat [head, print_tuple (map (str o lookup_var vars2) params),
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   185
                    str "match", str "{"], str "}")
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   186
                  (map print_clause eqs)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   187
              end)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   188
      | print_stmt (name, Code_Thingol.Datatype (_, (vs, cos))) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   189
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   190
            val tyvars = intro_vars (map fst vs) reserved;
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   191
            fun print_co ((co, _), []) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   192
                  concat [str "final", str "case", str "object", (str o deresolve_base) co,
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   193
                    str "extends", applify "[" "]" NOBR ((str o deresolve_base) name)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   194
                      (replicate (length vs) (str "Nothing"))]
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   195
              | print_co ((co, vs_args), tys) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   196
                  let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   197
                    val fields = Name.names (snd reserved) "a" tys;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   198
                    val vars = intro_vars (map fst fields) reserved;
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   199
                    fun add_typargs1 p = applify "[" "]" NOBR p
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   200
                      (map (str o lookup_tyvar tyvars o fst) vs); (*FIXME*)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   201
                    fun add_typargs2 p = applify "[" "]" NOBR p
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   202
                      (map (str o lookup_tyvar tyvars) vs_args); (*FIXME*)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   203
                  in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   204
                    concat [
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   205
                      applify "(" ")" NOBR
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   206
                        (add_typargs2 ((concat o map str)
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   207
                          ["final", "case", "class", deresolve_base co]))
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   208
                        (map (uncurry (print_typed tyvars) o apfst str) fields),
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   209
                      str "extends",
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   210
                      add_typargs1 ((str o deresolve_base) name)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   211
                    ]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   212
                  end
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   213
          in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   214
            Pretty.chunks (
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   215
              applify "[" "]" NOBR ((concat o map str)
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   216
                  ["sealed", "class", deresolve_base name])
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
   217
                (map (str o prefix "+" o lookup_tyvar tyvars o fst) vs)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   218
              :: map print_co cos
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   219
            )
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
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   223
            val tyvars = intro_vars [v] reserved;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   224
            val vs = [(v, [name])];
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
   225
            fun add_typarg p = applify "[" "]" NOBR p [(str o lookup_tyvar tyvars) v];
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"
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   228
                  :: separate (str "with")
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   229
                    (map (add_typarg o str o deresolve o fst) classes)));
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   230
            fun print_tupled_typ ([], ty) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   231
                  print_typ tyvars NOBR ty
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   232
              | print_tupled_typ ([ty1], ty2) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   233
                  concat [print_typ tyvars BR ty1, str "=>", print_typ tyvars NOBR ty2]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   234
              | print_tupled_typ (tys, ty) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   235
                  concat [enum "," "(" ")" (map (print_typ tyvars NOBR) tys),
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   236
                    str "=>", print_typ tyvars NOBR ty];
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   237
            fun print_classparam_val (classparam, ty) =
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   238
              concat [str "val", (str o Library.enclose "`" "+`:" o deresolve_base)
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   239
                  classparam,
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   240
                (print_tupled_typ o Code_Thingol.unfold_fun) ty]
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   241
            fun implicit_arguments tyvars vs vars = (*FIXME simplifiy*)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   242
              let
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   243
                val implicit_typ_ps = maps (fn (v, sort) => map (fn class => Pretty.block
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   244
                  [(str o deresolve) class, str "[",
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   245
                    (str o lookup_tyvar tyvars) v, str "]"]) sort) vs;
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   246
                val implicit_names = Name.variant_list [] (maps (fn (v, sort) =>
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   247
                  map (fn class => lookup_tyvar tyvars v ^ "_" ^
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   248
                    (Long_Name.base_name o deresolve) class) sort) vs);
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   249
                val vars' = intro_vars implicit_names vars;
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   250
                val implicit_ps = map2 (fn v => fn p => concat [str (v ^ ":"), p])
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   251
                  implicit_names implicit_typ_ps;
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   252
              in ((implicit_names, implicit_ps), vars') end;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   253
            fun print_classparam_def (classparam, ty) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   254
              let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   255
                val (tys, ty) = Code_Thingol.unfold_fun ty;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   256
                val params = Name.invents (snd reserved) "a" (length tys);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   257
                val vars = intro_vars params reserved;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   258
                val (([implicit], [p_implicit]), vars') = implicit_arguments tyvars vs vars;
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   259
                val head = print_defhead tyvars vars' ((str o deresolve) classparam)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   260
                  ((map o apsnd) (K []) vs) params tys [p_implicit] ty;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   261
              in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   262
                concat [head, applify "(" ")" NOBR
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   263
                  (Pretty.block [str implicit, str ".",
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   264
                    (str o Library.enclose "`" "+`" o deresolve_base) classparam])
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   265
                  (map (str o lookup_var vars') params)]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   266
              end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   267
          in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   268
            Pretty.chunks (
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   269
              (Pretty.block_enclose
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   270
                (concat ([str "trait", add_typarg ((str o deresolve_base) name)]
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37337
diff changeset
   271
                  @ the_list (print_super_classes super_classes) @ [str "{"]), str "}")
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   272
                (map print_classparam_val classparams))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   273
              :: map print_classparam_def classparams
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   274
            )
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   275
          end
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   276
      | print_stmt (name, Code_Thingol.Classinst ((class, (tyco, vs)),
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   277
            (super_instances, (classparam_instances, further_classparam_instances)))) =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   278
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   279
            val tyvars = intro_vars (map fst vs) reserved;
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   280
            val classtyp = (class, (tyco, map fst vs));
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   281
            fun print_classtyp' (class, (tyco, vs)) = (*FIXME already exists?*)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   282
              applify "[" "]" NOBR(*? FIXME*) ((str o deresolve) class)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   283
                [print_typ tyvars NOBR (tyco `%% map ITyVar vs)]; (*FIXME a mess...*)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   284
            fun print_typed' tyvars p classtyp = (*FIXME unify with existing print_typed*)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   285
              Pretty.block [p, str ":", Pretty.brk 1, print_classtyp' classtyp];
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   286
            fun print_defhead' tyvars vars p vs params tys classtyp = (*FIXME unify with existing def_head*)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   287
              Pretty.block [str "def ", print_typed' tyvars
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   288
                (applify "(" ")" NOBR
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   289
                  (applify "[" "]" NOBR p (map (fn (v, sort) =>
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   290
                      (str o implode) (lookup_tyvar tyvars v :: map (prefix ": " o deresolve) sort)) vs))
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   291
                    (map2 (fn param => fn ty => print_typed tyvars ((str o lookup_var vars) param) ty)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   292
                      params tys)) classtyp, str " ="];
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   293
            fun print_classparam_instance ((classparam, const as (_, (_, tys))), (thm, _)) =
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   294
              let
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   295
                val auxs = Name.invents (snd reserved) "a" (length tys);
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   296
                val vars = intro_vars auxs reserved;
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   297
                val args = if null auxs then [] else
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   298
                  [concat [enum "," "(" ")" (map2 (fn aux => fn ty => print_typed tyvars ((str o lookup_var vars) aux) ty)
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   299
                    auxs tys), str "=>"]];
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   300
              in 
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   301
                concat ([str "val", (str o Library.enclose "`" "+`" o deresolve_base) classparam,
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   302
                  str "="] @ args @ [print_app tyvars false (SOME thm) vars NOBR (const, map (IVar o SOME) auxs)])
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   303
              end;
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   304
            val head = print_defhead' tyvars reserved ((str o deresolve) name) vs [] [] classtyp;
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   305
            val body = [str "new", print_classtyp' classtyp,
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   306
              Pretty.enum ";" "{ " " }" (map print_classparam_instance (classparam_instances @ further_classparam_instances))];
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   307
          in concat (str "implicit" :: head :: body) end;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   308
  in print_stmt end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   309
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   310
fun scala_program_of_program labelled_name module_name reserved raw_module_alias program =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   311
  let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   312
    val the_module_name = the_default "Program" module_name;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   313
    val module_alias = K (SOME the_module_name);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   314
    val reserved = Name.make_context reserved;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   315
    fun prepare_stmt (name, stmt) (nsps, stmts) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   316
      let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   317
        val (_, base) = Code_Printer.dest_name name;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   318
        val mk_name_stmt = yield_singleton Name.variants;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   319
        fun add_class ((nsp_class, nsp_object), nsp_common) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   320
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   321
            val (base', nsp_class') = mk_name_stmt base nsp_class
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   322
          in (base', ((nsp_class', nsp_object), Name.declare base' nsp_common)) end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   323
        fun add_object ((nsp_class, nsp_object), nsp_common) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   324
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   325
            val (base', nsp_object') = mk_name_stmt base nsp_object
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   326
          in (base', ((nsp_class, nsp_object'), Name.declare base' nsp_common)) end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   327
        fun add_common upper ((nsp_class, nsp_object), nsp_common) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   328
          let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   329
            val (base', nsp_common') =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   330
              mk_name_stmt (if upper then first_upper base else base) nsp_common
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   331
          in
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   332
            (base',
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   333
              ((Name.declare base' nsp_class, Name.declare base' nsp_object), nsp_common'))
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   334
          end;
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   335
        val add_name = case stmt
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   336
         of Code_Thingol.Fun _ => add_object
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   337
          | Code_Thingol.Datatype _ => add_class
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   338
          | Code_Thingol.Datatypecons _ => add_common true
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   339
          | Code_Thingol.Class _ => add_class
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   340
          | Code_Thingol.Classrel _ => add_object
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   341
          | Code_Thingol.Classparam _ => add_object
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   342
          | Code_Thingol.Classinst _ => add_common false;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   343
        fun add_stmt base' = case stmt
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   344
         of Code_Thingol.Datatypecons _ => cons (name, (base', NONE))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   345
          | Code_Thingol.Classrel _ => cons (name, (base', NONE))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   346
          | Code_Thingol.Classparam _ => cons (name, (base', NONE))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   347
          | _ => cons (name, (base', SOME stmt));
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   348
      in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   349
        nsps
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   350
        |> add_name
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   351
        |-> (fn base' => rpair (add_stmt base' stmts))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   352
      end;
37439
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   353
    val stmts = AList.make (Graph.get_node program) (Graph.strong_conn program |> flat)
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   354
      |> filter_out (Code_Thingol.is_case o snd);
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   355
    val (_, sca_program) = fold prepare_stmt stmts (((reserved, reserved), reserved), []);
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   356
    fun deresolver name = (fst o the o AList.lookup (op =) sca_program) name
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   357
      handle Option => error ("Unknown statement name: " ^ labelled_name name);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   358
  in (deresolver, (the_module_name, sca_program)) end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   359
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   360
fun serialize_scala raw_module_name labelled_name
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   361
    raw_reserved includes raw_module_alias
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   362
    _ syntax_tyco syntax_const (code_of_pretty, code_writeln)
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   363
    program stmt_names destination =
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   364
  let
36535
0195ef994077 more coherent naming with ML serializer
haftmann
parents: 35228
diff changeset
   365
    val presentation_stmt_names = Code_Target.stmt_names_of_destination destination;
0195ef994077 more coherent naming with ML serializer
haftmann
parents: 35228
diff changeset
   366
    val module_name = if null presentation_stmt_names then raw_module_name else SOME "Code";
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   367
    val reserved = fold (insert (op =) o fst) includes raw_reserved;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   368
    val (deresolver, (the_module_name, sca_program)) = scala_program_of_program labelled_name
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   369
      module_name reserved raw_module_alias program;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   370
    val reserved = make_vars reserved;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   371
    fun args_num c = case Graph.get_node program c
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   372
     of Code_Thingol.Fun (_, (((_, ty), []), _)) =>
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   373
          (length o fst o Code_Thingol.unfold_fun) ty
37437
4202e11ae7dc formal introduction of case cong
haftmann
parents: 37384
diff changeset
   374
      | Code_Thingol.Fun (_, ((_, ((ts, _), _) :: _), _)) => length ts
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   375
      | Code_Thingol.Datatypecons (_, tyco) =>
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   376
          let val Code_Thingol.Datatype (_, (_, constrs)) = Graph.get_node program tyco
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   377
          in (length o the o AList.lookup (eq_fst op =) constrs) (c, []) end (*FIXME simplify lookup*)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   378
      | Code_Thingol.Classparam (_, class) =>
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   379
          let
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   380
            val Code_Thingol.Class (_, (_, (_, classparams))) =
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   381
              Graph.get_node program class
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   382
          in (length o fst o Code_Thingol.unfold_fun o the o AList.lookup (op =) classparams) c end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   383
    fun is_singleton c = case Graph.get_node program c
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   384
     of Code_Thingol.Datatypecons (_, tyco) =>
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   385
          let val Code_Thingol.Datatype (_, (_, constrs)) = Graph.get_node program tyco
37450
45073611170a first serious draft of a scala code generator
haftmann
parents: 37447
diff changeset
   386
          in (null o the o AList.lookup (eq_fst op =) constrs) (c, []) end (*FIXME simplify lookup*)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   387
      | _ => false;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   388
    val print_stmt = print_scala_stmt labelled_name syntax_tyco syntax_const
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   389
      reserved args_num is_singleton deresolver;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   390
    fun print_module name content =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   391
      (name, Pretty.chunks [
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   392
        str ("object " ^ name ^ " {"),
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   393
        str "",
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   394
        content,
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   395
        str "",
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   396
        str "}"
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   397
      ]);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   398
    fun serialize_module the_module_name sca_program =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   399
      let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   400
        val content = Pretty.chunks2 (map_filter
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   401
          (fn (name, (_, SOME stmt)) => SOME (print_stmt (name, stmt))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   402
            | (_, (_, NONE)) => NONE) sca_program);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   403
      in print_module the_module_name content end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   404
    fun check_destination destination =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   405
      (File.check destination; destination);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   406
    fun write_module destination (modlname, content) =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   407
      let
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   408
        val filename = case modlname
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   409
         of "" => Path.explode "Main.scala"
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   410
          | _ => (Path.ext "scala" o Path.explode o implode o separate "/"
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   411
                o Long_Name.explode) modlname;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   412
        val pathname = Path.append destination filename;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   413
        val _ = File.mkdir (Path.dir pathname);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   414
      in File.write pathname (code_of_pretty content) end
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   415
  in
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   416
    Code_Target.mk_serialization target NONE
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   417
      (fn NONE => K () o map (code_writeln o snd) | SOME file => K () o map
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   418
        (write_module (check_destination file)))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   419
      (rpair [] o cat_lines o map (code_of_pretty o snd))
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   420
      (map (uncurry print_module) includes
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   421
        @| serialize_module the_module_name sca_program)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   422
      destination
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   423
  end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   424
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   425
val literals = let
37224
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   426
  fun char_scala c = if c = "'" then "\\'"
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   427
    else if c = "\"" then "\\\""
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   428
    else if c = "\\" then "\\\\"
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   429
    else let val k = ord c
f4d3c929c526 corrected printing of characters
haftmann
parents: 36535
diff changeset
   430
    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
   431
  fun numeral_scala k = if k < 0
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   432
    then if k <= 2147483647 then "- " ^ string_of_int (~ k)
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   433
      else quote ("- " ^ string_of_int (~ k))
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   434
    else if k <= 2147483647 then string_of_int k
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   435
      else quote (string_of_int k)
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   436
in Literals {
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   437
  literal_char = Library.enclose "'" "'" o char_scala,
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   438
  literal_string = quote o translate_string char_scala,
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   439
  literal_numeral = fn k => "BigInt(" ^ numeral_scala k ^ ")",
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   440
  literal_positive_numeral = fn k => "Nat.Nat(" ^ numeral_scala k ^ ")",
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   441
  literal_naive_numeral = fn k => if k >= 0
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   442
    then string_of_int k else "(- " ^ string_of_int (~ k) ^ ")",
34888
460ec1a99aa2 being more accurate wrt. list syntax
haftmann
parents: 34308
diff changeset
   443
  literal_list = fn [] => str "Nil" | ps => Pretty.block [str "List", enum "," "(" ")" ps],
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   444
  infix_cons = (6, "::")
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   445
} end;
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   446
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   447
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   448
(** Isar setup **)
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   449
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   450
fun isar_seri_scala module_name =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   451
  Code_Target.parse_args (Scan.succeed ())
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   452
  #> (fn () => serialize_scala module_name);
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   453
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   454
val setup =
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   455
  Code_Target.add_target (target, (isar_seri_scala, literals))
37464
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   456
  #> Code_Target.add_syntax_tyco target "fun"
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   457
     (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   458
        brackify_infix (1, R) fxy (
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   459
          print_typ BR ty1 (*product type vs. tupled arguments!*),
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   460
          str "=>",
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   461
          print_typ (INFX (1, R)) ty2
9250ad1b98e0 tuned whitespace; dropped dead code
haftmann
parents: 37453
diff changeset
   462
        )))
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   463
  #> fold (Code_Target.add_reserved target) [
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   464
      "abstract", "case", "catch", "class", "def", "do", "else", "extends", "false",
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   465
      "final", "finally", "for", "forSome", "if", "implicit", "import", "lazy",
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   466
      "match", "new", "null", "object", "override", "package", "private", "protected",
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   467
      "requires", "return", "sealed", "super", "this", "throw", "trait", "try",
37243
6e2ac5358d6e capitalized type variables; added yield as keyword
haftmann
parents: 37224
diff changeset
   468
      "true", "type", "val", "var", "while", "with", "yield"
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   469
    ]
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   470
  #> fold (Code_Target.add_reserved target) [
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34900
diff changeset
   471
      "error", "apply", "List", "Nil", "BigInt"
34294
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   472
    ];
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   473
19c1fd52d6c9 a primitive scala serializer
haftmann
parents:
diff changeset
   474
end; (*struct*)