src/Tools/code/code_target.ML
author haftmann
Fri, 12 Oct 2007 08:20:45 +0200
changeset 24992 d33713284207
parent 24928 3419943838f5
child 25084 30ce1e078b72
permissions -rw-r--r--
code_include replaces code_moduleprolog
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_target.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     5
Serializer from intermediate language ("Thin-gol")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     6
to target languages (like SML or Haskell).
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
signature CODE_TARGET =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    10
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    11
  include BASIC_CODE_THINGOL;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    12
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    13
  val add_syntax_class: string -> class
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
    14
    -> (string * (string * string) list) option -> theory -> theory;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    15
  val add_syntax_inst: string -> string * class -> bool -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    16
  val add_syntax_tycoP: string -> string -> OuterParse.token list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    17
    -> (theory -> theory) * OuterParse.token list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    18
  val add_syntax_constP: string -> string -> OuterParse.token list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    19
    -> (theory -> theory) * OuterParse.token list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
  val add_undefined: string -> string -> string -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
  val add_pretty_list: string -> string -> string -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
  val add_pretty_list_string: string -> string -> string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
    -> string -> string list -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
  val add_pretty_char: string -> string -> string list -> theory -> theory
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
    26
  val add_pretty_numeral: string -> bool -> string -> string -> string -> string
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
    -> string -> string -> theory -> theory;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
    28
  val add_pretty_message: string -> string -> string list -> string
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
    -> string -> string -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    30
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
    31
  val allow_exception: string -> theory -> theory;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
    32
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
  type serializer;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
  val add_serializer: string * serializer -> theory -> theory;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
    35
  val get_serializer: theory -> string -> bool -> string option
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
    36
    -> string option -> Args.T list
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    37
    -> string list option -> CodeThingol.code -> unit;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
  val assert_serializer: theory -> string -> string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
  val eval_verbose: bool ref;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    41
  val eval_invoke: theory -> (string * (unit -> 'a) option ref)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    42
    -> CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
  val code_width: int ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
  val setup: theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
structure CodeTarget : CODE_TARGET =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
open BasicCodeThingol;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
(** basics **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
infixr 5 @@;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
infixr 5 @|;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
fun x @@ y = [x, y];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
fun xs @| y = xs @ [y];
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
    59
val str = PrintMode.setmp [] Pretty.str;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
val concat = Pretty.block o Pretty.breaks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
fun semicolon ps = Pretty.block [concat ps, str ";"];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
(** syntax **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
datatype lrx = L | R | X;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
datatype fixity =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
    BR
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
  | NOBR
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
  | INFX of (int * lrx);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    74
val APP = INFX (~1, L);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
fun eval_lrx L L = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
  | eval_lrx R R = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
  | eval_lrx _ _ = true;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
fun eval_fxy NOBR NOBR = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
  | eval_fxy BR NOBR = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
  | eval_fxy NOBR BR = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
  | eval_fxy (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
      pr < pr_ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
      orelse pr = pr_ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
        andalso eval_lrx lr lr_ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
      orelse pr_ctxt = ~1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
  | eval_fxy _ (INFX _) = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
  | eval_fxy (INFX _) NOBR = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
  | eval_fxy _ _ = true;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
fun gen_brackify _ [p] = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
  | gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
  | gen_brackify false (ps as _::_) = Pretty.block ps;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
fun brackify fxy_ctxt ps =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
  gen_brackify (eval_fxy BR fxy_ctxt) (Pretty.breaks ps);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
fun brackify_infix infx fxy_ctxt ps =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
  gen_brackify (eval_fxy (INFX infx) fxy_ctxt) (Pretty.breaks ps);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
type class_syntax = string * (string -> string option);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
type typ_syntax = int * ((fixity -> itype -> Pretty.T)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
  -> fixity -> itype list -> Pretty.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
type term_syntax = int * ((CodeName.var_ctxt -> fixity -> iterm -> Pretty.T)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
  -> CodeName.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
(* user-defined syntax *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
datatype 'a mixfix =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
    Arg of fixity
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
  | Pretty of Pretty.T;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
fun mk_mixfix prep_arg (fixity_this, mfx) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   117
    fun is_arg (Arg _) = true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   118
      | is_arg _ = false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
    val i = (length o filter is_arg) mfx;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   120
    fun fillin _ [] [] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   121
          []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
      | fillin pr (Arg fxy :: mfx) (a :: args) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   123
          (pr fxy o prep_arg) a :: fillin pr mfx args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
      | fillin pr (Pretty p :: mfx) args =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
          p :: fillin pr mfx args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
      | fillin _ [] _ =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
          error ("Inconsistent mixfix: too many arguments")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
      | fillin _ _ [] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
          error ("Inconsistent mixfix: too less arguments");
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   130
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   131
    (i, fn pr => fn fixity_ctxt => fn args =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
      gen_brackify (eval_fxy fixity_this fixity_ctxt) (fillin pr mfx args))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
fun parse_infix prep_arg (x, i) s =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
    val l = case x of L => INFX (i, L) | _ => INFX (i, X);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
    val r = case x of R => INFX (i, R) | _ => INFX (i, X);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
  in
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   140
    mk_mixfix prep_arg (INFX (i, x),
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   141
      [Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   142
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   143
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   144
fun parse_mixfix prep_arg s =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
    val sym_any = Scan.one Symbol.is_regular;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
    val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
         ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
      || ($$ "_" >> K (Arg BR))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
      || ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   151
      || (Scan.repeat1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
           (   $$ "'" |-- sym_any
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
            || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
                 sym_any) >> (Pretty o str o implode)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
  in case Scan.finite Symbol.stopper parse (Symbol.explode s)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
   of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
    | ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   158
    | _ => Scan.!!
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   159
        (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail ()
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
fun parse_args f args =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
  case Scan.read Args.stopper f args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
   of SOME x => x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
    | NONE => error "Bad serializer arguments";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   167
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
(* generic serializer combinators *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
fun gen_pr_app pr_app' pr_term const_syntax labelled_name is_cons
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
      lhs vars fxy (app as ((c, (_, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
  case const_syntax c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
   of NONE => if lhs andalso not (is_cons c) then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
          error ("non-constructor on left hand side of equation: " ^ labelled_name c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
        else brackify fxy (pr_app' lhs vars app)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
    | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
        let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
          val k = if i < 0 then length tys else i;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
          fun pr' fxy ts = pr (pr_term lhs) vars fxy (ts ~~ curry Library.take k tys);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
        in if k = length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
          then pr' fxy ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
        else if k < length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
          then case chop k ts of (ts1, ts2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
            brackify fxy (pr' APP ts1 :: map (pr_term lhs vars BR) ts2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
          else pr_term lhs vars fxy (CodeThingol.eta_expand app k)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
        end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
fun gen_pr_bind pr_bind' pr_term fxy ((v, pat), ty) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
    val vs = case pat
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
     of SOME pat => CodeThingol.fold_varnames (insert (op =)) pat []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
      | NONE => [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
    val vars' = CodeName.intro_vars (the_list v) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
    val vars'' = CodeName.intro_vars vs vars';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
    val v' = Option.map (CodeName.lookup_var vars') v;
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   196
    val pat' = Option.map (pr_term true vars'' fxy) pat;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   197
  in (pr_bind' ((v', pat'), ty), vars'') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
(* list, char, string, numeral and monad abstract syntax transformations *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
fun implode_list c_nil c_cons t =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
    fun dest_cons (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
          if c = c_cons
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
          then SOME (t1, t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
      | dest_cons _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
    val (ts, t') = CodeThingol.unfoldr dest_cons t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
  in case t'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
   of IConst (c, _) => if c = c_nil then SOME ts else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   212
    | _ => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   213
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
        fun idx c = find_index (curry (op =) c) c_nibbles;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
        fun decode ~1 _ = NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
          | decode _ ~1 = NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
          | decode n m = SOME (chr (n * 16 + m));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
      in decode (idx c1) (idx c2) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
  | decode_char _ _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
fun implode_string c_char c_nibbles mk_char mk_string ts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   226
    fun implode_char (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
          if c = c_char then decode_char c_nibbles (t1, t2) else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
      | implode_char _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
    val ts' = map implode_char ts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   230
  in if forall is_some ts'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   231
    then (SOME o str o mk_string o implode o map_filter I) ts'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
    else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
fun implode_numeral c_bit0 c_bit1 c_pls c_min c_bit =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   236
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   237
    fun dest_bit (IConst (c, _)) = if c = c_bit0 then SOME 0
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   238
          else if c = c_bit1 then SOME 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   239
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   240
      | dest_bit _ = NONE;
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
   241
    fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
   242
          else if c = c_min then SOME ~1
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   243
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   244
      | dest_numeral (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
          if c = c_bit then case (dest_numeral t1, dest_bit t2)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
   246
           of (SOME n, SOME b) => SOME (2 * n + b)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   247
            | _ => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   248
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   249
      | dest_numeral _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   250
  in dest_numeral end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   251
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   252
fun implode_monad c_bind t =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   253
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   254
    fun dest_monad (IConst (c, _) `$ t1 `$ t2) =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   255
          if c = c_bind
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   256
            then case CodeThingol.split_abs t2
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   257
             of SOME (((v, pat), ty), t') =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   258
                  SOME ((SOME (((SOME v, pat), ty), true), t1), t')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
              | NONE => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
            else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   261
      | dest_monad t = case CodeThingol.split_let t
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   262
           of SOME (((pat, ty), tbind), t') =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   263
                SOME ((SOME (((NONE, SOME pat), ty), false), tbind), t')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   264
            | NONE => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
  in CodeThingol.unfoldr dest_monad t end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
(** name auxiliary **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
val dest_name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
  apfst NameSpace.implode o split_last o fst o split_last o NameSpace.explode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
fun mk_modl_name_tab init_names prefix module_alias code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
    fun nsp_map f = NameSpace.explode #> map f #> NameSpace.implode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   279
    fun mk_alias name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
     case module_alias name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
      of SOME name' => name'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
       | NONE => nsp_map (fn name => (the_single o fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
            (Name.variants [name] init_names)) name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   284
    fun mk_prefix name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
      case prefix
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
       of SOME prefix => NameSpace.append prefix name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
        | NONE => name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   288
    val tab =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
      Symtab.empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
      |> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   291
           o fst o dest_name o fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
             code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
  in fn name => (the o Symtab.lookup tab) name end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   296
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
(** SML/OCaml serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   299
datatype ml_def =
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   300
    MLFuns of (string * (typscheme * (iterm list * iterm) list)) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   301
  | MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
   302
  | MLClass of string * (vname * ((class * string) list * (string * itype) list))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
  | MLClassinst of string * ((class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
        * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   305
      * (string * const) list));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   307
fun pr_sml tyco_syntax const_syntax labelled_name init_syms deresolv is_cons ml_def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
  let
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   309
    val pr_label_classrel = translate_string (fn "." => "__" | c => c)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   310
      o NameSpace.qualifier;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   311
    val pr_label_classparam = NameSpace.base o NameSpace.qualifier;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
    fun pr_dicts fxy ds =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
        fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
          | pr_dictvar (v, (i, _)) = first_upper v ^ string_of_int (i+1) ^ "_";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   316
        fun pr_proj [] p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
              p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
          | pr_proj [p'] p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
              brackets [p', p]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
          | pr_proj (ps as _ :: _) p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   321
              brackets [Pretty.enum " o" "(" ")" ps, p];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
        fun pr_dictc fxy (DictConst (inst, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
              brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
          | pr_dictc fxy (DictVar (classrels, v)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
              pr_proj (map (str o deresolv) classrels) ((str o pr_dictvar) v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
      in case ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
       of [] => str "()"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
        | [d] => pr_dictc fxy d
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
        | _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
    fun pr_tyvars vs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
      vs
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   333
      |> map (fn (v, sort) => map_index (fn (i, _) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   334
           DictVar ([], (v, (i, length sort)))) sort)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   335
      |> map (pr_dicts BR);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   336
    fun pr_tycoexpr fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   337
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
        val tyco' = (str o deresolv) tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
      in case map (pr_typ BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   340
       of [] => tyco'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   341
        | [p] => Pretty.block [p, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
        | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   343
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
    and pr_typ fxy (tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   346
           of NONE => pr_tycoexpr fxy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   347
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   348
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   349
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   350
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   351
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   352
                else pr pr_typ fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   353
      | pr_typ fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   354
          str ("'" ^ v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   355
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   356
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   357
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   358
          str (CodeName.lookup_var vars v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   359
      | pr_term lhs vars fxy (t as t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   360
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   361
           of SOME c_ts => pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   362
            | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   363
                brackify fxy [pr_term lhs vars NOBR t1, pr_term lhs vars BR t2])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   364
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   365
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   366
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   367
            fun pr ((v, pat), ty) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   368
              pr_bind NOBR ((SOME v, pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   369
              #>> (fn p => concat [str "fn", p, str "=>"]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   370
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   371
          in brackets (ps @ [pr_term lhs vars' NOBR t']) end
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   372
      | pr_term lhs vars fxy (ICase (cases as (_, t0))) =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   373
          (case CodeThingol.unfold_const_app t0
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   374
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   375
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   376
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   377
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   378
    and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   379
      if is_cons c then let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   380
        val k = length tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   381
      in if k < 2 then 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   382
        (str o deresolv) c :: map (pr_term lhs vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   383
      else if k = length ts then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   384
        [(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term lhs vars NOBR) ts)]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   385
      else [pr_term lhs vars BR (CodeThingol.eta_expand app k)] end else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   386
        (str o deresolv) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   387
          :: (map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   388
    and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   389
      labelled_name is_cons lhs vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   390
    and pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   391
      | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   392
      | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   393
      | pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p]
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   394
    and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   395
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   396
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   397
            val (binds, t') = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   398
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   399
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   400
              |> pr_bind NOBR ((NONE, SOME pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   401
              |>> (fn p => semicolon [str "val", p, str "=", pr_term false vars NOBR t])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   402
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   403
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   404
            Pretty.chunks [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   405
              [str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   406
              [str ("in"), Pretty.fbrk, pr_term false vars' NOBR t'] |> Pretty.block,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   407
              str ("end")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   408
            ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   409
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   410
      | pr_case vars fxy (((td, ty), b::bs), _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   411
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   412
            fun pr delim (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   413
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   414
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   415
              in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   416
                concat [str delim, p, str "=>", pr_term false vars' NOBR t]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   417
              end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   418
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   419
            (Pretty.enclose "(" ")" o single o brackify fxy) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   420
              str "case"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   421
              :: pr_term false vars NOBR td
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   422
              :: pr "of" b
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   423
              :: map (pr "|") bs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   424
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   425
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   426
      | pr_case vars fxy ((_, []), _) = str "raise Fail \"empty case\""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   427
    fun pr_def (MLFuns (funns as (funn :: funns'))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   428
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   429
            val definer =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   430
              let
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   431
                fun no_args _ ((ts, _) :: _) = length ts
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   432
                  | no_args ty [] = (length o fst o CodeThingol.unfold_fun) ty;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   433
                fun mk 0 [] = "val"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   434
                  | mk 0 vs = if (null o filter_out (null o snd)) vs
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   435
                      then "val" else "fun"
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   436
                  | mk k _ = "fun";
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   437
                fun chk (_, ((vs, ty), eqs)) NONE = SOME (mk (no_args ty eqs) vs)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   438
                  | chk (_, ((vs, ty), eqs)) (SOME defi) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   439
                      if defi = mk (no_args ty eqs) vs then SOME defi
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   440
                      else error ("Mixing simultaneous vals and funs not implemented: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   441
                        ^ commas (map (labelled_name o fst) funns));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   442
              in the (fold chk funns NONE) end;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   443
            fun pr_funn definer (name, ((raw_vs, ty), [])) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   444
                  let
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   445
                    val vs = filter_out (null o snd) raw_vs;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   446
                    val n = length vs + (length o fst o CodeThingol.unfold_fun) ty;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   447
                    val exc_str =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   448
                      (ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   449
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   450
                    concat (
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   451
                      str definer
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   452
                      :: (str o deresolv) name
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   453
                      :: map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   454
                      @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   455
                      :: str "raise"
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   456
                      :: str "(Fail"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   457
                      :: str exc_str
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   458
                      @@ str ")"
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   459
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   460
                  end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   461
              | pr_funn definer (name, ((raw_vs, ty), eqs as eq :: eqs')) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   462
                  let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   463
                    val vs = filter_out (null o snd) raw_vs;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   464
                    val shift = if null eqs' then I else
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   465
                      map (Pretty.block o single o Pretty.block o single);
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   466
                    fun pr_eq definer (ts, t) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   467
                      let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   468
                        val consts = map_filter
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   469
                          (fn c => if (is_some o const_syntax) c
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   470
                            then NONE else (SOME o NameSpace.base o deresolv) c)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   471
                            ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   472
                        val vars = init_syms
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   473
                          |> CodeName.intro_vars consts
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   474
                          |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   475
                               (insert (op =)) ts []);
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   476
                      in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   477
                        concat (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   478
                          [str definer, (str o deresolv) name]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   479
                          @ (if null ts andalso null vs
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   480
                             then [str ":", pr_typ NOBR ty]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   481
                             else
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   482
                               pr_tyvars vs
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   483
                               @ map (pr_term true vars BR) ts)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   484
                       @ [str "=", pr_term false vars NOBR t]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   485
                        )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   486
                      end
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   487
                  in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   488
                    (Pretty.block o Pretty.fbreaks o shift) (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   489
                      pr_eq definer eq
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   490
                      :: map (pr_eq "|") eqs'
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   491
                    )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   492
                  end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   493
            val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   494
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   495
     | pr_def (MLDatas (datas as (data :: datas'))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   496
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   497
            fun pr_co (co, []) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   498
                  str (deresolv co)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   499
              | pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   500
                  concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   501
                    str (deresolv co),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   502
                    str "of",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   503
                    Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   504
                  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   505
            fun pr_data definer (tyco, (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   506
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   507
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   508
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   509
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   510
                    @@ str "EMPTY__" 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   511
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   512
              | pr_data definer (tyco, (vs, cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   513
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   514
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   515
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   516
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   517
                    :: separate (str "|") (map pr_co cos)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   518
                  );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   519
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   520
              (pr_data "datatype" data :: map (pr_data "and") datas');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   521
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   522
     | pr_def (MLClass (class, (v, (superclasses, classparams)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   523
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   524
            val w = first_upper v ^ "_";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   525
            fun pr_superclass_field (class, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   526
              (concat o map str) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   527
                pr_label_classrel classrel, ":", "'" ^ v, deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   528
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   529
            fun pr_classparam_field (classparam, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   530
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   531
                (str o pr_label_classparam) classparam, str ":", pr_typ NOBR ty
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   532
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   533
            fun pr_classparam_proj (classparam, _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   534
              semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   535
                str "fun",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   536
                (str o deresolv) classparam,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   537
                Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   538
                str "=",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   539
                str ("#" ^ pr_label_classparam classparam),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   540
                str w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   541
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   542
            fun pr_superclass_proj (_, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   543
              semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   544
                str "fun",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   545
                (str o deresolv) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   546
                Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   547
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   548
                str ("#" ^ pr_label_classrel classrel),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   549
                str w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   550
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   551
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   552
            Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   553
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   554
                str ("type '" ^ v),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   555
                (str o deresolv) class,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   556
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   557
                Pretty.enum "," "{" "};" (
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   558
                  map pr_superclass_field superclasses @ map pr_classparam_field classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   559
                )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   560
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   561
              :: map pr_superclass_proj superclasses
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   562
              @ map pr_classparam_proj classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   563
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   564
          end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   565
     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   566
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   567
            fun pr_superclass (_, (classrel, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   568
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   569
                (str o pr_label_classrel) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   570
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   571
                pr_dicts NOBR [DictConst dss]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   572
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   573
            fun pr_classparam (classparam, c_inst) =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   574
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   575
                (str o pr_label_classparam) classparam,
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   576
                str "=",
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   577
                pr_app false init_syms NOBR (c_inst, [])
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   578
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   579
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   580
            semicolon ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   581
              str (if null arity then "val" else "fun"),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   582
              (str o deresolv) inst ] @
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   583
              pr_tyvars arity @ [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   584
              str "=",
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   585
              Pretty.enum "," "{" "}"
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   586
                (map pr_superclass superarities @ map pr_classparam classparam_insts),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   587
              str ":",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   588
              pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   589
            ])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   590
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   591
  in pr_def ml_def end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   592
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   593
fun pr_sml_modl name content =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   594
  Pretty.chunks ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   595
    str ("structure " ^ name ^ " = "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   596
    str "struct",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   597
    str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   598
  ] @ content @ [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   599
    str "",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   600
    str ("end; (*struct " ^ name ^ "*)")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   601
  ]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   602
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   603
fun pr_ocaml tyco_syntax const_syntax labelled_name
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
   604
    init_syms deresolv is_cons ml_def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   605
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   606
    fun pr_dicts fxy ds =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   607
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   608
        fun pr_dictvar (v, (_, 1)) = "_" ^ first_upper v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   609
          | pr_dictvar (v, (i, _)) = "_" ^ first_upper v ^ string_of_int (i+1);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   610
        fun pr_proj ps p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   611
          fold_rev (fn p2 => fn p1 => Pretty.block [p1, str ".", str p2]) ps p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   612
        fun pr_dictc fxy (DictConst (inst, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   613
              brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   614
          | pr_dictc fxy (DictVar (classrels, v)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   615
              pr_proj (map deresolv classrels) ((str o pr_dictvar) v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   616
      in case ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   617
       of [] => str "()"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   618
        | [d] => pr_dictc fxy d
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   619
        | _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   620
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   621
    fun pr_tyvars vs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   622
      vs
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   623
      |> map (fn (v, sort) => map_index (fn (i, _) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   624
           DictVar ([], (v, (i, length sort)))) sort)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   625
      |> map (pr_dicts BR);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   626
    fun pr_tycoexpr fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   627
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   628
        val tyco' = (str o deresolv) tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   629
      in case map (pr_typ BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   630
       of [] => tyco'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   631
        | [p] => Pretty.block [p, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   632
        | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   633
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   634
    and pr_typ fxy (tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   635
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   636
           of NONE => pr_tycoexpr fxy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   637
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   638
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   639
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   640
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   641
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   642
                else pr pr_typ fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   643
      | pr_typ fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   644
          str ("'" ^ v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   645
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   646
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   647
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   648
          str (CodeName.lookup_var vars v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   649
      | pr_term lhs vars fxy (t as t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   650
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   651
           of SOME c_ts => pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   652
            | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   653
                brackify fxy [pr_term lhs vars NOBR t1, pr_term lhs vars BR t2])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   654
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   655
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   656
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   657
            fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   658
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   659
          in brackets (str "fun" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   660
      | pr_term lhs vars fxy (ICase (cases as (_, t0))) = (case CodeThingol.unfold_const_app t0
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   661
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   662
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   663
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   664
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   665
    and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   666
      if is_cons c then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   667
        if length tys = length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   668
        then case ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   669
         of [] => [(str o deresolv) c]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   670
          | [t] => [(str o deresolv) c, pr_term lhs vars BR t]
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   671
          | _ => [(str o deresolv) c, Pretty.enum "," "(" ")"
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   672
                    (map (pr_term lhs vars NOBR) ts)]
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   673
        else [pr_term lhs vars BR (CodeThingol.eta_expand app (length tys))]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   674
      else (str o deresolv) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   675
        :: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   676
    and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   677
      labelled_name is_cons lhs vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   678
    and pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   679
      | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   680
      | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   681
      | pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v]
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   682
    and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   683
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   684
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   685
            val (binds, t') = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   686
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   687
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   688
              |> pr_bind NOBR ((NONE, SOME pat), ty)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   689
              |>> (fn p => concat
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   690
                  [str "let", p, str "=", pr_term false vars NOBR t, str "in"])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   691
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   692
          in Pretty.chunks (ps @| pr_term false vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   693
      | pr_case vars fxy (((td, ty), b::bs), _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   694
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   695
            fun pr delim (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   696
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   697
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   698
              in concat [str delim, p, str "->", pr_term false vars' NOBR t] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   699
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   700
            (Pretty.enclose "(" ")" o single o brackify fxy) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   701
              str "match"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   702
              :: pr_term false vars NOBR td
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   703
              :: pr "with" b
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   704
              :: map (pr "|") bs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   705
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   706
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   707
      | pr_case vars fxy ((_, []), _) = str "failwith \"empty case\"";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   708
    fun pr_def (MLFuns (funns as funn :: funns')) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   709
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   710
            fun fish_parm _ (w as SOME _) = w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   711
              | fish_parm (IVar v) NONE = SOME v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   712
              | fish_parm _ NONE = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   713
            fun fillup_parm _ (_, SOME v) = v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   714
              | fillup_parm x (i, NONE) = x ^ string_of_int i;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   715
            fun fish_parms vars eqs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   716
              let
24294
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   717
                val fished1 = fold (map2 fish_parm) eqs (replicate (length (hd eqs)) NONE);
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   718
                val x = Name.variant (map_filter I fished1) "x";
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   719
                val fished2 = map_index (fillup_parm x) fished1;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   720
                val (fished3, _) = Name.variants fished2 Name.context;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   721
                val vars' = CodeName.intro_vars fished3 vars;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   722
              in map (CodeName.lookup_var vars') fished3 end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   723
            fun pr_eq (ts, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   724
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   725
                val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   726
                  (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   727
                    then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   728
                    ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   729
                val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   730
                  |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   731
                  |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   732
                      (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   733
              in concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   734
                (Pretty.block o Pretty.commas) (map (pr_term true vars NOBR) ts),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   735
                str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   736
                pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   737
              ] end;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   738
            fun pr_eqs name ty [] =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   739
                  let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   740
                    val n = (length o fst o CodeThingol.unfold_fun) ty;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   741
                    val exc_str =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   742
                      (ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   743
                  in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   744
                    concat (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   745
                      map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   746
                      @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   747
                      :: str "failwith"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   748
                      @@ str exc_str
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   749
                    )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   750
                  end
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   751
              | pr_eqs _ _ [(ts, t)] =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   752
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   753
                    val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   754
                      (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   755
                        then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   756
                        ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   757
                    val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   758
                      |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   759
                      |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   760
                          (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   761
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   762
                    concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   763
                      map (pr_term true vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   764
                      @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   765
                      @@ pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   766
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   767
                  end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   768
              | pr_eqs _ _ (eqs as (eq as ([_], _)) :: eqs') =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   769
                  Pretty.block (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   770
                    str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   771
                    :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   772
                    :: str "function"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   773
                    :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   774
                    :: pr_eq eq
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   775
                    :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   776
                          o single o pr_eq) eqs'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   777
                  )
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   778
              | pr_eqs _ _ (eqs as eq :: eqs') =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   779
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   780
                    val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   781
                      (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   782
                        then NONE else (SOME o NameSpace.base o deresolv) c)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   783
                        ((fold o CodeThingol.fold_constnames)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   784
                          (insert (op =)) (map snd eqs) []);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   785
                    val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   786
                      |> CodeName.intro_vars consts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   787
                    val dummy_parms = (map str o fish_parms vars o map fst) eqs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   788
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   789
                    Pretty.block (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   790
                      Pretty.breaks dummy_parms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   791
                      @ Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   792
                      :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   793
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   794
                      :: str "match"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   795
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   796
                      :: (Pretty.block o Pretty.commas) dummy_parms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   797
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   798
                      :: str "with"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   799
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   800
                      :: pr_eq eq
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   801
                      :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   802
                           o single o pr_eq) eqs'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   803
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   804
                  end;
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   805
            fun pr_funn definer (name, ((vs, ty), eqs)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   806
              concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   807
                str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   808
                :: (str o deresolv) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   809
                :: pr_tyvars (filter_out (null o snd) vs)
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   810
                @| pr_eqs name ty eqs
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   811
              );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   812
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   813
              (pr_funn "let rec" funn :: map (pr_funn "and") funns');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   814
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   815
     | pr_def (MLDatas (datas as (data :: datas'))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   816
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   817
            fun pr_co (co, []) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   818
                  str (deresolv co)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   819
              | pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   820
                  concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   821
                    str (deresolv co),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   822
                    str "of",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   823
                    Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   824
                  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   825
            fun pr_data definer (tyco, (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   826
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   827
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   828
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   829
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   830
                    @@ str "EMPTY_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   831
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   832
              | pr_data definer (tyco, (vs, cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   833
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   834
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   835
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   836
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   837
                    :: separate (str "|") (map pr_co cos)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   838
                  );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   839
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   840
              (pr_data "type" data :: map (pr_data "and") datas');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   841
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   842
     | pr_def (MLClass (class, (v, (superclasses, classparams)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   843
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   844
            val w = "_" ^ first_upper v;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   845
            fun pr_superclass_field (class, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   846
              (concat o map str) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   847
                deresolv classrel, ":", "'" ^ v, deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   848
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   849
            fun pr_classparam_field (classparam, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   850
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   851
                (str o deresolv) classparam, str ":", pr_typ NOBR ty
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   852
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   853
            fun pr_classparam_proj (classparam, _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   854
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   855
                str "let",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   856
                (str o deresolv) classparam,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   857
                str w,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   858
                str "=",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   859
                str (w ^ "." ^ deresolv classparam ^ ";;")
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   860
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   861
          in Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   862
            concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   863
              str ("type '" ^ v),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   864
              (str o deresolv) class,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   865
              str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   866
              Pretty.enum ";" "{" "};;" (
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   867
                map pr_superclass_field superclasses
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   868
                @ map pr_classparam_field classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   869
              )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   870
            ]
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   871
            :: map pr_classparam_proj classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   872
          ) end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   873
     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   874
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   875
            fun pr_superclass (_, (classrel, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   876
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   877
                (str o deresolv) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   878
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   879
                pr_dicts NOBR [DictConst dss]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   880
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   881
            fun pr_classparam_inst (classparam, c_inst) =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   882
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   883
                (str o deresolv) classparam,
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   884
                str "=",
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   885
                pr_app false init_syms NOBR (c_inst, [])
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   886
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   887
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   888
            concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   889
              str "let"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   890
              :: (str o deresolv) inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   891
              :: pr_tyvars arity
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   892
              @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   893
              @@ (Pretty.enclose "(" ");;" o Pretty.breaks) [
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   894
                Pretty.enum ";" "{" "}" (map pr_superclass superarities
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   895
                  @ map pr_classparam_inst classparam_insts),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   896
                str ":",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   897
                pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   898
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   899
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   900
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   901
  in pr_def ml_def end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   902
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   903
fun pr_ocaml_modl name content =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   904
  Pretty.chunks ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   905
    str ("module " ^ name ^ " = "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   906
    str "struct",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   907
    str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   908
  ] @ content @ [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   909
    str "",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   910
    str ("end;; (*struct " ^ name ^ "*)")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   911
  ]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   912
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   913
val code_width = ref 80;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   914
fun code_output p = Pretty.setmp_margin (!code_width) Pretty.output p ^ "\n";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   915
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   916
fun seri_ml pr_def pr_modl module output labelled_name reserved_syms includes raw_module_alias
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   917
  (_ : string -> class_syntax option) tyco_syntax const_syntax code =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   918
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   919
    val module_alias = if is_some module then K module else raw_module_alias;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   920
    val is_cons = CodeThingol.is_cons code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   921
    datatype node =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   922
        Def of string * ml_def option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   923
      | Module of string * ((Name.context * Name.context) * node Graph.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   924
    val init_names = Name.make_context reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   925
    val init_module = ((init_names, init_names), Graph.empty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   926
    fun map_node [] f = f
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   927
      | map_node (m::ms) f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   928
          Graph.default_node (m, Module (m, init_module))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   929
          #> Graph.map_node m (fn (Module (dmodlname, (nsp, nodes))) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   930
               Module (dmodlname, (nsp, map_node ms f nodes)));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   931
    fun map_nsp_yield [] f (nsp, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   932
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   933
            val (x, nsp') = f nsp
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   934
          in (x, (nsp', nodes)) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   935
      | map_nsp_yield (m::ms) f (nsp, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   936
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   937
            val (x, nodes') =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   938
              nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   939
              |> Graph.default_node (m, Module (m, init_module))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   940
              |> Graph.map_node_yield m (fn Module (dmodlname, nsp_nodes) => 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   941
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   942
                    val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   943
                  in (x, Module (dmodlname, nsp_nodes')) end)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   944
          in (x, (nsp, nodes')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   945
    val init_syms = CodeName.make_vars reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   946
    val name_modl = mk_modl_name_tab init_names NONE module_alias code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   947
    fun name_def upper name nsp =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   948
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   949
        val (_, base) = dest_name name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   950
        val base' = if upper then first_upper base else base;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   951
        val ([base''], nsp') = Name.variants [base'] nsp;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   952
      in (base'', nsp') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   953
    fun map_nsp_fun f (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   954
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   955
        val (x, nsp_fun') = f nsp_fun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   956
      in (x, (nsp_fun', nsp_typ)) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   957
    fun map_nsp_typ f (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   958
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   959
        val (x, nsp_typ') = f nsp_typ
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   960
      in (x, (nsp_fun, nsp_typ')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   961
    fun mk_funs defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   962
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   963
        (fn (name, CodeThingol.Fun info) =>
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   964
              map_nsp_fun (name_def false name) >>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   965
                (fn base => (base, (name, (apsnd o map) fst info)))
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   966
          | (name, def) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   967
              error ("Function block containing illegal definition: " ^ labelled_name name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   968
        ) defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   969
      >> (split_list #> apsnd MLFuns);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   970
    fun mk_datatype defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   971
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   972
        (fn (name, CodeThingol.Datatype info) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   973
              map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   974
          | (name, CodeThingol.Datatypecons _) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   975
              map_nsp_fun (name_def true name) >> (fn base => (base, NONE))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   976
          | (name, def) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   977
              error ("Datatype block containing illegal definition: " ^ labelled_name name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   978
        ) defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   979
      >> (split_list #> apsnd (map_filter I
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   980
        #> (fn [] => error ("Datatype block without data definition: "
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   981
                  ^ (commas o map (labelled_name o fst)) defs)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   982
             | infos => MLDatas infos)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   983
    fun mk_class defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   984
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   985
        (fn (name, CodeThingol.Class info) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   986
              map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   987
          | (name, CodeThingol.Classrel _) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   988
              map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   989
          | (name, CodeThingol.Classparam _) =>
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   990
              map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   991
          | (name, def) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   992
              error ("Class block containing illegal definition: " ^ labelled_name name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   993
        ) defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   994
      >> (split_list #> apsnd (map_filter I
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   995
        #> (fn [] => error ("Class block without class definition: "
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   996
                  ^ (commas o map (labelled_name o fst)) defs)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   997
             | [info] => MLClass info)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   998
    fun mk_inst [(name, CodeThingol.Classinst info)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   999
      map_nsp_fun (name_def false name)
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1000
      >> (fn base => ([base], MLClassinst (name, (apsnd o apsnd o map) fst info)));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1001
    fun add_group mk defs nsp_nodes =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1002
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1003
        val names as (name :: names') = map fst defs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1004
        val deps =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1005
          []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1006
          |> fold (fold (insert (op =)) o Graph.imm_succs code) names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1007
          |> subtract (op =) names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1008
        val (modls, _) = (split_list o map dest_name) names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1009
        val modl' = (the_single o distinct (op =) o map name_modl) modls
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1010
          handle Empty =>
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1011
            error ("Different namespace prefixes for mutual dependencies:\n"
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1012
              ^ commas (map labelled_name names)
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1013
              ^ "\n"
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1014
              ^ commas (map (NameSpace.qualifier o NameSpace.qualifier) names));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1015
        val modl_explode = NameSpace.explode modl';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1016
        fun add_dep name name'' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1017
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1018
            val modl'' = (name_modl o fst o dest_name) name'';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1019
          in if modl' = modl'' then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1020
            map_node modl_explode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1021
              (Graph.add_edge (name, name''))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1022
          else let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1023
            val (common, (diff1::_, diff2::_)) = chop_prefix (op =)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1024
              (modl_explode, NameSpace.explode modl'');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1025
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1026
            map_node common
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1027
              (fn gr => Graph.add_edge_acyclic (diff1, diff2) gr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1028
                handle Graph.CYCLES _ => error ("Dependency "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1029
                  ^ quote name ^ " -> " ^ quote name''
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1030
                  ^ " would result in module dependency cycle"))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1031
          end end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1032
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1033
        nsp_nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1034
        |> map_nsp_yield modl_explode (mk defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1035
        |-> (fn (base' :: bases', def') =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1036
           apsnd (map_node modl_explode (Graph.new_node (name, (Def (base', SOME def')))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1037
              #> fold2 (fn name' => fn base' =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1038
                   Graph.new_node (name', (Def (base', NONE)))) names' bases')))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1039
        |> apsnd (fold (fn name => fold (add_dep name) deps) names)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1040
        |> apsnd (fold (map_node modl_explode o Graph.add_edge) (product names names))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1041
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1042
    fun group_defs [(_, CodeThingol.Bot)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1043
          I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1044
      | group_defs ((defs as (_, CodeThingol.Fun _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1045
          add_group mk_funs defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1046
      | group_defs ((defs as (_, CodeThingol.Datatypecons _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1047
          add_group mk_datatype defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1048
      | group_defs ((defs as (_, CodeThingol.Datatype _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1049
          add_group mk_datatype defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1050
      | group_defs ((defs as (_, CodeThingol.Class _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1051
          add_group mk_class defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1052
      | group_defs ((defs as (_, CodeThingol.Classrel _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1053
          add_group mk_class defs
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1054
      | group_defs ((defs as (_, CodeThingol.Classparam _)::_)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1055
          add_group mk_class defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1056
      | group_defs ((defs as [(_, CodeThingol.Classinst _)])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1057
          add_group mk_inst defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1058
      | group_defs defs = error ("Illegal mutual dependencies: " ^
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1059
          (commas o map (labelled_name o fst)) defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1060
    val (_, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1061
      init_module
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1062
      |> fold group_defs (map (AList.make (Graph.get_node code))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1063
          (rev (Graph.strong_conn code)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1064
    fun deresolver prefix name = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1065
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1066
        val modl = (fst o dest_name) name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1067
        val modl' = (NameSpace.explode o name_modl) modl;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1068
        val (_, (_, remainder)) = chop_prefix (op =) (prefix, modl');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1069
        val defname' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1070
          nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1071
          |> fold (fn m => fn g => case Graph.get_node g m
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1072
              of Module (_, (_, g)) => g) modl'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1073
          |> (fn g => case Graph.get_node g name of Def (defname, _) => defname);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1074
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1075
        NameSpace.implode (remainder @ [defname'])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1076
      end handle Graph.UNDEF _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1077
        error ("Unknown definition name: " ^ labelled_name name);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1078
    fun pr_node prefix (Def (_, NONE)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1079
          NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1080
      | pr_node prefix (Def (_, SOME def)) =
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1081
          SOME (pr_def tyco_syntax const_syntax labelled_name init_syms
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1082
            (deresolver prefix) is_cons def)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1083
      | pr_node prefix (Module (dmodlname, (_, nodes))) =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1084
          SOME (pr_modl dmodlname (
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1085
            separate (str "")
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1086
                ((map_filter (pr_node (prefix @ [dmodlname]) o Graph.get_node nodes)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1087
                o rev o flat o Graph.strong_conn) nodes)));
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1088
    val p = Pretty.chunks (separate (str "") (map snd includes @ (map_filter
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1089
      (pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1090
  in output p end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1091
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1092
val eval_verbose = ref false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1093
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1094
fun isar_seri_sml module file =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1095
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1096
    val output = case file
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1097
     of NONE => use_text "generated code" Output.ml_output (!eval_verbose) o code_output
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
  1098
      | SOME "-" => PrintMode.setmp [] writeln o code_output
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1099
      | SOME file => File.write (Path.explode file) o code_output;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1100
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1101
    parse_args (Scan.succeed ())
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1102
    #> (fn () => seri_ml pr_sml pr_sml_modl module output)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1103
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1104
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1105
fun isar_seri_ocaml module file =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1106
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1107
    val output = case file
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1108
     of NONE => error "OCaml: no internal compilation"
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
  1109
      | SOME "-" => PrintMode.setmp [] writeln o code_output
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1110
      | SOME file => File.write (Path.explode file) o code_output;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1111
    fun output_file file = File.write (Path.explode file) o code_output;
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
  1112
    val output_diag = PrintMode.setmp [] writeln o code_output;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1113
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1114
    parse_args (Scan.succeed ())
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1115
    #> (fn () => seri_ml pr_ocaml pr_ocaml_modl module output)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1116
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1117
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1118
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1119
(** Haskell serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1120
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1121
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1122
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1123
fun pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1124
  | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1125
  | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1126
  | pr_bind' ((SOME v, SOME p), _) = brackets [str v, str "@", p]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1127
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1128
val pr_bind_haskell = gen_pr_bind pr_bind';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1129
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1130
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1131
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1132
fun pr_haskell class_syntax tyco_syntax const_syntax labelled_name
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1133
    init_syms deresolv_here deresolv is_cons deriving_show def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1134
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1135
    fun class_name class = case class_syntax class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1136
     of NONE => deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1137
      | SOME (class, _) => class;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1138
    fun classparam_name class classparam = case class_syntax class
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1139
     of NONE => deresolv_here classparam
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1140
      | SOME (_, classparam_syntax) => case classparam_syntax classparam
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1141
         of NONE => (snd o dest_name) classparam
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1142
          | SOME classparam => classparam
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1143
    fun pr_typparms tyvars vs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1144
      case maps (fn (v, sort) => map (pair v) sort) vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1145
       of [] => str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1146
        | xs => Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1147
            Pretty.enum "," "(" ")" (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1148
              map (fn (v, class) => str
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1149
                (class_name class ^ " " ^ CodeName.lookup_var tyvars v)) xs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1150
            ),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1151
            str " => "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1152
          ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1153
    fun pr_tycoexpr tyvars fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1154
      brackify fxy (str tyco :: map (pr_typ tyvars BR) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1155
    and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1156
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1157
           of NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1158
                pr_tycoexpr tyvars fxy (deresolv tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1159
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1160
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1161
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1162
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1163
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1164
                else pr (pr_typ tyvars) fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1165
      | pr_typ tyvars fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1166
          (str o CodeName.lookup_var tyvars) v;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1167
    fun pr_typscheme_expr tyvars (vs, tycoexpr) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1168
      Pretty.block (pr_typparms tyvars vs @@ pr_tycoexpr tyvars NOBR tycoexpr);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1169
    fun pr_typscheme tyvars (vs, ty) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1170
      Pretty.block (pr_typparms tyvars vs @@ pr_typ tyvars NOBR ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1171
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1172
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1173
      | pr_term lhs vars fxy (t as (t1 `$ t2)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1174
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1175
           of SOME app => pr_app lhs vars fxy app
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1176
            | _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1177
                brackify fxy [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1178
                  pr_term lhs vars NOBR t1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1179
                  pr_term lhs vars BR t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1180
                ])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1181
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1182
          (str o CodeName.lookup_var vars) v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1183
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1184
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1185
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1186
            fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1187
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1188
          in brackets (str "\\" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1189
      | pr_term lhs vars fxy (ICase (cases as (_, t0))) =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1190
          (case CodeThingol.unfold_const_app t0
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1191
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1192
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1193
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1194
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1195
    and pr_app' lhs vars ((c, _), ts) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1196
      (str o deresolv) c :: map (pr_term lhs vars BR) ts
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1197
    and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1198
      labelled_name is_cons lhs vars
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
  1199
    and pr_bind fxy = pr_bind_haskell pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1200
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1201
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1202
            val (binds, t) = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1203
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1204
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1205
              |> pr_bind BR ((NONE, SOME pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1206
              |>> (fn p => semicolon [p, str "=", pr_term false vars NOBR t])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1207
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1208
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1209
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1210
              str "let {",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1211
              concat [str "}", str "in", pr_term false vars' NOBR t]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1212
            ) ps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1213
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1214
      | pr_case vars fxy (((td, ty), bs as _ :: _), _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1215
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1216
            fun pr (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1217
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1218
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1219
              in semicolon [p, str "->", pr_term false vars' NOBR t] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1220
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1221
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1222
              concat [str "(case", pr_term false vars NOBR td, str "of", str "{"],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1223
              str "})"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1224
            ) (map pr bs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1225
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1226
      | pr_case vars fxy ((_, []), _) = str "error \"empty case\"";
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1227
    fun pr_def (name, CodeThingol.Fun ((vs, ty), [])) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1228
          let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1229
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1230
            val n = (length o fst o CodeThingol.unfold_fun) ty;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1231
          in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1232
            Pretty.chunks [
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1233
              Pretty.block [
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1234
                (str o suffix " ::" o deresolv_here) name,
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1235
                Pretty.brk 1,
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1236
                pr_typscheme tyvars (vs, ty),
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1237
                str ";"
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1238
              ],
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1239
              concat (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1240
                (str o deresolv_here) name
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1241
                :: map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1242
                @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1243
                :: str "error"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1244
                @@ (str o (fn s => s ^ ";") o ML_Syntax.print_string
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1245
                    o NameSpace.base o NameSpace.qualifier) name
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1246
              )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1247
            ]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1248
          end
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1249
      | pr_def (name, CodeThingol.Fun ((vs, ty), eqs)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1250
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1251
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1252
            fun pr_eq ((ts, t), _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1253
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1254
                val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1255
                  (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1256
                    then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1257
                    ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1258
                val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1259
                  |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1260
                  |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1261
                       (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1262
              in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1263
                semicolon (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1264
                  (str o deresolv_here) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1265
                  :: map (pr_term true vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1266
                  @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1267
                  @@ pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1268
                )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1269
              end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1270
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1271
            Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1272
              Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1273
                (str o suffix " ::" o deresolv_here) name,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1274
                Pretty.brk 1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1275
                pr_typscheme tyvars (vs, ty),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1276
                str ";"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1277
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1278
              :: map pr_eq eqs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1279
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1280
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1281
      | pr_def (name, CodeThingol.Datatype (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1282
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1283
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1284
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1285
            semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1286
              str "data",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1287
              pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1288
            ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1289
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1290
      | pr_def (name, CodeThingol.Datatype (vs, [(co, [ty])])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1291
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1292
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1293
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1294
            semicolon (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1295
              str "newtype"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1296
              :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1297
              :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1298
              :: (str o deresolv_here) co
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1299
              :: pr_typ tyvars BR ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1300
              :: (if deriving_show name then [str "deriving (Read, Show)"] else [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1301
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1302
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1303
      | pr_def (name, CodeThingol.Datatype (vs, co :: cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1304
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1305
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1306
            fun pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1307
              concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1308
                (str o deresolv_here) co
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1309
                :: map (pr_typ tyvars BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1310
              )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1311
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1312
            semicolon (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1313
              str "data"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1314
              :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1315
              :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1316
              :: pr_co co
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1317
              :: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1318
              @ (if deriving_show name then [str "deriving (Read, Show)"] else [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1319
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1320
          end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1321
      | pr_def (name, CodeThingol.Class (v, (superclasses, classparams))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1322
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1323
            val tyvars = CodeName.intro_vars [v] init_syms;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1324
            fun pr_classparam (classparam, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1325
              semicolon [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1326
                (str o classparam_name name) classparam,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1327
                str "::",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1328
                pr_typ tyvars NOBR ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1329
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1330
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1331
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1332
              Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1333
                str "class ",
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1334
                pr_typparms tyvars [(v, map fst superclasses)],
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1335
                str (deresolv_here name ^ " " ^ CodeName.lookup_var tyvars v),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1336
                str " where {"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1337
              ],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1338
              str "};"
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1339
            ) (map pr_classparam classparams)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1340
          end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1341
      | pr_def (_, CodeThingol.Classinst ((class, (tyco, vs)), (_, classparam_insts))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1342
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1343
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1344
            fun pr_instdef ((classparam, c_inst), _) =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1345
              semicolon [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1346
                (str o classparam_name class) classparam,
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1347
                str "=",
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1348
                pr_app false init_syms NOBR (c_inst, [])
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1349
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1350
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1351
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1352
              Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1353
                str "instance ",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1354
                pr_typparms tyvars vs,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1355
                str (class_name class ^ " "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1356
                pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1357
                str " where {"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1358
              ],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1359
              str "};"
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1360
            ) (map pr_instdef classparam_insts)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1361
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1362
  in pr_def def end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1363
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1364
fun pretty_haskell_monad c_bind =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1365
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1366
    fun pretty pr vars fxy [(t, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1367
      let
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
  1368
        val pr_bind = pr_bind_haskell (K pr);
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1369
        fun pr_monad (NONE, t) vars =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1370
              (semicolon [pr vars NOBR t], vars)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1371
          | pr_monad (SOME (bind, true), t) vars = vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1372
              |> pr_bind NOBR bind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1373
              |>> (fn p => semicolon [p, str "<-", pr vars NOBR t])
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1374
          | pr_monad (SOME (bind, false), t) vars = vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1375
              |> pr_bind NOBR bind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1376
              |>> (fn p => semicolon [str "let", p, str "=", pr vars NOBR t]);
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1377
        val (binds, t) = implode_monad c_bind t;
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1378
        val (ps, vars') = fold_map pr_monad binds vars;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1379
        fun brack p = if eval_fxy BR fxy then Pretty.block [str "(", p, str ")"] else p;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1380
      in (brack o Pretty.block_enclose (str "do {", str "}")) (ps @| pr vars' NOBR t) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1381
  in (1, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1382
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1383
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1384
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1385
fun seri_haskell module_prefix module destination string_classes labelled_name
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1386
    reserved_syms includes raw_module_alias
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1387
    class_syntax tyco_syntax const_syntax code =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1388
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1389
    val _ = Option.map File.check destination;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1390
    val is_cons = CodeThingol.is_cons code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1391
    val module_alias = if is_some module then K module else raw_module_alias;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1392
    val init_names = Name.make_context reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1393
    val name_modl = mk_modl_name_tab init_names module_prefix module_alias code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1394
    fun add_def (name, (def, deps)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1395
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1396
        val (modl, base) = dest_name name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1397
        fun name_def base = Name.variants [base] #>> the_single;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1398
        fun add_fun upper (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1399
          let
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1400
            val (base', nsp_fun') =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1401
              name_def (if upper then first_upper base else base) nsp_fun
24219