src/Tools/Code/code_ml.ML
author haftmann
Fri, 04 Dec 2009 18:19:32 +0100
changeset 33992 bf22ff4f3d19
parent 33636 a9bb3f063773
child 34028 1e6206763036
permissions -rw-r--r--
signatures for generated code; tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
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
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32123
diff changeset
     9
  val eval: string option -> string * (unit -> 'a) option Unsynchronized.ref
30970
3fe2e418a071 generic postprocessing scheme for term evaluations
haftmann
parents: 30962
diff changeset
    10
    -> ((term -> term) -> 'a -> 'a) -> theory -> term -> string list -> 'a
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30941
diff changeset
    11
  val target_Eval: string
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    12
  val setup: theory -> theory
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    13
end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    14
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    15
structure Code_ML : CODE_ML =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    16
struct
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    17
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    18
open Basic_Code_Thingol;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    19
open Code_Printer;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    20
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    21
infixr 5 @@;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    22
infixr 5 @|;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    23
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    24
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    25
(** generic **)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    26
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    27
val target_SML = "SML";
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    28
val target_OCaml = "OCaml";
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30941
diff changeset
    29
val target_Eval = "Eval";
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    30
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    31
datatype ml_binding =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    32
    ML_Function of string * (typscheme * ((iterm list * iterm) * (thm * bool)) list)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    33
  | ML_Instance of string * ((class * (string * (vname * sort) list))
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    34
        * ((class * (string * (string * dict list list))) list
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28275
diff changeset
    35
      * ((string * const) * (thm * bool)) list));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    36
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    37
datatype ml_stmt =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    38
    ML_Exc of string * (typscheme * int)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    39
  | ML_Val of ml_binding
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    40
  | ML_Funs of ml_binding list * string list
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    41
  | ML_Datas of (string * ((vname * sort) list * (string * itype list) list)) list
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    42
  | ML_Class of string * (vname * ((class * string) list * (string * itype) list));
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    43
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    44
fun stmt_name_of_binding (ML_Function (name, _)) = name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    45
  | stmt_name_of_binding (ML_Instance (name, _)) = name;
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    46
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    47
fun stmt_names_of (ML_Exc (name, _)) = [name]
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    48
  | stmt_names_of (ML_Val binding) = [stmt_name_of_binding binding]
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    49
  | 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
    50
  | stmt_names_of (ML_Datas ds) = map fst ds
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
    51
  | stmt_names_of (ML_Class (name, _)) = [name];
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    52
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    53
fun print_product _ [] = NONE
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    54
  | print_product print [x] = SOME (print x)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    55
  | print_product print xs = (SOME o Pretty.enum " *" "" "") (map print xs);
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    56
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    57
fun print_tuple _ _ [] = NONE
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    58
  | print_tuple print fxy [x] = SOME (print fxy x)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    59
  | print_tuple print _ xs = SOME (Pretty.list "(" ")" (map (print NOBR) xs));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
    60
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    61
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    62
(** SML serializer **)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    63
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    64
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
    65
  let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    66
    fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    67
      | print_tyco_expr fxy (tyco, [ty]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    68
          concat [print_typ BR ty, (str o deresolve) tyco]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    69
      | print_tyco_expr fxy (tyco, tys) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    70
          concat [Pretty.list "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    71
    and print_typ fxy (tyco `%% tys) = (case syntax_tyco tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    72
         of NONE => print_tyco_expr fxy (tyco, tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    73
          | SOME (i, print) => print print_typ fxy tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    74
      | print_typ fxy (ITyVar v) = str ("'" ^ v);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    75
    fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    76
    fun print_typscheme_prefix (vs, p) = Pretty.enum " ->" "" ""
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    77
      (map_filter (fn (v, sort) =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    78
        (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    79
    fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    80
    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
    81
    fun print_dict is_pseudo_fun fxy (DictConst (inst, dss)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    82
          brackify fxy ((str o deresolve) inst ::
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    83
            (if is_pseudo_fun inst then [str "()"]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    84
            else map_filter (print_dicts is_pseudo_fun BR) dss))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    85
      | print_dict is_pseudo_fun fxy (DictVar (classrels, (v, (i, k)))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    86
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    87
            val v_p = str (if k = 1 then first_upper v ^ "_"
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    88
              else first_upper v ^ string_of_int (i+1) ^ "_");
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    89
          in case classrels
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    90
           of [] => v_p
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    91
            | [classrel] => brackets [(str o deresolve) classrel, v_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    92
            | classrels => brackets
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    93
                [Pretty.enum " o" "(" ")" (map (str o deresolve) classrels), v_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    94
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    95
    and print_dicts is_pseudo_fun = print_tuple (print_dict is_pseudo_fun);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    96
    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
    97
      (map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    98
    fun print_term is_pseudo_fun thm vars fxy (IConst c) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
    99
          print_app is_pseudo_fun thm vars fxy (c, [])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   100
      | print_term is_pseudo_fun thm vars fxy (IVar NONE) =
31889
fb2c8a687529 all variable names are optional
haftmann
parents: 31874
diff changeset
   101
          str "_"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   102
      | print_term is_pseudo_fun 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
   103
          str (lookup_var vars v)
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   104
      | print_term is_pseudo_fun thm vars fxy (t as t1 `$ t2) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   105
          (case Code_Thingol.unfold_const_app t
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   106
           of SOME c_ts => print_app is_pseudo_fun thm vars fxy c_ts
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   107
            | NONE => brackify fxy [print_term is_pseudo_fun thm vars NOBR t1,
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   108
                print_term is_pseudo_fun thm vars BR t2])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   109
      | print_term is_pseudo_fun thm vars fxy (t as _ `|=> _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   110
          let
31874
f172346ba805 simplified binding concept
haftmann
parents: 31775
diff changeset
   111
            val (binds, t') = Code_Thingol.unfold_pat_abs t;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   112
            fun print_abs (pat, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   113
              print_bind is_pseudo_fun thm NOBR pat
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   114
              #>> (fn p => concat [str "fn", p, str "=>"]);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   115
            val (ps, vars') = fold_map print_abs binds vars;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   116
          in brackets (ps @ [print_term is_pseudo_fun thm vars' NOBR t']) end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   117
      | print_term is_pseudo_fun thm vars fxy (ICase (cases as (_, t0))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   118
          (case Code_Thingol.unfold_const_app t0
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   119
           of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c)
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   120
                then print_case is_pseudo_fun thm vars fxy cases
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   121
                else print_app is_pseudo_fun thm vars fxy c_ts
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   122
            | NONE => print_case is_pseudo_fun thm vars fxy cases)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   123
    and print_app_expr is_pseudo_fun thm vars (app as ((c, ((_, iss), tys)), ts)) =
29175
27cd8cce605e tackling simultaneous val/fun bindings
haftmann
parents: 28971
diff changeset
   124
      if is_cons c then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   125
        let val k = length tys in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   126
          if k < 2 orelse length ts = k
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   127
          then (str o deresolve) c
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   128
            :: the_list (print_tuple (print_term is_pseudo_fun thm vars) BR ts)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   129
          else [print_term is_pseudo_fun thm vars BR (Code_Thingol.eta_expand k app)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   130
        end
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   131
      else if is_pseudo_fun c
29175
27cd8cce605e tackling simultaneous val/fun bindings
haftmann
parents: 28971
diff changeset
   132
        then (str o deresolve) c @@ str "()"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   133
      else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) iss
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   134
        @ map (print_term is_pseudo_fun thm vars BR) ts
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   135
    and print_app is_pseudo_fun thm vars = gen_print_app (print_app_expr is_pseudo_fun)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   136
      (print_term is_pseudo_fun) syntax_const thm vars
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   137
    and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   138
    and print_case is_pseudo_fun thm vars fxy (cases as ((_, [_]), _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   139
          let
29952
9aed85067721 unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents: 29264
diff changeset
   140
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   141
            fun print_match ((pat, ty), t) vars =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   142
              vars
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   143
              |> print_bind is_pseudo_fun thm NOBR pat
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   144
              |>> (fn p => semicolon [str "val", p, str "=",
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   145
                    print_term is_pseudo_fun thm vars NOBR t])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   146
            val (ps, vars') = fold_map print_match binds vars;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   147
          in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   148
            Pretty.chunks [
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   149
              Pretty.block [str ("let"), Pretty.fbrk, Pretty.chunks ps],
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   150
              Pretty.block [str ("in"), Pretty.fbrk, print_term is_pseudo_fun thm vars' NOBR body],
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   151
              str ("end")
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   152
            ]
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   153
          end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   154
      | print_case is_pseudo_fun thm vars fxy (((t, ty), clause :: clauses), _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   155
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   156
            fun print_select delim (pat, body) =
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
                val (p, vars') = print_bind is_pseudo_fun thm NOBR pat vars;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   159
              in
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   160
                concat [str delim, p, str "=>", print_term is_pseudo_fun thm vars' NOBR body]
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   161
              end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   162
          in
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   163
            brackets (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   164
              str "case"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   165
              :: print_term is_pseudo_fun thm vars NOBR t
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   166
              :: print_select "of" clause
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   167
              :: map (print_select "|") clauses
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   168
            )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   169
          end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   170
      | print_case is_pseudo_fun thm vars fxy ((_, []), _) =
31121
f79a0d03b75f tuned exception code
haftmann
parents: 31063
diff changeset
   171
          (concat o map str) ["raise", "Fail", "\"empty case\""];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   172
    fun print_val_decl print_typscheme (name, typscheme) = concat
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   173
      [str "val", str (deresolve name), str ":", print_typscheme typscheme];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   174
    fun print_datatype_decl definer (tyco, (vs, cos)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   175
      let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   176
        fun print_co (co, []) = str (deresolve co)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   177
          | print_co (co, tys) = concat [str (deresolve co), str "of",
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   178
              Pretty.enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   179
      in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   180
        concat (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   181
          str definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   182
          :: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   183
          :: str "="
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   184
          :: separate (str "|") (map print_co cos)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   185
        )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   186
      end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   187
    fun print_def is_pseudo_fun needs_typ definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   188
          (ML_Function (name, (vs_ty as (vs, ty), eq :: eqs))) =
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   189
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   190
            fun print_eqn definer ((ts, t), (thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   191
              let
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   192
                val consts = fold Code_Thingol.add_constnames (t :: ts) [];
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   193
                val vars = reserved
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   194
                  |> intro_base_names
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   195
                       (is_none o syntax_const) deresolve consts
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   196
                  |> intro_vars ((fold o Code_Thingol.fold_varnames)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   197
                       (insert (op =)) ts []);
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   198
                val prolog = if needs_typ then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   199
                  concat [str definer, (str o deresolve) name, str ":", print_typ NOBR ty]
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   200
                    else Pretty.strs [definer, deresolve name];
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   201
              in
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   202
                concat (
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   203
                  prolog
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   204
                  :: (if is_pseudo_fun name then [str "()"]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   205
                      else print_dict_args vs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   206
                        @ map (print_term is_pseudo_fun thm vars BR) ts)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   207
                  @ str "="
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   208
                  @@ print_term is_pseudo_fun thm vars NOBR t
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   209
                )
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   210
              end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   211
            val shift = if null eqs then I else
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   212
              map (Pretty.block o single o Pretty.block o single);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   213
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   214
            (print_val_decl print_typscheme (name, vs_ty))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   215
            ((Pretty.block o Pretty.fbreaks o shift) (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   216
              print_eqn definer eq
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   217
              :: map (print_eqn "|") eqs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   218
            ))
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   219
          end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   220
      | print_def is_pseudo_fun _ definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   221
          (ML_Instance (inst, ((class, (tyco, vs)), (superinsts, classparams)))) =
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   222
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   223
            fun print_superinst (_, (classrel, dss)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   224
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   225
                (str o Long_Name.base_name o deresolve) classrel,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   226
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   227
                print_dict is_pseudo_fun NOBR (DictConst dss)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   228
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   229
            fun print_classparam ((classparam, c_inst), (thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   230
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   231
                (str o Long_Name.base_name o deresolve) classparam,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   232
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   233
                print_app (K false) thm reserved NOBR (c_inst, [])
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   234
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   235
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   236
            (print_val_decl print_dicttypscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   237
              (inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   238
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   239
              str definer
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   240
              :: (str o deresolve) inst
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   241
              :: (if is_pseudo_fun inst then [str "()"]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   242
                  else print_dict_args vs)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   243
              @ str "="
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   244
              :: Pretty.list "{" "}"
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   245
                (map print_superinst superinsts @ map print_classparam classparams)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   246
              :: str ":"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   247
              @@ print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   248
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   249
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   250
    fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   251
          [print_val_decl print_typscheme (name, vs_ty)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   252
          ((semicolon o map str) (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   253
            (if n = 0 then "val" else "fun")
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   254
            :: deresolve name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   255
            :: replicate n "_"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   256
            @ "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   257
            :: "raise"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   258
            :: "Fail"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   259
            @@ (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
   260
          ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   261
      | print_stmt (ML_Val binding) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   262
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   263
            val (sig_p, p) = print_def (K false) true "val" binding
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   264
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   265
            [sig_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   266
            (semicolon [p])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   267
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   268
      | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   269
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   270
            val print_def' = print_def (member (op =) pseudo_funs) false;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   271
            fun print_pseudo_fun name = concat [
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   272
                str "val",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   273
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   274
                str "=",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   275
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   276
                str "();"
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   277
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   278
            val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   279
              (print_def' "fun" binding :: map (print_def' "and") bindings);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   280
            val pseudo_ps = map print_pseudo_fun pseudo_funs;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   281
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   282
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   283
            (Pretty.chunks (ps @ semicolon [p] :: pseudo_ps))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   284
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   285
     | print_stmt (ML_Datas [(tyco, (vs, []))]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   286
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   287
            val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   288
          in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   289
            pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   290
            [concat [str "type", ty_p]]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   291
            (concat [str "datatype", ty_p, str "=", str "EMPTY__"])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   292
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   293
     | print_stmt (ML_Datas (data :: datas)) = 
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   294
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   295
            val sig_ps = print_datatype_decl "datatype" data
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   296
              :: map (print_datatype_decl "and") datas;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   297
            val (ps, p) = split_last sig_ps;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   298
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   299
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   300
            (Pretty.chunks (ps @| semicolon [p]))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   301
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   302
     | print_stmt (ML_Class (class, (v, (superclasses, classparams)))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   303
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   304
            fun print_field s p = concat [str s, str ":", p];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   305
            fun print_proj s p = semicolon
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   306
              (map str ["val", s, "=", "#" ^ s, ":"] @| p);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   307
            fun print_superclass_decl (superclass, classrel) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   308
              print_val_decl print_dicttypscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   309
                (classrel, ([(v, [class])], (superclass, ITyVar v)));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   310
            fun print_superclass_field (superclass, classrel) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   311
              print_field (deresolve classrel) (print_dicttyp (superclass, ITyVar v));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   312
            fun print_superclass_proj (superclass, classrel) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   313
              print_proj (deresolve classrel)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   314
                (print_dicttypscheme ([(v, [class])], (superclass, ITyVar v)));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   315
            fun print_classparam_decl (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   316
              print_val_decl print_typscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   317
                (classparam, ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   318
            fun print_classparam_field (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   319
              print_field (deresolve classparam) (print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   320
            fun print_classparam_proj (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   321
              print_proj (deresolve classparam)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   322
                (print_typscheme ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   323
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   324
            (concat [str "type", print_dicttyp (class, ITyVar v)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   325
              :: map print_superclass_decl superclasses
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   326
              @ map print_classparam_decl classparams)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   327
            (Pretty.chunks (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   328
              concat [
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   329
                str ("type '" ^ v),
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   330
                (str o deresolve) class,
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   331
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   332
                Pretty.list "{" "};" (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   333
                  map print_superclass_field superclasses
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   334
                  @ map print_classparam_field classparams
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   335
                )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   336
              ]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   337
              :: map print_superclass_proj superclasses
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   338
              @ map print_classparam_proj classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   339
            ))
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   340
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   341
  in print_stmt end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   342
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   343
fun print_sml_module name some_decls body = Pretty.chunks2 (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   344
    Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   345
      str ("structure " ^ name ^ (if is_some some_decls then " : sig" else " ="))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   346
      :: (the_list o Option.map (Pretty.indent 2 o Pretty.chunks)) some_decls
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   347
      @| (if is_some some_decls then str "end = struct" else str "struct")
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   348
    )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   349
    :: body
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   350
    @| str ("end; (*struct " ^ name ^ "*)")
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   351
  );
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   352
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   353
val literals_sml = Literals {
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   354
  literal_char = prefix "#" o quote o ML_Syntax.print_char,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   355
  literal_string = quote o translate_string ML_Syntax.print_char,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   356
  literal_numeral = fn unbounded => fn k =>
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   357
    if unbounded then "(" ^ string_of_int k ^ " : IntInf.int)"
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   358
    else string_of_int k,
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   359
  literal_list = Pretty.list "[" "]",
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   360
  infix_cons = (7, "::")
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   361
};
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   362
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   363
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   364
(** OCaml serializer **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   365
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   366
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
   367
  let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   368
    fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   369
      | print_tyco_expr fxy (tyco, [ty]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   370
          concat [print_typ BR ty, (str o deresolve) tyco]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   371
      | print_tyco_expr fxy (tyco, tys) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   372
          concat [Pretty.list "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   373
    and print_typ fxy (tyco `%% tys) = (case syntax_tyco tyco
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   374
         of NONE => print_tyco_expr fxy (tyco, tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   375
          | SOME (i, print) => print print_typ fxy tys)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   376
      | print_typ fxy (ITyVar v) = str ("'" ^ v);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   377
    fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   378
    fun print_typscheme_prefix (vs, p) = Pretty.enum " ->" "" ""
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   379
      (map_filter (fn (v, sort) =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   380
        (print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   381
    fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   382
    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
   383
    fun print_dict is_pseudo_fun fxy (DictConst (inst, dss)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   384
          brackify fxy ((str o deresolve) inst ::
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   385
            (if is_pseudo_fun inst then [str "()"]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   386
            else map_filter (print_dicts is_pseudo_fun BR) dss))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   387
      | print_dict is_pseudo_fun fxy (DictVar (classrels, (v, (i, k)))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   388
          str (if k = 1 then "_" ^ first_upper v
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   389
            else "_" ^ first_upper v ^ string_of_int (i+1))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   390
          |> fold_rev (fn classrel => fn p =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   391
               Pretty.block [p, str ".", (str o deresolve) classrel]) classrels
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   392
    and print_dicts is_pseudo_fun = print_tuple (print_dict is_pseudo_fun);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   393
    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
   394
      (map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   395
    fun print_term is_pseudo_fun thm vars fxy (IConst c) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   396
          print_app is_pseudo_fun thm vars fxy (c, [])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   397
      | print_term is_pseudo_fun thm vars fxy (IVar NONE) =
31889
fb2c8a687529 all variable names are optional
haftmann
parents: 31874
diff changeset
   398
          str "_"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   399
      | print_term is_pseudo_fun 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
   400
          str (lookup_var vars v)
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   401
      | print_term is_pseudo_fun thm vars fxy (t as t1 `$ t2) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   402
          (case Code_Thingol.unfold_const_app t
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   403
           of SOME c_ts => print_app is_pseudo_fun thm vars fxy c_ts
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   404
            | NONE => brackify fxy [print_term is_pseudo_fun thm vars NOBR t1,
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   405
                print_term is_pseudo_fun thm vars BR t2])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   406
      | print_term is_pseudo_fun thm vars fxy (t as _ `|=> _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   407
          let
31874
f172346ba805 simplified binding concept
haftmann
parents: 31775
diff changeset
   408
            val (binds, t') = Code_Thingol.unfold_pat_abs t;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   409
            val (ps, vars') = fold_map (print_bind is_pseudo_fun thm BR o fst) binds vars;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   410
          in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun thm vars' NOBR t') end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   411
      | print_term is_pseudo_fun thm vars fxy (ICase (cases as (_, t0))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   412
          (case Code_Thingol.unfold_const_app t0
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   413
           of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c)
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   414
                then print_case is_pseudo_fun thm vars fxy cases
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   415
                else print_app is_pseudo_fun thm vars fxy c_ts
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   416
            | NONE => print_case is_pseudo_fun thm vars fxy cases)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   417
    and print_app_expr is_pseudo_fun thm vars (app as ((c, ((_, iss), tys)), ts)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   418
      if is_cons c then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   419
        let val k = length tys in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   420
          if length ts = k
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   421
          then (str o deresolve) c
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   422
            :: the_list (print_tuple (print_term is_pseudo_fun thm vars) BR ts)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   423
          else [print_term is_pseudo_fun thm vars BR (Code_Thingol.eta_expand k app)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   424
        end
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   425
      else if is_pseudo_fun c
29175
27cd8cce605e tackling simultaneous val/fun bindings
haftmann
parents: 28971
diff changeset
   426
        then (str o deresolve) c @@ str "()"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   427
      else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) iss
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   428
        @ map (print_term is_pseudo_fun thm vars BR) ts
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   429
    and print_app is_pseudo_fun thm vars = gen_print_app (print_app_expr is_pseudo_fun)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   430
      (print_term is_pseudo_fun) syntax_const thm vars
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   431
    and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   432
    and print_case is_pseudo_fun thm vars fxy (cases as ((_, [_]), _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   433
          let
29952
9aed85067721 unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents: 29264
diff changeset
   434
            val (binds, body) = Code_Thingol.unfold_let (ICase cases);
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   435
            fun print_let ((pat, ty), t) vars =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   436
              vars
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   437
              |> print_bind is_pseudo_fun thm NOBR pat
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   438
              |>> (fn p => concat
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   439
                  [str "let", p, str "=", print_term is_pseudo_fun thm vars NOBR t, str "in"])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   440
            val (ps, vars') = fold_map print_let binds vars;
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   441
          in
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   442
            brackify_block fxy (Pretty.chunks ps) []
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   443
              (print_term is_pseudo_fun thm vars' NOBR body)
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   444
          end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   445
      | print_case is_pseudo_fun thm vars fxy (((t, ty), clause :: clauses), _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   446
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   447
            fun print_select delim (pat, body) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   448
              let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   449
                val (p, vars') = print_bind is_pseudo_fun thm NOBR pat vars;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   450
              in concat [str delim, p, str "->", print_term is_pseudo_fun thm vars' NOBR body] end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   451
          in
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31598
diff changeset
   452
            brackets (
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   453
              str "match"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   454
              :: print_term is_pseudo_fun thm vars NOBR t
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   455
              :: print_select "with" clause
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   456
              :: map (print_select "|") clauses
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   457
            )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   458
          end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   459
      | print_case is_pseudo_fun thm vars fxy ((_, []), _) =
31121
f79a0d03b75f tuned exception code
haftmann
parents: 31063
diff changeset
   460
          (concat o map str) ["failwith", "\"empty case\""];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   461
    fun print_val_decl print_typscheme (name, typscheme) = concat
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   462
      [str "val", str (deresolve name), str ":", print_typscheme typscheme];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   463
    fun print_datatype_decl definer (tyco, (vs, cos)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   464
      let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   465
        fun print_co (co, []) = str (deresolve co)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   466
          | print_co (co, tys) = concat [str (deresolve co), str "of",
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   467
              Pretty.enum " *" "" "" (map (print_typ (INFX (2, X))) tys)];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   468
      in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   469
        concat (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   470
          str definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   471
          :: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   472
          :: str "="
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   473
          :: separate (str "|") (map print_co cos)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   474
        )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   475
      end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   476
    fun print_def is_pseudo_fun needs_typ definer
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   477
          (ML_Function (name, (vs_ty as (vs, ty), eqs))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   478
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   479
            fun print_eqn ((ts, t), (thm, _)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   480
              let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   481
                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
   482
                val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   483
                  |> intro_base_names
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   484
                      (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
   485
                  |> intro_vars ((fold o Code_Thingol.fold_varnames)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   486
                      (insert (op =)) ts []);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   487
              in concat [
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   488
                (Pretty.block o Pretty.commas)
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   489
                  (map (print_term is_pseudo_fun thm vars NOBR) ts),
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   490
                str "->",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   491
                print_term is_pseudo_fun thm vars NOBR t
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   492
              ] end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   493
            fun print_eqns is_pseudo [((ts, t), (thm, _))] =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   494
                  let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   495
                    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
   496
                    val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   497
                      |> intro_base_names
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   498
                          (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
   499
                      |> intro_vars ((fold o Code_Thingol.fold_varnames)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   500
                          (insert (op =)) ts []);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   501
                  in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   502
                    concat (
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   503
                      (if is_pseudo then [str "()"]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   504
                        else map (print_term is_pseudo_fun thm vars BR) ts)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   505
                      @ str "="
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   506
                      @@ print_term is_pseudo_fun thm vars NOBR t
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   507
                    )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   508
                  end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   509
              | print_eqns _ ((eq as (([_], _), _)) :: eqs) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   510
                  Pretty.block (
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   511
                    str "="
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   512
                    :: Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   513
                    :: str "function"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   514
                    :: Pretty.brk 1
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   515
                    :: print_eqn eq
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   516
                    :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   517
                          o single o print_eqn) eqs
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   518
                  )
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   519
              | print_eqns _ (eqs as eq :: eqs') =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   520
                  let
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   521
                    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
   522
                    val vars = reserved
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   523
                      |> intro_base_names
32926
342d89e5a808 tuned whitespace
haftmann
parents: 32924
diff changeset
   524
                          (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
   525
                    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
   526
                  in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   527
                    Pretty.block (
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   528
                      Pretty.breaks dummy_parms
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   529
                      @ Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   530
                      :: str "="
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   531
                      :: Pretty.brk 1
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   532
                      :: str "match"
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
                      :: (Pretty.block o Pretty.commas) dummy_parms
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 "with"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   537
                      :: Pretty.brk 1
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   538
                      :: print_eqn eq
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   539
                      :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   540
                           o single o print_eqn) eqs'
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   541
                    )
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   542
                  end;
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   543
            val prolog = if needs_typ then
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   544
              concat [str definer, (str o deresolve) name, str ":", print_typ NOBR ty]
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   545
                else Pretty.strs [definer, deresolve name];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   546
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   547
            (print_val_decl print_typscheme (name, vs_ty))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   548
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   549
              prolog
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   550
              :: print_dict_args vs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   551
              @| print_eqns (is_pseudo_fun name) eqs
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   552
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   553
          end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   554
      | print_def is_pseudo_fun _ definer (ML_Instance (inst, ((class, (tyco, vs)), (superinsts, classparams)))) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   555
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   556
            fun print_superinst (_, (classrel, dss)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   557
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   558
                (str o deresolve) classrel,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   559
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   560
                print_dict is_pseudo_fun NOBR (DictConst dss)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   561
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   562
            fun print_classparam ((classparam, c_inst), (thm, _)) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   563
              concat [
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   564
                (str o deresolve) classparam,
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   565
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   566
                print_app (K false) thm reserved NOBR (c_inst, [])
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   567
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   568
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   569
            (print_val_decl print_dicttypscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   570
              (inst, (vs, (class, tyco `%% map (ITyVar o fst) vs))))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   571
            (concat (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   572
              str definer
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   573
              :: (str o deresolve) inst
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   574
              :: print_dict_args vs
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   575
              @ str "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   576
              @@ brackets [
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   577
                enum_default "()" ";" "{" "}" (map print_superinst superinsts
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   578
                  @ map print_classparam classparams),
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   579
                str ":",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   580
                print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs])
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   581
              ]
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   582
            ))
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   583
          end;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   584
     fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   585
          [print_val_decl print_typscheme (name, vs_ty)]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   586
          ((doublesemicolon o map str) (
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   587
            "let"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   588
            :: deresolve name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   589
            :: replicate n "_"
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   590
            @ "="
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   591
            :: "failwith"
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   592
            @@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   593
          ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   594
      | print_stmt (ML_Val binding) =
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   595
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   596
            val (sig_p, p) = print_def (K false) true "let" binding
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   597
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   598
            [sig_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   599
            (doublesemicolon [p])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   600
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   601
      | print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   602
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   603
            val print_def' = print_def (member (op =) pseudo_funs) false;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   604
            fun print_pseudo_fun name = concat [
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   605
                str "let",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   606
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   607
                str "=",
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   608
                (str o deresolve) name,
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   609
                str "();;"
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   610
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   611
            val (sig_ps, (ps, p)) = (apsnd split_last o split_list)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   612
              (print_def' "let rec" binding :: map (print_def' "and") bindings);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   613
            val pseudo_ps = map print_pseudo_fun pseudo_funs;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   614
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   615
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   616
            (Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   617
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   618
     | print_stmt (ML_Datas [(tyco, (vs, []))]) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   619
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   620
            val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   621
          in
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   622
            pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   623
            [concat [str "type", ty_p]]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   624
            (concat [str "type", ty_p, str "=", str "EMPTY__"])
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   625
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   626
     | print_stmt (ML_Datas (data :: datas)) = 
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   627
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   628
            val sig_ps = print_datatype_decl "type" data
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   629
              :: map (print_datatype_decl "and") datas;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   630
            val (ps, p) = split_last sig_ps;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   631
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   632
            sig_ps
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   633
            (Pretty.chunks (ps @| doublesemicolon [p]))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   634
          end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   635
     | print_stmt (ML_Class (class, (v, (superclasses, classparams)))) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   636
          let
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   637
            fun print_field s p = concat [str s, str ":", p];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   638
            fun print_superclass_field (superclass, classrel) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   639
              print_field (deresolve classrel) (print_dicttyp (superclass, ITyVar v));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   640
            fun print_classparam_decl (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   641
              print_val_decl print_typscheme
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   642
                (classparam, ([(v, [class])], ty));
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   643
            fun print_classparam_field (classparam, ty) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   644
              print_field (deresolve classparam) (print_typ NOBR ty);
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   645
            val w = "_" ^ first_upper v;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   646
            fun print_classparam_proj (classparam, _) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   647
              (concat o map str) ["let", deresolve classparam, w, "=",
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   648
                w ^ "." ^ deresolve classparam ^ ";;"];
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   649
            val type_decl_p = concat [
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   650
                str ("type '" ^ v),
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   651
                (str o deresolve) class,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   652
                str "=",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   653
                enum_default "unit" ";" "{" "}" (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   654
                  map print_superclass_field superclasses
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   655
                  @ map print_classparam_field classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   656
                )
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   657
              ];
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   658
          in pair
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   659
            (type_decl_p :: map print_classparam_decl classparams)
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   660
            (Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   661
              doublesemicolon [type_decl_p]
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   662
              :: map print_classparam_proj classparams
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   663
            ))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   664
          end;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   665
  in print_stmt end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   666
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   667
fun print_ocaml_module name some_decls body = Pretty.chunks2 (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   668
    Pretty.chunks (
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   669
      str ("module " ^ name ^ (if is_some some_decls then " : sig" else " ="))
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   670
      :: (the_list o Option.map (Pretty.indent 2 o Pretty.chunks)) some_decls
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   671
      @| (if is_some some_decls then str "end = struct" else str "struct")
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   672
    )
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   673
    :: body
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   674
    @| str ("end;; (*struct " ^ name ^ "*)")
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   675
  );
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   676
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   677
val literals_ocaml = let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   678
  fun chr i =
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   679
    let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   680
      val xs = string_of_int i;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   681
      val ys = replicate_string (3 - length (explode xs)) "0";
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   682
    in "\\" ^ ys ^ xs end;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   683
  fun char_ocaml c =
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   684
    let
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   685
      val i = ord c;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   686
      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
   687
        then chr i else c
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   688
    in s end;
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31156
diff changeset
   689
  fun bignum_ocaml k = if k <= 1073741823
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31156
diff changeset
   690
    then "(Big_int.big_int_of_int " ^ string_of_int k ^ ")"
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31156
diff changeset
   691
    else "(Big_int.big_int_of_string " ^ quote (string_of_int k) ^ ")"
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   692
in Literals {
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   693
  literal_char = enclose "'" "'" o char_ocaml,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   694
  literal_string = quote o translate_string char_ocaml,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   695
  literal_numeral = fn unbounded => fn k => if k >= 0 then
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31156
diff changeset
   696
      if unbounded then bignum_ocaml k
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   697
      else string_of_int k
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   698
    else
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31156
diff changeset
   699
      if unbounded then "(Big_int.minus_big_int " ^ bignum_ocaml (~ k) ^ ")"
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   700
      else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   701
  literal_list = Pretty.enum ";" "[" "]",
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   702
  infix_cons = (6, "::")
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   703
} end;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   704
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
   705
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   706
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   707
(** SML/OCaml generic part **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   708
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   709
local
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   710
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   711
datatype ml_node =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   712
    Dummy of string
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   713
  | Stmt of string * ml_stmt
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   714
  | Module of string * ((Name.context * Name.context) * ml_node Graph.T);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   715
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   716
in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   717
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   718
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
   719
  let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   720
    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
   721
    val reserved = Name.make_context reserved;
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   722
    val empty_module = ((reserved, reserved), Graph.empty);
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   723
    fun map_node [] f = f
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   724
      | map_node (m::ms) f =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   725
          Graph.default_node (m, Module (m, empty_module))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   726
          #> Graph.map_node m (fn (Module (module_name, (nsp, nodes))) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   727
               Module (module_name, (nsp, map_node ms f nodes)));
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   728
    fun map_nsp_yield [] f (nsp, nodes) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   729
          let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   730
            val (x, nsp') = f nsp
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   731
          in (x, (nsp', nodes)) end
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   732
      | map_nsp_yield (m::ms) f (nsp, nodes) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   733
          let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   734
            val (x, nodes') =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   735
              nodes
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   736
              |> Graph.default_node (m, Module (m, empty_module))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   737
              |> Graph.map_node_yield m (fn Module (d_module_name, nsp_nodes) => 
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   738
                  let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   739
                    val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   740
                  in (x, Module (d_module_name, nsp_nodes')) end)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   741
          in (x, (nsp, nodes')) end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   742
    fun map_nsp_fun_yield f (nsp_fun, nsp_typ) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   743
      let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   744
        val (x, nsp_fun') = f nsp_fun
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   745
      in (x, (nsp_fun', nsp_typ)) end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   746
    fun map_nsp_typ_yield f (nsp_fun, nsp_typ) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   747
      let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   748
        val (x, nsp_typ') = f nsp_typ
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   749
      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
   750
    val mk_name_module = mk_name_module reserved NONE module_alias program;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   751
    fun mk_name_stmt upper name nsp =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   752
      let
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   753
        val (_, base) = dest_name name;
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   754
        val base' = if upper then first_upper base else base;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   755
        val ([base''], nsp') = Name.variants [base'] nsp;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   756
      in (base'', nsp') end;
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   757
    fun ml_binding_of_stmt (name, Code_Thingol.Fun (_, (tysm as (vs, ty), raw_eqs))) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   758
          let
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   759
            val eqs = filter (snd o snd) raw_eqs;
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   760
            val (eqs', is_value) = if null (filter_out (null o snd) vs) then case eqs
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   761
               of [(([], t), thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   762
                  then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), thm)], NONE)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   763
                  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
   764
                | _ => (eqs, NONE)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   765
              else (eqs, NONE)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   766
          in (ML_Function (name, (tysm, eqs')), is_value) end
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   767
      | ml_binding_of_stmt (name, Code_Thingol.Classinst (stmt as ((_, (_, vs)), _))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   768
          (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
   769
      | ml_binding_of_stmt (name, _) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   770
          error ("Binding block containing illegal statement: " ^ labelled_name name)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   771
    fun add_fun (name, stmt) =
29189
ee8572f3bb57 eliminated fun/val confusion
haftmann
parents: 29175
diff changeset
   772
      let
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   773
        val (binding, some_value_name) = ml_binding_of_stmt (name, stmt);
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   774
        val ml_stmt = case binding
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   775
         of ML_Function (name, ((vs, ty), [])) =>
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   776
              ML_Exc (name, ((vs, ty),
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   777
                (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
   778
          | _ => case some_value_name
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   779
             of NONE => ML_Funs ([binding], [])
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   780
              | SOME (name, true) => ML_Funs ([binding], [name])
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   781
              | SOME (name, false) => ML_Val binding
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   782
      in
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   783
        map_nsp_fun_yield (mk_name_stmt false name)
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   784
        #>> (fn name' => ([name'], ml_stmt))
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   785
      end;
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   786
    fun add_funs stmts =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   787
      let
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   788
        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
   789
      in
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   790
        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
   791
        #>> rpair ml_stmt
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   792
      end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   793
    fun add_datatypes stmts =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   794
      fold_map
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   795
        (fn (name, Code_Thingol.Datatype (_, stmt)) =>
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   796
              map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   797
          | (name, Code_Thingol.Datatypecons _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   798
              map_nsp_fun_yield (mk_name_stmt true name) #>> rpair NONE
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   799
          | (name, _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   800
              error ("Datatype block containing illegal statement: " ^ labelled_name name)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   801
        ) stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   802
      #>> (split_list #> apsnd (map_filter I
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   803
        #> (fn [] => error ("Datatype block without data statement: "
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   804
                  ^ (commas o map (labelled_name o fst)) stmts)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   805
             | stmts => ML_Datas stmts)));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   806
    fun add_class stmts =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   807
      fold_map
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   808
        (fn (name, Code_Thingol.Class (_, stmt)) =>
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   809
              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
   810
          | (name, Code_Thingol.Classrel _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   811
              map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   812
          | (name, Code_Thingol.Classparam _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   813
              map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   814
          | (name, _) =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   815
              error ("Class block containing illegal statement: " ^ labelled_name name)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   816
        ) stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   817
      #>> (split_list #> apsnd (map_filter I
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   818
        #> (fn [] => error ("Class block without class statement: "
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   819
                  ^ (commas o map (labelled_name o fst)) stmts)
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   820
             | [stmt] => ML_Class stmt)));
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   821
    fun add_stmts ([stmt as (name, Code_Thingol.Fun _)]) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   822
          add_fun stmt
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   823
      | add_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   824
          add_funs stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   825
      | add_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   826
          add_datatypes stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   827
      | add_stmts ((stmts as (_, Code_Thingol.Datatype _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   828
          add_datatypes stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   829
      | add_stmts ((stmts as (_, Code_Thingol.Class _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   830
          add_class stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   831
      | add_stmts ((stmts as (_, Code_Thingol.Classrel _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   832
          add_class stmts
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   833
      | add_stmts ((stmts as (_, Code_Thingol.Classparam _)::_)) =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   834
          add_class stmts
33636
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   835
      | add_stmts ([stmt as (_, Code_Thingol.Classinst _)]) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   836
          add_fun stmt
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   837
      | add_stmts ((stmts as (_, Code_Thingol.Classinst _)::_)) =
a9bb3f063773 accomplish mutual recursion between fun and inst
haftmann
parents: 33519
diff changeset
   838
          add_funs stmts
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   839
      | add_stmts stmts = error ("Illegal mutual dependencies: " ^
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   840
          (commas o map (labelled_name o fst)) stmts);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   841
    fun add_stmts' stmts nsp_nodes =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   842
      let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   843
        val names as (name :: names') = map fst stmts;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   844
        val deps =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   845
          []
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   846
          |> fold (fold (insert (op =)) o Graph.imm_succs program) names
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   847
          |> subtract (op =) names;
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   848
        val (module_names, _) = (split_list o map dest_name) names;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   849
        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
   850
          handle Empty =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   851
            error ("Different namespace prefixes for mutual dependencies:\n"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   852
              ^ commas (map labelled_name names)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   853
              ^ "\n"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   854
              ^ commas module_names);
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   855
        val module_name_path = Long_Name.explode module_name;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   856
        fun add_dep name name' =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   857
          let
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   858
            val module_name' = (mk_name_module o fst o dest_name) name';
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   859
          in if module_name = module_name' then
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   860
            map_node module_name_path (Graph.add_edge (name, name'))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   861
          else let
28705
c77a9f5672f8 slightly tuned
haftmann
parents: 28673
diff changeset
   862
            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
   863
              (module_name_path, Long_Name.explode module_name');
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   864
          in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   865
            map_node common
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   866
              (fn node => Graph.add_edge_acyclic (diff1, diff2) node
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   867
                handle Graph.CYCLES _ => error ("Dependency "
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   868
                  ^ quote name ^ " -> " ^ quote name'
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   869
                  ^ " would result in module dependency cycle"))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   870
          end end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   871
      in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   872
        nsp_nodes
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   873
        |> map_nsp_yield module_name_path (add_stmts stmts)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   874
        |-> (fn (base' :: bases', stmt') =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   875
           apsnd (map_node module_name_path (Graph.new_node (name, (Stmt (base', stmt')))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   876
              #> fold2 (fn name' => fn base' =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   877
                   Graph.new_node (name', (Dummy base'))) names' bases')))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   878
        |> apsnd (fold (fn name => fold (add_dep name) deps) names)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   879
        |> 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
   880
      end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   881
    val (_, nodes) = empty_module
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   882
      |> fold add_stmts' (map (AList.make (Graph.get_node program))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   883
          (rev (Graph.strong_conn program)));
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   884
    fun deresolver prefix name = 
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   885
      let
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   886
        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
   887
        val module_name' = (Long_Name.explode o mk_name_module) module_name;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   888
        val (_, (_, remainder)) = chop_prefix (op =) (prefix, module_name');
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   889
        val stmt_name =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   890
          nodes
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   891
          |> fold (fn name => fn node => case Graph.get_node node name
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   892
              of Module (_, (_, node)) => node) module_name'
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   893
          |> (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
   894
               | Dummy stmt_name => stmt_name);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   895
      in
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   896
        Long_Name.implode (remainder @ [stmt_name])
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   897
      end handle Graph.UNDEF _ =>
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   898
        error ("Unknown statement name: " ^ labelled_name name);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   899
  in (deresolver, nodes) end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   900
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   901
fun serialize_ml target compile print_module print_stmt raw_module_name with_signatures labelled_name
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   902
  reserved includes raw_module_alias _ syntax_tyco syntax_const program stmt_names destination =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   903
  let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   904
    val is_cons = Code_Thingol.is_cons program;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   905
    val presentation_stmt_names = Code_Target.stmt_names_of_destination destination;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   906
    val is_presentation = not (null presentation_stmt_names);
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   907
    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
   908
    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
   909
      reserved raw_module_alias program;
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   910
    val reserved = make_vars reserved;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   911
    fun print_node prefix (Dummy _) =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   912
          NONE
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   913
      | print_node prefix (Stmt (_, stmt)) = if is_presentation andalso
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   914
          (null o filter (member (op =) presentation_stmt_names) o stmt_names_of) stmt
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   915
          then NONE
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   916
          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
   917
      | print_node prefix (Module (module_name, (_, nodes))) =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   918
          let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   919
            val (decls, body) = print_nodes (prefix @ [module_name]) nodes;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   920
            val p = if is_presentation then Pretty.chunks2 body
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   921
              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
   922
          in SOME ([], p) end
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   923
    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
   924
        o rev o flat o Graph.strong_conn) nodes
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   925
      |> split_list
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   926
      |> (fn (decls, body) => (flat decls, body))
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   927
    val stmt_names' = (map o try)
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   928
      (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
   929
    val p = Pretty.chunks2 (map snd includes @ snd (print_nodes [] nodes));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   930
  in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   931
    Code_Target.mk_serialization target
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   932
      (case compile of SOME compile => SOME (compile o Code_Target.code_of_pretty) | NONE => NONE)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   933
      (fn NONE => Code_Target.code_writeln | SOME file => File.write file o Code_Target.code_of_pretty)
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   934
      (rpair stmt_names' o Code_Target.code_of_pretty) p destination
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   935
  end;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   936
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   937
end; (*local*)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   938
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   939
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   940
(** ML (system language) code for evaluation and instrumentalization **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   941
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   942
val eval_struct_name = "Code"
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   943
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   944
fun eval_code_of some_target with_struct thy =
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   945
  let
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   946
    val target = the_default target_Eval some_target;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   947
    val serialize = if with_struct then fn _ => fn [] =>
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   948
        serialize_ml target_SML (SOME (K ())) print_sml_module print_sml_stmt (SOME eval_struct_name) true
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   949
      else fn _ => fn [] => 
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   950
        serialize_ml target_SML (SOME (K ())) ((K o K) Pretty.chunks2) print_sml_stmt (SOME eval_struct_name) true;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   951
  in Code_Target.serialize_custom thy (target, (serialize, literals_sml)) end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   952
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   953
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   954
(* evaluation *)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   955
30970
3fe2e418a071 generic postprocessing scheme for term evaluations
haftmann
parents: 30962
diff changeset
   956
fun eval some_target reff postproc thy t args =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   957
  let
28275
8dab53900e8c ML_Context.evaluate: proper context (for ML environment);
wenzelm
parents: 28064
diff changeset
   958
    val ctxt = ProofContext.init thy;
31063
88aaab83b6fc dropped explicit suppport for frees in evaluation conversion stack
haftmann
parents: 31054
diff changeset
   959
    fun evaluator naming program ((_, (_, ty)), t) deps =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   960
      let
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   961
        val _ = if Code_Thingol.contains_dictvar t then
28724
haftmann
parents: 28708
diff changeset
   962
          error "Term to be evaluated contains free dictionaries" else ();
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   963
        val value_name = "Value.VALUE.value"
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   964
        val program' = program
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   965
          |> Graph.new_node (value_name,
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   966
              Code_Thingol.Fun (Term.dummy_patternN, (([], ty), [(([], t), (Drule.dummy_thm, true))])))
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   967
          |> fold (curry Graph.add_edge value_name) deps;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   968
        val (value_code, [SOME value_name']) = eval_code_of some_target false thy naming program' [value_name];
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   969
        val sml_code = "let\n" ^ value_code ^ "\nin " ^ value_name'
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   970
          ^ space_implode " " (map (enclose "(" ")") args) ^ " end";
30672
beaadd5af500 more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents: 30648
diff changeset
   971
      in ML_Context.evaluate ctxt false reff sml_code end;
32123
8bac9ee4b28d integrated add_triv_classes into evaluation stack
haftmann
parents: 31934
diff changeset
   972
  in Code_Thingol.eval thy postproc evaluator t end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   973
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   974
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   975
(* instrumentalization by antiquotation *)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   976
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   977
local
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   978
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33038
diff changeset
   979
structure CodeAntiqData = Proof_Data
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   980
(
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   981
  type T = (string list * string list) * (bool * (string
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   982
    * (string * ((string * string) list * (string * string) list)) lazy));
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   983
  fun init _ = (([], []), (true, ("", Lazy.value ("", ([], [])))));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   984
);
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   985
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   986
val is_first_occ = fst o snd o CodeAntiqData.get;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   987
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   988
fun delayed_code thy tycos consts () =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
   989
  let
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28350
diff changeset
   990
    val (consts', (naming, program)) = Code_Thingol.consts_program thy consts;
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   991
    val tycos' = map (the o Code_Thingol.lookup_tyco naming) tycos;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
   992
    val (ml_code, target_names) = eval_code_of NONE true thy naming program (consts' @ tycos');
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   993
    val (consts'', tycos'') = chop (length consts') target_names;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   994
    val consts_map = map2 (fn const => fn NONE =>
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31121
diff changeset
   995
        error ("Constant " ^ (quote o Code.string_of_const thy) const
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   996
          ^ "\nhas a user-defined serialization")
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   997
      | SOME const'' => (const, const'')) consts consts''
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   998
    val tycos_map = map2 (fn tyco => fn NONE =>
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
   999
        error ("Type " ^ (quote o Sign.extern_type thy) tyco
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1000
          ^ "\nhas a user-defined serialization")
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1001
      | SOME tyco'' => (tyco, tyco'')) tycos tycos'';
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1002
  in (ml_code, (tycos_map, consts_map)) end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1003
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1004
fun register_code new_tycos new_consts ctxt =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1005
  let
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1006
    val ((tycos, consts), (_, (struct_name, _))) = CodeAntiqData.get ctxt;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1007
    val tycos' = fold (insert (op =)) new_tycos tycos;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1008
    val consts' = fold (insert (op =)) new_consts consts;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1009
    val (struct_name', ctxt') = if struct_name = ""
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1010
      then ML_Antiquote.variant eval_struct_name ctxt
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1011
      else (struct_name, ctxt);
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1012
    val acc_code = Lazy.lazy (delayed_code (ProofContext.theory_of ctxt) tycos' consts');
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1013
  in CodeAntiqData.put ((tycos', consts'), (false, (struct_name', acc_code))) ctxt' end;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1014
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1015
fun register_const const = register_code [] [const];
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1016
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1017
fun register_datatype tyco constrs = register_code [tyco] constrs;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1018
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1019
fun print_const const all_struct_name tycos_map consts_map =
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1020
  (Long_Name.append all_struct_name o the o AList.lookup (op =) consts_map) const;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1021
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1022
fun print_datatype tyco constrs all_struct_name tycos_map consts_map =
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1023
  let
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1024
    val upperize = implode o nth_map 0 Symbol.to_ascii_upper o explode;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1025
    fun check_base name name'' =
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1026
      if upperize (Long_Name.base_name name) = upperize name''
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1027
      then () else error ("Name as printed " ^ quote name''
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1028
        ^ "\ndiffers from logical base name " ^ quote (Long_Name.base_name name) ^ "; sorry.");
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1029
    val tyco'' = (the o AList.lookup (op =) tycos_map) tyco;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1030
    val constrs'' = map (the o AList.lookup (op =) consts_map) constrs;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1031
    val _ = check_base tyco tyco'';
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1032
    val _ = map2 check_base constrs constrs'';
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1033
  in "datatype " ^ tyco'' ^ " = datatype " ^ Long_Name.append all_struct_name tyco'' end;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1034
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1035
fun print_code struct_name is_first print_it ctxt =
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1036
  let
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1037
    val (_, (_, (struct_code_name, acc_code))) = CodeAntiqData.get ctxt;
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1038
    val (ml_code, (tycos_map, consts_map)) = Lazy.force acc_code;
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1039
    val ml_code = if is_first then ml_code
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1040
      else "";
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1041
    val all_struct_name = Long_Name.append struct_name struct_code_name;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1042
  in (ml_code, print_it all_struct_name tycos_map consts_map) end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1043
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1044
in
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1045
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1046
fun ml_code_antiq raw_const {struct_name, background} =
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1047
  let
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31121
diff changeset
  1048
    val const = Code.check_const (ProofContext.theory_of background) raw_const;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1049
    val is_first = is_first_occ background;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1050
    val background' = register_const const background;
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1051
  in (print_code struct_name is_first (print_const const), background') end;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1052
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1053
fun ml_code_datatype_antiq (raw_tyco, raw_constrs) {struct_name, background} =
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1054
  let
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1055
    val thy = ProofContext.theory_of background;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1056
    val tyco = Sign.intern_type thy raw_tyco;
31156
90fed3d4430f merged module code_unit.ML into code.ML
haftmann
parents: 31121
diff changeset
  1057
    val constrs = map (Code.check_const thy) raw_constrs;
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1058
    val constrs' = (map fst o snd o Code.get_datatype thy) tyco;
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 32926
diff changeset
  1059
    val _ = if eq_set (op =) (constrs, constrs') then ()
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1060
      else error ("Type " ^ quote tyco ^ ": given constructors diverge from real constructors")
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1061
    val is_first = is_first_occ background;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1062
    val background' = register_datatype tyco constrs background;
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1063
  in (print_code struct_name is_first (print_datatype tyco constrs), background') end;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1064
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1065
end; (*local*)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1066
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1067
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1068
(** Isar setup **)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1069
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1070
val _ = ML_Context.add_antiq "code" (fn _ => Args.term >> ml_code_antiq);
30962
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1071
val _ = ML_Context.add_antiq "code_datatype" (fn _ =>
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1072
  (Args.tyname --| Scan.lift (Args.$$$ "=")
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1073
    -- (Args.term ::: Scan.repeat (Scan.lift (Args.$$$ "|") |-- Args.term)))
f5fd07c558f9 code_datatype antiquotation; tuned
haftmann
parents: 30947
diff changeset
  1074
      >> ml_code_datatype_antiq);
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1075
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1076
fun isar_seri_sml module_name =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1077
  Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1078
  >> (fn with_signatures => serialize_ml target_SML
31327
ffa5356cc343 ML_Env;
wenzelm
parents: 31156
diff changeset
  1079
      (SOME (use_text ML_Env.local_context (1, "generated code") false))
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1080
      print_sml_module print_sml_stmt module_name with_signatures));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1081
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1082
fun isar_seri_ocaml module_name =
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1083
  Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1084
  >> (fn with_signatures => serialize_ml target_OCaml NONE
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1085
      print_ocaml_module print_ocaml_stmt module_name with_signatures));
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1086
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1087
val setup =
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
  1088
  Code_Target.add_target (target_SML, (isar_seri_sml, literals_sml))
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28054
diff changeset
  1089
  #> Code_Target.add_target (target_OCaml, (isar_seri_ocaml, literals_ocaml))
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30941
diff changeset
  1090
  #> Code_Target.extend_target (target_Eval, (target_SML, K I))
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1091
  #> Code_Target.add_syntax_tyco target_SML "fun" (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1092
      brackify_infix (1, R) fxy [
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1093
        print_typ (INFX (1, X)) ty1,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1094
        str "->",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1095
        print_typ (INFX (1, R)) ty2
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1096
      ]))
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1097
  #> Code_Target.add_syntax_tyco target_OCaml "fun" (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] =>
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1098
      brackify_infix (1, R) fxy [
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1099
        print_typ (INFX (1, X)) ty1,
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1100
        str "->",
33992
bf22ff4f3d19 signatures for generated code; tuned
haftmann
parents: 33636
diff changeset
  1101
        print_typ (INFX (1, R)) ty2
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1102
      ]))
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1103
  #> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1104
  #> fold (Code_Target.add_reserved target_SML)
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1105
      ["o" (*dictionary projections use it already*), "Fail", "div", "mod" (*standard infixes*)]
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1106
  #> fold (Code_Target.add_reserved target_OCaml) [
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1107
      "and", "as", "assert", "begin", "class",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1108
      "constraint", "do", "done", "downto", "else", "end", "exception",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1109
      "external", "false", "for", "fun", "function", "functor", "if",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1110
      "in", "include", "inherit", "initializer", "lazy", "let", "match", "method",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1111
      "module", "mutable", "new", "object", "of", "open", "or", "private", "rec",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1112
      "sig", "struct", "then", "to", "true", "try", "type", "val",
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1113
      "virtual", "when", "while", "with"
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1114
    ]
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1115
  #> fold (Code_Target.add_reserved target_OCaml) ["failwith", "mod"];
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1116
2b84d34c5d02 restructured and split code serializer module
haftmann
parents:
diff changeset
  1117
end; (*struct*)