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