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