src/Tools/code/code_target.ML
author wenzelm
Wed, 26 Sep 2007 19:18:01 +0200
changeset 24726 fcf13a91cda2
parent 24702 f875049a13a1
child 24750 95a315591af8
permissions -rw-r--r--
Attrib.eval_thms; Syntax.parse/check;
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
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
    35
    -> (theory -> string -> string) -> (string -> bool) -> string list option
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
    36
    -> CodeThingol.code -> unit;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
  val assert_serializer: theory -> string -> string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
  val eval_verbose: bool ref;
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
    40
  val eval_invoke: theory -> (theory -> string -> string) -> (string -> bool)
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
    41
    -> (string * (unit -> 'a) option ref) -> CodeThingol.code
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
    42
    -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
  val code_width: int ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
  val setup: theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
structure CodeTarget : CODE_TARGET =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
open BasicCodeThingol;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
(** basics **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
infixr 5 @@;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
infixr 5 @|;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
fun x @@ y = [x, y];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
fun xs @| y = xs @ [y];
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
    59
val str = PrintMode.setmp [] Pretty.str;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
val concat = Pretty.block o Pretty.breaks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
fun semicolon ps = Pretty.block [concat ps, str ";"];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
(** syntax **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
datatype lrx = L | R | X;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
datatype fixity =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
    BR
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
  | NOBR
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
  | INFX of (int * lrx);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    74
val APP = INFX (~1, L);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
fun eval_lrx L L = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
  | eval_lrx R R = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
  | eval_lrx _ _ = true;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
fun eval_fxy NOBR NOBR = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
  | eval_fxy BR NOBR = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
  | eval_fxy NOBR BR = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
  | eval_fxy (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
      pr < pr_ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
      orelse pr = pr_ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
        andalso eval_lrx lr lr_ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
      orelse pr_ctxt = ~1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
  | eval_fxy _ (INFX _) = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
  | eval_fxy (INFX _) NOBR = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
  | eval_fxy _ _ = true;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
fun gen_brackify _ [p] = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
  | gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
  | gen_brackify false (ps as _::_) = Pretty.block ps;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
fun brackify fxy_ctxt ps =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
  gen_brackify (eval_fxy BR fxy_ctxt) (Pretty.breaks ps);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
fun brackify_infix infx fxy_ctxt ps =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
  gen_brackify (eval_fxy (INFX infx) fxy_ctxt) (Pretty.breaks ps);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
type class_syntax = string * (string -> string option);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
type typ_syntax = int * ((fixity -> itype -> Pretty.T)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
  -> fixity -> itype list -> Pretty.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
type term_syntax = int * ((CodeName.var_ctxt -> fixity -> iterm -> Pretty.T)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
  -> CodeName.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
(* user-defined syntax *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
datatype 'a mixfix =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
    Arg of fixity
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
  | Pretty of Pretty.T;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
fun mk_mixfix prep_arg (fixity_this, mfx) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   117
    fun is_arg (Arg _) = true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   118
      | is_arg _ = false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
    val i = (length o filter is_arg) mfx;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   120
    fun fillin _ [] [] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   121
          []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
      | fillin pr (Arg fxy :: mfx) (a :: args) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   123
          (pr fxy o prep_arg) a :: fillin pr mfx args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
      | fillin pr (Pretty p :: mfx) args =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
          p :: fillin pr mfx args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
      | fillin _ [] _ =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
          error ("Inconsistent mixfix: too many arguments")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
      | fillin _ _ [] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
          error ("Inconsistent mixfix: too less arguments");
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   130
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   131
    (i, fn pr => fn fixity_ctxt => fn args =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
      gen_brackify (eval_fxy fixity_this fixity_ctxt) (fillin pr mfx args))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
fun parse_infix prep_arg (x, i) s =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
    val l = case x of L => INFX (i, L) | _ => INFX (i, X);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
    val r = case x of R => INFX (i, R) | _ => INFX (i, X);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
    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
   141
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   142
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   143
fun parse_mixfix prep_arg s =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   144
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
    val sym_any = Scan.one Symbol.is_regular;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
    val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
         ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
      || ($$ "_" >> K (Arg BR))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
      || ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
      || (Scan.repeat1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   151
           (   $$ "'" |-- sym_any
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
            || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
                 sym_any) >> (Pretty o str o implode)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
  in case Scan.finite Symbol.stopper parse (Symbol.explode s)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
   of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
    | ((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
   157
    | _ => Scan.!! (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   158
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
fun parse_args f args =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
  case Scan.read Args.stopper f args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
   of SOME x => x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
    | NONE => error "Bad serializer arguments";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
(* generic serializer combinators *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   167
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
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
   169
      lhs vars fxy (app as ((c, (_, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
  case const_syntax c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
   of NONE => if lhs andalso not (is_cons c) then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
          error ("non-constructor on left hand side of equation: " ^ labelled_name c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
        else brackify fxy (pr_app' lhs vars app)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
    | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
        let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
          val k = if i < 0 then length tys else i;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
          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
   178
        in if k = length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
          then pr' fxy ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
        else if k < length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
          then case chop k ts of (ts1, ts2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
            brackify fxy (pr' APP ts1 :: map (pr_term lhs vars BR) ts2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
          else pr_term lhs vars fxy (CodeThingol.eta_expand app k)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
        end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
fun gen_pr_bind pr_bind' pr_term fxy ((v, pat), ty) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
    val vs = case pat
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
     of SOME pat => CodeThingol.fold_varnames (insert (op =)) pat []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
      | NONE => [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
    val vars' = CodeName.intro_vars (the_list v) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
    val vars'' = CodeName.intro_vars vs vars';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
    val v' = Option.map (CodeName.lookup_var vars') v;
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   194
    val pat' = Option.map (pr_term true vars'' fxy) pat;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
  in (pr_bind' ((v', pat'), ty), vars'') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   196
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   197
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
(* list, char, string, numeral and monad abstract syntax transformations *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
fun implode_list c_nil c_cons t =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
    fun dest_cons (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
          if c = c_cons
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
          then SOME (t1, t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
      | dest_cons _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
    val (ts, t') = CodeThingol.unfoldr dest_cons t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
  in case t'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
   of IConst (c, _) => if c = c_nil then SOME ts else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
    | _ => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   212
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   213
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
        fun idx c = find_index (curry (op =) c) c_nibbles;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
        fun decode ~1 _ = NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
          | decode _ ~1 = NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
          | decode n m = SOME (chr (n * 16 + m));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
      in decode (idx c1) (idx c2) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
  | decode_char _ _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
fun implode_string c_char c_nibbles mk_char mk_string ts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
    fun implode_char (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
          if c = c_char then decode_char c_nibbles (t1, t2) else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   226
      | implode_char _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
    val ts' = map implode_char ts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
  in if forall is_some ts'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
    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
   230
    else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   231
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
fun implode_numeral c_bit0 c_bit1 c_pls c_min c_bit =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
    fun dest_bit (IConst (c, _)) = if c = c_bit0 then SOME 0
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   236
          else if c = c_bit1 then SOME 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   237
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   238
      | dest_bit _ = NONE;
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
   239
    fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
   240
          else if c = c_min then SOME ~1
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   241
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   242
      | dest_numeral (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   243
          if c = c_bit then case (dest_numeral t1, dest_bit t2)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
   244
           of (SOME n, SOME b) => SOME (2 * n + b)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
            | _ => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   246
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   247
      | dest_numeral _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   248
  in dest_numeral end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   249
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   250
fun implode_monad c_mbind c_kbind t =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   251
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   252
    fun dest_monad (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   253
          if c = c_mbind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   254
            then case CodeThingol.split_abs t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   255
             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
   256
              | NONE => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   257
          else if c = c_kbind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   258
            then SOME ((NONE, t1), t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
            else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
      | dest_monad t = case CodeThingol.split_let t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   261
           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
   262
            | NONE => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   263
  in CodeThingol.unfoldr dest_monad t end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   264
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
(** name auxiliary **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
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
   269
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
   270
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
val dest_name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
  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
   273
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
fun mk_modl_name_tab init_names prefix module_alias code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
    fun nsp_map f = NameSpace.explode #> map f #> NameSpace.implode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
    fun mk_alias name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
     case module_alias name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   279
      of SOME name' => name'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
       | NONE => nsp_map (fn name => (the_single o fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
            (Name.variants [name] init_names)) name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
    fun mk_prefix name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
      case prefix
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   284
       of SOME prefix => NameSpace.append prefix name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
        | NONE => name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
    val tab =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
      Symtab.empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   288
      |> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
           o fst o dest_name o fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
             code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   291
  in fn name => (the o Symtab.lookup tab) name end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
(** SML/OCaml serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   296
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
datatype ml_def =
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   298
    MLFuns of (string * (typscheme * (iterm list * iterm) list)) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   299
  | MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   300
  | MLClass of string * ((class * string) list * (vname * (string * itype) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   301
  | MLClassinst of string * ((class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   302
        * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   303
      * (string * const) list));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
   305
fun pr_sml allows_exception tyco_syntax const_syntax labelled_name init_syms deresolv is_cons ml_def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   307
    val pr_label_classrel = translate_string (fn "." => "__" | c => c) o NameSpace.qualifier;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
    val pr_label_classop = NameSpace.base o NameSpace.qualifier;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   309
    fun pr_dicts fxy ds =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
        fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
          | pr_dictvar (v, (i, _)) = first_upper v ^ string_of_int (i+1) ^ "_";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
        fun pr_proj [] p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
              p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
          | pr_proj [p'] p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   316
              brackets [p', p]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
          | pr_proj (ps as _ :: _) p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
              brackets [Pretty.enum " o" "(" ")" ps, p];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
        fun pr_dictc fxy (DictConst (inst, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
              brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   321
          | pr_dictc fxy (DictVar (classrels, v)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
              pr_proj (map (str o deresolv) classrels) ((str o pr_dictvar) v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
      in case ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
       of [] => str "()"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
        | [d] => pr_dictc fxy d
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
        | _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
    fun pr_tyvars vs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
      vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
      |> 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
   331
      |> map (pr_dicts BR);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
    fun pr_tycoexpr fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   333
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
        val tyco' = (str o deresolv) tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   335
      in case map (pr_typ BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   336
       of [] => tyco'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   337
        | [p] => Pretty.block [p, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
        | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   340
    and pr_typ fxy (tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   341
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
           of NONE => pr_tycoexpr fxy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   343
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   346
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   347
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   348
                else pr pr_typ fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   349
      | pr_typ fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   350
          str ("'" ^ v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   351
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   352
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   353
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   354
          str (CodeName.lookup_var vars v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   355
      | pr_term lhs vars fxy (t as t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   356
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   357
           of SOME c_ts => pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   358
            | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   359
                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
   360
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   361
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   362
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   363
            fun pr ((v, pat), ty) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   364
              pr_bind NOBR ((SOME v, pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   365
              #>> (fn p => concat [str "fn", p, str "=>"]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   366
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   367
          in brackets (ps @ [pr_term lhs vars' NOBR t']) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   368
      | 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
   369
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   370
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   371
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   372
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   373
    and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   374
      if is_cons c then let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   375
        val k = length tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   376
      in if k < 2 then 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   377
        (str o deresolv) c :: map (pr_term lhs vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   378
      else if k = length ts then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   379
        [(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term lhs vars NOBR) ts)]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   380
      else [pr_term lhs vars BR (CodeThingol.eta_expand app k)] end else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   381
        (str o deresolv) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   382
          :: (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
   383
    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
   384
    and pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   385
      | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   386
      | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   387
      | pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p]
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   388
    and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   389
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   390
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   391
            val (binds, t') = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   392
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   393
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   394
              |> pr_bind NOBR ((NONE, SOME pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   395
              |>> (fn p => semicolon [str "val", p, str "=", pr_term false vars NOBR t])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   396
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   397
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   398
            Pretty.chunks [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   399
              [str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   400
              [str ("in"), Pretty.fbrk, pr_term false vars' NOBR t'] |> Pretty.block,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   401
              str ("end")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   402
            ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   403
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   404
      | pr_case vars fxy (((td, ty), b::bs), _) =
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
            fun pr delim (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   407
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   408
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   409
              in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   410
                concat [str delim, p, str "=>", pr_term false vars' NOBR t]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   411
              end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   412
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   413
            (Pretty.enclose "(" ")" o single o brackify fxy) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   414
              str "case"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   415
              :: pr_term false vars NOBR td
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   416
              :: pr "of" b
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   417
              :: map (pr "|") bs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   418
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   419
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   420
      | pr_case vars fxy ((_, []), _) = str "raise Fail \"empty case\""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   421
    fun pr_def (MLFuns (funns as (funn :: funns'))) =
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
            val definer =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   424
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   425
                fun mk [] [] = "val"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   426
                  | mk (_::_) _ = "fun"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   427
                  | 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
   428
                fun chk (_, ((vs, _), (ts, _) :: _)) NONE = SOME (mk ts vs)
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   429
                  | chk (_, ((vs, _), (ts, _) :: _)) (SOME defi) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   430
                      if defi = mk ts vs then SOME defi
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   431
                      else error ("Mixing simultaneous vals and funs not implemented: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   432
                        ^ commas (map (labelled_name o fst) funns));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   433
              in the (fold chk funns NONE) end;
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   434
            fun pr_funn definer (name, ((raw_vs, ty), eqs as eq :: eqs')) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   435
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   436
                val vs = filter_out (null o snd) raw_vs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   437
                val shift = if null eqs' then I else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   438
                  map (Pretty.block o single o Pretty.block o single);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   439
                fun pr_eq definer (ts, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   440
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   441
                    val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   442
                      (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   443
                        then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   444
                        ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   445
                    val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   446
                      |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   447
                      |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   448
                           (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   449
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   450
                    concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   451
                      [str definer, (str o deresolv) name]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   452
                      @ (if null ts andalso null vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   453
                         then [str ":", pr_typ NOBR ty]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   454
                         else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   455
                           pr_tyvars vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   456
                           @ map (pr_term true vars BR) ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   457
                   @ [str "=", pr_term false vars NOBR t]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   458
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   459
                  end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   460
              in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   461
                (Pretty.block o Pretty.fbreaks o shift) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   462
                  pr_eq definer eq
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   463
                  :: map (pr_eq "|") eqs'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   464
                )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   465
              end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   466
            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
   467
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   468
     | pr_def (MLDatas (datas as (data :: datas'))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   469
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   470
            fun pr_co (co, []) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   471
                  str (deresolv co)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   472
              | pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   473
                  concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   474
                    str (deresolv co),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   475
                    str "of",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   476
                    Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   477
                  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   478
            fun pr_data definer (tyco, (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   479
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   480
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   481
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   482
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   483
                    @@ str "EMPTY__" 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   484
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   485
              | pr_data definer (tyco, (vs, cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   486
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   487
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   488
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   489
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   490
                    :: separate (str "|") (map pr_co cos)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   491
                  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   492
            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
   493
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   494
     | pr_def (MLClass (class, (superclasses, (v, classops)))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   495
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   496
            val w = first_upper v ^ "_";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   497
            fun pr_superclass_field (class, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   498
              (concat o map str) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   499
                pr_label_classrel classrel, ":", "'" ^ v, deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   500
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   501
            fun pr_classop_field (classop, ty) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   502
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   503
                (str o pr_label_classop) classop, str ":", pr_typ NOBR ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   504
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   505
            fun pr_classop_proj (classop, _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   506
              semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   507
                str "fun",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   508
                (str o deresolv) classop,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   509
                Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   510
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   511
                str ("#" ^ pr_label_classop classop),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   512
                str w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   513
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   514
            fun pr_superclass_proj (_, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   515
              semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   516
                str "fun",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   517
                (str o deresolv) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   518
                Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   519
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   520
                str ("#" ^ pr_label_classrel classrel),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   521
                str w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   522
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   523
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   524
            Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   525
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   526
                str ("type '" ^ v),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   527
                (str o deresolv) class,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   528
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   529
                Pretty.enum "," "{" "};" (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   530
                  map pr_superclass_field superclasses @ map pr_classop_field classops
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   531
                )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   532
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   533
              :: map pr_superclass_proj superclasses
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   534
              @ map pr_classop_proj classops
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   535
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   536
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   537
     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   538
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   539
            fun pr_superclass (_, (classrel, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   540
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   541
                (str o pr_label_classrel) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   542
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   543
                pr_dicts NOBR [DictConst dss]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   544
              ];
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   545
            fun pr_classop (classop, c_inst) =
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   546
              concat [
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   547
                (str o pr_label_classop) classop,
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   548
                str "=",
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   549
                pr_app false init_syms NOBR (c_inst, [])
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   550
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   551
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   552
            semicolon ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   553
              str (if null arity then "val" else "fun"),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   554
              (str o deresolv) inst ] @
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   555
              pr_tyvars arity @ [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   556
              str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   557
              Pretty.enum "," "{" "}" (map pr_superclass superarities @ map pr_classop classop_defs),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   558
              str ":",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   559
              pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   560
            ])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   561
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   562
  in pr_def ml_def end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   563
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   564
fun pr_sml_modl name content =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   565
  Pretty.chunks ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   566
    str ("structure " ^ name ^ " = "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   567
    str "struct",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   568
    str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   569
  ] @ content @ [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   570
    str "",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   571
    str ("end; (*struct " ^ name ^ "*)")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   572
  ]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   573
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
   574
fun pr_ocaml allows_exception tyco_syntax const_syntax labelled_name
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
   575
    init_syms deresolv is_cons ml_def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   576
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   577
    fun pr_dicts fxy ds =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   578
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   579
        fun pr_dictvar (v, (_, 1)) = "_" ^ first_upper v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   580
          | pr_dictvar (v, (i, _)) = "_" ^ first_upper v ^ string_of_int (i+1);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   581
        fun pr_proj ps p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   582
          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
   583
        fun pr_dictc fxy (DictConst (inst, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   584
              brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   585
          | pr_dictc fxy (DictVar (classrels, v)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   586
              pr_proj (map deresolv classrels) ((str o pr_dictvar) v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   587
      in case ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   588
       of [] => str "()"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   589
        | [d] => pr_dictc fxy d
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   590
        | _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   591
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   592
    fun pr_tyvars vs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   593
      vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   594
      |> 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
   595
      |> map (pr_dicts BR);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   596
    fun pr_tycoexpr fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   597
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   598
        val tyco' = (str o deresolv) tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   599
      in case map (pr_typ BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   600
       of [] => tyco'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   601
        | [p] => Pretty.block [p, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   602
        | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   603
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   604
    and pr_typ fxy (tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   605
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   606
           of NONE => pr_tycoexpr fxy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   607
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   608
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   609
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   610
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   611
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   612
                else pr pr_typ fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   613
      | pr_typ fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   614
          str ("'" ^ v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   615
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   616
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   617
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   618
          str (CodeName.lookup_var vars v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   619
      | pr_term lhs vars fxy (t as t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   620
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   621
           of SOME c_ts => pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   622
            | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   623
                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
   624
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   625
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   626
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   627
            fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   628
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   629
          in brackets (str "fun" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   630
      | 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
   631
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   632
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   633
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   634
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   635
    and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   636
      if is_cons c then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   637
        if length tys = length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   638
        then case ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   639
         of [] => [(str o deresolv) c]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   640
          | [t] => [(str o deresolv) c, pr_term lhs vars BR t]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   641
          | _ => [(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term lhs vars NOBR) ts)]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   642
        else [pr_term lhs vars BR (CodeThingol.eta_expand app (length tys))]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   643
      else (str o deresolv) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   644
        :: ((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
   645
    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
   646
    and pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   647
      | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   648
      | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   649
      | pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v]
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   650
    and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   651
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   652
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   653
            val (binds, t') = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   654
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   655
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   656
              |> pr_bind NOBR ((NONE, SOME pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   657
              |>> (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
   658
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   659
          in Pretty.chunks (ps @| pr_term false vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   660
      | pr_case vars fxy (((td, ty), b::bs), _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   661
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   662
            fun pr delim (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   663
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   664
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   665
              in concat [str delim, p, str "->", pr_term false vars' NOBR t] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   666
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   667
            (Pretty.enclose "(" ")" o single o brackify fxy) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   668
              str "match"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   669
              :: pr_term false vars NOBR td
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   670
              :: pr "with" b
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   671
              :: map (pr "|") bs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   672
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   673
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   674
      | pr_case vars fxy ((_, []), _) = str "failwith \"empty case\"";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   675
    fun pr_def (MLFuns (funns as funn :: funns')) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   676
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   677
            fun fish_parm _ (w as SOME _) = w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   678
              | fish_parm (IVar v) NONE = SOME v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   679
              | fish_parm _ NONE = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   680
            fun fillup_parm _ (_, SOME v) = v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   681
              | fillup_parm x (i, NONE) = x ^ string_of_int i;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   682
            fun fish_parms vars eqs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   683
              let
24294
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   684
                val fished1 = fold (map2 fish_parm) eqs (replicate (length (hd eqs)) NONE);
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   685
                val x = Name.variant (map_filter I fished1) "x";
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   686
                val fished2 = map_index (fillup_parm x) fished1;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   687
                val (fished3, _) = Name.variants fished2 Name.context;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   688
                val vars' = CodeName.intro_vars fished3 vars;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   689
              in map (CodeName.lookup_var vars') fished3 end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   690
            fun pr_eq (ts, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   691
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   692
                val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   693
                  (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   694
                    then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   695
                    ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   696
                val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   697
                  |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   698
                  |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   699
                      (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   700
              in concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   701
                (Pretty.block o Pretty.commas) (map (pr_term true vars NOBR) ts),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   702
                str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   703
                pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   704
              ] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   705
            fun pr_eqs [(ts, t)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   706
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   707
                    val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   708
                      (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   709
                        then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   710
                        ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   711
                    val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   712
                      |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   713
                      |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   714
                          (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   715
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   716
                    concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   717
                      map (pr_term true vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   718
                      @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   719
                      @@ pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   720
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   721
                  end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   722
              | pr_eqs (eqs as (eq as ([_], _)) :: eqs') =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   723
                  Pretty.block (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   724
                    str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   725
                    :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   726
                    :: str "function"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   727
                    :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   728
                    :: pr_eq eq
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   729
                    :: 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
   730
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   731
              | pr_eqs (eqs as eq :: eqs') =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   732
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   733
                    val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   734
                      (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   735
                        then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   736
                        ((fold o CodeThingol.fold_constnames) (insert (op =)) (map snd eqs) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   737
                    val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   738
                      |> CodeName.intro_vars consts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   739
                    val dummy_parms = (map str o fish_parms vars o map fst) eqs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   740
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   741
                    Pretty.block (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   742
                      Pretty.breaks dummy_parms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   743
                      @ Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   744
                      :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   745
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   746
                      :: str "match"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   747
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   748
                      :: (Pretty.block o Pretty.commas) dummy_parms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   749
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   750
                      :: str "with"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   751
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   752
                      :: pr_eq eq
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   753
                      :: 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
   754
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   755
                  end;
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   756
            fun pr_funn definer (name, ((vs, ty), eqs)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   757
              concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   758
                str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   759
                :: (str o deresolv) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   760
                :: pr_tyvars (filter_out (null o snd) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   761
                @| pr_eqs eqs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   762
              );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   763
            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
   764
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   765
     | pr_def (MLDatas (datas as (data :: datas'))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   766
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   767
            fun pr_co (co, []) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   768
                  str (deresolv co)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   769
              | pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   770
                  concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   771
                    str (deresolv co),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   772
                    str "of",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   773
                    Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   774
                  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   775
            fun pr_data definer (tyco, (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   776
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   777
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   778
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   779
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   780
                    @@ str "EMPTY_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   781
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   782
              | pr_data definer (tyco, (vs, cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   783
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   784
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   785
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   786
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   787
                    :: separate (str "|") (map pr_co cos)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   788
                  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   789
            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
   790
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   791
     | pr_def (MLClass (class, (superclasses, (v, classops)))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   792
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   793
            val w = "_" ^ first_upper v;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   794
            fun pr_superclass_field (class, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   795
              (concat o map str) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   796
                deresolv classrel, ":", "'" ^ v, deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   797
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   798
            fun pr_classop_field (classop, ty) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   799
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   800
                (str o deresolv) classop, str ":", pr_typ NOBR ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   801
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   802
            fun pr_classop_proj (classop, _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   803
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   804
                str "let",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   805
                (str o deresolv) classop,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   806
                str w,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   807
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   808
                str (w ^ "." ^ deresolv classop ^ ";;")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   809
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   810
          in Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   811
            concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   812
              str ("type '" ^ v),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   813
              (str o deresolv) class,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   814
              str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   815
              Pretty.enum ";" "{" "};;" (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   816
                map pr_superclass_field superclasses @ map pr_classop_field classops
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   817
              )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   818
            ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   819
            :: map pr_classop_proj classops
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   820
          ) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   821
     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   822
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   823
            fun pr_superclass (_, (classrel, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   824
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   825
                (str o deresolv) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   826
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   827
                pr_dicts NOBR [DictConst dss]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   828
              ];
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   829
            fun pr_classop_def (classop, c_inst) =
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   830
              concat [
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   831
                (str o deresolv) classop,
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   832
                str "=",
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   833
                pr_app false init_syms NOBR (c_inst, [])
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   834
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   835
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   836
            concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   837
              str "let"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   838
              :: (str o deresolv) inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   839
              :: pr_tyvars arity
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   840
              @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   841
              @@ (Pretty.enclose "(" ");;" o Pretty.breaks) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   842
                Pretty.enum ";" "{" "}" (map pr_superclass superarities @ map pr_classop_def classop_defs),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   843
                str ":",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   844
                pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   845
              ]
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 pr_def ml_def end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   849
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   850
fun pr_ocaml_modl name content =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   851
  Pretty.chunks ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   852
    str ("module " ^ name ^ " = "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   853
    str "struct",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   854
    str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   855
  ] @ content @ [
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
    str ("end;; (*struct " ^ name ^ "*)")
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
val code_width = ref 80;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   861
fun code_output p = Pretty.setmp_margin (!code_width) Pretty.output p ^ "\n";
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 seri_ml pr_def pr_modl module output labelled_name reserved_syms raw_module_alias module_prolog
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
   864
  allows_exception (_ : string -> class_syntax option) tyco_syntax const_syntax code =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   865
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   866
    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
   867
    val is_cons = CodeThingol.is_cons code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   868
    datatype node =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   869
        Def of string * ml_def option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   870
      | Module of string * ((Name.context * Name.context) * node Graph.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   871
    val init_names = Name.make_context reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   872
    val init_module = ((init_names, init_names), Graph.empty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   873
    fun map_node [] f = f
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   874
      | map_node (m::ms) f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   875
          Graph.default_node (m, Module (m, init_module))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   876
          #> 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
   877
    fun map_nsp_yield [] f (nsp, nodes) =
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 (x, nsp') = f nsp
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   880
          in (x, (nsp', nodes)) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   881
      | map_nsp_yield (m::ms) f (nsp, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   882
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   883
            val (x, nodes') =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   884
              nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   885
              |> Graph.default_node (m, Module (m, init_module))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   886
              |> Graph.map_node_yield m (fn Module (dmodlname, nsp_nodes) => 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   887
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   888
                    val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   889
                  in (x, Module (dmodlname, nsp_nodes')) end)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   890
          in (x, (nsp, nodes')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   891
    val init_syms = CodeName.make_vars reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   892
    val name_modl = mk_modl_name_tab init_names NONE module_alias code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   893
    fun name_def upper name nsp =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   894
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   895
        val (_, base) = dest_name name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   896
        val base' = if upper then first_upper base else base;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   897
        val ([base''], nsp') = Name.variants [base'] nsp;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   898
      in (base'', nsp') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   899
    fun map_nsp_fun f (nsp_fun, nsp_typ) =
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_fun') = f nsp_fun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   902
      in (x, (nsp_fun', nsp_typ)) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   903
    fun map_nsp_typ f (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   904
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   905
        val (x, nsp_typ') = f nsp_typ
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   906
      in (x, (nsp_fun, nsp_typ')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   907
    fun mk_funs defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   908
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   909
        (fn (name, CodeThingol.Fun info) =>
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   910
              map_nsp_fun (name_def false name) >> (fn base => (base, (name, (apsnd o map) fst info)))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   911
          | (name, def) => error ("Function block containing illegal definition: " ^ labelled_name name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   912
        ) defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   913
      >> (split_list #> apsnd MLFuns);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   914
    fun mk_datatype defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   915
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   916
        (fn (name, CodeThingol.Datatype info) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   917
              map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   918
          | (name, CodeThingol.Datatypecons _) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   919
              map_nsp_fun (name_def true name) >> (fn base => (base, NONE))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   920
          | (name, def) => error ("Datatype block containing illegal definition: " ^ labelled_name name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   921
        ) defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   922
      >> (split_list #> apsnd (map_filter I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   923
        #> (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
   924
             | infos => MLDatas infos)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   925
    fun mk_class defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   926
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   927
        (fn (name, CodeThingol.Class info) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   928
              map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   929
          | (name, CodeThingol.Classrel _) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   930
              map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   931
          | (name, CodeThingol.Classop _) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   932
              map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   933
          | (name, def) => error ("Class 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 ("Class block without class definition: " ^ (commas o map (labelled_name o fst)) defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   937
             | [info] => MLClass info)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   938
    fun mk_inst [(name, CodeThingol.Classinst info)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   939
      map_nsp_fun (name_def false name)
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   940
      >> (fn base => ([base], MLClassinst (name, (apsnd o apsnd o map) fst info)));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   941
    fun add_group mk defs nsp_nodes =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   942
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   943
        val names as (name :: names') = map fst defs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   944
        val deps =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   945
          []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   946
          |> fold (fold (insert (op =)) o Graph.imm_succs code) names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   947
          |> subtract (op =) names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   948
        val (modls, _) = (split_list o map dest_name) names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   949
        val modl' = (the_single o distinct (op =) o map name_modl) modls
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   950
          handle Empty =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   951
            error ("Illegal mutual dependencies: " ^ commas (map labelled_name names));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   952
        val modl_explode = NameSpace.explode modl';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   953
        fun add_dep name name'' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   954
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   955
            val modl'' = (name_modl o fst o dest_name) name'';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   956
          in if modl' = modl'' then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   957
            map_node modl_explode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   958
              (Graph.add_edge (name, name''))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   959
          else let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   960
            val (common, (diff1::_, diff2::_)) = chop_prefix (op =)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   961
              (modl_explode, NameSpace.explode modl'');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   962
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   963
            map_node common
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   964
              (fn gr => Graph.add_edge_acyclic (diff1, diff2) gr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   965
                handle Graph.CYCLES _ => error ("Dependency "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   966
                  ^ quote name ^ " -> " ^ quote name''
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   967
                  ^ " would result in module dependency cycle"))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   968
          end end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   969
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   970
        nsp_nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   971
        |> map_nsp_yield modl_explode (mk defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   972
        |-> (fn (base' :: bases', def') =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   973
           apsnd (map_node modl_explode (Graph.new_node (name, (Def (base', SOME def')))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   974
              #> 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
   975
        |> apsnd (fold (fn name => fold (add_dep name) deps) names)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   976
        |> apsnd (fold (map_node modl_explode o Graph.add_edge) (product names names))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   977
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   978
    fun group_defs [(_, CodeThingol.Bot)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   979
          I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   980
      | group_defs ((defs as (_, CodeThingol.Fun _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   981
          add_group mk_funs defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   982
      | group_defs ((defs as (_, CodeThingol.Datatypecons _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   983
          add_group mk_datatype defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   984
      | group_defs ((defs as (_, CodeThingol.Datatype _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   985
          add_group mk_datatype defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   986
      | group_defs ((defs as (_, CodeThingol.Class _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   987
          add_group mk_class defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   988
      | group_defs ((defs as (_, CodeThingol.Classrel _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   989
          add_group mk_class defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   990
      | group_defs ((defs as (_, CodeThingol.Classop _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   991
          add_group mk_class defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   992
      | group_defs ((defs as [(_, CodeThingol.Classinst _)])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   993
          add_group mk_inst defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   994
      | group_defs defs = error ("Illegal mutual dependencies: " ^
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   995
          (commas o map (labelled_name o fst)) defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   996
    val (_, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   997
      init_module
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   998
      |> fold group_defs (map (AList.make (Graph.get_node code))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   999
          (rev (Graph.strong_conn code)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1000
    fun deresolver prefix name = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1001
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1002
        val modl = (fst o dest_name) name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1003
        val modl' = (NameSpace.explode o name_modl) modl;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1004
        val (_, (_, remainder)) = chop_prefix (op =) (prefix, modl');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1005
        val defname' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1006
          nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1007
          |> fold (fn m => fn g => case Graph.get_node g m
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1008
              of Module (_, (_, g)) => g) modl'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1009
          |> (fn g => case Graph.get_node g name of Def (defname, _) => defname);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1010
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1011
        NameSpace.implode (remainder @ [defname'])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1012
      end handle Graph.UNDEF _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1013
        error ("Unknown definition name: " ^ labelled_name name);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1014
    fun the_prolog modlname = case module_prolog modlname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1015
     of NONE => []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1016
      | SOME p => [p, str ""];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1017
    fun pr_node prefix (Def (_, NONE)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1018
          NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1019
      | pr_node prefix (Def (_, SOME def)) =
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1020
          SOME (pr_def allows_exception tyco_syntax const_syntax labelled_name init_syms
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1021
            (deresolver prefix) is_cons def)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1022
      | pr_node prefix (Module (dmodlname, (_, nodes))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1023
          SOME (pr_modl dmodlname (the_prolog (NameSpace.implode (prefix @ [dmodlname]))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1024
            @ separate (str "") ((map_filter (pr_node (prefix @ [dmodlname]) o Graph.get_node nodes)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1025
                o rev o flat o Graph.strong_conn) nodes)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1026
    val p = Pretty.chunks (the_prolog "" @ separate (str "") ((map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1027
      (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
  1028
  in output p end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1029
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1030
val eval_verbose = ref false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1031
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1032
fun isar_seri_sml module file =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1033
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1034
    val output = case file
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1035
     of NONE => use_text "generated code" Output.ml_output (!eval_verbose) o code_output
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
  1036
      | SOME "-" => PrintMode.setmp [] writeln o code_output
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1037
      | SOME file => File.write (Path.explode file) o code_output;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1038
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1039
    parse_args (Scan.succeed ())
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1040
    #> (fn () => seri_ml pr_sml pr_sml_modl module output)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1041
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1042
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1043
fun isar_seri_ocaml module file =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1044
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1045
    val output = case file
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1046
     of NONE => error "OCaml: no internal compilation"
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
  1047
      | SOME "-" => PrintMode.setmp [] writeln o code_output
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1048
      | SOME file => File.write (Path.explode file) o code_output;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1049
    fun output_file file = File.write (Path.explode file) o code_output;
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
  1050
    val output_diag = PrintMode.setmp [] writeln o code_output;
24219
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_ocaml pr_ocaml_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
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1057
(** Haskell serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1058
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1059
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1060
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1061
fun pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1062
  | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1063
  | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1064
  | pr_bind' ((SOME v, SOME p), _) = brackets [str v, str "@", p]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1065
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1066
val pr_bind_haskell = gen_pr_bind pr_bind';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1067
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1068
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1069
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1070
fun pr_haskell allows_exception class_syntax tyco_syntax const_syntax labelled_name
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1071
    init_syms deresolv_here deresolv is_cons deriving_show def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1072
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1073
    fun class_name class = case class_syntax class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1074
     of NONE => deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1075
      | SOME (class, _) => class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1076
    fun classop_name class classop = case class_syntax class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1077
     of NONE => deresolv_here classop
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1078
      | SOME (_, classop_syntax) => case classop_syntax classop
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1079
         of NONE => (snd o dest_name) classop
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1080
          | SOME classop => classop
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1081
    fun pr_typparms tyvars vs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1082
      case maps (fn (v, sort) => map (pair v) sort) vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1083
       of [] => str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1084
        | xs => Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1085
            Pretty.enum "," "(" ")" (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1086
              map (fn (v, class) => str
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1087
                (class_name class ^ " " ^ CodeName.lookup_var tyvars v)) xs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1088
            ),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1089
            str " => "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1090
          ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1091
    fun pr_tycoexpr tyvars fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1092
      brackify fxy (str tyco :: map (pr_typ tyvars BR) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1093
    and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1094
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1095
           of NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1096
                pr_tycoexpr tyvars fxy (deresolv tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1097
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1098
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1099
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1100
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1101
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1102
                else pr (pr_typ tyvars) fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1103
      | pr_typ tyvars fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1104
          (str o CodeName.lookup_var tyvars) v;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1105
    fun pr_typscheme_expr tyvars (vs, tycoexpr) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1106
      Pretty.block (pr_typparms tyvars vs @@ pr_tycoexpr tyvars NOBR tycoexpr);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1107
    fun pr_typscheme tyvars (vs, ty) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1108
      Pretty.block (pr_typparms tyvars vs @@ pr_typ tyvars NOBR ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1109
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1110
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1111
      | pr_term lhs vars fxy (t as (t1 `$ t2)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1112
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1113
           of SOME app => pr_app lhs vars fxy app
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1114
            | _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1115
                brackify fxy [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1116
                  pr_term lhs vars NOBR t1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1117
                  pr_term lhs vars BR t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1118
                ])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1119
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1120
          (str o CodeName.lookup_var vars) v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1121
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1122
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1123
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1124
            fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1125
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1126
          in brackets (str "\\" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1127
      | 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
  1128
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1129
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1130
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1131
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1132
    and pr_app' lhs vars ((c, _), ts) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1133
      (str o deresolv) c :: map (pr_term lhs vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1134
    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
  1135
    and pr_bind fxy = pr_bind_haskell pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1136
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1137
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1138
            val (binds, t) = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1139
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1140
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1141
              |> pr_bind BR ((NONE, SOME pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1142
              |>> (fn p => semicolon [p, str "=", pr_term false vars NOBR t])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1143
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1144
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1145
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1146
              str "let {",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1147
              concat [str "}", str "in", pr_term false vars' NOBR t]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1148
            ) ps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1149
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1150
      | pr_case vars fxy (((td, ty), bs as _ :: _), _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1151
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1152
            fun pr (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1153
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1154
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1155
              in semicolon [p, str "->", pr_term false vars' NOBR t] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1156
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1157
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1158
              concat [str "(case", pr_term false vars NOBR td, str "of", str "{"],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1159
              str "})"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1160
            ) (map pr bs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1161
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1162
      | pr_case vars fxy ((_, []), _) = str "error \"empty case\"";
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
  1163
    fun pr_def (name, CodeThingol.Fun ((vs, ty), eqs)) =
24219
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
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1166
            fun pr_eq ((ts, t), _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1167
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1168
                val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1169
                  (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1170
                    then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1171
                    ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1172
                val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1173
                  |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1174
                  |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1175
                       (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1176
              in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1177
                semicolon (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1178
                  (str o deresolv_here) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1179
                  :: map (pr_term true vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1180
                  @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1181
                  @@ pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1182
                )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1183
              end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1184
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1185
            Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1186
              Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1187
                (str o suffix " ::" o deresolv_here) name,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1188
                Pretty.brk 1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1189
                pr_typscheme tyvars (vs, ty),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1190
                str ";"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1191
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1192
              :: map pr_eq eqs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1193
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1194
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1195
      | pr_def (name, CodeThingol.Datatype (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1196
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1197
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1198
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1199
            semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1200
              str "data",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1201
              pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1202
            ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1203
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1204
      | pr_def (name, CodeThingol.Datatype (vs, [(co, [ty])])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1205
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1206
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1207
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1208
            semicolon (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1209
              str "newtype"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1210
              :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1211
              :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1212
              :: (str o deresolv_here) co
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1213
              :: pr_typ tyvars BR ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1214
              :: (if deriving_show name then [str "deriving (Read, Show)"] else [])
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 :: cos)) =
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
            fun pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1221
              concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1222
                (str o deresolv_here) co
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1223
                :: map (pr_typ tyvars BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1224
              )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1225
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1226
            semicolon (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1227
              str "data"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1228
              :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1229
              :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1230
              :: pr_co co
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1231
              :: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1232
              @ (if deriving_show name then [str "deriving (Read, Show)"] else [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1233
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1234
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1235
      | pr_def (name, CodeThingol.Class (superclasss, (v, classops))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1236
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1237
            val tyvars = CodeName.intro_vars [v] init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1238
            fun pr_classop (classop, ty) =
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 o classop_name name) classop,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1241
                str "::",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1242
                pr_typ tyvars NOBR ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1243
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1244
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1245
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1246
              Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1247
                str "class ",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1248
                pr_typparms tyvars [(v, map fst superclasss)],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1249
                str (deresolv_here name ^ " " ^ CodeName.lookup_var tyvars v),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1250
                str " where {"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1251
              ],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1252
              str "};"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1253
            ) (map pr_classop classops)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1254
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1255
      | pr_def (_, CodeThingol.Classinst ((class, (tyco, vs)), (_, classop_defs))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1256
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1257
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1258
            fun pr_instdef ((classop, c_inst), _) =
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1259
              semicolon [
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1260
                (str o classop_name class) classop,
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1261
                str "=",
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1262
                pr_app false init_syms NOBR (c_inst, [])
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1263
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1264
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1265
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1266
              Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1267
                str "instance ",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1268
                pr_typparms tyvars vs,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1269
                str (class_name class ^ " "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1270
                pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1271
                str " where {"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1272
              ],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1273
              str "};"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1274
            ) (map pr_instdef classop_defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1275
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1276
  in pr_def def end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1277
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1278
fun pretty_haskell_monad c_mbind c_kbind =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1279
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1280
    fun pretty pr vars fxy [(t, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1281
      let
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
  1282
        val pr_bind = pr_bind_haskell (K pr);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1283
        fun pr_mbind (NONE, t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1284
              (semicolon [pr vars NOBR t], vars)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1285
          | pr_mbind (SOME (bind, true), t) vars = vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1286
              |> pr_bind NOBR bind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1287
              |>> (fn p => semicolon [p, str "<-", pr vars NOBR t])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1288
          | pr_mbind (SOME (bind, false), t) vars = vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1289
              |> pr_bind NOBR bind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1290
              |>> (fn p => semicolon [str "let", p, str "=", pr vars NOBR t]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1291
        val (binds, t) = implode_monad c_mbind c_kbind t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1292
        val (ps, vars') = fold_map pr_mbind binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1293
        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
  1294
      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
  1295
  in (1, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1296
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1297
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1298
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1299
fun seri_haskell module_prefix module destination string_classes labelled_name
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1300
    reserved_syms raw_module_alias module_prolog
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1301
    allows_exception class_syntax tyco_syntax const_syntax code =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1302
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1303
    val _ = Option.map File.check destination;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1304
    val is_cons = CodeThingol.is_cons code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1305
    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
  1306
    val init_names = Name.make_context reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1307
    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
  1308
    fun add_def (name, (def, deps)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1309
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1310
        val (modl, base) = dest_name name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1311
        fun name_def base = Name.variants [base] #>> the_single;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1312
        fun add_fun upper (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1313
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1314
            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
  1315
          in (base', (nsp_fun', nsp_typ)) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1316
        fun add_typ (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1317
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1318
            val (base', nsp_typ') = name_def (first_upper base) nsp_typ
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1319
          in (base', (nsp_fun, nsp_typ')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1320
        val add_name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1321
          case def
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1322
           of CodeThingol.Bot => pair base
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1323
            | CodeThingol.Fun _ => add_fun false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1324
            | CodeThingol.Datatype _ => add_typ
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1325
            | CodeThingol.Datatypecons _ => add_fun true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1326
            | CodeThingol.Class _ => add_typ
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1327
            | CodeThingol.Classrel _ => pair base
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1328
            | CodeThingol.Classop _ => add_fun false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1329
            | CodeThingol.Classinst _ => pair base;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1330
        val modlname' = name_modl modl;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1331
        fun add_def base' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1332
          case def
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1333
           of CodeThingol.Bot => I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1334
            | CodeThingol.Datatypecons _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1335
                cons (name, ((NameSpace.append modlname' base', base'), NONE))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1336
            | CodeThingol.Classrel _ => I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1337
            | CodeThingol.Classop _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1338
                cons (name, ((NameSpace.append modlname' base', base'), NONE))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1339
            | _ => cons (name, ((NameSpace.append modlname' base', base'), SOME def));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1340
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1341
        Symtab.map_default (modlname', ([], ([], (init_names, init_names))))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1342
              (apfst (fold (insert (op = : string * string -> bool)) deps))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1343
        #> `(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
  1344
        #-> (fn (base', names) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1345
              (Symtab.map_entry modlname' o apsnd) (fn (defs, _) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1346
              (add_def base' defs, names)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1347
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1348
    val code' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1349
      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
  1350
        (Graph.strong_conn code |> flat)) Symtab.empty;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1351
    val init_syms = CodeName.make_vars reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1352
    fun deresolv name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1353
      (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
  1354
        o Symtab.lookup code') ((name_modl o fst o dest_name) name))) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1355
        handle Option => error ("Unknown definition name: " ^ labelled_name name);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1356
    fun deresolv_here name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1357
      (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
  1358
        o Symtab.lookup code') ((name_modl o fst o dest_name) name))) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1359
        handle Option => error ("Unknown definition name: " ^ labelled_name name);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1360
    fun deriving_show tyco =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1361
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1362
        fun deriv _ "fun" = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1363
          | deriv tycos tyco = member (op =) tycos tyco orelse
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1364
              case the_default CodeThingol.Bot (try (Graph.get_node code) tyco)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1365
               of CodeThingol.Bot => true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1366
                | CodeThingol.Datatype (_, cs) => forall (deriv' (tyco :: tycos))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1367
                    (maps snd cs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1368
        and deriv' tycos (tyco `%% tys) = deriv tycos tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1369
              andalso forall (deriv' tycos) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1370
          | deriv' _ (ITyVar _) = true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1371
      in deriv [] tyco end;
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1372
    fun seri_def qualified = pr_haskell allows_exception class_syntax tyco_syntax
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1373
      const_syntax labelled_name init_syms
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1374
      deresolv_here (if qualified then deresolv else deresolv_here) is_cons
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1375
      (if string_classes then deriving_show else K false);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1376
    fun write_module (SOME destination) modlname =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1377
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1378
            val filename = case modlname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1379
             of "" => Path.explode "Main.hs"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1380
              | _ => (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
  1381
            val pathname = Path.append destination filename;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1382
            val _ = File.mkdir (Path.dir pathname);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1383
          in File.write pathname end
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
  1384
      | write_module NONE _ = PrintMode.setmp [] writeln;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1385
    fun seri_module (modlname', (imports, (defs, _))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1386
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1387
        val imports' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1388
          imports
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1389
          |> map (name_modl o fst o dest_name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1390
          |> distinct (op =)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1391
          |> remove (op =) modlname';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1392
        val qualified =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1393
          imports @ map fst defs
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1394
          |> map_filter (try deresolv)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1395
          |> map NameSpace.base
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1396
          |> has_duplicates (op =);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1397
        val mk_import = str o (if qualified
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1398
          then prefix "import qualified "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1399
          else prefix "import ") o suffix ";";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1400
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1401
        Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1402
          str ("module " ^ modlname' ^ " where {")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1403
          :: str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1404
          :: map mk_import imports'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1405
          @ str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1406
          :: separate (str "") ((case module_prolog modlname'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1407
             of SOME prolog => [prolog]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1408
              | NONE => [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1409
          @ map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1410
            (fn (name, (_, SOME def)) => SOME (seri_def qualified (name, def))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1411
              | (_, (_, NONE)) => NONE) defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1412
          @ str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1413
          @@ str "}"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1414
        )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1415
        |> code_output
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1416
        |> write_module destination modlname'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1417
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1418
  in Symtab.fold (fn modl => fn () => seri_module modl) code' () end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1419
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1420
fun isar_seri_haskell module file =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1421
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1422
    val destination = case file
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1423
     of NONE => error ("Haskell: no internal compilation")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1424
      | SOME "-" => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1425
      | SOME file => SOME (Path.explode file)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1426
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1427
    parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1428
      -- Scan.optional (Args.$$$ "string_classes" >> K true) false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1429
      >> (fn (module_prefix, string_classes) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1430
        seri_haskell module_prefix module destination string_classes))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1431
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1432
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1433
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1434
(** diagnosis serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1435
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1436
fun seri_diagnosis labelled_name _ _ _ _ _ _ _ code =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1437
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1438
    val init_names = CodeName.make_vars [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1439
    fun pr_fun "fun" = SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1440
          brackify_infix (1, R) fxy [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1441
            pr_typ (INFX (1, X)) ty1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1442
            str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1443
            pr_typ (INFX (1, R)) ty2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1444
          ])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1445
      | pr_fun _ = NONE
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1446
    val pr = pr_haskell (K true) (K NONE) pr_fun (K NONE) labelled_name init_names I I (K false) (K false);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1447
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1448
    []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1449
    |> Graph.fold (fn (name, (def, _)) => case try pr (name, def) of SOME p => cons p | NONE => I) code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1450
    |> Pretty.chunks2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1451
    |> code_output
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
  1452
    |> PrintMode.setmp [] writeln
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1453
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1454
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1455
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1456
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1457
(** theory data **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1458
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1459
datatype syntax_expr = SyntaxExpr of {
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1460
  class: (string * (string -> string option)) Symtab.table,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1461
  inst: unit Symtab.table,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1462
  tyco: typ_syntax Symtab.table,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1463
  const: term_syntax Symtab.table
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1464
};
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1465
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1466
fun mk_syntax_expr ((class, inst), (tyco, const)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1467
  SyntaxExpr { class = class, inst = inst, tyco = tyco, const = const };
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1468
fun map_syntax_expr f (SyntaxExpr { class, inst, tyco, const }) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1469
  mk_syntax_expr (f ((class, inst), (tyco, const)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1470
fun merge_syntax_expr (SyntaxExpr { class = class1, inst = inst1, tyco = tyco1, const = const1 },
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1471
    SyntaxExpr { class = class2, inst = inst2, tyco = tyco2, const = const2 }) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1472
  mk_syntax_expr (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1473
    (Symtab.join (K snd) (class1, class2),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1474
       Symtab.join (K snd) (inst1, inst2)),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1475
    (Symtab.join (K snd) (tyco1, tyco2),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1476
       Symtab.join (K snd) (const1, const2))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1477
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1478
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1479
datatype syntax_modl = SyntaxModl of {
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1480
  alias: string Symtab.table,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1481
  prolog: Pretty.T Symtab.table
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1482
};
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1483
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1484
fun mk_syntax_modl (alias, prolog) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1485
  SyntaxModl { alias = alias, prolog = prolog };
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1486
fun map_syntax_modl f (SyntaxModl { alias, prolog }) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1487
  mk_syntax_modl (f (alias, prolog));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1488
fun merge_syntax_modl (SyntaxModl { alias = alias1, prolog = prolog1 },
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1489
    SyntaxModl { alias = alias2, prolog = prolog2 }) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1490
  mk_syntax_modl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1491
    Symtab.join (K snd) (alias1, alias2),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1492
    Symtab.join (K snd) (prolog1, prolog2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1493
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1494
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1495
type serializer =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1496
  string option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1497
  -> string option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1498
  -> Args.T list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1499
  -> (string -> string)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1500
  -> string list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1501
  -> (string -> string option)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1502
  -> (string -> Pretty.T option)
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1503
  -> (string -> bool)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1504
  -> (string -> class_syntax option)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1505
  -> (string -> typ_syntax option)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1506
  -> (string -> term_syntax option)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1507
  -> CodeThingol.code -> unit;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1508
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1509
datatype target = Target of {
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1510
  serial: serial,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1511
  serializer: serializer,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1512
  syntax_expr: syntax_expr,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1513
  syntax_modl: syntax_modl,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1514
  reserved: string list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1515
};
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1516
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1517
fun mk_target (serial, ((serializer, reserved), (syntax_expr, syntax_modl))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1518
  Target { serial = serial, reserved = reserved, serializer = serializer, syntax_expr = syntax_expr, syntax_modl = syntax_modl };
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1519
fun map_target f ( Target { serial, serializer, reserved, syntax_expr, syntax_modl } ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1520
  mk_target (f (serial, ((serializer, reserved), (syntax_expr, syntax_modl))));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1521
fun merge_target target (Target { serial = serial1, serializer = serializer, reserved = reserved1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1522
  syntax_expr = syntax_expr1, syntax_modl = syntax_modl1 },
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1523
    Target { serial = serial2, serializer = _, reserved = reserved2,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1524
      syntax_expr = syntax_expr2, syntax_modl = syntax_modl2 }) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1525
  if serial1 = serial2 then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1526
    mk_target (serial1, ((serializer, merge (op =) (reserved1, reserved2)),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1527
      (merge_syntax_expr (syntax_expr1, syntax_expr2),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1528
        merge_syntax_modl (syntax_modl1, syntax_modl2))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1529
    ))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1530
  else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1531
    error ("Incompatible serializers: " ^ quote target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1532
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1533
structure CodeTargetData = TheoryDataFun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1534
(
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1535
  type T = target Symtab.table;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1536
  val empty = Symtab.empty;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1537
  val copy = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1538
  val extend = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1539
  fun merge _ = Symtab.join merge_target;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1540
);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1541
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1542
fun the_serializer (Target { serializer, ... }) = serializer;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1543
fun the_reserved (Target { reserved, ... }) = reserved;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1544
fun the_syntax_expr (Target { syntax_expr = SyntaxExpr x, ... }) = x;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1545
fun the_syntax_modl (Target { syntax_modl = SyntaxModl x, ... }) = x;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1546
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1547
fun assert_serializer thy target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1548
  case Symtab.lookup (CodeTargetData.get thy) target
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1549
   of SOME data => target
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1550
    | NONE => error ("Unknown code target language: " ^ quote target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1551
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1552
fun add_serializer (target, seri) thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1553
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1554
    val _ = case Symtab.lookup (CodeTargetData.get thy) target
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1555
     of SOME _ => warning ("overwriting existing serializer " ^ quote target)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1556
      | NONE => ();
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1557
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1558
    thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1559
    |> (CodeTargetData.map oo Symtab.map_default)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1560
          (target, mk_target (serial (), ((seri, []),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1561
            (mk_syntax_expr ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1562
              mk_syntax_modl (Symtab.empty, Symtab.empty)))))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1563
          (map_target (fn (serial, ((_, keywords), syntax)) => (serial, ((seri, keywords), syntax))))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1564
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1565
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1566
fun map_seri_data target f thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1567
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1568
    val _ = assert_serializer thy target;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1569
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1570
    thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1571
    |> (CodeTargetData.map o Symtab.map_entry target o map_target) f
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1572
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1573
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1574
val target_SML = "SML";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1575
val target_OCaml = "OCaml";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1576
val target_Haskell = "Haskell";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1577
val target_diag = "diag";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1578
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1579
fun get_serializer thy target permissive module file args
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1580
    labelled_name allows_exception = fn cs =>
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1581
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1582
    val data = case Symtab.lookup (CodeTargetData.get thy) target
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1583
     of SOME data => data
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1584
      | NONE => error ("Unknown code target language: " ^ quote target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1585
    val seri = the_serializer data;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1586
    val reserved = the_reserved data;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1587
    val { alias, prolog } = the_syntax_modl data;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1588
    val { class, inst, tyco, const } = the_syntax_expr data;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1589
    val project = if target = target_diag then I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1590
      else CodeThingol.project_code permissive
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1591
        (Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const) cs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1592
    fun check_empty_funs code = case CodeThingol.empty_funs code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1593
     of [] => code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1594
      | names => error ("No defining equations for " ^ commas (map (labelled_name thy) names));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1595
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1596
    project
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1597
    #> check_empty_funs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1598
    #> seri module file args (labelled_name thy) reserved (Symtab.lookup alias) (Symtab.lookup prolog)
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1599
      allows_exception (Symtab.lookup class) (Symtab.lookup tyco) (Symtab.lookup const)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1600
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1601
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1602
fun eval_invoke thy labelled_name allows_exception (ref_name, reff) code (t, ty) args =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1603
  let
24662
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24634
diff changeset
  1604
    val _ = if CodeThingol.contains_dictvar t then
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24634
diff changeset
  1605
      error "Term to be evaluated constains free dictionaries" else ();
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1606
    val val_name = "Isabelle_Eval.EVAL.EVAL";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1607
    val val_name' = "Isabelle_Eval.EVAL";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1608
    val val_name'_args = space_implode " " (val_name' :: map (enclose "(" ")") args);
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1609
    val seri = get_serializer thy "SML" false (SOME "Isabelle_Eval") NONE []
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
  1610
      labelled_name allows_exception;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1611
    fun eval code = (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1612
      reff := NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1613
      seri (SOME [val_name]) code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1614
      use_text "generated code for evaluation" Output.ml_output (!eval_verbose)
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1615
        ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ val_name'_args ^ "))");
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1616
      case !reff
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1617
       of NONE => error ("Could not retrieve value of ML reference " ^ quote ref_name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1618
            ^ " (reference probably has been shadowed)")
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1619
        | SOME f => f ()
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1620
      );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1621
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1622
    code
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
  1623
    |> CodeThingol.add_eval_def (val_name, (t, ty))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1624
    |> eval
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1625
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1626
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1627
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1628
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1629
(** optional pretty serialization **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1630
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1631
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1632
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1633
val pretty : (string * {
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1634
    pretty_char: string -> string,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1635
    pretty_string: string -> string,
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1636
    pretty_numeral: bool -> int -> string,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1637
    pretty_list: Pretty.T list -> Pretty.T,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1638
    infix_cons: int * string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1639
  }) list = [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1640
  ("SML", { pretty_char = prefix "#" o quote o ML_Syntax.print_char,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1641
      pretty_string = ML_Syntax.print_string,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1642
      pretty_numeral = fn unbounded => fn k =>
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1643
        if unbounded then "(" ^ string_of_int k ^ " : int)"
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1644
        else string_of_int k,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1645
      pretty_list = Pretty.enum "," "[" "]",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1646
      infix_cons = (7, "::")}),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1647
  ("OCaml", { pretty_char = fn c => enclose "'" "'"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1648
        (let val i = ord c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1649
          in if i < 32 orelse i = 39 orelse i = 92
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1650
            then prefix "\\" (string_of_int i)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1651
            else c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1652
          end),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1653
      pretty_string = (fn _ => error "OCaml: no pretty strings"),
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1654
      pretty_numeral = fn unbounded => fn k => if k >= 0 then
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1655
            if unbounded then
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1656
              "(Big_int.big_int_of_int " ^ string_of_int k ^ ")"
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1657
            else string_of_int k
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1658
          else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1659
            if unbounded then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1660
              "(Big_int.big_int_of_int " ^ (enclose "(" ")" o prefix "-"
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1661
                o string_of_int o op ~) k ^ ")"
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1662
            else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1663
      pretty_list = Pretty.enum ";" "[" "]",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1664
      infix_cons = (6, "::")}),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1665
  ("Haskell", { pretty_char = fn c => enclose "'" "'"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1666
        (let val i = ord c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1667
          in if i < 32 orelse i = 39 orelse i = 92
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1668
            then Library.prefix "\\" (string_of_int i)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1669
            else c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1670
          end),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1671
      pretty_string = ML_Syntax.print_string,
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1672
      pretty_numeral = fn unbounded => fn k => if k >= 0 then string_of_int k
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1673
          else enclose "(" ")" (signed_string_of_int k),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1674
      pretty_list = Pretty.enum "," "[" "]",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1675
      infix_cons = (5, ":")})
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1676
];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1677
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1678
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1679
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1680
fun pr_pretty target = case AList.lookup (op =) pretty target
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1681
 of SOME x => x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1682
  | NONE => error ("Unknown code target language: " ^ quote target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1683
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1684
fun default_list (target_fxy, target_cons) pr fxy t1 t2 =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1685
  brackify_infix (target_fxy, R) fxy [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1686
    pr (INFX (target_fxy, X)) t1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1687
    str target_cons,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1688
    pr (INFX (target_fxy, R)) t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1689
  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1690
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1691
fun pretty_list c_nil c_cons target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1692
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1693
    val pretty_ops = pr_pretty target;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1694
    val mk_list = #pretty_list pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1695
    fun pretty pr vars fxy [(t1, _), (t2, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1696
      case Option.map (cons t1) (implode_list c_nil c_cons t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1697
       of SOME ts => mk_list (map (pr vars NOBR) ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1698
        | NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1699
  in (2, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1700
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1701
fun pretty_list_string c_nil c_cons c_char c_nibbles target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1702
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1703
    val pretty_ops = pr_pretty target;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1704
    val mk_list = #pretty_list pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1705
    val mk_char = #pretty_char pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1706
    val mk_string = #pretty_string pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1707
    fun pretty pr vars fxy [(t1, _), (t2, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1708
      case Option.map (cons t1) (implode_list c_nil c_cons t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1709
       of SOME ts => case implode_string c_char c_nibbles mk_char mk_string ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1710
           of SOME p => p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1711
            | NONE => mk_list (map (pr vars NOBR) ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1712
        | NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1713
  in (2, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1714
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1715
fun pretty_char c_char c_nibbles target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1716
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1717
    val mk_char = #pretty_char (pr_pretty target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1718
    fun pretty _ _ _ [(t1, _), (t2, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1719
      case decode_char c_nibbles (t1, t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1720
       of SOME c => (str o mk_char) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1721
        | NONE => error "Illegal character expression";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1722
  in (2, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1723
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1724
fun pretty_numeral unbounded c_bit0 c_bit1 c_pls c_min c_bit target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1725
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1726
    val mk_numeral = #pretty_numeral (pr_pretty target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1727
    fun pretty _ _ _ [(t, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1728
      case implode_numeral c_bit0 c_bit1 c_pls c_min c_bit t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1729
       of SOME k => (str o mk_numeral unbounded) k
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1730
        | NONE => error "Illegal numeral expression";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1731
  in (1, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1732
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1733
fun pretty_ml_string c_char c_nibbles c_nil c_cons target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1734
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1735
    val pretty_ops = pr_pretty target;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1736
    val mk_char = #pretty_char pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1737
    val mk_string = #pretty_string pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1738
    fun pretty pr vars fxy [(t, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1739
      case implode_list c_nil c_cons t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1740
       of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1741
           of SOME p => p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1742
            | NONE => error "Illegal ml_string expression")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1743
        | NONE => error "Illegal ml_string expression";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1744
  in (1, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1745
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1746
fun pretty_imperative_monad_bind bind' =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1747
  let
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1748
    fun dest_abs ((v, ty) `|-> t, _) = ((v, ty), t)
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1749
      | dest_abs (t, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1750
          let
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1751
            val vs = CodeThingol.fold_varnames cons t [];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1752
            val v = Name.variant vs "x";
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1753
            val ty' = (hd o fst o CodeThingol.unfold_fun) ty;
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1754
          in ((v, ty'), t `$ IVar v) end;
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1755
    fun tr_bind [(t1, _), (t2, ty2)] =
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1756
      let
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1757
        val ((v, ty), t) = dest_abs (t2, ty2);
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1758
      in ICase (((t1, ty), [(IVar v, tr_bind' t)]), IVar "") end
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1759
    and tr_bind' (t as _ `$ _) = (case CodeThingol.unfold_app t
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1760
         of (IConst (c, (_, ty1 :: ty2 :: _)), [x1, x2]) => if c = bind'
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1761
              then tr_bind [(x1, ty1), (x2, ty2)]
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1762
              else t
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1763
          | _ => t)
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1764
      | tr_bind' t = t;
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1765
    fun pretty pr vars fxy ts = pr vars fxy (tr_bind ts);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1766
  in (2, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1767
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1768
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1769
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1770
(** ML and Isar interface **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1771
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1772
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1773
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1774
fun map_syntax_exprs target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1775
  map_seri_data target o apsnd o apsnd o apfst o map_syntax_expr;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1776
fun map_syntax_modls target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1777
  map_seri_data target o apsnd o apsnd o apsnd o map_syntax_modl;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1778
fun map_reserveds target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1779
  map_seri_data target o apsnd o apfst o apsnd;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1780
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1781
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1782
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1783
    val cls = prep_class thy raw_class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1784
    val class = CodeName.class thy cls;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1785
    fun mk_classop c = case AxClass.class_of_param thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1786
     of SOME class' => if cls = class' then CodeName.const thy c
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1787
          else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1788
      | NONE => error ("Not a class operation: " ^ quote c);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1789
    fun mk_syntax_ops raw_ops = AList.lookup (op =)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1790
      ((map o apfst) (mk_classop o prep_const thy) raw_ops);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1791
  in case raw_syn
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1792
   of SOME (syntax, raw_ops) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1793
      thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1794
      |> (map_syntax_exprs target o apfst o apfst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1795
           (Symtab.update (class, (syntax, mk_syntax_ops raw_ops)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1796
    | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1797
      thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1798
      |> (map_syntax_exprs target o apfst o apfst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1799
           (Symtab.delete_safe class)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1800
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1801
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1802
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1803
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1804
    val inst = CodeName.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1805
  in if add_del then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1806
    thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1807
    |> (map_syntax_exprs target o apfst o apsnd)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1808
        (Symtab.update (inst, ()))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1809
  else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1810
    thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1811
    |> (map_syntax_exprs target o apfst o apsnd)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1812
        (Symtab.delete_safe inst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1813
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1814
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1815
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1816
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1817
    val tyco = prep_tyco thy raw_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1818
    val tyco' = if tyco = "fun" then "fun" else CodeName.tyco thy tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1819
    fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1820
      then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1821
      else syntax
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1822
  in case raw_syn
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1823
   of SOME syntax =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1824
      thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1825
      |> (map_syntax_exprs target o apsnd o apfst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1826
           (Symtab.update (tyco', check_args syntax))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1827
   | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1828
      thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1829
      |> (map_syntax_exprs target o apsnd o apfst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1830
           (Symtab.delete_safe tyco')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1831
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1832
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1833
fun gen_add_syntax_const prep_const target raw_c raw_syn thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1834
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1835
    val c = prep_const thy raw_c;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1836
    val c' = CodeName.const thy c;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1837
    fun check_args (syntax as (n, _)) = if n > CodeUnit.no_args thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1838
      then error ("Too many arguments in syntax for constant " ^ quote c)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1839
      else syntax;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1840
  in case raw_syn
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1841
   of SOME syntax =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1842
      thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1843
      |> (map_syntax_exprs target o apsnd o apsnd)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1844
           (Symtab.update (c', check_args syntax))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1845
   | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1846
      thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1847
      |> (map_syntax_exprs target o apsnd o apsnd)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1848
           (Symtab.delete_safe c')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1849
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1850
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1851
fun cert_class thy class =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1852
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1853
    val _ = AxClass.get_definition thy class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1854
  in class end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1855
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1856
fun read_class thy raw_class =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1857
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1858
    val class = Sign.intern_class thy raw_class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1859
    val _ = AxClass.get_definition thy class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1860
  in class end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1861
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1862
fun cert_tyco thy tyco =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1863
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1864
    val _ = if Sign.declared_tyname thy tyco then ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1865
      else error ("No such type constructor: " ^ quote tyco);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1866
  in tyco end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1867
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1868
fun read_tyco thy raw_tyco =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1869
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1870
    val tyco = Sign.intern_type thy raw_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1871
    val _ = if Sign.declared_tyname thy tyco then ()
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1872
      else error ("No such type constructor: " ^ quote raw_tyco);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1873
  in tyco end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1874
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1875
fun no_bindings x = (Option.map o apsnd)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1876
  (fn pretty => fn pr => fn vars => pretty (pr vars)) x;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1877
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1878
fun gen_add_haskell_monad prep_const c_run c_mbind c_kbind thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1879
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1880
    val c_run' = prep_const thy c_run;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1881
    val c_mbind' = prep_const thy c_mbind;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1882
    val c_mbind'' = CodeName.const thy c_mbind';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1883
    val c_kbind' = prep_const thy c_kbind;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1884
    val c_kbind'' = CodeName.const thy c_kbind';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1885
    val pr = pretty_haskell_monad c_mbind'' c_kbind''
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1886
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1887
    thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1888
    |> gen_add_syntax_const (K I) target_Haskell c_run' (SOME pr)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1889
    |> gen_add_syntax_const (K I) target_Haskell c_mbind'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1890
          (no_bindings (SOME (parse_infix fst (L, 1) ">>=")))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1891
    |> gen_add_syntax_const (K I) target_Haskell c_kbind'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1892
          (no_bindings (SOME (parse_infix fst (L, 1) ">>")))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1893
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1894
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1895
fun add_reserved target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1896
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1897
    fun add sym syms = if member (op =) syms sym
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1898
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1899
      else insert (op =) sym syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1900
  in map_reserveds target o add end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1901
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1902
fun add_modl_alias target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1903
  map_syntax_modls target o apfst o Symtab.update o apsnd CodeName.check_modulename;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1904
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1905
fun add_modl_prolog target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1906
  map_syntax_modls target o apsnd o
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1907
    (fn (modl, NONE) => Symtab.delete modl | (modl, SOME prolog) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1908
      Symtab.update (modl, Pretty.str prolog));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1909
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1910
fun zip_list (x::xs) f g =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1911
  f
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1912
  #-> (fn y =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1913
    fold_map (fn x => g |-- f >> pair x) xs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1914
    #-> (fn xys => pair ((x, y) :: xys)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1915
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1916
structure P = OuterParse
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1917
and K = OuterKeyword
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1918
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1919
fun parse_multi_syntax parse_thing parse_syntax =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1920
  P.and_list1 parse_thing
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1921
  #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1922
        (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1923
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1924
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr");
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1925
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1926
fun parse_syntax prep_arg xs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1927
  Scan.option ((
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1928
      ((P.$$$ infixK  >> K X)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1929
        || (P.$$$ infixlK >> K L)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1930
        || (P.$$$ infixrK >> K R))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1931
        -- P.nat >> parse_infix prep_arg
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1932
      || Scan.succeed (parse_mixfix prep_arg))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1933
      -- P.string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1934
      >> (fn (parse, s) => parse s)) xs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1935
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1936
val (code_classK, code_instanceK, code_typeK, code_constK, code_monadK,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1937
  code_reservedK, code_modulenameK, code_moduleprologK) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1938
  ("code_class", "code_instance", "code_type", "code_const", "code_monad",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1939
    "code_reserved", "code_modulename", "code_moduleprolog");
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1940
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1941
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1942
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1943
val parse_syntax = parse_syntax;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1944
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1945
val add_syntax_class = gen_add_syntax_class cert_class (K I);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1946
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1947
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1948
val add_syntax_const = gen_add_syntax_const (K I);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1949
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1950
val add_syntax_class_cmd = gen_add_syntax_class read_class CodeUnit.read_const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1951
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1952
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1953
val add_syntax_const_cmd = gen_add_syntax_const CodeUnit.read_const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1954
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1955
fun add_syntax_tycoP target tyco = parse_syntax I >> add_syntax_tyco_cmd target tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1956
fun add_syntax_constP target c = parse_syntax fst >> (add_syntax_const_cmd target c o no_bindings);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1957
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1958
fun add_undefined target undef target_undefined thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1959
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1960
    fun pr _ _ _ _ = str target_undefined;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1961
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1962
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1963
    |> add_syntax_const target undef (SOME (~1, pr))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1964
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1965
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1966
fun add_pretty_list target nill cons thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1967
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1968
    val nil' = CodeName.const thy nill;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1969
    val cons' = CodeName.const thy cons;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1970
    val pr = pretty_list nil' cons' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1971
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1972
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1973
    |> add_syntax_const target cons (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1974
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1975
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1976
fun add_pretty_list_string target nill cons charr nibbles thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1977
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1978
    val nil' = CodeName.const thy nill;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1979
    val cons' = CodeName.const thy cons;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1980
    val charr' = CodeName.const thy charr;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1981
    val nibbles' = map (CodeName.const thy) nibbles;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1982
    val pr = pretty_list_string nil' cons' charr' nibbles' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1983
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1984
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1985
    |> add_syntax_const target cons (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1986
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1987
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1988
fun add_pretty_char target charr nibbles thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1989
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1990
    val charr' = CodeName.const thy charr;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1991
    val nibbles' = map (CodeName.const thy) nibbles;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1992
    val pr = pretty_char charr' nibbles' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1993
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1994
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1995
    |> add_syntax_const target charr (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1996
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1997
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1998
fun add_pretty_numeral target unbounded number_of b0 b1 pls min bit thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1999
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2000
    val b0' = CodeName.const thy b0;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2001
    val b1' = CodeName.const thy b1;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2002
    val pls' = CodeName.const thy pls;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2003
    val min' = CodeName.const thy min;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2004
    val bit' = CodeName.const thy bit;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2005
    val pr = pretty_numeral unbounded b0' b1' pls' min' bit' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2006
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2007
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2008
    |> add_syntax_const target number_of (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2009
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2010
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2011
fun add_pretty_ml_string target charr nibbles nill cons str thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2012
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2013
    val charr' = CodeName.const thy charr;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2014
    val nibbles' = map (CodeName.const thy) nibbles;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2015
    val nil' = CodeName.const thy nill;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2016
    val cons' = CodeName.const thy cons;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2017
    val pr = pretty_ml_string charr' nibbles' nil' cons' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2018
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2019
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2020
    |> add_syntax_const target str (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2021
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2022
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2023
fun add_pretty_imperative_monad_bind target bind thy =
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  2024
  add_syntax_const target bind (SOME (pretty_imperative_monad_bind
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  2025
    (CodeName.const thy bind))) thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2026
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2027
val add_haskell_monad = gen_add_haskell_monad CodeUnit.read_const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2028
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2029
val code_classP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2030
  OuterSyntax.command code_classK "define code syntax for class" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2031
    parse_multi_syntax P.xname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2032
      (Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2033
        (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.string)) []))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2034
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2035
          fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2036
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2037
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2038
val code_instanceP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2039
  OuterSyntax.command code_instanceK "define code syntax for instance" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2040
    parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2041
      ((P.minus >> K true) || Scan.succeed false)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2042
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2043
          fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2044
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2045
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2046
val code_typeP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2047
  OuterSyntax.command code_typeK "define code syntax for type constructor" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2048
    parse_multi_syntax P.xname (parse_syntax I)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2049
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2050
          fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2051
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2052
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2053
val code_constP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2054
  OuterSyntax.command code_constK "define code syntax for constant" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2055
    parse_multi_syntax P.term (parse_syntax fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2056
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2057
          fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const (no_bindings syn)) syns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2058
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2059
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2060
val code_monadP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2061
  OuterSyntax.command code_monadK "define code syntax for Haskell monads" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2062
    P.term -- P.term -- P.term
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2063
    >> (fn ((raw_run, raw_mbind), raw_kbind) => Toplevel.theory 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2064
          (add_haskell_monad raw_run raw_mbind raw_kbind))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2065
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2066
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2067
val code_reservedP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2068
  OuterSyntax.command code_reservedK "declare words as reserved for target language" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2069
    P.name -- Scan.repeat1 P.name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2070
    >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2071
  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2072
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2073
val code_modulenameP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2074
  OuterSyntax.command code_modulenameK "alias module to other name" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2075
    P.name -- Scan.repeat1 (P.name -- P.name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2076
    >> (fn (target, modlnames) => (Toplevel.theory o fold (add_modl_alias target)) modlnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2077
  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2078
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2079
val code_moduleprologP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2080
  OuterSyntax.command code_moduleprologK "add prolog to module" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2081
    P.name -- Scan.repeat1 (P.name -- (P.text >> (fn "-" => NONE | s => SOME s)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2082
    >> (fn (target, prologs) => (Toplevel.theory o fold (add_modl_prolog target)) prologs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2083
  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2084
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2085
val _ = OuterSyntax.add_keywords [infixK, infixlK, infixrK];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2086
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2087
val _ = OuterSyntax.add_parsers [code_classP, code_instanceP, code_typeP, code_constP,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2088
  code_reservedP, code_modulenameP, code_moduleprologP, code_monadP];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2089
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2090
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2091
(*including serializer defaults*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2092
val setup =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2093
  add_serializer (target_SML, isar_seri_sml)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2094
  #> add_serializer (target_OCaml, isar_seri_ocaml)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2095
  #> add_serializer (target_Haskell, isar_seri_haskell)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2096
  #> add_serializer (target_diag, fn _ => fn _ => fn _ => seri_diagnosis)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2097
  #> add_syntax_tyco "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2098
      (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2099
        pr_typ (INFX (1, X)) ty1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2100
        str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2101
        pr_typ (INFX (1, R)) ty2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2102
      ]))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2103
  #> add_syntax_tyco "OCaml" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2104
      (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2105
        pr_typ (INFX (1, X)) ty1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2106
        str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2107
        pr_typ (INFX (1, R)) ty2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2108
      ]))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2109
  #> add_syntax_tyco "Haskell" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2110
      brackify_infix (1, R) fxy [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2111
        pr_typ (INFX (1, X)) ty1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2112
        str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2113
        pr_typ (INFX (1, R)) ty2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2114
      ]))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2115
  #> fold (add_reserved "SML") ML_Syntax.reserved_names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2116
  #> fold (add_reserved "SML")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2117
      ["o" (*dictionary projections use it already*), "Fail", "div", "mod" (*standard infixes*)]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2118
  #> fold (add_reserved "OCaml") [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2119
      "and", "as", "assert", "begin", "class",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2120
      "constraint", "do", "done", "downto", "else", "end", "exception",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2121
      "external", "false", "for", "fun", "function", "functor", "if",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2122
      "in", "include", "inherit", "initializer", "lazy", "let", "match", "method",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2123
      "module", "mutable", "new", "object", "of", "open", "or", "private", "rec",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2124
      "sig", "struct", "then", "to", "true", "try", "type", "val",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2125
      "virtual", "when", "while", "with"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2126
    ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2127
  #> fold (add_reserved "OCaml") ["failwith", "mod"]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2128
  #> fold (add_reserved "Haskell") [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2129
      "hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2130
      "import", "default", "forall", "let", "in", "class", "qualified", "data",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2131
      "newtype", "instance", "if", "then", "else", "type", "as", "do", "module"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2132
    ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2133
  #> fold (add_reserved "Haskell") [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2134
      "Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2135
      "Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2136
      "Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2137
      "AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2138
      "BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2139
      "Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2140
      "ExitSuccess", "False", "GT", "HeapOverflow",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2141
      "IOError", "IOException", "IllegalOperation",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2142
      "IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2143
      "NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2144
      "PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2145
      "RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2146
      "ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2147
      "UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2148
      "and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2149
      "atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2150
      "boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2151
      "catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2152
      "cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2153
      "doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2154
      "emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2155
      "enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2156
      "floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2157
      "floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2158
      "fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2159
      "fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2160
      "id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2161
      "isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2162
      "isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2163
      "last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2164
      "logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2165
      "maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2166
      "null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2167
      "numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2168
      "print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2169
      "quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2170
      "rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2171
      "readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2172
      "readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2173
      "realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2174
      "round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2175
      "sequence_", "show", "showChar", "showException", "showField", "showList",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2176
      "showLitChar", "showParen", "showString", "shows", "showsPrec", "significand",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2177
      "signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2178
      "succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2179
      "throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2180
      "undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2181
      "unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2182
    ] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2183
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2184
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2185
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2186
end; (*struct*)