src/Tools/Code/code_ml.ML
author haftmann
Thu, 26 Aug 2010 12:30:43 +0200
changeset 38778 49b885736e8f
parent 38070 5beae0d6b7bc
child 38779 89f654951200
permissions -rw-r--r--
private version of commas, cf. printmode
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
34032
f13b5c023e65 simplified notion of empty module name
haftmann
parents: 34028
diff changeset
    11
  val evaluation_code_of: theory -> string -> string
34028
1e6206763036 split off evaluation mechanisms in separte module Code_Eval
haftmann
parents: 33992
diff changeset
    12
    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> string * string option list
36515
4073bf588746 exported print_tuple
haftmann
parents: 35228
diff changeset
    13
  val print_tuple: (Code_Printer.fixity -> 'a -> Pretty.T)
4073bf588746 exported print_tuple
haftmann
parents: 35228
diff changeset
    14
    -> Code_Printer.fixity -> 'a list -> Pretty.T option
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    15
  val setup: theory -> theory
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    16
end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    17
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    18
structure Code_ML : CODE_ML =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    19
struct
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    20
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    21
open Basic_Code_Thingol;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    22
open Code_Printer;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    23
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    24
infixr 5 @@;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    25
infixr 5 @|;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    26
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    27
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    28
(** generic **)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    29
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    30
val target_SML = "SML";
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    31
val target_OCaml = "OCaml";
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    32
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    33
datatype ml_binding =
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
    34
    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
    35
  | 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
    36
        * ((class * (string * (string * dict list list))) list
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
    37
      * (((string * const) * (thm * bool)) list * ((string * const) * (thm * bool)) list)));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    38
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    39
datatype ml_stmt =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    40
    ML_Exc of string * (typscheme * int)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    41
  | ML_Val of ml_binding
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    42
  | ML_Funs of ml_binding list * string list
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
    43
  | 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
    44
  | 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
    45
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    46
fun stmt_name_of_binding (ML_Function (name, _)) = name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    47
  | stmt_name_of_binding (ML_Instance (name, _)) = name;
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    48
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    49
fun stmt_names_of (ML_Exc (name, _)) = [name]
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    50
  | stmt_names_of (ML_Val binding) = [stmt_name_of_binding binding]
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    51
  | 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
    52
  | stmt_names_of (ML_Datas ds) = map fst ds
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    53
  | stmt_names_of (ML_Class (name, _)) = [name];
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    54
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    55
fun print_product _ [] = NONE
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    56
  | 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
    57
  | print_product print xs = (SOME o enum " *" "" "") (map print 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
fun print_tuple _ _ [] = NONE
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    60
  | print_tuple print fxy [x] = SOME (print fxy x)
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
    61
  | print_tuple print _ xs = SOME (enum "," "(" ")" (map (print NOBR) xs));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    62
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    63
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    64
(** SML serializer **)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    65
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    66
fun print_sml_stmt labelled_name syntax_tyco syntax_const reserved is_cons deresolve =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    67
  let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    68
    fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    69
      | print_tyco_expr fxy (tyco, [ty]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    70
          concat [print_typ BR ty, (str o deresolve) tyco]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    71
      | print_tyco_expr fxy (tyco, tys) =
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
    72
          concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    73
    and print_typ fxy (tyco `%% tys) = (case syntax_tyco tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    74
         of NONE => print_tyco_expr fxy (tyco, tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    75
          | SOME (i, print) => print print_typ fxy tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    76
      | print_typ fxy (ITyVar v) = str ("'" ^ v);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    77
    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
    78
    fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    79
      (map_filter (fn (v, sort) =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    80
        (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    81
    fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    82
    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
    83
    fun print_dict is_pseudo_fun fxy (DictConst (inst, dss)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    84
          brackify fxy ((str o deresolve) inst ::
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    85
            (if is_pseudo_fun inst then [str "()"]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    86
            else map_filter (print_dicts is_pseudo_fun BR) dss))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    87
      | print_dict is_pseudo_fun fxy (DictVar (classrels, (v, (i, k)))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    88
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    89
            val v_p = str (if k = 1 then first_upper v ^ "_"
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    90
              else first_upper v ^ string_of_int (i+1) ^ "_");
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    91
          in case classrels
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    92
           of [] => v_p
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    93
            | [classrel] => brackets [(str o deresolve) classrel, v_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    94
            | classrels => brackets
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
    95
                [enum " o" "(" ")" (map (str o deresolve) classrels), v_p]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    96
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    97
    and print_dicts is_pseudo_fun = print_tuple (print_dict is_pseudo_fun);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    98
    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
    99
      (map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort));
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   100
    fun print_term is_pseudo_fun some_thm vars fxy (IConst c) =
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   101
          print_app is_pseudo_fun some_thm vars fxy (c, [])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   102
      | print_term is_pseudo_fun some_thm vars fxy (IVar NONE) =
31889
fb2c8a687529 all variable names are optional
haftmann
parents: 31874
diff changeset
   103
          str "_"
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   104
      | 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
   105
          str (lookup_var vars v)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   106
      | 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
   107
          (case Code_Thingol.unfold_const_app t
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   108
           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
   109
            | NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1,
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   110
                print_term is_pseudo_fun some_thm vars BR t2])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   111
      | print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   112
          let
31874
f172346ba805 simplified binding concept
haftmann
parents: 31775
diff changeset
   113
            val (binds, t') = Code_Thingol.unfold_pat_abs t;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   114
            fun print_abs (pat, ty) =
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   115
              print_bind is_pseudo_fun some_thm NOBR pat
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   116
              #>> (fn p => concat [str "fn", p, str "=>"]);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   117
            val (ps, vars') = fold_map print_abs binds vars;
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   118
          in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   119
      | 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
   120
          (case Code_Thingol.unfold_const_app t0
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   121
           of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   122
                then print_case is_pseudo_fun some_thm vars fxy cases
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   123
                else print_app is_pseudo_fun some_thm vars fxy c_ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   124
            | NONE => print_case is_pseudo_fun some_thm vars fxy cases)
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   125
    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
   126
      if is_cons c then
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   127
        let val k = length function_typs in
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   128
          if k < 2 orelse length ts = k
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   129
          then (str o deresolve) c
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   130
            :: the_list (print_tuple (print_term is_pseudo_fun some_thm vars) BR ts)
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   131
          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
   132
        end
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   133
      else if is_pseudo_fun c
29175
27cd8cce605e tackling simultaneous val/fun bindings
haftmann
parents: 28971
diff changeset
   134
        then (str o deresolve) c @@ str "()"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   135
      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
   136
        @ map (print_term is_pseudo_fun some_thm vars BR) ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   137
    and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun)
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   138
      (print_term is_pseudo_fun) syntax_const some_thm vars
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   139
    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
   140
    and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   141
          let
29952
9aed85067721 unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents: 29264
diff changeset
   142
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   143
            fun print_match ((pat, ty), t) vars =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   144
              vars
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   145
              |> print_bind is_pseudo_fun some_thm NOBR pat
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   146
              |>> (fn p => semicolon [str "val", p, str "=",
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   147
                    print_term is_pseudo_fun some_thm vars NOBR t])
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   148
            val (ps, vars') = fold_map print_match binds vars;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   149
          in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   150
            Pretty.chunks [
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   151
              Pretty.block [str "let", Pretty.fbrk, Pretty.chunks ps],
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   152
              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
   153
              str "end"
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   154
            ]
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   155
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   156
      | 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
   157
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   158
            fun print_select delim (pat, body) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   159
              let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   160
                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
   161
              in
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   162
                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
   163
              end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   164
          in
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   165
            brackets (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   166
              str "case"
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   167
              :: print_term is_pseudo_fun some_thm vars NOBR t
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   168
              :: print_select "of" clause
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   169
              :: map (print_select "|") clauses
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   170
            )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   171
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   172
      | print_case is_pseudo_fun some_thm vars fxy ((_, []), _) =
31121
f79a0d03b75f tuned exception code
haftmann
parents: 31063
diff changeset
   173
          (concat o map str) ["raise", "Fail", "\"empty case\""];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   174
    fun print_val_decl print_typscheme (name, typscheme) = concat
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   175
      [str "val", str (deresolve name), str ":", print_typscheme typscheme];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   176
    fun print_datatype_decl definer (tyco, (vs, cos)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   177
      let
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   178
        fun print_co ((co, _), []) = str (deresolve co)
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   179
          | 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
   180
              enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   181
      in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   182
        concat (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   183
          str definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   184
          :: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   185
          :: str "="
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   186
          :: separate (str "|") (map print_co cos)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   187
        )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   188
      end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   189
    fun print_def is_pseudo_fun needs_typ definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   190
          (ML_Function (name, (vs_ty as (vs, ty), eq :: eqs))) =
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   191
          let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   192
            fun print_eqn definer ((ts, t), (some_thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   193
              let
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   194
                val consts = fold Code_Thingol.add_constnames (t :: ts) [];
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   195
                val vars = reserved
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   196
                  |> intro_base_names
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   197
                       (is_none o syntax_const) deresolve consts
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   198
                  |> intro_vars ((fold o Code_Thingol.fold_varnames)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   199
                       (insert (op =)) ts []);
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   200
                val prolog = if needs_typ then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   201
                  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
   202
                    else (concat o map str) [definer, deresolve name];
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   203
              in
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   204
                concat (
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   205
                  prolog
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   206
                  :: (if is_pseudo_fun name then [str "()"]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   207
                      else print_dict_args vs
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   208
                        @ 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
   209
                  @ str "="
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   210
                  @@ print_term is_pseudo_fun some_thm vars NOBR t
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   211
                )
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   212
              end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   213
            val shift = if null eqs then I else
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   214
              map (Pretty.block o single o Pretty.block o single);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   215
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   216
            (print_val_decl print_typscheme (name, vs_ty))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   217
            ((Pretty.block o Pretty.fbreaks o shift) (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   218
              print_eqn definer eq
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   219
              :: map (print_eqn "|") eqs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   220
            ))
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   221
          end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   222
      | print_def is_pseudo_fun _ definer
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   223
          (ML_Instance (inst, ((class, (tyco, vs)), (super_instances, (classparam_instances, _))))) =
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   224
          let
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   225
            fun print_super_instance (_, (classrel, dss)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   226
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   227
                (str o Long_Name.base_name o deresolve) classrel,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   228
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   229
                print_dict is_pseudo_fun NOBR (DictConst dss)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   230
              ];
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   231
            fun print_classparam_instance ((classparam, const), (thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   232
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   233
                (str o Long_Name.base_name o deresolve) classparam,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   234
                str "=",
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   235
                print_app (K false) (SOME thm) reserved NOBR (const, [])
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   236
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   237
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   238
            (print_val_decl print_dicttypscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   239
              (inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   240
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   241
              str definer
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   242
              :: (str o deresolve) inst
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   243
              :: (if is_pseudo_fun inst then [str "()"]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   244
                  else print_dict_args vs)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   245
              @ str "="
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   246
              :: enum "," "{" "}"
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   247
                (map print_super_instance super_instances
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   248
                  @ map print_classparam_instance classparam_instances)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   249
              :: str ":"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   250
              @@ print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   251
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   252
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   253
    fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   254
          [print_val_decl print_typscheme (name, vs_ty)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   255
          ((semicolon o map str) (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   256
            (if n = 0 then "val" else "fun")
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   257
            :: deresolve name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   258
            :: replicate n "_"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   259
            @ "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   260
            :: "raise"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   261
            :: "Fail"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   262
            @@ (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
   263
          ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   264
      | print_stmt (ML_Val binding) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   265
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   266
            val (sig_p, p) = print_def (K false) true "val" binding
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   267
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   268
            [sig_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   269
            (semicolon [p])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   270
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   271
      | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   272
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   273
            val print_def' = print_def (member (op =) pseudo_funs) false;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   274
            fun print_pseudo_fun name = concat [
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   275
                str "val",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   276
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   277
                str "=",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   278
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   279
                str "();"
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   280
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   281
            val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   282
              (print_def' "fun" binding :: map (print_def' "and") bindings);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   283
            val pseudo_ps = map print_pseudo_fun pseudo_funs;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   284
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   285
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   286
            (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   287
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   288
     | print_stmt (ML_Datas [(tyco, (vs, []))]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   289
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   290
            val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   291
          in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   292
            pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   293
            [concat [str "type", ty_p]]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   294
            (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   295
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   296
     | print_stmt (ML_Datas (data :: datas)) = 
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   297
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   298
            val sig_ps = print_datatype_decl "datatype" data
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   299
              :: map (print_datatype_decl "and") datas;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   300
            val (ps, p) = split_last sig_ps;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   301
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   302
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   303
            (Pretty.chunks (ps @| semicolon [p]))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   304
          end
37447
ad3e04f289b6 transitive superclasses were also only a misunderstanding
haftmann
parents: 37446
diff changeset
   305
     | print_stmt (ML_Class (class, (v, (super_classes, classparams)))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   306
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   307
            fun print_field s p = concat [str s, str ":", p];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   308
            fun print_proj s p = semicolon
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   309
              (map str ["val", s, "=", "#" ^ s, ":"] @| p);
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   310
            fun print_super_class_decl (super_class, classrel) =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   311
              print_val_decl print_dicttypscheme
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   312
                (classrel, ([(v, [class])], (super_class, ITyVar v)));
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   313
            fun print_super_class_field (super_class, classrel) =
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   314
              print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v));
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   315
            fun print_super_class_proj (super_class, classrel) =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   316
              print_proj (deresolve classrel)
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   317
                (print_dicttypscheme ([(v, [class])], (super_class, ITyVar v)));
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   318
            fun print_classparam_decl (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   319
              print_val_decl print_typscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   320
                (classparam, ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   321
            fun print_classparam_field (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   322
              print_field (deresolve classparam) (print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   323
            fun print_classparam_proj (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   324
              print_proj (deresolve classparam)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   325
                (print_typscheme ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   326
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   327
            (concat [str "type", print_dicttyp (class, ITyVar v)]
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   328
              :: map print_super_class_decl super_classes
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   329
              @ map print_classparam_decl classparams)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   330
            (Pretty.chunks (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   331
              concat [
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   332
                str ("type '" ^ v),
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   333
                (str o deresolve) class,
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   334
                str "=",
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   335
                enum "," "{" "};" (
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   336
                  map print_super_class_field super_classes
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   337
                  @ map print_classparam_field classparams
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   338
                )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   339
              ]
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   340
              :: map print_super_class_proj super_classes
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   341
              @ map print_classparam_proj classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   342
            ))
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   343
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   344
  in print_stmt end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   345
34032
f13b5c023e65 simplified notion of empty module name
haftmann
parents: 34028
diff changeset
   346
fun print_sml_module name some_decls body = if name = ""
f13b5c023e65 simplified notion of empty module name
haftmann
parents: 34028
diff changeset
   347
  then Pretty.chunks2 body
f13b5c023e65 simplified notion of empty module name
haftmann
parents: 34028
diff changeset
   348
  else Pretty.chunks2 (
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   349
    Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   350
      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
   351
      :: (the_list o Option.map (indent 2 o Pretty.chunks)) some_decls
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   352
      @| (if is_some some_decls then str "end = struct" else str "struct")
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   353
    )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   354
    :: body
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   355
    @| str ("end; (*struct " ^ name ^ "*)")
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   356
  );
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   357
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   358
val literals_sml = Literals {
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   359
  literal_char = prefix "#" o quote o ML_Syntax.print_char,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   360
  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
   361
  literal_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)",
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   362
  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
   363
  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
   364
  literal_naive_numeral = string_of_int,
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   365
  literal_list = enum "," "[" "]",
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   366
  infix_cons = (7, "::")
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   367
};
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   368
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   369
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   370
(** OCaml serializer **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   371
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   372
fun print_ocaml_stmt labelled_name syntax_tyco syntax_const reserved is_cons deresolve =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   373
  let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   374
    fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   375
      | print_tyco_expr fxy (tyco, [ty]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   376
          concat [print_typ BR ty, (str o deresolve) tyco]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   377
      | print_tyco_expr fxy (tyco, tys) =
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   378
          concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   379
    and print_typ fxy (tyco `%% tys) = (case syntax_tyco tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   380
         of NONE => print_tyco_expr fxy (tyco, tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   381
          | SOME (i, print) => print print_typ fxy tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   382
      | print_typ fxy (ITyVar v) = str ("'" ^ v);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   383
    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
   384
    fun print_typscheme_prefix (vs, p) = enum " ->" "" ""
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   385
      (map_filter (fn (v, sort) =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   386
        (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   387
    fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   388
    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
   389
    fun print_dict is_pseudo_fun fxy (DictConst (inst, dss)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   390
          brackify fxy ((str o deresolve) inst ::
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   391
            (if is_pseudo_fun inst then [str "()"]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   392
            else map_filter (print_dicts is_pseudo_fun BR) dss))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   393
      | print_dict is_pseudo_fun fxy (DictVar (classrels, (v, (i, k)))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   394
          str (if k = 1 then "_" ^ first_upper v
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   395
            else "_" ^ first_upper v ^ string_of_int (i+1))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   396
          |> fold_rev (fn classrel => fn p =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   397
               Pretty.block [p, str ".", (str o deresolve) classrel]) classrels
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   398
    and print_dicts is_pseudo_fun = print_tuple (print_dict is_pseudo_fun);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   399
    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
   400
      (map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort));
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   401
    fun print_term is_pseudo_fun some_thm vars fxy (IConst c) =
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   402
          print_app is_pseudo_fun some_thm vars fxy (c, [])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   403
      | print_term is_pseudo_fun some_thm vars fxy (IVar NONE) =
31889
fb2c8a687529 all variable names are optional
haftmann
parents: 31874
diff changeset
   404
          str "_"
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   405
      | 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
   406
          str (lookup_var vars v)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   407
      | 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
   408
          (case Code_Thingol.unfold_const_app t
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   409
           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
   410
            | NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1,
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   411
                print_term is_pseudo_fun some_thm vars BR t2])
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   412
      | print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   413
          let
31874
f172346ba805 simplified binding concept
haftmann
parents: 31775
diff changeset
   414
            val (binds, t') = Code_Thingol.unfold_pat_abs t;
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   415
            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
   416
          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
   417
      | 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
   418
          (case Code_Thingol.unfold_const_app t0
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   419
           of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   420
                then print_case is_pseudo_fun some_thm vars fxy cases
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   421
                else print_app is_pseudo_fun some_thm vars fxy c_ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   422
            | NONE => print_case is_pseudo_fun some_thm vars fxy cases)
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   423
    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
   424
      if is_cons c then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   425
        let val k = length tys in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   426
          if length ts = k
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   427
          then (str o deresolve) c
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   428
            :: the_list (print_tuple (print_term is_pseudo_fun some_thm vars) BR ts)
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   429
          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
   430
        end
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   431
      else if is_pseudo_fun c
29175
27cd8cce605e tackling simultaneous val/fun bindings
haftmann
parents: 28971
diff changeset
   432
        then (str o deresolve) c @@ str "()"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   433
      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
   434
        @ map (print_term is_pseudo_fun some_thm vars BR) ts
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   435
    and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun)
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   436
      (print_term is_pseudo_fun) syntax_const some_thm vars
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   437
    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
   438
    and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   439
          let
29952
9aed85067721 unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents: 29264
diff changeset
   440
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   441
            fun print_let ((pat, ty), t) vars =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   442
              vars
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   443
              |> print_bind is_pseudo_fun some_thm NOBR pat
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   444
              |>> (fn p => concat
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   445
                  [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
   446
            val (ps, vars') = fold_map print_let binds vars;
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   447
          in
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   448
            brackify_block fxy (Pretty.chunks ps) []
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   449
              (print_term is_pseudo_fun some_thm vars' NOBR body)
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   450
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   451
      | 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
   452
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   453
            fun print_select delim (pat, body) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   454
              let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   455
                val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars;
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   456
              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
   457
          in
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   458
            brackets (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   459
              str "match"
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   460
              :: print_term is_pseudo_fun some_thm vars NOBR t
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   461
              :: print_select "with" clause
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   462
              :: map (print_select "|") clauses
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   463
            )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   464
          end
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   465
      | print_case is_pseudo_fun some_thm vars fxy ((_, []), _) =
31121
f79a0d03b75f tuned exception code
haftmann
parents: 31063
diff changeset
   466
          (concat o map str) ["failwith", "\"empty case\""];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   467
    fun print_val_decl print_typscheme (name, typscheme) = concat
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   468
      [str "val", str (deresolve name), str ":", print_typscheme typscheme];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   469
    fun print_datatype_decl definer (tyco, (vs, cos)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   470
      let
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   471
        fun print_co ((co, _), []) = str (deresolve co)
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   472
          | 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
   473
              enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   474
      in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   475
        concat (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   476
          str definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   477
          :: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   478
          :: str "="
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   479
          :: separate (str "|") (map print_co cos)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   480
        )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   481
      end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   482
    fun print_def is_pseudo_fun needs_typ definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   483
          (ML_Function (name, (vs_ty as (vs, ty), eqs))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   484
          let
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   485
            fun print_eqn ((ts, t), (some_thm, _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   486
              let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   487
                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
   488
                val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   489
                  |> intro_vars ((fold o Code_Thingol.fold_varnames)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   490
                      (insert (op =)) ts []);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   491
              in concat [
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   492
                (Pretty.block o commas)
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   493
                  (map (print_term is_pseudo_fun some_thm vars NOBR) ts),
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   494
                str "->",
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   495
                print_term is_pseudo_fun some_thm vars NOBR t
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   496
              ] end;
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   497
            fun print_eqns is_pseudo [((ts, t), (some_thm, _))] =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   498
                  let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   499
                    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
   500
                    val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   501
                      |> intro_base_names
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   502
                          (is_none o syntax_const) deresolve consts
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   503
                      |> intro_vars ((fold o Code_Thingol.fold_varnames)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   504
                          (insert (op =)) ts []);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   505
                  in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   506
                    concat (
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   507
                      (if is_pseudo then [str "()"]
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   508
                        else map (print_term is_pseudo_fun some_thm vars BR) ts)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   509
                      @ str "="
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   510
                      @@ print_term is_pseudo_fun some_thm vars NOBR t
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   511
                    )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   512
                  end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   513
              | print_eqns _ ((eq as (([_], _), _)) :: eqs) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   514
                  Pretty.block (
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   515
                    str "="
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   516
                    :: Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   517
                    :: str "function"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   518
                    :: Pretty.brk 1
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   519
                    :: print_eqn eq
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   520
                    :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   521
                          o single o print_eqn) eqs
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   522
                  )
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   523
              | print_eqns _ (eqs as eq :: eqs') =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   524
                  let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   525
                    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
   526
                    val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   527
                      |> intro_base_names
32926
342d89e5a808 tuned whitespace
haftmann
parents: 32924
diff changeset
   528
                          (is_none o syntax_const) deresolve consts;
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   529
                    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
   530
                  in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   531
                    Pretty.block (
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   532
                      Pretty.breaks dummy_parms
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   533
                      @ Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   534
                      :: str "="
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   535
                      :: Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   536
                      :: str "match"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   537
                      :: Pretty.brk 1
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   538
                      :: (Pretty.block o commas) dummy_parms
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   539
                      :: Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   540
                      :: str "with"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   541
                      :: Pretty.brk 1
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   542
                      :: print_eqn eq
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   543
                      :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   544
                           o single o print_eqn) eqs'
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   545
                    )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   546
                  end;
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   547
            val prolog = if needs_typ then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   548
              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
   549
                else (concat o map str) [definer, deresolve name];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   550
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   551
            (print_val_decl print_typscheme (name, vs_ty))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   552
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   553
              prolog
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   554
              :: print_dict_args vs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   555
              @| print_eqns (is_pseudo_fun name) eqs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   556
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   557
          end
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   558
      | print_def is_pseudo_fun _ definer
37449
034ebe92f090 more precise code
haftmann
parents: 37447
diff changeset
   559
            (ML_Instance (inst, ((class, (tyco, vs)), (super_instances, (classparam_instances, _))))) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   560
          let
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   561
            fun print_super_instance (_, (classrel, dss)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   562
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   563
                (str o deresolve) classrel,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   564
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   565
                print_dict is_pseudo_fun NOBR (DictConst dss)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   566
              ];
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   567
            fun print_classparam_instance ((classparam, const), (thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   568
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   569
                (str o deresolve) classparam,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   570
                str "=",
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   571
                print_app (K false) (SOME thm) reserved NOBR (const, [])
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   572
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   573
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   574
            (print_val_decl print_dicttypscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   575
              (inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   576
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   577
              str definer
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   578
              :: (str o deresolve) inst
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   579
              :: print_dict_args vs
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   580
              @ str "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   581
              @@ brackets [
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   582
                enum_default "()" ";" "{" "}" (map print_super_instance super_instances
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   583
                  @ map print_classparam_instance classparam_instances),
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   584
                str ":",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   585
                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
   586
              ]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   587
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   588
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   589
     fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   590
          [print_val_decl print_typscheme (name, vs_ty)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   591
          ((doublesemicolon o map str) (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   592
            "let"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   593
            :: deresolve name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   594
            :: replicate n "_"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   595
            @ "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   596
            :: "failwith"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   597
            @@ (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
   598
          ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   599
      | print_stmt (ML_Val binding) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   600
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   601
            val (sig_p, p) = print_def (K false) true "let" binding
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   602
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   603
            [sig_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   604
            (doublesemicolon [p])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   605
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   606
      | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   607
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   608
            val print_def' = print_def (member (op =) pseudo_funs) false;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   609
            fun print_pseudo_fun name = concat [
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   610
                str "let",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   611
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   612
                str "=",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   613
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   614
                str "();;"
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   615
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   616
            val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   617
              (print_def' "let rec" binding :: map (print_def' "and") bindings);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   618
            val pseudo_ps = map print_pseudo_fun pseudo_funs;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   619
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   620
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   621
            (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   622
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   623
     | print_stmt (ML_Datas [(tyco, (vs, []))]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   624
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   625
            val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   626
          in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   627
            pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   628
            [concat [str "type", ty_p]]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   629
            (concat [str "type", ty_p, str "=", str "EMPTY__"])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   630
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   631
     | print_stmt (ML_Datas (data :: datas)) = 
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   632
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   633
            val sig_ps = print_datatype_decl "type" data
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   634
              :: map (print_datatype_decl "and") datas;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   635
            val (ps, p) = split_last sig_ps;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   636
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   637
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   638
            (Pretty.chunks (ps @| doublesemicolon [p]))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   639
          end
37447
ad3e04f289b6 transitive superclasses were also only a misunderstanding
haftmann
parents: 37446
diff changeset
   640
     | print_stmt (ML_Class (class, (v, (super_classes, classparams)))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   641
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   642
            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
   643
            fun print_super_class_field (super_class, classrel) =
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   644
              print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v));
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   645
            fun print_classparam_decl (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   646
              print_val_decl print_typscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   647
                (classparam, ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   648
            fun print_classparam_field (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   649
              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
   650
            val w = "_" ^ first_upper v;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   651
            fun print_classparam_proj (classparam, _) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   652
              (concat o map str) ["let", deresolve classparam, w, "=",
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   653
                w ^ "." ^ deresolve classparam ^ ";;"];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   654
            val type_decl_p = concat [
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   655
                str ("type '" ^ v),
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   656
                (str o deresolve) class,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   657
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   658
                enum_default "unit" ";" "{" "}" (
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 37242
diff changeset
   659
                  map print_super_class_field super_classes
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   660
                  @ map print_classparam_field classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   661
                )
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   662
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   663
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   664
            (type_decl_p :: map print_classparam_decl classparams)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   665
            (Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   666
              doublesemicolon [type_decl_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   667
              :: map print_classparam_proj classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   668
            ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   669
          end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   670
  in print_stmt end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   671
34032
f13b5c023e65 simplified notion of empty module name
haftmann
parents: 34028
diff changeset
   672
fun print_ocaml_module name some_decls body = if name = ""
f13b5c023e65 simplified notion of empty module name
haftmann
parents: 34028
diff changeset
   673
  then Pretty.chunks2 body
f13b5c023e65 simplified notion of empty module name
haftmann
parents: 34028
diff changeset
   674
  else Pretty.chunks2 (
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   675
    Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   676
      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
   677
      :: (the_list o Option.map (indent 2 o Pretty.chunks)) some_decls
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   678
      @| (if is_some some_decls then str "end = struct" else str "struct")
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   679
    )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   680
    :: body
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   681
    @| str ("end;; (*struct " ^ name ^ "*)")
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   682
  );
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   683
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   684
val literals_ocaml = let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   685
  fun chr i =
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   686
    let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   687
      val xs = string_of_int i;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   688
      val ys = replicate_string (3 - length (explode xs)) "0";
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   689
    in "\\" ^ ys ^ xs end;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   690
  fun char_ocaml c =
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   691
    let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   692
      val i = ord c;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   693
      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
   694
        then chr i else c
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   695
    in s end;
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   696
  fun numeral_ocaml k = if k < 0
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   697
    then "(Big_int.minus_big_int " ^ numeral_ocaml (~ k) ^ ")"
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   698
    else if k <= 1073741823
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   699
      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
   700
      else "(Big_int.big_int_of_string " ^ quote (string_of_int k) ^ ")"
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   701
in Literals {
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   702
  literal_char = Library.enclose "'" "'" o char_ocaml,
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   703
  literal_string = quote o translate_string char_ocaml,
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   704
  literal_numeral = numeral_ocaml,
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   705
  literal_positive_numeral = numeral_ocaml,
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37822
diff changeset
   706
  literal_alternative_numeral = numeral_ocaml,
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
   707
  literal_naive_numeral = numeral_ocaml,
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34071
diff changeset
   708
  literal_list = enum ";" "[" "]",
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   709
  infix_cons = (6, "::")
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   710
} end;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   711
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   712
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   713
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   714
(** SML/OCaml generic part **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   715
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   716
local
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   717
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   718
datatype ml_node =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   719
    Dummy of string
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   720
  | Stmt of string * ml_stmt
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   721
  | Module of string * ((Name.context * Name.context) * ml_node Graph.T);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   722
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   723
in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   724
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   725
fun ml_node_of_program labelled_name module_name reserved raw_module_alias program =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   726
  let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   727
    val module_alias = if is_some module_name then K module_name else raw_module_alias;
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   728
    val reserved = Name.make_context reserved;
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   729
    val empty_module = ((reserved, reserved), Graph.empty);
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   730
    fun map_node [] f = f
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   731
      | map_node (m::ms) f =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   732
          Graph.default_node (m, Module (m, empty_module))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   733
          #> Graph.map_node m (fn (Module (module_name, (nsp, nodes))) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   734
               Module (module_name, (nsp, map_node ms f nodes)));
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   735
    fun map_nsp_yield [] f (nsp, nodes) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   736
          let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   737
            val (x, nsp') = f nsp
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   738
          in (x, (nsp', nodes)) end
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   739
      | map_nsp_yield (m::ms) f (nsp, nodes) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   740
          let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   741
            val (x, nodes') =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   742
              nodes
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   743
              |> Graph.default_node (m, Module (m, empty_module))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   744
              |> Graph.map_node_yield m (fn Module (d_module_name, nsp_nodes) => 
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   745
                  let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   746
                    val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   747
                  in (x, Module (d_module_name, nsp_nodes')) end)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   748
          in (x, (nsp, nodes')) end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   749
    fun map_nsp_fun_yield f (nsp_fun, nsp_typ) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   750
      let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   751
        val (x, nsp_fun') = f nsp_fun
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   752
      in (x, (nsp_fun', nsp_typ)) end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   753
    fun map_nsp_typ_yield f (nsp_fun, nsp_typ) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   754
      let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   755
        val (x, nsp_typ') = f nsp_typ
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   756
      in (x, (nsp_fun, nsp_typ')) end;
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   757
    val mk_name_module = mk_name_module reserved NONE module_alias program;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   758
    fun mk_name_stmt upper name nsp =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   759
      let
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   760
        val (_, base) = dest_name name;
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   761
        val base' = if upper then first_upper base else base;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   762
        val ([base''], nsp') = Name.variants [base'] nsp;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   763
      in (base'', nsp') end;
37439
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   764
    fun deps_of names =
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   765
      []
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   766
      |> fold (fold (insert (op =)) o Graph.imm_succs program) names
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   767
      |> subtract (op =) names
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   768
      |> filter_out (Code_Thingol.is_case o Graph.get_node program);
37437
4202e11ae7dc formal introduction of case cong
haftmann
parents: 37384
diff changeset
   769
    fun ml_binding_of_stmt (name, Code_Thingol.Fun (_, ((tysm as (vs, ty), raw_eqs), _))) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   770
          let
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   771
            val eqs = filter (snd o snd) raw_eqs;
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   772
            val (eqs', is_value) = if null (filter_out (null o snd) vs) then case eqs
35228
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   773
               of [(([], t), some_thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty
ac2cab4583f4 context theorem is optional
haftmann
parents: 34944
diff changeset
   774
                  then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   775
                  else (eqs, SOME (name, member (op =) (Code_Thingol.add_constnames t []) name))
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   776
                | _ => (eqs, NONE)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   777
              else (eqs, NONE)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   778
          in (ML_Function (name, (tysm, eqs')), is_value) end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   779
      | ml_binding_of_stmt (name, Code_Thingol.Classinst (stmt as ((_, (_, vs)), _))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   780
          (ML_Instance (name, stmt), if forall (null o snd) vs then SOME (name, false) else NONE)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   781
      | ml_binding_of_stmt (name, _) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   782
          error ("Binding block containing illegal statement: " ^ labelled_name name)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   783
    fun add_fun (name, stmt) =
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   784
      let
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   785
        val (binding, some_value_name) = ml_binding_of_stmt (name, stmt);
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   786
        val ml_stmt = case binding
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   787
         of ML_Function (name, ((vs, ty), [])) =>
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   788
              ML_Exc (name, ((vs, ty),
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   789
                (length o filter_out (null o snd)) vs + (length o fst o Code_Thingol.unfold_fun) ty))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   790
          | _ => case some_value_name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   791
             of NONE => ML_Funs ([binding], [])
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   792
              | SOME (name, true) => ML_Funs ([binding], [name])
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   793
              | SOME (name, false) => ML_Val binding
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   794
      in
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   795
        map_nsp_fun_yield (mk_name_stmt false name)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   796
        #>> (fn name' => ([name'], ml_stmt))
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   797
      end;
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   798
    fun add_funs stmts =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   799
      let
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   800
        val ml_stmt = ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst);
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   801
      in
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   802
        fold_map (fn (name, _) => map_nsp_fun_yield (mk_name_stmt false name)) stmts
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   803
        #>> rpair ml_stmt
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   804
      end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   805
    fun add_datatypes stmts =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   806
      fold_map
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   807
        (fn (name, Code_Thingol.Datatype (_, stmt)) =>
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   808
              map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   809
          | (name, Code_Thingol.Datatypecons _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   810
              map_nsp_fun_yield (mk_name_stmt true name) #>> rpair NONE
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   811
          | (name, _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   812
              error ("Datatype block containing illegal statement: " ^ labelled_name name)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   813
        ) stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   814
      #>> (split_list #> apsnd (map_filter I
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   815
        #> (fn [] => error ("Datatype block without data statement: "
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   816
                  ^ (Library.commas o map (labelled_name o fst)) stmts)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   817
             | stmts => ML_Datas stmts)));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   818
    fun add_class stmts =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   819
      fold_map
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   820
        (fn (name, Code_Thingol.Class (_, stmt)) =>
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   821
              map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt))
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   822
          | (name, Code_Thingol.Classrel _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   823
              map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   824
          | (name, Code_Thingol.Classparam _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   825
              map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   826
          | (name, _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   827
              error ("Class block containing illegal statement: " ^ labelled_name name)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   828
        ) stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   829
      #>> (split_list #> apsnd (map_filter I
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   830
        #> (fn [] => error ("Class block without class statement: "
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   831
                  ^ (Library.commas o map (labelled_name o fst)) stmts)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   832
             | [stmt] => ML_Class stmt)));
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   833
    fun add_stmts ([stmt as (name, Code_Thingol.Fun _)]) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   834
          add_fun stmt
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   835
      | add_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   836
          add_funs stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   837
      | add_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   838
          add_datatypes stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   839
      | add_stmts ((stmts as (_, Code_Thingol.Datatype _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   840
          add_datatypes stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   841
      | add_stmts ((stmts as (_, Code_Thingol.Class _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   842
          add_class stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   843
      | add_stmts ((stmts as (_, Code_Thingol.Classrel _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   844
          add_class stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   845
      | add_stmts ((stmts as (_, Code_Thingol.Classparam _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   846
          add_class stmts
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   847
      | add_stmts ([stmt as (_, Code_Thingol.Classinst _)]) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   848
          add_fun stmt
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   849
      | add_stmts ((stmts as (_, Code_Thingol.Classinst _)::_)) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   850
          add_funs stmts
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   851
      | add_stmts stmts = error ("Illegal mutual dependencies: " ^
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   852
          (Library.commas o map (labelled_name o fst)) stmts);
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   853
    fun add_stmts' stmts nsp_nodes =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   854
      let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   855
        val names as (name :: names') = map fst stmts;
37439
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   856
        val deps = deps_of names;
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   857
        val (module_names, _) = (split_list o map dest_name) names;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   858
        val module_name = (the_single o distinct (op =) o map mk_name_module) module_names
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   859
          handle Empty =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   860
            error ("Different namespace prefixes for mutual dependencies:\n"
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   861
              ^ Library.commas (map labelled_name names)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   862
              ^ "\n"
38778
49b885736e8f private version of commas, cf. printmode
haftmann
parents: 38070
diff changeset
   863
              ^ Library.commas module_names);
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   864
        val module_name_path = Long_Name.explode module_name;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   865
        fun add_dep name name' =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   866
          let
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   867
            val module_name' = (mk_name_module o fst o dest_name) name';
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   868
          in if module_name = module_name' then
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   869
            map_node module_name_path (Graph.add_edge (name, name'))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   870
          else let
28705
c77a9f5672f8 slightly tuned
haftmann
parents: 28673
diff changeset
   871
            val (common, (diff1 :: _, diff2 :: _)) = chop_prefix (op =)
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   872
              (module_name_path, Long_Name.explode module_name');
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   873
          in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   874
            map_node common
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   875
              (fn node => Graph.add_edge_acyclic (diff1, diff2) node
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   876
                handle Graph.CYCLES _ => error ("Dependency "
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   877
                  ^ quote name ^ " -> " ^ quote name'
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   878
                  ^ " would result in module dependency cycle"))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   879
          end end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   880
      in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   881
        nsp_nodes
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   882
        |> map_nsp_yield module_name_path (add_stmts stmts)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   883
        |-> (fn (base' :: bases', stmt') =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   884
           apsnd (map_node module_name_path (Graph.new_node (name, (Stmt (base', stmt')))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   885
              #> fold2 (fn name' => fn base' =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   886
                   Graph.new_node (name', (Dummy base'))) names' bases')))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   887
        |> apsnd (fold (fn name => fold (add_dep name) deps) names)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   888
        |> apsnd (fold_product (curry (map_node module_name_path o Graph.add_edge)) names names)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   889
      end;
37439
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   890
    val stmts = map (AList.make (Graph.get_node program)) (rev (Graph.strong_conn program))
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   891
      |> filter_out (fn [(_, stmt)] => Code_Thingol.is_case stmt | _ => false);
c72a43a7d2c5 drop function definitions of combinators
haftmann
parents: 37437
diff changeset
   892
    val (_, nodes) = fold add_stmts' stmts empty_module;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   893
    fun deresolver prefix name = 
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   894
      let
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   895
        val module_name = (fst o dest_name) name;
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   896
        val module_name' = (Long_Name.explode o mk_name_module) module_name;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   897
        val (_, (_, remainder)) = chop_prefix (op =) (prefix, module_name');
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   898
        val stmt_name =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   899
          nodes
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   900
          |> fold (fn name => fn node => case Graph.get_node node name
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   901
              of Module (_, (_, node)) => node) module_name'
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   902
          |> (fn node => case Graph.get_node node name of Stmt (stmt_name, _) => stmt_name
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   903
               | Dummy stmt_name => stmt_name);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   904
      in
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   905
        Long_Name.implode (remainder @ [stmt_name])
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   906
      end handle Graph.UNDEF _ =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   907
        error ("Unknown statement name: " ^ labelled_name name);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   908
  in (deresolver, nodes) end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   909
37748
0af0d45257be dropped ancient in-place compilation of SML
haftmann
parents: 37745
diff changeset
   910
fun serialize_ml target print_module print_stmt raw_module_name with_signatures labelled_name
34071
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 34032
diff changeset
   911
  reserved includes raw_module_alias _ syntax_tyco syntax_const (code_of_pretty, code_writeln) program stmt_names destination =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   912
  let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   913
    val is_cons = Code_Thingol.is_cons program;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   914
    val presentation_stmt_names = Code_Target.stmt_names_of_destination destination;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   915
    val is_presentation = not (null presentation_stmt_names);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   916
    val module_name = if is_presentation then SOME "Code" else raw_module_name;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   917
    val (deresolver, nodes) = ml_node_of_program labelled_name module_name
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   918
      reserved raw_module_alias program;
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   919
    val reserved = make_vars reserved;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   920
    fun print_node prefix (Dummy _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   921
          NONE
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   922
      | print_node prefix (Stmt (_, stmt)) = if is_presentation andalso
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   923
          (null o filter (member (op =) presentation_stmt_names) o stmt_names_of) stmt
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   924
          then NONE
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   925
          else SOME (print_stmt labelled_name syntax_tyco syntax_const reserved is_cons (deresolver prefix) stmt)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   926
      | print_node prefix (Module (module_name, (_, nodes))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   927
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   928
            val (decls, body) = print_nodes (prefix @ [module_name]) nodes;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   929
            val p = if is_presentation then Pretty.chunks2 body
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   930
              else print_module module_name (if with_signatures then SOME decls else NONE) body;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   931
          in SOME ([], p) end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   932
    and print_nodes prefix nodes = (map_filter (print_node prefix o Graph.get_node nodes)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   933
        o rev o flat o Graph.strong_conn) nodes
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   934
      |> split_list
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   935
      |> (fn (decls, body) => (flat decls, body))
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   936
    val stmt_names' = (map o try)
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   937
      (deresolver (if is_some module_name then the_list module_name else [])) stmt_names;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   938
    val p = Pretty.chunks2 (map snd includes @ snd (print_nodes [] nodes));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   939
  in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   940
    Code_Target.mk_serialization target
34071
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 34032
diff changeset
   941
      (fn NONE => code_writeln | SOME file => File.write file o code_of_pretty)
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 34032
diff changeset
   942
      (rpair stmt_names' o code_of_pretty) p destination
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   943
  end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   944
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   945
end; (*local*)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   946
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   947
34028
1e6206763036 split off evaluation mechanisms in separte module Code_Eval
haftmann
parents: 33992
diff changeset
   948
(** for instrumentalization **)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   949
34032
f13b5c023e65 simplified notion of empty module name
haftmann
parents: 34028
diff changeset
   950
fun evaluation_code_of thy target struct_name =
37821
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   951
  Code_Target.serialize_custom thy (target, fn _ => fn [] =>
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   952
    serialize_ml target print_sml_module print_sml_stmt (SOME struct_name) true);
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   953
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   954
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   955
(** Isar setup **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   956
37821
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   957
fun isar_serializer_sml module_name =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   958
  Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   959
  >> (fn with_signatures => serialize_ml target_SML
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   960
      print_sml_module print_sml_stmt module_name with_signatures));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   961
37821
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   962
fun isar_serializer_ocaml module_name =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   963
  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
   964
  >> (fn with_signatures => serialize_ml target_OCaml
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   965
      print_ocaml_module print_ocaml_stmt module_name with_signatures));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   966
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   967
val setup =
37821
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   968
  Code_Target.add_target
37822
cf3588177676 use generic description slot for formal code checking
haftmann
parents: 37821
diff changeset
   969
    (target_SML, { serializer = isar_serializer_sml, literals = literals_sml,
cf3588177676 use generic description slot for formal code checking
haftmann
parents: 37821
diff changeset
   970
      check = { env_var = "ISABELLE_PROCESS", make_destination = fn p => Path.append p (Path.explode "ROOT.ML"),
cf3588177676 use generic description slot for formal code checking
haftmann
parents: 37821
diff changeset
   971
        make_command = fn isabelle => fn p => fn _ => isabelle ^ " -r -q -u Pure" } })
37821
3cbb22cec751 formal slot for code checker
haftmann
parents: 37819
diff changeset
   972
  #> Code_Target.add_target
37822
cf3588177676 use generic description slot for formal code checking
haftmann
parents: 37821
diff changeset
   973
    (target_OCaml, { serializer = isar_serializer_ocaml, literals = literals_ocaml,
cf3588177676 use generic description slot for formal code checking
haftmann
parents: 37821
diff changeset
   974
      check = { env_var = "EXEC_OCAML", make_destination = fn p => Path.append p (Path.explode "ROOT.ocaml"),
cf3588177676 use generic description slot for formal code checking
haftmann
parents: 37821
diff changeset
   975
        make_command = fn ocaml => fn p => fn _ => ocaml ^ " -w pu nums.cma " ^ File.shell_path p } })
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   976
  #> Code_Target.add_syntax_tyco 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
   977
      brackify_infix (1, R) fxy (
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   978
        print_typ (INFX (1, X)) ty1,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   979
        str "->",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   980
        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
   981
      )))
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   982
  #> Code_Target.add_syntax_tyco 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
   983
      brackify_infix (1, R) fxy (
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   984
        print_typ (INFX (1, X)) ty1,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   985
        str "->",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   986
        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
   987
      )))
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   988
  #> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   989
  #> fold (Code_Target.add_reserved target_SML)
38070
5beae0d6b7bc rebinding ref is illegal
haftmann
parents: 37958
diff changeset
   990
      ["ref" (*rebinding is illegal*), "o" (*dictionary projections use it already*),
5beae0d6b7bc rebinding ref is illegal
haftmann
parents: 37958
diff changeset
   991
        "Fail", "div", "mod" (*standard infixes*), "IntInf"]
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   992
  #> fold (Code_Target.add_reserved target_OCaml) [
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   993
      "and", "as", "assert", "begin", "class",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   994
      "constraint", "do", "done", "downto", "else", "end", "exception",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   995
      "external", "false", "for", "fun", "function", "functor", "if",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   996
      "in", "include", "inherit", "initializer", "lazy", "let", "match", "method",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   997
      "module", "mutable", "new", "object", "of", "open", "or", "private", "rec",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   998
      "sig", "struct", "then", "to", "true", "try", "type", "val",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   999
      "virtual", "when", "while", "with"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1000
    ]
34944
970e1466028d code literals: distinguish numeral classes by different entries
haftmann
parents: 34246
diff changeset
  1001
  #> fold (Code_Target.add_reserved target_OCaml) ["failwith", "mod", "Big_int"];
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1002
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1003
end; (*struct*)