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