src/Tools/Code/code_ml.ML
author haftmann
Sat, 04 Sep 2010 21:14:40 +0200
changeset 39148 b6530978c14d
parent 39142 f63715f00fdd
parent 39147 3c284a152bd6
child 40627 becf5d5187cc
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37745
6315b6426200 checking generated code for various target languages
haftmann
parents: 37449
diff changeset
     1
(*  Title:      Tools/Code/code_ml.ML
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
     3
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
     4
Serializer for SML and OCaml.
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
     5
*)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
     6
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
     7
signature CODE_ML =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
     8
sig
34028
1e6206763036 split off evaluation mechanisms in separte module Code_Eval
haftmann
parents: 33992
diff changeset
     9
  val target_SML: string
37745
6315b6426200 checking generated code for various target languages
haftmann
parents: 37449
diff changeset
    10
  val target_OCaml: string
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    11
  val setup: theory -> theory
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    12
end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    13
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    14
structure Code_ML : CODE_ML =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    15
struct
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    16
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    17
open Basic_Code_Thingol;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    18
open Code_Printer;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    19
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    20
infixr 5 @@;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    21
infixr 5 @|;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    22
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    23
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    24
(** generic **)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    25
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    26
val target_SML = "SML";
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    27
val target_OCaml = "OCaml";
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    28
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    29
datatype ml_binding =
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    30
    ML_Function of string * (typscheme * ((iterm list * iterm) * (thm option * bool)) list)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    31
  | ML_Instance of string * ((class * (string * (vname * sort) list))
37445
e372fa3c7239 dropped obscure type argument weakening mapping -- was only a misunderstanding
haftmann
parents: 37439
diff changeset
    32
        * ((class * (string * (string * dict list list))) list
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
    33
      * (((string * const) * (thm * bool)) list * ((string * const) * (thm * bool)) list)));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    34
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    35
datatype ml_stmt =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    36
    ML_Exc of string * (typscheme * int)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    37
  | ML_Val of ml_binding
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    38
  | ML_Funs of ml_binding list * string list
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
    39
  | ML_Datas of (string * ((vname * sort) list * ((string * vname list) * itype list) list)) list
37447
ad3e04f289b6 transitive superclasses were also only a misunderstanding
haftmann
parents: 37446
diff changeset
    40
  | ML_Class of string * (vname * ((class * string) list * (string * itype) list));
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    41
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    42
fun stmt_name_of_binding (ML_Function (name, _)) = name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    43
  | stmt_name_of_binding (ML_Instance (name, _)) = name;
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    44
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    45
fun stmt_names_of (ML_Exc (name, _)) = [name]
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    46
  | stmt_names_of (ML_Val binding) = [stmt_name_of_binding binding]
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    47
  | stmt_names_of (ML_Funs (bindings, _)) = map stmt_name_of_binding bindings
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    48
  | stmt_names_of (ML_Datas ds) = map fst ds
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    49
  | stmt_names_of (ML_Class (name, _)) = [name];
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    50
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    51
fun print_product _ [] = NONE
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    52
  | print_product print [x] = SOME (print x)
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
    53
  | print_product print xs = (SOME o enum " *" "" "") (map print xs);
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    54
38922
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38921
diff changeset
    55
fun tuplify _ _ [] = NONE
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38921
diff changeset
    56
  | tuplify print fxy [x] = SOME (print fxy x)
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38921
diff changeset
    57
  | tuplify print _ xs = SOME (enum "," "(" ")" (map (print NOBR) xs));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    58
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    59
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    60
(** SML serializer **)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    61
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
    62
