src/Tools/Code/code_haskell.ML
author haftmann
Tue, 07 Sep 2010 11:08:57 +0200
changeset 39432 dd0660d93c31
parent 39381 f63715f00fdd
child 39434 3d30f501b7c2
permissions -rw-r--r--
added generic flat_program procedure
haftmann@37744
     1
(*  Title:      Tools/Code/code_haskell.ML
haftmann@28054
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@28054
     3
haftmann@28054
     4
Serializer for Haskell.
haftmann@28054
     5
*)
haftmann@28054
     6
haftmann@28054
     7
signature CODE_HASKELL =
haftmann@28054
     8
sig
haftmann@37744
     9
  val target: string
haftmann@28054
    10
  val setup: theory -> theory
haftmann@28054
    11
end;
haftmann@28054
    12
haftmann@28054
    13
structure Code_Haskell : CODE_HASKELL =
haftmann@28054
    14
struct
haftmann@28054
    15
haftmann@28054
    16
val target = "Haskell";
haftmann@28054
    17
haftmann@28054
    18
open Basic_Code_Thingol;
haftmann@28054
    19
open Code_Printer;
haftmann@28054
    20
haftmann@28054
    21
infixr 5 @@;
haftmann@28054
    22
infixr 5 @|;
haftmann@28054
    23
haftmann@28054
    24
haftmann@28054
    25
(** Haskell serializer **)
haftmann@28054
    26
haftmann@39149
    27
fun print_haskell_stmt labelled_name class_syntax tyco_syntax const_syntax
haftmann@32924
    28
    reserved deresolve contr_classparam_typs deriving_show =
haftmann@28054
    29
  let
wenzelm@30364
    30
    val deresolve_base = Long_Name.base_name o deresolve;
haftmann@39149
    31
    fun class_name class = case class_syntax class
haftmann@28054
    32
     of NONE => deresolve class
haftmann@28687
    33
      | SOME class => class;
haftmann@33988
    34
    fun print_typcontext tyvars vs = case maps (fn (v, sort) => map (pair v) sort) vs
haftmann@28054
    35
     of [] => []
haftmann@37359
    36
      | constraints => enum "," "(" ")" (
haftmann@28054
    37
          map (fn (v, class) =>
haftmann@37359
    38
            str (class_name class ^ " " ^ lookup_var tyvars v)) constraints)
haftmann@28054
    39
          @@ str " => ";
haftmann@33988
    40
    fun print_typforall tyvars vs = case map fst vs
haftmann@28054
    41
     of [] => []
haftmann@28054
    42
      | vnames => str "forall " :: Pretty.breaks
haftmann@32924
    43
          (map (str o lookup_var tyvars) vnames) @ str "." @@ Pretty.brk 1;
haftmann@33988
    44
    fun print_tyco_expr tyvars fxy (tyco, tys) =
haftmann@33988
    45
      brackify fxy (str tyco :: map (print_typ tyvars BR) tys)
haftmann@39149
    46
    and print_typ tyvars fxy (tycoexpr as tyco `%% tys) = (case tyco_syntax tyco
haftmann@33988
    47
         of NONE => print_tyco_expr tyvars fxy (deresolve tyco, tys)
haftmann@33988
    48
          | SOME (i, print) => print (print_typ tyvars) fxy tys)
haftmann@33988
    49
      | print_typ tyvars fxy (ITyVar v) = (str o lookup_var tyvars) v;
haftmann@33988
    50
    fun print_typdecl tyvars (vs, tycoexpr) =
haftmann@33988
    51
      Pretty.block (print_typcontext tyvars vs @| print_tyco_expr tyvars NOBR tycoexpr);
haftmann@33988
    52
    fun print_typscheme tyvars (vs, ty) =
haftmann@33988
    53
      Pretty.block (print_typforall tyvars vs @ print_typcontext tyvars vs @| print_typ tyvars NOBR ty);
haftmann@35228
    54
    fun print_term tyvars some_thm vars fxy (IConst c) =
haftmann@35228
    55
          print_app tyvars some_thm vars fxy (c, [])
haftmann@35228
    56
      | print_term tyvars some_thm vars fxy (t as (t1 `$ t2)) =
haftmann@28054
    57
          (case Code_Thingol.unfold_const_app t
haftmann@35228
    58
           of SOME app => print_app tyvars some_thm vars fxy app
haftmann@28054
    59
            | _ =>
haftmann@28054
    60
                brackify fxy [
haftmann@35228
    61
                  print_term tyvars some_thm vars NOBR t1,
haftmann@35228
    62
                  print_term tyvars some_thm vars BR t2
haftmann@28054
    63
                ])
haftmann@35228
    64
      | print_term tyvars some_thm vars fxy (IVar NONE) =
haftmann@31889
    65
          str "_"
haftmann@35228
    66
      | print_term tyvars some_thm vars fxy (IVar (SOME v)) =
haftmann@32924
    67
          (str o lookup_var vars) v
