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