fun print_sml_stmt labelled_name tyco_syntax const_syntax reserved is_cons deresolve =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    63
  let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    64
    fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    65
      | print_tyco_expr fxy (tyco, [ty]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    66
          concat [print_typ BR ty, (str o deresolve) tyco]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    67
      | print_tyco_expr fxy (tyco, tys) =
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
    68
          concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
    69
    and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    70
         of NONE => print_tyco_expr fxy (tyco, tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    71
          | SOME (i, print) => print print_typ fxy tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    72
      | print_typ fxy (ITyVar v) = str ("'" ^ v);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    73
    fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]);
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
    74
    fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    75
      (map_filter (fn (v, sort) =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    76
        (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    77
    fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    78
    fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    79
    fun print_dict is_pseudo_fun fxy (DictConst (inst, dss)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    80
          brackify fxy ((str o deresolve) inst ::
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    81
            (if is_pseudo_fun inst then [str "()"]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    82
            else map_filter (print_dicts is_pseudo_fun BR) dss))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    83
      | print_dict is_pseudo_fun fxy (DictVar (classrels, (v, (i, k)))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    84
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    85
            val v_p = str (if k = 1 then first_upper v ^ "_"
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    86
              else first_upper v ^ string_of_int (i+1) ^ "_");
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    87
          in case classrels
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    88
           of [] => v_p
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    89
            | [classrel] => brackets [(str o deresolve) classrel, v_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    90
            | classrels => brackets
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
    91
                [enum " o" "(" ")" (map (str o deresolve) classrels), v_p]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    92
          end
38922
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38921
diff changeset
    93
    and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    94
    val print_dict_args = map_filter (fn (v, sort) => print_dicts (K false) BR
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    95
      (map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort));
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    96
    fun print_term is_pseudo_fun some_thm vars fxy (IConst c) =
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    97
          print_app is_pseudo_fun some_thm vars fxy (c, [])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    98
      | print_term is_pseudo_fun some_thm vars fxy (IVar NONE) =
31889
fb2c8a687529 all variable names are optional
haftmann
parents: 31874
diff changeset
    99
          str "_"
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   100
      | print_term is_pseudo_fun some_thm vars fxy (IVar (SOME v)) =
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   101
          str (lookup_var vars v)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   102
      | print_term is_pseudo_fun some_thm vars fxy (t as t1 `$ t2) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   103
          (case Code_Thingol.unfold_const_app t
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   104
           of SOME c_ts => print_app is_pseudo_fun some_thm vars fxy c_ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   105
            | NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1,
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   106
                print_term is_pseudo_fun some_thm vars BR t2])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   107
      | print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   108
          let
31874
f172346ba805 simplified binding concept
haftmann
parents: 31775
diff changeset
   109
            val (binds, t') = Code_Thingol.unfold_pat_abs t;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   110
            fun print_abs (pat, ty) =
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   111
              print_bind is_pseudo_fun some_thm NOBR pat
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   112
              #>> (fn p => concat [str "fn", p, str "=>"]);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   113
            val (ps, vars') = fold_map print_abs binds vars;
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   114
          in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   115
      | print_term is_pseudo_fun some_thm vars fxy (ICase (cases as (_, t0))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   116
          (case Code_Thingol.unfold_const_app t0
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   117
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   118
                then print_case is_pseudo_fun some_thm vars fxy cases
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   119
                else print_app is_pseudo_fun some_thm vars fxy c_ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   120
            | NONE => print_case is_pseudo_fun some_thm vars fxy cases)
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   121
    and print_app_expr is_pseudo_fun some_thm vars (app as ((c, ((_, iss), function_typs)), ts)) =
29175
27cd8cce605e tackling simultaneous val/fun bindings
haftmann
parents: 28971
diff changeset
   122
      if is_cons c then
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   123
        let val k = length function_typs in
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   124
          if k < 2 orelse length ts = k
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   125
          then (str o deresolve) c
38922
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38921
diff changeset
   126
            :: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   127
          else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   128
        end
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   129
      else if is_pseudo_fun c
29175
27cd8cce605e tackling simultaneous val/fun bindings
haftmann
parents: 28971
diff changeset
   130
        then (str o deresolve) c @@ str "()"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   131
      else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) iss
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   132
        @ map (print_term is_pseudo_fun some_thm vars BR) ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   133
    and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun)
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   134
      (print_term is_pseudo_fun) const_syntax some_thm vars
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   135
    and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   136
    and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   137
          let
29952
9aed85067721 unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents: 29264
diff changeset
   138
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   139
            fun print_match ((pat, ty), t) vars =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   140
              vars
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   141
              |> print_bind is_pseudo_fun some_thm NOBR pat
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   142
              |>> (fn p => semicolon [str "val", p, str "=",
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   143
                    print_term is_pseudo_fun some_thm vars NOBR t])
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   144
            val (ps, vars') = fold_map print_match binds vars;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   145
          in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   146
            Pretty.chunks [
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   147
              Pretty.block [str "let", Pretty.fbrk, Pretty.chunks ps],
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   148
              Pretty.block [str "in", Pretty.fbrk, print_term is_pseudo_fun some_thm vars' NOBR body],
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   149
              str "end"
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   150
            ]
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   151
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   152
      | print_case is_pseudo_fun some_thm vars fxy (((t, ty), clause :: clauses), _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   153
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   154
            fun print_select delim (pat, body) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   155
              let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   156
                val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   157
              in
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   158
                concat [str delim, p, str "=>", print_term is_pseudo_fun some_thm vars' NOBR body]
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   159
              end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   160
          in
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   161
            brackets (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   162
              str "case"
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   163
              :: print_term is_pseudo_fun some_thm vars NOBR t
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   164
              :: print_select "of" clause
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   165
              :: map (print_select "|") clauses
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   166
            )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   167
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   168
      | print_case is_pseudo_fun some_thm vars fxy ((_, []), _) =
31121
f79a0d03b75f tuned exception code
haftmann
parents: 31063
diff changeset
   169
          (concat o map str) ["raise", "Fail", "\"empty case\""];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   170
    fun print_val_decl print_typscheme (name, typscheme) = concat
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   171
      [str "val", str (deresolve name), str ":", print_typscheme typscheme];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   172
    fun print_datatype_decl definer (tyco, (vs, cos)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   173
      let
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   174
        fun print_co ((co, _), []) = str (deresolve co)
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   175
          | print_co ((co, _), tys) = concat [str (deresolve co), str "of",
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   176
              enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   177
      in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   178
        concat (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   179
          str definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   180
          :: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   181
          :: str "="
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   182
          :: separate (str "|") (map print_co cos)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   183
        )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   184
      end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   185
    fun print_def is_pseudo_fun needs_typ definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   186
          (ML_Function (name, (vs_ty as (vs, ty), eq :: eqs))) =
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   187
          let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   188
            fun print_eqn definer ((ts, t), (some_thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   189
              let
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   190
                val consts = fold Code_Thingol.add_constnames (t :: ts) [];
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   191
                val vars = reserved
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   192
                  |> intro_base_names
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   193
                       (is_none o const_syntax) deresolve consts
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   194
                  |> intro_vars ((fold o Code_Thingol.fold_varnames)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   195
                       (insert (op =)) ts []);
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   196
                val prolog = if needs_typ then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   197
                  concat [str definer, (str o deresolve) name, str ":", print_typ NOBR ty]
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   198
                    else (concat o map str) [definer, deresolve name];
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   199
              in
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   200
                concat (
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   201
                  prolog
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   202
                  :: (if is_pseudo_fun name then [str "()"]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   203
                      else print_dict_args vs
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   204
                        @ map (print_term is_pseudo_fun some_thm vars BR) ts)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   205
                  @ str "="
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   206
                  @@ print_term is_pseudo_fun some_thm vars NOBR t
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   207
                )
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   208
              end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   209
            val shift = if null eqs then I else
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   210
              map (Pretty.block o single o Pretty.block o single);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   211
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   212
            (print_val_decl print_typscheme (name, vs_ty))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   213
            ((Pretty.block o Pretty.fbreaks o shift) (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   214
              print_eqn definer eq
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   215
              :: map (print_eqn "|") eqs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   216
            ))
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   217
          end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   218
      | print_def is_pseudo_fun _ definer
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   219
          (ML_Instance (inst, ((class, (tyco, vs)), (super_instances, (classparam_instances, _))))) =
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   220
          let
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   221
            fun print_super_instance (_, (classrel, dss)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   222
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   223
                (str o Long_Name.base_name o deresolve) classrel,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   224
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   225
                print_dict is_pseudo_fun NOBR (DictConst dss)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   226
              ];
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   227
            fun print_classparam_instance ((classparam, const), (thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   228
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   229
                (str o Long_Name.base_name o deresolve) classparam,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   230
                str "=",
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   231
                print_app (K false) (SOME thm) reserved NOBR (const, [])
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   232
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   233
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   234
            (print_val_decl print_dicttypscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   235
              (inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   236
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   237
              str definer
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   238
              :: (str o deresolve) inst
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   239
              :: (if is_pseudo_fun inst then [str "()"]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   240
                  else print_dict_args vs)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   241
              @ str "="
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   242
              :: enum "," "{" "}"
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   243
                (map print_super_instance super_instances
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   244
                  @ map print_classparam_instance classparam_instances)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   245
              :: str ":"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   246
              @@ print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   247
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   248
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   249
    fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   250
          [print_val_decl print_typscheme (name, vs_ty)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   251
          ((semicolon o map str) (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   252
            (if n = 0 then "val" else "fun")
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   253
            :: deresolve name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   254
            :: replicate n "_"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   255
            @ "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   256
            :: "raise"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   257
            :: "Fail"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   258
            @@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   259
          ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   260
      | print_stmt (ML_Val binding) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   261
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   262
            val (sig_p, p) = print_def (K false) true "val" binding
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   263
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   264
            [sig_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   265
            (semicolon [p])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   266
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   267
      | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   268
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   269
            val print_def' = print_def (member (op =) pseudo_funs) false;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   270
            fun print_pseudo_fun name = concat [
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   271
                str "val",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   272
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   273
                str "=",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   274
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   275
                str "();"
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   276
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   277
            val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   278
              (print_def' "fun" binding :: map (print_def' "and") bindings);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   279
            val pseudo_ps = map print_pseudo_fun pseudo_funs;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   280
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   281
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   282
            (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   283
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   284
     | print_stmt (ML_Datas [(tyco, (vs, []))]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   285
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   286
            val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   287
          in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   288
            pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   289
            [concat [str "type", ty_p]]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   290
            (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   291
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   292
     | print_stmt (ML_Datas (data :: datas)) = 
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   293
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   294
            val sig_ps = print_datatype_decl "datatype" data
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   295
              :: map (print_datatype_decl "and") datas;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   296
            val (ps, p) = split_last sig_ps;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   297
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   298
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   299
            (Pretty.chunks (ps @| semicolon [p]))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   300
          end
37447
ad3e04f289b6 transitive superclasses were also only a misunderstanding
haftmann
parents: 37446
diff changeset
   301
     | print_stmt (ML_Class (class, (v, (super_classes, classparams)))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   302
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   303
            fun print_field s p = concat [str s, str ":", p];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   304
            fun print_proj s p = semicolon
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   305
              (map str ["val", s, "=", "#" ^ s, ":"] @| p);
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   306
            fun print_super_class_decl (super_class, classrel) =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   307
              print_val_decl print_dicttypscheme
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   308
                (classrel, ([(v, [class])], (super_class, ITyVar v)));
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   309
            fun print_super_class_field (super_class, classrel) =
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   310
              print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v));
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   311
            fun print_super_class_proj (super_class, classrel) =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   312
              print_proj (deresolve classrel)
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   313
                (print_dicttypscheme ([(v, [class])], (super_class, ITyVar v)));
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   314
            fun print_classparam_decl (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   315
              print_val_decl print_typscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   316
                (classparam, ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   317
            fun print_classparam_field (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   318
              print_field (deresolve classparam) (print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   319
            fun print_classparam_proj (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   320
              print_proj (deresolve classparam)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   321
                (print_typscheme ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   322
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   323
            (concat [str "type", print_dicttyp (class, ITyVar v)]
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   324
              :: map print_super_class_decl super_classes
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   325
              @ map print_classparam_decl classparams)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   326
            (Pretty.chunks (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   327
              concat [
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   328
                str ("type '" ^ v),
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   329
                (str o deresolve) class,
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   330
                str "=",
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   331
                enum "," "{" "};" (
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   332
                  map print_super_class_field super_classes
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   333
                  @ map print_classparam_field classparams
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   334
                )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   335
              ]
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   336
              :: map print_super_class_proj super_classes
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   337
              @ map print_classparam_proj classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   338
            ))
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   339
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   340
  in print_stmt end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   341
38933
bd77e092f67c avoid strange special treatment of empty module names
haftmann
parents: 38928
diff changeset
   342
fun print_sml_module name some_decls body =
bd77e092f67c avoid strange special treatment of empty module names
haftmann
parents: 38928
diff changeset
   343
  Pretty.chunks2 (
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   344
    Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   345
      str ("structure " ^ name ^ (if is_some some_decls then " : sig" else " ="))
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   346
      :: (the_list o Option.map (indent 2 o Pretty.chunks)) some_decls
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   347
      @| (if is_some some_decls then str "end = struct" else str "struct")
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   348
    )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   349
    :: body
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   350
    @| str ("end; (*struct " ^ name ^ "*)")
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   351
  );
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   352
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   353
val literals_sml = Literals {
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   354
  literal_char = prefix "#" o quote o ML_Syntax.print_char,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   355
  literal_string = quote o translate_string ML_Syntax.print_char,
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   356
  literal_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)",
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   357
  literal_positive_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)",
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37822
diff changeset
   358
  literal_alternative_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)",
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   359
  literal_naive_numeral = string_of_int,
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   360
  literal_list = enum "," "[" "]",
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   361
  infix_cons = (7, "::")
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   362
};
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   363
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   364
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   365
(** OCaml serializer **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   366
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   367
fun print_ocaml_stmt labelled_name tyco_syntax const_syntax reserved is_cons deresolve =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   368
  let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   369
    fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   370
      | print_tyco_expr fxy (tyco, [ty]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   371
          concat [print_typ BR ty, (str o deresolve) tyco]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   372
      | print_tyco_expr fxy (tyco, tys) =
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   373
          concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   374
    and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   375
         of NONE => print_tyco_expr fxy (tyco, tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   376
          | SOME (i, print) => print print_typ fxy tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   377
      | print_typ fxy (ITyVar v) = str ("'" ^ v);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   378
    fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]);
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   379
    fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   380
      (map_filter (fn (v, sort) =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   381
        (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   382
    fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   383
    fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   384
    fun print_dict is_pseudo_fun fxy (DictConst (inst, dss)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   385
          brackify fxy ((str o deresolve) inst ::
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   386
            (if is_pseudo_fun inst then [str "()"]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   387
            else map_filter (print_dicts is_pseudo_fun BR) dss))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   388
      | print_dict is_pseudo_fun fxy (DictVar (classrels, (v, (i, k)))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   389
          str (if k = 1 then "_" ^ first_upper v
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   390
            else "_" ^ first_upper v ^ string_of_int (i+1))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   391
          |> fold_rev (fn classrel => fn p =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   392
               Pretty.block [p, str ".", (str o deresolve) classrel]) classrels
38922
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38921
diff changeset
   393
    and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   394
    val print_dict_args = map_filter (fn (v, sort) => print_dicts (K false) BR
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   395
      (map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort));
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   396
    fun print_term is_pseudo_fun some_thm vars fxy (IConst c) =
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   397
          print_app is_pseudo_fun some_thm vars fxy (c, [])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   398
      | print_term is_pseudo_fun some_thm vars fxy (IVar NONE) =
31889
fb2c8a687529 all variable names are optional
haftmann
parents: 31874
diff changeset
   399
          str "_"
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   400
      | print_term is_pseudo_fun some_thm vars fxy (IVar (SOME v)) =
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   401
          str (lookup_var vars v)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   402
      | print_term is_pseudo_fun some_thm vars fxy (t as t1 `$ t2) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   403
          (case Code_Thingol.unfold_const_app t
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   404
           of SOME c_ts => print_app is_pseudo_fun some_thm vars fxy c_ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   405
            | NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1,
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   406
                print_term is_pseudo_fun some_thm vars BR t2])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   407
      | print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   408
          let
31874
f172346ba805 simplified binding concept
haftmann
parents: 31775
diff changeset
   409
            val (binds, t') = Code_Thingol.unfold_pat_abs t;
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   410
            val (ps, vars') = fold_map (print_bind is_pseudo_fun some_thm BR o fst) binds vars;
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   411
          in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun some_thm vars' NOBR t') end
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   412
      | print_term is_pseudo_fun some_thm vars fxy (ICase (cases as (_, t0))) =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   413
          (case Code_Thingol.unfold_const_app t0
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   414
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   415
                then print_case is_pseudo_fun some_thm vars fxy cases
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   416
                else print_app is_pseudo_fun some_thm vars fxy c_ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   417
            | NONE => print_case is_pseudo_fun some_thm vars fxy cases)
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   418
    and print_app_expr is_pseudo_fun some_thm vars (app as ((c, ((_, iss), tys)), ts)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   419
      if is_cons c then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   420
        let val k = length tys in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   421
          if length ts = k
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   422
          then (str o deresolve) c
38922
ec2a8efd8990 Code_Printer.tuplify
haftmann
parents: 38921
diff changeset
   423
            :: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   424
          else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   425
        end
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   426
      else if is_pseudo_fun c
29175
27cd8cce605e tackling simultaneous val/fun bindings
haftmann
parents: 28971
diff changeset
   427
        then (str o deresolve) c @@ str "()"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   428
      else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) iss
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   429
        @ map (print_term is_pseudo_fun some_thm vars BR) ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   430
    and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun)
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   431
      (print_term is_pseudo_fun) const_syntax some_thm vars
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   432
    and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   433
    and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   434
          let
29952
9aed85067721 unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents: 29264
diff changeset
   435
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   436
            fun print_let ((pat, ty), t) vars =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   437
              vars
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   438
              |> print_bind is_pseudo_fun some_thm NOBR pat
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   439
              |>> (fn p => concat
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   440
                  [str "let", p, str "=", print_term is_pseudo_fun some_thm vars NOBR t, str "in"])
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   441
            val (ps, vars') = fold_map print_let binds vars;
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   442
          in
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   443
            brackify_block fxy (Pretty.chunks ps) []
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   444
              (print_term is_pseudo_fun some_thm vars' NOBR body)
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   445
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   446
      | print_case is_pseudo_fun some_thm vars fxy (((t, ty), clause :: clauses), _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   447
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   448
            fun print_select delim (pat, body) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   449
              let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   450
                val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars;
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   451
              in concat [str delim, p, str "->", print_term is_pseudo_fun some_thm vars' NOBR body] end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   452
          in
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   453
            brackets (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   454
              str "match"
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   455
              :: print_term is_pseudo_fun some_thm vars NOBR t
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   456
              :: print_select "with" clause
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   457
              :: map (print_select "|") clauses
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   458
            )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   459
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   460
      | print_case is_pseudo_fun some_thm vars fxy ((_, []), _) =
31121
f79a0d03b75f tuned exception code
haftmann
parents: 31063
diff changeset
   461
          (concat o map str) ["failwith", "\"empty case\""];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   462
    fun print_val_decl print_typscheme (name, typscheme) = concat
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   463
      [str "val", str (deresolve name), str ":", print_typscheme typscheme];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   464
    fun print_datatype_decl definer (tyco, (vs, cos)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   465
      let
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   466
        fun print_co ((co, _), []) = str (deresolve co)
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   467
          | print_co ((co, _), tys) = concat [str (deresolve co), str "of",
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   468
              enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   469
      in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   470
        concat (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   471
          str definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   472
          :: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   473
          :: str "="
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   474
          :: separate (str "|") (map print_co cos)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   475
        )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   476
      end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   477
    fun print_def is_pseudo_fun needs_typ definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   478
          (ML_Function (name, (vs_ty as (vs, ty), eqs))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   479
          let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   480
            fun print_eqn ((ts, t), (some_thm, _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   481
              let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   482
                val consts = fold Code_Thingol.add_constnames (t :: ts) [];
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   483
                val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   484
                  |> intro_vars ((fold o Code_Thingol.fold_varnames)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   485
                      (insert (op =)) ts []);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   486
              in concat [
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   487
                (Pretty.block o commas)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   488
                  (map (print_term is_pseudo_fun some_thm vars NOBR) ts),
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   489
                str "->",
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   490
                print_term is_pseudo_fun some_thm vars NOBR t
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   491
              ] end;
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   492
            fun print_eqns is_pseudo [((ts, t), (some_thm, _))] =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   493
                  let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   494
                    val consts = fold Code_Thingol.add_constnames (t :: ts) [];
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   495
                    val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   496
                      |> intro_base_names
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   497
                          (is_none o const_syntax) deresolve consts
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   498
                      |> intro_vars ((fold o Code_Thingol.fold_varnames)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   499
                          (insert (op =)) ts []);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   500
                  in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   501
                    concat (
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   502
                      (if is_pseudo then [str "()"]
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   503
                        else map (print_term is_pseudo_fun some_thm vars BR) ts)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   504
                      @ str "="
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   505
                      @@ print_term is_pseudo_fun some_thm vars NOBR t
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   506
                    )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   507
                  end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   508
              | print_eqns _ ((eq as (([_], _), _)) :: eqs) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   509
                  Pretty.block (
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   510
                    str "="
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   511
                    :: Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   512
                    :: str "function"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   513
                    :: Pretty.brk 1
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   514
                    :: print_eqn eq
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   515
                    :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   516
                          o single o print_eqn) eqs
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   517
                  )
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   518
              | print_eqns _ (eqs as eq :: eqs') =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   519
                  let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   520
                    val consts = fold Code_Thingol.add_constnames (map (snd o fst) eqs) [];
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   521
                    val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   522
                      |> intro_base_names
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   523
                          (is_none o const_syntax) deresolve consts;
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   524
                    val dummy_parms = (map str o aux_params vars o map (fst o fst)) eqs;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   525
                  in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   526
                    Pretty.block (
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   527
                      Pretty.breaks dummy_parms
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   528
                      @ Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   529
                      :: str "="
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   530
                      :: Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   531
                      :: str "match"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   532
                      :: Pretty.brk 1
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   533
                      :: (Pretty.block o commas) dummy_parms
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   534
                      :: Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   535
                      :: str "with"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   536
                      :: Pretty.brk 1
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   537
                      :: print_eqn eq
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   538
                      :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   539
                           o single o print_eqn) eqs'
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   540
                    )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   541
                  end;
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   542
            val prolog = if needs_typ then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   543
              concat [str definer, (str o deresolve) name, str ":", print_typ NOBR ty]
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   544
                else (concat o map str) [definer, deresolve name];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   545
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   546
            (print_val_decl print_typscheme (name, vs_ty))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   547
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   548
              prolog
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   549
              :: print_dict_args vs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   550
              @| print_eqns (is_pseudo_fun name) eqs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   551
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   552
          end
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   553
      | print_def is_pseudo_fun _ definer
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   554
            (ML_Instance (inst, ((class, (tyco, vs)), (super_instances, (classparam_instances, _))))) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   555
          let
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   556
            fun print_super_instance (_, (classrel, dss)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   557
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   558
                (str o deresolve) classrel,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   559
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   560
                print_dict is_pseudo_fun NOBR (DictConst dss)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   561
              ];
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   562
            fun print_classparam_instance ((classparam, const), (thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   563
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   564
                (str o deresolve) classparam,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   565
                str "=",
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   566
                print_app (K false) (SOME thm) reserved NOBR (const, [])
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   567
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   568
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   569
            (print_val_decl print_dicttypscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   570
              (inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   571
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   572
              str definer
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   573
              :: (str o deresolve) inst
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   574
              :: print_dict_args vs
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   575
              @ str "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   576
              @@ brackets [
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   577
                enum_default "()" ";" "{" "}" (map print_super_instance super_instances
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   578
                  @ map print_classparam_instance classparam_instances),
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   579
                str ":",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   580
                print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs])
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   581
              ]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   582
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   583
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   584
     fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   585
          [print_val_decl print_typscheme (name, vs_ty)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   586
          ((doublesemicolon o map str) (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   587
            "let"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   588
            :: deresolve name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   589
            :: replicate n "_"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   590
            @ "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   591
            :: "failwith"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   592
            @@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   593
          ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   594
      | print_stmt (ML_Val binding) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   595
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   596
            val (sig_p, p) = print_def (K false) true "let" binding
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   597
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   598
            [sig_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   599
            (doublesemicolon [p])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   600
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   601
      | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   602
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   603
            val print_def' = print_def (member (op =) pseudo_funs) false;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   604
            fun print_pseudo_fun name = concat [
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   605
                str "let",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   606
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   607
                str "=",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   608
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   609
                str "();;"
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   610
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   611
            val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   612
              (print_def' "let rec" binding :: map (print_def' "and") bindings);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   613
            val pseudo_ps = map print_pseudo_fun pseudo_funs;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   614
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   615
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   616
            (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   617
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   618
     | print_stmt (ML_Datas [(tyco, (vs, []))]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   619
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   620
            val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   621
          in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   622
            pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   623
            [concat [str "type", ty_p]]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   624
            (concat [str "type", ty_p, str "=", str "EMPTY__"])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   625
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   626
     | print_stmt (ML_Datas (data :: datas)) = 
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   627
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   628
            val sig_ps = print_datatype_decl "type" data
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   629
              :: map (print_datatype_decl "and") datas;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   630
            val (ps, p) = split_last sig_ps;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   631
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   632
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   633
            (Pretty.chunks (ps @| doublesemicolon [p]))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   634
          end
37447
ad3e04f289b6 transitive superclasses were also only a misunderstanding
haftmann
parents: 37446
diff changeset
   635
     | print_stmt (ML_Class (class, (v, (super_classes, classparams)))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   636
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   637
            fun print_field s p = concat [str s, str ":", p];
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   638
            fun print_super_class_field (super_class, classrel) =
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   639
              print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v));
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   640
            fun print_classparam_decl (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   641
              print_val_decl print_typscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   642
                (classparam, ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   643
            fun print_classparam_field (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   644
              print_field (deresolve classparam) (print_typ NOBR ty);
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   645
            val w = "_" ^ first_upper v;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   646
            fun print_classparam_proj (classparam, _) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   647
              (concat o map str) ["let", deresolve classparam, w, "=",
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   648
                w ^ "." ^ deresolve classparam ^ ";;"];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   649
            val type_decl_p = concat [
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   650
                str ("type '" ^ v),
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   651
                (str o deresolve) class,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   652
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   653
                enum_default "unit" ";" "{" "}" (
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   654
                  map print_super_class_field super_classes
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   655
                  @ map print_classparam_field classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   656
                )
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   657
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   658
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   659
            (type_decl_p :: map print_classparam_decl classparams)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   660
            (Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   661
              doublesemicolon [type_decl_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   662
              :: map print_classparam_proj classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   663
            ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   664
          end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   665
  in print_stmt end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   666
38933
bd77e092f67c avoid strange special treatment of empty module names
haftmann
parents: 38928
diff changeset
   667
fun print_ocaml_module name some_decls body =
bd77e092f67c avoid strange special treatment of empty module names
haftmann
parents: 38928
diff changeset
   668
  Pretty.chunks2 (
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   669
    Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   670
      str ("module " ^ name ^ (if is_some some_decls then " : sig" else " ="))
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   671
      :: (the_list o Option.map (indent 2 o Pretty.chunks)) some_decls
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   672
      @| (if is_some some_decls then str "end = struct" else str "struct")
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   673
    )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   674
    :: body
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   675
    @| str ("end;; (*struct " ^ name ^ "*)")
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   676
  );
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   677
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   678
val literals_ocaml = let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   679
  fun chr i =
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   680
    let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   681
      val xs = string_of_int i;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   682
      val ys = replicate_string (3 - length (explode xs)) "0";
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   683
    in "\\" ^ ys ^ xs end;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   684
  fun char_ocaml c =
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   685
    let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   686
      val i = ord c;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   687
      val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   688
        then chr i else c
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   689
    in s end;
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   690
  fun numeral_ocaml k = if k < 0
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   691
    then "(Big_int.minus_big_int " ^ numeral_ocaml (~ k) ^ ")"
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   692
    else if k <= 1073741823
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   693
      then "(Big_int.big_int_of_int " ^ string_of_int k ^ ")"
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   694
      else "(Big_int.big_int_of_string " ^ quote (string_of_int k) ^ ")"
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   695
in Literals {
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   696
  literal_char = Library.enclose "'" "'" o char_ocaml,
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   697
  literal_string = quote o translate_string char_ocaml,
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   698
  literal_numeral = numeral_ocaml,
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   699
  literal_positive_numeral = numeral_ocaml,
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37822
diff changeset
   700
  literal_alternative_numeral = numeral_ocaml,
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   701
  literal_naive_numeral = numeral_ocaml,
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   702
  literal_list = enum ";" "[" "]",
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   703
  infix_cons = (6, "::")
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   704
} end;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   705
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   706
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   707
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   708
(** SML/OCaml generic part **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   709
39028
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   710
fun ml_program_of_program labelled_name reserved module_alias program =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   711
  let
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   712
    fun namify_const upper base (nsp_const, nsp_type) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   713
      let
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   714
        val (base', nsp_const') = yield_singleton Name.variants
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   715
          (if upper then first_upper base else base) nsp_const
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   716
      in (base', (nsp_const', nsp_type)) end;
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   717
    fun namify_type base (nsp_const, nsp_type) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   718
      let
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   719
        val (base', nsp_type') = yield_singleton Name.variants base nsp_type
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   720
      in (base', (nsp_const, nsp_type')) end;
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   721
    fun namify_stmt (Code_Thingol.Fun _) = namify_const false
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   722
      | namify_stmt (Code_Thingol.Datatype _) = namify_type
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   723
      | namify_stmt (Code_Thingol.Datatypecons _) = namify_const true
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   724
      | namify_stmt (Code_Thingol.Class _) = namify_type
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   725
      | namify_stmt (Code_Thingol.Classrel _) = namify_const false
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   726
      | namify_stmt (Code_Thingol.Classparam _) = namify_const false
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   727
      | namify_stmt (Code_Thingol.Classinst _) = namify_const false;
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   728
    fun ml_binding_of_stmt (name, Code_Thingol.Fun (_, ((tysm as (vs, ty), raw_eqs), _))) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   729
          let
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   730
            val eqs = filter (snd o snd) raw_eqs;
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   731
            val (eqs', some_value_name) = if null (filter_out (null o snd) vs) then case eqs
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   732
               of [(([], t), some_thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   733
                  then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE)
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   734
                  else (eqs, SOME (name, member (op =) (Code_Thingol.add_constnames t []) name))
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   735
                | _ => (eqs, NONE)
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   736
              else (eqs, NONE)
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   737
          in (ML_Function (name, (tysm, eqs')), some_value_name) end
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   738
      | ml_binding_of_stmt (name, Code_Thingol.Classinst (stmt as ((_, (_, vs)), _))) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   739
          (ML_Instance (name, stmt), if forall (null o snd) vs then SOME (name, false) else NONE)
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   740
      | ml_binding_of_stmt (name, _) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   741
          error ("Binding block containing illegal statement: " ^ labelled_name name)
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   742
    fun modify_fun (name, stmt) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   743
      let
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   744
        val (binding, some_value_name) = ml_binding_of_stmt (name, stmt);
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   745
        val ml_stmt = case binding
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   746
         of ML_Function (name, ((vs, ty), [])) =>
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   747
              ML_Exc (name, ((vs, ty),
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   748
                (length o filter_out (null o snd)) vs + (length o fst o Code_Thingol.unfold_fun) ty))
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   749
          | _ => case some_value_name
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   750
             of NONE => ML_Funs ([binding], [])
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   751
              | SOME (name, true) => ML_Funs ([binding], [name])
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   752
              | SOME (name, false) => ML_Val binding
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   753
      in SOME ml_stmt end;
39031
b27d6643591c dropped dead code; tuned
haftmann
parents: 39028
diff changeset
   754
    fun modify_funs stmts = single (SOME
b27d6643591c dropped dead code; tuned
haftmann
parents: 39028
diff changeset
   755
      (ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst)))
b27d6643591c dropped dead code; tuned
haftmann
parents: 39028
diff changeset
   756
    fun modify_datatypes stmts = single (SOME
b27d6643591c dropped dead code; tuned
haftmann
parents: 39028
diff changeset
   757
      (ML_Datas (map_filter
b27d6643591c dropped dead code; tuned
haftmann
parents: 39028
diff changeset
   758
        (fn (name, Code_Thingol.Datatype (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts)))
b27d6643591c dropped dead code; tuned
haftmann
parents: 39028
diff changeset
   759
    fun modify_class stmts = single (SOME
b27d6643591c dropped dead code; tuned
haftmann
parents: 39028
diff changeset
   760
      (ML_Class (the_single (map_filter
b27d6643591c dropped dead code; tuned
haftmann
parents: 39028
diff changeset
   761
        (fn (name, Code_Thingol.Class (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts))))
39059
3a11a667af75 restored and added surpression of case combinators
haftmann
parents: 39058
diff changeset
   762
    fun modify_stmts ([stmt as (name, stmt' as Code_Thingol.Fun _)]) =
3a11a667af75 restored and added surpression of case combinators
haftmann
parents: 39058
diff changeset
   763
          if Code_Thingol.is_case stmt' then [] else [modify_fun stmt]
39028
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   764
      | modify_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) =
39061
9b1fd2df743c swapped slip
haftmann
parents: 39059
diff changeset
   765
          modify_funs (filter_out (Code_Thingol.is_case o snd) stmts)
39028
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   766
      | modify_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   767
          modify_datatypes stmts
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   768
      | modify_stmts ((stmts as (_, Code_Thingol.Datatype _)::_)) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   769
          modify_datatypes stmts
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   770
      | modify_stmts ((stmts as (_, Code_Thingol.Class _)::_)) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   771
          modify_class stmts
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   772
      | modify_stmts ((stmts as (_, Code_Thingol.Classrel _)::_)) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   773
          modify_class stmts
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   774
      | modify_stmts ((stmts as (_, Code_Thingol.Classparam _)::_)) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   775
          modify_class stmts
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   776
      | modify_stmts ([stmt as (_, Code_Thingol.Classinst _)]) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   777
          [modify_fun stmt]
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   778
      | modify_stmts ((stmts as (_, Code_Thingol.Classinst _)::_)) =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   779
          modify_funs stmts
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   780
      | modify_stmts stmts = error ("Illegal mutual dependencies: " ^
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   781
          (Library.commas o map (labelled_name o fst)) stmts);
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   782
  in
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   783
    Code_Namespace.hierarchical_program labelled_name { module_alias = module_alias, reserved = reserved,
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   784
      empty_nsp = (reserved, reserved), namify_module = pair, namify_stmt = namify_stmt,
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   785
      cyclic_modules = false, empty_data = (), memorize_data = K I, modify_stmts = modify_stmts } program
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   786
  end;
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   787
39148
haftmann
parents: 39142 39147
diff changeset
   788
fun serialize_ml target print_ml_module print_ml_stmt with_signatures
39142
f63715f00fdd dropped names from serializer interface
haftmann
parents: 39102
diff changeset
   789
    { labelled_name, reserved_syms, includes, module_alias,
f63715f00fdd dropped names from serializer interface
haftmann
parents: 39102
diff changeset
   790
      class_syntax, tyco_syntax, const_syntax, program } =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   791
  let
39147
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   792
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   793
    (* build program *)
39028
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   794
    val { deresolver, hierarchical_program = ml_program } =
0dd6c5a0beef use code_namespace module for SML and OCaml code
haftmann
parents: 38966
diff changeset
   795
      ml_program_of_program labelled_name (Name.make_context reserved_syms) module_alias program;
39147
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   796
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   797
    (* print statements *)
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   798
    fun print_stmt prefix_fragments (_, stmt) = print_ml_stmt
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   799
      labelled_name tyco_syntax const_syntax (make_vars reserved_syms)
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   800
      (Code_Thingol.is_cons program) (deresolver prefix_fragments) stmt
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   801
      |> apfst SOME;
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   802
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   803
    (* print modules *)
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   804
    fun print_module prefix_fragments base _ xs =
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   805
      let
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   806
        val (raw_decls, body) = split_list xs;
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   807
        val decls = if with_signatures then SOME (maps these raw_decls) else NONE 
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   808
      in (NONE, print_ml_module base decls body) end;
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   809
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   810
    (* serialization *)
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   811
    val p = Pretty.chunks2 (map snd includes
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   812
      @ map snd (Code_Namespace.print_hierarchical {
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   813
        print_module = print_module, print_stmt = print_stmt,
3c284a152bd6 printing combinator for hierarchical programs
haftmann
parents: 39102
diff changeset
   814
        lift_markup = apsnd } ml_program));
39056
fa197571676b formal markup of generated code for statements
haftmann
parents: 39034
diff changeset
   815
    fun write width NONE = writeln o format [] width
fa197571676b formal markup of generated code for statements
haftmann
parents: 39034
diff changeset
   816
      | write width (SOME p) = File.write p o format [] width;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   817
  in
39102
4ae1d212100f hand out deresolver from serializer invocation
haftmann
parents: 39061
diff changeset
   818
    Code_Target.serialization write (rpair (try (deresolver [])) ooo format) p
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   819
  end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   820
38966
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38933
diff changeset
   821
val serializer_sml : Code_Target.serializer =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   822
  Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   823
  >> (fn with_signatures => serialize_ml target_SML
38924
fcd1d0457e27 removed serializer interface redundancies
haftmann
parents: 38923
diff changeset
   824
      print_sml_module print_sml_stmt with_signatures));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   825
38966
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38933
diff changeset
   826
val serializer_ocaml : Code_Target.serializer =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   827
  Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
37748
0af0d45257be dropped ancient in-place compilation of SML
haftmann
parents: 37745
diff changeset
   828
  >> (fn with_signatures => serialize_ml target_OCaml
38924
fcd1d0457e27 removed serializer interface redundancies
haftmann
parents: 38923
diff changeset
   829
      print_ocaml_module print_ocaml_stmt with_signatures));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   830
38966
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38933
diff changeset
   831
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38933
diff changeset
   832
(** Isar setup **)
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38933
diff changeset
   833
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   834
val setup =
37821
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   835
  Code_Target.add_target
38966
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38933
diff changeset
   836
    (target_SML, { serializer = serializer_sml, literals = literals_sml,
37822
cf3588177676 use generic description slot for formal code checking
haftmann
parents: 37821
diff changeset
   837
      check = { env_var = "ISABELLE_PROCESS", make_destination = fn p => Path.append p (Path.explode "ROOT.ML"),
38863
9070a7c356c9 code checking: compiler invocation happens in same directory as generated file -- avoid problem with different path representations on cygwin
haftmann
parents: 38784
diff changeset
   838
        make_command = fn isabelle => fn _ => isabelle ^ " -r -q -u Pure" } })
37821
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   839
  #> Code_Target.add_target
38966
68853347ba37 tuned internally and made smlnj happy
haftmann
parents: 38933
diff changeset
   840
    (target_OCaml, { serializer = serializer_ocaml, literals = literals_ocaml,
37822
cf3588177676 use generic description slot for formal code checking
haftmann
parents: 37821
diff changeset
   841
      check = { env_var = "EXEC_OCAML", make_destination = fn p => Path.append p (Path.explode "ROOT.ocaml"),
38863
9070a7c356c9 code checking: compiler invocation happens in same directory as generated file -- avoid problem with different path representations on cygwin
haftmann
parents: 38784
diff changeset
   842
        make_command = fn ocaml => fn _ => ocaml ^ " -w pu nums.cma ROOT.ocaml" } })
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   843
  #> Code_Target.add_tyco_syntax target_SML "fun" (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
37242
97097e589715 brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents: 36536
diff changeset
   844
      brackify_infix (1, R) fxy (
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   845
        print_typ (INFX (1, X)) ty1,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   846
        str "->",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   847
        print_typ (INFX (1, R)) ty2
37242
97097e589715 brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents: 36536
diff changeset
   848
      )))
38923
79d7f2b4cf71 more coherent naming of syntax data structures
haftmann
parents: 38922
diff changeset
   849
  #> Code_Target.add_tyco_syntax target_OCaml "fun" (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
37242
97097e589715 brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents: 36536
diff changeset
   850
      brackify_infix (1, R) fxy (
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   851
        print_typ (INFX (1, X)) ty1,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   852
        str "->",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   853
        print_typ (INFX (1, R)) ty2
37242
97097e589715 brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents: 36536
diff changeset
   854
      )))
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   855
  #> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   856
  #> fold (Code_Target.add_reserved target_SML)
38070
5beae0d6b7bc rebinding ref is illegal
haftmann
parents: 37958
diff changeset
   857
      ["ref" (*rebinding is illegal*), "o" (*dictionary projections use it already*),
5beae0d6b7bc rebinding ref is illegal
haftmann
parents: 37958
diff changeset
   858
        "Fail", "div", "mod" (*standard infixes*), "IntInf"]
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   859
  #> fold (Code_Target.add_reserved target_OCaml) [
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   860
      "and", "as", "assert", "begin", "class",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   861
      "constraint", "do", "done", "downto", "else", "end", "exception",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   862
      "external", "false", "for", "fun", "function", "functor", "if",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   863
      "in", "include", "inherit", "initializer", "lazy", "let", "match", "method",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   864
      "module", "mutable", "new", "object", "of", "open", "or", "private", "rec",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   865
      "sig", "struct", "then", "to", "true", "try", "type", "val",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   866
      "virtual", "when", "while", "with"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   867
    ]
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   868
  #> fold (Code_Target.add_reserved target_OCaml) ["failwith", "mod", "Big_int"];
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   869
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   870
end; (*struct*)