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