haftmann@35228
    68
      | print_term tyvars some_thm vars fxy (t as _ `|=> _) =
haftmann@28054
    69
          let
haftmann@31873
    70
            val (binds, t') = Code_Thingol.unfold_pat_abs t;
haftmann@35228
    71
            val (ps, vars') = fold_map (print_bind tyvars some_thm BR o fst) binds vars;
haftmann@35228
    72
          in brackets (str "\\" :: ps @ str "->" @@ print_term tyvars some_thm vars' NOBR t') end
haftmann@35228
    73
      | print_term tyvars some_thm vars fxy (ICase (cases as (_, t0))) =
haftmann@28054
    74
          (case Code_Thingol.unfold_const_app t0
haftmann@39149
    75
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
haftmann@35228
    76
                then print_case tyvars some_thm vars fxy cases
haftmann@35228
    77
                else print_app tyvars some_thm vars fxy c_ts
haftmann@35228
    78
            | NONE => print_case tyvars some_thm vars fxy cases)
haftmann@37424
    79
    and print_app_expr tyvars some_thm vars ((c, (_, function_typs)), ts) = case contr_classparam_typs c
haftmann@35228
    80
     of [] => (str o deresolve) c :: map (print_term tyvars some_thm vars BR) ts
haftmann@28054
    81
      | fingerprint => let
haftmann@33956
    82
          val ts_fingerprint = ts ~~ take (length ts) fingerprint;
haftmann@28054
    83
          val needs_annotation = forall (fn (_, NONE) => true | (t, SOME _) =>
haftmann@28054
    84
            (not o Code_Thingol.locally_monomorphic) t) ts_fingerprint;
haftmann@35228
    85
          fun print_term_anno (t, NONE) _ = print_term tyvars some_thm vars BR t
haftmann@33988
    86
            | print_term_anno (t, SOME _) ty =
haftmann@35228
    87
                brackets [print_term tyvars some_thm vars NOBR t, str "::", print_typ tyvars NOBR ty];
haftmann@28054
    88
        in
haftmann@28054
    89
          if needs_annotation then
haftmann@37424
    90
            (str o deresolve) c :: map2 print_term_anno ts_fingerprint (take (length ts) function_typs)
haftmann@35228
    91
          else (str o deresolve) c :: map (print_term tyvars some_thm vars BR) ts
haftmann@28054
    92
        end
haftmann@39149
    93
    and print_app tyvars = gen_print_app (print_app_expr tyvars) (print_term tyvars) const_syntax
haftmann@35228
    94
    and print_bind tyvars some_thm fxy p = gen_print_bind (print_term tyvars) some_thm fxy p
haftmann@35228
    95
    and print_case tyvars some_thm vars fxy (cases as ((_, [_]), _)) =
haftmann@28054
    96
          let
haftmann@29889
    97
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
haftmann@33988
    98
            fun print_match ((pat, ty), t) vars =
haftmann@28054
    99
              vars
haftmann@35228
   100
              |> print_bind tyvars some_thm BR pat
haftmann@35228
   101
              |>> (fn p => semicolon [p, str "=", print_term tyvars some_thm vars NOBR t])
haftmann@33988
   102
            val (ps, vars') = fold_map print_match binds vars;
haftmann@31665
   103
          in brackify_block fxy (str "let {")
haftmann@31665
   104
            ps
haftmann@35228
   105
            (concat [str "}", str "in", print_term tyvars some_thm vars' NOBR body])
haftmann@28054
   106
          end
haftmann@35228
   107
      | print_case tyvars some_thm vars fxy (((t, ty), clauses as _ :: _), _) =
haftmann@28054
   108
          let
haftmann@33988
   109
            fun print_select (pat, body) =
haftmann@28054
   110
              let
haftmann@35228
   111
                val (p, vars') = print_bind tyvars some_thm NOBR pat vars;
haftmann@35228
   112
              in semicolon [p, str "->", print_term tyvars some_thm vars' NOBR body] end;
haftmann@36574
   113
          in Pretty.block_enclose
haftmann@36574
   114
            (concat [str "(case", print_term tyvars some_thm vars NOBR t, str "of", str "{"], str "})")
haftmann@33988
   115
            (map print_select clauses)
haftmann@28054
   116
          end
haftmann@35228
   117
      | print_case tyvars some_thm vars fxy ((_, []), _) =
haftmann@31376
   118
          (brackify fxy o Pretty.breaks o map str) ["error", "\"empty case\""];
haftmann@37412
   119
    fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) =
haftmann@28054
   120
          let
haftmann@32924
   121
            val tyvars = intro_vars (map fst vs) reserved;
haftmann@34262
   122
            fun print_err n =
haftmann@33988
   123
              semicolon (
haftmann@28054
   124
                (str o deresolve_base) name
haftmann@28054
   125
                :: map str (replicate n "_")
haftmann@28054
   126
                @ str "="
haftmann@28054
   127
                :: str "error"
haftmann@33988
   128
                @@ (str o ML_Syntax.print_string
wenzelm@30364
   129
                    o Long_Name.base_name o Long_Name.qualifier) name
haftmann@34262
   130
              );
haftmann@35228
   131
            fun print_eqn ((ts, t), (some_thm, _)) =
haftmann@28054
   132
              let
haftmann@32913
   133
                val consts = fold Code_Thingol.add_constnames (t :: ts) [];
haftmann@32924
   134
                val vars = reserved
haftmann@32924
   135
                  |> intro_base_names
haftmann@39149
   136
                      (is_none o const_syntax) deresolve consts
haftmann@32924
   137
                  |> intro_vars ((fold o Code_Thingol.fold_varnames)
haftmann@32925
   138
                      (insert (op =)) ts []);
haftmann@28054
   139
              in
haftmann@28054
   140
                semicolon (
haftmann@28054
   141
                  (str o deresolve_base) name
haftmann@35228
   142
                  :: map (print_term tyvars some_thm vars BR) ts
haftmann@28054
   143
                  @ str "="
haftmann@35228
   144
                  @@ print_term tyvars some_thm vars NOBR t
haftmann@28054
   145
                )
haftmann@28054
   146
              end;
haftmann@28054
   147
          in
haftmann@28054
   148
            Pretty.chunks (
haftmann@33988
   149
              semicolon [
haftmann@28054
   150
                (str o suffix " ::" o deresolve_base) name,
haftmann@33988
   151
                print_typscheme tyvars (vs, ty)
haftmann@28054
   152
              ]
haftmann@34262
   153
              :: (case filter (snd o snd) raw_eqs
haftmann@34262
   154
               of [] => [print_err ((length o fst o Code_Thingol.unfold_fun) ty)]
haftmann@34262
   155
                | eqs => map print_eqn eqs)
haftmann@28054
   156
            )
haftmann@28054
   157
          end
haftmann@33988
   158
      | print_stmt (name, Code_Thingol.Datatype (_, (vs, []))) =
haftmann@28054
   159
          let
haftmann@32924
   160
            val tyvars = intro_vars (map fst vs) reserved;
haftmann@28054
   161
          in
haftmann@28054
   162
            semicolon [
haftmann@28054
   163
              str "data",
haftmann@33988
   164
              print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs))
haftmann@28054
   165
            ]
haftmann@28054
   166
          end
haftmann@37424
   167
      | print_stmt (name, Code_Thingol.Datatype (_, (vs, [((co, _), [ty])]))) =
haftmann@28054
   168
          let
haftmann@32924
   169
            val tyvars = intro_vars (map fst vs) reserved;
haftmann@28054
   170
          in
haftmann@28054
   171
            semicolon (
haftmann@28054
   172
              str "newtype"
haftmann@33988
   173
              :: print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs))
haftmann@28054
   174
              :: str "="
haftmann@28054
   175
              :: (str o deresolve_base) co
haftmann@33988
   176
              :: print_typ tyvars BR ty
haftmann@28054
   177
              :: (if deriving_show name then [str "deriving (Read, Show)"] else [])
haftmann@28054
   178
            )
haftmann@28054
   179
          end
haftmann@33988
   180
      | print_stmt (name, Code_Thingol.Datatype (_, (vs, co :: cos))) =
haftmann@28054
   181
          let
haftmann@32924
   182
            val tyvars = intro_vars (map fst vs) reserved;
haftmann@37424
   183
            fun print_co ((co, _), tys) =
haftmann@28054
   184
              concat (
haftmann@28054
   185
                (str o deresolve_base) co
haftmann@33988
   186
                :: map (print_typ tyvars BR) tys
haftmann@28054
   187
              )
haftmann@28054
   188
          in
haftmann@28054
   189
            semicolon (
haftmann@28054
   190
              str "data"
haftmann@33988
   191
              :: print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs))
haftmann@28054
   192
              :: str "="
haftmann@33988
   193
              :: print_co co
haftmann@33988
   194
              :: map ((fn p => Pretty.block [str "| ", p]) o print_co) cos
haftmann@28054
   195
              @ (if deriving_show name then [str "deriving (Read, Show)"] else [])
haftmann@28054
   196
            )
haftmann@28054
   197
          end
haftmann@37422
   198
      | print_stmt (name, Code_Thingol.Class (_, (v, (super_classes, classparams)))) =
haftmann@28054
   199
          let
haftmann@32924
   200
            val tyvars = intro_vars [v] reserved;
haftmann@33988
   201
            fun print_classparam (classparam, ty) =
haftmann@28054
   202
              semicolon [
haftmann@28687
   203
                (str o deresolve_base) classparam,
haftmann@28054
   204
                str "::",
haftmann@33988
   205
                print_typ tyvars NOBR ty
haftmann@28054
   206
              ]
haftmann@28054
   207
          in
haftmann@28054
   208
            Pretty.block_enclose (
haftmann@28054
   209
              Pretty.block [
haftmann@28054
   210
                str "class ",
haftmann@37359
   211
                Pretty.block (print_typcontext tyvars [(v, map fst super_classes)]),
haftmann@32924
   212
                str (deresolve_base name ^ " " ^ lookup_var tyvars v),
haftmann@28054
   213
                str " where {"
haftmann@28054
   214
              ],
haftmann@28054
   215
              str "};"
haftmann@33988
   216
            ) (map print_classparam classparams)
haftmann@28054
   217
          end
haftmann@37424
   218
      | print_stmt (_, Code_Thingol.Classinst ((class, (tyco, vs)), (_, (classparam_instances, _)))) =
haftmann@28054
   219
          let
haftmann@32924
   220
            val tyvars = intro_vars (map fst vs) reserved;
haftmann@39149
   221
            fun requires_args classparam = case const_syntax classparam
haftmann@37854
   222
             of NONE => 0
haftmann@37854
   223
              | SOME (Code_Printer.Plain_const_syntax _) => 0
haftmann@37854
   224
              | SOME (Code_Printer.Complex_const_syntax (k,_ )) => k;
haftmann@37854
   225
            fun print_classparam_instance ((classparam, const), (thm, _)) =
haftmann@37854
   226
              case requires_args classparam
haftmann@37854
   227
               of 0 => semicolon [
haftmann@37854
   228
                      (str o deresolve_base) classparam,
haftmann@28687
   229
                      str "=",
haftmann@37854
   230
                      print_app tyvars (SOME thm) reserved NOBR (const, [])
haftmann@28687
   231
                    ]
haftmann@37854
   232
                | k =>
haftmann@37854
   233
                    let
haftmann@37854
   234
                      val (c, (_, tys)) = const;
haftmann@37854
   235
                      val (vs, rhs) = (apfst o map) fst
haftmann@37854
   236
                        (Code_Thingol.unfold_abs (Code_Thingol.eta_expand k (const, [])));
haftmann@39149
   237
                      val s = if (is_some o const_syntax) c
haftmann@37854
   238
                        then NONE else (SOME o Long_Name.base_name o deresolve) c;
haftmann@37854
   239
                      val vars = reserved
haftmann@37854
   240
                        |> intro_vars (map_filter I (s :: vs));
haftmann@37854
   241
                      val lhs = IConst (classparam, (([], []), tys)) `$$ map IVar vs;
haftmann@37854
   242
                        (*dictionaries are not relevant at this late stage*)
haftmann@37854
   243
                    in
haftmann@37854
   244
                      semicolon [
haftmann@37854
   245
                        print_term tyvars (SOME thm) vars NOBR lhs,
haftmann@37854
   246
                        str "=",
haftmann@37854
   247
                        print_term tyvars (SOME thm) vars NOBR rhs
haftmann@37854
   248
                      ]
haftmann@37854
   249
                    end;
haftmann@28054
   250
          in
haftmann@28054
   251
            Pretty.block_enclose (
haftmann@28054
   252
              Pretty.block [
haftmann@28054
   253
                str "instance ",
haftmann@33988
   254
                Pretty.block (print_typcontext tyvars vs),
haftmann@28054
   255
                str (class_name class ^ " "),
haftmann@33988
   256
                print_typ tyvars BR (tyco `%% map (ITyVar o fst) vs),
haftmann@28054
   257
                str " where {"
haftmann@28054
   258
              ],
haftmann@28054
   259
              str "};"
haftmann@37359
   260
            ) (map print_classparam_instance classparam_instances)
haftmann@28054
   261
          end;
haftmann@33988
   262
  in print_stmt end;
haftmann@28054
   263
haftmann@39432
   264
type flat_program = ((string * Code_Thingol.stmt) Graph.T * ((string * (string list * string list)) list)) Graph.T;
haftmann@39432
   265
haftmann@39432
   266
fun flat_program labelled_name { module_alias, module_prefix, reserved,
haftmann@39432
   267
      empty_nsp, namify_stmt, modify_stmt } program =
haftmann@39432
   268
  let
haftmann@39432
   269
haftmann@39432
   270
    (* building module name hierarchy *)
haftmann@39432
   271
    val fragments_tab = Code_Namespace.build_module_namespace { module_alias = module_alias,
haftmann@39432
   272
      module_prefix = module_prefix, reserved = reserved } program;
haftmann@39432
   273
    val dest_name = Code_Namespace.dest_name
haftmann@39432
   274
      #>> (Long_Name.implode o the o Symtab.lookup fragments_tab);
haftmann@39432
   275
haftmann@39432
   276
    (* distribute statements over hierarchy *)
haftmann@39432
   277
    fun add_stmt name stmt =
haftmann@39432
   278
      let
haftmann@39432
   279
        val (module_name, base) = dest_name name;
haftmann@39432
   280
      in case modify_stmt stmt
haftmann@39432
   281
       of SOME stmt' => 
haftmann@39432
   282
            Graph.default_node (module_name, (Graph.empty, []))
haftmann@39432
   283
            #> (Graph.map_node module_name o apfst) (Graph.new_node (name, (base, stmt')))
haftmann@39432
   284
        | NONE => I
haftmann@39432
   285
      end;
haftmann@39432
   286
    fun add_dependency name name' =
haftmann@39432
   287
      let
haftmann@39432
   288
        val (module_name, base) = dest_name name;
haftmann@39432
   289
        val (module_name', base') = dest_name name';
haftmann@39432
   290
      in if module_name = module_name'
haftmann@39432
   291
        then (Graph.map_node module_name o apfst) (Graph.add_edge (name, name'))
haftmann@39432
   292
        else (Graph.map_node module_name o apsnd)
haftmann@39432
   293
          (AList.map_default (op =) (module_name', []) (insert (op =) name'))
haftmann@39432
   294
      end;
haftmann@39432
   295
    val proto_program = Graph.empty
haftmann@39432
   296
      |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
haftmann@39432
   297
      |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
haftmann@39432
   298
haftmann@39432
   299
    (* name declarations *)
haftmann@39432
   300
    fun declare name (base, stmt) (gr, nsp) = 
haftmann@39432
   301
      let
haftmann@39432
   302
        val (base', nsp') = namify_stmt stmt base nsp;
haftmann@39432
   303
        val gr' = (Graph.map_node name o apfst) (K base') gr;
haftmann@39432
   304
      in (gr', nsp') end;
haftmann@39432
   305
    fun declarations gr = (gr, empty_nsp)
haftmann@39432
   306
      |> fold (fn name => declare name (Graph.get_node gr name)) (Graph.keys gr) 
haftmann@39432
   307
      |> fst;
haftmann@39432
   308
    val intermediate_program = proto_program
haftmann@39432
   309
      |> Graph.map ((K o apfst) declarations);
haftmann@39432
   310
haftmann@39432
   311
    (* qualified and unqualified imports, deresolving *)
haftmann@39432
   312
    fun base_deresolver name = fst (Graph.get_node
haftmann@39432
   313
      (fst (Graph.get_node intermediate_program (fst (dest_name name)))) name);
haftmann@39432
   314
    fun classify_imports gr imports =
haftmann@39432
   315
      let
haftmann@39432
   316
        val import_tab = maps
haftmann@39432
   317
          (fn (module_name, names) => map (rpair module_name) names) imports;
haftmann@39432
   318
        val imported_names = map fst import_tab;
haftmann@39432
   319
        val here_names = Graph.keys gr;
haftmann@39432
   320
        val qualified_names = []
haftmann@39432
   321
          |> fold (fn name => AList.map_default (op =) (base_deresolver name, [])
haftmann@39432
   322
               (insert (op =) name)) (here_names @ imported_names)
haftmann@39432
   323
          |> filter (fn (_, names) => length names > 1)
haftmann@39432
   324
          |> maps snd;
haftmann@39432
   325
        val name_tab = Symtab.empty
haftmann@39432
   326
          |> fold (fn name => Symtab.update (name, base_deresolver name)) here_names
haftmann@39432
   327
          |> fold (fn name => Symtab.update (name,
haftmann@39432
   328
               if member (op =) qualified_names name
haftmann@39432
   329
               then Long_Name.append (the (AList.lookup (op =) import_tab name))
haftmann@39432
   330
                 (base_deresolver name)
haftmann@39432
   331
               else base_deresolver name)) imported_names;
haftmann@39432
   332
        val imports' = (map o apsnd) (List.partition (member (op =) qualified_names))
haftmann@39432
   333
          imports;
haftmann@39432
   334
      in (name_tab, imports') end;
haftmann@39432
   335
    val classified = AList.make (uncurry classify_imports o Graph.get_node intermediate_program)
haftmann@39432
   336
      (Graph.keys intermediate_program);
haftmann@39432
   337
    val flat_program = Graph.map (apsnd o K o snd o the o AList.lookup (op =) classified)
haftmann@39432
   338
      intermediate_program;
haftmann@39432
   339
    val deresolver_tab = Symtab.empty
haftmann@39432
   340
      |> fold (fn (module_name, (name_tab, _)) => Symtab.update (module_name, name_tab)) classified;
haftmann@39432
   341
    fun deresolver module_name name =
haftmann@39432
   342
      the (Symtab.lookup (the (Symtab.lookup deresolver_tab module_name)) name)
haftmann@39432
   343
      handle Option => error ("Unknown statement name: " ^ labelled_name name);
haftmann@39432
   344
haftmann@39432
   345
  in (deresolver, flat_program) end;
haftmann@39432
   346
haftmann@39432
   347
fun haskell_program_of_program labelled_name module_alias module_prefix reserved =
haftmann@39432
   348
  let
haftmann@39432
   349
    fun namify_fun upper base (nsp_fun, nsp_typ) =
haftmann@39432
   350
      let
haftmann@39432
   351
        val (base', nsp_fun') = yield_singleton Name.variants
haftmann@39432
   352
          (if upper then first_upper base else base) nsp_fun;
haftmann@39432
   353
      in (base', (nsp_fun', nsp_typ)) end;
haftmann@39432
   354
    fun namify_typ base (nsp_fun, nsp_typ) =
haftmann@39432
   355
      let
haftmann@39432
   356
        val (base', nsp_typ') = yield_singleton Name.variants
haftmann@39432
   357
          (first_upper base) nsp_typ
haftmann@39432
   358
      in (base', (nsp_fun, nsp_typ')) end;
haftmann@39432
   359
    fun namify_stmt (Code_Thingol.Fun (_, (_, SOME _))) = pair
haftmann@39432
   360
      | namify_stmt (Code_Thingol.Fun _) = namify_fun false
haftmann@39432
   361
      | namify_stmt (Code_Thingol.Datatype _) = namify_typ
haftmann@39432
   362
      | namify_stmt (Code_Thingol.Datatypecons _) = namify_fun true
haftmann@39432
   363
      | namify_stmt (Code_Thingol.Class _) = namify_typ
haftmann@39432
   364
      | namify_stmt (Code_Thingol.Classrel _) = pair
haftmann@39432
   365
      | namify_stmt (Code_Thingol.Classparam _) = namify_fun false
haftmann@39432
   366
      | namify_stmt (Code_Thingol.Classinst _) = pair;
haftmann@39432
   367
    fun select_stmt (Code_Thingol.Fun (_, (_, SOME _))) = false
haftmann@39432
   368
      | select_stmt (Code_Thingol.Fun _) = true
haftmann@39432
   369
      | select_stmt (Code_Thingol.Datatype _) = true
haftmann@39432
   370
      | select_stmt (Code_Thingol.Datatypecons _) = false
haftmann@39432
   371
      | select_stmt (Code_Thingol.Class _) = true
haftmann@39432
   372
      | select_stmt (Code_Thingol.Classrel _) = false
haftmann@39432
   373
      | select_stmt (Code_Thingol.Classparam _) = false
haftmann@39432
   374
      | select_stmt (Code_Thingol.Classinst _) = true;
haftmann@39432
   375
  in
haftmann@39432
   376
    flat_program labelled_name
haftmann@39432
   377
      { module_alias = module_alias, module_prefix = module_prefix,
haftmann@39432
   378
        reserved = reserved, empty_nsp = (reserved, reserved), namify_stmt = namify_stmt,
haftmann@39432
   379
        modify_stmt = fn stmt => if select_stmt stmt then SOME stmt else NONE }
haftmann@39432
   380
  end;
haftmann@39432
   381
haftmann@39289
   382
fun mk_name_module reserved module_prefix module_alias program =
haftmann@39289
   383
  let
haftmann@39432
   384
    val fragments_tab = Code_Namespace.build_module_namespace { module_alias = module_alias,
haftmann@39432
   385
      module_prefix = module_prefix, reserved = reserved } program;
haftmann@39432
   386
  in Long_Name.implode o the o Symtab.lookup fragments_tab end;
haftmann@39289
   387
haftmann@39012
   388
fun haskell_program_of_program labelled_name module_prefix reserved module_alias program =
haftmann@28054
   389
  let
haftmann@32924
   390
    val reserved = Name.make_context reserved;
haftmann@32924
   391
    val mk_name_module = mk_name_module reserved module_prefix module_alias program;
haftmann@28054
   392
    fun add_stmt (name, (stmt, deps)) =
haftmann@28054
   393
      let
haftmann@39289
   394
        val (module_name, base) = Code_Namespace.dest_name name;
haftmann@28054
   395
        val module_name' = mk_name_module module_name;
haftmann@28054
   396
        val mk_name_stmt = yield_singleton Name.variants;
haftmann@28054
   397
        fun add_fun upper (nsp_fun, nsp_typ) =
haftmann@28054
   398
          let
haftmann@28054
   399
            val (base', nsp_fun') =
haftmann@32924
   400
              mk_name_stmt (if upper then first_upper base else base) nsp_fun
haftmann@28054
   401
          in (base', (nsp_fun', nsp_typ)) end;
haftmann@28054
   402
        fun add_typ (nsp_fun, nsp_typ) =
haftmann@28054
   403
          let
haftmann@32924
   404
            val (base', nsp_typ') = mk_name_stmt (first_upper base) nsp_typ
haftmann@28054
   405
          in (base', (nsp_fun, nsp_typ')) end;
haftmann@28054
   406
        val add_name = case stmt
haftmann@37414
   407
         of Code_Thingol.Fun (_, (_, SOME _)) => pair base
haftmann@37414
   408
          | Code_Thingol.Fun _ => add_fun false
haftmann@28054
   409
          | Code_Thingol.Datatype _ => add_typ
haftmann@28054
   410
          | Code_Thingol.Datatypecons _ => add_fun true
haftmann@28054
   411
          | Code_Thingol.Class _ => add_typ
haftmann@28054
   412
          | Code_Thingol.Classrel _ => pair base
haftmann@28054
   413
          | Code_Thingol.Classparam _ => add_fun false
haftmann@28054
   414
          | Code_Thingol.Classinst _ => pair base;
haftmann@28054
   415
        fun add_stmt' base' = case stmt
haftmann@37414
   416
         of Code_Thingol.Fun (_, (_, SOME _)) =>
haftmann@37414
   417
              I
haftmann@37414
   418
          | Code_Thingol.Datatypecons _ =>
wenzelm@30364
   419
              cons (name, (Long_Name.append module_name' base', NONE))
haftmann@28054
   420
          | Code_Thingol.Classrel _ => I
haftmann@28054
   421
          | Code_Thingol.Classparam _ =>
wenzelm@30364
   422
              cons (name, (Long_Name.append module_name' base', NONE))
wenzelm@30364
   423
          | _ => cons (name, (Long_Name.append module_name' base', SOME stmt));
haftmann@28054
   424
      in
haftmann@32924
   425
        Symtab.map_default (module_name', ([], ([], (reserved, reserved))))
haftmann@28054
   426
              (apfst (fold (insert (op = : string * string -> bool)) deps))
haftmann@28054
   427
        #> `(fn program => add_name ((snd o snd o the o Symtab.lookup program) module_name'))
haftmann@28054
   428
        #-> (fn (base', names) =>
haftmann@28054
   429
              (Symtab.map_entry module_name' o apsnd) (fn (stmts, _) =>
haftmann@28054
   430
              (add_stmt' base' stmts, names)))
haftmann@28054
   431
      end;
haftmann@28054
   432
    val hs_program = fold add_stmt (AList.make (fn name =>
haftmann@28054
   433
      (Graph.get_node program name, Graph.imm_succs program name))
haftmann@28054
   434
      (Graph.strong_conn program |> flat)) Symtab.empty;
haftmann@28663
   435
    fun deresolver name = (fst o the o AList.lookup (op =) ((fst o snd o the
haftmann@39289
   436
      o Symtab.lookup hs_program) ((mk_name_module o fst o Code_Namespace.dest_name) name))) name
haftmann@28663
   437
      handle Option => error ("Unknown statement name: " ^ labelled_name name);
haftmann@28054
   438
  in (deresolver, hs_program) end;
haftmann@28054
   439
haftmann@39381
   440
fun serialize_haskell module_prefix string_classes { labelled_name, reserved_syms,
haftmann@39381
   441
    includes, module_alias, class_syntax, tyco_syntax, const_syntax, program } =
haftmann@28054
   442
  let
haftmann@39152
   443
    val reserved = fold (insert (op =) o fst) includes reserved_syms;
haftmann@28054
   444
    val (deresolver, hs_program) = haskell_program_of_program labelled_name
haftmann@39012
   445
      module_prefix reserved module_alias program;
haftmann@28054
   446
    val contr_classparam_typs = Code_Thingol.contr_classparam_typs program;
haftmann@28054
   447
    fun deriving_show tyco =
haftmann@28054
   448
      let
haftmann@28054
   449
        fun deriv _ "fun" = false
haftmann@34085
   450
          | deriv tycos tyco = not (tyco = Code_Thingol.fun_tyco)
haftmann@34085
   451
              andalso (member (op =) tycos tyco
haftmann@34085
   452
              orelse case try (Graph.get_node program) tyco
haftmann@28663
   453
                of SOME (Code_Thingol.Datatype (_, (_, cs))) => forall (deriv' (tyco :: tycos))
haftmann@28054
   454
                    (maps snd cs)
haftmann@34085
   455
                 | NONE => true)
haftmann@28054
   456
        and deriv' tycos (tyco `%% tys) = deriv tycos tyco
haftmann@28054
   457
              andalso forall (deriv' tycos) tys
haftmann@28054
   458
          | deriv' _ (ITyVar _) = true
haftmann@28054
   459
      in deriv [] tyco end;
haftmann@32924
   460
    val reserved = make_vars reserved;
haftmann@33988
   461
    fun print_stmt qualified = print_haskell_stmt labelled_name
haftmann@39149
   462
      class_syntax tyco_syntax const_syntax reserved
wenzelm@30364
   463
      (if qualified then deresolver else Long_Name.base_name o deresolver)
haftmann@32903
   464
      contr_classparam_typs
haftmann@28054
   465
      (if string_classes then deriving_show else K false);
haftmann@33988
   466
    fun print_module name content =
haftmann@39001
   467
      (name, Pretty.chunks2 [
haftmann@28054
   468
        str ("module " ^ name ^ " where {"),
haftmann@28054
   469
        content,
haftmann@28054
   470
        str "}"
haftmann@28054
   471
      ]);
haftmann@39291
   472
    fun serialize_module (module_name', (deps, (stmts, _))) =
haftmann@28054
   473
      let
haftmann@28054
   474
        val stmt_names = map fst stmts;
haftmann@39291
   475
        val qualified = true;
haftmann@33410
   476
        val imports = subtract (op =) stmt_names deps
haftmann@28054
   477
          |> distinct (op =)
haftmann@33410
   478
          |> map_filter (try deresolver)
wenzelm@30364
   479
          |> map Long_Name.qualifier
haftmann@28054
   480
          |> distinct (op =);
haftmann@33988
   481
        fun print_import_include (name, _) = str ("import qualified " ^ name ^ ";");
haftmann@34045
   482
        fun print_import_module name = str ((if qualified
haftmann@34045
   483
          then "import qualified "
haftmann@34045
   484
          else "import ") ^ name ^ ";");
haftmann@33988
   485
        val import_ps = map print_import_include includes @ map print_import_module imports
haftmann@34073
   486
        val content = Pretty.chunks2 ((if null import_ps then [] else [Pretty.chunks import_ps])
haftmann@33988
   487
            @ map_filter
haftmann@39290
   488
              (fn (name, (_, SOME stmt)) => SOME (markup_stmt name (print_stmt qualified (name, stmt)))
haftmann@33988
   489
                | (_, (_, NONE)) => NONE) stmts
haftmann@33988
   490
          );
haftmann@33988
   491
      in print_module module_name' content end;
haftmann@39141
   492
    fun write_module width (SOME destination) (modlname, content) =
haftmann@39141
   493
          let
haftmann@39141
   494
            val _ = File.check destination;
haftmann@39141
   495
            val filename = case modlname
haftmann@39141
   496
             of "" => Path.explode "Main.hs"
haftmann@39141
   497
              | _ => (Path.ext "hs" o Path.explode o implode o separate "/"
haftmann@39141
   498
                    o Long_Name.explode) modlname;
haftmann@39141
   499
            val pathname = Path.append destination filename;
haftmann@39141
   500
            val _ = File.mkdir_leaf (Path.dir pathname);
haftmann@39141
   501
          in File.write pathname
haftmann@39141
   502
            ("{-# OPTIONS_GHC -fglasgow-exts #-}\n\n"
haftmann@39290
   503
              ^ format [] width content)
haftmann@39141
   504
          end
haftmann@39290
   505
      | write_module width NONE (_, content) = writeln (format [] width content);
haftmann@28054
   506
  in
haftmann@39142
   507
    Code_Target.serialization
haftmann@39141
   508
      (fn width => fn destination => K () o map (write_module width destination))
haftmann@39336
   509
      (fn present => fn width => rpair (fn _ => error "no deresolving") o format present width o Pretty.chunks o map snd)
haftmann@33988
   510
      (map (uncurry print_module) includes
haftmann@28054
   511
        @ map serialize_module (Symtab.dest hs_program))
haftmann@28054
   512
  end;
haftmann@28054
   513
haftmann@39192
   514
val serializer : Code_Target.serializer =
haftmann@39192
   515
  Code_Target.parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name)
haftmann@39192
   516
    -- Scan.optional (Args.$$$ "string_classes" >> K true) false
haftmann@39192
   517
    >> (fn (module_prefix, string_classes) =>
haftmann@39192
   518
      serialize_haskell module_prefix string_classes));
haftmann@39192
   519
haftmann@28064
   520
val literals = let
haftmann@28064
   521
  fun char_haskell c =
haftmann@28064
   522
    let
haftmann@28064
   523
      val s = ML_Syntax.print_char c;
haftmann@28064
   524
    in if s = "'" then "\\'" else s end;
haftmann@34931
   525
  fun numeral_haskell k = if k >= 0 then string_of_int k
haftmann@34931
   526
    else Library.enclose "(" ")" (signed_string_of_int k);
haftmann@28064
   527
in Literals {
haftmann@34178
   528
  literal_char = Library.enclose "'" "'" o char_haskell,
haftmann@28064
   529
  literal_string = quote o translate_string char_haskell,
haftmann@34931
   530
  literal_numeral = numeral_haskell,
haftmann@34931
   531
  literal_positive_numeral = numeral_haskell,
haftmann@38195
   532
  literal_alternative_numeral = numeral_haskell,
haftmann@34931
   533
  literal_naive_numeral = numeral_haskell,
haftmann@34178
   534
  literal_list = enum "," "[" "]",
haftmann@28064
   535
  infix_cons = (5, ":")
haftmann@28064
   536
} end;
haftmann@28064
   537
haftmann@28054
   538
haftmann@28054
   539
(** optional monad syntax **)
haftmann@28054
   540
haftmann@28054
   541
fun pretty_haskell_monad c_bind =
haftmann@28054
   542
  let
haftmann@31873
   543
    fun dest_bind t1 t2 = case Code_Thingol.split_pat_abs t2
haftmann@31889
   544
     of SOME ((pat, ty), t') =>
haftmann@31889
   545
          SOME ((SOME ((pat, ty), true), t1), t')
haftmann@28145
   546
      | NONE => NONE;
haftmann@28663
   547
    fun dest_monad c_bind_name (IConst (c, _) `$ t1 `$ t2) =
haftmann@28663
   548
          if c = c_bind_name then dest_bind t1 t2
haftmann@28145
   549
          else NONE
haftmann@28663
   550
      | dest_monad _ t = case Code_Thingol.split_let t
haftmann@28145
   551
         of SOME (((pat, ty), tbind), t') =>
haftmann@31889
   552
              SOME ((SOME ((pat, ty), false), tbind), t')
haftmann@28145
   553
          | NONE => NONE;
haftmann@28663
   554
    fun implode_monad c_bind_name = Code_Thingol.unfoldr (dest_monad c_bind_name);
haftmann@33988
   555
    fun print_monad print_bind print_term (NONE, t) vars =
haftmann@33988
   556
          (semicolon [print_term vars NOBR t], vars)
haftmann@33988
   557
      | print_monad print_bind print_term (SOME ((bind, _), true), t) vars = vars
haftmann@33988
   558
          |> print_bind NOBR bind
haftmann@33988
   559
          |>> (fn p => semicolon [p, str "<-", print_term vars NOBR t])
haftmann@33988
   560
      | print_monad print_bind print_term (SOME ((bind, _), false), t) vars = vars
haftmann@33988
   561
          |> print_bind NOBR bind
haftmann@37832
   562
          |>> (fn p => semicolon [str "let", str "{", p, str "=", print_term vars NOBR t, str "}"]);
haftmann@33988
   563
    fun pretty _ [c_bind'] print_term thm vars fxy [(t1, _), (t2, _)] = case dest_bind t1 t2
haftmann@28145
   564
     of SOME (bind, t') => let
haftmann@31054
   565
          val (binds, t'') = implode_monad c_bind' t'
haftmann@33988
   566
          val (ps, vars') = fold_map (print_monad (gen_print_bind (K print_term) thm) print_term)
haftmann@33988
   567
            (bind :: binds) vars;
haftmann@33988
   568
        in
haftmann@37833
   569
          (brackify fxy o single o enclose "do { " " }" o Pretty.breaks)
haftmann@33988
   570
            (ps @| print_term vars' NOBR t'')
haftmann@33988
   571
        end
haftmann@28145
   572
      | NONE => brackify_infix (1, L) fxy
haftmann@37239
   573
          (print_term vars (INFX (1, L)) t1, str ">>=", print_term vars (INFX (1, X)) t2)
haftmann@31054
   574
  in (2, ([c_bind], pretty)) end;
haftmann@28054
   575
haftmann@28145
   576
fun add_monad target' raw_c_bind thy =
haftmann@28054
   577
  let
haftmann@31156
   578
    val c_bind = Code.read_const thy raw_c_bind;
haftmann@28054
   579
  in if target = target' then
haftmann@28054
   580
    thy
haftmann@39149
   581
    |> Code_Target.add_const_syntax target c_bind
haftmann@37854
   582
        (SOME (Code_Printer.complex_const_syntax (pretty_haskell_monad c_bind)))
haftmann@28054
   583
  else error "Only Haskell target allows for monad syntax" end;
haftmann@28054
   584
haftmann@28054
   585
haftmann@28054
   586
(** Isar setup **)
haftmann@28054
   587
haftmann@28054
   588
val _ =
wenzelm@36970
   589
  Outer_Syntax.command "code_monad" "define code syntax for monads" Keyword.thy_decl (
wenzelm@36970
   590
    Parse.term_group -- Parse.name >> (fn (raw_bind, target) =>
haftmann@28145
   591
      Toplevel.theory  (add_monad target raw_bind))
haftmann@28054
   592
  );
haftmann@28054
   593
haftmann@28054
   594
val setup =
haftmann@37821
   595
  Code_Target.add_target
haftmann@39192
   596
    (target, { serializer = serializer, literals = literals,
haftmann@37822
   597
      check = { env_var = "EXEC_GHC", make_destination = I,
haftmann@39092
   598
        make_command = fn ghc => fn module_name =>
haftmann@37822
   599
          ghc ^ " -fglasgow-exts -odir build -hidir build -stubdir build -e \"\" " ^ module_name ^ ".hs" } })
haftmann@39149
   600
  #> Code_Target.add_tyco_syntax target "fun" (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
haftmann@37239
   601
      brackify_infix (1, R) fxy (
haftmann@33988
   602
        print_typ (INFX (1, X)) ty1,
haftmann@28054
   603
        str "->",
haftmann@33988
   604
        print_typ (INFX (1, R)) ty2
haftmann@37239
   605
      )))
haftmann@28054
   606
  #> fold (Code_Target.add_reserved target) [
haftmann@28054
   607
      "hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr",
haftmann@28054
   608
      "import", "default", "forall", "let", "in", "class", "qualified", "data",
haftmann@28054
   609
      "newtype", "instance", "if", "then", "else", "type", "as", "do", "module"
haftmann@28054
   610
    ]
haftmann@28054
   611
  #> fold (Code_Target.add_reserved target) [
haftmann@28054
   612
      "Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int",
haftmann@28054
   613
      "Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded",
haftmann@28054
   614
      "Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor",
haftmann@28054
   615
      "AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException",
haftmann@28054
   616
      "BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException",
haftmann@28054
   617
      "Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure",
haftmann@28054
   618
      "ExitSuccess", "False", "GT", "HeapOverflow",
haftmann@28054
   619
      "IOError", "IOException", "IllegalOperation",
haftmann@28054
   620
      "IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError",
haftmann@28054
   621
      "NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow",
haftmann@28054
   622
      "PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError",
haftmann@28054
   623
      "RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow",
haftmann@28054
   624
      "ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow",
haftmann@28054
   625
      "UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all",
haftmann@28054
   626
      "and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan",
haftmann@28054
   627
      "atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen",
haftmann@28054
   628
      "boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break",
haftmann@28054
   629
      "catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const",
haftmann@28054
   630
      "cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod",
haftmann@28054
   631
      "doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem",
haftmann@28054
   632
      "emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo",
haftmann@28054
   633
      "enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip",
haftmann@28054
   634
      "floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational",
haftmann@28054
   635
      "floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble",
haftmann@28054
   636
      "fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj",
haftmann@28054
   637
      "fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head",
haftmann@28054
   638
      "id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha",
haftmann@28054
   639
      "isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite",
haftmann@28054
   640
      "isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'",
haftmann@28054
   641
      "last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log",
haftmann@28054
   642
      "logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum",
haftmann@28054
   643
      "maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem",
haftmann@28054
   644
      "null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo",
haftmann@28054
   645
      "numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", 
haftmann@28054
   646
      "print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn",
haftmann@28054
   647
      "quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat",
haftmann@28054
   648
      "rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile",
haftmann@28054
   649
      "readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn",
haftmann@28054
   650
      "readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational",
haftmann@28054
   651
      "realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse",
haftmann@28054
   652
      "round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence",
haftmann@28054
   653
      "sequence_", "show", "showChar", "showException", "showField", "showList",
haftmann@28054
   654
      "showLitChar", "showParen", "showString", "shows", "showsPrec", "significand",
haftmann@28054
   655
      "signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract",
haftmann@28054
   656
      "succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult",
haftmann@28054
   657
      "throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry",
haftmann@28054
   658
      "undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords",
haftmann@28054
   659
      "unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3"
haftmann@28054
   660
    ] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*);
haftmann@28054
   661
haftmann@28054
   662
end; (*struct*)