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