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